From 73975131380d6407dbdec622eaf42328f15e16d3 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Mon, 29 Jul 2024 15:39:16 +0900 Subject: [PATCH 1/4] fix: do not set localhost to hostname unnecessarily --- ext/http/00_serve.ts | 19 +++++++++---------- tests/unit/serve_test.ts | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/ext/http/00_serve.ts b/ext/http/00_serve.ts index a58d19d767622c..a7ad7dabdc4d52 100644 --- a/ext/http/00_serve.ts +++ b/ext/http/00_serve.ts @@ -654,22 +654,21 @@ function serve(arg1, arg2) { } const addr = listener.addr; - // If the hostname is "0.0.0.0", we display "localhost" in console - // because browsers in Windows don't resolve "0.0.0.0". - // See the discussion in https://github.com/denoland/deno_std/issues/1165 - const hostname = (addr.hostname == "0.0.0.0" || addr.hostname == "::") && - (Deno.build.os === "windows") - ? "localhost" - : addr.hostname; - addr.hostname = hostname; const onListen = (scheme) => { if (options.onListen) { options.onListen(addr); } else { - const host = StringPrototypeIncludes(addr.hostname, ":") - ? `[${addr.hostname}]` + // If the hostname is "0.0.0.0", we display "localhost" in console + // because browsers in Windows don't resolve "0.0.0.0". + // See the discussion in https://github.com/denoland/deno_std/issues/1165 + const hostname = (addr.hostname == "0.0.0.0" || addr.hostname == "::") && + (Deno.build.os === "windows") + ? "localhost" : addr.hostname; + const host = StringPrototypeIncludes(hostname, ":") + ? `[${hostname}]` + : hostname; console.log(`Listening on ${scheme}${host}:${addr.port}/`); } }; diff --git a/tests/unit/serve_test.ts b/tests/unit/serve_test.ts index 450ab6d93b6d93..8b7f1501c57739 100644 --- a/tests/unit/serve_test.ts +++ b/tests/unit/serve_test.ts @@ -4061,3 +4061,19 @@ Deno.test({ 'Operation `"op_net_listen_unix"` not supported on non-unix platforms.', ); }); + +Deno.test({ + name: "onListen callback gets 0.0.0.0 hostname as is", +}, async () => { + const { promise, resolve } = Promise.withResolvers<{ hostname: string }>(); + + const server = Deno.serve({ + handler: (_) => new Response("ok"), + hostname: "0.0.0.0", + port: 0, + onListen: resolve, + }); + const { hostname } = await promise; + assertEquals(hostname, "0.0.0.0"); + await server.shutdown(); +}); From 2fb460a4ca5afc571b894f18e48830c8c5ee5c26 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Fri, 16 Aug 2024 20:07:16 +0900 Subject: [PATCH 2/4] fix test case on windows --- tests/unit_node/net_test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit_node/net_test.ts b/tests/unit_node/net_test.ts index 521de1e73df46d..76671cf0a38758 100644 --- a/tests/unit_node/net_test.ts +++ b/tests/unit_node/net_test.ts @@ -77,6 +77,7 @@ Deno.test("[node/net] net.connect().unref() works", async () => { port: 0, // any available port will do handler: () => new Response("hello"), onListen: async ({ port, hostname }) => { + hostname = Deno.build.os === "windows" ? "localhost" : hostname; const { stdout, stderr } = await new Deno.Command(Deno.execPath(), { args: [ "eval", From 902550e07466da01067db816da162f4a18f55fa9 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 20 Aug 2024 12:10:02 +0900 Subject: [PATCH 3/4] perform the same addr transform for deno serve on windows --- ext/http/00_serve.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/ext/http/00_serve.ts b/ext/http/00_serve.ts index 4f026b81614e6c..b70458c82b5ce5 100644 --- a/ext/http/00_serve.ts +++ b/ext/http/00_serve.ts @@ -581,6 +581,19 @@ type RawServeOptions = { const kLoadBalanced = Symbol("kLoadBalanced"); +function mapAnyAddrToLocalhostForWindows(hostname: string) { + // If the hostname is "0.0.0.0", we display "localhost" in console + // because browsers in Windows don't resolve "0.0.0.0". + // See the discussion in https://github.com/denoland/deno_std/issues/1165 + if ( + (hostname == "0.0.0.0" || hostname == "::") && + (Deno.build.os === "windows") + ) { + return "localhost"; + } + return hostname; +} + function serve(arg1, arg2) { let options: RawServeOptions | undefined; let handler: RawHandler | undefined; @@ -673,13 +686,7 @@ function serve(arg1, arg2) { if (options.onListen) { options.onListen(addr); } else { - // If the hostname is "0.0.0.0", we display "localhost" in console - // because browsers in Windows don't resolve "0.0.0.0". - // See the discussion in https://github.com/denoland/deno_std/issues/1165 - const hostname = (addr.hostname == "0.0.0.0" || addr.hostname == "::") && - (Deno.build.os === "windows") - ? "localhost" - : addr.hostname; + const hostname = mapAnyAddrToLocalhostForWindows(addr.hostname); const host = StringPrototypeIncludes(hostname, ":") ? `[${hostname}]` : hostname; @@ -855,8 +862,9 @@ function registerDeclarativeServer(exports) { const nThreads = serveWorkerCount > 1 ? ` with ${serveWorkerCount} threads` : ""; + const hostname_ = mapAnyAddrToLocalhostForWindows(hostname); console.debug( - `%cdeno serve%c: Listening on %chttp://${hostname}:${port}/%c${nThreads}`, + `%cdeno serve%c: Listening on %chttp://${hostname_}:${port}/%c${nThreads}`, "color: green", "color: inherit", "color: yellow", From 6b53b47f55a21bf080313139c24fa9d80470850d Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Mon, 2 Sep 2024 11:10:39 +0900 Subject: [PATCH 4/4] address review feedback --- ext/http/00_serve.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/http/00_serve.ts b/ext/http/00_serve.ts index 0bfc76858dc7b5..c8ddaa64becd40 100644 --- a/ext/http/00_serve.ts +++ b/ext/http/00_serve.ts @@ -588,8 +588,8 @@ function mapAnyAddrToLocalhostForWindows(hostname: string) { // because browsers in Windows don't resolve "0.0.0.0". // See the discussion in https://github.com/denoland/deno_std/issues/1165 if ( - (hostname == "0.0.0.0" || hostname == "::") && - (Deno.build.os === "windows") + (Deno.build.os === "windows") && + (hostname == "0.0.0.0" || hostname == "::") ) { return "localhost"; }