diff --git a/src/js/node/http.ts b/src/js/node/http.ts index d32a1669a8..d5238dd0f3 100644 --- a/src/js/node/http.ts +++ b/src/js/node/http.ts @@ -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) { diff --git a/test/js/node/http/node-http.test.ts b/test/js/node/http/node-http.test.ts index 355969b003..298b489617 100644 --- a/test/js/node/http/node-http.test.ts +++ b/test/js/node/http/node-http.test.ts @@ -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(); +});