Skip to content

Commit

Permalink
changed jsbn to native BigInt, require -> import
Browse files Browse the repository at this point in the history
  • Loading branch information
ortexx committed Dec 17, 2023
1 parent b5538c9 commit 337ee66
Showing 8 changed files with 2,306 additions and 3,573 deletions.
1,460 changes: 723 additions & 737 deletions dist/ip-cidr.js

Large diffs are not rendered by default.

21 changes: 10 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Address4, Address6 } from "ip-address";
import { BigInteger } from "jsbn";

type Address = Address4 | Address6;

@@ -8,7 +7,7 @@ declare class IPCIDR {
address: Address;
addressStart: Address;
addressEnd: Address;
size: BigInteger;
size: BigInt;

constructor(cidr: string);

@@ -22,7 +21,7 @@ declare class IPCIDR {

getChunkInfo(length: number, options: IPCIDR.FormatOptions): IPCIDR.ChunkInfo;

contains(address: IPCIDR.Address | string | BigInteger): boolean;
contains(address: IPCIDR.Address | string | BigInt): boolean;

toString(): string;

@@ -33,20 +32,20 @@ declare class IPCIDR {

declare namespace IPCIDR {
type Address = Address4 | Address6;
type FormatResult = BigInteger | Address | string;
type FormatResult = BigInt | Address | string;

interface FormatOptions {
type?: "bigInteger" | "addressObject",
from?: string | number | BigInteger;
to?: string | number | BigInteger;
limit?: number | BigInteger;
from?: string | number | BigInt;
to?: string | number | BigInt;
limit?: number | BigInt;
}

interface ChunkInfo {
from: BigInteger;
to: BigInteger;
limit: BigInteger;
length: BigInteger;
from: BigInt;
to: BigInt;
limit: BigInt;
length: BigInt;
}

export function formatIP<T = FormatResult>(address: Address, options?: any): T;
39 changes: 20 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

const ipAddress = require('ip-address');
const BigInteger = require('jsbn').BigInteger;
import ipAddress from 'ip-address';

class IPCIDR {
constructor(cidr) {
@@ -17,21 +16,23 @@ class IPCIDR {
this.addressEnd = address.endAddress();
this.addressStart.subnet = this.addressEnd.subnet = this.address.subnet;
this.addressStart.subnetMask = this.addressEnd.subnetMask = this.address.subnetMask;
this.size = new BigInteger(this.addressEnd.bigInteger().subtract(this.addressStart.bigInteger()).add(new BigInteger('1')).toString());
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)) {
if(typeof address == 'object') {
if(typeof address == 'bigint') {
address = this.ipAddressType.fromBigInteger(address);
}
else {
address = this.constructor.createAddress(address);
}
}

return address.isInSubnet(this.address)
return address.isInSubnet(this.address);
}
catch(err) {
return false;
@@ -66,15 +67,15 @@ class IPCIDR {
const list = [];
const start = this.constructor.formatIP(this.addressStart, { type: 'bigInteger' });
const end = this.constructor.formatIP(this.addressEnd, { type: 'bigInteger' });
const length = end.subtract(start).add(new BigInteger('1'));
const length = end - start + 1n;
const info = this.getChunkInfo(length, options);

if(results) {
Object.assign(results, info);
}

this.loopInfo(info, (val) => {
const num = start.add(val);
const num = start + val;
const ip = this.constructor.formatIP(this.ipAddressType.fromBigInteger(num), options);
list.push(ip);
});
@@ -87,15 +88,15 @@ class IPCIDR {
const promise = [];
const start = this.constructor.formatIP(this.addressStart, { type: 'bigInteger' });
const end = this.constructor.formatIP(this.addressEnd, { type: 'bigInteger' });
const length = end.subtract(start).add(new BigInteger('1'));
const length = end - start + 1n;
const info = this.getChunkInfo(length, options);

if(results) {
Object.assign(results, info);
}

this.loopInfo(info, (val) => {
const num = start.add(val);
const num = start + val;
const ip = this.constructor.formatIP(this.ipAddressType.fromBigInteger(num), options);
promise.push(fn(ip));
});
@@ -106,9 +107,9 @@ class IPCIDR {
loopInfo(info, fn) {
let i = info.from;

while(i.compareTo(info.to) < 0) {
while(i < info.to) {
fn(i);
i = i.add(new BigInteger('1'));
i = i + 1n;
}
}

@@ -121,10 +122,10 @@ class IPCIDR {

const getBigInteger = (val) => {
if(typeof val == 'string' && val.match(/:|\./)) {
return this.constructor.formatIP(this.constructor.createAddress(val), { type: 'bigInteger' }).subtract(addressBigInteger);
return this.constructor.formatIP(this.constructor.createAddress(val), { type: 'bigInteger' }) - addressBigInteger;
}
else if(typeof val != 'object') {
return new BigInteger(val + '');
return BigInt(val + '');
}

return val;
@@ -134,19 +135,19 @@ class IPCIDR {

if(to !== undefined) {
to = getBigInteger(to);
limit = to.subtract(from);
limit = to - from;
}
else {
limit = limit !== undefined? getBigInteger(limit): length;
}

maxLength = length.subtract(from);
maxLength = length - from;

if(limit.compareTo(maxLength) > 0) {
if(limit > maxLength) {
limit = maxLength;
}

to = from.add(limit);
to = from + limit;
return {
from: from,
to: to,
@@ -160,7 +161,7 @@ IPCIDR.formatIP = function(address, options) {
options = options || {};

if (options.type == "bigInteger") {
return new BigInteger(address.bigInteger().toString());
return BigInt(address.bigInteger());
}
else if (options.type == "addressObject") {
return address;
@@ -219,4 +220,4 @@ IPCIDR.isValidCIDR = function (address) {
}
}

module.exports = IPCIDR;
export default IPCIDR;
3 changes: 2 additions & 1 deletion ip-cidr.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
window.IPCIDR = require('./index');
import IPCIDR from './index.js';
window.IPCIDR = IPCIDR;
Loading

0 comments on commit 337ee66

Please sign in to comment.