Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use ipv4 for windows and wsl default hosts #119

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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