From 60da9d493c97fdfa026d163f80ddc25ee01a6e57 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 25 Dec 2023 08:58:51 +0530 Subject: [PATCH] fix(ext/node): add ClientRequest#setNoDelay (#21694) Fixes https://github.com/denoland/deno/issues/18316 --- cli/tests/unit_node/http_test.ts | 21 +++++++++++++++++++++ ext/node/polyfills/http.ts | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/cli/tests/unit_node/http_test.ts b/cli/tests/unit_node/http_test.ts index 5285d8e23e..72525d803f 100644 --- a/cli/tests/unit_node/http_test.ts +++ b/cli/tests/unit_node/http_test.ts @@ -584,6 +584,27 @@ Deno.test("[node/http] ClientRequest setTimeout", async () => { assertEquals(body, "HTTP/1.1"); }); +Deno.test("[node/http] ClientRequest setNoDelay", async () => { + let body = ""; + const { promise, resolve, reject } = Promise.withResolvers(); + const timer = setTimeout(() => reject("timed out"), 50000); + const req = http.request("http://localhost:4545/http_version", (resp) => { + resp.on("data", (chunk) => { + body += chunk; + }); + + resp.on("end", () => { + resolve(); + }); + }); + req.setNoDelay(true); + req.once("error", (e) => reject(e)); + req.end(); + await promise; + clearTimeout(timer); + assertEquals(body, "HTTP/1.1"); +}); + Deno.test("[node/http] ClientRequest PATCH", async () => { let body = ""; const { promise, resolve, reject } = Promise.withResolvers(); diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index d458541628..e49f717023 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -892,6 +892,11 @@ class ClientRequest extends OutgoingMessage { } headers.push([key, value]); } + + // Once a socket is assigned to this request and is connected socket.setNoDelay() will be called. + setNoDelay() { + this.socket?.setNoDelay?.(); + } } // isCookieField performs a case-insensitive comparison of a provided string