Skip to content

Commit f0cf2a8

Browse files
committed
fixup
1 parent 7e539df commit f0cf2a8

File tree

4 files changed

+10
-14
lines changed

4 files changed

+10
-14
lines changed

lib/dispatcher/dispatcher.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class Dispatcher extends EventEmitter {
3131
throw new TypeError(`invalid interceptor, expected function received ${typeof interceptor}`)
3232
}
3333

34-
dispatch = wrapInterceptor(dispatch)
3534
dispatch = interceptor(dispatch)
3635
dispatch = wrapInterceptor(dispatch)
3736

lib/handler/cache-revalidation-handler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const assert = require('node:assert')
1818
class CacheRevalidationHandler {
1919
#successful = false
2020
/**
21-
* @type {((boolean) => void) | null}
21+
* @type {((boolean, any) => void) | null}
2222
*/
2323
#callback
2424
/**
@@ -29,7 +29,7 @@ class CacheRevalidationHandler {
2929
#context
3030

3131
/**
32-
* @param {(boolean) => void} callback Function to call if the cached value is valid
32+
* @param {(boolean, any) => void} callback Function to call if the cached value is valid
3333
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandlers} handler
3434
*/
3535
constructor (callback, handler) {
@@ -56,7 +56,7 @@ class CacheRevalidationHandler {
5656

5757
// https://www.rfc-editor.org/rfc/rfc9111.html#name-handling-a-validation-respo
5858
this.#successful = statusCode === 304
59-
this.#callback(this.#successful)
59+
this.#callback(this.#successful, this.#context)
6060
this.#callback = null
6161

6262
if (this.#successful) {

lib/interceptor/cache.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ module.exports = (opts = {}) => {
146146
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
147147
* @param {number} age
148148
*/
149-
const respondWithCachedValue = ({ headers, statusCode, statusMessage, body }, age) => {
149+
const respondWithCachedValue = ({ headers, statusCode, statusMessage, body }, age, context) => {
150150
const stream = util.isStream(body)
151151
? body
152152
: Readable.from(body ?? [])
@@ -188,18 +188,15 @@ module.exports = (opts = {}) => {
188188
}
189189
})
190190

191-
handler.onRequestStart?.(controller)
191+
handler.onRequestStart?.(controller, context)
192192

193193
if (stream.destroyed) {
194194
return
195195
}
196196

197197
// Add the age header
198198
// https://www.rfc-editor.org/rfc/rfc9111.html#name-age
199-
headers = {
200-
...headers,
201-
age: String(Math.round((Date.now() - result.cachedAt) / 1000))
202-
}
199+
headers = age ? { ...headers, age: String(age) } : headers
203200

204201
handler.onResponseStart?.(controller, statusCode, statusMessage, headers)
205202

@@ -249,8 +246,8 @@ module.exports = (opts = {}) => {
249246
},
250247
new CacheRevalidationHandler(
251248
(success) => {
252-
if (success) {
253-
respondWithCachedValue(result, age)
249+
if (success, context) {
250+
respondWithCachedValue(result, age, context)
254251
} else if (util.isStream(result.body)) {
255252
result.body.on('error', () => {}).destroy()
256253
}

test/types/cache-interceptor.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ expectAssignable<CacheInterceptor.CacheOptions>({ store, methods: ['GET'] })
2424
expectAssignable<CacheInterceptor.CacheValue>({
2525
statusCode: 200,
2626
statusMessage: 'OK',
27-
rawHeaders: [],
27+
headers: {},
2828
cachedAt: 0,
2929
staleAt: 0,
3030
deleteAt: 0
@@ -33,7 +33,7 @@ expectAssignable<CacheInterceptor.CacheValue>({
3333
expectAssignable<CacheInterceptor.CacheValue>({
3434
statusCode: 200,
3535
statusMessage: 'OK',
36-
rawHeaders: [],
36+
headers: {},
3737
vary: {},
3838
cachedAt: 0,
3939
staleAt: 0,

0 commit comments

Comments
 (0)