Skip to content

Commit

Permalink
fix: use ipv4 for windows and wsl default hostname (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 30, 2023
1 parent c64fc61 commit 9418ae2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { networkInterfaces } from "node:os";
import { relative } from "pathe";
import { colors } from "consola/utils";
import { ListenOptions } from "./types";
import { isWsl } from "./lib/wsl";

export function getNetworkInterfaces(includeIPV6?: boolean): string[] {
const addrs = new Set<string>();
Expand Down Expand Up @@ -70,6 +71,16 @@ export function generateURL(
);
}

export function getDefaultHost(preferPublic?: boolean) {
// Prefer IPV4 stack for Windows and WSL to avoid performance issues
if (process.platform === "win32" || isWsl()) {
return preferPublic ? "0.0.0.0" : "127.0.0.1";
}
// For local, use "localhost" to be developer friendly and allow loopback customization}
// For public, use "" to listen on all NIC interfaces (IPV4 and IPV6)
return preferPublic ? "" : "localhost";
}

export function getPublicURL(
listhenOptions: ListenOptions,
baseURL?: string,
Expand Down
3 changes: 2 additions & 1 deletion src/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
isAnyhost,
getPublicURL,
generateURL,
getDefaultHost,
} from "./_utils";
import { resolveCertificate } from "./_cert";

Expand All @@ -49,7 +50,7 @@ export async function listen(
name: "",
https: false,
port: process.env.PORT || 3000,
hostname: _hostname ?? (_public ? "" : "localhost"),
hostname: _hostname ?? getDefaultHost(_public),
showURL: true,
baseURL: "/",
open: false,
Expand Down

0 comments on commit 9418ae2

Please sign in to comment.