Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions dist/ip-cidr.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _ipAddress = _interopRequireDefault(require("ip-address"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var ipAddress = _interopRequireWildcard(require("ip-address"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
class IPCIDR {
constructor(cidr) {
if (typeof cidr !== 'string' || !cidr.match('/')) {
Expand All @@ -26,7 +27,7 @@ class IPCIDR {
}
contains(address) {
try {
if (!(address instanceof _ipAddress.default.Address6) && !(address instanceof _ipAddress.default.Address4)) {
if (!(address instanceof ipAddress.Address6) && !(address instanceof ipAddress.Address4)) {
if (typeof address == 'bigint') {
address = this.ipAddressType.fromBigInteger(address);
} else {
Expand Down Expand Up @@ -157,7 +158,7 @@ IPCIDR.createAddress = function (val) {
throw new Error('Invalid IP address.');
}
val.match(/:.\./) && (val = val.split(':').pop());
const ipAddressType = val.match(":") ? _ipAddress.default.Address6 : _ipAddress.default.Address4;
const ipAddressType = val.match(":") ? ipAddress.Address6 : ipAddress.Address4;
let ip = new ipAddressType(val);
if (ip.v4 && val.match(":") && ip.address4) {
ip = ip.address4;
Expand Down
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

import ipAddress from 'ip-address';
import * as ipAddress from 'ip-address';

class IPCIDR {
constructor(cidr) {
Expand All @@ -13,14 +13,14 @@ class IPCIDR {
this.ipAddressType = address.constructor;
this.address = address;
this.addressStart = address.startAddress();
this.addressEnd = address.endAddress();
this.addressEnd = address.endAddress();
this.addressStart.subnet = this.addressEnd.subnet = this.address.subnet;
this.addressStart.subnetMask = this.addressEnd.subnetMask = this.address.subnetMask;
const end = BigInt(this.addressEnd.bigInteger());
const start = BigInt(this.addressStart.bigInteger());
this.size = end - start + 1n;
}

contains(address) {
try {
if(!(address instanceof ipAddress.Address6) && !(address instanceof ipAddress.Address4)) {
Expand All @@ -36,7 +36,7 @@ class IPCIDR {
}
catch(err) {
return false;
}
}
}

start(options) {
Expand Down Expand Up @@ -82,15 +82,15 @@ class IPCIDR {

return list;
}

loop(fn, options, results) {
options = options || {};
const promise = [];
const start = this.constructor.formatIP(this.addressStart, { type: 'bigInteger' });
const end = this.constructor.formatIP(this.addressEnd, { type: 'bigInteger' });
const length = end - start + 1n;
const info = this.getChunkInfo(length, options);

if(results) {
Object.assign(results, info);
}
Expand Down Expand Up @@ -139,14 +139,14 @@ class IPCIDR {
}
else {
limit = limit !== undefined? getBigInteger(limit): length;
}
}

maxLength = length - from;

if(limit > maxLength) {
limit = maxLength;
}

to = from + limit;
return {
from: from,
Expand Down
58 changes: 29 additions & 29 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { assert } from 'chai';
import IPCIDR from '../index.js';
import ipAddress from 'ip-address';
import * as ipAddress from 'ip-address';

const validCIDR = '5.5.5.8/29';
const validCIDRMapped = '5.5.5.8/29';
Expand All @@ -28,16 +28,16 @@ describe('IPCIDR:', function () {
});

it('should be valid v6', function () {
const cidr = new IPCIDR('2001:db8::/120');
assert.equal(cidr.addressStart.addressMinusSuffix, '2001:0db8:0000:0000:0000:0000:0000:0000', 'check the start');
assert.equal(cidr.addressEnd.addressMinusSuffix, '2001:0db8:0000:0000:0000:0000:0000:00ff', 'check the end');
assert.equal(cidr.toArray().length, cidr.size.toString(), 'check the size');
const cidr = new IPCIDR('2001:db8::/120');
assert.equal(cidr.addressStart.addressMinusSuffix, '2001:0db8:0000:0000:0000:0000:0000:0000', 'check the start');
assert.equal(cidr.addressEnd.addressMinusSuffix, '2001:0db8:0000:0000:0000:0000:0000:00ff', 'check the end');
assert.equal(cidr.toArray().length, cidr.size.toString(), 'check the size');
});

it('should be valid mapped cidr', function () {
const cidr = new IPCIDR('::FFFF:' + validCIDRMapped);
assert.equal(cidr.addressStart.addressMinusSuffix, validCIDRStart, 'check the start');
assert.equal(cidr.addressEnd.addressMinusSuffix, validCIDREnd, 'check the end');
const cidr = new IPCIDR('::FFFF:' + validCIDRMapped);
assert.equal(cidr.addressStart.addressMinusSuffix, validCIDRStart, 'check the start');
assert.equal(cidr.addressEnd.addressMinusSuffix, validCIDREnd, 'check the end');
});

it('should be invalid', function () {
Expand All @@ -47,7 +47,7 @@ describe('IPCIDR:', function () {

describe(".formatIP()", function () {
it('check as a string', function () {
const cidr = new IPCIDR(validCIDR);
const cidr = new IPCIDR(validCIDR);
assert.equal(IPCIDR.formatIP(cidr.address), validCIDRClear);
});

Expand All @@ -63,29 +63,29 @@ describe('IPCIDR:', function () {
});

describe(".isValidAddress()", function () {
it('check a wrong address', function () {;
it('check a wrong address', function () {;
assert.isFalse(IPCIDR.isValidAddress('wrong'));
});

it('check an ip address', function () {;
it('check an ip address', function () {;
assert.isTrue(IPCIDR.isValidAddress('1.1.1.1'));
});

it('check CIDR', function () {;
it('check CIDR', function () {;
assert.isTrue(IPCIDR.isValidAddress('1.1.1.1/24'));
});
});

describe(".isValidCIDR()", function () {
it('check a wrong address', function () {;
it('check a wrong address', function () {;
assert.isFalse(IPCIDR.isValidCIDR('wrong'));
});

it('check an ip address', function () {;
it('check an ip address', function () {;
assert.isFalse(IPCIDR.isValidCIDR('1.1.1.1'));
});

it('check CIDR', function () {;
it('check CIDR', function () {;
assert.isTrue(IPCIDR.isValidCIDR('1.1.1.1/24'));
});
});
Expand All @@ -94,17 +94,17 @@ describe('IPCIDR:', function () {
describe("check as a string", function () {
it('should be true', function () {
const cidr = new IPCIDR(validCIDR);
assert.isTrue(cidr.contains('5.5.5.15'));
assert.isTrue(cidr.contains('5.5.5.15'));
});

it('should be true for mapped ip address', function () {
const cidr = new IPCIDR(validCIDR);
const cidr = new IPCIDR(validCIDR);
assert.isTrue(cidr.contains('::ffff:5.5.5.15'));
});

it('should be false', function () {
const cidr = new IPCIDR(validCIDR);
assert.isFalse(cidr.contains('5.5.5.16'));
assert.isFalse(cidr.contains('5.5.5.16'));
});

it('should be false with a random string', function () {
Expand All @@ -115,30 +115,30 @@ describe('IPCIDR:', function () {
it('should be false with octal notation', function () {
const cidr = new IPCIDR('10.0.0.1/8');
assert.isFalse(cidr.contains('010.1.1.1'));
});
});
});

describe("check as a big integer", function () {
it('should be true', function () {
const cidr = new IPCIDR(validCIDR);
assert.isTrue(cidr.contains(BigInt('84215055')));
assert.isTrue(cidr.contains(BigInt('84215055')));
});

it('should be false', function () {
const cidr = new IPCIDR(validCIDR);
assert.isFalse(cidr.contains(BigInt('84215056')));
assert.isFalse(cidr.contains(BigInt('84215056')));
});
});

describe("check as an object", function () {
it('should be true', function () {
const cidr = new IPCIDR(validCIDR);
assert.isTrue(cidr.contains(new ipAddress.Address4('5.5.5.15')));
assert.isTrue(cidr.contains(new ipAddress.Address4('5.5.5.15')));
});

it('should be false', function () {
const cidr = new IPCIDR(validCIDR);
assert.isFalse(cidr.contains(new ipAddress.Address4('5.5.5.16')));
assert.isFalse(cidr.contains(new ipAddress.Address4('5.5.5.16')));
});
});
});
Expand Down Expand Up @@ -183,36 +183,36 @@ describe('IPCIDR:', function () {

it('should return an empty array with from/limit', function () {
const cidr = new IPCIDR(validCIDR);
const array = cidr.toArray({ from: 0, limit: -1 });
const array = cidr.toArray({ from: 0, limit: -1 });
assert.lengthOf(array, 0);
});

it('should return an empty array with from/to', function () {
const cidr = new IPCIDR(validCIDR);
const array = cidr.toArray({ from: 5, to: 3 });
const array = cidr.toArray({ from: 5, to: 3 });
assert.lengthOf(array, 0);
});

it('should return a part of the range with from/limit with numbers', function () {
const cidr = new IPCIDR(validCIDR);
const results = {};
const options = { from: 3, limit: 10 };
const array = cidr.toArray(options, results);
const array = cidr.toArray(options, results);
assert.equal(results.from, options.from);
assert.equal(results.to, results.length);
assert.lengthOf(array, validRange.length - options.from);
});

it('should return a part of the range with from/limit with numbers', function () {
const cidr = new IPCIDR(validCIDR);
const results = {};
const options = { from: BigInt('3'), limit: BigInt('2') };
const array = cidr.toArray(options, results);
const array = cidr.toArray(options, results);
assert.equal(results.from, +options.from.toString());
assert.equal(results.limit, +options.limit.toString());
assert.lengthOf(array, +options.limit.toString());
});

it('should return a part of the range with from/to and numbers', function () {
const cidr = new IPCIDR(validCIDR);
const results = {};
Expand Down