@nxtedition/nxt-undici 1.0.5 → 1.0.6
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 +15 -2
- package/lib/interceptor/redirect.js +10 -13
- package/lib/interceptor/request-id.js +0 -4
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -6,6 +6,19 @@ const { parseHeaders } = require('./utils')
|
|
|
6
6
|
|
|
7
7
|
const dispatcherCache = new WeakMap()
|
|
8
8
|
|
|
9
|
+
// https://github.com/fastify/fastify/blob/main/lib/reqIdGenFactory.js
|
|
10
|
+
// 2,147,483,647 (2^31 − 1) stands for max SMI value (an internal optimization of V8).
|
|
11
|
+
// With this upper bound, if you'll be generating 1k ids/sec, you're going to hit it in ~25 days.
|
|
12
|
+
// This is very likely to happen in real-world applications, hence the limit is enforced.
|
|
13
|
+
// Growing beyond this value will make the id generation slower and cause a deopt.
|
|
14
|
+
// In the worst cases, it will become a float, losing accuracy.
|
|
15
|
+
const maxInt = 2147483647
|
|
16
|
+
let nextReqId = Math.floor(Math.random() * maxInt)
|
|
17
|
+
function genReqId() {
|
|
18
|
+
nextReqId = (nextReqId + 1) & maxInt
|
|
19
|
+
return `req-${nextReqId.toString(36)}`
|
|
20
|
+
}
|
|
21
|
+
|
|
9
22
|
class Readable extends stream.Readable {
|
|
10
23
|
constructor({ statusCode, statusMessage, headers, size, ...opts }) {
|
|
11
24
|
super(opts)
|
|
@@ -157,7 +170,7 @@ async function request(url, opts) {
|
|
|
157
170
|
return new Promise((resolve, reject) =>
|
|
158
171
|
dispatch(
|
|
159
172
|
{
|
|
160
|
-
id: opts.id,
|
|
173
|
+
id: opts.id ?? genReqId(),
|
|
161
174
|
url,
|
|
162
175
|
method,
|
|
163
176
|
body: opts.body,
|
|
@@ -174,7 +187,7 @@ async function request(url, opts) {
|
|
|
174
187
|
proxy: opts.proxy,
|
|
175
188
|
cache: opts.cache,
|
|
176
189
|
upgrade: opts.upgrade,
|
|
177
|
-
follow: opts.follow ??
|
|
190
|
+
follow: opts.follow ?? 8,
|
|
178
191
|
logger: opts.logger,
|
|
179
192
|
},
|
|
180
193
|
{
|
|
@@ -11,6 +11,7 @@ class Handler {
|
|
|
11
11
|
this.abort = null
|
|
12
12
|
this.aborted = false
|
|
13
13
|
this.reason = null
|
|
14
|
+
this.maxCount = Number.isFinite(opts.follow) ? opts.follow : opts.follow?.count ?? 0
|
|
14
15
|
|
|
15
16
|
this.count = 0
|
|
16
17
|
this.location = null
|
|
@@ -67,9 +68,8 @@ class Handler {
|
|
|
67
68
|
return this.handler.onHeaders(statusCode, headers, resume, statusText)
|
|
68
69
|
}
|
|
69
70
|
} else {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
throw new Error(`Max redirections reached: ${maxCount}.`)
|
|
71
|
+
if (this.count >= this.maxCount) {
|
|
72
|
+
throw new Error(`Max redirections reached: ${this.maxCount}.`)
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
|
|
@@ -158,17 +158,12 @@ function shouldRemoveHeader(header, removeContent, unknownOrigin) {
|
|
|
158
158
|
|
|
159
159
|
// https://tools.ietf.org/html/rfc7231#section-6.4
|
|
160
160
|
function cleanRequestHeaders(headers, removeContent, unknownOrigin) {
|
|
161
|
-
|
|
162
|
-
if (
|
|
163
|
-
for (let i = 0; i < headers.length; i += 2) {
|
|
164
|
-
if (!shouldRemoveHeader(headers[i], removeContent, unknownOrigin)) {
|
|
165
|
-
ret.push(headers[i], headers[i + 1])
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
} else if (headers && typeof headers === 'object') {
|
|
161
|
+
let ret
|
|
162
|
+
if (headers && typeof headers === 'object') {
|
|
169
163
|
for (const key of Object.keys(headers)) {
|
|
170
164
|
if (!shouldRemoveHeader(key, removeContent, unknownOrigin)) {
|
|
171
|
-
ret
|
|
165
|
+
ret ??= {}
|
|
166
|
+
ret[key] = headers[key]
|
|
172
167
|
}
|
|
173
168
|
}
|
|
174
169
|
} else {
|
|
@@ -178,4 +173,6 @@ function cleanRequestHeaders(headers, removeContent, unknownOrigin) {
|
|
|
178
173
|
}
|
|
179
174
|
|
|
180
175
|
module.exports = (dispatch) => (opts, handler) =>
|
|
181
|
-
opts.follow
|
|
176
|
+
opts.follow != null
|
|
177
|
+
? dispatch(opts, new Handler(opts, { handler, dispatch }))
|
|
178
|
+
: dispatch(opts, handler)
|
|
@@ -12,10 +12,6 @@ function genReqId() {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
module.exports = (dispatch) => (opts, handler) => {
|
|
15
|
-
if (opts.id === null) {
|
|
16
|
-
return dispatch(opts, handler)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
15
|
let id = opts.id ?? opts.headers?.['request-id'] ?? opts.headers?.['Request-Id']
|
|
20
16
|
id = id ? `${id},${genReqId()}` : genReqId()
|
|
21
17
|
|