* @param {import('../../types/dispatcher.d.ts').default.DispatchHandler} handler * @param {import('../../types/dispatcher.d.ts').default.RequestOptions} opts * @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result * @param {number} age * @param {any} context * @param {b
(handler, opts, result, age, context, isStale)
| 175 | * @param {boolean} isStale |
| 176 | */ |
| 177 | function sendCachedValue (handler, opts, result, age, context, isStale) { |
| 178 | // TODO (perf): Readable.from path can be optimized... |
| 179 | const stream = util.isStream(result.body) |
| 180 | ? result.body |
| 181 | : Readable.from(result.body ?? []) |
| 182 | |
| 183 | assert(!stream.destroyed, 'stream should not be destroyed') |
| 184 | assert(!stream.readableDidRead, 'stream should not be readableDidRead') |
| 185 | |
| 186 | const controller = { |
| 187 | rawHeaders: [], |
| 188 | rawTrailers: [], |
| 189 | resume () { |
| 190 | stream.resume() |
| 191 | }, |
| 192 | pause () { |
| 193 | stream.pause() |
| 194 | }, |
| 195 | get paused () { |
| 196 | return stream.isPaused() |
| 197 | }, |
| 198 | get aborted () { |
| 199 | return stream.destroyed |
| 200 | }, |
| 201 | get reason () { |
| 202 | return stream.errored |
| 203 | }, |
| 204 | abort (reason) { |
| 205 | stream.destroy(reason ?? new AbortError()) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | stream |
| 210 | .on('error', function (err) { |
| 211 | if (!this.readableEnded) { |
| 212 | if (typeof handler.onResponseError === 'function') { |
| 213 | handler.onResponseError(controller, err) |
| 214 | } else { |
| 215 | throw err |
| 216 | } |
| 217 | } |
| 218 | }) |
| 219 | .on('close', function () { |
| 220 | if (!this.errored) { |
| 221 | handler.onResponseEnd?.(controller, {}) |
| 222 | } |
| 223 | }) |
| 224 | |
| 225 | handler.onRequestStart?.(controller, context) |
| 226 | |
| 227 | if (stream.destroyed) { |
| 228 | return |
| 229 | } |
| 230 | |
| 231 | // Add the age header |
| 232 | // https://www.rfc-editor.org/rfc/rfc9111.html#name-age |
| 233 | const headers = { ...result.headers, age: String(age) } |
| 234 |
no test coverage detected
searching dependent graphs…