From a6e60f182f612c0770b599baf4a81c2b5639e2e7 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Wed, 13 Nov 2024 11:40:44 +0100 Subject: [PATCH] fixup --- lib/cache/memory-cache-store.js | 5 +---- lib/interceptor/cache.js | 34 ++++++++++++++++----------------- types/cache-interceptor.d.ts | 5 +---- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/lib/cache/memory-cache-store.js b/lib/cache/memory-cache-store.js index d78d8bbf369..6a00568ca22 100644 --- a/lib/cache/memory-cache-store.js +++ b/lib/cache/memory-cache-store.js @@ -81,10 +81,7 @@ class MemoryCacheStore { return undefined } - return { - response: value.opts, - body: value.body - } + return { ...value.opts, body: value.body } } /** diff --git a/lib/interceptor/cache.js b/lib/interceptor/cache.js index 07b6d982f7f..cb0bc6fa650 100644 --- a/lib/interceptor/cache.js +++ b/lib/interceptor/cache.js @@ -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') @@ -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... @@ -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) @@ -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') { @@ -185,7 +183,7 @@ module.exports = (opts = {}) => { } }) } else { - handleStream(result) + handleResult(result) } return true diff --git a/types/cache-interceptor.d.ts b/types/cache-interceptor.d.ts index b21e1b7c3ad..1c30b42d359 100644 --- a/types/cache-interceptor.d.ts +++ b/types/cache-interceptor.d.ts @@ -31,10 +31,7 @@ declare namespace CacheHandler { path: string } - export interface GetResult { - response: CachedResponse - body?: Readable | Iterable | Buffer | Iterable | string - } + type GetResult = CachedResponse & { body: null | Readable | Iterable | Buffer | Iterable | string } /** * Underlying storage provider for cached responses