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

Method onResponseStart

lib/handler/cache-handler.js:194–445  ·  view source on GitHub ↗

* @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller * @param {number} statusCode * @param {import('../../types/header.d.ts').IncomingHttpHeaders} resHeaders * @param {string} statusMessage

(
    controller,
    statusCode,
    resHeaders,
    statusMessage
  )

Source from the content-addressed store, hash-verified

192 * @param {string} statusMessage
193 */
194 onResponseStart (
195 controller,
196 statusCode,
197 resHeaders,
198 statusMessage
199 ) {
200 const downstreamOnHeaders = () =>
201 this.#handler.onResponseStart?.(
202 controller,
203 statusCode,
204 resHeaders,
205 statusMessage
206 )
207 const handler = this
208
209 if (
210 !arrayIncludes(util.safeHTTPMethods, this.#cacheKey.method) &&
211 statusCode >= 200 &&
212 statusCode <= 399
213 ) {
214 // Successful response to an unsafe method, delete it from cache
215 // https://www.rfc-editor.org/rfc/rfc9111.html#name-invalidating-stored-response
216 invalidateUnsafeRequest(this.#store, this.#cacheKey, resHeaders)
217 return downstreamOnHeaders()
218 }
219
220 const cacheControlHeader = resHeaders['cache-control']
221 const cacheControlDirectives = cacheControlHeader ? parseCacheControlHeader(cacheControlHeader) : {}
222
223 if (revalidationResponseDisallowsCachedReuse(this.#cacheType, resHeaders, cacheControlDirectives)) {
224 deleteCachedValue(this.#store, this.#cacheKey)
225 return downstreamOnHeaders()
226 }
227
228 const heuristicallyCacheable = resHeaders['last-modified'] && arrayIncludes(HEURISTICALLY_CACHEABLE_STATUS_CODES, statusCode)
229 if (
230 !cacheControlHeader &&
231 !resHeaders['expires'] &&
232 !heuristicallyCacheable &&
233 !this.#cacheByDefault
234 ) {
235 if (statusCode === 304 && resHeaders.vary && isInvalidOrWildcardVaryHeader(resHeaders.vary)) {
236 deleteCachedValue(this.#store, this.#cacheKey)
237 }
238
239 // Don't have anything to tell us this response is cachable and we're not
240 // caching by default
241 return downstreamOnHeaders()
242 }
243
244 if (!canCacheResponse(this.#cacheType, this.#cacheKey.method, statusCode, resHeaders, cacheControlDirectives, this.#cacheKey.headers)) {
245 if (statusCode === 304 && (cacheControlHeader || revalidationResponseDisallowsCachedReuse(this.#cacheType, resHeaders, cacheControlDirectives))) {
246 deleteCachedValue(this.#store, this.#cacheKey)
247 }
248
249 return downstreamOnHeaders()
250 }
251

Callers

nothing calls this directly

Calls 15

invalidateUnsafeRequestFunction · 0.85
parseCacheControlHeaderFunction · 0.85
canCacheResponseFunction · 0.85
getAgeFunction · 0.85
getDateFunction · 0.85
isEtagUsableFunction · 0.85
determineStaleAtFunction · 0.85
parseVaryHeaderFunction · 0.85
determineDeleteAtFunction · 0.85
stripNecessaryHeadersFunction · 0.85

Tested by

no test coverage detected