Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Nov 13, 2024
1 parent 9b65862 commit 2840fe7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 27 deletions.
5 changes: 1 addition & 4 deletions lib/cache/memory-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ class MemoryCacheStore {
return undefined
}

return {
response: value.opts,
body: value.body
}
return { ...value.opts, body: value.body }
}

/**
Expand Down
34 changes: 16 additions & 18 deletions lib/interceptor/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ module.exports = (opts = {}) => {
}

/**
* @param {import('node:stream').Readable} stream
* @param {import('../../types/cache-interceptor.d.ts').default.CachedResponse} value
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
*/
const respondWithCachedValue = (stream, { cachedAt, rawHeaders, statusCode, statusMessage }) => {
const respondWithCachedValue = ({ cachedAt, rawHeaders, statusCode, statusMessage, body }) => {
const stream = util.isStream(body)
? body
: Readable.from(body ?? [])

assert(!stream.destroyed, 'stream should not be destroyed')
assert(!stream.readableDidRead, 'stream should not be readableDidRead')

Expand Down Expand Up @@ -122,26 +125,21 @@ module.exports = (opts = {}) => {
/**
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
*/
const handleStream = (result) => {
const { response: value, body } = result

const handleResult = (result) => {
// TODO (perf): Readable.from path can be optimized...
const stream = util.isStream(body)
? body
: Readable.from(body ?? [])

if (!stream && opts.method !== 'HEAD') {
if (!result.body && opts.method !== 'HEAD') {
throw new Error('stream is undefined but method isn\'t HEAD')
}

// Check if the response is stale
const now = Date.now()
if (now < value.staleAt) {
if (now < result.staleAt) {
// Dump request body.
if (util.isStream(opts.body)) {
opts.body.on('error', () => {}).destroy()
}
respondWithCachedValue(stream, value)
respondWithCachedValue(result)
} else if (util.isStream(opts.body) && util.bodyLength(opts.body) !== 0) {
// If body is is stream we can't revalidate...
// TODO (fix): This could be less strict...
Expand All @@ -153,15 +151,15 @@ module.exports = (opts = {}) => {
...opts,
headers: {
...opts.headers,
'if-modified-since': new Date(value.cachedAt).toUTCString()
'if-modified-since': new Date(result.cachedAt).toUTCString()
}
},
new CacheRevalidationHandler(
(success) => {
if (success) {
respondWithCachedValue(stream, value)
} else {
stream.on('error', () => {}).destroy()
respondWithCachedValue(result)
} else if (util.isStream(result.body)) {
result.body.on('error', () => {}).destroy()
}
},
new CacheHandler(globalOpts, cacheKey, handler)
Expand All @@ -175,7 +173,7 @@ module.exports = (opts = {}) => {
if (!result) {
dispatch(opts, new CacheHandler(globalOpts, cacheKey, handler))
} else {
handleStream(result)
handleResult(result)
}
}, err => {
if (typeof handler.onError === 'function') {
Expand All @@ -185,7 +183,7 @@ module.exports = (opts = {}) => {
}
})
} else {
handleStream(result)
handleResult(result)
}

return true
Expand Down
2 changes: 1 addition & 1 deletion test/cache-interceptor/cache-stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function writeResponse (stream, body) {
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
* @returns {Promise<import('../../types/cache-interceptor.d.ts').default.GetResult | { body: Buffer[] }>}
*/
async function readResponse ({ response, body: src }) {
async function readResponse ({ body: src, ...response }) {
notEqual(response, undefined)
notEqual(src, undefined)

Expand Down
5 changes: 1 addition & 4 deletions types/cache-interceptor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ declare namespace CacheHandler {
path: string
}

export interface GetResult {
response: CachedResponse
body?: Readable | Iterable<Buffer> | Buffer | Iterable<string> | string
}
type GetResult = CachedResponse & { body: null | Readable | Iterable<Buffer> | Buffer | Iterable<string> | string }

/**
* Underlying storage provider for cached responses
Expand Down

0 comments on commit 2840fe7

Please sign in to comment.