Skip to content

Commit b7d9de3

Browse files
saintwinklerauchg
authored andcommitted
Use os.networkInterfaces() to detect network address (#492)
1 parent f799412 commit b7d9de3

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

bin/serve.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const path = require('path');
66
const fs = require('fs');
77
const {promisify} = require('util');
88
const {parse} = require('url');
9-
const dns = require('dns');
109
const os = require('os');
1110

1211
// Packages
@@ -24,9 +23,10 @@ const compression = require('compression');
2423
const pkg = require('../package');
2524

2625
const readFile = promisify(fs.readFile);
27-
const lookup = promisify(dns.lookup);
2826
const compressionHandler = promisify(compression());
2927

28+
const interfaces = os.networkInterfaces();
29+
3030
const warning = (message) => chalk`{yellow WARNING:} ${message}`;
3131
const info = (message) => chalk`{magenta INFO:} ${message}`;
3232
const error = (message) => chalk`{red ERROR:} ${message}`;
@@ -154,6 +154,17 @@ const registerShutdown = (fn) => {
154154
process.on('exit', wrapper);
155155
};
156156

157+
const getNetworkAddress = () => {
158+
for (const name of Object.keys(interfaces)) {
159+
for (const interface of interfaces[name]) {
160+
const {address, family, internal} = interface;
161+
if (family === 'IPv4' && !internal) {
162+
return address;
163+
}
164+
}
165+
}
166+
};
167+
157168
const startEndpoint = (endpoint, config, args, previous) => {
158169
const {isTTY} = process.stdout;
159170
const clipboard = args['--no-clipboard'] !== true;
@@ -188,14 +199,10 @@ const startEndpoint = (endpoint, config, args, previous) => {
188199
localAddress = details;
189200
} else if (typeof details === 'object' && details.port) {
190201
const address = details.address === '::' ? 'localhost' : details.address;
202+
const ip = getNetworkAddress();
191203

192204
localAddress = `http://${address}:${details.port}`;
193-
try {
194-
const {address: ip} = await lookup(os.hostname());
195-
networkAddress = `http://${ip}:${details.port}`;
196-
} catch (err) {
197-
console.error(error(`DNS lookup failed: ${err.message}`));
198-
}
205+
networkAddress = `http://${ip}:${details.port}`;
199206
}
200207

201208
if (isTTY && process.env.NODE_ENV !== 'production') {

0 commit comments

Comments
 (0)