network-address
Advanced tools
Comparing version 1.0.0 to 1.1.0
14
cli.js
#!/usr/bin/env node | ||
if (process.argv.indexOf('-6') > -1) console.log(require('./index').ipv6()) | ||
else console.log(require('./index')()) | ||
var argv = process.argv.slice(2) | ||
var ipv6 | ||
var iface | ||
for (var i = 0; i < argv.length; i++) { | ||
if (argv[i] === '-6') ipv6 = true | ||
else if (argv[i][0] !== '-') iface = argv[i] | ||
} | ||
if (ipv6) console.log(require('./index').ipv6(iface)) | ||
else console.log(require('./index')(iface)) |
42
index.js
@@ -1,25 +0,33 @@ | ||
var os = require('os'); | ||
var os = require('os') | ||
var ipv4 = function() { | ||
var interfaces = os.networkInterfaces() | ||
for (var i in interfaces) { | ||
for (var j = interfaces[i].length-1; j >= 0; j--) { | ||
var face = interfaces[i][j] | ||
if (!face.internal && face.family === 'IPv4') return face.address | ||
} | ||
} | ||
return '127.0.0.1' | ||
} | ||
var ipv6 = function() { | ||
var interfaces = os.networkInterfaces() | ||
function pickInterface (interfaces, family) { | ||
for (var i in interfaces) { | ||
for (var j = interfaces[i].length-1; j >= 0; j--) { | ||
for (var j = interfaces[i].length - 1; j >= 0; j--) { | ||
var face = interfaces[i][j] | ||
if (!face.internal && face.family === 'IPv6') return face.address | ||
if (!face.internal && face.family === family) return face.address | ||
} | ||
} | ||
return '::1' | ||
return family === 'IPv4' ? '127.0.0.1' : '::1' | ||
} | ||
function reduceInterfaces (interfaces, iface) { | ||
var ifaces = {} | ||
for (var i in interfaces) { | ||
if (i === iface) ifaces[i] = interfaces[i] | ||
} | ||
return ifaces | ||
} | ||
function ipv4 (iface) { | ||
var interfaces = os.networkInterfaces() | ||
if (iface) interfaces = reduceInterfaces(interfaces, iface) | ||
return pickInterface(interfaces, 'IPv4') | ||
} | ||
function ipv6 (iface) { | ||
var interfaces = os.networkInterfaces() | ||
if (iface) interfaces = reduceInterfaces(interfaces, iface) | ||
return pickInterface(interfaces, 'IPv6') | ||
} | ||
ipv4.ipv4 = ipv4 | ||
@@ -26,0 +34,0 @@ ipv4.ipv6 = ipv6 |
{ | ||
"name": "network-address", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"scripts": { | ||
"test": "tape test.js" | ||
"test": "standard && tape test.js" | ||
}, | ||
@@ -22,2 +22,3 @@ "dependencies": {}, | ||
"devDependencies": { | ||
"standard": "^6.0.7", | ||
"tape": "^3.0.3" | ||
@@ -24,0 +25,0 @@ }, |
13
test.js
var tape = require('tape') | ||
var address = require('./') | ||
tape('ipv4', function(t) { | ||
tape('ipv4', function (t) { | ||
t.ok(/^\d+\.\d+\.\d+\.\d+$/.test(address()), 'looks like a ipv4') | ||
@@ -9,3 +9,3 @@ t.end() | ||
tape('ipv4 #2', function(t) { | ||
tape('ipv4 #2', function (t) { | ||
t.ok(/^\d+\.\d+\.\d+\.\d+$/.test(address.ipv4()), 'looks like a ipv4') | ||
@@ -15,5 +15,10 @@ t.end() | ||
tape('ipv6', function(t) { | ||
tape('ipv4 #3', function (t) { | ||
t.ok(/^\d+\.\d+\.\d+\.\d+$/.test(address.ipv4('lo')), 'looks like a ipv4') | ||
t.end() | ||
}) | ||
tape('ipv6', function (t) { | ||
t.ok(/([0-f]*:)?([0-f]*:)?([0-f]*:)?([0-f]*:)?$/.test(address.ipv6()), 'looks like a ipv6') | ||
t.end() | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4214
58
2