fix(node:http/node:https) emit continue (#13434)

This commit is contained in:
Ciro Spaciari
2024-08-21 00:10:21 +00:00
committed by GitHub
parent 02ff16d95c
commit 8ace981fbc
2 changed files with 58 additions and 5 deletions

View File

@@ -1133,6 +1133,16 @@ Object.defineProperty(OutgoingMessage.prototype, "finished", {
},
});
function emitContinueAndSocketNT(self) {
if (self.destroyed) return;
// Ref: https://github.com/nodejs/node/blob/f63e8b7fa7a4b5e041ddec67307609ec8837154f/lib/_http_client.js#L803-L839
self.emit("socket", self.socket);
//Emit continue event for the client (internally we auto handle it)
if (!self._closed && self.getHeader("expect") === "100-continue") {
self.emit("continue");
}
}
function emitCloseNT(self) {
if (!self._closed) {
self._closed = true;
@@ -1888,11 +1898,7 @@ class ClientRequest extends OutgoingMessage {
this._httpMessage = this;
process.nextTick(() => {
// Ref: https://github.com/nodejs/node/blob/f63e8b7fa7a4b5e041ddec67307609ec8837154f/lib/_http_client.js#L803-L839
if (this.destroyed) return;
this.emit("socket", this.socket);
});
process.nextTick(emitContinueAndSocketNT, this);
}
setSocketKeepAlive(enable = true, initialDelay = 0) {

View File

@@ -2252,3 +2252,50 @@ it.skip("should be able to stream huge amounts of data", async () => {
server.close();
}
}, 30_000);
// TODO: today we use a workaround to continue event, we need to fix it in the future.
it("should emit continue event #7480", done => {
let receivedContinue = false;
const req = request(
"https://example.com",
{ headers: { "accept-encoding": "identity", "expect": "100-continue" } },
res => {
let data = "";
res.setEncoding("utf8");
res.on("data", chunk => {
data += chunk;
});
res.on("end", () => {
expect(receivedContinue).toBe(true);
expect(data).toContain("This domain is for use in illustrative examples in documents");
done();
});
res.on("error", err => done(err));
},
);
req.on("continue", () => {
receivedContinue = true;
});
req.end();
});
it("should not emit continue event #7480", done => {
let receivedContinue = false;
const req = request("https://example.com", { headers: { "accept-encoding": "identity" } }, res => {
let data = "";
res.setEncoding("utf8");
res.on("data", chunk => {
data += chunk;
});
res.on("end", () => {
expect(receivedContinue).toBe(false);
expect(data).toContain("This domain is for use in illustrative examples in documents");
done();
});
res.on("error", err => done(err));
});
req.on("continue", () => {
receivedContinue = true;
});
req.end();
});