Skip to content

Commit

Permalink
fix(node-http-handler): stop waiting for continue event on error (#4805)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe authored Jun 8, 2023
1 parent 603c919 commit 20a210e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
30 changes: 30 additions & 0 deletions packages/node-http-handler/src/write-request-body.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import EventEmitter from "events";

import { writeRequestBody } from "./write-request-body";

describe(writeRequestBody.name, () => {
it("should continue on the continue event", async () => {
const emitter = Object.assign(new EventEmitter(), { end() {} }) as any;
const request = {
headers: { expect: "100-continue" },
body: Buffer.from(""),
end() {},
} as any;

const promise = writeRequestBody(emitter, request, 10_000);
emitter.emit("continue", "ok");
await promise;
});

it("should continue on the error event", async () => {
const emitter = Object.assign(new EventEmitter(), { end() {} }) as any;
const request = {
headers: { expect: "100-continue" },
body: Buffer.from(""),
} as any;

const promise = writeRequestBody(emitter, request, 10_000);
emitter.emit("error", "uh oh");
await promise;
});
});
14 changes: 13 additions & 1 deletion packages/node-http-handler/src/write-request-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export async function writeRequestBody(
const expect = headers["Expect"] || headers["expect"];

let timeoutId = -1;
let hasError = false;

if (expect === "100-continue") {
await Promise.race<void>([
Expand All @@ -32,11 +33,22 @@ export async function writeRequestBody(
clearTimeout(timeoutId);
resolve();
});
httpRequest.on("error", () => {
hasError = true;
clearTimeout(timeoutId);
// this handler does not reject with the error
// because there is already an error listener
// on the request in node-http-handler
// and node-http2-handler.
resolve();
});
}),
]);
}

writeBody(httpRequest, request.body);
if (!hasError) {
writeBody(httpRequest, request.body);
}
}

function writeBody(
Expand Down

0 comments on commit 20a210e

Please sign in to comment.