Skip to content

Commit 5b22d96

Browse files
committed
fixup
1 parent 9b65862 commit 5b22d96

File tree

4 files changed

+20
-28
lines changed

4 files changed

+20
-28
lines changed

lib/cache/memory-cache-store.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ class MemoryCacheStore {
8181
return undefined
8282
}
8383

84-
return {
85-
response: value.opts,
86-
body: value.body
87-
}
84+
return { ...value.opts, body: value.body }
8885
}
8986

9087
/**

lib/interceptor/cache.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ module.exports = (opts = {}) => {
6262
}
6363

6464
/**
65-
* @param {import('node:stream').Readable} stream
66-
* @param {import('../../types/cache-interceptor.d.ts').default.CachedResponse} value
65+
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
6766
*/
68-
const respondWithCachedValue = (stream, { cachedAt, rawHeaders, statusCode, statusMessage }) => {
67+
const respondWithCachedValue = ({ cachedAt, rawHeaders, statusCode, statusMessage, body }) => {
68+
const stream = util.isStream(body)
69+
? body
70+
: Readable.from(body ?? [])
71+
6972
assert(!stream.destroyed, 'stream should not be destroyed')
7073
assert(!stream.readableDidRead, 'stream should not be readableDidRead')
7174

@@ -122,26 +125,21 @@ module.exports = (opts = {}) => {
122125
/**
123126
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
124127
*/
125-
const handleStream = (result) => {
126-
const { response: value, body } = result
127-
128+
const handleResult = (result) => {
128129
// TODO (perf): Readable.from path can be optimized...
129-
const stream = util.isStream(body)
130-
? body
131-
: Readable.from(body ?? [])
132130

133-
if (!stream && opts.method !== 'HEAD') {
131+
if (!result.body && opts.method !== 'HEAD') {
134132
throw new Error('stream is undefined but method isn\'t HEAD')
135133
}
136134

137135
// Check if the response is stale
138136
const now = Date.now()
139-
if (now < value.staleAt) {
137+
if (now < result.staleAt) {
140138
// Dump request body.
141139
if (util.isStream(opts.body)) {
142140
opts.body.on('error', () => {}).destroy()
143141
}
144-
respondWithCachedValue(stream, value)
142+
respondWithCachedValue(result)
145143
} else if (util.isStream(opts.body) && util.bodyLength(opts.body) !== 0) {
146144
// If body is is stream we can't revalidate...
147145
// TODO (fix): This could be less strict...
@@ -153,15 +151,15 @@ module.exports = (opts = {}) => {
153151
...opts,
154152
headers: {
155153
...opts.headers,
156-
'if-modified-since': new Date(value.cachedAt).toUTCString()
154+
'if-modified-since': new Date(result.cachedAt).toUTCString()
157155
}
158156
},
159157
new CacheRevalidationHandler(
160158
(success) => {
161159
if (success) {
162-
respondWithCachedValue(stream, value)
163-
} else {
164-
stream.on('error', () => {}).destroy()
160+
respondWithCachedValue(result)
161+
} else if (util.isStream(result.body)) {
162+
result.body.on('error', () => {}).destroy()
165163
}
166164
},
167165
new CacheHandler(globalOpts, cacheKey, handler)
@@ -175,7 +173,7 @@ module.exports = (opts = {}) => {
175173
if (!result) {
176174
dispatch(opts, new CacheHandler(globalOpts, cacheKey, handler))
177175
} else {
178-
handleStream(result)
176+
handleResult(result)
179177
}
180178
}, err => {
181179
if (typeof handler.onError === 'function') {
@@ -185,7 +183,7 @@ module.exports = (opts = {}) => {
185183
}
186184
})
187185
} else {
188-
handleStream(result)
186+
handleResult(result)
189187
}
190188

191189
return true

test/cache-interceptor/cache-stores.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,11 @@ function writeResponse (stream, body) {
269269
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
270270
* @returns {Promise<import('../../types/cache-interceptor.d.ts').default.GetResult | { body: Buffer[] }>}
271271
*/
272-
async function readResponse ({ response, body: src }) {
272+
async function readResponse ({ body: src, ...response }) {
273273
notEqual(response, undefined)
274274
notEqual(src, undefined)
275275

276-
const stream = Readable.from(src)
276+
const stream = Readable.from(src ?? [])
277277

278278
/**
279279
* @type {Buffer[]}

types/cache-interceptor.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ declare namespace CacheHandler {
3131
path: string
3232
}
3333

34-
export interface GetResult {
35-
response: CachedResponse
36-
body?: Readable | Iterable<Buffer> | Buffer | Iterable<string> | string
37-
}
34+
type GetResult = CachedResponse & { body: null | Readable | Iterable<Buffer> | Buffer | Iterable<string> | string }
3835

3936
/**
4037
* Underlying storage provider for cached responses

0 commit comments

Comments
 (0)