* Execute a middleware chain
( middlewares: Array<TODO>, ctx: TODO, )
| 212 | * Execute a middleware chain |
| 213 | */ |
| 214 | async function executeMiddleware( |
| 215 | middlewares: Array<TODO>, |
| 216 | ctx: TODO, |
| 217 | ): Promise<{ ctx: TODO; response: HandlerCallbackResult }> { |
| 218 | let index = -1 |
| 219 | let streamResponse: |
| 220 | | Extract<SsrResponse, { serverSsrCleanup: 'stream' }> |
| 221 | | undefined |
| 222 | |
| 223 | const setResponse = (response: TODO) => { |
| 224 | if (isSsrResponse(response)) { |
| 225 | if (response.serverSsrCleanup === 'stream') { |
| 226 | streamResponse = response |
| 227 | } |
| 228 | ctx.response = response.response |
| 229 | return |
| 230 | } |
| 231 | |
| 232 | ctx.response = response |
| 233 | } |
| 234 | |
| 235 | const disposeStreamResponse = async (reason: string) => { |
| 236 | const response = streamResponse |
| 237 | if (!response) { |
| 238 | return |
| 239 | } |
| 240 | |
| 241 | streamResponse = undefined |
| 242 | const currentResponse = ctx.response |
| 243 | if ( |
| 244 | currentResponse === response.response || |
| 245 | (currentResponse instanceof Response && |
| 246 | response.response.body !== null && |
| 247 | currentResponse.body === response.response.body) |
| 248 | ) { |
| 249 | ctx.response = undefined |
| 250 | } |
| 251 | await response.dispose(reason) |
| 252 | } |
| 253 | |
| 254 | const getFinalResponse = async (): Promise<HandlerCallbackResult> => { |
| 255 | const response = ctx.response |
| 256 | if (!response) { |
| 257 | throwRouteHandlerError() |
| 258 | } |
| 259 | |
| 260 | if (!streamResponse) { |
| 261 | return response |
| 262 | } |
| 263 | |
| 264 | if (response === streamResponse.response) { |
| 265 | return streamResponse |
| 266 | } |
| 267 | |
| 268 | if ( |
| 269 | streamResponse.response.body !== null && |
| 270 | response.body === streamResponse.response.body |
| 271 | ) { |
no test coverage detected