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

Method onResponseStart

lib/handler/cache-handler.js:186–431  ·  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

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

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