Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Nov 25, 2024
1 parent 7e539df commit 9d2bf4f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/cache/memory-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion lib/dispatcher/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions lib/handler/cache-revalidation-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const assert = require('node:assert')
class CacheRevalidationHandler {
#successful = false
/**
* @type {((boolean) => void) | null}
* @type {((boolean, any) => void) | null}
*/
#callback
/**
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
17 changes: 9 additions & 8 deletions lib/interceptor/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? [])
Expand All @@ -161,6 +161,9 @@ module.exports = (opts = {}) => {
pause () {
stream.pause()
},
get paused () {
return stream.isPaused()
},
get aborted () {
return stream.destroyed
},
Expand Down Expand Up @@ -188,18 +191,15 @@ module.exports = (opts = {}) => {
}
})

handler.onRequestStart?.(controller)
handler.onRequestStart?.(controller, context)

if (stream.destroyed) {
return
}

// 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)

Expand Down Expand Up @@ -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()
}
Expand All @@ -264,6 +264,7 @@ module.exports = (opts = {}) => {
if (util.isStream(opts.body)) {
opts.body.on('error', () => {}).destroy()
}

respondWithCachedValue(result, age)
}

Expand Down
8 changes: 4 additions & 4 deletions test/cache-interceptor/cache-store-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -144,7 +144,7 @@ function cacheStoreTests (CacheStore) {
statusCode: 200,
statusMessage: '',
cachedAt: Date.now() - 20000,
rawHeaders: [],
headers: {},
staleAt: Date.now() - 10000,
deleteAt: Date.now() - 5
}
Expand Down
4 changes: 2 additions & 2 deletions test/types/cache-interceptor.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ expectAssignable<CacheInterceptor.CacheOptions>({ store, methods: ['GET'] })
expectAssignable<CacheInterceptor.CacheValue>({
statusCode: 200,
statusMessage: 'OK',
rawHeaders: [],
headers: {},
cachedAt: 0,
staleAt: 0,
deleteAt: 0
Expand All @@ -33,7 +33,7 @@ expectAssignable<CacheInterceptor.CacheValue>({
expectAssignable<CacheInterceptor.CacheValue>({
statusCode: 200,
statusMessage: 'OK',
rawHeaders: [],
headers: {},
vary: {},
cachedAt: 0,
staleAt: 0,
Expand Down

0 comments on commit 9d2bf4f

Please sign in to comment.