Skip to content

Commit 5927a72

Browse files
authored
refactor: remove internal-ip dependency (#4023)
1 parent aaa42f0 commit 5927a72

File tree

8 files changed

+66
-146
lines changed

8 files changed

+66
-146
lines changed

lib/Server.js

Lines changed: 47 additions & 6 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");
@@ -62,13 +62,54 @@ class Server {
6262
return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(URL);
6363
}
6464

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

74115
return hostname;
@@ -1814,13 +1855,13 @@ class Server {
18141855
if (parsedIP.range() === "unspecified") {
18151856
localhost = prettyPrintURL("localhost");
18161857

1817-
const networkIPv4 = internalIp.v4.sync();
1858+
const networkIPv4 = Server.internalIPSync("v4");
18181859

18191860
if (networkIPv4) {
18201861
networkUrlIPv4 = prettyPrintURL(networkIPv4);
18211862
}
18221863

1823-
const networkIPv6 = internalIp.v6.sync();
1864+
const networkIPv6 = Server.internalIPSync("v6");
18241865

18251866
if (networkIPv6) {
18261867
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");
43
const { testBin, normalizeStderr } = require("../helpers/test-bin");
54
const port = require("../ports-map")["cli-host"];
5+
const Server = require("../../lib/Server");
66

7-
const localIPv4 = internalIp.v4.sync();
8-
const localIPv6 = internalIp.v6.sync();
7+
const localIPv4 = Server.internalIPSync("v4");
8+
const localIPv6 = Server.internalIPSync("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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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");
76
const Server = require("../../lib/Server");
@@ -1211,7 +1210,7 @@ describe("allowed hosts", () => {
12111210
});
12121211

12131212
it("should always allow value from the `host` options if options.allowedHosts is auto", async () => {
1214-
const networkIP = internalIp.v4.sync();
1213+
const networkIP = Server.internalIPSync("v4");
12151214
const options = {
12161215
host: networkIP,
12171216
allowedHosts: "auto",

test/e2e/host.test.js

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

33
const webpack = require("webpack");
4-
const internalIp = require("internal-ip");
54
const Server = require("../../lib/Server");
65
const config = require("../fixtures/client-config/webpack.config");
76
const runBrowser = require("../helpers/run-browser");
87
const port = require("../ports-map").host;
98

10-
const ipv4 = internalIp.v4.sync();
11-
const ipv6 = internalIp.v6.sync();
9+
const ipv4 = Server.internalIPSync("v4");
10+
const ipv6 = Server.internalIPSync("v6");
1211
// macos requires root for using ip v6
1312
const isMacOS = process.platform === "darwin";
1413

0 commit comments

Comments
 (0)