Skip to content

Commit

Permalink
Use os.networkInterfaces() to detect network address (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
saintwinkle authored and rauchg committed Dec 3, 2018
1 parent f799412 commit b7d9de3
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions bin/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const path = require('path');
const fs = require('fs');
const {promisify} = require('util');
const {parse} = require('url');
const dns = require('dns');
const os = require('os');

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

const readFile = promisify(fs.readFile);
const lookup = promisify(dns.lookup);
const compressionHandler = promisify(compression());

const interfaces = os.networkInterfaces();

const warning = (message) => chalk`{yellow WARNING:} ${message}`;
const info = (message) => chalk`{magenta INFO:} ${message}`;
const error = (message) => chalk`{red ERROR:} ${message}`;
Expand Down Expand Up @@ -154,6 +154,17 @@ const registerShutdown = (fn) => {
process.on('exit', wrapper);
};

const getNetworkAddress = () => {
for (const name of Object.keys(interfaces)) {
for (const interface of interfaces[name]) {
const {address, family, internal} = interface;
if (family === 'IPv4' && !internal) {
return address;
}
}
}
};

const startEndpoint = (endpoint, config, args, previous) => {
const {isTTY} = process.stdout;
const clipboard = args['--no-clipboard'] !== true;
Expand Down Expand Up @@ -188,14 +199,10 @@ const startEndpoint = (endpoint, config, args, previous) => {
localAddress = details;
} else if (typeof details === 'object' && details.port) {
const address = details.address === '::' ? 'localhost' : details.address;
const ip = getNetworkAddress();

localAddress = `http://${address}:${details.port}`;
try {
const {address: ip} = await lookup(os.hostname());
networkAddress = `http://${ip}:${details.port}`;
} catch (err) {
console.error(error(`DNS lookup failed: ${err.message}`));
}
networkAddress = `http://${ip}:${details.port}`;
}

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

0 comments on commit b7d9de3

Please sign in to comment.