Skip to content

fix: wait for the ephemeral server before writing request bodies - #912

Open
codesbysaravana wants to merge 2 commits into
forwardemail:masterfrom
codesbysaravana:fix/streaming-ephemeral-port-911
Open

codesbysaravana wants to merge 2 commits into
forwardemail:masterfrom
codesbysaravana:fix/streaming-ephemeral-port-911

Conversation

@codesbysaravana

Copy link
Copy Markdown

Fixes #911.

After #907, the loopback bind resolves asynchronously. serverAddress() temporarily returns a URL without the ephemeral port, which end() corrects once the server is listening. Raw write() calls Superagent's request() earlier and creates a cached request against port 80.

This delays raw writes until the server has an address and returns false while waiting so piped sources pause. Once listening, it resolves the URL, forwards the queued chunks in order, and resumes the source. Later drain events are forwarded because Superagent only forwards the first one. It also resolves the placeholder URL when the first write arrives after listening.

Abort, startup-error and close handlers discard queued writes and remove the startup listeners. Existing-server writes still use Superagent directly. The loopback-only binding from #907 remains unchanged; this does not modify the shared-server lifetime work in #910.

Tests

Added 13 regression tests covering:

  • immediate string/Buffer writes, piped input and first write after listening
  • query strings and chunk encoding
  • already-listening servers and agents
  • HTTPS and HTTP/2
  • paused startup with a multi-drain 512 KiB upload
  • startup failure, abort, buffered sends and close-before-callback

npm test passes with 150 tests on Linux under Node 18.20.8, 20.20.2, 22.23.3, 24.21.0 and 26.10.0. This includes the repository lint and coverage run. The streaming tests passed 20 consecutive runs on Node 22. git diff --check passes.

The supplied .write() reproduction failed with ECONNREFUSED on unmodified 7.3.1. No dependency or lockfile changes are included.

Not tested on macOS or Windows. The fix covers request-body streaming; response piping behavior is outside this patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7.3.1: streaming requests (.write) against an app factory hit port 80 (ECONNREFUSED)

1 participant