* Execute a middleware chain
( middlewares: Array<TODO>, ctx: TODO, signal: AbortSignal, )
| 227 | * Execute a middleware chain |
| 228 | */ |
| 229 | async function executeMiddleware( |
| 230 | middlewares: Array<TODO>, |
| 231 | ctx: TODO, |
| 232 | signal: AbortSignal, |
| 233 | ): Promise<{ ctx: TODO; response: HandlerCallbackResult }> { |
| 234 | let index = -1 |
| 235 | let streamResponse: |
| 236 | | Extract<SsrResponse, { serverSsrCleanup: 'stream' }> |
| 237 | | undefined |
| 238 | let retiredStreamIdentities: WeakSet<object> | undefined |
| 239 | |
| 240 | const isResponseAlias = (candidate: unknown, response: Response) => |
| 241 | candidate === response || |
| 242 | (candidate instanceof Response && |
| 243 | response.body !== null && |
| 244 | candidate.body === response.body) |
| 245 | |
| 246 | const setResponse = (response: TODO) => { |
| 247 | if (isSsrResponse(response)) { |
| 248 | if (response.serverSsrCleanup === 'stream') { |
| 249 | streamResponse = response |
| 250 | } |
| 251 | ctx.response = response.response |
| 252 | return |
| 253 | } |
| 254 | |
| 255 | ctx.response = response |
| 256 | } |
| 257 | |
| 258 | const disposeStreamResponse = async (reason: unknown) => { |
| 259 | const response = streamResponse |
| 260 | if (!response) { |
| 261 | return |
| 262 | } |
| 263 | |
| 264 | streamResponse = undefined |
| 265 | retiredStreamIdentities ??= new WeakSet() |
| 266 | retiredStreamIdentities.add(response.response) |
| 267 | if (response.response.body) { |
| 268 | retiredStreamIdentities.add(response.response.body) |
| 269 | } |
| 270 | const currentResponse = ctx.response |
| 271 | if (isResponseAlias(currentResponse, response.response)) { |
| 272 | ctx.response = undefined |
| 273 | } |
| 274 | await response.dispose(reason) |
| 275 | } |
| 276 | |
| 277 | const disposeAbandonedResult = (result: TODO) => { |
| 278 | const exposed = handleCtxResult(result)?.response |
| 279 | const response = isSsrResponse(exposed) ? exposed.response : exposed |
| 280 | if (streamResponse && isResponseAlias(response, streamResponse.response)) { |
| 281 | void disposeStreamResponse(signal.reason).catch(console.error) |
| 282 | return |
| 283 | } |
| 284 | if ( |
| 285 | response instanceof Response && |
| 286 | retiredStreamIdentities && |
no test coverage detected