@nxtedition/nxt-undici 1.0.0 → 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.
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)
|
|
@@ -102,6 +114,12 @@ async function request(url, opts) {
|
|
|
102
114
|
headers = opts.headers
|
|
103
115
|
}
|
|
104
116
|
|
|
117
|
+
headers = {
|
|
118
|
+
'request-id': `req-${genReqId()}`,
|
|
119
|
+
'user-agent': opts.userAgent ?? globalThis.userAgent,
|
|
120
|
+
...headers,
|
|
121
|
+
}
|
|
122
|
+
|
|
105
123
|
if (method === 'CONNECT') {
|
|
106
124
|
throw new createError.MethodNotAllowed()
|
|
107
125
|
}
|
|
@@ -117,11 +135,7 @@ async function request(url, opts) {
|
|
|
117
135
|
url,
|
|
118
136
|
method,
|
|
119
137
|
body: opts.body,
|
|
120
|
-
headers
|
|
121
|
-
'request-id': xuid(),
|
|
122
|
-
'user-agent': opts.userAgent ?? globalThis.userAgent,
|
|
123
|
-
...headers,
|
|
124
|
-
},
|
|
138
|
+
headers,
|
|
125
139
|
origin: url.origin,
|
|
126
140
|
path: url.path ? url.path : url.search ? `${url.pathname}${url.search ?? ''}` : url.pathname,
|
|
127
141
|
reset: opts.reset ?? false,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/nxt-undici",
|
|
3
|
-
"version": "1.0.
|
|
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,7 +11,56 @@
|
|
|
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
|
-
|
|
14
|
+
"undici": "^5.26.4"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/node": "^20.8.2",
|
|
18
|
+
"eslint": "^8.50.0",
|
|
19
|
+
"eslint-config-prettier": "^9.0.0",
|
|
20
|
+
"eslint-config-standard": "^17.0.0",
|
|
21
|
+
"husky": "^8.0.3",
|
|
22
|
+
"lint-staged": "^14.0.1",
|
|
23
|
+
"pinst": "^3.0.0",
|
|
24
|
+
"prettier": "^3.0.3",
|
|
25
|
+
"tap": "^18.4.2"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"prepare": "husky install",
|
|
29
|
+
"prepublishOnly": "pinst --disable",
|
|
30
|
+
"postpublish": "pinst --enable",
|
|
31
|
+
"test": "tap test"
|
|
32
|
+
},
|
|
33
|
+
"lint-staged": {
|
|
34
|
+
"*.{js,jsx,md,ts}": [
|
|
35
|
+
"eslint",
|
|
36
|
+
"prettier --write"
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
"prettier": {
|
|
40
|
+
"printWidth": 100,
|
|
41
|
+
"semi": false,
|
|
42
|
+
"singleQuote": true
|
|
43
|
+
},
|
|
44
|
+
"eslintConfig": {
|
|
45
|
+
"parserOptions": {
|
|
46
|
+
"ecmaFeatures": {
|
|
47
|
+
"ecmaVersion": 2020
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"extends": [
|
|
51
|
+
"standard",
|
|
52
|
+
"prettier",
|
|
53
|
+
"prettier/prettier"
|
|
54
|
+
],
|
|
55
|
+
"rules": {
|
|
56
|
+
"quotes": [
|
|
57
|
+
"error",
|
|
58
|
+
"single",
|
|
59
|
+
{
|
|
60
|
+
"avoidEscape": true,
|
|
61
|
+
"allowTemplateLiterals": true
|
|
62
|
+
}
|
|
63
|
+
]
|
|
64
|
+
}
|
|
16
65
|
}
|
|
17
66
|
}
|