|
1 | 1 | import { createServer } from "node:http"; |
| 2 | +import { Readable } from "node:stream"; |
2 | 3 | import { connect, type AddressInfo } from "node:net"; |
3 | 4 | import { describe, expect, test, vi } from "vitest"; |
4 | 5 |
|
@@ -1380,6 +1381,66 @@ describe("node fetch-spec correctness regressions", () => { |
1380 | 1381 | await server.close(true); |
1381 | 1382 | }); |
1382 | 1383 |
|
| 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 | + |
1383 | 1444 | // statusText defaults to the spec's empty reason phrase, not Node's phrase. |
1384 | 1445 | test("statusText defaults to empty string, not Node's reason phrase", () => { |
1385 | 1446 | expect(new FastResponse("x", { status: 200 }).statusText).toBe(""); |
|
0 commit comments