Skip to content

Commit 547f7db

Browse files
committed
Merge branch 'master' into issue-2843/refactor-e2e-tests-from-puppeter-to-playwright
# Conflicts: # package-lock.json
2 parents b101a6d + f5f0902 commit 547f7db

File tree

8 files changed

+998
-1477
lines changed

8 files changed

+998
-1477
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);

0 commit comments

Comments
 (0)