diff options
author | Marvin Hagemeister <marvin@deno.com> | 2024-09-06 11:22:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-06 09:22:52 +0000 |
commit | 73ab32c55124124467ce66eca2220bc4a5dfad0f (patch) | |
tree | 759b8d19b7e7f56c207658629de9cc46a0d06912 /ext/http | |
parent | dcf155516b95ce540805eb04beec823cd079fa0f (diff) |
fix: invalid ipv6 hostname on `deno serve` (#25482)
This PR fixes an invalid URL being printed when running `deno serve`
Before: invalid URL
```sh
$ deno serve --host localhost
deno serve: Listening on http://::1:8000/
```
After: valid URL
```sh
$ deno serve --host localhost
deno serve: Listening on http://[::1]:8000/
```
Diffstat (limited to 'ext/http')
-rw-r--r-- | ext/http/00_serve.ts | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/ext/http/00_serve.ts b/ext/http/00_serve.ts index c8ddaa64b..be1f1f809 100644 --- a/ext/http/00_serve.ts +++ b/ext/http/00_serve.ts @@ -583,7 +583,7 @@ type RawServeOptions = { const kLoadBalanced = Symbol("kLoadBalanced"); -function mapAnyAddrToLocalhostForWindows(hostname: string) { +function formatHostName(hostname: string): 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 @@ -593,7 +593,9 @@ function mapAnyAddrToLocalhostForWindows(hostname: string) { ) { return "localhost"; } - return hostname; + + // Add brackets around ipv6 hostname + return StringPrototypeIncludes(hostname, ":") ? `[${hostname}]` : hostname; } function serve(arg1, arg2) { @@ -690,10 +692,8 @@ function serve(arg1, arg2) { if (options.onListen) { options.onListen(addr); } else { - const hostname = mapAnyAddrToLocalhostForWindows(addr.hostname); - const host = StringPrototypeIncludes(hostname, ":") - ? `[${hostname}]` - : hostname; + const host = formatHostName(addr.hostname); + // deno-lint-ignore no-console console.log(`Listening on ${scheme}${host}:${addr.port}/`); } @@ -868,10 +868,11 @@ function registerDeclarativeServer(exports) { const nThreads = serveWorkerCount > 1 ? ` with ${serveWorkerCount} threads` : ""; - const hostname_ = mapAnyAddrToLocalhostForWindows(hostname); + const host = formatHostName(hostname); + // deno-lint-ignore no-console console.debug( - `%cdeno serve%c: Listening on %chttp://${hostname_}:${port}/%c${nThreads}`, + `%cdeno serve%c: Listening on %chttp://${host}:${port}/%c${nThreads}`, "color: green", "color: inherit", "color: yellow", |