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

ip-cidr

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ip-cidr - npm Package Compare versions

Comparing version 2.0.17 to 2.1.0

7

index.d.ts

@@ -36,3 +36,4 @@ import { Address4, Address6 } from "ip-address";

type: "bigInteger" | "addressObject",
from: number | BigInteger;
from: string | number | BigInteger;
to: string | number | BigInteger;
limit: number | BigInteger;

@@ -44,4 +45,4 @@ }

to: BigInteger;
limit: number;
length: number;
limit: BigInteger;
length: BigInteger;
}

@@ -48,0 +49,0 @@ }

@@ -6,2 +6,8 @@ "use strict";

function createAddress(val) {
val.match(/:.\./) && (val = val.split(':').pop());
const ipAddressType = val.match(":")? ipAddress.Address6: ipAddress.Address4;
return new ipAddressType(val);
}
class IPCIDR {

@@ -14,5 +20,3 @@ constructor(cidr) {

cidr.match(/:.\./) && (cidr = cidr.split(':').pop());
let ipAddressType = cidr.match(":")? ipAddress.Address6: ipAddress.Address4;
let address = new ipAddressType(cidr);
const address = createAddress(cidr);
this._isValid = !!(address.isValid() && cidr.match('/'));

@@ -24,4 +28,4 @@

this.cidr = cidr;
this.ipAddressType = ipAddressType;
this.cidr = address.address;
this.ipAddressType = address.constructor;
this.address = address;

@@ -89,7 +93,7 @@ this.addressStart = address.startAddress();

options = options || {};
let list = [];
let start = this.addressStart.bigInteger();
let end = this.addressEnd.bigInteger();
let length = end.subtract(start).add(new BigInteger('1'));
let info = this.getChunkInfo(length, options);
const list = [];
const start = this.addressStart.bigInteger();
const end = this.addressEnd.bigInteger();
const length = end.subtract(start).add(new BigInteger('1'));
const info = this.getChunkInfo(length, options);

@@ -101,4 +105,4 @@ if(results) {

this.loopInfo(info, (val) => {
let num = start.add(val);
let ip = this.formatIP(this.ipAddressType.fromBigInteger(num), options);
const num = start.add(val);
const ip = this.formatIP(this.ipAddressType.fromBigInteger(num), options);
list.push(ip);

@@ -112,8 +116,7 @@ });

options = options || {};
let promise = [];
let start = this.addressStart.bigInteger();
let end = this.addressEnd.bigInteger();
let length = end.subtract(start).add(new BigInteger('1'));
let info = this.getChunkInfo(length, options);
const promise = [];
const start = this.addressStart.bigInteger();
const end = this.addressEnd.bigInteger();
const length = end.subtract(start).add(new BigInteger('1'));
const info = this.getChunkInfo(length, options);

@@ -125,4 +128,4 @@ if(results) {

this.loopInfo(info, (val) => {
let num = start.add(val);
let ip = this.formatIP(this.ipAddressType.fromBigInteger(num), options);
const num = start.add(val);
const ip = this.formatIP(this.ipAddressType.fromBigInteger(num), options);
promise.push(fn(ip));

@@ -145,22 +148,27 @@ });

let from = options.from;
let limit = options.limit
let to, maxLength;
let limit = options.limit;
let to = options.to;
let maxLength;
const addressBigInteger = this.address.bigInteger();
if(from !== undefined) {
if(typeof from != 'object') {
from = new BigInteger(from + '');
function getBigInteger(val) {
if(typeof val == 'string' && val.match(/:|\./)) {
return createAddress(val).bigInteger().subtract(addressBigInteger);
}
else if(typeof val != 'object') {
return new BigInteger(val + '');
}
return val;
}
else {
from = new BigInteger('0');
}
if(limit !== undefined) {
if(typeof limit != 'object') {
limit = new BigInteger(limit + '');
}
from = getBigInteger(from !== undefined? from: 0);
if(to !== undefined) {
to = getBigInteger(to);
limit = to.subtract(from);
}
else {
limit = length;
}
limit = limit !== undefined? getBigInteger(limit): length;
}

@@ -174,3 +182,2 @@ maxLength = length.subtract(from);

to = from.add(limit);
return {

@@ -177,0 +184,0 @@ from: from,

{
"name": "ip-cidr",
"version": "2.0.17",
"version": "2.1.0",
"description": "Module for working with CIDR (v4, v6)",

@@ -5,0 +5,0 @@ "main": "./index.js",

@@ -11,2 +11,3 @@ # Install

const IPCIDR = require("ip-cidr");
const BigInteger = require("jsbn").BigInteger;
const cidr = new IPCIDR("50.165.190.0/23");

@@ -30,2 +31,9 @@

// get an array by chunks using from/limit
cidr.toArray({ from: 1, limit: new BigInteger('2') });
// get an array by chunks using from/to
cidr.toArray({ from: new BigInteger('1'), to: 3 });
cidr.toArray({ from: '50.165.190.1', to: '50.165.190.3' });
// get an array of start and end ip addresses as a string [startIpAsString, endIpAsString]

@@ -62,4 +70,3 @@ cidr.toRange();

to convert the cidr to an array with all ip addresses in the range
you can get information by chunks using **options.from** and **options.limit**
the options might be an integer or a big integer("jsbn" instance)
you can get information by chunks using **options.from/options.limit** or **options.from/options.to**
you can pass the second argument "results" (object) to get all chunk pagination information

@@ -66,0 +73,0 @@

@@ -8,9 +8,9 @@ "use strict";

let validCIDR = '5.5.5.8/29';
let validCIDRMapped = '5.5.5.8/29';
let validCIDRClear = '5.5.5.8';
let validCIDRStart = '5.5.5.8';
let validCIDREnd = '5.5.5.15';
const validCIDR = '5.5.5.8/29';
const validCIDRMapped = '5.5.5.8/29';
const validCIDRClear = '5.5.5.8';
const validCIDRStart = '5.5.5.8';
const validCIDREnd = '5.5.5.15';
let validRange = [
const validRange = [
'5.5.5.8',

@@ -29,3 +29,3 @@ '5.5.5.9',

it('should be valid', function () {
let cidr = new IPCIDR(validCIDR);
const cidr = new IPCIDR(validCIDR);
assert.isTrue(cidr.isValid());

@@ -35,3 +35,3 @@ });

it('should be valid v6', function () {
let cidr = new IPCIDR('2001:db8::/120');
const cidr = new IPCIDR('2001:db8::/120');
assert.isTrue(cidr.isValid(), 'check the status');

@@ -43,3 +43,3 @@ assert.equal(cidr.addressStart.addressMinusSuffix, '2001:0db8:0000:0000:0000:0000:0000:0000', 'check the start');

it('should be valid mapped cidr', function () {
let cidr = new IPCIDR('::FFFF:' + validCIDRMapped);
const cidr = new IPCIDR('::FFFF:' + validCIDRMapped);
assert.isTrue(cidr.isValid(), 'check the status');

@@ -52,3 +52,3 @@ assert.equal(cidr.addressStart.addressMinusSuffix, validCIDRStart, 'check the start');

it('should be invalid', function () {
let cidr = new IPCIDR('192.168.1.1');
const cidr = new IPCIDR('192.168.1.1');
assert.isFalse(cidr.isValid());

@@ -60,3 +60,3 @@ });

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

@@ -66,3 +66,3 @@ });

it('check as big integer', function () {
let cidr = new IPCIDR(validCIDR);
const cidr = new IPCIDR(validCIDR);
assert.equal(JSON.stringify(cidr.address.bigInteger()), JSON.stringify(cidr.formatIP(cidr.address, { type: "bigInteger" })));

@@ -72,3 +72,3 @@ });

it('check as object', function () {
let cidr = new IPCIDR(validCIDR);
const cidr = new IPCIDR(validCIDR);
assert.strictEqual(cidr.address, cidr.formatIP(cidr.address, { type: "addressObject" }));

@@ -81,3 +81,3 @@ });

it('should be true', function () {
let cidr = new IPCIDR(validCIDR);
const cidr = new IPCIDR(validCIDR);
assert.isTrue(cidr.contains('5.5.5.15'));

@@ -87,3 +87,3 @@ });

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

@@ -95,3 +95,3 @@ });

it('should be true', function () {
let cidr = new IPCIDR(validCIDR);
const cidr = new IPCIDR(validCIDR);
assert.isTrue(cidr.contains(new BigInteger('84215055')));

@@ -101,3 +101,3 @@ });

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

@@ -109,3 +109,3 @@ });

it('should be true', function () {
let cidr = new IPCIDR(validCIDR);
const cidr = new IPCIDR(validCIDR);
assert.isTrue(cidr.contains(new ipAddress.Address4('5.5.5.15')));

@@ -115,3 +115,3 @@ });

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

@@ -124,3 +124,3 @@ });

it('.start()', function () {
let cidr = new IPCIDR(validCIDR);
const cidr = new IPCIDR(validCIDR);
assert.equal(cidr.start(), validCIDRStart);

@@ -130,3 +130,3 @@ });

it('.end()', function () {
let cidr = new IPCIDR(validCIDR);
const cidr = new IPCIDR(validCIDR);
assert.equal(cidr.end(), validCIDREnd);

@@ -136,3 +136,3 @@ });

it('.toString()', function () {
let cidr = new IPCIDR(validCIDR);
const cidr = new IPCIDR(validCIDR);
assert.equal(cidr.toString(), validCIDR);

@@ -142,4 +142,4 @@ });

it('.toRange()', function () {
let cidr = new IPCIDR(validCIDR);
let range = cidr.toRange();
const cidr = new IPCIDR(validCIDR);
const range = cidr.toRange();
assert.equal(range[0], validCIDRStart);

@@ -150,4 +150,4 @@ assert.equal(range[1], validCIDREnd);

it('.toObject()', function () {
let cidr = new IPCIDR(validCIDR);
let obj = cidr.toObject();
const cidr = new IPCIDR(validCIDR);
const obj = cidr.toObject();
assert.equal(obj.start, validCIDRStart);

@@ -160,16 +160,64 @@ assert.equal(obj.end, validCIDREnd);

it('should return the full array', function () {
let cidr = new IPCIDR(validCIDR);
let array = cidr.toArray();
const cidr = new IPCIDR(validCIDR);
const array = cidr.toArray();
assert.equal(JSON.stringify(array), JSON.stringify(validRange));
});
it('should return a part of the range', function () {
let cidr = new IPCIDR(validCIDR);
let results = {};
let options = { from: 3, limit: new BigInteger('10') };
let array = cidr.toArray(options, results);
it('should return an empty array with from/limit', function () {
const cidr = new IPCIDR(validCIDR);
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 });
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);
assert.equal(results.from.intValue(), options.from);
assert.equal(results.to.intValue(), results.length.intValue());
assert.equal(array.length, 5);
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: new BigInteger('3'), limit: new BigInteger('2') };
const array = cidr.toArray(options, results);
assert.equal(results.from.intValue(), +options.from.toString());
assert.equal(results.limit.intValue(), +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 = {};
const options = { from: 3, to: 5 };
const array = cidr.toArray(options, results);
assert.equal(results.from.intValue(), options.from);
assert.equal(results.to.intValue(), options.to);
assert.equal(array[0], validRange[options.from]);
assert.equal(array[1], validRange[options.to - 1]);
assert.lengthOf(array, 2);
});
it('should return a part of the range with from/to and strings', function () {
const cidr = new IPCIDR(validCIDR);
const results = {};
const from = 3;
const to = 5;
const options = { from: validRange[from], to: validRange[to] };
const array = cidr.toArray(options, results);
assert.equal(results.from.intValue(), from);
assert.equal(results.to.intValue(), to);
assert.equal(array[0], validRange[from]);
assert.equal(array[1], validRange[to - 1]);
assert.lengthOf(array, 2);
});
});

@@ -179,3 +227,3 @@

it('should read the full range', function () {
let cidr = new IPCIDR(validCIDR);
const cidr = new IPCIDR(validCIDR);
let counter = 0;

@@ -192,6 +240,6 @@

it('should read a part of the range', function () {
let cidr = new IPCIDR(validCIDR);
const cidr = new IPCIDR(validCIDR);
let counter = 1;
let results = {};
let options = {from: counter, limit: 2 };
const results = {};
const options = { from: counter, limit: 2 };

@@ -198,0 +246,0 @@ return cidr.loop((ip) => {

Sorry, the diff of this file is too big to display

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