@nxtedition/nxt-undici 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -115,7 +115,7 @@ async function request(url, opts) {
115
115
  }
116
116
 
117
117
  headers = {
118
- 'request-id': `req-${genReqId()}`,
118
+ 'request-id': genReqId(),
119
119
  'user-agent': opts.userAgent ?? globalThis.userAgent,
120
120
  ...headers,
121
121
  }
@@ -1,11 +1,23 @@
1
1
  const { parseHeaders } = require('../utils')
2
- const xuid = require('xuid')
3
2
  const { performance } = require('perf_hooks')
4
3
 
4
+ // https://github.com/fastify/fastify/blob/main/lib/reqIdGenFactory.js
5
+ // 2,147,483,647 (2^31 − 1) stands for max SMI value (an internal optimization of V8).
6
+ // With this upper bound, if you'll be generating 1k ids/sec, you're going to hit it in ~25 days.
7
+ // This is very likely to happen in real-world applications, hence the limit is enforced.
8
+ // Growing beyond this value will make the id generation slower and cause a deopt.
9
+ // In the worst cases, it will become a float, losing accuracy.
10
+ const maxInt = 2147483647
11
+ let nextReqId = Math.floor(Math.random() * maxInt)
12
+ function genReqId() {
13
+ nextReqId = (nextReqId + 1) & maxInt
14
+ return `req-${nextReqId.toString(36)}`
15
+ }
16
+
5
17
  class Handler {
6
18
  constructor(opts, { handler }) {
7
19
  this.handler = handler
8
- this.opts = opts.id ? opts : { ...opts, id: xuid() }
20
+ this.opts = opts.id ? opts : { ...opts, id: genReqId() }
9
21
  this.abort = null
10
22
  this.aborted = false
11
23
  this.logger = opts.logger.child({ ureq: { id: opts.id } })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",