Skip to content

Commit 9d2bf4f

Browse files
committed
fixup
1 parent 7e539df commit 9d2bf4f

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

lib/cache/memory-cache-store.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class MemoryCacheStore {
8787
: {
8888
statusMessage: entry.statusMessage,
8989
statusCode: entry.statusCode,
90-
rawHeaders: entry.rawHeaders,
90+
headers: entry.headers,
9191
body: entry.body,
9292
etag: entry.etag,
9393
cachedAt: entry.cachedAt,

lib/dispatcher/dispatcher.js

-1
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

+3-3
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

+9-8
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 ?? [])
@@ -161,6 +161,9 @@ module.exports = (opts = {}) => {
161161
pause () {
162162
stream.pause()
163163
},
164+
get paused () {
165+
return stream.isPaused()
166+
},
164167
get aborted () {
165168
return stream.destroyed
166169
},
@@ -188,18 +191,15 @@ module.exports = (opts = {}) => {
188191
}
189192
})
190193

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

193196
if (stream.destroyed) {
194197
return
195198
}
196199

197200
// Add the age header
198201
// 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-
}
202+
headers = age ? { ...headers, age: String(age) } : headers
203203

204204
handler.onResponseStart?.(controller, statusCode, statusMessage, headers)
205205

@@ -248,9 +248,9 @@ module.exports = (opts = {}) => {
248248
}
249249
},
250250
new CacheRevalidationHandler(
251-
(success) => {
251+
(success, context) => {
252252
if (success) {
253-
respondWithCachedValue(result, age)
253+
respondWithCachedValue(result, age, context)
254254
} else if (util.isStream(result.body)) {
255255
result.body.on('error', () => {}).destroy()
256256
}
@@ -264,6 +264,7 @@ module.exports = (opts = {}) => {
264264
if (util.isStream(opts.body)) {
265265
opts.body.on('error', () => {}).destroy()
266266
}
267+
267268
respondWithCachedValue(result, age)
268269
}
269270

test/cache-interceptor/cache-store-test-utils.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function cacheStoreTests (CacheStore) {
3131
const requestValue = {
3232
statusCode: 200,
3333
statusMessage: '',
34-
rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')],
34+
headers: { foo: 'bar' },
3535
cachedAt: Date.now(),
3636
staleAt: Date.now() + 10000,
3737
deleteAt: Date.now() + 20000
@@ -71,7 +71,7 @@ function cacheStoreTests (CacheStore) {
7171
const anotherValue = {
7272
statusCode: 200,
7373
statusMessage: '',
74-
rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')],
74+
headers: { foo: 'bar' },
7575
cachedAt: Date.now(),
7676
staleAt: Date.now() + 10000,
7777
deleteAt: Date.now() + 20000
@@ -109,7 +109,7 @@ function cacheStoreTests (CacheStore) {
109109
const requestValue = {
110110
statusCode: 200,
111111
statusMessage: '',
112-
rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')],
112+
headers: { foo: 'bar' },
113113
cachedAt: Date.now() - 10000,
114114
staleAt: Date.now() - 1,
115115
deleteAt: Date.now() + 20000
@@ -144,7 +144,7 @@ function cacheStoreTests (CacheStore) {
144144
statusCode: 200,
145145
statusMessage: '',
146146
cachedAt: Date.now() - 20000,
147-
rawHeaders: [],
147+
headers: {},
148148
staleAt: Date.now() - 10000,
149149
deleteAt: Date.now() - 5
150150
}

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

+2-2
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)