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

Method handle304

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

Source from the content-addressed store, hash-verified

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