Skip to content

Commit aa53ab4

Browse files
committed
Only output ipv4 addresses + localhost and :: when starting dev server
1 parent f1e9cdf commit aa53ab4

6 files changed

Lines changed: 86 additions & 108 deletions

File tree

.changeset/small-gifts-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"miniflare": patch
3+
---
4+
5+
Only output ipv4 addresses when starting

packages/miniflare/src/http/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export function getAccessibleHosts(ipv4Only = false): string[] {
99
// @ts-expect-error the `family` property is numeric as of Node.js 18.0.0
1010
if (family === "IPv4" || family === 4) {
1111
hosts.push(address);
12-
} else if (!ipv4Only && (family === "IPv6" || family === 6)) {
13-
hosts.push(`[${address}]`);
12+
} else if (!ipv4Only) {
13+
hosts.push(address);
1414
}
1515
});
1616
});

packages/miniflare/src/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,13 +1181,14 @@ export class Miniflare {
11811181
);
11821182

11831183
if (initial) {
1184-
let hosts: string[];
1185-
if (host === "::" || host === "*") {
1186-
hosts = getAccessibleHosts(false);
1187-
} else if (host === "0.0.0.0") {
1188-
hosts = getAccessibleHosts(true);
1189-
} else {
1190-
hosts = [];
1184+
const hosts: string[] = [];
1185+
if (host === "::" || host === "*" || host === "0.0.0.0") {
1186+
hosts.push(...getAccessibleHosts(true));
1187+
1188+
if (host !== "0.0.0.0") {
1189+
hosts.push("localhost");
1190+
hosts.push("[::1]");
1191+
}
11911192
}
11921193

11931194
for (const h of hosts) {

packages/wrangler/src/dev/proxy.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { createServer as createHttpServer } from "node:http";
22
import { connect } from "node:http2";
33
import { createServer as createHttpsServer } from "node:https";
44
import https from "node:https";
5-
import { networkInterfaces } from "node:os";
65
import { createHttpTerminator } from "http-terminator";
6+
import { getAccessibleHosts } from "miniflare";
77
import { useEffect, useRef, useState } from "react";
88
import serveStatic from "serve-static";
99
import { getHttpsOptions } from "../https-options";
@@ -166,7 +166,15 @@ export async function startPreviewServer({
166166
const usedPort =
167167
address && typeof address === "object" ? address.port : port;
168168
logger.log(`⬣ Listening at ${localProtocol}://${ip}:${usedPort}`);
169-
const accessibleHosts = getAccessibleHosts(ip);
169+
const accessibleHosts = [];
170+
if (ip === "::" || ip === "*" || ip === "0.0.0.0") {
171+
accessibleHosts.push(...getAccessibleHosts(true));
172+
173+
if (ip !== "0.0.0.0") {
174+
accessibleHosts.push("localhost");
175+
accessibleHosts.push("[::1]");
176+
}
177+
}
170178
for (const accessibleHost of accessibleHosts) {
171179
logger.log(`- ${localProtocol}://${accessibleHost}:${usedPort}`);
172180
}
@@ -299,7 +307,15 @@ export function usePreviewServer({
299307
const usedPort =
300308
address && typeof address === "object" ? address.port : port;
301309
logger.log(`⬣ Listening at ${localProtocol}://${ip}:${usedPort}`);
302-
const accessibleHosts = getAccessibleHosts(ip);
310+
const accessibleHosts = [];
311+
if (ip === "::" || ip === "*" || ip === "0.0.0.0") {
312+
accessibleHosts.push(...getAccessibleHosts(true));
313+
314+
if (ip !== "0.0.0.0") {
315+
accessibleHosts.push("localhost");
316+
accessibleHosts.push("[::1]");
317+
}
318+
}
303319
for (const accessibleHost of accessibleHosts) {
304320
logger.log(`- ${localProtocol}://${accessibleHost}:${usedPort}`);
305321
}
@@ -681,24 +697,3 @@ export async function waitForPortToBeAvailable(
681697
}
682698
});
683699
}
684-
685-
function getAccessibleHosts(ip: string): string[] {
686-
if (ip !== "0.0.0.0" && ip !== "::" && ip !== "*") {
687-
return [ip];
688-
}
689-
690-
const hosts: string[] = [];
691-
Object.values(networkInterfaces()).forEach((net) => {
692-
net?.forEach(({ family, address }) => {
693-
if (ip === "0.0.0.0") {
694-
// @ts-expect-error the `family` property is numeric as of Node.js 18.0.0
695-
if (family === "IPv4" || family === 4) {
696-
hosts.push(address);
697-
}
698-
} else {
699-
hosts.push(address);
700-
}
701-
});
702-
});
703-
return hosts;
704-
}

packages/wrangler/src/https-options.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from "node:fs";
2-
import os from "node:os";
32
import * as path from "node:path";
43
import { promisify } from "node:util";
4+
import { getAccessibleHosts } from "miniflare";
55
import { getGlobalWranglerConfigPath } from "./global-wrangler-config-path";
66
import { logger } from "./logger";
77
import type { Attributes, Options } from "selfsigned";
@@ -103,7 +103,7 @@ async function generateCertificate() {
103103
name: "subjectAltName",
104104
altNames: [
105105
{ type: 2, value: "localhost" },
106-
...getAccessibleHosts().map((ip) => ({ type: 7, ip })),
106+
...getAccessibleHosts(false).map((ip) => ({ type: 7, ip })),
107107
],
108108
},
109109
],
@@ -112,16 +112,3 @@ async function generateCertificate() {
112112
const { private: key, cert } = await generate(certAttrs, certOptions);
113113
return { key, cert };
114114
}
115-
116-
/**
117-
* Ask the OS for the addresses of locally accessible hosts.
118-
*/
119-
function getAccessibleHosts(ipv4 = false): string[] {
120-
const hosts: string[] = [];
121-
Object.values(os.networkInterfaces()).forEach((net) =>
122-
net?.forEach(({ family, address }) => {
123-
if (!ipv4 || family === "IPv4") hosts.push(address);
124-
})
125-
);
126-
return hosts;
127-
}

pnpm-lock.yaml

Lines changed: 50 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)