ip-address
Advanced tools
Comparing version 4.2.0 to 5.0.1
@@ -1,2 +0,6 @@ | ||
exports.v4 = require('./lib/ipv4.js').v4; | ||
exports.v6 = require('./lib/ipv6.js').v6; | ||
exports.Address4 = require('./lib/ipv4.js'); | ||
exports.Address6 = require('./lib/ipv6.js'); | ||
exports.v6 = { | ||
helpers: require('./lib/v6/helpers.js') | ||
}; |
168
lib/ipv4.js
@@ -5,16 +5,15 @@ 'use strict'; | ||
var common = require('./common.js'); | ||
var merge = require('lodash.merge'); | ||
var sprintf = require('sprintf').sprintf; | ||
var v4 = exports.v4 = {}; | ||
var constants = require('./v4/constants.js'); | ||
merge(v4, require('./v4/constants.js')); | ||
/* | ||
* Instantiates an IPv4 address | ||
/** | ||
* Represents an IPv4 address | ||
* @class Address4 | ||
* @param {string} address - An IPv4 address string | ||
*/ | ||
v4.Address = function (address) { | ||
function Address4(address) { | ||
this.valid = false; | ||
this.address = address; | ||
this.groups = v4.GROUPS; | ||
this.groups = constants.GROUPS; | ||
@@ -26,3 +25,3 @@ this.v4 = true; | ||
var subnet = v4.RE_SUBNET_STRING.exec(address); | ||
var subnet = constants.RE_SUBNET_STRING.exec(address); | ||
@@ -34,3 +33,3 @@ if (subnet) { | ||
if (this.subnetMask < 0 || this.subnetMask > v4.BITS) { | ||
if (this.subnetMask < 0 || this.subnetMask > constants.BITS) { | ||
this.valid = false; | ||
@@ -42,3 +41,3 @@ this.error = 'Invalid subnet mask.'; | ||
address = address.replace(v4.RE_SUBNET_STRING, ''); | ||
address = address.replace(constants.RE_SUBNET_STRING, ''); | ||
} | ||
@@ -49,3 +48,3 @@ | ||
this.parsedAddress = this.parse(address); | ||
}; | ||
} | ||
@@ -55,6 +54,6 @@ /* | ||
*/ | ||
v4.Address.prototype.parse = function (address) { | ||
Address4.prototype.parse = function (address) { | ||
var groups = address.split('.'); | ||
if (address.match(v4.RE_ADDRESS)) { | ||
if (address.match(constants.RE_ADDRESS)) { | ||
this.valid = true; | ||
@@ -68,13 +67,19 @@ } else { | ||
/* | ||
* Returns true if the address is valid | ||
/** | ||
* Return true if the address is valid | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {Boolean} | ||
*/ | ||
v4.Address.prototype.isValid = function () { | ||
Address4.prototype.isValid = function () { | ||
return this.valid; | ||
}; | ||
/* | ||
/** | ||
* Returns the correct form of an address | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v4.Address.prototype.correctForm = function () { | ||
Address4.prototype.correctForm = function () { | ||
return this.parsedAddress.map(function (part) { | ||
@@ -85,11 +90,18 @@ return parseInt(part, 10); | ||
/* | ||
/** | ||
* Returns true if the address is correct, false otherwise | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {Boolean} | ||
*/ | ||
v4.Address.prototype.isCorrect = common.isCorrect(v4.BITS); | ||
Address4.prototype.isCorrect = common.isCorrect(constants.BITS); | ||
/* | ||
/** | ||
* Converts a hex string to an IPv4 address object | ||
* @memberof Address4 | ||
* @static | ||
* @param {string} hex - a hex string to convert | ||
* @returns {Address4} | ||
*/ | ||
v4.Address.fromHex = function (hex) { | ||
Address4.fromHex = function (hex) { | ||
var padded = common.zeroPad(hex.replace(/:/g, ''), 8); | ||
@@ -105,16 +117,23 @@ var groups = []; | ||
return new v4.Address(groups.join('.')); | ||
return new Address4(groups.join('.')); | ||
}; | ||
/* | ||
/** | ||
* Converts an integer into a IPv4 address object | ||
* @memberof Address4 | ||
* @static | ||
* @param {integer} integer - a number to convert | ||
* @returns {Address4} | ||
*/ | ||
v4.Address.fromInteger = function (integer) { | ||
return v4.Address.fromHex(integer.toString(16)); | ||
Address4.fromInteger = function (integer) { | ||
return Address4.fromHex(integer.toString(16)); | ||
}; | ||
/* | ||
/** | ||
* Converts an IPv4 address object to a hex string | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v4.Address.prototype.toHex = function () { | ||
Address4.prototype.toHex = function () { | ||
return this.parsedAddress.map(function (part) { | ||
@@ -125,6 +144,9 @@ return sprintf('%02x', parseInt(part, 10)); | ||
/* | ||
/** | ||
* Converts an IPv4 address object to an array of bytes | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {Array} | ||
*/ | ||
v4.Address.prototype.toArray = function () { | ||
Address4.prototype.toArray = function () { | ||
return this.parsedAddress.map(function (part) { | ||
@@ -135,10 +157,13 @@ return parseInt(part, 10); | ||
/* | ||
/** | ||
* Converts an IPv4 address object to an IPv6 address group | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v4.Address.prototype.toV6Group = function () { | ||
Address4.prototype.toGroup6 = function () { | ||
var output = []; | ||
var i; | ||
for (i = 0; i < v4.GROUPS; i += 2) { | ||
for (i = 0; i < constants.GROUPS; i += 2) { | ||
var hex = sprintf('%02x%02x', | ||
@@ -154,6 +179,13 @@ parseInt(this.parsedAddress[i], 10), | ||
/* | ||
Address4.prototype.toV6Group = | ||
util.deprecate(Address4.prototype.toGroup6, | ||
'deprecated: `toV6Group` has been renamed to `toGroup6`'); | ||
/** | ||
* Returns the address as a BigInteger | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {BigInteger} | ||
*/ | ||
v4.Address.prototype.bigInteger = function () { | ||
Address4.prototype.bigInteger = function () { | ||
if (!this.valid) { | ||
@@ -168,36 +200,49 @@ return null; | ||
/* | ||
/** | ||
* The first address in the range given by this address' subnet. | ||
* Often referred to as the Network Address. | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {Address4} | ||
*/ | ||
v4.Address.prototype.startAddress = function () { | ||
Address4.prototype.startAddress = function () { | ||
var startAddress = new BigInteger(this.mask() + | ||
common.repeatString(0, v4.BITS - this.subnetMask), 2); | ||
common.repeatString(0, constants.BITS - this.subnetMask), 2); | ||
return v4.Address.fromBigInteger(startAddress); | ||
return Address4.fromBigInteger(startAddress); | ||
}; | ||
/* | ||
/** | ||
* The last address in the range given by this address' subnet | ||
* Often referred to as the Broadcast | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {Address4} | ||
*/ | ||
v4.Address.prototype.endAddress = function () { | ||
Address4.prototype.endAddress = function () { | ||
var endAddress = new BigInteger(this.mask() + | ||
common.repeatString(1, v4.BITS - this.subnetMask), 2); | ||
common.repeatString(1, constants.BITS - this.subnetMask), 2); | ||
return v4.Address.fromBigInteger(endAddress); | ||
return Address4.fromBigInteger(endAddress); | ||
}; | ||
/* | ||
/** | ||
* Converts a BigInteger to a v4 address object | ||
* @memberof Address4 | ||
* @static | ||
* @param {BigInteger} bigInteger - a BigInteger to convert | ||
* @returns {Address4} | ||
*/ | ||
v4.Address.fromBigInteger = function (bigInteger) { | ||
return v4.Address.fromInteger(parseInt(bigInteger.toString(), 10)); | ||
Address4.fromBigInteger = function (bigInteger) { | ||
return Address4.fromInteger(parseInt(bigInteger.toString(), 10)); | ||
}; | ||
/* | ||
/** | ||
* Returns the first n bits of the address, defaulting to the | ||
* subnet mask | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v4.Address.prototype.mask = function (optionalMask) { | ||
Address4.prototype.mask = function (optionalMask) { | ||
if (optionalMask === undefined) { | ||
@@ -210,19 +255,30 @@ optionalMask = this.subnetMask; | ||
/* | ||
/** | ||
* Returns the bits in the given range as a base-2 string | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {string} | ||
*/ | ||
v4.Address.prototype.getBitsBase2 = function (start, end) { | ||
Address4.prototype.getBitsBase2 = function (start, end) { | ||
return this.binaryZeroPad().slice(start, end); | ||
}; | ||
/* | ||
/** | ||
* Returns true if the given address is in the subnet of the current address | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {boolean} | ||
*/ | ||
v4.Address.prototype.isInSubnet = common.isInSubnet; | ||
Address4.prototype.isInSubnet = common.isInSubnet; | ||
/* | ||
/** | ||
* Returns a zero-padded base-2 string representation of the address | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {string} | ||
*/ | ||
v4.Address.prototype.binaryZeroPad = function () { | ||
return common.zeroPad(this.bigInteger().toString(2), v4.BITS); | ||
Address4.prototype.binaryZeroPad = function () { | ||
return common.zeroPad(this.bigInteger().toString(2), constants.BITS); | ||
}; | ||
module.exports = Address4; |
455
lib/ipv6.js
@@ -8,11 +8,9 @@ 'use strict'; | ||
var sprintf = require('sprintf').sprintf; | ||
var util = require('util'); | ||
var v4 = require('./ipv4.js').v4; | ||
var constants4 = require('./v4/constants.js'); | ||
var constants6 = require('./v6/constants.js'); | ||
var v6 = exports.v6 = { | ||
helpers: require('./v6/helpers.js') | ||
}; | ||
var Address4 = require('./ipv4.js'); | ||
merge(v6, require('./v6/constants.js')); | ||
function addCommas(number) { | ||
@@ -35,8 +33,13 @@ var r = /(\d+)(\d{3})/; | ||
/* | ||
* Instantiates an IPv6 address | ||
/** | ||
* Represents an IPv6 address | ||
* @class Address6 | ||
* @param {string} address - An IPv6 address string | ||
* @param {number} [groups=8] - How many octets to parse | ||
* @example | ||
* var address = new Address6('2001::/32'); | ||
*/ | ||
v6.Address = function (address, optionalGroups) { | ||
function Address6(address, optionalGroups) { | ||
if (optionalGroups === undefined) { | ||
this.groups = v6.GROUPS; | ||
this.groups = constants6.GROUPS; | ||
} else { | ||
@@ -55,3 +58,3 @@ this.groups = optionalGroups; | ||
var subnet = v6.RE_SUBNET_STRING.exec(address); | ||
var subnet = constants6.RE_SUBNET_STRING.exec(address); | ||
@@ -65,3 +68,3 @@ if (subnet) { | ||
this.subnetMask < 0 || | ||
this.subnetMask > v6.BITS) { | ||
this.subnetMask > constants6.BITS) { | ||
this.valid = false; | ||
@@ -73,3 +76,3 @@ this.error = 'Invalid subnet mask.'; | ||
address = address.replace(v6.RE_SUBNET_STRING, ''); | ||
address = address.replace(constants6.RE_SUBNET_STRING, ''); | ||
} else if (/\//.test(address)) { | ||
@@ -82,3 +85,3 @@ this.valid = false; | ||
var zone = v6.RE_ZONE_STRING.exec(address); | ||
var zone = constants6.RE_ZONE_STRING.exec(address); | ||
@@ -88,3 +91,3 @@ if (zone) { | ||
address = address.replace(v6.RE_ZONE_STRING, ''); | ||
address = address.replace(constants6.RE_ZONE_STRING, ''); | ||
} | ||
@@ -95,12 +98,20 @@ | ||
this.parsedAddress = this.parse(this.addressMinusSuffix); | ||
}; | ||
} | ||
merge(v6.Address.prototype, require('./v6/attributes.js')); | ||
merge(v6.Address.prototype, require('./v6/html.js')); | ||
merge(v6.Address.prototype, require('./v6/regular-expressions.js')); | ||
merge(Address6.prototype, require('./v6/attributes.js')); | ||
merge(Address6.prototype, require('./v6/html.js')); | ||
merge(Address6.prototype, require('./v6/regular-expressions.js')); | ||
/* | ||
* Converts a BigInteger to a v6 address object | ||
/** | ||
* Convert a BigInteger to a v6 address object | ||
* @memberof Address6 | ||
* @static | ||
* @param {BigInteger} bigInteger - a BigInteger to convert | ||
* @returns {Address6} | ||
* @example | ||
* var bigInteger = new BigInteger('1000000000000'); | ||
* var address = Address6.fromBigInteger(bigInteger); | ||
* address.correctForm(); // '::e8:d4a5:1000' | ||
*/ | ||
v6.Address.fromBigInteger = function (bigInteger) { | ||
Address6.fromBigInteger = function (bigInteger) { | ||
var hex = common.zeroPad(bigInteger.toString(16), 32); | ||
@@ -110,13 +121,21 @@ var groups = []; | ||
for (i = 0; i < v6.GROUPS; i++) { | ||
for (i = 0; i < constants6.GROUPS; i++) { | ||
groups.push(hex.slice(i * 4, (i + 1) * 4)); | ||
} | ||
return new v6.Address(groups.join(':')); | ||
return new Address6(groups.join(':')); | ||
}; | ||
/* | ||
* Converts a URL (optional port number) to an address object | ||
/** | ||
* Convert a URL (with optional port number) to an address object | ||
* @memberof Address6 | ||
* @static | ||
* @param {string} url - a URL with optional port number | ||
* @returns {Address6} | ||
* @example | ||
* var addressAndPort = Address6.fromURL('http://[ffff::]:8080/foo/'); | ||
* addressAndPort.address.correctForm(); // 'ffff::' | ||
* addressAndPort.port; // 8080 | ||
*/ | ||
v6.Address.fromURL = function (url) { | ||
Address6.fromURL = function (url) { | ||
var host; | ||
@@ -128,3 +147,3 @@ var port; | ||
if (url.indexOf('[') !== -1 && url.indexOf(']:') !== -1) { | ||
result = v6.RE_URL_WITH_PORT.exec(url); | ||
result = constants6.RE_URL_WITH_PORT.exec(url); | ||
@@ -147,3 +166,3 @@ if (result === null) { | ||
// Parse the address | ||
result = v6.RE_URL.exec(url); | ||
result = constants6.RE_URL.exec(url); | ||
@@ -178,3 +197,3 @@ if (result === null) { | ||
return { | ||
address: new v6.Address(host), | ||
address: new Address6(host), | ||
port: port | ||
@@ -184,7 +203,15 @@ }; | ||
/* | ||
/** | ||
* Create an IPv6-mapped address given an IPv4 address | ||
* @memberof Address6 | ||
* @static | ||
* @param {string} address - An IPv4 address string | ||
* @returns {Address6} | ||
* @example | ||
* var address = Address6.fromAddress4('192.168.0.1'); | ||
* address.correctForm(); // '::ffff:c0a8:1' | ||
* address.to4in6(); // '::ffff:192.168.0.1' | ||
*/ | ||
v6.Address.fromAddress4 = function (address4) { | ||
return new v6.Address('::ffff:' + address4); | ||
Address6.fromAddress4 = function (address4) { | ||
return new Address6('::ffff:' + address4); | ||
}; | ||
@@ -195,3 +222,3 @@ | ||
*/ | ||
v6.Address.compact = function (address, slice) { | ||
function compact (address, slice) { | ||
var s1 = []; | ||
@@ -210,8 +237,11 @@ var s2 = []; | ||
return s1.concat(['compact']).concat(s2); | ||
}; | ||
} | ||
/* | ||
* Returns the Microsoft UNC transcription of the address | ||
/** | ||
* Return the Microsoft UNC transcription of the address | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {String} the Microsoft UNC transcription of the address | ||
*/ | ||
v6.Address.prototype.microsoftTranscription = function () { | ||
Address6.prototype.microsoftTranscription = function () { | ||
return sprintf('%s.ipv6-literal.net', | ||
@@ -221,7 +251,10 @@ this.correctForm().replace(/:/g, '-')); | ||
/* | ||
* Returns the first n bits of the address, defaulting to the | ||
* subnet mask | ||
/** | ||
* Return the first n bits of the address, defaulting to the subnet mask | ||
* @memberof Address6 | ||
* @instance | ||
* @param {number} [mask=subnet] - the number of bits to mask | ||
* @returns {String} the first n bits of the address as a string | ||
*/ | ||
v6.Address.prototype.mask = function (optionalMask) { | ||
Address6.prototype.mask = function (optionalMask) { | ||
if (optionalMask === undefined) { | ||
@@ -234,6 +267,11 @@ optionalMask = this.subnetMask; | ||
/* | ||
* Returns the number of possible subnets of a given size in the address | ||
/** | ||
* Return the number of possible subnets of a given size in the address | ||
* @memberof Address6 | ||
* @instance | ||
* @param {number} [size=128] - the subnet size | ||
* @returns {String} | ||
*/ | ||
v6.Address.prototype.possibleSubnets = function (optionalSubnetSize) { | ||
// TODO: probably useful to have a numeric version of this too | ||
Address6.prototype.possibleSubnets = function (optionalSubnetSize) { | ||
if (optionalSubnetSize === undefined) { | ||
@@ -243,4 +281,4 @@ optionalSubnetSize = 128; | ||
var availableBits = v6.BITS - this.subnetMask; | ||
var subnetBits = Math.abs(optionalSubnetSize - v6.BITS); | ||
var availableBits = constants6.BITS - this.subnetMask; | ||
var subnetBits = Math.abs(optionalSubnetSize - constants6.BITS); | ||
var subnetPowers = availableBits - subnetBits; | ||
@@ -255,27 +293,36 @@ | ||
/* | ||
/** | ||
* The first address in the range given by this address' subnet | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {Address6} | ||
*/ | ||
v6.Address.prototype.startAddress = function () { | ||
Address6.prototype.startAddress = function () { | ||
var startAddress = new BigInteger(this.mask() + | ||
common.repeatString(0, v6.BITS - this.subnetMask), 2); | ||
common.repeatString(0, constants6.BITS - this.subnetMask), 2); | ||
return v6.Address.fromBigInteger(startAddress); | ||
return Address6.fromBigInteger(startAddress); | ||
}; | ||
/* | ||
/** | ||
* The last address in the range given by this address' subnet | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {Address6} | ||
*/ | ||
v6.Address.prototype.endAddress = function () { | ||
Address6.prototype.endAddress = function () { | ||
var endAddress = new BigInteger(this.mask() + | ||
common.repeatString(1, v6.BITS - this.subnetMask), 2); | ||
common.repeatString(1, constants6.BITS - this.subnetMask), 2); | ||
return v6.Address.fromBigInteger(endAddress); | ||
return Address6.fromBigInteger(endAddress); | ||
}; | ||
/* | ||
* Returns the scope of the address | ||
/** | ||
* Return the scope of the address | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v6.Address.prototype.getScope = function () { | ||
var scope = v6.SCOPES[this.getBits(12, 16)]; | ||
Address6.prototype.getScope = function () { | ||
var scope = constants6.SCOPES[this.getBits(12, 16)]; | ||
@@ -290,33 +337,45 @@ if (this.getType() === 'Global unicast' && | ||
/* | ||
* Returns the type of the address | ||
/** | ||
* Return the type of the address | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v6.Address.prototype.getType = function () { | ||
Address6.prototype.getType = function () { | ||
var self = this; | ||
function isType(name, type) { | ||
return self.isInSubnet(new v6.Address(type)); | ||
return self.isInSubnet(new Address6(type)); | ||
} | ||
return find(v6.TYPES, isType) || 'Global unicast'; | ||
return find(constants6.TYPES, isType) || 'Global unicast'; | ||
}; | ||
/* | ||
* Returns the bits in the given range as a BigInteger | ||
/** | ||
* Return the bits in the given range as a BigInteger | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {BigInteger} | ||
*/ | ||
v6.Address.prototype.getBits = function (start, end) { | ||
Address6.prototype.getBits = function (start, end) { | ||
return new BigInteger(this.getBitsBase2(start, end), 2); | ||
}; | ||
/* | ||
* Returns the bits in the given range as a base-2 string | ||
/** | ||
* Return the bits in the given range as a base-2 string | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v6.Address.prototype.getBitsBase2 = function (start, end) { | ||
Address6.prototype.getBitsBase2 = function (start, end) { | ||
return this.binaryZeroPad().slice(start, end); | ||
}; | ||
/* | ||
* Returns the bits in the given range as a base-16 string | ||
/** | ||
* Return the bits in the given range as a base-16 string | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v6.Address.prototype.getBitsBase16 = function (start, end) { | ||
Address6.prototype.getBitsBase16 = function (start, end) { | ||
var length = end - start; | ||
@@ -331,13 +390,25 @@ | ||
/* | ||
* Returns the bits that are set past the subnet mask length | ||
/** | ||
* Return the bits that are set past the subnet mask length | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v6.Address.prototype.getBitsPastSubnet = function () { | ||
return this.getBitsBase2(this.subnetMask, v6.BITS); | ||
Address6.prototype.getBitsPastSubnet = function () { | ||
return this.getBitsBase2(this.subnetMask, constants6.BITS); | ||
}; | ||
/* | ||
* Returns the reversed ip6.arpa form of the address | ||
/** | ||
* Return the reversed ip6.arpa form of the address | ||
* @memberof Address6 | ||
* @param {Object} options | ||
* @param {boolean} options.omitSuffix - omit the "ip6.arpa" suffix | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v6.Address.prototype.reverseForm = function () { | ||
Address6.prototype.reverseForm = function (options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
var characters = Math.floor(this.subnetMask / 4); | ||
@@ -353,2 +424,6 @@ | ||
if (characters > 0) { | ||
if (options.omitSuffix) { | ||
return reversed; | ||
} | ||
return sprintf('%s.ip6.arpa.', reversed); | ||
@@ -360,6 +435,9 @@ } | ||
/* | ||
* Returns the correct form of the address | ||
/** | ||
* Return the correct form of the address | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v6.Address.prototype.correctForm = function () { | ||
Address6.prototype.correctForm = function () { | ||
if (!this.parsedAddress) { | ||
@@ -394,3 +472,3 @@ return null; | ||
zeroes.push([this.parsedAddress.length - zeroCounter, | ||
this.parsedAddress.length - 1]); | ||
this.parsedAddress.length - 1]); | ||
} | ||
@@ -403,6 +481,7 @@ | ||
if (zeroes.length > 0) { | ||
// TODO: replace with lodash.max | ||
var max = Math.max.apply(Math, zeroLengths); | ||
var index = zeroLengths.indexOf(max); | ||
groups = v6.Address.compact(this.parsedAddress, zeroes[index]); | ||
groups = compact(this.parsedAddress, zeroes[index]); | ||
} else { | ||
@@ -427,18 +506,26 @@ groups = this.parsedAddress; | ||
/* | ||
* Returns a zero-padded base-2 string representation of the address | ||
/** | ||
* Return a zero-padded base-2 string representation of the address | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {String} | ||
* @example | ||
* var address = new Address6('2001:4860:4001:803::1011'); | ||
* address.binaryZeroPad(); | ||
* // '0010000000000001010010000110000001000000000000010000100000000011 | ||
* // 0000000000000000000000000000000000000000000000000001000000010001' | ||
*/ | ||
v6.Address.prototype.binaryZeroPad = function () { | ||
return common.zeroPad(this.bigInteger().toString(2), v6.BITS); | ||
Address6.prototype.binaryZeroPad = function () { | ||
return common.zeroPad(this.bigInteger().toString(2), constants6.BITS); | ||
}; | ||
// TODO: Improve the semantics of this helper function | ||
v6.Address.prototype.parse4in6 = function (address) { | ||
Address6.prototype.parse4in6 = function (address) { | ||
var groups = address.split(':'); | ||
var lastGroup = groups.slice(-1)[0]; | ||
var address4 = lastGroup.match(v4.RE_ADDRESS); | ||
var address4 = lastGroup.match(constants4.RE_ADDRESS); | ||
if (address4) { | ||
var temp4 = new v4.Address(address4[0]); | ||
var temp4 = new Address4(address4[0]); | ||
@@ -450,3 +537,3 @@ for (var i = 0; i < temp4.groups; i++) { | ||
this.parseError = address.replace(v4.RE_ADDRESS, | ||
this.parseError = address.replace(constants4.RE_ADDRESS, | ||
temp4.parsedAddress.map(spanLeadingZeroes4).join('.')); | ||
@@ -460,3 +547,3 @@ | ||
groups[groups.length - 1] = temp4.toV6Group(); | ||
groups[groups.length - 1] = temp4.toGroup6(); | ||
@@ -470,3 +557,3 @@ address = groups.join(':'); | ||
// TODO: Make private? | ||
v6.Address.prototype.parse = function (address) { | ||
Address6.prototype.parse = function (address) { | ||
address = this.parse4in6(address); | ||
@@ -478,3 +565,3 @@ | ||
var badCharacters = address.match(v6.RE_BAD_CHARACTERS); | ||
var badCharacters = address.match(constants6.RE_BAD_CHARACTERS); | ||
@@ -486,3 +573,3 @@ if (badCharacters) { | ||
this.parseError = address.replace(v6.RE_BAD_CHARACTERS, | ||
this.parseError = address.replace(constants6.RE_BAD_CHARACTERS, | ||
'<span class="parse-error">$1</span>'); | ||
@@ -493,3 +580,3 @@ | ||
var badAddress = address.match(v6.RE_BAD_ADDRESS); | ||
var badAddress = address.match(constants6.RE_BAD_ADDRESS); | ||
@@ -500,3 +587,3 @@ if (badAddress) { | ||
this.parseError = address.replace(v6.RE_BAD_ADDRESS, | ||
this.parseError = address.replace(constants6.RE_BAD_ADDRESS, | ||
'<span class="parse-error">$1</span>'); | ||
@@ -577,6 +664,13 @@ | ||
/* | ||
* Returns the canonical form of the address | ||
function paddedHex(octet) { | ||
return sprintf('%04x', parseInt(octet, 16)); | ||
} | ||
/** | ||
* Return the canonical form of the address | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v6.Address.prototype.canonicalForm = function () { | ||
Address6.prototype.canonicalForm = function () { | ||
if (!this.valid) { | ||
@@ -586,11 +680,12 @@ return null; | ||
return this.parsedAddress.map(function (n) { | ||
return sprintf('%04x', parseInt(n, 16)); | ||
}).join(':'); | ||
return this.parsedAddress.map(paddedHex).join(':'); | ||
}; | ||
/* | ||
* Returns the decimal form of the address | ||
/** | ||
* Return the decimal form of the address | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v6.Address.prototype.decimal = function () { | ||
Address6.prototype.decimal = function () { | ||
if (!this.valid) { | ||
@@ -605,6 +700,9 @@ return null; | ||
/* | ||
* Returns the address as a BigInteger | ||
/** | ||
* Return the address as a BigInteger | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {BigInteger} | ||
*/ | ||
v6.Address.prototype.bigInteger = function () { | ||
Address6.prototype.bigInteger = function () { | ||
if (!this.valid) { | ||
@@ -614,20 +712,34 @@ return null; | ||
return new BigInteger(this.parsedAddress.map(function (n) { | ||
return sprintf('%04x', parseInt(n, 16)); | ||
}).join(''), 16); | ||
return new BigInteger(this.parsedAddress.map(paddedHex).join(''), 16); | ||
}; | ||
v6.Address.prototype.tov4 = function () { | ||
/** | ||
* Return the last two groups of this address as an IPv4 address string | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {String} | ||
* @example | ||
* var address = new Address6('2001:4860:4001::1825:bf11'); | ||
* address.to4(); // '24.37.191.17' | ||
*/ | ||
Address6.prototype.to4 = function () { | ||
var binary = this.binaryZeroPad().split(''); | ||
return v4.Address.fromHex(new BigInteger(binary.slice(96, 128) | ||
return Address4.fromHex(new BigInteger(binary.slice(96, 128) | ||
.join(''), 2).toString(16)); | ||
}; | ||
/* | ||
* Returns the v4-in-v6 form of the address | ||
Address6.prototype.tov4 = | ||
util.deprecate(Address6.prototype.to4, | ||
'deprecated: `tov4` has been renamed to `to4`'); | ||
/** | ||
* Return the v4-in-v6 form of the address | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {String} | ||
*/ | ||
v6.Address.prototype.v4inv6 = function () { | ||
var address4 = this.tov4(); | ||
var address6 = new v6.Address(this.parsedAddress.slice(0, 6).join(':'), 6); | ||
Address6.prototype.to4in6 = function () { | ||
var address4 = this.to4(); | ||
var address6 = new Address6(this.parsedAddress.slice(0, 6).join(':'), 6); | ||
@@ -645,6 +757,13 @@ var correct = address6.correctForm(); | ||
/* | ||
* Returns an object containing the Teredo properties of the address | ||
Address6.prototype.v4inv6 = | ||
util.deprecate(Address6.prototype.to4in6, | ||
'deprecated: `v4inv6` has been renamed to `to4in6`'); | ||
/** | ||
* Return an object containing the Teredo properties of the address | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {Object} | ||
*/ | ||
v6.Address.prototype.teredo = function () { | ||
Address6.prototype.inspectTeredo = function () { | ||
/* | ||
@@ -675,4 +794,4 @@ - Bits 0 to 31 are set to the Teredo prefix (normally 2001:0000::/32). | ||
var server4 = v4.Address.fromHex(this.getBitsBase16(32, 64)); | ||
var client4 = v4.Address.fromHex(this.getBits(96, 128) | ||
var server4 = Address4.fromHex(this.getBitsBase16(32, 64)); | ||
var client4 = Address4.fromHex(this.getBits(96, 128) | ||
.xor(new BigInteger('ffffffff', 16)).toString(16)); | ||
@@ -706,6 +825,13 @@ | ||
/* | ||
* Returns an object containing the 6to4 properties of the address | ||
Address6.prototype.teredo = | ||
util.deprecate(Address6.prototype.inspectTeredo, | ||
'deprecated: `teredo` has been renamed to `inspectTeredo`'); | ||
/** | ||
* Return an object containing the 6to4 properties of the address | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {Object} | ||
*/ | ||
v6.Address.prototype.six2four = function () { | ||
Address6.prototype.inspect6to4 = function () { | ||
/* | ||
@@ -718,3 +844,3 @@ - Bits 0 to 15 are set to the 6to4 prefix (2002::/16). | ||
var gateway = v4.Address.fromHex(this.getBitsBase16(16, 48)); | ||
var gateway = Address4.fromHex(this.getBitsBase16(16, 48)); | ||
@@ -727,6 +853,13 @@ return { | ||
/* | ||
* Returns a v6 6to4 address from a v6 v4inv6 address. | ||
Address6.prototype.six2four = | ||
util.deprecate(Address6.prototype.inspect6to4, | ||
'deprecated: `six2four` has been renamed to `inspect6to4`'); | ||
/** | ||
* Return a v6 6to4 address from a v6 v4inv6 address | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {Address6} | ||
*/ | ||
v6.Address.prototype.get6to4 = function () { | ||
Address6.prototype.to6to4 = function () { | ||
if (!this.is4()) { | ||
@@ -744,16 +877,50 @@ return null; | ||
return new v6.Address(addr6to4); | ||
return new Address6(addr6to4); | ||
}; | ||
v6.Address.prototype.toByteArray = function () { | ||
Address6.prototype.get6to4 = | ||
util.deprecate(Address6.prototype.to6to4, | ||
'deprecated: `get6to4` has been renamed to `to6to4`'); | ||
/** | ||
* Return a byte array | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {Array} | ||
*/ | ||
Address6.prototype.toByteArray = function () { | ||
return this.bigInteger().toByteArray(); | ||
}; | ||
v6.Address.prototype.toUnsignedByteArray = function () { | ||
return this.toByteArray().map(function (b) { | ||
return b & 0xFF; | ||
}); | ||
function unsignByte(b) { | ||
return b & 0xFF; | ||
} | ||
/** | ||
* Return an unsigned byte array | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {Array} | ||
*/ | ||
Address6.prototype.toUnsignedByteArray = function () { | ||
return this.toByteArray().map(unsignByte); | ||
}; | ||
v6.Address.fromUnsignedByteArray = function (bytes) { | ||
/** | ||
* Convert a byte array to an Address6 object | ||
* @memberof Address6 | ||
* @static | ||
* @returns {Address6} | ||
*/ | ||
Address6.fromByteArray = function (bytes) { | ||
return this.fromUnsignedByteArray(bytes.map(unsignByte)); | ||
}; | ||
/** | ||
* Convert an unsigned byte array to an Address6 object | ||
* @memberof Address6 | ||
* @static | ||
* @returns {Address6} | ||
*/ | ||
Address6.fromUnsignedByteArray = function (bytes) { | ||
var BYTE_MAX = new BigInteger('256', 10); | ||
@@ -770,9 +937,5 @@ var result = new BigInteger('0', 10); | ||
return v6.Address.fromBigInteger(result); | ||
return Address6.fromBigInteger(result); | ||
}; | ||
v6.Address.fromByteArray = function (bytes) { | ||
return this.fromUnsignedByteArray(bytes.map(function (b) { | ||
return b & 0xFF; | ||
})); | ||
}; | ||
module.exports = Address6; |
@@ -5,2 +5,3 @@ exports.BITS = 32; | ||
exports.RE_ADDRESS = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/g; | ||
exports.RE_SUBNET_STRING = /\/\d{1,2}$/; |
@@ -6,4 +6,7 @@ 'use strict'; | ||
/* | ||
/** | ||
* Returns true if the address is valid, false otherwise | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {boolean} | ||
*/ | ||
@@ -14,14 +17,23 @@ exports.isValid = function () { | ||
/* | ||
/** | ||
* Returns true if the given address is in the subnet of the current address | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {boolean} | ||
*/ | ||
exports.isInSubnet = common.isInSubnet; | ||
/* | ||
/** | ||
* Returns true if the address is correct, false otherwise | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {boolean} | ||
*/ | ||
exports.isCorrect = common.isCorrect(v6.BITS); | ||
/* | ||
/** | ||
* Returns true if the address is in the canonical form, false otherwise | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {boolean} | ||
*/ | ||
@@ -32,4 +44,7 @@ exports.isCanonical = common.falseIfInvalid(function () { | ||
/* | ||
/** | ||
* Returns true if the address is a link local address, false otherwise | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {boolean} | ||
*/ | ||
@@ -46,4 +61,7 @@ exports.isLinkLocal = common.falseIfInvalid(function () { | ||
/* | ||
/** | ||
* Returns true if the address is a multicast address, false otherwise | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {boolean} | ||
*/ | ||
@@ -54,4 +72,7 @@ exports.isMulticast = common.falseIfInvalid(function () { | ||
/* | ||
/** | ||
* Returns true if the address is a v4-in-v6 address, false otherwise | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {boolean} | ||
*/ | ||
@@ -62,4 +83,7 @@ exports.is4 = common.falseIfInvalid(function () { | ||
/* | ||
/** | ||
* Returns true if the address is a Teredo address, false otherwise | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {boolean} | ||
*/ | ||
@@ -70,4 +94,7 @@ exports.isTeredo = common.falseIfInvalid(function () { | ||
/* | ||
/** | ||
* Returns true if the address is a 6to4 address, false otherwise | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {boolean} | ||
*/ | ||
@@ -78,4 +105,7 @@ exports.is6to4 = common.falseIfInvalid(function () { | ||
/* | ||
/** | ||
* Returns true if the address is a loopback address, false otherwise | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {boolean} | ||
*/ | ||
@@ -82,0 +112,0 @@ exports.isLoopback = common.falseIfInvalid(function () { |
exports.BITS = 128; | ||
exports.GROUPS = 8; | ||
/** | ||
* Represents IPv6 address scopes | ||
* @memberof Address6 | ||
* @static | ||
*/ | ||
exports.SCOPES = { | ||
@@ -15,3 +20,7 @@ 0: 'Reserved', | ||
// TODO: Add ff0x::fb, etc. for multicast DNS | ||
/** | ||
* Represents IPv6 address types | ||
* @memberof Address6 | ||
* @static | ||
*/ | ||
exports.TYPES = { | ||
@@ -42,6 +51,28 @@ 'ff01::1/128': 'Multicast (All nodes on this interface)', | ||
/** | ||
* A regular expression that matches bad characters in an IPv6 address | ||
* @memberof Address6 | ||
* @static | ||
*/ | ||
exports.RE_BAD_CHARACTERS = /([^0-9a-f:\/%])/ig; | ||
/** | ||
* A regular expression that matches an incorrect IPv6 address | ||
* @memberof Address6 | ||
* @static | ||
*/ | ||
exports.RE_BAD_ADDRESS = /([0-9a-f]{5,}|:{3,}|[^:]:$|^:[^:]|\/$)/ig; | ||
/** | ||
* A regular expression that matches an IPv6 subnet | ||
* @memberof Address6 | ||
* @static | ||
*/ | ||
exports.RE_SUBNET_STRING = /\/\d{1,3}(?=%|$)/; | ||
/** | ||
* A regular expression that matches an IPv6 zone | ||
* @memberof Address6 | ||
* @static | ||
*/ | ||
exports.RE_ZONE_STRING = /%.*$/; | ||
@@ -48,0 +79,0 @@ |
@@ -5,4 +5,4 @@ 'use strict'; | ||
/* | ||
* Returns the string with all zeroes contained in a <span> | ||
/** | ||
* @returns {String} the string with all zeroes contained in a <span> | ||
*/ | ||
@@ -13,4 +13,4 @@ var spanAllZeroes = exports.spanAllZeroes = function (s) { | ||
/* | ||
* Returns the string with each character contained in a <span> | ||
/** | ||
* @returns {String} the string with each character contained in a <span> | ||
*/ | ||
@@ -35,4 +35,4 @@ exports.spanAll = function (s, optionalOffset) { | ||
/* | ||
* Returns the string with leading zeroes contained in a <span> | ||
/** | ||
* @returns {String} the string with leading zeroes contained in a <span> | ||
*/ | ||
@@ -47,4 +47,5 @@ exports.spanLeadingZeroes = function (address) { | ||
/* | ||
/** | ||
* Groups an address | ||
* @returns {String} a grouped address | ||
*/ | ||
@@ -51,0 +52,0 @@ exports.simpleGroup = function (addressString, offset) { |
@@ -7,4 +7,4 @@ 'use strict'; | ||
/* | ||
* Returns the address in link form with a default port of 80 | ||
/** | ||
* @returns {String} the address in link form with a default port of 80 | ||
*/ | ||
@@ -21,4 +21,4 @@ exports.href = function (optionalPort) { | ||
/* | ||
* Returns a link suitable for conveying the address via a URL hash | ||
/** | ||
* @returns {String} a link suitable for conveying the address via a URL hash | ||
*/ | ||
@@ -45,3 +45,3 @@ exports.link = function (options) { | ||
if (options.v4) { | ||
formFunction = this.v4inv6; | ||
formFunction = this.to4in6; | ||
} | ||
@@ -58,4 +58,5 @@ | ||
/* | ||
/** | ||
* Groups an address | ||
* @returns {String} | ||
*/ | ||
@@ -62,0 +63,0 @@ exports.group = function () { |
@@ -92,5 +92,9 @@ 'use strict'; | ||
/* | ||
* Generate a regular expression string that can be used to find or validate all | ||
* variations of this address. | ||
/** | ||
* Generate a regular expression string that can be used to find or validate | ||
* all variations of this address | ||
* @memberof Address6 | ||
* @instance | ||
* @param {string} optionalSubString | ||
* @returns {string} | ||
*/ | ||
@@ -139,5 +143,9 @@ exports.regularExpressionString = function (optionalSubString) { | ||
/* | ||
/** | ||
* Generate a regular expression that can be used to find or validate all | ||
* variations of this address. | ||
* @memberof Address6 | ||
* @instance | ||
* @param {string} optionalSubString | ||
* @returns {RegExp} | ||
*/ | ||
@@ -144,0 +152,0 @@ exports.regularExpression = function (optionalSubstring) { |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "4.2.0", | ||
"version": "5.0.1", | ||
"author": "Beau Gunderson <beau@beaugunderson.com> (https://beaugunderson.com/)", | ||
@@ -16,3 +16,4 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
"test": "mocha -R spec", | ||
"docs": "documentation --github --output docs --format html ./index.js" | ||
}, | ||
@@ -31,15 +32,16 @@ "bin": { | ||
"dependencies": { | ||
"cli": "0.6.x", | ||
"cli": "0.10.x", | ||
"cliff": "0.1.x", | ||
"jsbn": "0.0.0", | ||
"lodash.find": "^3.2.0", | ||
"lodash.merge": "^3.2.1", | ||
"lodash.find": "^3.2.1", | ||
"lodash.merge": "^3.3.2", | ||
"sprintf": "0.1.x" | ||
}, | ||
"devDependencies": { | ||
"chai": "^2.3.0", | ||
"coveralls": "^2.11.2", | ||
"istanbul": "^0.3.13", | ||
"mocha": "^2.2.4" | ||
"chai": "^3.3.0", | ||
"coveralls": "^2.11.4", | ||
"documentation": "^2.1.0-alpha2", | ||
"istanbul": "^0.3.22", | ||
"mocha": "^2.3.3" | ||
} | ||
} |
123
README.md
@@ -1,54 +0,103 @@ | ||
[![Build Status](https://secure.travis-ci.org/beaugunderson/ip-address.png?branch=master)](http://travis-ci.org/beaugunderson/ip-address) [![Coverage Status](https://img.shields.io/coveralls/beaugunderson/ip-address.svg)](https://coveralls.io/r/beaugunderson/ip-address?branch=master) | ||
[![travis]](http://travis-ci.org/beaugunderson/ip-address) | ||
[![coverage]](https://coveralls.io/r/beaugunderson/ip-address?branch=master) | ||
[![downloads]](https://www.npmjs.com/package/ip-address) | ||
[![version]](https://www.npmjs.com/package/ip-address) | ||
[travis]: https://img.shields.io/travis/beaugunderson/ip-address.svg | ||
[coverage]: https://img.shields.io/coveralls/beaugunderson/ip-address.svg | ||
[downloads]: https://img.shields.io/npm/dm/ip-address.svg | ||
[version]: https://img.shields.io/npm/v/ip-address.svg | ||
## ip-address | ||
`ip-address` is a library for manipulating IPv6 and IPv4 addresses in JavaScript. | ||
`ip-address` is a library for validating and manipulating IPv4 and IPv6 | ||
addresses in JavaScript. | ||
### Upgrading to 5.0 | ||
- `v4.Address` and `v6.Address` have been renamed `Address4` and `Address6` | ||
- `Address4#toV6Group` has been renamed `Address4#toGroup6` | ||
- `Address6#get6to4` has been renamed `Address6#to6to4` | ||
- `Address6#six2four` has been renamed `Address6#inspect6to4` | ||
- `Address6#teredo` has been renamed `Address6#inspectTeredo` | ||
- `Address6#tov4` has been renamed `Address6#to4` | ||
- `Address6#v4inv6` has been renamed `Address6#to4in6` | ||
Deprecation warnings are in place for all of these methods. | ||
### Documentation | ||
Documentation is available at [ip-address.js.org](http://ip-address.js.org/). | ||
### Examples | ||
For node: | ||
```js | ||
var v6 = require('ip-address').v6; | ||
var Address6 = require('ip-address').Address6; | ||
var address = new v6.Address('2001:0:ce49:7601:e866:efff:62c3:fffe'); | ||
var address = new Address6('2001:0:ce49:7601:e866:efff:62c3:fffe'); | ||
console.log(address.isValid()); // Prints "true" | ||
address.isValid(); // true | ||
var teredo = address.teredo(); | ||
var teredo = address.inspectTeredo(); | ||
console.log(teredo.client4); // Prints "157.60.0.1" | ||
teredo.client4; // '157.60.0.1' | ||
``` | ||
### Current functionality | ||
### Features | ||
- Parsing of most IPv6 notations | ||
- Parsing of IPv6 Addresses and Ports from URLs with `v6.Address.fromURL(url)` | ||
- Validity checking | ||
- Decoding of the [Teredo | ||
information](http://en.wikipedia.org/wiki/Teredo_tunneling#IPv6_addressing) | ||
in an address | ||
- Whether one address is a valid subnet of another | ||
- What special properties a given address has (multicast prefix, unique | ||
local address prefix, etc.) | ||
- Number of subnets of a certain size in a given address | ||
- Display methods | ||
- Hex, binary, and decimal | ||
- Canonical form | ||
- Correct form | ||
- IPv4-compatible (i.e. `::ffff:192.168.0.1`) | ||
- Works in [node.js](http://nodejs.org/) and the browser (with browserify) | ||
- 1,500+ unit tests with [node.js](http://nodejs.org/) and | ||
[Mocha](http://visionmedia.github.com/mocha/) | ||
- Parsing of all IPv6 notations | ||
- Parsing of IPv6 addresses and ports from URLs with `Address6.fromURL(url)` | ||
- Validity checking | ||
- Decoding of the [Teredo | ||
information](http://en.wikipedia.org/wiki/Teredo_tunneling#IPv6_addressing) | ||
in an address | ||
- Whether one address is a valid subnet of another | ||
- What special properties a given address has (multicast prefix, unique | ||
local address prefix, etc.) | ||
- Number of subnets of a certain size in a given address | ||
- Display methods | ||
- Hex, binary, and decimal | ||
- Canonical form | ||
- Correct form | ||
- IPv4-compatible (i.e. `::ffff:192.168.0.1`) | ||
- Works in [node](http://nodejs.org/) and the browser (with browserify) | ||
- ~1,600 test cases | ||
### Used by | ||
- [anon](https://github.com/edsu/anon) which powers | ||
[@congressedits](https://twitter.com/congressedits), among | ||
[many others](https://github.com/edsu/anon#community) | ||
- [node-swiz](https://github.com/racker/node-swiz) which is used by [Rackspace](http://www.rackspace.com/) | ||
- [node-socksified](https://github.com/vially/node-socksified) | ||
### TODO | ||
- [Better documentation](https://github.com/documentationjs/documentation) | ||
- [anon](https://github.com/edsu/anon) which powers | ||
[@congressedits](https://twitter.com/congressedits), among | ||
[many others](https://github.com/edsu/anon#community) | ||
- [base85](https://github.com/noseglid/base85): base85 encoding/decoding | ||
- [contrail-web-core](https://github.com/Juniper/contrail-web-core): part of | ||
Contrail, a network virtualization solution made by Juniper Networks | ||
- [dhcpjs](https://github.com/apaprocki/node-dhcpjs): a DHCP client and server | ||
- [geoip-web](https://github.com/tfrce/node-geoip-web): a server for | ||
quickly geolocating IP addresses | ||
- [hexabus](https://github.com/mysmartgrid/hexabus): an IPv6-based home | ||
automation bus | ||
- [heroku-portscanner](https://github.com/robison/heroku-portscanner): nmap | ||
hosted on Heroku | ||
- [ipfs-swarm](https://github.com/diasdavid/node-ipfs-swarm): a swarm | ||
implementation based on IPFS | ||
- [javascript-x-server](https://github.com/GothAck/javascript-x-server): an X | ||
server written in JavaScript | ||
- [libnmap](https://github.com/jas-/node-libnmap): a node API for nmap | ||
- [mail-io](https://github.com/mofux/mail-io): a lightweight SMTP server | ||
- [maxmind-db-reader](https://github.com/PaddeK/node-maxmind-db): a library for | ||
reading MaxMind database files | ||
- [proxy-protocol-v2](https://github.com/ably/proxy-protocol-v2): a proxy | ||
protocol encoder/decoder built by [Ably](https://www.ably.io/) | ||
- [Samsara](https://github.com/mariusGundersen/Samsara): a Docker web interface | ||
- [sis-api](https://github.com/sis-cmdb/sis-api): a configuration management | ||
database API | ||
- [socks5-client](https://github.com/mattcg/socks5-client): a SOCKS v5 client | ||
- [socksified](https://github.com/vially/node-socksified): a SOCKS v5 client | ||
- [socksv5](https://github.com/mscdex/socksv5): a SOCKS v5 server/client | ||
- [ssdapi](https://github.com/rsolomou/ssdapi): an API created by the | ||
University of Portsmouth | ||
- [SwitchyOmega](https://github.com/FelisCatus/SwitchyOmega): a [Chrome | ||
extension](https://chrome.google.com/webstore/detail/padekgcemlokbadohgkifijomclgjgif) | ||
for switching between multiple proxies with ~260k users! | ||
- [swiz](https://github.com/racker/node-swiz): a serialization framework built | ||
and used by [Rackspace](http://www.rackspace.com/) |
@@ -7,8 +7,8 @@ 'use strict'; | ||
var v4 = ip.v4; | ||
var v6 = ip.v6; | ||
var Address4 = ip.Address4; | ||
var Address6 = ip.Address6; | ||
function addressIs(addressString, descriptors) { | ||
var address4 = new v4.Address(addressString); | ||
var address6 = new v6.Address(addressString); | ||
var address4 = new Address4(addressString); | ||
var address6 = new Address6(addressString); | ||
@@ -15,0 +15,0 @@ describe(addressString, function () { |
@@ -6,6 +6,6 @@ 'use strict'; | ||
var v4 = require('..').v4; | ||
var Address4 = require('../lib/ipv4.js'); | ||
// A convenience function to convert a list of IPv4 address notations | ||
// to v4.Address instances | ||
// to Address4 instances | ||
function notationsToAddresseses(notations) { | ||
@@ -15,3 +15,3 @@ var addresses = []; | ||
notations.forEach(function (notation) { | ||
addresses.push(new v4.Address(notation)); | ||
addresses.push(new Address4(notation)); | ||
}); | ||
@@ -24,3 +24,3 @@ | ||
describe('An invalid address', function () { | ||
var topic = new v4.Address('127.0.0'); | ||
var topic = new Address4('127.0.0'); | ||
@@ -40,3 +40,3 @@ it('is invalid', function () { | ||
describe('A correct address', function () { | ||
var topic = new v4.Address('127.0.0.1'); | ||
var topic = new Address4('127.0.0.1'); | ||
@@ -51,7 +51,7 @@ it('validates as correct', function () { | ||
describe('An address with a subnet', function () { | ||
var topic = new v4.Address('127.0.0.1/16'); | ||
var topic = new Address4('127.0.0.1/16'); | ||
it('is contained by an identical address with an identical subnet', | ||
function () { | ||
var same = new v4.Address('127.0.0.1/16'); | ||
var same = new Address4('127.0.0.1/16'); | ||
@@ -63,7 +63,7 @@ topic.isInSubnet(same).should.equal(true); | ||
describe('A small subnet', function () { | ||
var topic = new v4.Address('127.0.0.1/16'); | ||
var topic = new Address4('127.0.0.1/16'); | ||
it('is contained by larger subnets', function () { | ||
for (var i = 15; i > 0; i--) { | ||
var larger = new v4.Address(sprintf('127.0.0.1/%d', i)); | ||
var larger = new Address4(sprintf('127.0.0.1/%d', i)); | ||
@@ -76,7 +76,7 @@ topic.isInSubnet(larger).should.equal(true); | ||
describe('A large subnet', function () { | ||
var topic = new v4.Address('127.0.0.1/8'); | ||
var topic = new Address4('127.0.0.1/8'); | ||
it('is not contained by smaller subnets', function () { | ||
for (var i = 9; i <= 32; i++) { | ||
var smaller = new v4.Address(sprintf('127.0.0.1/%d', i)); | ||
var smaller = new Address4(sprintf('127.0.0.1/%d', i)); | ||
@@ -89,3 +89,3 @@ topic.isInSubnet(smaller).should.equal(false); | ||
describe('An integer v4 address', function () { | ||
var topic = v4.Address.fromInteger(432432423); | ||
var topic = Address4.fromInteger(432432423); | ||
@@ -104,3 +104,3 @@ it('validates', function () { | ||
it('should match an address from its hex representation', function () { | ||
var hex = v4.Address.fromHex('19c66527'); | ||
var hex = Address4.fromHex('19c66527'); | ||
@@ -115,3 +115,3 @@ hex.address.should.equal('25.198.101.39'); | ||
describe('An address with a subnet', function () { | ||
var topic = new v4.Address('127.0.0.1/16'); | ||
var topic = new Address4('127.0.0.1/16'); | ||
@@ -135,7 +135,7 @@ it('validates', function () { | ||
it('is in its own subnet', function () { | ||
topic.isInSubnet(new v4.Address('127.0.0.1/16')).should.equal(true); | ||
topic.isInSubnet(new Address4('127.0.0.1/16')).should.equal(true); | ||
}); | ||
it('is not in another subnet', function () { | ||
topic.isInSubnet(new v4.Address('192.168.0.1/16')).should.equal(false); | ||
topic.isInSubnet(new Address4('192.168.0.1/16')).should.equal(false); | ||
}); | ||
@@ -145,3 +145,3 @@ }); | ||
describe('Creating an address from a BigInteger', function () { | ||
var topic = v4.Address.fromBigInteger(2130706433); | ||
var topic = Address4.fromBigInteger(2130706433); | ||
@@ -155,3 +155,3 @@ it('should parse correctly', function () { | ||
describe('Converting an address to a BigInteger', function () { | ||
var topic = new v4.Address('127.0.0.1'); | ||
var topic = new Address4('127.0.0.1'); | ||
@@ -164,3 +164,3 @@ it('should convert properly', function () { | ||
describe('Creating an address from hex', function () { | ||
var topic = v4.Address.fromHex('7f:00:00:01'); | ||
var topic = Address4.fromHex('7f:00:00:01'); | ||
@@ -174,3 +174,3 @@ it('should parse correctly', function () { | ||
describe('Converting an address to hex', function () { | ||
var topic = new v4.Address('127.0.0.1'); | ||
var topic = new Address4('127.0.0.1'); | ||
@@ -183,3 +183,3 @@ it('should convert correctly', function () { | ||
describe('Converting an address to an array', function () { | ||
var topic = new v4.Address('127.0.0.1'); | ||
var topic = new Address4('127.0.0.1'); | ||
@@ -186,0 +186,0 @@ it('should convert correctly', function () { |
@@ -11,9 +11,11 @@ 'use strict'; | ||
var Address6 = require('../lib/ipv6.js'); | ||
var v6 = require('..').v6; | ||
// A convenience function to convert a list of IPv6 address notations | ||
// to v6.Address instances | ||
// to Address6 instances | ||
function notationsToAddresseses(notations) { | ||
return notations.map(function (notation) { | ||
return new v6.Address(notation); | ||
return new Address6(notation); | ||
}); | ||
@@ -24,3 +26,3 @@ } | ||
describe('An invalid address', function () { | ||
var topic = new v6.Address('a:abcde::'); | ||
var topic = new Address6('a:abcde::'); | ||
@@ -37,3 +39,3 @@ it('is invalid', function () { | ||
should.equal(topic.bigInteger(), null); | ||
should.equal(topic.get6to4(), null); | ||
should.equal(topic.to6to4(), null); | ||
@@ -45,3 +47,3 @@ topic.isTeredo().should.equal(false); | ||
describe('a fully ellided /0 address', function () { | ||
var topic = new v6.Address('::/0'); | ||
var topic = new Address6('::/0'); | ||
@@ -54,3 +56,3 @@ it('gets the correct reverse from', function () { | ||
describe('A link local address', function () { | ||
var topic = new v6.Address('fe80::baf6:b1ff:fe15:4885'); | ||
var topic = new Address6('fe80::baf6:b1ff:fe15:4885'); | ||
@@ -68,3 +70,3 @@ it('gets the correct type', function () { | ||
describe('A correct address', function () { | ||
var topic = new v6.Address('a:b:c:d:e:f:0:1/64'); | ||
var topic = new Address6('a:b:c:d:e:f:0:1/64'); | ||
@@ -83,3 +85,3 @@ it('contains no uppercase letters', function () { | ||
var bytes = topic.toByteArray(); | ||
var address = v6.Address.fromByteArray(bytes); | ||
var address = Address6.fromByteArray(bytes); | ||
@@ -91,3 +93,3 @@ address.correctForm().should.equal(topic.correctForm()); | ||
var unsignedBytes = topic.toUnsignedByteArray(); | ||
var address = v6.Address.fromUnsignedByteArray(unsignedBytes); | ||
var address = Address6.fromUnsignedByteArray(unsignedBytes); | ||
@@ -142,7 +144,7 @@ address.correctForm().should.equal(topic.correctForm()); | ||
describe('An address with a subnet', function () { | ||
var topic = new v6.Address('ffff::/64'); | ||
var topic = new Address6('ffff::/64'); | ||
it('is contained by an identical address with an identical subnet', | ||
function () { | ||
var same = new v6.Address('ffff::/64'); | ||
var same = new Address6('ffff::/64'); | ||
@@ -173,7 +175,7 @@ topic.isInSubnet(same).should.equal(true); | ||
describe('Small subnets', function () { | ||
var topic = new v6.Address('ffff::/64'); | ||
var topic = new Address6('ffff::/64'); | ||
it('is contained by larger subnets', function () { | ||
for (var i = 63; i > 0; i--) { | ||
var larger = new v6.Address(sprintf('ffff::/%d', i)); | ||
var larger = new Address6(sprintf('ffff::/%d', i)); | ||
@@ -186,7 +188,7 @@ topic.isInSubnet(larger).should.equal(true); | ||
describe('Large subnets', function () { | ||
var topic = new v6.Address('ffff::/8'); | ||
var topic = new Address6('ffff::/8'); | ||
it('is not contained by smaller subnets', function () { | ||
for (var i = 9; i <= 128; i++) { | ||
var smaller = new v6.Address(sprintf('ffff::/%d', i)); | ||
var smaller = new Address6(sprintf('ffff::/%d', i)); | ||
@@ -199,3 +201,3 @@ topic.isInSubnet(smaller).should.equal(false); | ||
describe('A canonical address', function () { | ||
var topic = new v6.Address('000a:0000:0000:0000:0000:0000:0000:000b'); | ||
var topic = new Address6('000a:0000:0000:0000:0000:0000:0000:000b'); | ||
@@ -215,3 +217,3 @@ it('is 39 characters long', function () { | ||
describe('A v4-in-v6 address', function () { | ||
var topic = new v6.Address('::192.168.0.1'); | ||
var topic = new Address6('::192.168.0.1'); | ||
@@ -228,3 +230,3 @@ it('validates', function () { | ||
describe('An address with a subnet', function () { | ||
var topic = new v6.Address('a:b::/48'); | ||
var topic = new Address6('a:b::/48'); | ||
@@ -240,7 +242,7 @@ it('validates', function () { | ||
it('is in its own subnet', function () { | ||
topic.isInSubnet(new v6.Address('a:b::/48')).should.equal(true); | ||
topic.isInSubnet(new Address6('a:b::/48')).should.equal(true); | ||
}); | ||
it('is not in another subnet', function () { | ||
topic.isInSubnet(new v6.Address('a:c::/48')).should.equal(false); | ||
topic.isInSubnet(new Address6('a:c::/48')).should.equal(false); | ||
}); | ||
@@ -250,3 +252,3 @@ }); | ||
describe('An address with a zone', function () { | ||
var topic = new v6.Address('a::b%abcdefg'); | ||
var topic = new Address6('a::b%abcdefg'); | ||
@@ -263,3 +265,3 @@ it('validates', function () { | ||
describe('A teredo address', function () { | ||
var topic = new v6.Address('2001:0000:ce49:7601:e866:efff:62c3:fffe'); | ||
var topic = new Address6('2001:0000:ce49:7601:e866:efff:62c3:fffe'); | ||
@@ -271,3 +273,3 @@ it('validates as Teredo', function () { | ||
it('contains valid Teredo information', function () { | ||
var teredo = topic.teredo(); | ||
var teredo = topic.inspectTeredo(); | ||
@@ -283,3 +285,3 @@ should.equal(teredo.prefix, '2001:0000'); | ||
describe('A 6to4 address', function () { | ||
var topic = new v6.Address('2002:ce49:7601:1:2de:adff:febe:eeef'); | ||
var topic = new Address6('2002:ce49:7601:1:2de:adff:febe:eeef'); | ||
@@ -291,6 +293,6 @@ it('validates as 6to4', function () { | ||
it('contains valid 6to4 information', function () { | ||
var six2four = topic.six2four(); | ||
var sixToFourProperties = topic.inspect6to4(); | ||
should.equal(six2four.prefix, '2002'); | ||
should.equal(six2four.gateway, '206.73.118.1'); | ||
should.equal(sixToFourProperties.prefix, '2002'); | ||
should.equal(sixToFourProperties.gateway, '206.73.118.1'); | ||
}); | ||
@@ -319,3 +321,3 @@ }); | ||
'2001:0db8:0000:0000:0001:0000:0000:0001'); | ||
should.equal(topic.v4inv6(), '2001:db8::1:0:0.0.0.1'); | ||
should.equal(topic.to4in6(), '2001:db8::1:0:0.0.0.1'); | ||
should.equal(topic.decimal(), | ||
@@ -331,3 +333,3 @@ '08193:03512:00000:00000:00001:00000:00000:00001'); | ||
describe('Address from an IPv4 address', function () { | ||
var obj = v6.Address.fromAddress4('192.168.0.1'); | ||
var obj = Address6.fromAddress4('192.168.0.1'); | ||
@@ -337,11 +339,11 @@ it('should parse correctly', function () { | ||
expect(obj.correctForm()).to.equal('::ffff:c0a8:1'); | ||
expect(obj.v4inv6()).to.equal('::ffff:192.168.0.1'); | ||
expect(obj.to4in6()).to.equal('::ffff:192.168.0.1'); | ||
}); | ||
it('should generate a 6to4 address', function () { | ||
expect(obj.get6to4().correctForm()).to.equal('2002:c0a8:1::'); | ||
expect(obj.to6to4().correctForm()).to.equal('2002:c0a8:1::'); | ||
}); | ||
it('should generate a v4 address', function () { | ||
expect(obj.tov4().correctForm()).to.equal('192.168.0.1'); | ||
expect(obj.to4().correctForm()).to.equal('192.168.0.1'); | ||
}); | ||
@@ -352,3 +354,3 @@ }); | ||
it('should work with a host address', function () { | ||
var obj = v6.Address.fromURL('2001:db8::5'); | ||
var obj = Address6.fromURL('2001:db8::5'); | ||
@@ -361,3 +363,3 @@ expect(obj.address.valid).to.equal(true); | ||
it('should fail with an invalid URL', function () { | ||
var obj = v6.Address.fromURL('http://zombo/foo'); | ||
var obj = Address6.fromURL('http://zombo/foo'); | ||
@@ -370,3 +372,3 @@ expect(obj.error).to.equal('failed to parse address from URL'); | ||
it('should work with a basic URL', function () { | ||
var obj = v6.Address.fromURL('http://2001:db8::5/foo'); | ||
var obj = Address6.fromURL('http://2001:db8::5/foo'); | ||
@@ -379,3 +381,3 @@ expect(obj.address.isValid()).to.equal(true); | ||
it('should work with a basic URL enclosed in brackets', function () { | ||
var obj = v6.Address.fromURL('http://[2001:db8::5]/foo'); | ||
var obj = Address6.fromURL('http://[2001:db8::5]/foo'); | ||
@@ -388,3 +390,3 @@ expect(obj.address.isValid()).to.equal(true); | ||
it('should work with a URL with a port', function () { | ||
var obj = v6.Address.fromURL('http://[2001:db8::5]:80/foo'); | ||
var obj = Address6.fromURL('http://[2001:db8::5]:80/foo'); | ||
@@ -397,3 +399,3 @@ expect(obj.address.isValid()).to.equal(true); | ||
it('should work with a URL with a long port number', function () { | ||
var obj = v6.Address.fromURL('http://[2001:db8::5]:65536/foo'); | ||
var obj = Address6.fromURL('http://[2001:db8::5]:65536/foo'); | ||
@@ -406,3 +408,3 @@ expect(obj.address.isValid()).to.equal(true); | ||
it('should work with a address with a port', function () { | ||
var obj = v6.Address.fromURL('[2001:db8::5]:80'); | ||
var obj = Address6.fromURL('[2001:db8::5]:80'); | ||
@@ -415,3 +417,3 @@ expect(obj.address.isValid()).to.equal(true); | ||
it('should work with an address with a long port', function () { | ||
var obj = v6.Address.fromURL('[2001:db8::5]:65536'); | ||
var obj = Address6.fromURL('[2001:db8::5]:65536'); | ||
@@ -424,3 +426,3 @@ expect(obj.address.isValid()).to.equal(true); | ||
it('should parse the address but fail with an invalid port', function () { | ||
var obj = v6.Address.fromURL('[2001:db8::5]:65537'); | ||
var obj = Address6.fromURL('[2001:db8::5]:65537'); | ||
@@ -434,3 +436,3 @@ expect(obj.address.isValid()).to.equal(true); | ||
function () { | ||
var obj = v6.Address.fromURL('[2001:db8:z:5]:65536'); | ||
var obj = Address6.fromURL('[2001:db8:z:5]:65536'); | ||
@@ -443,3 +445,3 @@ expect(obj.error).to.equal('failed to parse address with port'); | ||
describe('An address from a BigInteger', function () { | ||
var topic = v6.Address | ||
var topic = Address6 | ||
.fromBigInteger(new BigInteger('51923840109643282840007714694758401')); | ||
@@ -459,3 +461,3 @@ | ||
describe('href', function () { | ||
var topic = new v6.Address('2001:4860:4001:803::1011'); | ||
var topic = new Address6('2001:4860:4001:803::1011'); | ||
@@ -469,3 +471,3 @@ it('should generate a URL correctly', function () { | ||
describe('link', function () { | ||
var topic = new v6.Address('2001:4860:4001:803::1011'); | ||
var topic = new Address6('2001:4860:4001:803::1011'); | ||
@@ -483,3 +485,3 @@ it('should generate an anchor correctly', function () { | ||
it('should generate a v4inv6 anchor correctly', function () { | ||
var topic4 = new v6.Address('::ffff:c0a8:1'); | ||
var topic4 = new Address6('::ffff:c0a8:1'); | ||
@@ -494,3 +496,3 @@ topic4.link({v4: true}) | ||
it('should group a fully ellided address', function () { | ||
var topic = new v6.Address('::'); | ||
var topic = new Address6('::'); | ||
@@ -503,3 +505,3 @@ topic.group() | ||
it('should group an address with no ellision', function () { | ||
var topic = new v6.Address('a:b:c:d:1:2:3:4'); | ||
var topic = new Address6('a:b:c:d:1:2:3:4'); | ||
@@ -518,3 +520,3 @@ topic.group() | ||
it('should group an ellided address', function () { | ||
var topic = new v6.Address('2001:4860:4001:803::1011'); | ||
var topic = new Address6('2001:4860:4001:803::1011'); | ||
@@ -532,3 +534,3 @@ topic.group() | ||
it('should group an IPv4 address', function () { | ||
var topic = new v6.Address('192.168.0.1'); | ||
var topic = new Address6('192.168.0.1'); | ||
@@ -535,0 +537,0 @@ topic.group().should.equal( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
897698
35
11728
104
5
2
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedcli@0.10.0(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedglob@5.0.15(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedcli@0.6.6(transitive)
- Removedglob@3.2.11(transitive)
- Removedlru-cache@2.7.3(transitive)
- Removedminimatch@0.3.0(transitive)
- Removedsigmund@1.0.1(transitive)
Updatedcli@0.10.x
Updatedlodash.find@^3.2.1
Updatedlodash.merge@^3.3.2