From 9d2bf4fa88804d9c9dcf4f2ae9d53675b30104e6 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 25 Nov 2024 09:52:34 +0100 Subject: [PATCH] fixup --- lib/cache/memory-cache-store.js | 2 +- lib/dispatcher/dispatcher.js | 1 - lib/handler/cache-revalidation-handler.js | 6 +++--- lib/interceptor/cache.js | 17 +++++++++-------- .../cache-interceptor/cache-store-test-utils.js | 8 ++++---- test/types/cache-interceptor.test-d.ts | 4 ++-- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/cache/memory-cache-store.js b/lib/cache/memory-cache-store.js index aa552da2abd..78c1c90439c 100644 --- a/lib/cache/memory-cache-store.js +++ b/lib/cache/memory-cache-store.js @@ -87,7 +87,7 @@ class MemoryCacheStore { : { statusMessage: entry.statusMessage, statusCode: entry.statusCode, - rawHeaders: entry.rawHeaders, + headers: entry.headers, body: entry.body, etag: entry.etag, cachedAt: entry.cachedAt, diff --git a/lib/dispatcher/dispatcher.js b/lib/dispatcher/dispatcher.js index a74cf06af6f..824dfb6d822 100644 --- a/lib/dispatcher/dispatcher.js +++ b/lib/dispatcher/dispatcher.js @@ -31,7 +31,6 @@ class Dispatcher extends EventEmitter { throw new TypeError(`invalid interceptor, expected function received ${typeof interceptor}`) } - dispatch = wrapInterceptor(dispatch) dispatch = interceptor(dispatch) dispatch = wrapInterceptor(dispatch) diff --git a/lib/handler/cache-revalidation-handler.js b/lib/handler/cache-revalidation-handler.js index 956e4c2045e..1175a2c4f91 100644 --- a/lib/handler/cache-revalidation-handler.js +++ b/lib/handler/cache-revalidation-handler.js @@ -18,7 +18,7 @@ const assert = require('node:assert') class CacheRevalidationHandler { #successful = false /** - * @type {((boolean) => void) | null} + * @type {((boolean, any) => void) | null} */ #callback /** @@ -29,7 +29,7 @@ class CacheRevalidationHandler { #context /** - * @param {(boolean) => void} callback Function to call if the cached value is valid + * @param {(boolean, any) => void} callback Function to call if the cached value is valid * @param {import('../../types/dispatcher.d.ts').default.DispatchHandlers} handler */ constructor (callback, handler) { @@ -56,7 +56,7 @@ class CacheRevalidationHandler { // https://www.rfc-editor.org/rfc/rfc9111.html#name-handling-a-validation-respo this.#successful = statusCode === 304 - this.#callback(this.#successful) + this.#callback(this.#successful, this.#context) this.#callback = null if (this.#successful) { diff --git a/lib/interceptor/cache.js b/lib/interceptor/cache.js index f8474add775..aa147f78fed 100644 --- a/lib/interceptor/cache.js +++ b/lib/interceptor/cache.js @@ -146,7 +146,7 @@ module.exports = (opts = {}) => { * @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result * @param {number} age */ - const respondWithCachedValue = ({ headers, statusCode, statusMessage, body }, age) => { + const respondWithCachedValue = ({ headers, statusCode, statusMessage, body }, age, context) => { const stream = util.isStream(body) ? body : Readable.from(body ?? []) @@ -161,6 +161,9 @@ module.exports = (opts = {}) => { pause () { stream.pause() }, + get paused () { + return stream.isPaused() + }, get aborted () { return stream.destroyed }, @@ -188,7 +191,7 @@ module.exports = (opts = {}) => { } }) - handler.onRequestStart?.(controller) + handler.onRequestStart?.(controller, context) if (stream.destroyed) { return @@ -196,10 +199,7 @@ module.exports = (opts = {}) => { // Add the age header // https://www.rfc-editor.org/rfc/rfc9111.html#name-age - headers = { - ...headers, - age: String(Math.round((Date.now() - result.cachedAt) / 1000)) - } + headers = age ? { ...headers, age: String(age) } : headers handler.onResponseStart?.(controller, statusCode, statusMessage, headers) @@ -248,9 +248,9 @@ module.exports = (opts = {}) => { } }, new CacheRevalidationHandler( - (success) => { + (success, context) => { if (success) { - respondWithCachedValue(result, age) + respondWithCachedValue(result, age, context) } else if (util.isStream(result.body)) { result.body.on('error', () => {}).destroy() } @@ -264,6 +264,7 @@ module.exports = (opts = {}) => { if (util.isStream(opts.body)) { opts.body.on('error', () => {}).destroy() } + respondWithCachedValue(result, age) } diff --git a/test/cache-interceptor/cache-store-test-utils.js b/test/cache-interceptor/cache-store-test-utils.js index 09cbb1be08d..997c7ca4938 100644 --- a/test/cache-interceptor/cache-store-test-utils.js +++ b/test/cache-interceptor/cache-store-test-utils.js @@ -31,7 +31,7 @@ function cacheStoreTests (CacheStore) { const requestValue = { statusCode: 200, statusMessage: '', - rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')], + headers: { foo: 'bar' }, cachedAt: Date.now(), staleAt: Date.now() + 10000, deleteAt: Date.now() + 20000 @@ -71,7 +71,7 @@ function cacheStoreTests (CacheStore) { const anotherValue = { statusCode: 200, statusMessage: '', - rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')], + headers: { foo: 'bar' }, cachedAt: Date.now(), staleAt: Date.now() + 10000, deleteAt: Date.now() + 20000 @@ -109,7 +109,7 @@ function cacheStoreTests (CacheStore) { const requestValue = { statusCode: 200, statusMessage: '', - rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')], + headers: { foo: 'bar' }, cachedAt: Date.now() - 10000, staleAt: Date.now() - 1, deleteAt: Date.now() + 20000 @@ -144,7 +144,7 @@ function cacheStoreTests (CacheStore) { statusCode: 200, statusMessage: '', cachedAt: Date.now() - 20000, - rawHeaders: [], + headers: {}, staleAt: Date.now() - 10000, deleteAt: Date.now() - 5 } diff --git a/test/types/cache-interceptor.test-d.ts b/test/types/cache-interceptor.test-d.ts index 3bbd35a3163..b219a528750 100644 --- a/test/types/cache-interceptor.test-d.ts +++ b/test/types/cache-interceptor.test-d.ts @@ -24,7 +24,7 @@ expectAssignable({ store, methods: ['GET'] }) expectAssignable({ statusCode: 200, statusMessage: 'OK', - rawHeaders: [], + headers: {}, cachedAt: 0, staleAt: 0, deleteAt: 0 @@ -33,7 +33,7 @@ expectAssignable({ expectAssignable({ statusCode: 200, statusMessage: 'OK', - rawHeaders: [], + headers: {}, vary: {}, cachedAt: 0, staleAt: 0,