Skip to content

fix(backport): apply body delay before the response end#2969

Merged
mikicho merged 1 commit into
nock:mainfrom
BridgeAR:backport-delay-body-end-of-response
Apr 30, 2026
Merged

fix(backport): apply body delay before the response end#2969
mikicho merged 1 commit into
nock:mainfrom
BridgeAR:backport-delay-body-end-of-response

Conversation

@BridgeAR

Copy link
Copy Markdown
Contributor

This fixes a regression where delayBody(N) no longer makes a body look slow to the caller. Read timeouts on the response stream are expected to fire when N exceeds them, but in v14 the @mswjs/interceptors-based pipeline drains the response body before delivering the response event. Pausing the body source (the v13 mechanism) ended up delaying the whole response — response, data, and end events all moved to +delayBodyInMs, indistinguishable from a slow connection.

Two coordinated pieces fix it:

  1. lib/playback_interceptor.js: drop the pause/resume mechanism and gate only the end-of-response push (response.push(null)) on delayBodyInMs. Schedule that gate inside respond() so delay({ head, body }) continues to compound — head defers the response event, body then defers the end signal by an additional body ms. Reply callbacks like replyWithFile hand back an explicitly-paused fs.createReadStream, so call bodyAsStream.resume() to ensure the body flows.
  2. tests/got/test_delay.js: the two tests that asserted timing on the first data event are updated to assert on the end event, matching the new (and intended) semantics.

Refs: #2867
Fixes: #2863

This fixes a regression where `delayBody(N)` no longer makes a body look
slow to the caller. Read timeouts on the response stream are expected to
fire when N exceeds them, but in v14 the @mswjs/interceptors-based
pipeline drains the response body before delivering the response event.
Pausing the body source (the v13 mechanism) ended up delaying the
*whole* response — response, data, and end events all moved to
`+delayBodyInMs`, indistinguishable from a slow connection.

Two coordinated pieces fix it:

1. `lib/playback_interceptor.js`: drop the pause/resume mechanism and
   gate only the end-of-response push (`response.push(null)`) on
   `delayBodyInMs`. Schedule that gate inside `respond()` so
   `delay({ head, body })` continues to compound — head defers the
   response event, body then defers the end signal by an additional
   `body` ms. Reply callbacks like `replyWithFile` hand back an
   explicitly-paused `fs.createReadStream`, so call
   `bodyAsStream.resume()` to ensure the body flows.
2. `tests/got/test_delay.js`: the two tests that asserted timing on the
   first `data` event are updated to assert on the `end` event,
   matching the new (and intended) semantics.

Refs: nock#2867
Fixes: nock#2863
@mikicho mikicho enabled auto-merge (squash) April 30, 2026 18:31
@mikicho mikicho disabled auto-merge April 30, 2026 18:38
@mikicho mikicho merged commit 215cd2a into nock:main Apr 30, 2026
12 of 14 checks passed
@mikicho

mikicho commented Apr 30, 2026

Copy link
Copy Markdown
Member

Thanks!

@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 14.0.14 🎉

The release is available on:

Your semantic-release bot 📦🚀

mikicho added a commit that referenced this pull request May 7, 2026
…#2973)

Revert "fix(backport): apply body delay before the response end (#2969)"

This reverts commit 215cd2a.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

delayBody doesn't timeout anymore

2 participants