diff --git a/ext/node/polyfills/_http_outgoing.ts b/ext/node/polyfills/_http_outgoing.ts index e412ce7571..b3bbcdb3ff 100644 --- a/ext/node/polyfills/_http_outgoing.ts +++ b/ext/node/polyfills/_http_outgoing.ts @@ -556,6 +556,7 @@ Object.defineProperties( if (data instanceof Buffer) { data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength); } + await this._bodyWriter.ready; ret = await this._bodyWriter.write(data).then(() => { callback?.(); this.emit("drain"); @@ -587,9 +588,6 @@ Object.defineProperties( if (this._bodyWriter.desiredSize > 0) { this._bodyWriter.write(data).then(() => { callback?.(); - if (this.outputData.length == 0) { - this.emit("finish"); - } this.emit("drain"); }).catch((e) => { this._requestSendError = e; diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index 4a1b951872..7307934d14 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -678,10 +678,14 @@ class ClientRequest extends OutgoingMessage { } }; - if (this.socket && this._bodyWriter) { + if (this.socket && this._bodyWriter && this.outputData.length === 0) { finish(); } else { - this.on("finish", finish); + this.on("drain", () => { + if (this.outputData.length === 0) { + finish(); + } + }); } return this; diff --git a/tests/unit_node/http_test.ts b/tests/unit_node/http_test.ts index f3f03ca8a6..6a1d708f5f 100644 --- a/tests/unit_node/http_test.ts +++ b/tests/unit_node/http_test.ts @@ -998,7 +998,7 @@ Deno.test( request.on("close", () => {}); request.end(); setTimeout(() => { - request.destroy(new Error()); + request.destroy(); resolve(); }, 100); @@ -1463,9 +1463,7 @@ Deno.test( }, ); -Deno.test("[node/http] http.request() post streaming body works", { - ignore: true, -}, async () => { +Deno.test("[node/http] http.request() post streaming body works", async () => { const server = http.createServer((req, res) => { if (req.method === "POST") { let receivedBytes = 0;