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 1 commit
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
54 changes: 33 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,41 @@ 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 match(gateway, family) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe findIp might be a better name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

from line 16 // Look for the matching interface in all local interfaces - that's why I chose the name. doesn't much matter to me what you think it should be, happy to change it, but I thought it was appropriate given that comment

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I still think findIp is more appropriate. The matching is only part of the process.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

aye, will change

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 match(result.gateway, family);
}).catch(() => defaults[family]);
}

module.exports.v6 = () => internalIp('v6');
module.exports.v4 = () => internalIp('v4');
function sync(family) {
const result = defaultGateway[family].sync();
Copy link
Collaborator

@silverwind silverwind Sep 28, 2017

Choose a reason for hiding this comment

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

you need to wrap this line in a try...catch and return the defaults in case of error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do

return match(result.gateway, 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()));
});