undici 8.12.3 → 8.12.4
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/README.md +1 -1
- package/docs/docs/api/Dispatcher.md +4 -1
- package/docs/docs/api/RetryAgent.md +1 -1
- package/docs/docs/api/RetryHandler.md +1 -1
- package/docs/docs/index.md +1 -1
- package/lib/cache/sqlite-cache-store.js +58 -12
- package/lib/dispatcher/client-h1.js +3 -1
- package/lib/dispatcher/client-h2.js +80 -23
- package/lib/dispatcher/parser-h1.js +2 -1
- package/lib/handler/cache-handler.js +305 -39
- package/lib/handler/decorator-handler.js +6 -2
- package/lib/handler/redirect-handler.js +32 -11
- package/lib/interceptor/cache.js +112 -23
- package/lib/mock/mock-utils.js +11 -1
- package/lib/web/fetch/body.js +28 -8
- package/lib/web/fetch/index.js +3 -9
- package/lib/web/fetch/request.js +93 -115
- package/lib/web/subresource-integrity/subresource-integrity.js +2 -2
- package/package.json +2 -2
- package/types/dispatcher.d.ts +1 -1
- package/types/formdata.d.ts +6 -6
- package/types/retry-handler.d.ts +3 -3
|
@@ -10,6 +10,17 @@ const {
|
|
|
10
10
|
isInvalidOrWildcardVaryHeader,
|
|
11
11
|
isEtagUsable
|
|
12
12
|
} = require('../util/cache')
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A suppressed redirect hop recorded by the redirect handler. Carried on
|
|
16
|
+
* {@link kRedirectCacheControl} and inspected by the cache handler before the
|
|
17
|
+
* final response is stored under the original request key.
|
|
18
|
+
*
|
|
19
|
+
* @typedef {object} RedirectHop
|
|
20
|
+
* @property {number} statusCode The hop's response status code (e.g. 302).
|
|
21
|
+
* @property {import('../../types/header.d.ts').IncomingHttpHeaders} headers The hop's response headers.
|
|
22
|
+
* @property {number} receivedAt The time the hop's response headers were received.
|
|
23
|
+
*/
|
|
13
24
|
const { parseHttpDate } = require('../util/date.js')
|
|
14
25
|
|
|
15
26
|
function noop () {}
|
|
@@ -105,6 +116,16 @@ function arrayIncludes (array, value) {
|
|
|
105
116
|
return false
|
|
106
117
|
}
|
|
107
118
|
|
|
119
|
+
function appendVaryValues (target, varyValue) {
|
|
120
|
+
if (Array.isArray(varyValue)) {
|
|
121
|
+
for (let i = 0; i < varyValue.length; i++) {
|
|
122
|
+
target.push(varyValue[i])
|
|
123
|
+
}
|
|
124
|
+
} else if (varyValue != null) {
|
|
125
|
+
target.push(varyValue)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
108
129
|
function appendConnectionHeaderTokens (headersToRemove, connectionHeader) {
|
|
109
130
|
const values = Array.isArray(connectionHeader) ? connectionHeader : [connectionHeader]
|
|
110
131
|
|
|
@@ -176,6 +197,26 @@ function invalidateUnsafeRequest (store, cacheKey, resHeaders) {
|
|
|
176
197
|
deleteLocationTargets(store, cacheKey, resHeaders.location)
|
|
177
198
|
deleteLocationTargets(store, cacheKey, resHeaders['content-location'])
|
|
178
199
|
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Whether the original request carried a credential that is not reflected in
|
|
203
|
+
* the cache key. The memory/sqlite stores key an entry on origin, method, path
|
|
204
|
+
* and `vary` only, so a credentialed request that is redirected to a resource
|
|
205
|
+
* that varies per principal must not be stored under the original (alias) key:
|
|
206
|
+
* a later caller would be served the first principal's body. The set mirrors the
|
|
207
|
+
* request credentials the redirect handler strips across origins (`authorization`,
|
|
208
|
+
* `cookie`, `proxy-authorization`).
|
|
209
|
+
*
|
|
210
|
+
* @param {import('../../types/header.d.ts').IncomingHttpHeaders | undefined} headers
|
|
211
|
+
* @returns {boolean}
|
|
212
|
+
*/
|
|
213
|
+
function requestCarriesCredentials (headers) {
|
|
214
|
+
return headers != null && (
|
|
215
|
+
Object.hasOwn(headers, 'authorization') ||
|
|
216
|
+
Object.hasOwn(headers, 'cookie') ||
|
|
217
|
+
Object.hasOwn(headers, 'proxy-authorization')
|
|
218
|
+
)
|
|
219
|
+
}
|
|
179
220
|
/**
|
|
180
221
|
* @typedef {import('../../types/dispatcher.d.ts').default.DispatchHandler} DispatchHandler
|
|
181
222
|
*
|
|
@@ -220,7 +261,7 @@ class CacheHandler {
|
|
|
220
261
|
/**
|
|
221
262
|
* The URLs visited while following redirects, shared by reference from the
|
|
222
263
|
* redirect handler through the request context. Carries the suppressed hops'
|
|
223
|
-
*
|
|
264
|
+
* status/headers under {@link kRedirectCacheControl}.
|
|
224
265
|
* @type {URL[] | undefined}
|
|
225
266
|
*/
|
|
226
267
|
#history
|
|
@@ -296,35 +337,62 @@ class CacheHandler {
|
|
|
296
337
|
return downstreamOnHeaders()
|
|
297
338
|
}
|
|
298
339
|
|
|
340
|
+
// A credential-bearing request that was redirected must not have the final
|
|
341
|
+
// hop's response stored under the original request's key. The cache key
|
|
342
|
+
// does not include the credential, and the redirect target may vary per
|
|
343
|
+
// principal (e.g. `/me` -> `/profile/{user}`), so replaying the body under
|
|
344
|
+
// the alias key would leak one principal's data to another — or to an
|
|
345
|
+
// unauthenticated caller — from a shared cache. The final hop's permissive
|
|
346
|
+
// cache-control (e.g. `public`) does not authorize this: the credential was
|
|
347
|
+
// presented to the original hop, not the resource that actually produced
|
|
348
|
+
// the body. Fail closed. A history with more than one entry means at least
|
|
349
|
+
// one redirect was actually followed (a single non-redirected hop records
|
|
350
|
+
// exactly one entry), so a plain revalidation is not mistaken for a redirect.
|
|
351
|
+
if (
|
|
352
|
+
this.#history && this.#history.length > 1 &&
|
|
353
|
+
requestCarriesCredentials(this.#cacheKey.headers)
|
|
354
|
+
) {
|
|
355
|
+
return downstreamOnHeaders()
|
|
356
|
+
}
|
|
357
|
+
|
|
299
358
|
// A cross-origin redirect can change the origin that actually produced the
|
|
300
359
|
// final response. Keying that response on the original (trusted) origin
|
|
301
360
|
// would let a redirect target control an entry for a URL the application
|
|
302
|
-
// trusts.
|
|
303
|
-
//
|
|
361
|
+
// trusts. Every hop in the redirect chain must stay on the cache key's
|
|
362
|
+
// origin. An intermediate cross-origin hop can bounce back to the trusted
|
|
363
|
+
// origin (or to any cacheable asset there) even when the final response
|
|
364
|
+
// comes from the trusted origin, so checking only the final URL is not
|
|
365
|
+
// enough.
|
|
304
366
|
if (this.#history && this.#history.length > 0) {
|
|
305
|
-
const
|
|
306
|
-
|
|
307
|
-
return downstreamOnHeaders()
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// A redirect hop that is not forwarded can still carry a restrictive
|
|
312
|
-
// storage policy (for example `no-store`, or `private` for a shared cache).
|
|
313
|
-
// Honor it, otherwise a redirect that must not be stored could be laundered
|
|
314
|
-
// into a cacheable final response under the original key.
|
|
315
|
-
const redirectCacheControl = this.#history?.[kRedirectCacheControl]
|
|
316
|
-
if (redirectCacheControl) {
|
|
317
|
-
for (const hopCacheControl of redirectCacheControl) {
|
|
318
|
-
const hopDirectives = hopCacheControl ? parseCacheControlHeader(hopCacheControl) : {}
|
|
319
|
-
if (
|
|
320
|
-
hopDirectives['no-store'] === true ||
|
|
321
|
-
(this.#cacheType === 'shared' && hopDirectives.private === true)
|
|
322
|
-
) {
|
|
367
|
+
for (const hopUrl of this.#history) {
|
|
368
|
+
if (!sameOrigin(hopUrl.origin, this.#cacheKey.origin)) {
|
|
323
369
|
return downstreamOnHeaders()
|
|
324
370
|
}
|
|
325
371
|
}
|
|
326
372
|
}
|
|
327
373
|
|
|
374
|
+
// A redirect hop that is not forwarded can still carry a storage policy
|
|
375
|
+
// (for example `no-store`, `private` for a shared cache, `no-cache`,
|
|
376
|
+
// `must-revalidate`, zero freshness, or `Vary: *`) that the final response
|
|
377
|
+
// body does not inherit. Honor it, otherwise a redirect that must not be
|
|
378
|
+
// stored (or that selects a private/authenticated target) could be
|
|
379
|
+
// laundered into a cacheable final response under the original key.
|
|
380
|
+
const now = Date.now()
|
|
381
|
+
const redirectHopInfo = this.#history?.[kRedirectCacheControl]
|
|
382
|
+
const redirectPolicy = redirectHopInfo?.length > 0
|
|
383
|
+
? getRedirectPolicy(
|
|
384
|
+
this.#cacheType,
|
|
385
|
+
this.#cacheKey.method,
|
|
386
|
+
redirectHopInfo,
|
|
387
|
+
this.#cacheKey.headers,
|
|
388
|
+
this.#cacheByDefault,
|
|
389
|
+
now
|
|
390
|
+
)
|
|
391
|
+
: undefined
|
|
392
|
+
if (redirectHopInfo?.length > 0 && redirectPolicy === undefined) {
|
|
393
|
+
return downstreamOnHeaders()
|
|
394
|
+
}
|
|
395
|
+
|
|
328
396
|
const cacheControlHeader = resHeaders['cache-control']
|
|
329
397
|
const cacheControlDirectives = cacheControlHeader ? parseCacheControlHeader(cacheControlHeader) : {}
|
|
330
398
|
|
|
@@ -357,7 +425,6 @@ class CacheHandler {
|
|
|
357
425
|
return downstreamOnHeaders()
|
|
358
426
|
}
|
|
359
427
|
|
|
360
|
-
const now = Date.now()
|
|
361
428
|
const resAge = Object.hasOwn(resHeaders, 'age') ? getAge(resHeaders.age) : undefined
|
|
362
429
|
if (resAge !== undefined && resAge >= MAX_RESPONSE_AGE) {
|
|
363
430
|
// Response considered stale
|
|
@@ -394,24 +461,65 @@ class CacheHandler {
|
|
|
394
461
|
}
|
|
395
462
|
|
|
396
463
|
const baseTime = now - currentAge
|
|
397
|
-
|
|
464
|
+
let absoluteStaleAt = staleAt + baseTime
|
|
465
|
+
if (redirectPolicy !== undefined) {
|
|
466
|
+
absoluteStaleAt = Math.min(absoluteStaleAt, redirectPolicy.staleAt)
|
|
467
|
+
}
|
|
398
468
|
if (now >= absoluteStaleAt && !revalidationOnly) {
|
|
399
469
|
// Response is already stale
|
|
400
470
|
deleteCachedValueIfNotModified(statusCode, this.#store, this.#cacheKey)
|
|
401
471
|
return downstreamOnHeaders()
|
|
402
472
|
}
|
|
403
473
|
|
|
474
|
+
// Union the `Vary` of every suppressed redirect hop with the final
|
|
475
|
+
// response's `Vary`. A 3xx hop that negotiates its target on `Authorization`
|
|
476
|
+
// / `Cookie` (or any other header) must make the stored entry vary on those
|
|
477
|
+
// headers too, or an authenticated user's private response would be served
|
|
478
|
+
// to unrelated callers.
|
|
404
479
|
let varyDirectives
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
480
|
+
const combinedVary = []
|
|
481
|
+
if (redirectHopInfo) {
|
|
482
|
+
for (const hop of redirectHopInfo) {
|
|
483
|
+
appendVaryValues(combinedVary, hop?.headers?.vary)
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
appendVaryValues(combinedVary, resHeaders.vary)
|
|
487
|
+
if (combinedVary.length > 0) {
|
|
488
|
+
for (const varyHeader of combinedVary) {
|
|
489
|
+
if (hasVaryStar(varyHeader)) {
|
|
490
|
+
// `Vary: *` means the response can never be reused (RFC 9111 §4.1)
|
|
491
|
+
return downstreamOnHeaders()
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
if (this.#cacheKey.headers) {
|
|
495
|
+
varyDirectives = parseVaryHeader(combinedVary, this.#cacheKey.headers)
|
|
496
|
+
if (!varyDirectives) {
|
|
497
|
+
// Parse error
|
|
498
|
+
return downstreamOnHeaders()
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// When the entry was folded from a redirect chain, the final response's
|
|
504
|
+
// stale-serving directives (RFC 5861) must not outlive the hops' freshness.
|
|
505
|
+
// A hop that grants no `stale-while-revalidate` / `stale-if-error` caps
|
|
506
|
+
// stale serving at its freshness, so the final response cannot launder a
|
|
507
|
+
// stale-serving window past the hop-bounded `staleAt`. Drop those directives
|
|
508
|
+
// (and keep the `proxy-revalidate` the hop policy may require) so the entry
|
|
509
|
+
// revalidates at `staleAt` instead of being served stale for the final
|
|
510
|
+
// response's window.
|
|
511
|
+
let effectiveCacheControlDirectives = cacheControlDirectives
|
|
512
|
+
if (redirectPolicy !== undefined) {
|
|
513
|
+
effectiveCacheControlDirectives = { ...cacheControlDirectives }
|
|
514
|
+
delete effectiveCacheControlDirectives['stale-while-revalidate']
|
|
515
|
+
delete effectiveCacheControlDirectives['stale-if-error']
|
|
516
|
+
if (redirectPolicy.requiresRevalidation === true) {
|
|
517
|
+
effectiveCacheControlDirectives['proxy-revalidate'] = true
|
|
410
518
|
}
|
|
411
519
|
}
|
|
412
520
|
|
|
413
521
|
const cachedAt = baseTime
|
|
414
|
-
const deleteAt = determineDeleteAt(baseTime, now,
|
|
522
|
+
const deleteAt = determineDeleteAt(baseTime, now, effectiveCacheControlDirectives, absoluteStaleAt)
|
|
415
523
|
const strippedHeaders = stripNecessaryHeaders(resHeaders, cacheControlDirectives)
|
|
416
524
|
|
|
417
525
|
/**
|
|
@@ -422,7 +530,7 @@ class CacheHandler {
|
|
|
422
530
|
statusMessage,
|
|
423
531
|
headers: strippedHeaders,
|
|
424
532
|
vary: varyDirectives,
|
|
425
|
-
cacheControlDirectives,
|
|
533
|
+
cacheControlDirectives: effectiveCacheControlDirectives,
|
|
426
534
|
cachedAt,
|
|
427
535
|
staleAt: absoluteStaleAt,
|
|
428
536
|
deleteAt
|
|
@@ -440,6 +548,22 @@ class CacheHandler {
|
|
|
440
548
|
this.#downstreamController.release()
|
|
441
549
|
}
|
|
442
550
|
|
|
551
|
+
// The cached body below is replayed to the downstream handler from later
|
|
552
|
+
// stream ticks (or an iterator loop), outside the dispatcher's own error
|
|
553
|
+
// handling, so a throwing downstream handler must be converged to a
|
|
554
|
+
// per-request error instead of escaping as an uncaught exception.
|
|
555
|
+
const failReplay = (err) => {
|
|
556
|
+
if (done) {
|
|
557
|
+
return
|
|
558
|
+
}
|
|
559
|
+
done = true
|
|
560
|
+
// Abort the revalidation request: this releases the paused origin
|
|
561
|
+
// response (freeing the connection) and routes the error through
|
|
562
|
+
// CacheHandler.onResponseError, which destroys the cache write stream
|
|
563
|
+
// and forwards the error downstream exactly once.
|
|
564
|
+
controller.abort(err)
|
|
565
|
+
}
|
|
566
|
+
|
|
443
567
|
const handle304 = (cachedValue) => {
|
|
444
568
|
if (!cachedValue) {
|
|
445
569
|
// Do not create a new cache entry, as a 304 won't have a body - so cannot be cached.
|
|
@@ -471,15 +595,19 @@ class CacheHandler {
|
|
|
471
595
|
if (done) {
|
|
472
596
|
return
|
|
473
597
|
}
|
|
474
|
-
|
|
475
|
-
const
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
598
|
+
try {
|
|
599
|
+
for (const chunk of bodyIterator) {
|
|
600
|
+
const full = this.#writeStream?.write(chunk) === false
|
|
601
|
+
this.#handler.onResponseData?.(this.#downstreamController, chunk)
|
|
602
|
+
// when stream is full stop writing until we get a 'drain' event
|
|
603
|
+
if (full) {
|
|
604
|
+
return
|
|
605
|
+
}
|
|
480
606
|
}
|
|
607
|
+
finish304()
|
|
608
|
+
} catch (err) {
|
|
609
|
+
failReplay(err)
|
|
481
610
|
}
|
|
482
|
-
finish304()
|
|
483
611
|
}
|
|
484
612
|
|
|
485
613
|
this.#writeStream
|
|
@@ -505,8 +633,13 @@ class CacheHandler {
|
|
|
505
633
|
// Readable stream body (e.g. from async/remote cache stores)
|
|
506
634
|
cachedValue.body
|
|
507
635
|
.on('data', (chunk) => {
|
|
508
|
-
|
|
509
|
-
|
|
636
|
+
try {
|
|
637
|
+
this.#writeStream?.write(chunk)
|
|
638
|
+
this.#handler.onResponseData?.(this.#downstreamController, chunk)
|
|
639
|
+
} catch (err) {
|
|
640
|
+
cachedValue.body.destroy()
|
|
641
|
+
failReplay(err)
|
|
642
|
+
}
|
|
510
643
|
})
|
|
511
644
|
.on('end', () => {
|
|
512
645
|
this.#writeStream?.end()
|
|
@@ -713,6 +846,139 @@ function canCacheResponse (cacheType, method, statusCode, resHeaders, cacheContr
|
|
|
713
846
|
return true
|
|
714
847
|
}
|
|
715
848
|
|
|
849
|
+
/**
|
|
850
|
+
* Determine the absolute freshness deadline imposed by all suppressed redirect
|
|
851
|
+
* hops. Returning `undefined` means at least one hop cannot safely be folded
|
|
852
|
+
* into the final response under the original request key.
|
|
853
|
+
*
|
|
854
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType
|
|
855
|
+
* @param {string} method
|
|
856
|
+
* @param {RedirectHop[]} redirectHops
|
|
857
|
+
* @param {import('../../types/header.d.ts').IncomingHttpHeaders} reqHeaders
|
|
858
|
+
* @param {number | undefined} cacheByDefault
|
|
859
|
+
* @param {number} now
|
|
860
|
+
* @returns {{ staleAt: number, requiresRevalidation: boolean } | undefined}
|
|
861
|
+
*/
|
|
862
|
+
function getRedirectPolicy (cacheType, method, redirectHops, reqHeaders, cacheByDefault, now) {
|
|
863
|
+
let staleAt = Infinity
|
|
864
|
+
let requiresRevalidation = false
|
|
865
|
+
|
|
866
|
+
for (const hop of redirectHops) {
|
|
867
|
+
const hopPolicy = getRedirectHopPolicy(cacheType, method, hop, reqHeaders, cacheByDefault)
|
|
868
|
+
if (hopPolicy === undefined || hopPolicy.staleAt <= now) {
|
|
869
|
+
return undefined
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
staleAt = Math.min(staleAt, hopPolicy.staleAt)
|
|
873
|
+
requiresRevalidation ||= hopPolicy.requiresRevalidation
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
return { staleAt, requiresRevalidation }
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
/**
|
|
880
|
+
* Calculate one redirect hop's absolute freshness deadline using the same age,
|
|
881
|
+
* date, expiration, invalid-directive, and heuristic rules as a final response.
|
|
882
|
+
*
|
|
883
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType
|
|
884
|
+
* @param {string} method
|
|
885
|
+
* @param {RedirectHop} hop
|
|
886
|
+
* @param {import('../../types/header.d.ts').IncomingHttpHeaders} reqHeaders
|
|
887
|
+
* @param {number | undefined} cacheByDefault
|
|
888
|
+
* @returns {{ staleAt: number, requiresRevalidation: boolean } | undefined}
|
|
889
|
+
*/
|
|
890
|
+
function getRedirectHopPolicy (cacheType, method, hop, reqHeaders, cacheByDefault) {
|
|
891
|
+
if (!Number.isInteger(hop?.statusCode) || !Number.isFinite(hop?.receivedAt)) {
|
|
892
|
+
return undefined
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
const hopHeaders = hop.headers ?? {}
|
|
896
|
+
const cacheControlHeader = hopHeaders['cache-control']
|
|
897
|
+
const hopCacheControl = cacheControlHeader
|
|
898
|
+
? parseCacheControlHeader(cacheControlHeader)
|
|
899
|
+
: {}
|
|
900
|
+
|
|
901
|
+
// https://www.rfc-editor.org/rfc/rfc9111.html#section-4.1-5
|
|
902
|
+
if (hopHeaders.vary && hasVaryStar(hopHeaders.vary)) {
|
|
903
|
+
return undefined
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
if (hopCacheControl['no-store'] === true) {
|
|
907
|
+
return undefined
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
// A qualified `private` directive still marks the named fields as private.
|
|
911
|
+
// Folding a redirect that says `private="location"` would preserve the
|
|
912
|
+
// private target choice in the final body even though a shared cache is not
|
|
913
|
+
// allowed to store that Location field.
|
|
914
|
+
if (cacheType === 'shared' && Object.hasOwn(hopCacheControl, 'private')) {
|
|
915
|
+
return undefined
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
// These directives require the hop to be revalidated before reuse. Since the
|
|
919
|
+
// cache stores only the final response, it cannot independently validate the
|
|
920
|
+
// suppressed redirect response.
|
|
921
|
+
if (
|
|
922
|
+
Object.hasOwn(hopCacheControl, 'no-cache') ||
|
|
923
|
+
hopCacheControl['must-revalidate'] === true ||
|
|
924
|
+
(cacheType === 'shared' && hopCacheControl['proxy-revalidate'] === true)
|
|
925
|
+
) {
|
|
926
|
+
return undefined
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
const heuristicallyCacheable =
|
|
930
|
+
hopHeaders['last-modified'] &&
|
|
931
|
+
arrayIncludes(HEURISTICALLY_CACHEABLE_STATUS_CODES, hop.statusCode)
|
|
932
|
+
if (
|
|
933
|
+
!cacheControlHeader &&
|
|
934
|
+
!hopHeaders.expires &&
|
|
935
|
+
!heuristicallyCacheable &&
|
|
936
|
+
!cacheByDefault
|
|
937
|
+
) {
|
|
938
|
+
return undefined
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
if (!canCacheResponse(cacheType, method, hop.statusCode, hopHeaders, hopCacheControl, reqHeaders)) {
|
|
942
|
+
return undefined
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
const age = Object.hasOwn(hopHeaders, 'age') ? getAge(hopHeaders.age) : undefined
|
|
946
|
+
if (age !== undefined && age >= MAX_RESPONSE_AGE) {
|
|
947
|
+
return undefined
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
const responseDate = Object.hasOwn(hopHeaders, 'date') ? getDate(hopHeaders.date) : undefined
|
|
951
|
+
if (responseDate === null) {
|
|
952
|
+
return undefined
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
const apparentAge = responseDate ? Math.max(0, hop.receivedAt - responseDate.getTime()) : 0
|
|
956
|
+
const currentAge = Math.max(apparentAge, age ?? 0)
|
|
957
|
+
const hasValidator =
|
|
958
|
+
(typeof hopHeaders.etag === 'string' && isEtagUsable(hopHeaders.etag)) ||
|
|
959
|
+
typeof hopHeaders['last-modified'] === 'string'
|
|
960
|
+
const freshnessLifetime =
|
|
961
|
+
determineStaleAt(
|
|
962
|
+
cacheType,
|
|
963
|
+
hop.receivedAt,
|
|
964
|
+
age,
|
|
965
|
+
hopHeaders,
|
|
966
|
+
responseDate,
|
|
967
|
+
hopCacheControl,
|
|
968
|
+
hasValidator
|
|
969
|
+
) ?? cacheByDefault
|
|
970
|
+
|
|
971
|
+
if (freshnessLifetime === undefined) {
|
|
972
|
+
return undefined
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
return {
|
|
976
|
+
staleAt: hop.receivedAt - currentAge + freshnessLifetime,
|
|
977
|
+
// RFC 9111 §5.2.2.10: s-maxage implies proxy-revalidate for shared caches.
|
|
978
|
+
requiresRevalidation: cacheType === 'shared' && Object.hasOwn(hopCacheControl, 's-maxage')
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
|
|
716
982
|
/**
|
|
717
983
|
* @param {string | string[]} dateHeader
|
|
718
984
|
* @returns {Date | null | undefined}
|
|
@@ -32,9 +32,13 @@ module.exports = class DecoratorHandler {
|
|
|
32
32
|
onResponseStart (...args) {
|
|
33
33
|
assert(!this.#onCompleteCalled)
|
|
34
34
|
assert(!this.#onErrorCalled)
|
|
35
|
-
assert(!this.#onResponseStartCalled)
|
|
36
35
|
|
|
37
|
-
|
|
36
|
+
// Interim 1xx responses (e.g. 103 Early Hints) come before the final one.
|
|
37
|
+
const statusCode = args[1]
|
|
38
|
+
if (statusCode >= 200) {
|
|
39
|
+
assert(!this.#onResponseStartCalled)
|
|
40
|
+
this.#onResponseStartCalled = true
|
|
41
|
+
}
|
|
38
42
|
|
|
39
43
|
return this.#handler.onResponseStart?.(...args)
|
|
40
44
|
}
|
|
@@ -38,11 +38,12 @@ class RedirectHandler {
|
|
|
38
38
|
this.maxRedirections = maxRedirections
|
|
39
39
|
this.handler = handler
|
|
40
40
|
this.history = []
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
//
|
|
44
|
-
//
|
|
45
|
-
//
|
|
41
|
+
// Per-hop metadata for each intermediate (suppressed) redirect hop: the
|
|
42
|
+
// hop's status code and response headers. An outer cache uses it to honor a
|
|
43
|
+
// hop's storage policy (e.g. `no-store`, `no-cache`, `Vary: *`) that would
|
|
44
|
+
// otherwise be lost because the hop's response is not forwarded. It travels
|
|
45
|
+
// as a non-enumerable property of the history array so it reaches the cache
|
|
46
|
+
// without becoming part of the public request/response context.
|
|
46
47
|
Object.defineProperty(this.history, kRedirectCacheControl, {
|
|
47
48
|
value: [],
|
|
48
49
|
enumerable: false
|
|
@@ -124,9 +125,13 @@ class RedirectHandler {
|
|
|
124
125
|
}
|
|
125
126
|
|
|
126
127
|
// This hop's response is suppressed (only its Location is used). Record its
|
|
127
|
-
//
|
|
128
|
-
// otherwise be lost when the final response is stored.
|
|
129
|
-
this.history[kRedirectCacheControl].push(
|
|
128
|
+
// status and headers so an outer cache can honor a restrictive policy that
|
|
129
|
+
// would otherwise be lost when the final response is stored.
|
|
130
|
+
this.history[kRedirectCacheControl].push({
|
|
131
|
+
statusCode,
|
|
132
|
+
headers: { ...headers },
|
|
133
|
+
receivedAt: Date.now()
|
|
134
|
+
})
|
|
130
135
|
|
|
131
136
|
const baseUrl = requestOrigin
|
|
132
137
|
? new URL(this.opts.path, requestOrigin)
|
|
@@ -241,9 +246,25 @@ function normalizeStripHeaders (headers, optionName) {
|
|
|
241
246
|
function cleanRequestHeaders (headers, removeContent, unknownOrigin, stripHeaders, stripHeadersOnCrossOrigin) {
|
|
242
247
|
const ret = []
|
|
243
248
|
if (Array.isArray(headers)) {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
249
|
+
// Headers may be either a flat alternating [name, value, name, value, ...]
|
|
250
|
+
// list or an iterable of [name, value] pairs. A plain JavaScript array of
|
|
251
|
+
// tuples (e.g. `Object.entries(headers)`) satisfies the iterable-of-pairs
|
|
252
|
+
// public type, so we must distinguish the two representations before
|
|
253
|
+
// iterating. Treating every Array as a flat list misreads each tuple as a
|
|
254
|
+
// header name/value pair and defeats the cross-origin credential stripping.
|
|
255
|
+
if (headers.length > 0 && Array.isArray(headers[0])) {
|
|
256
|
+
// Array of [name, value] pairs.
|
|
257
|
+
for (const [key, value] of headers) {
|
|
258
|
+
if (!shouldRemoveHeader(key, removeContent, unknownOrigin, stripHeaders, stripHeadersOnCrossOrigin)) {
|
|
259
|
+
ret.push(key, value)
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
// Flat alternating [name, value, name, value, ...] list.
|
|
264
|
+
for (let i = 0; i < headers.length; i += 2) {
|
|
265
|
+
if (!shouldRemoveHeader(headers[i], removeContent, unknownOrigin, stripHeaders, stripHeadersOnCrossOrigin)) {
|
|
266
|
+
ret.push(headers[i], headers[i + 1])
|
|
267
|
+
}
|
|
247
268
|
}
|
|
248
269
|
}
|
|
249
270
|
} else if (headers && typeof headers === 'object') {
|