Skip to content

Commit 2f958a1

Browse files
committed
refactor: remove internal-ip dependency
1 parent d576068 commit 2f958a1

File tree

9 files changed

+117
-147
lines changed

9 files changed

+117
-147
lines changed

lib/Server.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const url = require("url");
66
const util = require("util");
77
const fs = require("graceful-fs");
88
const ipaddr = require("ipaddr.js");
9-
const internalIp = require("internal-ip");
9+
const defaultGateway = require("default-gateway");
1010
const express = require("express");
1111
const { validate } = require("schema-utils");
1212
const schema = require("./options.json");
@@ -50,7 +50,6 @@ class Server {
5050
};
5151
}
5252

53-
// eslint-disable-next-line class-methods-use-this
5453
static isAbsoluteURL(URL) {
5554
// Don't match Windows paths `c:\`
5655
if (/^[a-zA-Z]:\\/.test(URL)) {
@@ -62,13 +61,54 @@ class Server {
6261
return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(URL);
6362
}
6463

64+
static findIp (gateway) {
65+
const gatewayIp = ipaddr.parse(gateway);
66+
67+
// Look for the matching interface in all local interfaces.
68+
for (const addresses of Object.values(os.networkInterfaces())) {
69+
for (const { cidr } of addresses) {
70+
const net = ipaddr.parseCIDR(cidr);
71+
72+
if (
73+
net[0] &&
74+
net[0].kind() === gatewayIp.kind() &&
75+
gatewayIp.match(net)
76+
) {
77+
return net[0].toString();
78+
}
79+
}
80+
}
81+
};
82+
83+
static async asyncInternalIp (family) {
84+
try {
85+
const { gateway } = await defaultGateway[family]();
86+
return Server.findIp(gateway);
87+
} catch {
88+
// ignore
89+
}
90+
};
91+
92+
static syncInternalIp (family) {
93+
try {
94+
const { gateway } = defaultGateway[family].sync();
95+
return Server.findIp(gateway);
96+
} catch {
97+
// ignore
98+
}
99+
};
100+
65101
static async getHostname(hostname) {
66102
if (hostname === "local-ip") {
67-
return (await internalIp.v4()) || (await internalIp.v6()) || "0.0.0.0";
103+
return (
104+
(await Server.asyncInternalIp("v4")) ||
105+
(await Server.asyncInternalIp("v6")) ||
106+
"0.0.0.0"
107+
);
68108
} else if (hostname === "local-ipv4") {
69-
return (await internalIp.v4()) || "0.0.0.0";
109+
return (await Server.asyncInternalIp("v4")) || "0.0.0.0";
70110
} else if (hostname === "local-ipv6") {
71-
return (await internalIp.v6()) || "::";
111+
return (await Server.asyncInternalIp("v6")) || "::";
72112
}
73113

74114
return hostname;
@@ -1812,13 +1852,13 @@ class Server {
18121852
if (parsedIP.range() === "unspecified") {
18131853
localhost = prettyPrintURL("localhost");
18141854

1815-
const networkIPv4 = internalIp.v4.sync();
1855+
const networkIPv4 = Server.syncInternalIp("v4");
18161856

18171857
if (networkIPv4) {
18181858
networkUrlIPv4 = prettyPrintURL(networkIPv4);
18191859
}
18201860

1821-
const networkIPv6 = internalIp.v6.sync();
1861+
const networkIPv6 = Server.syncInternalIp("v6");
18221862

18231863
if (networkIPv6) {
18241864
networkUrlIPv6 = prettyPrintURL(networkIPv6);

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838
"colorette": "^2.0.10",
3939
"compression": "^1.7.4",
4040
"connect-history-api-fallback": "^1.6.0",
41+
"default-gateway": "^6.0.3",
4142
"del": "^6.0.0",
4243
"express": "^4.17.1",
4344
"graceful-fs": "^4.2.6",
4445
"html-entities": "^2.3.2",
4546
"http-proxy-middleware": "^2.0.0",
46-
"internal-ip": "^6.2.0",
4747
"ipaddr.js": "^2.0.1",
4848
"open": "^8.0.9",
4949
"p-retry": "^4.5.0",

test/cli/host-option.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"use strict";
22

3-
const internalIp = require("internal-ip");
3+
const { syncInternalIp } = require("../helpers/internal-ip");
44
const { testBin, normalizeStderr } = require("../helpers/test-bin");
55
const port = require("../ports-map")["cli-host"];
66

7-
const localIPv4 = internalIp.v4.sync();
8-
const localIPv6 = internalIp.v6.sync();
7+
const localIPv4 = syncInternalIp("v4");
8+
const localIPv6 = syncInternalIp("v6");
99

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

test/e2e/allowed-hosts.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22

33
const express = require("express");
4-
const internalIp = require("internal-ip");
54
const webpack = require("webpack");
65
const { createProxyMiddleware } = require("http-proxy-middleware");
6+
const { syncInternalIp } = require("../helpers/internal-ip");
77
const Server = require("../../lib/Server");
88
const config = require("../fixtures/client-config/webpack.config");
99
const runBrowser = require("../helpers/run-browser");
@@ -1132,7 +1132,7 @@ describe("allowed hosts", () => {
11321132
});
11331133

11341134
it("should always allow value from the `host` options if options.allowedHosts is auto", async () => {
1135-
const networkIP = internalIp.v4.sync();
1135+
const networkIP = syncInternalIp("v4");
11361136
const options = {
11371137
host: networkIP,
11381138
allowedHosts: "auto",

test/e2e/host.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"use strict";
22

33
const webpack = require("webpack");
4-
const internalIp = require("internal-ip");
4+
const { syncInternalIp } = require("../helpers/internal-ip");
55
const Server = require("../../lib/Server");
66
const config = require("../fixtures/client-config/webpack.config");
77
const runBrowser = require("../helpers/run-browser");
88
const port = require("../ports-map").host;
99

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

0 commit comments

Comments
 (0)