1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

fix(http): Adjust hostname display for Windows when using 0.0.0.0 (#24698)

Fixes #24687

---------

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
(cherry picked from commit 41f8988dc7)
This commit is contained in:
Yazan AbdAl-Rahman 2024-07-29 00:04:05 +03:00 committed by crowlkats
parent bcb7e600c0
commit 4473e81a93
No known key found for this signature in database
GPG key ID: A82C9D461FC483E8
5 changed files with 24 additions and 10 deletions

View file

@ -657,7 +657,8 @@ function serve(arg1, arg2) {
// 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 == "::"
const hostname = (addr.hostname == "0.0.0.0" || addr.hostname == "::") &&
(Deno.build.os === "windows")
? "localhost"
: addr.hostname;
addr.hostname = hostname;

View file

@ -21,8 +21,8 @@ async fn deno_serve_port_0() {
let mut buffer = [0; 52];
let _read = stdout.read(&mut buffer).unwrap();
let msg = std::str::from_utf8(&buffer).unwrap();
let port_regex = Regex::new(r"(\d+)").unwrap();
let port = port_regex.find(msg).unwrap().as_str();
let port_regex = Regex::new(r":(\d+)").unwrap();
let port = port_regex.captures(msg).unwrap().get(1).unwrap().as_str();
let cert = reqwest::Certificate::from_pem(include_bytes!(
"../testdata/tls/RootCA.crt"
@ -64,8 +64,8 @@ async fn deno_serve_no_args() {
let mut buffer = [0; 52];
let _read = stdout.read(&mut buffer).unwrap();
let msg = std::str::from_utf8(&buffer).unwrap();
let port_regex = Regex::new(r"(\d+)").unwrap();
let port = port_regex.find(msg).unwrap().as_str();
let port_regex = Regex::new(r":(\d+)").unwrap();
let port = port_regex.captures(msg).unwrap().get(1).unwrap().as_str();
let cert = reqwest::Certificate::from_pem(include_bytes!(
"../testdata/tls/RootCA.crt"

View file

@ -1,5 +1,15 @@
{
"args": "serve --port 12345 main.ts",
"output": "main.out",
"tempDir": true
"tempDir": true,
"tests": {
"basic_win": {
"if": "windows",
"args": "serve --host 0.0.0.0 --port 12345 main.ts",
"output": "main.out"
},
"basic_not_win": {
"if": "unix",
"args": "serve --host 0.0.0.0 --port 12345 main.ts",
"output": "main_not_win.out"
}
}
}

View file

@ -0,0 +1 @@
deno serve: Listening on http://0.0.0.0:12345/

View file

@ -748,9 +748,11 @@ Deno.test(
const consoleLog = console.log;
console.log = (msg) => {
try {
const match = msg.match(/Listening on http:\/\/localhost:(\d+)\//);
const match = msg.match(
/Listening on http:\/\/(localhost|0\.0\.0\.0):(\d+)\//,
);
assert(!!match, `Didn't match ${msg}`);
const port = +match[1];
const port = +match[2];
assert(port > 0 && port < 65536);
} finally {
ac.abort();