diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2024-09-05 14:13:06 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-05 14:13:06 +0900 |
commit | e799c2857c81f7656811aaf6a248432fe88e8169 (patch) | |
tree | 98d91c90b9020251b71e4d12dc2bf8618e02599c /ext/http | |
parent | 186f7484da3116aeda474f7f529d417ee542b450 (diff) |
fix(ext/http): do not set localhost to hostname unnecessarily (#24777)
This commit changes when to cause the hostname substition of `0.0.0.0` ->
`localhost`.
Currently we substitute `localhost` to the hostname on windows before
calling `options.onListen`, which prevents the users to do more advanced
thing using hostname string like
https://github.com/denoland/std/issues/5558. This PR changes it not to
substitute it when the user provide `onListen` callback.
closes #24776
unblocks https://github.com/denoland/std/issues/5558
Diffstat (limited to 'ext/http')
-rw-r--r-- | ext/http/00_serve.ts | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/ext/http/00_serve.ts b/ext/http/00_serve.ts index c8f4e0604..c8ddaa64b 100644 --- a/ext/http/00_serve.ts +++ b/ext/http/00_serve.ts @@ -583,6 +583,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 ( + (Deno.build.os === "windows") && + (hostname == "0.0.0.0" || hostname == "::") + ) { + return "localhost"; + } + return hostname; +} + function serve(arg1, arg2) { let options: RawServeOptions | undefined; let handler: RawHandler | undefined; @@ -672,22 +685,15 @@ 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}]` - : addr.hostname; + const hostname = mapAnyAddrToLocalhostForWindows(addr.hostname); + const host = StringPrototypeIncludes(hostname, ":") + ? `[${hostname}]` + : hostname; // deno-lint-ignore no-console console.log(`Listening on ${scheme}${host}:${addr.port}/`); } @@ -862,9 +868,10 @@ function registerDeclarativeServer(exports) { const nThreads = serveWorkerCount > 1 ? ` with ${serveWorkerCount} threads` : ""; + const hostname_ = mapAnyAddrToLocalhostForWindows(hostname); // deno-lint-ignore no-console 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", |