Skip to content

Commit 8847699

Browse files
committed
emit continue
1 parent 02ff16d commit 8847699

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/js/node/http.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,16 @@ Object.defineProperty(OutgoingMessage.prototype, "finished", {
11331133
},
11341134
});
11351135

1136+
function emitContinueAndSocketNT(self) {
1137+
if (self.destroyed) return;
1138+
// Ref: https://github.com/nodejs/node/blob/f63e8b7fa7a4b5e041ddec67307609ec8837154f/lib/_http_client.js#L803-L839
1139+
self.emit("socket", self.socket);
1140+
1141+
//Emit continue event for the client (internally we auto handle it)
1142+
if (!self._closed && self.getHeader("expect") === "100-continue") {
1143+
self.emit("continue");
1144+
}
1145+
}
11361146
function emitCloseNT(self) {
11371147
if (!self._closed) {
11381148
self._closed = true;
@@ -1888,11 +1898,7 @@ class ClientRequest extends OutgoingMessage {
18881898

18891899
this._httpMessage = this;
18901900

1891-
process.nextTick(() => {
1892-
// Ref: https://github.com/nodejs/node/blob/f63e8b7fa7a4b5e041ddec67307609ec8837154f/lib/_http_client.js#L803-L839
1893-
if (this.destroyed) return;
1894-
this.emit("socket", this.socket);
1895-
});
1901+
process.nextTick(emitContinueAndSocketNT, this);
18961902
}
18971903

18981904
setSocketKeepAlive(enable = true, initialDelay = 0) {

test/js/node/http/node-http.test.ts

+47
Original file line numberDiff line numberDiff line change
@@ -2252,3 +2252,50 @@ it.skip("should be able to stream huge amounts of data", async () => {
22522252
server.close();
22532253
}
22542254
}, 30_000);
2255+
2256+
// TODO: today we use a workaround to continue event, we need to fix it in the future.
2257+
it("should emit continue event #7480", done => {
2258+
let receivedContinue = false;
2259+
const req = request(
2260+
"https://example.com",
2261+
{ headers: { "accept-encoding": "identity", "expect": "100-continue" } },
2262+
res => {
2263+
let data = "";
2264+
res.setEncoding("utf8");
2265+
res.on("data", chunk => {
2266+
data += chunk;
2267+
});
2268+
res.on("end", () => {
2269+
expect(receivedContinue).toBe(true);
2270+
expect(data).toContain("This domain is for use in illustrative examples in documents");
2271+
done();
2272+
});
2273+
res.on("error", err => done(err));
2274+
},
2275+
);
2276+
req.on("continue", () => {
2277+
receivedContinue = true;
2278+
});
2279+
req.end();
2280+
});
2281+
2282+
it("should not emit continue event #7480", done => {
2283+
let receivedContinue = false;
2284+
const req = request("https://example.com", { headers: { "accept-encoding": "identity" } }, res => {
2285+
let data = "";
2286+
res.setEncoding("utf8");
2287+
res.on("data", chunk => {
2288+
data += chunk;
2289+
});
2290+
res.on("end", () => {
2291+
expect(receivedContinue).toBe(false);
2292+
expect(data).toContain("This domain is for use in illustrative examples in documents");
2293+
done();
2294+
});
2295+
res.on("error", err => done(err));
2296+
});
2297+
req.on("continue", () => {
2298+
receivedContinue = true;
2299+
});
2300+
req.end();
2301+
});

0 commit comments

Comments
 (0)