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 f0cf2a8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
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
13 changes: 5 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 Down Expand Up @@ -188,18 +188,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 @@ -249,8 +246,8 @@ module.exports = (opts = {}) => {
},
new CacheRevalidationHandler(
(success) => {
if (success) {
respondWithCachedValue(result, age)
if (success, context) {

Check failure on line 249 in lib/interceptor/cache.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected use of comma operator

Check failure on line 249 in lib/interceptor/cache.js

View workflow job for this annotation

GitHub Actions / Lint

'context' is not defined
respondWithCachedValue(result, age, context)

Check failure on line 250 in lib/interceptor/cache.js

View workflow job for this annotation

GitHub Actions / Lint

'context' is not defined
} else if (util.isStream(result.body)) {
result.body.on('error', () => {}).destroy()
}
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 f0cf2a8

Please # to comment.