Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node-http-handler): stop waiting for continue event on error #4805

Merged
merged 1 commit into from Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions 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;
});
});
14 changes: 13 additions & 1 deletion packages/node-http-handler/src/write-request-body.ts
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