MCPcopy Create free account
hub / github.com/nodejs/undici / handle304

Method handle304

lib/handler/cache-handler.js:326–402  ·  view source on GitHub ↗
(cachedValue)

Source from the content-addressed store, hash-verified

324 // https://www.rfc-editor.org/rfc/rfc9111.html#name-handling-304-not-modified
325 if (statusCode === 304) {
326 const handle304 = (cachedValue) => {
327 if (!cachedValue) {
328 // Do not create a new cache entry, as a 304 won't have a body - so cannot be cached.
329 return downstreamOnHeaders()
330 }
331
332 // Re-use the cached value: statuscode, statusmessage, headers and body
333 value.statusCode = cachedValue.statusCode
334 value.statusMessage = cachedValue.statusMessage
335 value.etag = cachedValue.etag
336 value.vary = varyDirectives ?? cachedValue.vary
337 value.headers = { ...cachedValue.headers, ...strippedHeaders }
338
339 downstreamOnHeaders()
340
341 this.#writeStream = this.#store.createWriteStream(this.#cacheKey, value)
342
343 if (!this.#writeStream || !cachedValue?.body) {
344 return
345 }
346
347 if (typeof cachedValue.body.values === 'function') {
348 const bodyIterator = cachedValue.body.values()
349
350 const streamCachedBody = () => {
351 for (const chunk of bodyIterator) {
352 const full = this.#writeStream.write(chunk) === false
353 this.#handler.onResponseData?.(controller, chunk)
354 // when stream is full stop writing until we get a 'drain' event
355 if (full) {
356 break
357 }
358 }
359 }
360
361 this.#writeStream
362 .on('error', function () {
363 handler.#writeStream = undefined
364 handler.#store.delete(handler.#cacheKey)
365 })
366 .on('drain', () => {
367 streamCachedBody()
368 })
369 .on('close', function () {
370 if (handler.#writeStream === this) {
371 handler.#writeStream = undefined
372 }
373 })
374
375 streamCachedBody()
376 } else if (typeof cachedValue.body.on === 'function') {
377 // Readable stream body (e.g. from async/remote cache stores)
378 cachedValue.body
379 .on('data', (chunk) => {
380 this.#writeStream.write(chunk)
381 this.#handler.onResponseData?.(controller, chunk)
382 })
383 .on('end', () => {

Callers

nothing calls this directly

Calls 7

valuesMethod · 0.80
createWriteStreamMethod · 0.65
deleteMethod · 0.65
onResponseDataMethod · 0.65
onMethod · 0.45
writeMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected