nock 14.0.13 → 14.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -273,22 +273,27 @@ function playbackInterceptor({
273
273
  function continueWithResponseBody(rawBody) {
274
274
  prepareResponseHeaders(rawBody)
275
275
  const bodyAsStream = convertBodyToStream(rawBody)
276
- bodyAsStream.pause()
276
+ // `replyWithFile` and similar reply callbacks hand back a paused stream,
277
+ // so force flowing mode here. `delayBody` gates only the end-of-response
278
+ // signal below; a slow body must present as a slow body — read timeouts
279
+ // on the response can fire — instead of as a slow connection.
280
+ bodyAsStream.resume()
277
281
 
278
282
  // IncomingMessage extends Readable so we can't simply pipe.
279
283
  bodyAsStream.on('data', function (chunk) {
280
284
  response.push(chunk)
281
285
  })
282
- bodyAsStream.on('end', function () {
286
+ bodyAsStream.on('error', function (err) {
287
+ response.emit('error', err)
288
+ })
289
+
290
+ function emitEnd() {
283
291
  // https://nodejs.org/dist/latest-v10.x/docs/api/http.html#http_message_complete
284
292
  response.complete = true
285
293
  response.push(null)
286
294
 
287
295
  interceptor.scope.emit('replied', req, interceptor)
288
- })
289
- bodyAsStream.on('error', function (err) {
290
- response.emit('error', err)
291
- })
296
+ }
292
297
 
293
298
  const { delayBodyInMs, delayConnectionInMs } = interceptor
294
299
 
@@ -306,7 +311,17 @@ function playbackInterceptor({
306
311
  logger('emitting response')
307
312
  req.emit('response', response)
308
313
 
309
- common.setTimeout(() => bodyAsStream.resume(), delayBodyInMs)
314
+ // Apply the body delay only after the response event has been emitted
315
+ // and the body source has finished, so `delay({ head, body })`
316
+ // compounds the two waits.
317
+ function scheduleEnd() {
318
+ common.setTimeout(emitEnd, delayBodyInMs)
319
+ }
320
+ if (bodyAsStream.readableEnded) {
321
+ scheduleEnd()
322
+ } else {
323
+ bodyAsStream.once('end', scheduleEnd)
324
+ }
310
325
  }
311
326
 
312
327
  socket.applyDelay(delayConnectionInMs)
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "testing",
8
8
  "isolation"
9
9
  ],
10
- "version": "14.0.13",
10
+ "version": "14.0.14",
11
11
  "author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
12
12
  "repository": {
13
13
  "type": "git",