@nxtedition/nxt-undici 1.0.1 → 1.0.2

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.
Files changed (2) hide show
  1. package/lib/index.js +14 -2
  2. package/package.json +2 -3
package/lib/index.js CHANGED
@@ -1,10 +1,22 @@
1
1
  const assert = require('assert')
2
2
  const createError = require('http-errors')
3
- const xuid = require('xuid')
4
3
  const undici = require('undici')
5
4
  const stream = require('stream')
6
5
  const { parseHeaders } = require('./utils')
7
6
 
7
+ // https://github.com/fastify/fastify/blob/main/lib/reqIdGenFactory.js
8
+ // 2,147,483,647 (2^31 − 1) stands for max SMI value (an internal optimization of V8).
9
+ // With this upper bound, if you'll be generating 1k ids/sec, you're going to hit it in ~25 days.
10
+ // This is very likely to happen in real-world applications, hence the limit is enforced.
11
+ // Growing beyond this value will make the id generation slower and cause a deopt.
12
+ // In the worst cases, it will become a float, losing accuracy.
13
+ const maxInt = 2147483647
14
+ let nextReqId = Math.floor(Math.random() * maxInt)
15
+ function genReqId() {
16
+ nextReqId = (nextReqId + 1) & maxInt
17
+ return `req-${nextReqId.toString(36)}`
18
+ }
19
+
8
20
  class Readable extends stream.Readable {
9
21
  constructor({ statusCode, statusMessage, headers, size, ...opts }) {
10
22
  super(opts)
@@ -103,7 +115,7 @@ async function request(url, opts) {
103
115
  }
104
116
 
105
117
  headers = {
106
- 'request-id': xuid(),
118
+ 'request-id': `req-${genReqId()}`,
107
119
  'user-agent': opts.userAgent ?? globalThis.userAgent,
108
120
  ...headers,
109
121
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",
@@ -11,8 +11,7 @@
11
11
  "cache-control-parser": "^2.0.4",
12
12
  "http-errors": "^2.0.0",
13
13
  "lru-cache": "^10.0.1",
14
- "undici": "^5.26.4",
15
- "xuid": "^4.1.2"
14
+ "undici": "^5.26.4"
16
15
  },
17
16
  "devDependencies": {
18
17
  "@types/node": "^20.8.2",