Skip to content

Commit e2fc775

Browse files
committed
fix(node): discard the body for HEAD requests to a Node stream response
1 parent 27ffd76 commit e2fc775

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

‎src/adapters/_node/send.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,22 @@ function pipeBody(
222222
return;
223223
}
224224

225+
// HEAD responses must carry no body. Node's write() is a no-op that always
226+
// returns true for HEAD, so pipeline() never sees backpressure and drains an
227+
// unbounded body (e.g. an SSE stream) as fast as it is produced, starving the
228+
// event loop. Discard the source up front instead, mirroring streamBody.
229+
// Headers are still deferred here, so write them before ending.
230+
if ((nodeRes as NodeHttp.ServerResponse).req?.method === "HEAD") {
231+
if (typeof stream.destroy === "function") {
232+
stream.destroy();
233+
} else {
234+
// Duck-typed pipe objects (e.g. React's PipeableStream) expose abort().
235+
(stream as unknown as { abort?: () => void }).abort?.();
236+
}
237+
writeHead(nodeRes, status, statusText, headers);
238+
return endNodeResponse(nodeRes);
239+
}
240+
225241
// Duck-typed pipe objects (e.g. React's PipeableStream) only have .pipe()
226242
// and don't support pipeline() — use the raw path.
227243
if (typeof stream.on !== "function" || typeof stream.destroy !== "function") {

‎test/node-adapters.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createServer } from "node:http";
2+
import { Readable } from "node:stream";
23
import { connect, type AddressInfo } from "node:net";
34
import { describe, expect, test, vi } from "vitest";
45

@@ -1380,6 +1381,66 @@ describe("node fetch-spec correctness regressions", () => {
13801381
await server.close(true);
13811382
});
13821383

1384+
// Same guarantee for the Node `Readable` fast path, which goes through
1385+
// pipeBody instead of streamBody. Node's write() is a no-op for HEAD, so
1386+
// without the guard the stream is pumped with zero backpressure.
1387+
test("HEAD discards a Node Readable body instead of pumping it", async () => {
1388+
let reads = 0;
1389+
let readable!: Readable;
1390+
const server = serve({
1391+
port: 0,
1392+
fetch() {
1393+
readable = new Readable({
1394+
read() {
1395+
reads++;
1396+
// Bounded so a regression fails the assertion below instead of
1397+
// OOMing the worker (the real-world body would be unbounded).
1398+
this.push(reads > 64 ? null : Buffer.alloc(1024, "x"));
1399+
},
1400+
});
1401+
return new FastResponse(readable as unknown as BodyInit);
1402+
},
1403+
});
1404+
await server.ready();
1405+
1406+
const res = await fetch(server.url!, { method: "HEAD" });
1407+
expect(res.status).toBe(200);
1408+
expect((await res.arrayBuffer()).byteLength).toBe(0);
1409+
// Let the destroy settle.
1410+
await new Promise((r) => setTimeout(r, 50));
1411+
expect(readable.destroyed).toBe(true);
1412+
// The stream was destroyed up front rather than pumped.
1413+
expect(reads).toBeLessThanOrEqual(1);
1414+
await server.close(true);
1415+
});
1416+
1417+
// The HEAD guard still commits to the handler's status/headers, and does not
1418+
// leak into non-HEAD requests on the same server.
1419+
test("HEAD keeps the handler's headers for a Node Readable body", async () => {
1420+
const server = serve({
1421+
port: 0,
1422+
fetch() {
1423+
return new FastResponse(Readable.from(["hello", "world"]) as unknown as BodyInit, {
1424+
status: 206,
1425+
headers: { "content-length": "10", "x-custom": "1" },
1426+
});
1427+
},
1428+
});
1429+
await server.ready();
1430+
1431+
const headRes = await fetch(server.url!, { method: "HEAD" });
1432+
expect(headRes.status).toBe(206);
1433+
expect(headRes.headers.get("content-length")).toBe("10");
1434+
expect(headRes.headers.get("x-custom")).toBe("1");
1435+
expect((await headRes.arrayBuffer()).byteLength).toBe(0);
1436+
1437+
// A GET on the same server still streams the body.
1438+
const getRes = await fetch(server.url!);
1439+
expect(getRes.status).toBe(206);
1440+
expect(await getRes.text()).toBe("helloworld");
1441+
await server.close(true);
1442+
});
1443+
13831444
// statusText defaults to the spec's empty reason phrase, not Node's phrase.
13841445
test("statusText defaults to empty string, not Node's reason phrase", () => {
13851446
expect(new FastResponse("x", { status: 200 }).statusText).toBe("");

‎test/node-streams.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,35 @@ describe("node response stream error handling", () => {
300300
expect(await res.text()).toBe("hello from pipeable");
301301
await server.close(true);
302302
});
303+
304+
test("HEAD aborts a duck-typed pipe object instead of piping it", async () => {
305+
let aborted = false;
306+
let piped = false;
307+
const server = serve({
308+
port: 0,
309+
fetch() {
310+
const pipeableStream = {
311+
pipe(writable: NodeJS.WritableStream) {
312+
piped = true;
313+
writable.write("hello from pipeable");
314+
writable.end();
315+
return writable;
316+
},
317+
abort() {
318+
aborted = true;
319+
},
320+
};
321+
return new FastResponse(pipeableStream as unknown as BodyInit);
322+
},
323+
});
324+
await server.ready();
325+
326+
const res = await fetch(server.url!, { method: "HEAD" });
327+
expect(res.status).toBe(200);
328+
expect((await res.arrayBuffer()).byteLength).toBe(0);
329+
await new Promise((r) => setTimeout(r, 50));
330+
expect(aborted).toBe(true);
331+
expect(piped).toBe(false);
332+
await server.close(true);
333+
});
303334
});

0 commit comments

Comments
 (0)