From f0cf2a85c38701eb900bac4996c418c41ef0047e Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 25 Nov 2024 09:52:34 +0100 Subject: [PATCH] fixup --- lib/dispatcher/dispatcher.js | 1 - lib/handler/cache-revalidation-handler.js | 6 +++--- lib/interceptor/cache.js | 13 +++++-------- test/types/cache-interceptor.test-d.ts | 4 ++-- 4 files changed, 10 insertions(+), 14 deletions(-) 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..2d4f9c319aa 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 ?? []) @@ -188,7 +188,7 @@ module.exports = (opts = {}) => { } }) - handler.onRequestStart?.(controller) + handler.onRequestStart?.(controller, context) if (stream.destroyed) { return @@ -196,10 +196,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) @@ -249,8 +246,8 @@ module.exports = (opts = {}) => { }, new CacheRevalidationHandler( (success) => { - if (success) { - respondWithCachedValue(result, age) + if (success, context) { + respondWithCachedValue(result, age, context) } else if (util.isStream(result.body)) { result.body.on('error', () => {}).destroy() } 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,