deps: update undici to 7.22.0 · nodejs/node@51ded81
@@ -193,57 +193,92 @@ class CacheHandler {
193193// Not modified, re-use the cached value
194194// https://www.rfc-editor.org/rfc/rfc9111.html#name-handling-304-not-modified
195195if (statusCode === 304) {
196-/**
197- * @type {import('../../types/cache-interceptor.d.ts').default.CacheValue}
198- */
199-const cachedValue = this.#store.get(this.#cacheKey)
200-if (!cachedValue) {
201-// Do not create a new cache entry, as a 304 won't have a body - so cannot be cached.
202-return downstreamOnHeaders()
203-}
204-205-// Re-use the cached value: statuscode, statusmessage, headers and body
206-value.statusCode = cachedValue.statusCode
207-value.statusMessage = cachedValue.statusMessage
208-value.etag = cachedValue.etag
209-value.headers = { ...cachedValue.headers, ...strippedHeaders }
196+const handle304 = (cachedValue) => {
197+if (!cachedValue) {
198+// Do not create a new cache entry, as a 304 won't have a body - so cannot be cached.
199+return downstreamOnHeaders()
200+}
210201211-downstreamOnHeaders()
202+// Re-use the cached value: statuscode, statusmessage, headers and body
203+value.statusCode = cachedValue.statusCode
204+value.statusMessage = cachedValue.statusMessage
205+value.etag = cachedValue.etag
206+value.headers = { ...cachedValue.headers, ...strippedHeaders }
212207213-this.#writeStream = this.#store.createWriteStream(this.#cacheKey, value)
208+ downstreamOnHeaders()
214209215-if (!this.#writeStream || !cachedValue?.body) {
216-return
217-}
210+this.#writeStream = this.#store.createWriteStream(this.#cacheKey, value)
218211219-const bodyIterator = cachedValue.body.values()
212+if (!this.#writeStream || !cachedValue?.body) {
213+return
214+}
220215221-const streamCachedBody = () => {
222-for (const chunk of bodyIterator) {
223-const full = this.#writeStream.write(chunk) === false
224-this.#handler.onResponseData?.(controller, chunk)
225-// when stream is full stop writing until we get a 'drain' event
226-if (full) {
227-break
216+if (typeof cachedValue.body.values === 'function') {
217+const bodyIterator = cachedValue.body.values()
218+219+const streamCachedBody = () => {
220+for (const chunk of bodyIterator) {
221+const full = this.#writeStream.write(chunk) === false
222+this.#handler.onResponseData?.(controller, chunk)
223+// when stream is full stop writing until we get a 'drain' event
224+if (full) {
225+break
226+}
227+}
228228}
229-}
230-}
231229232-this.#writeStream
233-.on('error', function () {
234-handler.#writeStream = undefined
235-handler.#store.delete(handler.#cacheKey)
236-})
237-.on('drain', () => {
230+this.#writeStream
231+.on('error', function () {
232+handler.#writeStream = undefined
233+handler.#store.delete(handler.#cacheKey)
234+})
235+.on('drain', () => {
236+streamCachedBody()
237+})
238+.on('close', function () {
239+if (handler.#writeStream === this) {
240+handler.#writeStream = undefined
241+}
242+})
243+238244streamCachedBody()
239-})
240-.on('close', function () {
241-if (handler.#writeStream === this) {
242-handler.#writeStream = undefined
243-}
244-})
245+} else if (typeof cachedValue.body.on === 'function') {
246+// Readable stream body (e.g. from async/remote cache stores)
247+cachedValue.body
248+.on('data', (chunk) => {
249+this.#writeStream.write(chunk)
250+this.#handler.onResponseData?.(controller, chunk)
251+})
252+.on('end', () => {
253+this.#writeStream.end()
254+})
255+.on('error', () => {
256+this.#writeStream = undefined
257+this.#store.delete(this.#cacheKey)
258+})
259+260+this.#writeStream
261+.on('error', function () {
262+handler.#writeStream = undefined
263+handler.#store.delete(handler.#cacheKey)
264+})
265+.on('close', function () {
266+if (handler.#writeStream === this) {
267+handler.#writeStream = undefined
268+}
269+})
270+}
271+}
245272246-streamCachedBody()
273+/**
274+ * @type {import('../../types/cache-interceptor.d.ts').default.CacheValue}
275+ */
276+const result = this.#store.get(this.#cacheKey)
277+if (result && typeof result.then === 'function') {
278+result.then(handle304)
279+} else {
280+handle304(result)
281+}
247282} else {
248283if (typeof resHeaders.etag === 'string' && isEtagUsable(resHeaders.etag)) {
249284value.etag = resHeaders.etag