(
rawBody,
fullResponseStatus,
fullResponseRawHeaders = [],
)
| 252 | } |
| 253 | |
| 254 | async function continueWithResponseBody( |
| 255 | rawBody, |
| 256 | fullResponseStatus, |
| 257 | fullResponseRawHeaders = [], |
| 258 | ) { |
| 259 | const headers = await prepareResponseHeaders( |
| 260 | rawBody, |
| 261 | fullResponseRawHeaders, |
| 262 | ) |
| 263 | const bodyAsStream = convertBodyToStream(rawBody) |
| 264 | bodyAsStream.resume() |
| 265 | |
| 266 | // TODO: there is probably a better way to support delay. |
| 267 | // Wrap the stream in a duplex stream to support the delay. |
| 268 | const readable = new stream.Readable({ |
| 269 | read() {}, |
| 270 | }) |
| 271 | bodyAsStream.on('data', function (chunk) { |
| 272 | readable.push(chunk) |
| 273 | }) |
| 274 | bodyAsStream.on('end', function () { |
| 275 | common.setTimeout(() => { |
| 276 | readable.push(null) |
| 277 | interceptor.scope.emit('replied', decompressedRequest, interceptor) |
| 278 | }, interceptor.delayBodyInMs) |
| 279 | }) |
| 280 | bodyAsStream.on('error', function (err) { |
| 281 | readable.emit('error', err) |
| 282 | }) |
| 283 | |
| 284 | const status = interceptor.statusCode || fullResponseStatus |
| 285 | const hasBody = FetchResponse.isResponseWithBody(status) |
| 286 | return new Response(hasBody ? readable : null, { |
| 287 | status, |
| 288 | statusText: STATUS_CODES[status], |
| 289 | headers, |
| 290 | }) |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | module.exports = { playbackInterceptor } |
no test coverage detected
searching dependent graphs…