Skip to content

Commit

Permalink
chore: add uuid store
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 15, 2024
1 parent 3ddeb1a commit 3cab6b9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const { createServer } = require('http')

const { API_URL, NODE_ENV, PORT } = require('./constant')

const server = createServer(require('.'))
const server = createServer((req, res) =>
require('./util/uuid').withUUID(() => require('.')(req, res))
)

server.listen(PORT, () => {
debug({
Expand Down Expand Up @@ -53,6 +55,7 @@ if (NODE_ENV === 'production') {
process.on('uncaughtException', error => {
debug.error('uncaughtException', {
message: error.message || error,
requestUrl: error.response?.requestUrl
requestUrl: error.response?.requestUrl,
stack: error.stack
})
})
22 changes: 22 additions & 0 deletions src/util/uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const { AsyncLocalStorage } = require('async_hooks')
const { randomUUID } = require('crypto')

const asyncLocalStorage = new AsyncLocalStorage()

const overrideWrite = stream => {
const originalWrite = stream.write
stream.write = function (data) {
originalWrite.call(stream, `${getUUID()} ${data}`)
}
}

overrideWrite(process.stderr)
overrideWrite(process.stdout)

const getUUID = () => asyncLocalStorage.getStore()

const withUUID = fn => asyncLocalStorage.run(randomUUID(), fn)

module.exports = { getUUID, withUUID }

0 comments on commit 3cab6b9

Please sign in to comment.