(cachedValue)
| 195 | // https://www.rfc-editor.org/rfc/rfc9111.html#name-handling-304-not-modified |
| 196 | if (statusCode === 304) { |
| 197 | const handle304 = (cachedValue) => { |
| 198 | if (!cachedValue) { |
| 199 | // Do not create a new cache entry, as a 304 won't have a body - so cannot be cached. |
| 200 | return downstreamOnHeaders() |
| 201 | } |
| 202 | |
| 203 | // Re-use the cached value: statuscode, statusmessage, headers and body |
| 204 | value.statusCode = cachedValue.statusCode |
| 205 | value.statusMessage = cachedValue.statusMessage |
| 206 | value.etag = cachedValue.etag |
| 207 | value.headers = { ...cachedValue.headers, ...strippedHeaders } |
| 208 | |
| 209 | downstreamOnHeaders() |
| 210 | |
| 211 | this.#writeStream = this.#store.createWriteStream(this.#cacheKey, value) |
| 212 | |
| 213 | if (!this.#writeStream || !cachedValue?.body) { |
| 214 | return |
| 215 | } |
| 216 | |
| 217 | if (typeof cachedValue.body.values === 'function') { |
| 218 | const bodyIterator = cachedValue.body.values() |
| 219 | |
| 220 | const streamCachedBody = () => { |
| 221 | for (const chunk of bodyIterator) { |
| 222 | const full = this.#writeStream.write(chunk) === false |
| 223 | this.#handler.onResponseData?.(controller, chunk) |
| 224 | // when stream is full stop writing until we get a 'drain' event |
| 225 | if (full) { |
| 226 | break |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | this.#writeStream |
| 232 | .on('error', function () { |
| 233 | handler.#writeStream = undefined |
| 234 | handler.#store.delete(handler.#cacheKey) |
| 235 | }) |
| 236 | .on('drain', () => { |
| 237 | streamCachedBody() |
| 238 | }) |
| 239 | .on('close', function () { |
| 240 | if (handler.#writeStream === this) { |
| 241 | handler.#writeStream = undefined |
| 242 | } |
| 243 | }) |
| 244 | |
| 245 | streamCachedBody() |
| 246 | } else if (typeof cachedValue.body.on === 'function') { |
| 247 | // Readable stream body (e.g. from async/remote cache stores) |
| 248 | cachedValue.body |
| 249 | .on('data', (chunk) => { |
| 250 | this.#writeStream.write(chunk) |
| 251 | this.#handler.onResponseData?.(controller, chunk) |
| 252 | }) |
| 253 | .on('end', () => { |
| 254 | this.#writeStream.end() |
nothing calls this directly
no test coverage detected