From 76d198736d280c7411bcbfbf57039e9d4e2624e8 Mon Sep 17 00:00:00 2001 From: Olamide Olatunji Date: Sun, 9 Jun 2024 14:50:53 +0300 Subject: [PATCH] fix: use import * as for ip-address as it exports no default --- dist/ip-cidr.js | 9 ++++---- index.js | 18 +++++++-------- test/test.js | 58 ++++++++++++++++++++++++------------------------- 3 files changed, 43 insertions(+), 42 deletions(-) diff --git a/dist/ip-cidr.js b/dist/ip-cidr.js index d7e824f..23e898f 100644 --- a/dist/ip-cidr.js +++ b/dist/ip-cidr.js @@ -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('/')) { @@ -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 { @@ -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; diff --git a/index.js b/index.js index 2fbc00b..27c9b32 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ "use strict"; -import ipAddress from 'ip-address'; +import * as ipAddress from 'ip-address'; class IPCIDR { constructor(cidr) { @@ -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)) { @@ -36,7 +36,7 @@ class IPCIDR { } catch(err) { return false; - } + } } start(options) { @@ -82,7 +82,7 @@ class IPCIDR { return list; } - + loop(fn, options, results) { options = options || {}; const promise = []; @@ -90,7 +90,7 @@ class IPCIDR { 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); } @@ -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, diff --git a/test/test.js b/test/test.js index 1488e24..03e21f5 100644 --- a/test/test.js +++ b/test/test.js @@ -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'; @@ -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 () { @@ -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); }); @@ -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')); }); }); @@ -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 () { @@ -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'))); }); }); }); @@ -183,13 +183,13 @@ 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); }); @@ -197,22 +197,22 @@ describe('IPCIDR:', 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 = {};