undici 7.1.1 → 7.2.1
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/docs/docs/api/DiagnosticsChannel.md +1 -1
- package/docs/docs/api/Dispatcher.md +1 -1
- package/lib/handler/cache-handler.js +165 -110
- package/lib/handler/retry-handler.js +2 -2
- package/lib/interceptor/cache.js +14 -5
- package/lib/interceptor/dns.js +6 -2
- package/lib/util/cache.js +0 -1
- package/lib/util/date.js +259 -0
- package/lib/web/websocket/receiver.js +61 -27
- package/lib/web/websocket/util.js +1 -1
- package/package.json +4 -3
- package/types/errors.d.ts +16 -0
|
@@ -40,7 +40,7 @@ diagnosticsChannel.channel('undici:request:bodySent').subscribe(({ request }) =>
|
|
|
40
40
|
|
|
41
41
|
## `undici:request:headers`
|
|
42
42
|
|
|
43
|
-
This message is published after the response headers have been received
|
|
43
|
+
This message is published after the response headers have been received.
|
|
44
44
|
|
|
45
45
|
```js
|
|
46
46
|
import diagnosticsChannel from 'diagnostics_channel'
|
|
@@ -652,7 +652,7 @@ return null
|
|
|
652
652
|
|
|
653
653
|
A faster version of `Dispatcher.request`. This method expects the second argument `factory` to return a [`stream.Writable`](https://nodejs.org/api/stream.html#stream_class_stream_writable) stream which the response will be written to. This improves performance by avoiding creating an intermediate [`stream.Readable`](https://nodejs.org/api/stream.html#stream_readable_streams) stream when the user expects to directly pipe the response body to a [`stream.Writable`](https://nodejs.org/api/stream.html#stream_class_stream_writable) stream.
|
|
654
654
|
|
|
655
|
-
As demonstrated in [Example 1 - Basic GET stream request](/docs/docs/api/Dispatcher.md#example-1
|
|
655
|
+
As demonstrated in [Example 1 - Basic GET stream request](/docs/docs/api/Dispatcher.md#example-1-basic-get-stream-request), it is recommended to use the `option.opaque` property to avoid creating a closure for the `factory` method. This pattern works well with Node.js Web Frameworks such as [Fastify](https://fastify.io). See [Example 2 - Stream to Fastify Response](/docs/docs/api/Dispatch.md#example-2-stream-to-fastify-response) for more details.
|
|
656
656
|
|
|
657
657
|
Arguments:
|
|
658
658
|
|
|
@@ -6,9 +6,17 @@ const {
|
|
|
6
6
|
parseVaryHeader,
|
|
7
7
|
isEtagUsable
|
|
8
8
|
} = require('../util/cache')
|
|
9
|
+
const { parseHttpDate } = require('../util/date.js')
|
|
9
10
|
|
|
10
11
|
function noop () {}
|
|
11
12
|
|
|
13
|
+
// Status codes that we can use some heuristics on to cache
|
|
14
|
+
const HEURISTICALLY_CACHEABLE_STATUS_CODES = [
|
|
15
|
+
200, 203, 204, 206, 300, 301, 308, 404, 405, 410, 414, 501
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
const MAX_RESPONSE_AGE = 2147483647000
|
|
19
|
+
|
|
12
20
|
/**
|
|
13
21
|
* @typedef {import('../../types/dispatcher.d.ts').default.DispatchHandler} DispatchHandler
|
|
14
22
|
*
|
|
@@ -68,17 +76,23 @@ class CacheHandler {
|
|
|
68
76
|
this.#handler.onRequestUpgrade?.(controller, statusCode, headers, socket)
|
|
69
77
|
}
|
|
70
78
|
|
|
79
|
+
/**
|
|
80
|
+
* @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller
|
|
81
|
+
* @param {number} statusCode
|
|
82
|
+
* @param {import('../../types/header.d.ts').IncomingHttpHeaders} resHeaders
|
|
83
|
+
* @param {string} statusMessage
|
|
84
|
+
*/
|
|
71
85
|
onResponseStart (
|
|
72
86
|
controller,
|
|
73
87
|
statusCode,
|
|
74
|
-
|
|
88
|
+
resHeaders,
|
|
75
89
|
statusMessage
|
|
76
90
|
) {
|
|
77
91
|
const downstreamOnHeaders = () =>
|
|
78
92
|
this.#handler.onResponseStart?.(
|
|
79
93
|
controller,
|
|
80
94
|
statusCode,
|
|
81
|
-
|
|
95
|
+
resHeaders,
|
|
82
96
|
statusMessage
|
|
83
97
|
)
|
|
84
98
|
|
|
@@ -87,97 +101,113 @@ class CacheHandler {
|
|
|
87
101
|
statusCode >= 200 &&
|
|
88
102
|
statusCode <= 399
|
|
89
103
|
) {
|
|
90
|
-
//
|
|
104
|
+
// Successful response to an unsafe method, delete it from cache
|
|
105
|
+
// https://www.rfc-editor.org/rfc/rfc9111.html#name-invalidating-stored-response
|
|
91
106
|
try {
|
|
92
|
-
this.#store.delete(this.#cacheKey)
|
|
107
|
+
this.#store.delete(this.#cacheKey)?.catch?.(noop)
|
|
93
108
|
} catch {
|
|
94
109
|
// Fail silently
|
|
95
110
|
}
|
|
96
111
|
return downstreamOnHeaders()
|
|
97
112
|
}
|
|
98
113
|
|
|
99
|
-
const cacheControlHeader =
|
|
100
|
-
|
|
101
|
-
|
|
114
|
+
const cacheControlHeader = resHeaders['cache-control']
|
|
115
|
+
const heuristicallyCacheable = resHeaders['last-modified'] && HEURISTICALLY_CACHEABLE_STATUS_CODES.includes(statusCode)
|
|
116
|
+
if (
|
|
117
|
+
!cacheControlHeader &&
|
|
118
|
+
!resHeaders['expires'] &&
|
|
119
|
+
!heuristicallyCacheable &&
|
|
120
|
+
!this.#cacheByDefault
|
|
121
|
+
) {
|
|
122
|
+
// Don't have anything to tell us this response is cachable and we're not
|
|
123
|
+
// caching by default
|
|
102
124
|
return downstreamOnHeaders()
|
|
103
125
|
}
|
|
104
126
|
|
|
105
127
|
const cacheControlDirectives = cacheControlHeader ? parseCacheControlHeader(cacheControlHeader) : {}
|
|
106
|
-
if (!canCacheResponse(this.#cacheType, statusCode,
|
|
128
|
+
if (!canCacheResponse(this.#cacheType, statusCode, resHeaders, cacheControlDirectives)) {
|
|
107
129
|
return downstreamOnHeaders()
|
|
108
130
|
}
|
|
109
131
|
|
|
110
|
-
const age = getAge(headers)
|
|
111
|
-
|
|
112
132
|
const now = Date.now()
|
|
113
|
-
const
|
|
114
|
-
if (
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
133
|
+
const resAge = resHeaders.age ? getAge(resHeaders.age) : undefined
|
|
134
|
+
if (resAge && resAge >= MAX_RESPONSE_AGE) {
|
|
135
|
+
// Response considered stale
|
|
136
|
+
return downstreamOnHeaders()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const resDate = typeof resHeaders.date === 'string'
|
|
140
|
+
? parseHttpDate(resHeaders.date)
|
|
141
|
+
: undefined
|
|
142
|
+
|
|
143
|
+
const staleAt =
|
|
144
|
+
determineStaleAt(this.#cacheType, now, resAge, resHeaders, resDate, cacheControlDirectives) ??
|
|
145
|
+
this.#cacheByDefault
|
|
146
|
+
if (staleAt === undefined || (resAge && resAge > staleAt)) {
|
|
147
|
+
return downstreamOnHeaders()
|
|
148
|
+
}
|
|
123
149
|
|
|
124
|
-
|
|
150
|
+
const baseTime = resDate ? resDate.getTime() : now
|
|
151
|
+
const absoluteStaleAt = staleAt + baseTime
|
|
152
|
+
if (now >= absoluteStaleAt) {
|
|
153
|
+
// Response is already stale
|
|
154
|
+
return downstreamOnHeaders()
|
|
155
|
+
}
|
|
125
156
|
|
|
126
|
-
|
|
127
|
-
|
|
157
|
+
let varyDirectives
|
|
158
|
+
if (this.#cacheKey.headers && resHeaders.vary) {
|
|
159
|
+
varyDirectives = parseVaryHeader(resHeaders.vary, this.#cacheKey.headers)
|
|
160
|
+
if (!varyDirectives) {
|
|
161
|
+
// Parse error
|
|
128
162
|
return downstreamOnHeaders()
|
|
129
163
|
}
|
|
164
|
+
}
|
|
130
165
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
166
|
+
const deleteAt = determineDeleteAt(baseTime, cacheControlDirectives, absoluteStaleAt)
|
|
167
|
+
const strippedHeaders = stripNecessaryHeaders(resHeaders, cacheControlDirectives)
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @type {import('../../types/cache-interceptor.d.ts').default.CacheValue}
|
|
171
|
+
*/
|
|
172
|
+
const value = {
|
|
173
|
+
statusCode,
|
|
174
|
+
statusMessage,
|
|
175
|
+
headers: strippedHeaders,
|
|
176
|
+
vary: varyDirectives,
|
|
177
|
+
cacheControlDirectives,
|
|
178
|
+
cachedAt: resAge ? now - resAge : now,
|
|
179
|
+
staleAt: absoluteStaleAt,
|
|
180
|
+
deleteAt
|
|
181
|
+
}
|
|
139
182
|
|
|
140
|
-
|
|
141
|
-
|
|
183
|
+
if (typeof resHeaders.etag === 'string' && isEtagUsable(resHeaders.etag)) {
|
|
184
|
+
value.etag = resHeaders.etag
|
|
185
|
+
}
|
|
142
186
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
statusCode,
|
|
148
|
-
statusMessage,
|
|
149
|
-
headers: strippedHeaders,
|
|
150
|
-
vary: varyDirectives,
|
|
151
|
-
cacheControlDirectives,
|
|
152
|
-
cachedAt: age ? now - (age * 1000) : now,
|
|
153
|
-
staleAt: absoluteStaleAt,
|
|
154
|
-
deleteAt
|
|
155
|
-
}
|
|
187
|
+
this.#writeStream = this.#store.createWriteStream(this.#cacheKey, value)
|
|
188
|
+
if (!this.#writeStream) {
|
|
189
|
+
return downstreamOnHeaders()
|
|
190
|
+
}
|
|
156
191
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
192
|
+
const handler = this
|
|
193
|
+
this.#writeStream
|
|
194
|
+
.on('drain', () => controller.resume())
|
|
195
|
+
.on('error', function () {
|
|
196
|
+
// TODO (fix): Make error somehow observable?
|
|
197
|
+
handler.#writeStream = undefined
|
|
198
|
+
|
|
199
|
+
// Delete the value in case the cache store is holding onto state from
|
|
200
|
+
// the call to createWriteStream
|
|
201
|
+
handler.#store.delete(handler.#cacheKey)
|
|
202
|
+
})
|
|
203
|
+
.on('close', function () {
|
|
204
|
+
if (handler.#writeStream === this) {
|
|
205
|
+
handler.#writeStream = undefined
|
|
206
|
+
}
|
|
160
207
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const handler = this
|
|
165
|
-
this.#writeStream
|
|
166
|
-
.on('drain', () => controller.resume())
|
|
167
|
-
.on('error', function () {
|
|
168
|
-
// TODO (fix): Make error somehow observable?
|
|
169
|
-
handler.#writeStream = undefined
|
|
170
|
-
})
|
|
171
|
-
.on('close', function () {
|
|
172
|
-
if (handler.#writeStream === this) {
|
|
173
|
-
handler.#writeStream = undefined
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// TODO (fix): Should we resume even if was paused downstream?
|
|
177
|
-
controller.resume()
|
|
178
|
-
})
|
|
179
|
-
}
|
|
180
|
-
}
|
|
208
|
+
// TODO (fix): Should we resume even if was paused downstream?
|
|
209
|
+
controller.resume()
|
|
210
|
+
})
|
|
181
211
|
|
|
182
212
|
return downstreamOnHeaders()
|
|
183
213
|
}
|
|
@@ -207,18 +237,15 @@ class CacheHandler {
|
|
|
207
237
|
*
|
|
208
238
|
* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType
|
|
209
239
|
* @param {number} statusCode
|
|
210
|
-
* @param {
|
|
240
|
+
* @param {import('../../types/header.d.ts').IncomingHttpHeaders} resHeaders
|
|
211
241
|
* @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} cacheControlDirectives
|
|
212
242
|
*/
|
|
213
|
-
function canCacheResponse (cacheType, statusCode,
|
|
243
|
+
function canCacheResponse (cacheType, statusCode, resHeaders, cacheControlDirectives) {
|
|
214
244
|
if (statusCode !== 200 && statusCode !== 307) {
|
|
215
245
|
return false
|
|
216
246
|
}
|
|
217
247
|
|
|
218
|
-
if (
|
|
219
|
-
cacheControlDirectives['no-cache'] === true ||
|
|
220
|
-
cacheControlDirectives['no-store']
|
|
221
|
-
) {
|
|
248
|
+
if (cacheControlDirectives['no-store']) {
|
|
222
249
|
return false
|
|
223
250
|
}
|
|
224
251
|
|
|
@@ -227,13 +254,13 @@ function canCacheResponse (cacheType, statusCode, headers, cacheControlDirective
|
|
|
227
254
|
}
|
|
228
255
|
|
|
229
256
|
// https://www.rfc-editor.org/rfc/rfc9111.html#section-4.1-5
|
|
230
|
-
if (
|
|
257
|
+
if (resHeaders.vary?.includes('*')) {
|
|
231
258
|
return false
|
|
232
259
|
}
|
|
233
260
|
|
|
234
261
|
// https://www.rfc-editor.org/rfc/rfc9111.html#name-storing-responses-to-authen
|
|
235
|
-
if (
|
|
236
|
-
if (!cacheControlDirectives.public || typeof
|
|
262
|
+
if (resHeaders.authorization) {
|
|
263
|
+
if (!cacheControlDirectives.public || typeof resHeaders.authorization !== 'string') {
|
|
237
264
|
return false
|
|
238
265
|
}
|
|
239
266
|
|
|
@@ -256,58 +283,77 @@ function canCacheResponse (cacheType, statusCode, headers, cacheControlDirective
|
|
|
256
283
|
}
|
|
257
284
|
|
|
258
285
|
/**
|
|
259
|
-
* @param {
|
|
286
|
+
* @param {string | string[]} ageHeader
|
|
260
287
|
* @returns {number | undefined}
|
|
261
288
|
*/
|
|
262
|
-
function getAge (
|
|
263
|
-
|
|
264
|
-
return undefined
|
|
265
|
-
}
|
|
289
|
+
function getAge (ageHeader) {
|
|
290
|
+
const age = parseInt(Array.isArray(ageHeader) ? ageHeader[0] : ageHeader)
|
|
266
291
|
|
|
267
|
-
|
|
268
|
-
if (isNaN(age) || age >= 2147483647) {
|
|
269
|
-
return undefined
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
return age
|
|
292
|
+
return isNaN(age) ? undefined : age * 1000
|
|
273
293
|
}
|
|
274
294
|
|
|
275
295
|
/**
|
|
276
296
|
* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType
|
|
277
297
|
* @param {number} now
|
|
278
|
-
* @param {
|
|
298
|
+
* @param {number | undefined} age
|
|
299
|
+
* @param {import('../../types/header.d.ts').IncomingHttpHeaders} resHeaders
|
|
300
|
+
* @param {Date | undefined} responseDate
|
|
279
301
|
* @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} cacheControlDirectives
|
|
280
302
|
*
|
|
281
|
-
* @returns {number | undefined} time that the value is stale at or undefined if it shouldn't be cached
|
|
303
|
+
* @returns {number | undefined} time that the value is stale at in seconds or undefined if it shouldn't be cached
|
|
282
304
|
*/
|
|
283
|
-
function determineStaleAt (cacheType, now,
|
|
305
|
+
function determineStaleAt (cacheType, now, age, resHeaders, responseDate, cacheControlDirectives) {
|
|
284
306
|
if (cacheType === 'shared') {
|
|
285
307
|
// Prioritize s-maxage since we're a shared cache
|
|
286
308
|
// s-maxage > max-age > Expire
|
|
287
309
|
// https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.2.10-3
|
|
288
310
|
const sMaxAge = cacheControlDirectives['s-maxage']
|
|
289
|
-
if (sMaxAge) {
|
|
290
|
-
return sMaxAge * 1000
|
|
311
|
+
if (sMaxAge !== undefined) {
|
|
312
|
+
return sMaxAge > 0 ? sMaxAge * 1000 : undefined
|
|
291
313
|
}
|
|
292
314
|
}
|
|
293
315
|
|
|
294
316
|
const maxAge = cacheControlDirectives['max-age']
|
|
295
|
-
if (maxAge) {
|
|
296
|
-
return maxAge * 1000
|
|
317
|
+
if (maxAge !== undefined) {
|
|
318
|
+
return maxAge > 0 ? maxAge * 1000 : undefined
|
|
297
319
|
}
|
|
298
320
|
|
|
299
|
-
if (
|
|
321
|
+
if (typeof resHeaders.expires === 'string') {
|
|
300
322
|
// https://www.rfc-editor.org/rfc/rfc9111.html#section-5.3
|
|
301
|
-
const expiresDate =
|
|
302
|
-
if (expiresDate
|
|
323
|
+
const expiresDate = parseHttpDate(resHeaders.expires)
|
|
324
|
+
if (expiresDate) {
|
|
303
325
|
if (now >= expiresDate.getTime()) {
|
|
304
326
|
return undefined
|
|
305
327
|
}
|
|
306
328
|
|
|
329
|
+
if (responseDate) {
|
|
330
|
+
if (responseDate >= expiresDate) {
|
|
331
|
+
return undefined
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (age !== undefined && age > (expiresDate - responseDate)) {
|
|
335
|
+
return undefined
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
307
339
|
return expiresDate.getTime() - now
|
|
308
340
|
}
|
|
309
341
|
}
|
|
310
342
|
|
|
343
|
+
if (typeof resHeaders['last-modified'] === 'string') {
|
|
344
|
+
// https://www.rfc-editor.org/rfc/rfc9111.html#name-calculating-heuristic-fresh
|
|
345
|
+
const lastModified = new Date(resHeaders['last-modified'])
|
|
346
|
+
if (isValidDate(lastModified)) {
|
|
347
|
+
if (lastModified.getTime() >= now) {
|
|
348
|
+
return undefined
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const responseAge = now - lastModified.getTime()
|
|
352
|
+
|
|
353
|
+
return responseAge * 0.1
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
311
357
|
if (cacheControlDirectives.immutable) {
|
|
312
358
|
// https://www.rfc-editor.org/rfc/rfc8246.html#section-2.2
|
|
313
359
|
return 31536000
|
|
@@ -317,10 +363,11 @@ function determineStaleAt (cacheType, now, headers, cacheControlDirectives) {
|
|
|
317
363
|
}
|
|
318
364
|
|
|
319
365
|
/**
|
|
366
|
+
* @param {number} now
|
|
320
367
|
* @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} cacheControlDirectives
|
|
321
368
|
* @param {number} staleAt
|
|
322
369
|
*/
|
|
323
|
-
function determineDeleteAt (cacheControlDirectives, staleAt) {
|
|
370
|
+
function determineDeleteAt (now, cacheControlDirectives, staleAt) {
|
|
324
371
|
let staleWhileRevalidate = -Infinity
|
|
325
372
|
let staleIfError = -Infinity
|
|
326
373
|
let immutable = -Infinity
|
|
@@ -334,7 +381,7 @@ function determineDeleteAt (cacheControlDirectives, staleAt) {
|
|
|
334
381
|
}
|
|
335
382
|
|
|
336
383
|
if (staleWhileRevalidate === -Infinity && staleIfError === -Infinity) {
|
|
337
|
-
immutable =
|
|
384
|
+
immutable = now + 31536000000
|
|
338
385
|
}
|
|
339
386
|
|
|
340
387
|
return Math.max(staleAt, staleWhileRevalidate, staleIfError, immutable)
|
|
@@ -342,11 +389,11 @@ function determineDeleteAt (cacheControlDirectives, staleAt) {
|
|
|
342
389
|
|
|
343
390
|
/**
|
|
344
391
|
* Strips headers required to be removed in cached responses
|
|
345
|
-
* @param {
|
|
392
|
+
* @param {import('../../types/header.d.ts').IncomingHttpHeaders} resHeaders
|
|
346
393
|
* @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} cacheControlDirectives
|
|
347
394
|
* @returns {Record<string, string | string []>}
|
|
348
395
|
*/
|
|
349
|
-
function stripNecessaryHeaders (
|
|
396
|
+
function stripNecessaryHeaders (resHeaders, cacheControlDirectives) {
|
|
350
397
|
const headersToRemove = [
|
|
351
398
|
'connection',
|
|
352
399
|
'proxy-authenticate',
|
|
@@ -360,14 +407,14 @@ function stripNecessaryHeaders (headers, cacheControlDirectives) {
|
|
|
360
407
|
'age'
|
|
361
408
|
]
|
|
362
409
|
|
|
363
|
-
if (
|
|
364
|
-
if (Array.isArray(
|
|
410
|
+
if (resHeaders['connection']) {
|
|
411
|
+
if (Array.isArray(resHeaders['connection'])) {
|
|
365
412
|
// connection: a
|
|
366
413
|
// connection: b
|
|
367
|
-
headersToRemove.push(...
|
|
414
|
+
headersToRemove.push(...resHeaders['connection'].map(header => header.trim()))
|
|
368
415
|
} else {
|
|
369
416
|
// connection: a, b
|
|
370
|
-
headersToRemove.push(...
|
|
417
|
+
headersToRemove.push(...resHeaders['connection'].split(',').map(header => header.trim()))
|
|
371
418
|
}
|
|
372
419
|
}
|
|
373
420
|
|
|
@@ -381,13 +428,21 @@ function stripNecessaryHeaders (headers, cacheControlDirectives) {
|
|
|
381
428
|
|
|
382
429
|
let strippedHeaders
|
|
383
430
|
for (const headerName of headersToRemove) {
|
|
384
|
-
if (
|
|
385
|
-
strippedHeaders ??= { ...
|
|
431
|
+
if (resHeaders[headerName]) {
|
|
432
|
+
strippedHeaders ??= { ...resHeaders }
|
|
386
433
|
delete strippedHeaders[headerName]
|
|
387
434
|
}
|
|
388
435
|
}
|
|
389
436
|
|
|
390
|
-
return strippedHeaders ??
|
|
437
|
+
return strippedHeaders ?? resHeaders
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* @param {Date} date
|
|
442
|
+
* @returns {boolean}
|
|
443
|
+
*/
|
|
444
|
+
function isValidDate (date) {
|
|
445
|
+
return date instanceof Date && Number.isFinite(date.valueOf())
|
|
391
446
|
}
|
|
392
447
|
|
|
393
448
|
module.exports = CacheHandler
|
|
@@ -133,7 +133,7 @@ class RetryHandler {
|
|
|
133
133
|
? Math.min(retryAfterHeader, maxTimeout)
|
|
134
134
|
: Math.min(minTimeout * timeoutFactor ** (counter - 1), maxTimeout)
|
|
135
135
|
|
|
136
|
-
setTimeout(() => cb(null), retryTimeout)
|
|
136
|
+
setTimeout(() => cb(null), retryTimeout)
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
onResponseStart (controller, statusCode, headers, statusMessage) {
|
|
@@ -277,7 +277,7 @@ class RetryHandler {
|
|
|
277
277
|
}
|
|
278
278
|
|
|
279
279
|
onResponseError (controller, err) {
|
|
280
|
-
if (
|
|
280
|
+
if (controller?.aborted || isDisturbed(this.opts.body)) {
|
|
281
281
|
this.handler.onResponseError?.(controller, err)
|
|
282
282
|
return
|
|
283
283
|
}
|
package/lib/interceptor/cache.js
CHANGED
|
@@ -103,10 +103,14 @@ function handleUncachedResponse (
|
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
/**
|
|
106
|
+
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandler} handler
|
|
107
|
+
* @param {import('../../types/dispatcher.d.ts').default.RequestOptions} opts
|
|
106
108
|
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
|
|
107
109
|
* @param {number} age
|
|
110
|
+
* @param {any} context
|
|
111
|
+
* @param {boolean} isStale
|
|
108
112
|
*/
|
|
109
|
-
function sendCachedValue (handler, opts, result, age, context) {
|
|
113
|
+
function sendCachedValue (handler, opts, result, age, context, isStale) {
|
|
110
114
|
// TODO (perf): Readable.from path can be optimized...
|
|
111
115
|
const stream = util.isStream(result.body)
|
|
112
116
|
? result.body
|
|
@@ -160,8 +164,13 @@ function sendCachedValue (handler, opts, result, age, context) {
|
|
|
160
164
|
|
|
161
165
|
// Add the age header
|
|
162
166
|
// https://www.rfc-editor.org/rfc/rfc9111.html#name-age
|
|
163
|
-
|
|
164
|
-
|
|
167
|
+
const headers = { ...result.headers, age: String(age) }
|
|
168
|
+
|
|
169
|
+
if (isStale) {
|
|
170
|
+
// Add warning header
|
|
171
|
+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Warning
|
|
172
|
+
headers.warning = '110 - "response is stale"'
|
|
173
|
+
}
|
|
165
174
|
|
|
166
175
|
handler.onResponseStart?.(controller, result.statusCode, headers, result.statusMessage)
|
|
167
176
|
|
|
@@ -248,7 +257,7 @@ function handleResult (
|
|
|
248
257
|
new CacheRevalidationHandler(
|
|
249
258
|
(success, context) => {
|
|
250
259
|
if (success) {
|
|
251
|
-
sendCachedValue(handler, opts, result, age, context)
|
|
260
|
+
sendCachedValue(handler, opts, result, age, context, true)
|
|
252
261
|
} else if (util.isStream(result.body)) {
|
|
253
262
|
result.body.on('error', () => {}).destroy()
|
|
254
263
|
}
|
|
@@ -264,7 +273,7 @@ function handleResult (
|
|
|
264
273
|
opts.body.on('error', () => {}).destroy()
|
|
265
274
|
}
|
|
266
275
|
|
|
267
|
-
sendCachedValue(handler, opts, result, age, null)
|
|
276
|
+
sendCachedValue(handler, opts, result, age, null, false)
|
|
268
277
|
}
|
|
269
278
|
|
|
270
279
|
/**
|
package/lib/interceptor/dns.js
CHANGED
|
@@ -213,6 +213,10 @@ class DNSInstance {
|
|
|
213
213
|
this.#records.set(origin.hostname, records)
|
|
214
214
|
}
|
|
215
215
|
|
|
216
|
+
deleteRecords (origin) {
|
|
217
|
+
this.#records.delete(origin.hostname)
|
|
218
|
+
}
|
|
219
|
+
|
|
216
220
|
getHandler (meta, opts) {
|
|
217
221
|
return new DNSDispatchHandler(this, meta, opts)
|
|
218
222
|
}
|
|
@@ -261,7 +265,7 @@ class DNSDispatchHandler extends DecoratorHandler {
|
|
|
261
265
|
break
|
|
262
266
|
}
|
|
263
267
|
case 'ENOTFOUND':
|
|
264
|
-
this.#state.
|
|
268
|
+
this.#state.deleteRecords(this.#origin)
|
|
265
269
|
// eslint-disable-next-line no-fallthrough
|
|
266
270
|
default:
|
|
267
271
|
super.onResponseError(controller, err)
|
|
@@ -349,7 +353,7 @@ module.exports = interceptorOpts => {
|
|
|
349
353
|
|
|
350
354
|
instance.runLookup(origin, origDispatchOpts, (err, newOrigin) => {
|
|
351
355
|
if (err) {
|
|
352
|
-
return handler.
|
|
356
|
+
return handler.onResponseError(null, err)
|
|
353
357
|
}
|
|
354
358
|
|
|
355
359
|
let dispatchOpts = null
|
package/lib/util/cache.js
CHANGED
package/lib/util/date.js
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const IMF_DAYS = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
|
|
4
|
+
const IMF_SPACES = [4, 7, 11, 16, 25]
|
|
5
|
+
const IMF_MONTHS = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
|
|
6
|
+
const IMF_COLONS = [19, 22]
|
|
7
|
+
|
|
8
|
+
const ASCTIME_SPACES = [3, 7, 10, 19]
|
|
9
|
+
|
|
10
|
+
const RFC850_DAYS = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @see https://www.rfc-editor.org/rfc/rfc9110.html#name-date-time-formats
|
|
14
|
+
*
|
|
15
|
+
* @param {string} date
|
|
16
|
+
* @param {Date} [now]
|
|
17
|
+
* @returns {Date | undefined}
|
|
18
|
+
*/
|
|
19
|
+
function parseHttpDate (date, now) {
|
|
20
|
+
// Sun, 06 Nov 1994 08:49:37 GMT ; IMF-fixdate
|
|
21
|
+
// Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
|
|
22
|
+
// Sunday, 06-Nov-94 08:49:37 GMT ; obsolete RFC 850 format
|
|
23
|
+
|
|
24
|
+
date = date.toLowerCase()
|
|
25
|
+
|
|
26
|
+
switch (date[3]) {
|
|
27
|
+
case ',': return parseImfDate(date)
|
|
28
|
+
case ' ': return parseAscTimeDate(date)
|
|
29
|
+
default: return parseRfc850Date(date, now)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @see https://httpwg.org/specs/rfc9110.html#preferred.date.format
|
|
35
|
+
*
|
|
36
|
+
* @param {string} date
|
|
37
|
+
* @returns {Date | undefined}
|
|
38
|
+
*/
|
|
39
|
+
function parseImfDate (date) {
|
|
40
|
+
if (date.length !== 29) {
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!date.endsWith('gmt')) {
|
|
45
|
+
// Unsupported timezone
|
|
46
|
+
return undefined
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for (const spaceInx of IMF_SPACES) {
|
|
50
|
+
if (date[spaceInx] !== ' ') {
|
|
51
|
+
return undefined
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
for (const colonIdx of IMF_COLONS) {
|
|
56
|
+
if (date[colonIdx] !== ':') {
|
|
57
|
+
return undefined
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const dayName = date.substring(0, 3)
|
|
62
|
+
if (!IMF_DAYS.includes(dayName)) {
|
|
63
|
+
return undefined
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const dayString = date.substring(5, 7)
|
|
67
|
+
const day = Number.parseInt(dayString)
|
|
68
|
+
if (isNaN(day) || (day < 10 && dayString[0] !== '0')) {
|
|
69
|
+
// Not a number, 0, or it's less than 10 and didn't start with a 0
|
|
70
|
+
return undefined
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const month = date.substring(8, 11)
|
|
74
|
+
const monthIdx = IMF_MONTHS.indexOf(month)
|
|
75
|
+
if (monthIdx === -1) {
|
|
76
|
+
return undefined
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const year = Number.parseInt(date.substring(12, 16))
|
|
80
|
+
if (isNaN(year)) {
|
|
81
|
+
return undefined
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const hourString = date.substring(17, 19)
|
|
85
|
+
const hour = Number.parseInt(hourString)
|
|
86
|
+
if (isNaN(hour) || (hour < 10 && hourString[0] !== '0')) {
|
|
87
|
+
return undefined
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const minuteString = date.substring(20, 22)
|
|
91
|
+
const minute = Number.parseInt(minuteString)
|
|
92
|
+
if (isNaN(minute) || (minute < 10 && minuteString[0] !== '0')) {
|
|
93
|
+
return undefined
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const secondString = date.substring(23, 25)
|
|
97
|
+
const second = Number.parseInt(secondString)
|
|
98
|
+
if (isNaN(second) || (second < 10 && secondString[0] !== '0')) {
|
|
99
|
+
return undefined
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return new Date(Date.UTC(year, monthIdx, day, hour, minute, second))
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @see https://httpwg.org/specs/rfc9110.html#obsolete.date.formats
|
|
107
|
+
*
|
|
108
|
+
* @param {string} date
|
|
109
|
+
* @returns {Date | undefined}
|
|
110
|
+
*/
|
|
111
|
+
function parseAscTimeDate (date) {
|
|
112
|
+
// This is assumed to be in UTC
|
|
113
|
+
|
|
114
|
+
if (date.length !== 24) {
|
|
115
|
+
return undefined
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
for (const spaceIdx of ASCTIME_SPACES) {
|
|
119
|
+
if (date[spaceIdx] !== ' ') {
|
|
120
|
+
return undefined
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const dayName = date.substring(0, 3)
|
|
125
|
+
if (!IMF_DAYS.includes(dayName)) {
|
|
126
|
+
return undefined
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const month = date.substring(4, 7)
|
|
130
|
+
const monthIdx = IMF_MONTHS.indexOf(month)
|
|
131
|
+
if (monthIdx === -1) {
|
|
132
|
+
return undefined
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const dayString = date.substring(8, 10)
|
|
136
|
+
const day = Number.parseInt(dayString)
|
|
137
|
+
if (isNaN(day) || (day < 10 && dayString[0] !== ' ')) {
|
|
138
|
+
return undefined
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const hourString = date.substring(11, 13)
|
|
142
|
+
const hour = Number.parseInt(hourString)
|
|
143
|
+
if (isNaN(hour) || (hour < 10 && hourString[0] !== '0')) {
|
|
144
|
+
return undefined
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const minuteString = date.substring(14, 16)
|
|
148
|
+
const minute = Number.parseInt(minuteString)
|
|
149
|
+
if (isNaN(minute) || (minute < 10 && minuteString[0] !== '0')) {
|
|
150
|
+
return undefined
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const secondString = date.substring(17, 19)
|
|
154
|
+
const second = Number.parseInt(secondString)
|
|
155
|
+
if (isNaN(second) || (second < 10 && secondString[0] !== '0')) {
|
|
156
|
+
return undefined
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const year = Number.parseInt(date.substring(20, 24))
|
|
160
|
+
if (isNaN(year)) {
|
|
161
|
+
return undefined
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return new Date(Date.UTC(year, monthIdx, day, hour, minute, second))
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* @see https://httpwg.org/specs/rfc9110.html#obsolete.date.formats
|
|
169
|
+
*
|
|
170
|
+
* @param {string} date
|
|
171
|
+
* @param {Date} [now]
|
|
172
|
+
* @returns {Date | undefined}
|
|
173
|
+
*/
|
|
174
|
+
function parseRfc850Date (date, now = new Date()) {
|
|
175
|
+
if (!date.endsWith('gmt')) {
|
|
176
|
+
// Unsupported timezone
|
|
177
|
+
return undefined
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const commaIndex = date.indexOf(',')
|
|
181
|
+
if (commaIndex === -1) {
|
|
182
|
+
return undefined
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if ((date.length - commaIndex - 1) !== 23) {
|
|
186
|
+
return undefined
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const dayName = date.substring(0, commaIndex)
|
|
190
|
+
if (!RFC850_DAYS.includes(dayName)) {
|
|
191
|
+
return undefined
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (
|
|
195
|
+
date[commaIndex + 1] !== ' ' ||
|
|
196
|
+
date[commaIndex + 4] !== '-' ||
|
|
197
|
+
date[commaIndex + 8] !== '-' ||
|
|
198
|
+
date[commaIndex + 11] !== ' ' ||
|
|
199
|
+
date[commaIndex + 14] !== ':' ||
|
|
200
|
+
date[commaIndex + 17] !== ':' ||
|
|
201
|
+
date[commaIndex + 20] !== ' '
|
|
202
|
+
) {
|
|
203
|
+
return undefined
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const dayString = date.substring(commaIndex + 2, commaIndex + 4)
|
|
207
|
+
const day = Number.parseInt(dayString)
|
|
208
|
+
if (isNaN(day) || (day < 10 && dayString[0] !== '0')) {
|
|
209
|
+
// Not a number, or it's less than 10 and didn't start with a 0
|
|
210
|
+
return undefined
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const month = date.substring(commaIndex + 5, commaIndex + 8)
|
|
214
|
+
const monthIdx = IMF_MONTHS.indexOf(month)
|
|
215
|
+
if (monthIdx === -1) {
|
|
216
|
+
return undefined
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// As of this point year is just the decade (i.e. 94)
|
|
220
|
+
let year = Number.parseInt(date.substring(commaIndex + 9, commaIndex + 11))
|
|
221
|
+
if (isNaN(year)) {
|
|
222
|
+
return undefined
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const currentYear = now.getUTCFullYear()
|
|
226
|
+
const currentDecade = currentYear % 100
|
|
227
|
+
const currentCentury = Math.floor(currentYear / 100)
|
|
228
|
+
|
|
229
|
+
if (year > currentDecade && year - currentDecade >= 50) {
|
|
230
|
+
// Over 50 years in future, go to previous century
|
|
231
|
+
year += (currentCentury - 1) * 100
|
|
232
|
+
} else {
|
|
233
|
+
year += currentCentury * 100
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const hourString = date.substring(commaIndex + 12, commaIndex + 14)
|
|
237
|
+
const hour = Number.parseInt(hourString)
|
|
238
|
+
if (isNaN(hour) || (hour < 10 && hourString[0] !== '0')) {
|
|
239
|
+
return undefined
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const minuteString = date.substring(commaIndex + 15, commaIndex + 17)
|
|
243
|
+
const minute = Number.parseInt(minuteString)
|
|
244
|
+
if (isNaN(minute) || (minute < 10 && minuteString[0] !== '0')) {
|
|
245
|
+
return undefined
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const secondString = date.substring(commaIndex + 18, commaIndex + 20)
|
|
249
|
+
const second = Number.parseInt(secondString)
|
|
250
|
+
if (isNaN(second) || (second < 10 && secondString[0] !== '0')) {
|
|
251
|
+
return undefined
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return new Date(Date.UTC(year, monthIdx, day, hour, minute, second))
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
module.exports = {
|
|
258
|
+
parseHttpDate
|
|
259
|
+
}
|
|
@@ -24,6 +24,7 @@ const { PerMessageDeflate } = require('./permessage-deflate')
|
|
|
24
24
|
|
|
25
25
|
class ByteParser extends Writable {
|
|
26
26
|
#buffers = []
|
|
27
|
+
#fragmentsBytes = 0
|
|
27
28
|
#byteOffset = 0
|
|
28
29
|
#loop = false
|
|
29
30
|
|
|
@@ -208,16 +209,14 @@ class ByteParser extends Writable {
|
|
|
208
209
|
this.#state = parserStates.INFO
|
|
209
210
|
} else {
|
|
210
211
|
if (!this.#info.compressed) {
|
|
211
|
-
this
|
|
212
|
+
this.writeFragments(body)
|
|
212
213
|
|
|
213
214
|
// If the frame is not fragmented, a message has been received.
|
|
214
215
|
// If the frame is fragmented, it will terminate with a fin bit set
|
|
215
216
|
// and an opcode of 0 (continuation), therefore we handle that when
|
|
216
217
|
// parsing continuation frames, not here.
|
|
217
218
|
if (!this.#info.fragmented && this.#info.fin) {
|
|
218
|
-
|
|
219
|
-
websocketMessageReceived(this.#handler, this.#info.binaryType, fullMessage)
|
|
220
|
-
this.#fragments.length = 0
|
|
219
|
+
websocketMessageReceived(this.#handler, this.#info.binaryType, this.consumeFragments())
|
|
221
220
|
}
|
|
222
221
|
|
|
223
222
|
this.#state = parserStates.INFO
|
|
@@ -228,7 +227,7 @@ class ByteParser extends Writable {
|
|
|
228
227
|
return
|
|
229
228
|
}
|
|
230
229
|
|
|
231
|
-
this
|
|
230
|
+
this.writeFragments(data)
|
|
232
231
|
|
|
233
232
|
if (!this.#info.fin) {
|
|
234
233
|
this.#state = parserStates.INFO
|
|
@@ -237,11 +236,10 @@ class ByteParser extends Writable {
|
|
|
237
236
|
return
|
|
238
237
|
}
|
|
239
238
|
|
|
240
|
-
websocketMessageReceived(this.#handler, this.#info.binaryType,
|
|
239
|
+
websocketMessageReceived(this.#handler, this.#info.binaryType, this.consumeFragments())
|
|
241
240
|
|
|
242
241
|
this.#loop = true
|
|
243
242
|
this.#state = parserStates.INFO
|
|
244
|
-
this.#fragments.length = 0
|
|
245
243
|
this.run(callback)
|
|
246
244
|
})
|
|
247
245
|
|
|
@@ -265,34 +263,70 @@ class ByteParser extends Writable {
|
|
|
265
263
|
return emptyBuffer
|
|
266
264
|
}
|
|
267
265
|
|
|
268
|
-
|
|
269
|
-
|
|
266
|
+
this.#byteOffset -= n
|
|
267
|
+
|
|
268
|
+
const first = this.#buffers[0]
|
|
269
|
+
|
|
270
|
+
if (first.length > n) {
|
|
271
|
+
// replace with remaining buffer
|
|
272
|
+
this.#buffers[0] = first.subarray(n, first.length)
|
|
273
|
+
return first.subarray(0, n)
|
|
274
|
+
} else if (first.length === n) {
|
|
275
|
+
// prefect match
|
|
270
276
|
return this.#buffers.shift()
|
|
277
|
+
} else {
|
|
278
|
+
let offset = 0
|
|
279
|
+
// If Buffer.allocUnsafe is used, extra copies will be made because the offset is non-zero.
|
|
280
|
+
const buffer = Buffer.allocUnsafeSlow(n)
|
|
281
|
+
while (offset !== n) {
|
|
282
|
+
const next = this.#buffers[0]
|
|
283
|
+
const length = next.length
|
|
284
|
+
|
|
285
|
+
if (length + offset === n) {
|
|
286
|
+
buffer.set(this.#buffers.shift(), offset)
|
|
287
|
+
break
|
|
288
|
+
} else if (length + offset > n) {
|
|
289
|
+
buffer.set(next.subarray(0, n - offset), offset)
|
|
290
|
+
this.#buffers[0] = next.subarray(n - offset)
|
|
291
|
+
break
|
|
292
|
+
} else {
|
|
293
|
+
buffer.set(this.#buffers.shift(), offset)
|
|
294
|
+
offset += length
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return buffer
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
writeFragments (fragment) {
|
|
303
|
+
this.#fragmentsBytes += fragment.length
|
|
304
|
+
this.#fragments.push(fragment)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
consumeFragments () {
|
|
308
|
+
const fragments = this.#fragments
|
|
309
|
+
|
|
310
|
+
if (fragments.length === 1) {
|
|
311
|
+
// single fragment
|
|
312
|
+
this.#fragmentsBytes = 0
|
|
313
|
+
return fragments.shift()
|
|
271
314
|
}
|
|
272
315
|
|
|
273
|
-
const buffer = Buffer.allocUnsafe(n)
|
|
274
316
|
let offset = 0
|
|
317
|
+
// If Buffer.allocUnsafe is used, extra copies will be made because the offset is non-zero.
|
|
318
|
+
const output = Buffer.allocUnsafeSlow(this.#fragmentsBytes)
|
|
275
319
|
|
|
276
|
-
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
if (length + offset === n) {
|
|
281
|
-
buffer.set(this.#buffers.shift(), offset)
|
|
282
|
-
break
|
|
283
|
-
} else if (length + offset > n) {
|
|
284
|
-
buffer.set(next.subarray(0, n - offset), offset)
|
|
285
|
-
this.#buffers[0] = next.subarray(n - offset)
|
|
286
|
-
break
|
|
287
|
-
} else {
|
|
288
|
-
buffer.set(this.#buffers.shift(), offset)
|
|
289
|
-
offset += next.length
|
|
290
|
-
}
|
|
320
|
+
for (let i = 0; i < fragments.length; ++i) {
|
|
321
|
+
const buffer = fragments[i]
|
|
322
|
+
output.set(buffer, offset)
|
|
323
|
+
offset += buffer.length
|
|
291
324
|
}
|
|
292
325
|
|
|
293
|
-
this.#
|
|
326
|
+
this.#fragments = []
|
|
327
|
+
this.#fragmentsBytes = 0
|
|
294
328
|
|
|
295
|
-
return
|
|
329
|
+
return output
|
|
296
330
|
}
|
|
297
331
|
|
|
298
332
|
parseCloseBody (data) {
|
|
@@ -87,7 +87,7 @@ function toArrayBuffer (buffer) {
|
|
|
87
87
|
if (buffer.byteLength === buffer.buffer.byteLength) {
|
|
88
88
|
return buffer.buffer
|
|
89
89
|
}
|
|
90
|
-
return
|
|
90
|
+
return new Uint8Array(buffer).buffer
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "undici",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.2.1",
|
|
4
4
|
"description": "An HTTP/1.1 client, written from scratch for Node.js",
|
|
5
5
|
"homepage": "https://undici.nodejs.org",
|
|
6
6
|
"bugs": {
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"lint:fix": "eslint --fix --cache",
|
|
70
70
|
"test": "npm run test:javascript && cross-env NODE_V8_COVERAGE= npm run test:typescript",
|
|
71
71
|
"test:javascript": "npm run test:javascript:no-jest && npm run test:jest",
|
|
72
|
-
"test:javascript:no-jest": "npm run generate-pem && npm run test:unit && npm run test:node-fetch && npm run test:cache && npm run test:cache-interceptor && npm run test:interceptors && npm run test:fetch && npm run test:cookies && npm run test:eventsource && npm run test:wpt && npm run test:websocket && npm run test:node-test",
|
|
72
|
+
"test:javascript:no-jest": "npm run generate-pem && npm run test:unit && npm run test:node-fetch && npm run test:cache && npm run test:cache-interceptor && npm run test:interceptors && npm run test:fetch && npm run test:cookies && npm run test:eventsource && npm run test:wpt && npm run test:websocket && npm run test:node-test && npm run test:cache-tests",
|
|
73
73
|
"test:javascript:without-intl": "npm run test:javascript:no-jest",
|
|
74
74
|
"test:busboy": "borp -p \"test/busboy/*.js\"",
|
|
75
75
|
"test:cache": "borp -p \"test/cache/*.js\"",
|
|
@@ -96,6 +96,7 @@
|
|
|
96
96
|
"test:websocket:autobahn:report": "node test/autobahn/report.js",
|
|
97
97
|
"test:wpt": "node test/wpt/start-fetch.mjs && node test/wpt/start-mimesniff.mjs && node test/wpt/start-xhr.mjs && node test/wpt/start-websockets.mjs && node test/wpt/start-cacheStorage.mjs && node test/wpt/start-eventsource.mjs",
|
|
98
98
|
"test:wpt:withoutintl": "node test/wpt/start-fetch.mjs && node test/wpt/start-mimesniff.mjs && node test/wpt/start-xhr.mjs && node test/wpt/start-cacheStorage.mjs && node test/wpt/start-eventsource.mjs",
|
|
99
|
+
"test:cache-tests": "node test/cache-interceptor/cache-tests.mjs --ci",
|
|
99
100
|
"coverage": "npm run coverage:clean && cross-env NODE_V8_COVERAGE=./coverage/tmp npm run test:javascript && npm run coverage:report",
|
|
100
101
|
"coverage:ci": "npm run coverage:clean && cross-env NODE_V8_COVERAGE=./coverage/tmp npm run test:javascript && npm run coverage:report:ci",
|
|
101
102
|
"coverage:clean": "node ./scripts/clean-coverage.js",
|
|
@@ -106,7 +107,7 @@
|
|
|
106
107
|
"prepare": "husky && node ./scripts/platform-shell.js"
|
|
107
108
|
},
|
|
108
109
|
"devDependencies": {
|
|
109
|
-
"@fastify/busboy": "3.1.
|
|
110
|
+
"@fastify/busboy": "3.1.1",
|
|
110
111
|
"@matteo.collina/tspl": "^0.1.1",
|
|
111
112
|
"@sinonjs/fake-timers": "^12.0.0",
|
|
112
113
|
"@types/node": "^18.19.50",
|
package/types/errors.d.ts
CHANGED
|
@@ -33,6 +33,22 @@ declare namespace Errors {
|
|
|
33
33
|
code: 'UND_ERR_BODY_TIMEOUT'
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
export class ResponseError extends UndiciError {
|
|
37
|
+
constructor (
|
|
38
|
+
message: string,
|
|
39
|
+
code: number,
|
|
40
|
+
options: {
|
|
41
|
+
headers?: IncomingHttpHeaders | string[] | null,
|
|
42
|
+
body?: null | Record<string, any> | string
|
|
43
|
+
}
|
|
44
|
+
)
|
|
45
|
+
name: 'ResponseError'
|
|
46
|
+
code: 'UND_ERR_RESPONSE'
|
|
47
|
+
statusCode: number
|
|
48
|
+
body: null | Record<string, any> | string
|
|
49
|
+
headers: IncomingHttpHeaders | string[] | null
|
|
50
|
+
}
|
|
51
|
+
|
|
36
52
|
export class ResponseStatusCodeError extends UndiciError {
|
|
37
53
|
constructor (
|
|
38
54
|
message?: string,
|