(mockDispatches, _data = data)
| 360 | } |
| 361 | |
| 362 | function handleReply (mockDispatches, _data = data) { |
| 363 | // Don't send response if the request was aborted |
| 364 | if (aborted) { |
| 365 | return |
| 366 | } |
| 367 | |
| 368 | // fetch's HeadersList is a 1D string array |
| 369 | const optsHeaders = Array.isArray(opts.headers) |
| 370 | ? buildHeadersFromArray(opts.headers) |
| 371 | : opts.headers |
| 372 | const body = typeof _data === 'function' |
| 373 | ? _data({ ...opts, headers: optsHeaders }) |
| 374 | : _data |
| 375 | |
| 376 | // util.types.isPromise is likely needed for jest. |
| 377 | if (isPromise(body)) { |
| 378 | // If handleReply is asynchronous, throwing an error |
| 379 | // in the callback will reject the promise, rather than |
| 380 | // synchronously throw the error, which breaks some tests. |
| 381 | // Rather, we wait for the callback to resolve if it is a |
| 382 | // promise, and then re-run handleReply with the new body. |
| 383 | return body.then((newData) => handleReply(mockDispatches, newData)) |
| 384 | } |
| 385 | |
| 386 | // Check again if aborted after async body resolution |
| 387 | if (aborted) { |
| 388 | return |
| 389 | } |
| 390 | |
| 391 | const responseData = getResponseData(body) |
| 392 | const responseHeaders = generateKeyValues(headers) |
| 393 | const responseTrailers = generateKeyValues(trailers) |
| 394 | |
| 395 | // Update the controller with response data |
| 396 | controller.rawHeaders = responseHeaders |
| 397 | controller.rawTrailers = responseTrailers |
| 398 | |
| 399 | handler.onResponseStart?.(controller, statusCode, parseHeaders(responseHeaders), getStatusText(statusCode)) |
| 400 | handler.onResponseData?.(controller, Buffer.from(responseData)) |
| 401 | handler.onResponseEnd?.(controller, parseHeaders(responseTrailers)) |
| 402 | deleteMockDispatch(mockDispatches, key) |
| 403 | } |
| 404 | |
| 405 | return true |
| 406 | } |
no test coverage detected