Skip to content

refactor: remove internal-ip dependency #4023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const url = require("url");
const util = require("util");
const fs = require("graceful-fs");
const ipaddr = require("ipaddr.js");
const internalIp = require("internal-ip");
const defaultGateway = require("default-gateway");
const express = require("express");
const { validate } = require("schema-utils");
const schema = require("./options.json");
Expand Down Expand Up @@ -62,13 +62,54 @@ class Server {
return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(URL);
}

static findIp(gateway) {
const gatewayIp = ipaddr.parse(gateway);

// Look for the matching interface in all local interfaces.
for (const addresses of Object.values(os.networkInterfaces())) {
for (const { cidr } of addresses) {
const net = ipaddr.parseCIDR(cidr);

if (
net[0] &&
net[0].kind() === gatewayIp.kind() &&
gatewayIp.match(net)
) {
return net[0].toString();
}
}
}
}

static async internalIP(family) {
try {
const { gateway } = await defaultGateway[family]();
return Server.findIp(gateway);
} catch {
// ignore
}
}

static internalIPSync(family) {
try {
const { gateway } = defaultGateway[family].sync();
return Server.findIp(gateway);
} catch {
// ignore
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use normal name for async (i.e. internalIP) and *Sync for sync (i.e. internalIPSync)


static async getHostname(hostname) {
if (hostname === "local-ip") {
return (await internalIp.v4()) || (await internalIp.v6()) || "0.0.0.0";
return (
(await Server.internalIP("v4")) ||
(await Server.internalIP("v6")) ||
"0.0.0.0"
);
} else if (hostname === "local-ipv4") {
return (await internalIp.v4()) || "0.0.0.0";
return (await Server.internalIP("v4")) || "0.0.0.0";
} else if (hostname === "local-ipv6") {
return (await internalIp.v6()) || "::";
return (await Server.internalIP("v6")) || "::";
}

return hostname;
Expand Down Expand Up @@ -1812,13 +1853,13 @@ class Server {
if (parsedIP.range() === "unspecified") {
localhost = prettyPrintURL("localhost");

const networkIPv4 = internalIp.v4.sync();
const networkIPv4 = Server.internalIPSync("v4");

if (networkIPv4) {
networkUrlIPv4 = prettyPrintURL(networkIPv4);
}

const networkIPv6 = internalIp.v6.sync();
const networkIPv6 = Server.internalIPSync("v6");

if (networkIPv6) {
networkUrlIPv6 = prettyPrintURL(networkIPv6);
Expand Down
125 changes: 4 additions & 121 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
"colorette": "^2.0.10",
"compression": "^1.7.4",
"connect-history-api-fallback": "^1.6.0",
"default-gateway": "^6.0.3",
"del": "^6.0.0",
"express": "^4.17.1",
"graceful-fs": "^4.2.6",
"html-entities": "^2.3.2",
"http-proxy-middleware": "^2.0.0",
"internal-ip": "^6.2.0",
"ipaddr.js": "^2.0.1",
"open": "^8.0.9",
"p-retry": "^4.5.0",
Expand Down
6 changes: 3 additions & 3 deletions test/cli/host-option.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use strict";

const internalIp = require("internal-ip");
const { testBin, normalizeStderr } = require("../helpers/test-bin");
const port = require("../ports-map")["cli-host"];
const Server = require("../../lib/Server");

const localIPv4 = internalIp.v4.sync();
const localIPv6 = internalIp.v6.sync();
const localIPv4 = Server.internalIPSync("v4");
const localIPv6 = Server.internalIPSync("v6");

describe('"host" CLI option', () => {
it('should work using "--host 0.0.0.0" (IPv4)', async () => {
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/allowed-hosts.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

const express = require("express");
const internalIp = require("internal-ip");
const webpack = require("webpack");
const { createProxyMiddleware } = require("http-proxy-middleware");
const Server = require("../../lib/Server");
Expand Down Expand Up @@ -1132,7 +1131,7 @@ describe("allowed hosts", () => {
});

it("should always allow value from the `host` options if options.allowedHosts is auto", async () => {
const networkIP = internalIp.v4.sync();
const networkIP = Server.internalIPSync("v4");
const options = {
host: networkIP,
allowedHosts: "auto",
Expand Down
5 changes: 2 additions & 3 deletions test/e2e/host.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"use strict";

const webpack = require("webpack");
const internalIp = require("internal-ip");
const Server = require("../../lib/Server");
const config = require("../fixtures/client-config/webpack.config");
const runBrowser = require("../helpers/run-browser");
const port = require("../ports-map").host;

const ipv4 = internalIp.v4.sync();
const ipv6 = internalIp.v6.sync();
const ipv4 = Server.internalIPSync("v4");
const ipv6 = Server.internalIPSync("v6");
// macos requires root for using ip v6
const isMacOS = process.platform === "darwin";

Expand Down
Loading