Skip to content

Commit bf61f18

Browse files
committed
fix(node/web): handle requests without an abort signal
1 parent 2fa4969 commit bf61f18

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎src/adapters/_node/web/socket.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ export class WebRequestSocket extends Duplex implements NodeSocket {
7070
// signal into the `Duplex` constructor instead would destroy the stream
7171
// mid-construction for an already-aborted signal, making `_destroy()` throw
7272
// while accessing not-yet-initialized private fields.
73-
addAbortSignal(request.signal, this);
73+
// `signal` is not guaranteed: request-like objects may omit it, and `addAbortSignal`
74+
// throws on anything that is not an `AbortSignal`.
75+
if (request.signal) {
76+
addAbortSignal(request.signal, this);
77+
}
7478
}
7579

7680
setTimeout(ms?: number, cb?: () => void): this {

‎test/node-adapters.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,28 @@ describe("adapters", () => {
243243
expect(settled).not.toBe("hung");
244244
});
245245

246+
test.skipIf(noBridge)("request without a signal is handled", async () => {
247+
const req = new Request("http://localhost/", { method: "POST", body: "ping" });
248+
// Shadow the prototype `signal` getter with an own `undefined` property,
249+
// keeping the rest of the Request (headers, body) intact.
250+
Object.defineProperty(req, "signal", { value: undefined, configurable: true });
251+
expect(req.signal).toBeUndefined();
252+
253+
// Echoes the request body so the socket's readable side is exercised too.
254+
const echoHandler: NodeHttp1Handler = (nodeReq, nodeRes) => {
255+
const chunks: Buffer[] = [];
256+
nodeReq.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
257+
nodeReq.on("end", () => {
258+
nodeRes.writeHead(200, { "content-type": "text/plain" });
259+
nodeRes.end(Buffer.concat(chunks));
260+
});
261+
};
262+
263+
const res = await fetchNodeHandler(echoHandler, req);
264+
expect(res.status).toBe(200);
265+
expect(await res.text()).toBe("ping");
266+
});
267+
246268
// https://github.com/h3js/srvx/issues/208
247269
test("client abort mid-stream does not crash or hang", async () => {
248270
const controller = new AbortController();

0 commit comments

Comments
 (0)