Comparing version 0.3.3 to 1.0.0
126
lib/ip.js
@@ -1,5 +0,7 @@ | ||
var ip = exports, | ||
Buffer = require('buffer').Buffer, | ||
os = require('os'); | ||
'use strict'; | ||
var ip = exports; | ||
var Buffer = require('buffer').Buffer; | ||
var os = require('os'); | ||
ip.toBuffer = function toBuffer(ip, buff, offset) { | ||
@@ -10,2 +12,6 @@ offset = ~~offset; | ||
if (/^::ffff:(\d{1,3}\.){3,3}\d{1,3}$/.test(ip)) { | ||
ip = ip.replace(/^::ffff:/, ''); | ||
} | ||
if (/^(\d{1,3}\.){3,3}\d{1,3}$/.test(ip)) { | ||
@@ -17,5 +23,5 @@ result = buff || new Buffer(offset + 4); | ||
} else if (/^[a-f0-9:]+$/.test(ip)) { | ||
var s = ip.split(/::/g, 2), | ||
head = (s[0] || '').split(/:/g, 8), | ||
tail = (s[1] || '').split(/:/g, 8); | ||
var s = ip.split(/::/g, 2); | ||
var head = (s[0] || '').split(/:/g, 8); | ||
var tail = (s[1] || '').split(/:/g, 8); | ||
@@ -70,2 +76,6 @@ if (tail.length === 0) { | ||
function _normalizeFamily(family) { | ||
return family ? family.toLowerCase() : 'ipv4'; | ||
} | ||
ip.fromPrefixLen = function fromPrefixLen(prefixlen, family) { | ||
@@ -134,10 +144,10 @@ if (prefixlen > 32) { | ||
if (cidrParts.length != 2) | ||
var addr = cidrParts[0]; | ||
if (cidrParts.length !== 2) | ||
throw new Error('invalid CIDR subnet: ' + addr); | ||
var addr = cidrParts[0]; | ||
var mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10)); | ||
return ip.mask(addr, mask); | ||
} | ||
}; | ||
@@ -152,3 +162,3 @@ ip.subnet = function subnet(addr, mask) { | ||
for (var i = 0; i < maskBuffer.length; i++) { | ||
if (maskBuffer[i] == 0xff) { | ||
if (maskBuffer[i] === 0xff) { | ||
maskLength += 8; | ||
@@ -181,3 +191,3 @@ } else { | ||
}; | ||
} | ||
}; | ||
@@ -187,10 +197,10 @@ ip.cidrSubnet = function cidrSubnet(cidrString) { | ||
var addr = cidrParts[0]; | ||
if (cidrParts.length !== 2) | ||
throw new Error('invalid CIDR subnet: ' + addr); | ||
var addr = cidrParts[0]; | ||
var mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10)); | ||
return ip.subnet(addr, mask); | ||
} | ||
}; | ||
@@ -210,3 +220,3 @@ ip.not = function not(addr) { | ||
// same protocol | ||
if (a.length == b.length) { | ||
if (a.length === b.length) { | ||
for (var i = 0; i < a.length; ++i) { | ||
@@ -270,10 +280,11 @@ a[i] |= b[i]; | ||
ip.isPrivate = function isPrivate(addr) { | ||
return addr.match(/^10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/) != null || | ||
addr.match(/^192\.168\.([0-9]{1,3})\.([0-9]{1,3})/) != null || | ||
addr.match( | ||
/^172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})/) != null || | ||
addr.match(/^127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/) != null || | ||
addr.match(/^169\.254\.([0-9]{1,3})\.([0-9]{1,3})/) != null || | ||
addr.match(/^fc00:/) != null || addr.match(/^fe80:/) != null || | ||
addr.match(/^::1$/) != null || addr.match(/^::$/) != null; | ||
return /^10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) || | ||
/^192\.168\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) || | ||
/^172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) || | ||
/^127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) || | ||
/^169\.254\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) || | ||
/^fc00:/.test(addr) || | ||
/^fe80:/.test(addr) || | ||
/^::1$/.test(addr) || | ||
/^::$/.test(addr); | ||
}; | ||
@@ -283,9 +294,9 @@ | ||
return !ip.isPrivate(addr); | ||
} | ||
}; | ||
ip.isLoopback = function isLoopback(addr) { | ||
return /^127\.0\.0\.1$/.test(addr) | ||
|| /^fe80::1$/.test(addr) | ||
|| /^::1$/.test(addr) | ||
|| /^::$/.test(addr); | ||
return /^127\.0\.0\.1$/.test(addr) || | ||
/^fe80::1$/.test(addr) || | ||
/^::1$/.test(addr) || | ||
/^::$/.test(addr); | ||
}; | ||
@@ -303,5 +314,3 @@ | ||
return family === 'ipv4' | ||
? '127.0.0.1' | ||
: 'fe80::1'; | ||
return family === 'ipv4' ? '127.0.0.1' : 'fe80::1'; | ||
}; | ||
@@ -325,4 +334,4 @@ | ||
ip.address = function address(name, family) { | ||
var interfaces = os.networkInterfaces(), | ||
all; | ||
var interfaces = os.networkInterfaces(); | ||
var all; | ||
@@ -338,7 +347,10 @@ // | ||
// | ||
if (name && !~['public', 'private'].indexOf(name)) { | ||
return interfaces[name].filter(function (details) { | ||
details.family = details.family.toLowerCase(); | ||
return details.family === family; | ||
})[0].address; | ||
if (name && name !== 'private' && name !== 'public') { | ||
var res = interfaces[name].filter(function(details) { | ||
var itemFamily = details.family.toLowerCase(); | ||
return itemFamily === family; | ||
}); | ||
if (res.length === 0) | ||
return undefined; | ||
return res[0].address; | ||
} | ||
@@ -355,40 +367,30 @@ | ||
return false; | ||
} | ||
else if (!name) { | ||
} else if (!name) { | ||
return true; | ||
} | ||
return name === 'public' | ||
? !ip.isPrivate(details.address) | ||
: ip.isPrivate(details.address) | ||
return name === 'public' ? !ip.isPrivate(details.address) : | ||
ip.isPrivate(details.address); | ||
}); | ||
return addresses.length | ||
? addresses[0].address | ||
: undefined; | ||
return addresses.length ? addresses[0].address : undefined; | ||
}).filter(Boolean); | ||
return !all.length | ||
? ip.loopback(family) | ||
: all[0]; | ||
return !all.length ? ip.loopback(family) : all[0]; | ||
}; | ||
ip.toLong = function toInt(ip){ | ||
var ipl=0; | ||
ip.split('.').forEach(function( octet ) { | ||
ipl<<=8; | ||
ipl+=parseInt(octet); | ||
ip.toLong = function toInt(ip) { | ||
var ipl = 0; | ||
ip.split('.').forEach(function(octet) { | ||
ipl <<= 8; | ||
ipl += parseInt(octet); | ||
}); | ||
return(ipl >>>0); | ||
return(ipl >>> 0); | ||
}; | ||
ip.fromLong = function fromInt(ipl){ | ||
return ( (ipl>>>24) +'.' + | ||
(ipl>>16 & 255) +'.' + | ||
(ipl>>8 & 255) +'.' + | ||
ip.fromLong = function fromInt(ipl) { | ||
return ((ipl >>> 24) + '.' + | ||
(ipl >> 16 & 255) + '.' + | ||
(ipl >> 8 & 255) + '.' + | ||
(ipl & 255) ); | ||
}; | ||
function _normalizeFamily(family) { | ||
return family ? family.toLowerCase() : 'ipv4'; | ||
} |
{ | ||
"name": "ip", | ||
"version": "0.3.3", | ||
"version": "1.0.0", | ||
"author": "Fedor Indutny <fedor@indutny.com>", | ||
@@ -12,8 +12,10 @@ "homepage": "https://github.com/indutny/node-ip", | ||
"devDependencies": { | ||
"jscs": "^2.1.1", | ||
"jshint": "^2.8.0", | ||
"mocha": "~1.3.2" | ||
}, | ||
"scripts": { | ||
"test": "mocha --reporter spec test/*-test.js" | ||
"test": "jscs lib/*.js test/*.js && jshint lib/*.js && mocha --reporter spec test/*-test.js" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -7,3 +7,3 @@ # IP | ||
``` | ||
```js | ||
var ip = require('ip'); | ||
@@ -10,0 +10,0 @@ |
@@ -1,6 +0,8 @@ | ||
var ip = require('..'), | ||
assert = require('assert'), | ||
net = require('net'), | ||
os = require('os'); | ||
'use strict'; | ||
var ip = require('..'); | ||
var assert = require('assert'); | ||
var net = require('net'); | ||
var os = require('os'); | ||
describe('IP library for node.js', function() { | ||
@@ -41,2 +43,8 @@ describe('toBuffer()/toString() methods', function() { | ||
}); | ||
it('should convert to buffer IPv6 mapped IPv4 address', function() { | ||
var buf = ip.toBuffer('::ffff:127.0.0.1'); | ||
assert.equal(buf.toString('hex'), '7f000001'); | ||
assert.equal(ip.toString(buf), '127.0.0.1'); | ||
}); | ||
}); | ||
@@ -202,2 +210,4 @@ | ||
assert(!ip.isEqual('127.0.0.1', '::ffaf:7f00:1')); | ||
assert(ip.isEqual('::ffff:127.0.0.1', '::ffff:127.0.0.1')); | ||
assert(ip.isEqual('::ffff:127.0.0.1', '127.0.0.1')); | ||
}); | ||
@@ -254,5 +264,5 @@ }); | ||
describe('loopback() method', function () { | ||
describe('undefined', function () { | ||
it('should respond with 127.0.0.1', function () { | ||
describe('loopback() method', function() { | ||
describe('undefined', function() { | ||
it('should respond with 127.0.0.1', function() { | ||
assert.equal(ip.loopback(), '127.0.0.1') | ||
@@ -262,4 +272,4 @@ }); | ||
describe('ipv4', function () { | ||
it('should respond with 127.0.0.1', function () { | ||
describe('ipv4', function() { | ||
it('should respond with 127.0.0.1', function() { | ||
assert.equal(ip.loopback('ipv4'), '127.0.0.1') | ||
@@ -269,4 +279,4 @@ }); | ||
describe('ipv6', function () { | ||
it('should respond with fe80::1', function () { | ||
describe('ipv6', function() { | ||
it('should respond with fe80::1', function() { | ||
assert.equal(ip.loopback('ipv6'), 'fe80::1') | ||
@@ -277,5 +287,5 @@ }); | ||
describe('isLoopback() method', function () { | ||
describe('127.0.0.1', function () { | ||
it('should respond with true', function () { | ||
describe('isLoopback() method', function() { | ||
describe('127.0.0.1', function() { | ||
it('should respond with true', function() { | ||
assert.ok(ip.isLoopback('127.0.0.1')) | ||
@@ -285,4 +295,4 @@ }); | ||
describe('8.8.8.8', function () { | ||
it('should respond with false', function () { | ||
describe('8.8.8.8', function() { | ||
it('should respond with false', function() { | ||
assert.equal(ip.isLoopback('8.8.8.8'), false); | ||
@@ -292,4 +302,4 @@ }); | ||
describe('fe80::1', function () { | ||
it('should respond with true', function () { | ||
describe('fe80::1', function() { | ||
it('should respond with true', function() { | ||
assert.ok(ip.isLoopback('fe80::1')) | ||
@@ -299,4 +309,4 @@ }); | ||
describe('::1', function () { | ||
it('should respond with true', function () { | ||
describe('::1', function() { | ||
it('should respond with true', function() { | ||
assert.ok(ip.isLoopback('::1')) | ||
@@ -306,4 +316,4 @@ }); | ||
describe('::', function () { | ||
it('should respond with true', function () { | ||
describe('::', function() { | ||
it('should respond with true', function() { | ||
assert.ok(ip.isLoopback('::')) | ||
@@ -314,5 +324,5 @@ }); | ||
describe('address() method', function () { | ||
describe('undefined', function () { | ||
it('should respond with a private ip', function () { | ||
describe('address() method', function() { | ||
describe('undefined', function() { | ||
it('should respond with a private ip', function() { | ||
assert.ok(ip.isPrivate(ip.address())); | ||
@@ -322,6 +332,6 @@ }); | ||
describe('private', function () { | ||
[undefined, 'ipv4', 'ipv6'].forEach(function (family) { | ||
describe(family, function () { | ||
it('should respond with a private ip', function () { | ||
describe('private', function() { | ||
[ undefined, 'ipv4', 'ipv6' ].forEach(function(family) { | ||
describe(family, function() { | ||
it('should respond with a private ip', function() { | ||
assert.ok(ip.isPrivate(ip.address('private', family))); | ||
@@ -335,8 +345,9 @@ }); | ||
Object.keys(interfaces).forEach(function (nic) { | ||
describe(nic, function () { | ||
[undefined, 'ipv4'].forEach(function (family) { | ||
describe(family, function () { | ||
it('should respond with an ipv4 address', function () { | ||
assert.ok(net.isIPv4(ip.address(nic, family))); | ||
Object.keys(interfaces).forEach(function(nic) { | ||
describe(nic, function() { | ||
[ undefined, 'ipv4' ].forEach(function(family) { | ||
describe(family, function() { | ||
it('should respond with an ipv4 address', function() { | ||
var addr = ip.address(nic, family); | ||
assert.ok(!addr || net.isIPv4(addr)); | ||
}); | ||
@@ -346,5 +357,6 @@ }); | ||
describe('ipv6', function () { | ||
it('should respond with an ipv6 address', function () { | ||
assert.ok(net.isIPv6(ip.address(nic, 'ipv6'))); | ||
describe('ipv6', function() { | ||
it('should respond with an ipv6 address', function() { | ||
var addr = ip.address(nic, 'ipv6'); | ||
assert.ok(!addr || net.isIPv6(addr)); | ||
}); | ||
@@ -356,4 +368,4 @@ }) | ||
describe('toLong() method', function(){ | ||
it('should respond with a int', function(){ | ||
describe('toLong() method', function() { | ||
it('should respond with a int', function() { | ||
assert.equal(ip.toLong('127.0.0.1'), 2130706433); | ||
@@ -364,4 +376,4 @@ assert.equal(ip.toLong('255.255.255.255'), 4294967295); | ||
describe('fromLong() method', function(){ | ||
it('should repond with ipv4 address', function(){ | ||
describe('fromLong() method', function() { | ||
it('should repond with ipv4 address', function() { | ||
assert.equal(ip.fromLong(2130706433), '127.0.0.1'); | ||
@@ -368,0 +380,0 @@ assert.equal(ip.fromLong(4294967295), '255.255.255.255'); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
32742
7
626
0
3