Skip to content
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

Reimplement a synchronous method - Fixes #13 #14

Merged
merged 2 commits into from
Sep 30, 2017
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
58 changes: 37 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,45 @@ const defaults = {
v4: '127.0.0.1'
};

function internalIp(family) {
return defaultGateway[family]().then(result => {
const interfaces = os.networkInterfaces();
const gatewayIp = ipaddr.parse(result.gateway);
let ret;

// Look for the matching interface in all local interfaces
Object.keys(interfaces).some(name => {
return interfaces[name].some(addr => {
const prefix = ipaddr.parse(addr.netmask).prefixLengthFromSubnetMask();
const net = ipaddr.parseCIDR(`${addr.address}/${prefix}`);

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

return Boolean(ret);
});
function findIp(gateway, family) {
const interfaces = os.networkInterfaces();
const gatewayIp = ipaddr.parse(gateway);
let ret;

// Look for the matching interface in all local interfaces
Object.keys(interfaces).some(name => {
return interfaces[name].some(addr => {
const prefix = ipaddr.parse(addr.netmask).prefixLengthFromSubnetMask();
const net = ipaddr.parseCIDR(`${addr.address}/${prefix}`);

if (net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

just noticed this: could you add a check for net[0] here, e.g. if (net[0] && net[0].kind()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is a direct copy paste from the code as it presently is in master. mentioning only to confirm that you want to piggy back on this PR for the change

Copy link
Collaborator

Choose a reason for hiding this comment

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

right, I can fix that later myself, no need to do it here.

ret = net[0].toString();
}

return Boolean(ret);
});
});

return ret ? ret : defaults[family];
}

return ret ? ret : defaults[family];
function promise(family) {
return defaultGateway[family]().then(result => {
return findIp(result.gateway, family);
}).catch(() => defaults[family]);
}

module.exports.v6 = () => internalIp('v6');
module.exports.v4 = () => internalIp('v4');
function sync(family) {
try {
const result = defaultGateway[family].sync();
return findIp(result.gateway, family);
} catch (err) {
return defaults[family];
}
}

module.exports.v6 = () => promise('v6');
module.exports.v4 = () => promise('v4');

module.exports.v6.sync = () => sync('v6');
module.exports.v4.sync = () => sync('v4');
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"gateway"
],
"dependencies": {
"default-gateway": "^2.2.2",
"default-gateway": "^2.6.0",
"ipaddr.js": "^1.5.2"
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ test('IPv6', async t => {
test('IPv4', async t => {
t.true(isIPv4(await m.v4()));
});

test('synchronous IPv6', t => {
t.true(isIPv6(m.v6.sync()));
});

test('synchronous IPv4', t => {
t.true(isIPv4(m.v4.sync()));
});