Comparing version 0.2.0 to 0.2.1
@@ -205,2 +205,32 @@ /** | ||
export const rangeBigInt = function (addr, mask0, mask1, abbr) { | ||
if (!_validate(addr)) { | ||
throw new Error('Invalid address: ' + addr); | ||
} | ||
mask0 *= 1; | ||
mask1 *= 1; | ||
mask1 = mask1 || 128; | ||
if (mask0 < 1 || mask1 < 1 || mask0 > 128 || mask1 > 128 || mask0 > mask1) { | ||
throw new Error('Invalid masks.'); | ||
} | ||
const binAddr = _addr2bin(addr); | ||
const binNetPart = binAddr.substr(0, mask0); | ||
const binHostPart = '0'.repeat(128 - mask1); | ||
const binStartAddr = binNetPart + '0'.repeat(mask1 - mask0) + binHostPart; | ||
const binEndAddr = binNetPart + '1'.repeat(mask1 - mask0) + binHostPart; | ||
if (!!abbr) { | ||
return { | ||
start: abbreviate(_bin2addr(binStartAddr)), | ||
end: abbreviate(_bin2addr(binEndAddr)), | ||
size: BigInt(2 ** (mask1 - mask0)).toString() | ||
}; | ||
} else { | ||
return { | ||
start: _bin2addr(binStartAddr), | ||
end: _bin2addr(binEndAddr), | ||
size: BigInt(2 ** (mask1 - mask0)).toString() | ||
}; | ||
} | ||
}; | ||
export const randomSubnet = function (addr, mask0, mask1, limit, abbr) { | ||
@@ -207,0 +237,0 @@ if (!_validate(addr)) { |
32
ip6.js
@@ -205,2 +205,32 @@ /** | ||
const rangeBigInt = function (addr, mask0, mask1, abbr) { | ||
if (!_validate(addr)) { | ||
throw new Error('Invalid address: ' + addr); | ||
} | ||
mask0 *= 1; | ||
mask1 *= 1; | ||
mask1 = mask1 || 128; | ||
if (mask0 < 1 || mask1 < 1 || mask0 > 128 || mask1 > 128 || mask0 > mask1) { | ||
throw new Error('Invalid masks.'); | ||
} | ||
const binAddr = _addr2bin(addr); | ||
const binNetPart = binAddr.substr(0, mask0); | ||
const binHostPart = '0'.repeat(128 - mask1); | ||
const binStartAddr = binNetPart + '0'.repeat(mask1 - mask0) + binHostPart; | ||
const binEndAddr = binNetPart + '1'.repeat(mask1 - mask0) + binHostPart; | ||
if (!!abbr) { | ||
return { | ||
start: abbreviate(_bin2addr(binStartAddr)), | ||
end: abbreviate(_bin2addr(binEndAddr)), | ||
size: BigInt(2 ** (mask1 - mask0)).toString() | ||
}; | ||
} else { | ||
return { | ||
start: _bin2addr(binStartAddr), | ||
end: _bin2addr(binEndAddr), | ||
size: BigInt(2 ** (mask1 - mask0)).toString() | ||
}; | ||
} | ||
}; | ||
const randomSubnet = function (addr, mask0, mask1, limit, abbr) { | ||
@@ -262,2 +292,3 @@ if (!_validate(addr)) { | ||
exports.range = range; | ||
exports.rangeBigInt = rangeBigInt; | ||
exports.randomSubnet = randomSubnet; | ||
@@ -270,4 +301,5 @@ exports.ptr = ptr; | ||
window.range = range; | ||
window.rangeBigInt = rangeBigInt; | ||
window.randomSubnet = randomSubnet; | ||
window.ptr = ptr; | ||
} |
159
ip6Spec.js
/** | ||
* Created by elgs on 3/5/16. | ||
*/ | ||
;(function () { | ||
'use strict'; | ||
; (function () { | ||
'use strict'; | ||
let ip6 = require('./ip6.js'); | ||
let ip6 = require('./ip6.js'); | ||
//////////////////////////////////////////////////////////////////////////// | ||
//////////////////////////////////////////////////////////////////////////// | ||
describe('To normalize IPv6 addresses', function () { | ||
it('should normalize IPv6 addresses', function () { | ||
expect(ip6.normalize('2404:6800:4003:808::200e')).toBe('2404:6800:4003:0808:0000:0000:0000:200e'); | ||
expect(ip6.normalize('2404:6800:4003:0808:0000:0000:0000:200e')).toBe('2404:6800:4003:0808:0000:0000:0000:200e'); | ||
expect(ip6.normalize('2404:6800:4003:808::')).toBe('2404:6800:4003:0808:0000:0000:0000:0000'); | ||
expect(ip6.normalize('2404:68::')).toBe('2404:0068:0000:0000:0000:0000:0000:0000'); | ||
expect(ip6.normalize('2404:6800:4003:0808:0:0:0:200e')).toBe('2404:6800:4003:0808:0000:0000:0000:200e'); | ||
expect(ip6.normalize('::1')).toBe('0000:0000:0000:0000:0000:0000:0000:0001'); | ||
expect(ip6.normalize('::')).toBe('0000:0000:0000:0000:0000:0000:0000:0000'); | ||
expect(ip6.normalize('2607:5300:60:465c:0000:0000:0000::')).toBe('2607:5300:0060:465c:0000:0000:0000:0000'); | ||
expect(ip6.normalize('AB:00AB::E')).toBe('00ab:00ab:0000:0000:0000:0000:0000:000e'); | ||
}); | ||
}); | ||
describe('To normalize IPv6 addresses', function () { | ||
it('should normalize IPv6 addresses', function () { | ||
expect(ip6.normalize('2404:6800:4003:808::200e')).toBe('2404:6800:4003:0808:0000:0000:0000:200e'); | ||
expect(ip6.normalize('2404:6800:4003:0808:0000:0000:0000:200e')).toBe('2404:6800:4003:0808:0000:0000:0000:200e'); | ||
expect(ip6.normalize('2404:6800:4003:808::')).toBe('2404:6800:4003:0808:0000:0000:0000:0000'); | ||
expect(ip6.normalize('2404:68::')).toBe('2404:0068:0000:0000:0000:0000:0000:0000'); | ||
expect(ip6.normalize('2404:6800:4003:0808:0:0:0:200e')).toBe('2404:6800:4003:0808:0000:0000:0000:200e'); | ||
expect(ip6.normalize('::1')).toBe('0000:0000:0000:0000:0000:0000:0000:0001'); | ||
expect(ip6.normalize('::')).toBe('0000:0000:0000:0000:0000:0000:0000:0000'); | ||
expect(ip6.normalize('2607:5300:60:465c:0000:0000:0000::')).toBe('2607:5300:0060:465c:0000:0000:0000:0000'); | ||
expect(ip6.normalize('AB:00AB::E')).toBe('00ab:00ab:0000:0000:0000:0000:0000:000e'); | ||
}); | ||
}); | ||
describe('To abbreviate IPv6 addresses.', function () { | ||
it('should abbreviate IPv6 addresses', function () { | ||
expect(ip6.abbreviate('2001:0000:0111:0000:0011:0000:0001:0000')).toBe('2001:0:111:0:11:0:1:0'); | ||
expect(ip6.abbreviate('2001:0001:0000:0001:0000:0000:0000:0001')).toBe('2001:1:0:1::1'); | ||
expect(ip6.abbreviate('2001:0001:0000:0001:0000:0000:0000:0000')).toBe('2001:1:0:1::'); | ||
expect(ip6.abbreviate('0000:0000:0000:0000:0000:0000:0000:0000')).toBe('::'); | ||
expect(ip6.abbreviate('0000:0000:0000:0000:0000:0000:0000:0001')).toBe('::1'); | ||
expect(ip6.abbreviate('2041:0000:140F:0000:0000:0000:875B:131B')).toBe('2041:0:140f::875b:131b'); | ||
expect(ip6.abbreviate('2001:0001:0002:0003:0004:0005:0006:0007')).toBe('2001:1:2:3:4:5:6:7'); | ||
expect(ip6.abbreviate('2001:0000:0000:0000:1111:0000:0000:0000')).toBe('2001::1111:0:0:0'); | ||
expect(ip6.abbreviate('2001:db8:0:0:0:0:2:1')).toBe('2001:db8::2:1'); | ||
}); | ||
}); | ||
describe('To abbreviate IPv6 addresses.', function () { | ||
it('should abbreviate IPv6 addresses', function () { | ||
expect(ip6.abbreviate('2001:0000:0111:0000:0011:0000:0001:0000')).toBe('2001:0:111:0:11:0:1:0'); | ||
expect(ip6.abbreviate('2001:0001:0000:0001:0000:0000:0000:0001')).toBe('2001:1:0:1::1'); | ||
expect(ip6.abbreviate('2001:0001:0000:0001:0000:0000:0000:0000')).toBe('2001:1:0:1::'); | ||
expect(ip6.abbreviate('0000:0000:0000:0000:0000:0000:0000:0000')).toBe('::'); | ||
expect(ip6.abbreviate('0000:0000:0000:0000:0000:0000:0000:0001')).toBe('::1'); | ||
expect(ip6.abbreviate('2041:0000:140F:0000:0000:0000:875B:131B')).toBe('2041:0:140f::875b:131b'); | ||
expect(ip6.abbreviate('2001:0001:0002:0003:0004:0005:0006:0007')).toBe('2001:1:2:3:4:5:6:7'); | ||
expect(ip6.abbreviate('2001:0000:0000:0000:1111:0000:0000:0000')).toBe('2001::1111:0:0:0'); | ||
expect(ip6.abbreviate('2001:db8:0:0:0:0:2:1')).toBe('2001:db8::2:1'); | ||
}); | ||
}); | ||
describe('To divide IPv6 subnet.', function () { | ||
it('should divide a /64 into 4 /66 subnets.', function () { | ||
let n66 = ip6.divideSubnet("2607:5300:60:1234::", 64, 66); | ||
expect(n66.length).toBe(4); | ||
expect(n66[0]).toBe('2607:5300:0060:1234:0000:0000:0000:0000'); | ||
expect(n66[1]).toBe('2607:5300:0060:1234:4000:0000:0000:0000'); | ||
expect(n66[2]).toBe('2607:5300:0060:1234:8000:0000:0000:0000'); | ||
expect(n66[3]).toBe('2607:5300:0060:1234:c000:0000:0000:0000'); | ||
}); | ||
describe('To divide IPv6 subnet.', function () { | ||
it('should divide a /64 into 4 /66 subnets.', function () { | ||
let n66 = ip6.divideSubnet("2607:5300:60:1234::", 64, 66); | ||
expect(n66.length).toBe(4); | ||
expect(n66[0]).toBe('2607:5300:0060:1234:0000:0000:0000:0000'); | ||
expect(n66[1]).toBe('2607:5300:0060:1234:4000:0000:0000:0000'); | ||
expect(n66[2]).toBe('2607:5300:0060:1234:8000:0000:0000:0000'); | ||
expect(n66[3]).toBe('2607:5300:0060:1234:c000:0000:0000:0000'); | ||
}); | ||
it('should divide a /64 into 4 /66 subnets, but limit to 2 subnets.', function () { | ||
let n128 = ip6.divideSubnet("2607:5300:60:1234::", 64, 128, 2); | ||
expect(n128.length).toBe(2); | ||
expect(n128[0]).toBe('2607:5300:0060:1234:0000:0000:0000:0000'); | ||
expect(n128[1]).toBe('2607:5300:0060:1234:0000:0000:0000:0001'); | ||
}); | ||
it('should divide a /64 into 4 /66 subnets, but limit to 2 subnets.', function () { | ||
let n128 = ip6.divideSubnet("2607:5300:60:1234::", 64, 128, 2); | ||
expect(n128.length).toBe(2); | ||
expect(n128[0]).toBe('2607:5300:0060:1234:0000:0000:0000:0000'); | ||
expect(n128[1]).toBe('2607:5300:0060:1234:0000:0000:0000:0001'); | ||
}); | ||
it('should divide a /64 into 4 abbreviated /66 subnets.', function () { | ||
let n66 = ip6.divideSubnet("2607:5300:60:1234::", 64, 66, null, true); | ||
expect(n66.length).toBe(4); | ||
expect(n66[0]).toBe('2607:5300:60:1234::'); | ||
expect(n66[1]).toBe('2607:5300:60:1234:4000::'); | ||
expect(n66[2]).toBe('2607:5300:60:1234:8000::'); | ||
expect(n66[3]).toBe('2607:5300:60:1234:c000::'); | ||
}); | ||
}); | ||
it('should divide a /64 into 4 abbreviated /66 subnets.', function () { | ||
let n66 = ip6.divideSubnet("2607:5300:60:1234::", 64, 66, null, true); | ||
expect(n66.length).toBe(4); | ||
expect(n66[0]).toBe('2607:5300:60:1234::'); | ||
expect(n66[1]).toBe('2607:5300:60:1234:4000::'); | ||
expect(n66[2]).toBe('2607:5300:60:1234:8000::'); | ||
expect(n66[3]).toBe('2607:5300:60:1234:c000::'); | ||
}); | ||
}); | ||
describe('To calculate the range of an IPv6 subnet.', function () { | ||
it('should calculate the first and the last address of an IPv6 subnet.', function () { | ||
let n65 = ip6.range("2607:5300:60:1234::", 64, 65, true); | ||
expect(n65.start).toBe('2607:5300:60:1234::'); | ||
expect(n65.end).toBe('2607:5300:60:1234:8000::'); | ||
expect(n65.size).toBe(2); | ||
}); | ||
describe('To calculate the range of an IPv6 subnet.', function () { | ||
it('should calculate the first and the last address of an IPv6 subnet.', function () { | ||
let n65 = ip6.range("2607:5300:60:1234::", 64, 65, true); | ||
expect(n65.start).toBe('2607:5300:60:1234::'); | ||
expect(n65.end).toBe('2607:5300:60:1234:8000::'); | ||
expect(n65.size).toBe(2); | ||
}); | ||
it('should calculate the first and the last address of an IPv6 subnet.', function () { | ||
let n56 = ip6.range("2607:5300:60::", 48, 56); | ||
expect(n56.start).toBe('2607:5300:0060:0000:0000:0000:0000:0000'); | ||
expect(n56.end).toBe('2607:5300:0060:ff00:0000:0000:0000:0000'); | ||
expect(n56.size).toBe(256); | ||
it('should calculate the first and the last address of an IPv6 subnet.', function () { | ||
let n65 = ip6.rangeBigInt("2607:5300:60:1234::", 64, 65, true); | ||
expect(n65.start).toBe('2607:5300:60:1234::'); | ||
expect(n65.end).toBe('2607:5300:60:1234:8000::'); | ||
expect(n65.size).toBe('2'); | ||
}); | ||
let r128 = ip6.randomSubnet("2607:5300:60::", 48, 128, 5, true); | ||
console.log(r128); | ||
}); | ||
}); | ||
it('should calculate the first and the last address of an IPv6 subnet.', function () { | ||
let n56 = ip6.range("2607:5300:60::", 48, 56); | ||
expect(n56.start).toBe('2607:5300:0060:0000:0000:0000:0000:0000'); | ||
expect(n56.end).toBe('2607:5300:0060:ff00:0000:0000:0000:0000'); | ||
expect(n56.size).toBe(256); | ||
describe('To generate PTR records for DNS zone file.', function () { | ||
it('should generate a PTR record for DNS zone file.', function () { | ||
let ptr = ip6.ptr("2607:5300:60:1234:cafe:babe:dead:beef", 64); | ||
expect(ptr).toBe('f.e.e.b.d.a.e.d.e.b.a.b.e.f.a.c'); | ||
}); | ||
}); | ||
let r128 = ip6.randomSubnet("2607:5300:60::", 48, 128, 5, true); | ||
console.log(r128); | ||
}); | ||
}); | ||
describe('To generate PTR records for DNS zone file.', function () { | ||
it('should generate a PTR record for DNS zone file.', function () { | ||
let ptr = ip6.ptr("2607:5300:60:1234:cafe:babe:dead:beef", 64); | ||
expect(ptr).toBe('f.e.e.b.d.a.e.d.e.b.a.b.e.f.a.c'); | ||
}); | ||
}); | ||
})(); |
{ | ||
"name": "ip6", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "IPv6 address helper utilities.", | ||
@@ -5,0 +5,0 @@ "main": "ip6.js", |
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
37224
642