Skip to content

Commit c7510cf

Browse files
authored
Merge branch 'master' into issue-2843/refactor-e2e-tests-from-puppeter-to-playwright
2 parents b101a6d + f5f0902 commit c7510cf

File tree

8 files changed

+123
-492
lines changed

8 files changed

+123
-492
lines changed

lib/Server.js

+51-26
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,50 @@ class Server {
379379
}
380380

381381
/**
382-
* @param {string} gateway
382+
* @param {string} gatewayOrFamily or family
383+
* @param {boolean} [isInternal=false] ip should be internal
383384
* @returns {string | undefined}
384385
*/
385-
static findIp(gateway) {
386-
const gatewayIp = ipaddr.parse(gateway);
386+
static findIp(gatewayOrFamily, isInternal = false) {
387+
if (gatewayOrFamily === "v4" || gatewayOrFamily === "v6") {
388+
let host;
389+
390+
Object.values(os.networkInterfaces())
391+
.flatMap((networks) => networks ?? [])
392+
.filter((network) => {
393+
if (!network || !network.address) {
394+
return false;
395+
}
396+
397+
if (network.family !== `IP${gatewayOrFamily}`) {
398+
return false;
399+
}
400+
401+
if (network.internal !== isInternal) {
402+
return false;
403+
}
404+
405+
if (gatewayOrFamily === "v6") {
406+
const range = ipaddr.parse(network.address).range();
407+
408+
if (range !== "ipv4Mapped" && range !== "uniqueLocal") {
409+
return false;
410+
}
411+
}
412+
413+
return network.address;
414+
})
415+
.forEach((network) => {
416+
host = network.address;
417+
if (host.includes(":")) {
418+
host = `[${host}]`;
419+
}
420+
});
421+
422+
return host;
423+
}
424+
425+
const gatewayIp = ipaddr.parse(gatewayOrFamily);
387426

388427
// Look for the matching interface in all local interfaces.
389428
for (const addresses of Object.values(os.networkInterfaces())) {
@@ -403,32 +442,22 @@ class Server {
403442
}
404443
}
405444

445+
// TODO remove me in the next major release, we have `findIp`
406446
/**
407447
* @param {"v4" | "v6"} family
408448
* @returns {Promise<string | undefined>}
409449
*/
410450
static async internalIP(family) {
411-
try {
412-
const { gateway } = await require("default-gateway")[family]();
413-
414-
return Server.findIp(gateway);
415-
} catch {
416-
// ignore
417-
}
451+
return Server.findIp(family);
418452
}
419453

454+
// TODO remove me in the next major release, we have `findIp`
420455
/**
421456
* @param {"v4" | "v6"} family
422457
* @returns {string | undefined}
423458
*/
424459
static internalIPSync(family) {
425-
try {
426-
const { gateway } = require("default-gateway")[family].sync();
427-
428-
return Server.findIp(gateway);
429-
} catch {
430-
// ignore
431-
}
460+
return Server.findIp(family);
432461
}
433462

434463
/**
@@ -437,15 +466,11 @@ class Server {
437466
*/
438467
static async getHostname(hostname) {
439468
if (hostname === "local-ip") {
440-
return (
441-
(await Server.internalIP("v4")) ||
442-
(await Server.internalIP("v6")) ||
443-
"0.0.0.0"
444-
);
469+
return Server.findIp("v4") || Server.findIp("v6") || "0.0.0.0";
445470
} else if (hostname === "local-ipv4") {
446-
return (await Server.internalIP("v4")) || "0.0.0.0";
471+
return Server.findIp("v4") || "0.0.0.0";
447472
} else if (hostname === "local-ipv6") {
448-
return (await Server.internalIP("v6")) || "::";
473+
return Server.findIp("v6") || "::";
449474
}
450475

451476
return hostname;
@@ -2777,13 +2802,13 @@ class Server {
27772802
if (parsedIP.range() === "unspecified") {
27782803
localhost = prettyPrintURL("localhost");
27792804

2780-
const networkIPv4 = await Server.internalIP("v4");
2805+
const networkIPv4 = Server.findIp("v4");
27812806

27822807
if (networkIPv4) {
27832808
networkUrlIPv4 = prettyPrintURL(networkIPv4);
27842809
}
27852810

2786-
const networkIPv6 = await Server.internalIP("v6");
2811+
const networkIPv6 = Server.findIp("v6");
27872812

27882813
if (networkIPv6) {
27892814
networkUrlIPv6 = prettyPrintURL(networkIPv6);

package-lock.json

+23-41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"colorette": "^2.0.10",
6161
"compression": "^1.7.4",
6262
"connect-history-api-fallback": "^2.0.0",
63-
"default-gateway": "^6.0.3",
6463
"express": "^4.19.2",
6564
"graceful-fs": "^4.2.6",
6665
"html-entities": "^2.4.0",
@@ -89,7 +88,6 @@
8988
"@commitlint/config-conventional": "^19.0.3",
9089
"@playwright/test": "^1.44.0",
9190
"@types/compression": "^1.7.2",
92-
"@types/default-gateway": "^3.0.1",
9391
"@types/node": "^20.11.16",
9492
"@types/node-forge": "^1.3.1",
9593
"@types/sockjs-client": "^1.5.1",
@@ -135,7 +133,7 @@
135133
"style-loader": "^4.0.0",
136134
"supertest": "^6.1.3",
137135
"tcp-port-used": "^1.0.2",
138-
"typescript": "^5.3.3",
136+
"typescript": "^5.5.4",
139137
"wait-for-expect": "^3.0.2",
140138
"webpack": "^5.91.0",
141139
"webpack-cli": "^5.0.1",

test/cli/host-option.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const { testBin, normalizeStderr } = require("../helpers/test-bin");
44
const port = require("../ports-map")["cli-host"];
55
const Server = require("../../lib/Server");
66

7-
const localIPv4 = Server.internalIPSync("v4");
8-
const localIPv6 = Server.internalIPSync("v6");
7+
const localIPv4 = Server.findIp("v4");
8+
const localIPv6 = Server.findIp("v6");
99

1010
describe('"host" CLI option', () => {
1111
it('should work using "--host 0.0.0.0" (IPv4)', async () => {

test/e2e/host.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const { expect } = require("../helpers/playwright-custom-expects");
77
const config = require("../fixtures/client-config/webpack.config");
88
const port = require("../ports-map").host;
99

10-
const ipv4 = Server.internalIPSync("v4");
11-
const ipv6 = Server.internalIPSync("v6");
10+
const ipv4 = Server.findIp("v4");
11+
const ipv6 = Server.findIp("v6");
1212
// macos requires root for using ip v6
1313
const isMacOS = process.platform === "darwin";
1414

0 commit comments

Comments
 (0)