Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ip6

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ip6 - npm Package Compare versions

Comparing version 0.2.1 to 0.2.3

65

ip6-es.js

@@ -5,5 +5,4 @@ /**

export const normalize = function (a) {
if (!_validate(a)) {
throw new Error('Invalid address: ' + a);
}
validate(a);
a = a.toLowerCase()

@@ -17,3 +16,3 @@

let sections = [];
if (nh.length == 1) {
if (nh.length === 1) {
// full mode

@@ -24,3 +23,3 @@ sections = a.split(/\:/g);

}
} else if (nh.length == 2) {
} else if (nh.length === 2) {
// compact mode

@@ -48,5 +47,3 @@ const n = nh[0];

export const abbreviate = function (a) {
if (!_validate(a)) {
throw new Error('Invalid address: ' + a);
}
validate(a);
a = normalize(a);

@@ -103,4 +100,30 @@ a = a.replace(/0000/g, 'g');

// Basic validation
const _validate = function (a) {
return /^[a-f0-9\\:]+$/ig.test(a);
export const validate = function (a) {
const ns = [];
const nh = a.split('::');
if (nh.length > 2) {
throw new Error('Invalid address: ' + a);
} else if (nh.length === 2) {
if (nh[0].startsWith(':') || nh[0].endsWith(':') || nh[1].startsWith(':') || nh[1].endsWith(':')) {
throw new Error('Invalid address: ' + a);
}
ns.push(... (nh[0].split(':').filter(a => a)));
ns.push(... (nh[1].split(':').filter(a => a)));
if (ns > 7) {
throw new Error('Invalid address: ' + a);
}
} else if (nh.length === 1) {
ns.push(... (nh[0].split(':').filter(a => a)));
if (ns.length > 8) {
throw new Error('Invalid address: ' + a);
}
}
for (const n of ns) {
const match = n.match(/^[a-f0-9]{1,4}$/i);
if (match?.[0] !== n) {
throw new Error('Invalid address: ' + a);
}
}
};

@@ -144,5 +167,3 @@

export const divideSubnet = function (addr, mask0, mask1, limit, abbr) {
if (!_validate(addr)) {
throw new Error('Invalid address: ' + addr);
}
validate(addr);
mask0 *= 1;

@@ -182,5 +203,3 @@ mask1 *= 1;

export const range = function (addr, mask0, mask1, abbr) {
if (!_validate(addr)) {
throw new Error('Invalid address: ' + addr);
}
validate(addr);
mask0 *= 1;

@@ -213,5 +232,7 @@ mask1 *= 1;

export const rangeBigInt = function (addr, mask0, mask1, abbr) {
if (!_validate(addr)) {
throw new Error('Invalid address: ' + addr);
if (typeof BigInt === 'undefined') {
return range(addr, mask0, mask1, abbr);
}
validate(addr);
mask0 *= 1;

@@ -244,5 +265,3 @@ mask1 *= 1;

export const randomSubnet = function (addr, mask0, mask1, limit, abbr) {
if (!_validate(addr)) {
throw new Error('Invalid address: ' + addr);
}
validate(addr);
mask0 *= 1;

@@ -283,5 +302,3 @@ mask1 *= 1;

export const ptr = function (addr, mask) {
if (!_validate(addr)) {
throw new Error('Invalid address: ' + addr);
}
validate(addr);
mask *= 1;

@@ -288,0 +305,0 @@ if (mask < 1 || mask > 128 || Math.floor(mask / 4) != mask / 4) {

@@ -5,5 +5,4 @@ /**

const normalize = function (a) {
if (!_validate(a)) {
throw new Error('Invalid address: ' + a);
}
validate(a);
a = a.toLowerCase()

@@ -17,3 +16,3 @@

let sections = [];
if (nh.length == 1) {
if (nh.length === 1) {
// full mode

@@ -24,3 +23,3 @@ sections = a.split(/\:/g);

}
} else if (nh.length == 2) {
} else if (nh.length === 2) {
// compact mode

@@ -48,5 +47,3 @@ const n = nh[0];

const abbreviate = function (a) {
if (!_validate(a)) {
throw new Error('Invalid address: ' + a);
}
validate(a);
a = normalize(a);

@@ -103,4 +100,30 @@ a = a.replace(/0000/g, 'g');

// Basic validation
const _validate = function (a) {
return /^[a-f0-9\\:]+$/ig.test(a);
const validate = function (a) {
const ns = [];
const nh = a.split('::');
if (nh.length > 2) {
throw new Error('Invalid address: ' + a);
} else if (nh.length === 2) {
if (nh[0].startsWith(':') || nh[0].endsWith(':') || nh[1].startsWith(':') || nh[1].endsWith(':')) {
throw new Error('Invalid address: ' + a);
}
ns.push(... (nh[0].split(':').filter(a => a)));
ns.push(... (nh[1].split(':').filter(a => a)));
if (ns > 7) {
throw new Error('Invalid address: ' + a);
}
} else if (nh.length === 1) {
ns.push(... (nh[0].split(':').filter(a => a)));
if (ns.length > 8) {
throw new Error('Invalid address: ' + a);
}
}
for (const n of ns) {
const match = n.match(/^[a-f0-9]{1,4}$/i);
if (match?.[0] !== n) {
throw new Error('Invalid address: ' + a);
}
}
};

@@ -144,5 +167,3 @@

const divideSubnet = function (addr, mask0, mask1, limit, abbr) {
if (!_validate(addr)) {
throw new Error('Invalid address: ' + addr);
}
validate(addr);
mask0 *= 1;

@@ -182,5 +203,3 @@ mask1 *= 1;

const range = function (addr, mask0, mask1, abbr) {
if (!_validate(addr)) {
throw new Error('Invalid address: ' + addr);
}
validate(addr);
mask0 *= 1;

@@ -213,5 +232,7 @@ mask1 *= 1;

const rangeBigInt = function (addr, mask0, mask1, abbr) {
if (!_validate(addr)) {
throw new Error('Invalid address: ' + addr);
if (typeof BigInt === 'undefined') {
return range(addr, mask0, mask1, abbr);
}
validate(addr);
mask0 *= 1;

@@ -244,5 +265,3 @@ mask1 *= 1;

const randomSubnet = function (addr, mask0, mask1, limit, abbr) {
if (!_validate(addr)) {
throw new Error('Invalid address: ' + addr);
}
validate(addr);
mask0 *= 1;

@@ -283,5 +302,3 @@ mask1 *= 1;

const ptr = function (addr, mask) {
if (!_validate(addr)) {
throw new Error('Invalid address: ' + addr);
}
validate(addr);
mask *= 1;

@@ -297,2 +314,3 @@ if (mask < 1 || mask > 128 || Math.floor(mask / 4) != mask / 4) {

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
exports.validate = validate;
exports.normalize = normalize;

@@ -306,9 +324,10 @@ exports.abbreviate = abbreviate;

} else {
window.normalize = normalize;
window.abbreviate = abbreviate;
window.divideSubnet = divideSubnet;
window.range = range;
window.rangeBigInt = rangeBigInt;
window.randomSubnet = randomSubnet;
window.ptr = ptr;
window.ip6_validate = validate;
window.ip6_normalize = normalize;
window.ip6_abbreviate = abbreviate;
window.ip6_divideSubnet = divideSubnet;
window.ip6_range = range;
window.ip6_rangeBigInt = rangeBigInt;
window.ip6_randomSubnet = randomSubnet;
window.ip6_ptr = ptr;
}
/**
* Created by elgs on 3/5/16.
*/
; (function () {
'use strict';
const ip6 = require('./ip6.js');
let ip6 = require('./ip6.js');
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
describe('To validate IPv6 addresses', function () {
it('should validate IPv6 addresses', function () {
expect(function () { ip6.validate('1111::11111') }).toThrow();
expect(function () { ip6.validate('1111:::11111') }).toThrow();
expect(function () { ip6.validate('1111::1111:') }).toThrow();
expect(function () { ip6.validate(':1111::1111') }).toThrow();
expect(function () { ip6.validate(':1111::1111:') }).toThrow();
expect(function () { ip6.validate('1:1:1:1:1:1:1:1:1') }).toThrow();
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');
});
expect(function () { ip6.validate('1111::1111') }).not.toThrow();
});
});
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 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 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 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');
});
});
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');
});
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 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 /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');
});
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 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 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');
});
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);
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');
});
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.1",
"version": "0.2.3",
"description": "IPv6 address helper utilities.",

@@ -23,3 +23,6 @@ "main": "ip6.js",

},
"homepage": "https://github.com/elgs/ip6#readme"
"homepage": "https://github.com/elgs/ip6#readme",
"devDependencies": {
"jasmine": "^3.7.0"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc