* Compose middleware around a terminal response handler. With no middleware * the terminal runs directly.
( middlewares: Array<TODO>, terminal: TODO, ctx: PipelineContext, signal: AbortSignal, event: StartEvent, terminalNext?: TODO, )
| 302 | * the terminal runs directly. |
| 303 | */ |
| 304 | async function executeMiddleware( |
| 305 | middlewares: Array<TODO>, |
| 306 | terminal: TODO, |
| 307 | ctx: PipelineContext, |
| 308 | signal: AbortSignal, |
| 309 | event: StartEvent, |
| 310 | terminalNext?: TODO, |
| 311 | ): Promise<HandlerCallbackResult> { |
| 312 | let index = -1 |
| 313 | let responseOwnership: MiddlewareResponseOwnership | undefined |
| 314 | // Invocations form one chain: invocation `d` runs middleware `d` and calls |
| 315 | // next() at most once; the pipeline itself is depth -1. Once an invocation |
| 316 | // returns, a result of next() still pending below it is abandoned: it must |
| 317 | // not replace or cancel the response that invocation chose (for example the |
| 318 | // winner of a `Promise.race`). A caller at depth `d` is therefore abandoned |
| 319 | // when `returnedDepth <= d`, where this is the shallowest returned depth. |
| 320 | let returnedDepth = Infinity |
| 321 | const disposeAbandonedResult = createLateResponseDisposer(signal) |
| 322 | |
| 323 | const adoptResponse = ( |
| 324 | exposed: Response | undefined, |
| 325 | streamResponse?: StreamSsrResponse, |
| 326 | ) => { |
| 327 | const current = responseOwnership |
| 328 | // Read the body once: the getter can allocate a stream for a response |
| 329 | // that has not exposed one yet. |
| 330 | const body = exposed instanceof Response ? exposed.body : null |
| 331 | // `exposed` is a Response with this body whenever `body` is set. |
| 332 | const response = exposed as ResponseWithBody |
| 333 | if (streamResponse && !body && streamResponse !== current?.streamResponse) { |
| 334 | streamResponse.dispose('Response body dropped by Start reconciliation') |
| 335 | } |
| 336 | if (current && current.response === exposed) { |
| 337 | current.streamResponse ??= streamResponse |
| 338 | } else if (current && body && inheritsResponseOwnership(current, body)) { |
| 339 | // A wrapper around the same body stream carries the same encoded bytes, |
| 340 | // so it keeps the transport requirements that decode them. |
| 341 | if (body === current.response.body) { |
| 342 | transferResponseProtocol(current.response, response) |
| 343 | } |
| 344 | current.response = response |
| 345 | current.streamResponse ??= streamResponse |
| 346 | } else { |
| 347 | if (current) { |
| 348 | responseOwnership = undefined |
| 349 | disposeResponseOwnership(current, 'middleware response replaced') |
| 350 | } |
| 351 | responseOwnership = body |
| 352 | ? { response, sourceBody: body, streamResponse } |
| 353 | : undefined |
| 354 | } |
| 355 | ctx.response = exposed |
| 356 | } |
| 357 | |
| 358 | const disposeCurrentBody = (reason: string) => { |
| 359 | const current = responseOwnership |
| 360 | responseOwnership = undefined |
| 361 | if (current) { |
no test coverage detected