MCPcopy
hub / github.com/nodejs/undici / onResponseStart

Method onResponseStart

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

92 * @param {string} statusMessage
93 */
94 onResponseStart (
95 controller,
96 statusCode,
97 resHeaders,
98 statusMessage
99 ) {
100 const downstreamOnHeaders = () =>
101 this.#handler.onResponseStart?.(
102 controller,
103 statusCode,
104 resHeaders,
105 statusMessage
106 )
107 const handler = this
108
109 if (
110 !util.safeHTTPMethods.includes(this.#cacheKey.method) &&
111 statusCode >= 200 &&
112 statusCode <= 399
113 ) {
114 // Successful response to an unsafe method, delete it from cache
115 // https://www.rfc-editor.org/rfc/rfc9111.html#name-invalidating-stored-response
116 try {
117 this.#store.delete(this.#cacheKey)?.catch?.(noop)
118 } catch {
119 // Fail silently
120 }
121 return downstreamOnHeaders()
122 }
123
124 const cacheControlHeader = resHeaders['cache-control']
125 const heuristicallyCacheable = resHeaders['last-modified'] && HEURISTICALLY_CACHEABLE_STATUS_CODES.includes(statusCode)
126 if (
127 !cacheControlHeader &&
128 !resHeaders['expires'] &&
129 !heuristicallyCacheable &&
130 !this.#cacheByDefault
131 ) {
132 // Don't have anything to tell us this response is cachable and we're not
133 // caching by default
134 return downstreamOnHeaders()
135 }
136
137 const cacheControlDirectives = cacheControlHeader ? parseCacheControlHeader(cacheControlHeader) : {}
138 if (!canCacheResponse(this.#cacheType, statusCode, resHeaders, cacheControlDirectives, this.#cacheKey.headers)) {
139 return downstreamOnHeaders()
140 }
141
142 const now = Date.now()
143 const resAge = resHeaders.age ? getAge(resHeaders.age) : undefined
144 if (resAge && resAge >= MAX_RESPONSE_AGE) {
145 // Response considered stale
146 return downstreamOnHeaders()
147 }
148
149 const resDate = typeof resHeaders.date === 'string'
150 ? parseHttpDate(resHeaders.date)
151 : undefined

Callers

nothing calls this directly

Calls 14

parseCacheControlHeaderFunction · 0.85
canCacheResponseFunction · 0.85
getAgeFunction · 0.85
parseHttpDateFunction · 0.85
determineStaleAtFunction · 0.85
parseVaryHeaderFunction · 0.85
determineDeleteAtFunction · 0.85
stripNecessaryHeadersFunction · 0.85
isEtagUsableFunction · 0.85
deleteMethod · 0.65
getMethod · 0.65
createWriteStreamMethod · 0.65

Tested by

no test coverage detected