pino-http icon indicating copy to clipboard operation
pino-http copied to clipboard

The reqId type is overly-broad

Open jamiehodge opened this issue 2 years ago • 3 comments

The patching of the http IncomingMessage to add the id properly is arguably bad, but that discussion aside, reqId is too broad. The inclusion of number and object seems odd, considering that this value should be serialisable as a string in a header.

jamiehodge avatar Jan 23 '23 13:01 jamiehodge

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar Jan 23 '23 13:01 mcollina

The default numbering could also use a change. As hint: crypto.randomUUID could be good enough on node 16 or newer.

Otherwise use https://www.npmjs.com/package/hyperid or apply the same concept with a custom function to have zero additional dependencies.

const {randomBytes} = require('crypto')

function createHyperId (bytes = 16) {
  let count = 1
  let id = randomBytes(bytes).toString('hex')

  function hyperid () {
    if (count < 1000000) return `${id}-${count++}`
    return `${id = randomBytes(bytes).toString('hex')}-${count = 1, count++}`
  }

  return hyperid
}

module.exports = createHyperId

marcbachmann avatar Jan 23 '23 14:01 marcbachmann

Here's a PR for the string based request id generation: https://github.com/pinojs/pino-http/issues/268 There's no type change in there.

marcbachmann avatar Jan 25 '23 00:01 marcbachmann