diff --git a/packages/node-http-handler/src/write-request-body.spec.ts b/packages/node-http-handler/src/write-request-body.spec.ts new file mode 100644 index 0000000000000..2847d541ae985 --- /dev/null +++ b/packages/node-http-handler/src/write-request-body.spec.ts @@ -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; + }); +}); diff --git a/packages/node-http-handler/src/write-request-body.ts b/packages/node-http-handler/src/write-request-body.ts index afd05c9bb6e99..bb018437e3180 100644 --- a/packages/node-http-handler/src/write-request-body.ts +++ b/packages/node-http-handler/src/write-request-body.ts @@ -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([ @@ -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(