1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
'use strict'
const assert = require('node:assert')
const { Readable } = require('node:stream')
const util = require('../core/util')
const CacheHandler = require('../handler/cache-handler')
const MemoryCacheStore = require('../cache/memory-cache-store')
const CacheRevalidationHandler = require('../handler/cache-revalidation-handler')
const { assertCacheStore, assertCacheMethods, makeCacheKey, normalizeHeaders, parseCacheControlHeader } = require('../util/cache.js')
const { AbortError } = require('../core/errors.js')
/**
* @param {(string | RegExp)[] | undefined} origins
* @param {string} name
*/
function assertCacheOrigins (origins, name) {
if (origins === undefined) return
if (!Array.isArray(origins)) {
throw new TypeError(`expected ${name} to be an array or undefined, got ${typeof origins}`)
}
for (let i = 0; i < origins.length; i++) {
const origin = origins[i]
if (typeof origin !== 'string' && !(origin instanceof RegExp)) {
throw new TypeError(`expected ${name}[${i}] to be a string or RegExp, got ${typeof origin}`)
}
}
}
const nop = () => {}
/**
* @typedef {(options: import('../../types/dispatcher.d.ts').default.DispatchOptions, handler: import('../../types/dispatcher.d.ts').default.DispatchHandler) => void} DispatchFn
*/
/**
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
* @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives | undefined} cacheControlDirectives
* @param {import('../../types/dispatcher.d.ts').default.RequestOptions} opts
* @returns {boolean}
*/
function needsRevalidation (result, cacheControlDirectives, { headers = {} }) {
// Always revalidate requests with the no-cache request directive.
if (cacheControlDirectives?.['no-cache']) {
return true
}
// Always revalidate requests with unqualified no-cache response directive.
if (result.cacheControlDirectives?.['no-cache'] && !Array.isArray(result.cacheControlDirectives['no-cache'])) {
return true
}
// Always revalidate requests with conditional headers.
if (headers['if-modified-since'] || headers['if-none-match']) {
return true
}
return false
}
/**
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
* @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives | undefined} cacheControlDirectives
* @returns {boolean}
*/
function isStale (result, cacheControlDirectives) {
const now = Date.now()
if (now > result.staleAt) {
// Response is stale
if (cacheControlDirectives?.['max-stale']) {
// There's a threshold where we can serve stale responses, let's see if
// we're in it
// https://www.rfc-editor.org/rfc/rfc9111.html#name-max-stale
const gracePeriod = result.staleAt + (cacheControlDirectives['max-stale'] * 1000)
return now > gracePeriod
}
return true
}
if (cacheControlDirectives?.['min-fresh']) {
// https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.1.3
// At this point, staleAt is always > now
const timeLeftTillStale = result.staleAt - now
const threshold = cacheControlDirectives['min-fresh'] * 1000
return timeLeftTillStale <= threshold
}
return false
}
/**
* Check if we're within the stale-while-revalidate window for a stale response
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
* @returns {boolean}
*/
function withinStaleWhileRevalidateWindow (result) {
const staleWhileRevalidate = result.cacheControlDirectives?.['stale-while-revalidate']
if (!staleWhileRevalidate) {
return false
}
const now = Date.now()
const staleWhileRevalidateExpiry = result.staleAt + (staleWhileRevalidate * 1000)
return now <= staleWhileRevalidateExpiry
}
/**
* @param {DispatchFn} dispatch
* @param {import('../../types/cache-interceptor.d.ts').default.CacheHandlerOptions} globalOpts
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} cacheKey
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandler} handler
* @param {import('../../types/dispatcher.d.ts').default.RequestOptions} opts
* @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives | undefined} reqCacheControl
*/
function handleUncachedResponse (
dispatch,
globalOpts,
cacheKey,
handler,
opts,
reqCacheControl
) {
if (reqCacheControl?.['only-if-cached']) {
let aborted = false
try {
if (typeof handler.onConnect === 'function') {
handler.onConnect(() => {
aborted = true
})
if (aborted) {
return
}
}
if (typeof handler.onHeaders === 'function') {
handler.onHeaders(504, [], nop, 'Gateway Timeout')
if (aborted) {
return
}
}
if (typeof handler.onComplete === 'function') {
handler.onComplete([])
}
} catch (err) {
if (typeof handler.onError === 'function') {
handler.onError(err)
}
}
return true
}
return dispatch(opts, new CacheHandler(globalOpts, cacheKey, handler))
}
/**
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandler} handler
* @param {import('../../types/dispatcher.d.ts').default.RequestOptions} opts
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
* @param {number} age
* @param {any} context
* @param {boolean} isStale
*/
function sendCachedValue (handler, opts, result, age, context, isStale) {
// TODO (perf): Readable.from path can be optimized...
const stream = util.isStream(result.body)
? result.body
: Readable.from(result.body ?? [])
assert(!stream.destroyed, 'stream should not be destroyed')
assert(!stream.readableDidRead, 'stream should not be readableDidRead')
const controller = {
resume () {
stream.resume()
},
pause () {
stream.pause()
},
get paused () {
return stream.isPaused()
},
get aborted () {
return stream.destroyed
},
get reason () {
return stream.errored
},
abort (reason) {
stream.destroy(reason ?? new AbortError())
}
}
stream
.on('error', function (err) {
if (!this.readableEnded) {
if (typeof handler.onResponseError === 'function') {
handler.onResponseError(controller, err)
} else {
throw err
}
}
})
.on('close', function () {
if (!this.errored) {
handler.onResponseEnd?.(controller, {})
}
})
handler.onRequestStart?.(controller, context)
if (stream.destroyed) {
return
}
// Add the age header
// https://www.rfc-editor.org/rfc/rfc9111.html#name-age
const headers = { ...result.headers, age: String(age) }
if (isStale) {
// Add warning header
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Warning
headers.warning = '110 - "response is stale"'
}
handler.onResponseStart?.(controller, result.statusCode, headers, result.statusMessage)
if (opts.method === 'HEAD') {
stream.destroy()
} else {
stream.on('data', function (chunk) {
handler.onResponseData?.(controller, chunk)
})
}
}
/**
* @param {DispatchFn} dispatch
* @param {import('../../types/cache-interceptor.d.ts').default.CacheHandlerOptions} globalOpts
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} cacheKey
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandler} handler
* @param {import('../../types/dispatcher.d.ts').default.RequestOptions} opts
* @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives | undefined} reqCacheControl
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult | undefined} result
*/
function handleResult (
dispatch,
globalOpts,
cacheKey,
handler,
opts,
reqCacheControl,
result
) {
if (!result) {
return handleUncachedResponse(dispatch, globalOpts, cacheKey, handler, opts, reqCacheControl)
}
const now = Date.now()
if (now > result.deleteAt) {
// Response is expired, cache store shouldn't have given this to us
return dispatch(opts, new CacheHandler(globalOpts, cacheKey, handler))
}
const age = Math.round((now - result.cachedAt) / 1000)
if (reqCacheControl?.['max-age'] && age >= reqCacheControl['max-age']) {
// Response is considered expired for this specific request
// https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.1.1
return dispatch(opts, handler)
}
const stale = isStale(result, reqCacheControl)
const revalidate = needsRevalidation(result, reqCacheControl, opts)
// Check if the response is stale
if (stale || revalidate) {
if (util.isStream(opts.body) && util.bodyLength(opts.body) !== 0) {
// If body is a stream we can't revalidate...
// TODO (fix): This could be less strict...
return dispatch(opts, new CacheHandler(globalOpts, cacheKey, handler))
}
// RFC 5861: If we're within stale-while-revalidate window, serve stale immediately
// and revalidate in background, unless immediate revalidation is necessary
if (!revalidate && withinStaleWhileRevalidateWindow(result)) {
// Serve stale response immediately
sendCachedValue(handler, opts, result, age, null, true)
// Start background revalidation (fire-and-forget)
queueMicrotask(() => {
const headers = {
...opts.headers,
'if-modified-since': new Date(result.cachedAt).toUTCString()
}
if (result.etag) {
headers['if-none-match'] = result.etag
}
if (result.vary) {
for (const key in result.vary) {
if (result.vary[key] != null) {
headers[key] = result.vary[key]
}
}
}
// Background revalidation - update cache if we get new data
dispatch(
{
...opts,
headers
},
new CacheHandler(globalOpts, cacheKey, {
// Silent handler that just updates the cache
onRequestStart () {},
onRequestUpgrade () {},
onResponseStart () {},
onResponseData () {},
onResponseEnd () {},
onResponseError () {}
})
)
})
return true
}
let withinStaleIfErrorThreshold = false
const staleIfErrorExpiry = result.cacheControlDirectives['stale-if-error'] ?? reqCacheControl?.['stale-if-error']
if (staleIfErrorExpiry) {
withinStaleIfErrorThreshold = now < (result.staleAt + (staleIfErrorExpiry * 1000))
}
const headers = {
...opts.headers,
'if-modified-since': new Date(result.cachedAt).toUTCString()
}
if (result.etag) {
headers['if-none-match'] = result.etag
}
if (result.vary) {
for (const key in result.vary) {
if (result.vary[key] != null) {
headers[key] = result.vary[key]
}
}
}
// We need to revalidate the response
return dispatch(
{
...opts,
headers
},
new CacheRevalidationHandler(
(success, context) => {
if (success) {
// TODO: successful revalidation should be considered fresh (not give stale warning).
sendCachedValue(handler, opts, result, age, context, stale)
} else if (util.isStream(result.body)) {
result.body.on('error', nop).destroy()
}
},
new CacheHandler(globalOpts, cacheKey, handler),
withinStaleIfErrorThreshold
)
)
}
// Dump request body.
if (util.isStream(opts.body)) {
opts.body.on('error', nop).destroy()
}
sendCachedValue(handler, opts, result, age, null, false)
}
/**
* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions} [opts]
* @returns {import('../../types/dispatcher.d.ts').default.DispatcherComposeInterceptor}
*/
module.exports = (opts = {}) => {
const {
store = new MemoryCacheStore(),
methods = ['GET'],
cacheByDefault = undefined,
type = 'shared',
origins = undefined
} = opts
if (typeof opts !== 'object' || opts === null) {
throw new TypeError(`expected type of opts to be an Object, got ${opts === null ? 'null' : typeof opts}`)
}
assertCacheStore(store, 'opts.store')
assertCacheMethods(methods, 'opts.methods')
assertCacheOrigins(origins, 'opts.origins')
if (typeof cacheByDefault !== 'undefined' && typeof cacheByDefault !== 'number') {
throw new TypeError(`expected opts.cacheByDefault to be number or undefined, got ${typeof cacheByDefault}`)
}
if (typeof type !== 'undefined' && type !== 'shared' && type !== 'private') {
throw new TypeError(`expected opts.type to be shared, private, or undefined, got ${typeof type}`)
}
const globalOpts = {
store,
methods,
cacheByDefault,
type
}
const safeMethodsToNotCache = util.safeHTTPMethods.filter(method => methods.includes(method) === false)
return dispatch => {
return (opts, handler) => {
if (!opts.origin || safeMethodsToNotCache.includes(opts.method)) {
// Not a method we want to cache or we don't have the origin, skip
return dispatch(opts, handler)
}
// Check if origin is in whitelist
if (origins !== undefined) {
const requestOrigin = opts.origin.toString().toLowerCase()
let isAllowed = false
for (let i = 0; i < origins.length; i++) {
const allowed = origins[i]
if (typeof allowed === 'string') {
if (allowed.toLowerCase() === requestOrigin) {
isAllowed = true
break
}
} else if (allowed.test(requestOrigin)) {
isAllowed = true
break
}
}
if (!isAllowed) {
return dispatch(opts, handler)
}
}
opts = {
...opts,
headers: normalizeHeaders(opts)
}
const reqCacheControl = opts.headers?.['cache-control']
? parseCacheControlHeader(opts.headers['cache-control'])
: undefined
if (reqCacheControl?.['no-store']) {
return dispatch(opts, handler)
}
/**
* @type {import('../../types/cache-interceptor.d.ts').default.CacheKey}
*/
const cacheKey = makeCacheKey(opts)
const result = store.get(cacheKey)
if (result && typeof result.then === 'function') {
return result
.then(result => handleResult(dispatch,
globalOpts,
cacheKey,
handler,
opts,
reqCacheControl,
result
))
} else {
return handleResult(
dispatch,
globalOpts,
cacheKey,
handler,
opts,
reqCacheControl,
result
)
}
}
}
}
|