Repository navigation
Description
Environment
- httpxy 0.5.5 (via http-proxy-middleware 4.2.0)
- msw 2.15.0 (
msw/node) - Node 22.x, vitest
Problem
Every proxied request hangs indefinitely when the outbound connection is intercepted by msw's socket-level mock (@mswjs/interceptors). This makes httpxy-based proxies untestable under the common vitest + msw setup, and is the reason we had to stay on http-proxy-middleware v3 (node-http-proxy engine) instead of adopting v4 (httpxy engine) — same app code, same tests: v3 passes, v4 hangs on all 13 proxied-request tests.
Root cause
In src/middleware/web-incoming.ts, piping the incoming request into the outbound request is deferred until the outbound socket emits connect:
proxyReq.on("socket", (socket) => {
if (socket.pending) {
socket.on("connect", () => (options.buffer || req).pipe(proxyReq));
} else {
(options.buffer || req).pipe(proxyReq);
}
});msw's mock socket stays pending and never emits connect, so the pipe never starts. Since the pipe is also what propagates end to proxyReq, the outbound request is never finalized — even body-less GETs hang, and the client request never completes.
node-http-proxy pipes immediately without waiting for the socket, which is why it works under msw: Node's http.ClientRequest already buffers writes issued before the socket connects, so the deferral isn't needed for correctness.
Suggested fix
Pipe immediately (as node-http-proxy does), relying on ClientRequest's internal buffering:
(options.buffer || req).pipe(proxyReq);If the connect deferral guards against a specific issue, an alternative would be to also start the pipe on ready/first writable state, or fall back when connect never fires.
Repro sketch
import { createServer } from "node:http";
import { createProxyServer } from "httpxy";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
const msw = setupServer(
http.get("http://upstream.test/ping", () => HttpResponse.json({ ok: true }))
);
msw.listen();
const proxy = createProxyServer({ target: "http://upstream.test" });
createServer((req, res) => proxy.web(req, res)).listen(3000);
// curl http://localhost:3000/ping -> hangs forever
// (same code with node-http-proxy responds 200)Happy to open a PR if the maintainers agree on the direction.
Activity
cuongluu8 commentedon Sep 4, 2026
Follow-up after digging into the history: the suggested fix in my original report ("pipe immediately") is too naive, so amending it here.
The
connect-gated pipe was introduced deliberately in 7f74a06 (PR #111, "defer pipe until socket connects") to fix a data-loss race with large/chunked POST bodies, and it landed alongside the HTTP/2 listener support — where writing before the session is ready is genuinely unsafe. It's guarded by the streamed 8KB POST regression test. So simply removing the gate would trade this bug for that one.A couple of observations toward a fix that holds both:
ClientRequestalready buffers writes issued before the socket connects — that's the contract node-http-proxy relied on for a decade. If the data-loss race was specific to the HTTP/2 path (or to writing to the raw socket rather than throughClientRequest), the gate could be conditional on that path instead of applying to every request.connectnever fires is a silent hang until timeout rather than a fast error. That's what every in-process interception library hits: msw's interceptor cannot emitconnectearly by design — it decides mock-vs-passthrough only after consuming the request body, which httpxy won't send untilconnect. Mutual deadlock, so no interceptor-side fix is possible (mswjs/interceptors documents this catch-22).For context on impact: this is why http-proxy-middleware v4's own msw e2e suite has to force
followRedirects: trueon every proxy (swapping to the follow-redirects transport, which buffers) — and why downstream consumers testing proxies with msw/nock can't move to httpxy-based engines at all without rewriting their tests against real servers.Happy to open a PR making the gate conditional (immediate pipe for HTTP/1
ClientRequest, keep the deferral where the race actually lives) if that direction sounds right to you — it would keep the PR #111 regression test green while restoring interceptability.cuongluu8 commentedon Sep 4, 2026
Verified downstream: built
main(3e68d30) into a tarball and ran it under http-proxy-middleware 4.2.0 against our Express BFF's vitest + msw server suite — the 13 proxied-route tests that previously hung (including 8 KB POST bodies through mocked upstreams) all pass with zero test changes. Thanks for the quick fix — looking forward to the next release.pi0x commentedon Sep 4, 2026
Thanks for the follow-up — fixed in 3e68d30, which does pipe unconditionally.
Two corrections on the history, since they change the conclusion:
proxy.web()with the samereqafter a failed connect, where an eager pipe has already drained the body.http/https.request(), so there is no HTTP/2 or raw-socket write path for a conditional gate to protect — it would be a no-op branch.The retry case from #111 is genuinely lost, and it can't be kept: under msw the mock socket is indistinguishable from a real connecting one (
pending,connecting,readyState,_handle,_handle.fdall identical), so there's no condition to gate on. Regression coverage added with a mock socket that connects only after the request is written, so this stays fixed.Your
followRedirects: trueobservation checks out, by the way — verified against the pre-fix code: plain hangs,followRedirectsresponds, because that path tees the body instead of waiting onconnect.🤖 Written with AI assistance; claims above verified against the repo.
cuongluu8 commentedon Sep 15, 2026
When will this change be released? @pi0x
fix(web-incoming): defer body piping until socket is assigned