Skip to content

Commit 20a210e

Browse files
authored
fix(node-http-handler): stop waiting for continue event on error (#4805)
1 parent 603c919 commit 20a210e

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import EventEmitter from "events";
2+
3+
import { writeRequestBody } from "./write-request-body";
4+
5+
describe(writeRequestBody.name, () => {
6+
it("should continue on the continue event", async () => {
7+
const emitter = Object.assign(new EventEmitter(), { end() {} }) as any;
8+
const request = {
9+
headers: { expect: "100-continue" },
10+
body: Buffer.from(""),
11+
end() {},
12+
} as any;
13+
14+
const promise = writeRequestBody(emitter, request, 10_000);
15+
emitter.emit("continue", "ok");
16+
await promise;
17+
});
18+
19+
it("should continue on the error event", async () => {
20+
const emitter = Object.assign(new EventEmitter(), { end() {} }) as any;
21+
const request = {
22+
headers: { expect: "100-continue" },
23+
body: Buffer.from(""),
24+
} as any;
25+
26+
const promise = writeRequestBody(emitter, request, 10_000);
27+
emitter.emit("error", "uh oh");
28+
await promise;
29+
});
30+
});

packages/node-http-handler/src/write-request-body.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export async function writeRequestBody(
2121
const expect = headers["Expect"] || headers["expect"];
2222

2323
let timeoutId = -1;
24+
let hasError = false;
2425

2526
if (expect === "100-continue") {
2627
await Promise.race<void>([
@@ -32,11 +33,22 @@ export async function writeRequestBody(
3233
clearTimeout(timeoutId);
3334
resolve();
3435
});
36+
httpRequest.on("error", () => {
37+
hasError = true;
38+
clearTimeout(timeoutId);
39+
// this handler does not reject with the error
40+
// because there is already an error listener
41+
// on the request in node-http-handler
42+
// and node-http2-handler.
43+
resolve();
44+
});
3545
}),
3646
]);
3747
}
3848

39-
writeBody(httpRequest, request.body);
49+
if (!hasError) {
50+
writeBody(httpRequest, request.body);
51+
}
4052
}
4153

4254
function writeBody(

0 commit comments

Comments
 (0)