ip-address
Advanced tools
Comparing version 9.1.0-0 to 10.0.0
@@ -10,2 +10,7 @@ import { Address4 } from './ipv4'; | ||
export declare function stringToPaddedHex(numberString: string): string; | ||
/** | ||
* @param binaryValue Binary representation of a value (e.g. `10`) | ||
* @param position Byte position, where 0 is the least significant bit | ||
*/ | ||
export declare function testBit(binaryValue: string, position: number): boolean; | ||
//# sourceMappingURL=common.d.ts.map |
@@ -7,2 +7,3 @@ "use strict"; | ||
exports.stringToPaddedHex = stringToPaddedHex; | ||
exports.testBit = testBit; | ||
function isInSubnet(address) { | ||
@@ -34,2 +35,14 @@ if (this.subnetMask < address.subnetMask) { | ||
} | ||
/** | ||
* @param binaryValue Binary representation of a value (e.g. `10`) | ||
* @param position Byte position, where 0 is the least significant bit | ||
*/ | ||
function testBit(binaryValue, position) { | ||
const { length } = binaryValue; | ||
if (position > length) { | ||
return false; | ||
} | ||
const positionInString = length - position; | ||
return binaryValue.substring(positionInString, positionInString + 1) === '1'; | ||
} | ||
//# sourceMappingURL=common.js.map |
import * as common from './common'; | ||
import { BigInteger } from 'jsbn'; | ||
/** | ||
@@ -83,8 +82,8 @@ * Represents an IPv4 address | ||
/** | ||
* Returns the address as a BigInteger | ||
* Returns the address as a `bigint` | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
bigInteger(): BigInteger; | ||
bigInt(): BigInt; | ||
/** | ||
@@ -94,5 +93,5 @@ * Helper function getting start address. | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
_startAddress(): BigInteger; | ||
_startAddress(): bigint; | ||
/** | ||
@@ -118,5 +117,5 @@ * The first address in the range given by this address' subnet. | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
_endAddress(): BigInteger; | ||
_endAddress(): bigint; | ||
/** | ||
@@ -139,9 +138,9 @@ * The last address in the range given by this address' subnet | ||
/** | ||
* Converts a BigInteger to a v4 address object | ||
* Converts a BigInt to a v4 address object | ||
* @memberof Address4 | ||
* @static | ||
* @param {BigInteger} bigInteger - a BigInteger to convert | ||
* @param {bigint} bigInt - a BigInt to convert | ||
* @returns {Address4} | ||
*/ | ||
static fromBigInteger(bigInteger: BigInteger): Address4; | ||
static fromBigInt(bigInt: bigint): Address4; | ||
/** | ||
@@ -148,0 +147,0 @@ * Returns the first n bits of the address, defaulting to the |
@@ -31,3 +31,2 @@ "use strict"; | ||
const address_error_1 = require("./address-error"); | ||
const jsbn_1 = require("jsbn"); | ||
/** | ||
@@ -179,9 +178,9 @@ * Represents an IPv4 address | ||
/** | ||
* Returns the address as a BigInteger | ||
* Returns the address as a `bigint` | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
bigInteger() { | ||
return new jsbn_1.BigInteger(this.parsedAddress.map((n) => common.stringToPaddedHex(n)).join(''), 16); | ||
bigInt() { | ||
return BigInt(`0x${this.parsedAddress.map((n) => common.stringToPaddedHex(n)).join('')}`); | ||
} | ||
@@ -192,6 +191,6 @@ /** | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
_startAddress() { | ||
return new jsbn_1.BigInteger(this.mask() + '0'.repeat(constants.BITS - this.subnetMask), 2); | ||
return BigInt(`0b${this.mask() + '0'.repeat(constants.BITS - this.subnetMask)}`); | ||
} | ||
@@ -206,3 +205,3 @@ /** | ||
startAddress() { | ||
return Address4.fromBigInteger(this._startAddress()); | ||
return Address4.fromBigInt(this._startAddress()); | ||
} | ||
@@ -217,4 +216,4 @@ /** | ||
startAddressExclusive() { | ||
const adjust = new jsbn_1.BigInteger('1'); | ||
return Address4.fromBigInteger(this._startAddress().add(adjust)); | ||
const adjust = BigInt('1'); | ||
return Address4.fromBigInt(this._startAddress() + adjust); | ||
} | ||
@@ -225,6 +224,6 @@ /** | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
_endAddress() { | ||
return new jsbn_1.BigInteger(this.mask() + '1'.repeat(constants.BITS - this.subnetMask), 2); | ||
return BigInt(`0b${this.mask() + '1'.repeat(constants.BITS - this.subnetMask)}`); | ||
} | ||
@@ -239,3 +238,3 @@ /** | ||
endAddress() { | ||
return Address4.fromBigInteger(this._endAddress()); | ||
return Address4.fromBigInt(this._endAddress()); | ||
} | ||
@@ -250,14 +249,14 @@ /** | ||
endAddressExclusive() { | ||
const adjust = new jsbn_1.BigInteger('1'); | ||
return Address4.fromBigInteger(this._endAddress().subtract(adjust)); | ||
const adjust = BigInt('1'); | ||
return Address4.fromBigInt(this._endAddress() - adjust); | ||
} | ||
/** | ||
* Converts a BigInteger to a v4 address object | ||
* Converts a BigInt to a v4 address object | ||
* @memberof Address4 | ||
* @static | ||
* @param {BigInteger} bigInteger - a BigInteger to convert | ||
* @param {bigint} bigInt - a BigInt to convert | ||
* @returns {Address4} | ||
*/ | ||
static fromBigInteger(bigInteger) { | ||
return Address4.fromInteger(parseInt(bigInteger.toString(), 10)); | ||
static fromBigInt(bigInt) { | ||
return Address4.fromHex(bigInt.toString(16)); | ||
} | ||
@@ -320,3 +319,3 @@ /** | ||
binaryZeroPad() { | ||
return this.bigInteger().toString(2).padStart(constants.BITS, '0'); | ||
return this.bigInt().toString(2).padStart(constants.BITS, '0'); | ||
} | ||
@@ -323,0 +322,0 @@ /** |
import * as common from './common'; | ||
import { Address4 } from './ipv4'; | ||
import { BigInteger } from 'jsbn'; | ||
interface SixToFourProperties { | ||
@@ -48,13 +47,13 @@ prefix: string; | ||
/** | ||
* Convert a BigInteger to a v6 address object | ||
* Convert a BigInt to a v6 address object | ||
* @memberof Address6 | ||
* @static | ||
* @param {BigInteger} bigInteger - a BigInteger to convert | ||
* @param {bigint} bigInt - a BigInt to convert | ||
* @returns {Address6} | ||
* @example | ||
* var bigInteger = new BigInteger('1000000000000'); | ||
* var address = Address6.fromBigInteger(bigInteger); | ||
* var bigInt = BigInt('1000000000000'); | ||
* var address = Address6.fromBigInt(bigInt); | ||
* address.correctForm(); // '::e8:d4a5:1000' | ||
*/ | ||
static fromBigInteger(bigInteger: BigInteger): Address6; | ||
static fromBigInt(bigInt: bigint): Address6; | ||
/** | ||
@@ -121,3 +120,3 @@ * Convert a URL (with optional port number) to an address object | ||
* @instance | ||
* @param {number} [size=128] - the subnet size | ||
* @param {number} [subnetSize=128] - the subnet size | ||
* @returns {String} | ||
@@ -130,5 +129,5 @@ */ | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
_startAddress(): BigInteger; | ||
_startAddress(): bigint; | ||
/** | ||
@@ -154,5 +153,5 @@ * The first address in the range given by this address' subnet | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
_endAddress(): BigInteger; | ||
_endAddress(): bigint; | ||
/** | ||
@@ -189,8 +188,8 @@ * The last address in the range given by this address' subnet | ||
/** | ||
* Return the bits in the given range as a BigInteger | ||
* Return the bits in the given range as a BigInt | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
getBits(start: number, end: number): BigInteger; | ||
getBits(start: number, end: number): bigint; | ||
/** | ||
@@ -262,8 +261,8 @@ * Return the bits in the given range as a base-2 string | ||
/** | ||
* Return the address as a BigInteger | ||
* Return the address as a BigInt | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
bigInteger(): BigInteger; | ||
bigInt(): bigint; | ||
/** | ||
@@ -270,0 +269,0 @@ * Return the last two groups of this address as an IPv4 address string |
101
dist/ipv6.js
@@ -36,3 +36,3 @@ "use strict"; | ||
const address_error_1 = require("./address-error"); | ||
const jsbn_1 = require("jsbn"); | ||
const common_1 = require("./common"); | ||
function assert(condition) { | ||
@@ -151,14 +151,14 @@ if (!condition) { | ||
/** | ||
* Convert a BigInteger to a v6 address object | ||
* Convert a BigInt to a v6 address object | ||
* @memberof Address6 | ||
* @static | ||
* @param {BigInteger} bigInteger - a BigInteger to convert | ||
* @param {bigint} bigInt - a BigInt to convert | ||
* @returns {Address6} | ||
* @example | ||
* var bigInteger = new BigInteger('1000000000000'); | ||
* var address = Address6.fromBigInteger(bigInteger); | ||
* var bigInt = BigInt('1000000000000'); | ||
* var address = Address6.fromBigInt(bigInt); | ||
* address.correctForm(); // '::e8:d4a5:1000' | ||
*/ | ||
static fromBigInteger(bigInteger) { | ||
const hex = bigInteger.toString(16).padStart(32, '0'); | ||
static fromBigInt(bigInt) { | ||
const hex = bigInt.toString(16).padStart(32, '0'); | ||
const groups = []; | ||
@@ -299,3 +299,3 @@ let i; | ||
* @instance | ||
* @param {number} [size=128] - the subnet size | ||
* @param {number} [subnetSize=128] - the subnet size | ||
* @returns {String} | ||
@@ -311,3 +311,3 @@ */ | ||
} | ||
return addCommas(new jsbn_1.BigInteger('2', 10).pow(subnetPowers).toString(10)); | ||
return addCommas((BigInt('2') ** BigInt(subnetPowers)).toString(10)); | ||
} | ||
@@ -318,6 +318,6 @@ /** | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
_startAddress() { | ||
return new jsbn_1.BigInteger(this.mask() + '0'.repeat(constants6.BITS - this.subnetMask), 2); | ||
return BigInt(`0b${this.mask() + '0'.repeat(constants6.BITS - this.subnetMask)}`); | ||
} | ||
@@ -332,3 +332,3 @@ /** | ||
startAddress() { | ||
return Address6.fromBigInteger(this._startAddress()); | ||
return Address6.fromBigInt(this._startAddress()); | ||
} | ||
@@ -343,4 +343,4 @@ /** | ||
startAddressExclusive() { | ||
const adjust = new jsbn_1.BigInteger('1'); | ||
return Address6.fromBigInteger(this._startAddress().add(adjust)); | ||
const adjust = BigInt('1'); | ||
return Address6.fromBigInt(this._startAddress() + adjust); | ||
} | ||
@@ -351,6 +351,6 @@ /** | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
_endAddress() { | ||
return new jsbn_1.BigInteger(this.mask() + '1'.repeat(constants6.BITS - this.subnetMask), 2); | ||
return BigInt(`0b${this.mask() + '1'.repeat(constants6.BITS - this.subnetMask)}`); | ||
} | ||
@@ -365,3 +365,3 @@ /** | ||
endAddress() { | ||
return Address6.fromBigInteger(this._endAddress()); | ||
return Address6.fromBigInt(this._endAddress()); | ||
} | ||
@@ -376,4 +376,4 @@ /** | ||
endAddressExclusive() { | ||
const adjust = new jsbn_1.BigInteger('1'); | ||
return Address6.fromBigInteger(this._endAddress().subtract(adjust)); | ||
const adjust = BigInt('1'); | ||
return Address6.fromBigInt(this._endAddress() - adjust); | ||
} | ||
@@ -387,3 +387,3 @@ /** | ||
getScope() { | ||
let scope = constants6.SCOPES[this.getBits(12, 16).intValue()]; | ||
let scope = constants6.SCOPES[parseInt(this.getBits(12, 16).toString(10), 10)]; | ||
if (this.getType() === 'Global unicast' && scope !== 'Link local') { | ||
@@ -409,9 +409,9 @@ scope = 'Global'; | ||
/** | ||
* Return the bits in the given range as a BigInteger | ||
* Return the bits in the given range as a BigInt | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
getBits(start, end) { | ||
return new jsbn_1.BigInteger(this.getBitsBase2(start, end), 2); | ||
return BigInt(`0b${this.getBitsBase2(start, end)}`); | ||
} | ||
@@ -539,3 +539,3 @@ /** | ||
binaryZeroPad() { | ||
return this.bigInteger().toString(2).padStart(constants6.BITS, '0'); | ||
return this.bigInt().toString(2).padStart(constants6.BITS, '0'); | ||
} | ||
@@ -628,9 +628,9 @@ // TODO: Improve the semantics of this helper function | ||
/** | ||
* Return the address as a BigInteger | ||
* Return the address as a BigInt | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
bigInteger() { | ||
return new jsbn_1.BigInteger(this.parsedAddress.map(paddedHex).join(''), 16); | ||
bigInt() { | ||
return BigInt(`0x${this.parsedAddress.map(paddedHex).join('')}`); | ||
} | ||
@@ -648,3 +648,3 @@ /** | ||
const binary = this.binaryZeroPad().split(''); | ||
return ipv4_1.Address4.fromHex(new jsbn_1.BigInteger(binary.slice(96, 128).join(''), 2).toString(16)); | ||
return ipv4_1.Address4.fromHex(BigInt(`0b${binary.slice(96, 128).join('')}`).toString(16)); | ||
} | ||
@@ -696,12 +696,15 @@ /** | ||
const prefix = this.getBitsBase16(0, 32); | ||
const udpPort = this.getBits(80, 96).xor(new jsbn_1.BigInteger('ffff', 16)).toString(); | ||
const bitsForUdpPort = this.getBits(80, 96); | ||
// eslint-disable-next-line no-bitwise | ||
const udpPort = (bitsForUdpPort ^ BigInt('0xffff')).toString(); | ||
const server4 = ipv4_1.Address4.fromHex(this.getBitsBase16(32, 64)); | ||
const client4 = ipv4_1.Address4.fromHex(this.getBits(96, 128).xor(new jsbn_1.BigInteger('ffffffff', 16)).toString(16)); | ||
const flags = this.getBits(64, 80); | ||
const bitsForClient4 = this.getBits(96, 128); | ||
// eslint-disable-next-line no-bitwise | ||
const client4 = ipv4_1.Address4.fromHex((bitsForClient4 ^ BigInt('0xffffffff')).toString(16)); | ||
const flagsBase2 = this.getBitsBase2(64, 80); | ||
const coneNat = flags.testBit(15); | ||
const reserved = flags.testBit(14); | ||
const groupIndividual = flags.testBit(8); | ||
const universalLocal = flags.testBit(9); | ||
const nonce = new jsbn_1.BigInteger(flagsBase2.slice(2, 6) + flagsBase2.slice(8, 16), 2).toString(10); | ||
const coneNat = (0, common_1.testBit)(flagsBase2, 15); | ||
const reserved = (0, common_1.testBit)(flagsBase2, 14); | ||
const groupIndividual = (0, common_1.testBit)(flagsBase2, 8); | ||
const universalLocal = (0, common_1.testBit)(flagsBase2, 9); | ||
const nonce = BigInt(`0b${flagsBase2.slice(2, 6) + flagsBase2.slice(8, 16)}`).toString(10); | ||
return { | ||
@@ -766,8 +769,10 @@ prefix: `${prefix.slice(0, 4)}:${prefix.slice(4, 8)}`, | ||
toByteArray() { | ||
const byteArray = this.bigInteger().toByteArray(); | ||
// work around issue where `toByteArray` returns a leading 0 element | ||
if (byteArray.length === 17 && byteArray[0] === 0) { | ||
return byteArray.slice(1); | ||
const valueWithoutPadding = this.bigInt().toString(16); | ||
const leadingPad = '0'.repeat(valueWithoutPadding.length % 2); | ||
const value = `${leadingPad}${valueWithoutPadding}`; | ||
const bytes = []; | ||
for (let i = 0, length = value.length; i < length; i += 2) { | ||
bytes.push(parseInt(value.substring(i, i + 2), 16)); | ||
} | ||
return byteArray; | ||
return bytes; | ||
} | ||
@@ -799,10 +804,10 @@ /** | ||
static fromUnsignedByteArray(bytes) { | ||
const BYTE_MAX = new jsbn_1.BigInteger('256', 10); | ||
let result = new jsbn_1.BigInteger('0', 10); | ||
let multiplier = new jsbn_1.BigInteger('1', 10); | ||
const BYTE_MAX = BigInt('256'); | ||
let result = BigInt('0'); | ||
let multiplier = BigInt('1'); | ||
for (let i = bytes.length - 1; i >= 0; i--) { | ||
result = result.add(multiplier.multiply(new jsbn_1.BigInteger(bytes[i].toString(10), 10))); | ||
multiplier = multiplier.multiply(BYTE_MAX); | ||
result += multiplier * BigInt(bytes[i].toString(10)); | ||
multiplier *= BYTE_MAX; | ||
} | ||
return Address6.fromBigInteger(result); | ||
return Address6.fromBigInt(result); | ||
} | ||
@@ -809,0 +814,0 @@ /** |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "9.1.0-0", | ||
"version": "10.0.0", | ||
"author": "Beau Gunderson <beau@beaugunderson.com> (https://beaugunderson.com/)", | ||
@@ -55,6 +55,2 @@ "license": "MIT", | ||
}, | ||
"dependencies": { | ||
"@types/jsbn": "^1.2.33", | ||
"jsbn": "1.1.0" | ||
}, | ||
"devDependencies": { | ||
@@ -61,0 +57,0 @@ "@types/chai": "^5.0.0", |
@@ -17,15 +17,12 @@ [![CircleCI](https://dl.circleci.com/status-badge/img/circleci/9fJmTZfn8d8p7GtVt688PY/JjriGjhcxBD6zYKygMZaet/tree/master.svg?style=svg&circle-token=7baede7efd3db5f1f25fb439e97d5f695ff76318)](https://dl.circleci.com/status-badge/redirect/circleci/9fJmTZfn8d8p7GtVt688PY/JjriGjhcxBD6zYKygMZaet/tree/master) | ||
### Upgrading from 9.x to 10.x | ||
### Migrating from 6.x to 7.x | ||
The dependency on `jsbn` was removed thanks to | ||
[michal-kocarek](https://github.com/michal-kocarek). Thanks Michal! For | ||
clarity, all methods with BigInteger in the name were renamed to BigInt. | ||
`ip-address` was rewritten in TypeScript for version 7. If you were using | ||
version 6 you'll need to make these changes to upgrade: | ||
#### Breaking changes | ||
- Instead of checking `isValid()`, which has been removed, you'll need to use a | ||
`try`/`catch` if you're accepting unknown input. This made the TypeScript | ||
types substantially easier as well as allowed the use of an `AddressError` | ||
class which will contain a `parseMessage` if an error occurred in the parsing | ||
step. | ||
- Instead of using the `error`, `parseError`, and `valid` attributes you'll | ||
need to use the `message` and `parseMessage` of the thrown `AddressError`. | ||
- `#fromBigInteger()` → `#fromBigInt()`; now returns a native BigInt | ||
- `#bigInteger()` → `#bigInt()`; now returns a native BigInt | ||
@@ -32,0 +29,0 @@ ### Documentation |
@@ -41,1 +41,16 @@ import { Address4 } from './ipv4'; | ||
} | ||
/** | ||
* @param binaryValue Binary representation of a value (e.g. `10`) | ||
* @param position Byte position, where 0 is the least significant bit | ||
*/ | ||
export function testBit(binaryValue: string, position: number): boolean { | ||
const { length } = binaryValue; | ||
if (position > length) { | ||
return false; | ||
} | ||
const positionInString = length - position; | ||
return binaryValue.substring(positionInString, positionInString + 1) === '1'; | ||
} |
@@ -6,3 +6,2 @@ /* eslint-disable no-param-reassign */ | ||
import { AddressError } from './address-error'; | ||
import { BigInteger } from 'jsbn'; | ||
@@ -181,9 +180,9 @@ /** | ||
/** | ||
* Returns the address as a BigInteger | ||
* Returns the address as a `bigint` | ||
* @memberof Address4 | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
bigInteger(): BigInteger { | ||
return new BigInteger(this.parsedAddress.map((n) => common.stringToPaddedHex(n)).join(''), 16); | ||
bigInt(): BigInt { | ||
return BigInt(`0x${this.parsedAddress.map((n) => common.stringToPaddedHex(n)).join('')}`); | ||
} | ||
@@ -195,6 +194,6 @@ | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
_startAddress(): BigInteger { | ||
return new BigInteger(this.mask() + '0'.repeat(constants.BITS - this.subnetMask), 2); | ||
_startAddress(): bigint { | ||
return BigInt(`0b${this.mask() + '0'.repeat(constants.BITS - this.subnetMask)}`); | ||
} | ||
@@ -210,3 +209,3 @@ | ||
startAddress(): Address4 { | ||
return Address4.fromBigInteger(this._startAddress()); | ||
return Address4.fromBigInt(this._startAddress()); | ||
} | ||
@@ -222,4 +221,4 @@ | ||
startAddressExclusive(): Address4 { | ||
const adjust = new BigInteger('1'); | ||
return Address4.fromBigInteger(this._startAddress().add(adjust)); | ||
const adjust = BigInt('1'); | ||
return Address4.fromBigInt(this._startAddress() + adjust); | ||
} | ||
@@ -231,6 +230,6 @@ | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
_endAddress(): BigInteger { | ||
return new BigInteger(this.mask() + '1'.repeat(constants.BITS - this.subnetMask), 2); | ||
_endAddress(): bigint { | ||
return BigInt(`0b${this.mask() + '1'.repeat(constants.BITS - this.subnetMask)}`); | ||
} | ||
@@ -246,3 +245,3 @@ | ||
endAddress(): Address4 { | ||
return Address4.fromBigInteger(this._endAddress()); | ||
return Address4.fromBigInt(this._endAddress()); | ||
} | ||
@@ -258,15 +257,15 @@ | ||
endAddressExclusive(): Address4 { | ||
const adjust = new BigInteger('1'); | ||
return Address4.fromBigInteger(this._endAddress().subtract(adjust)); | ||
const adjust = BigInt('1'); | ||
return Address4.fromBigInt(this._endAddress() - adjust); | ||
} | ||
/** | ||
* Converts a BigInteger to a v4 address object | ||
* Converts a BigInt to a v4 address object | ||
* @memberof Address4 | ||
* @static | ||
* @param {BigInteger} bigInteger - a BigInteger to convert | ||
* @param {bigint} bigInt - a BigInt to convert | ||
* @returns {Address4} | ||
*/ | ||
static fromBigInteger(bigInteger: BigInteger): Address4 { | ||
return Address4.fromInteger(parseInt(bigInteger.toString(), 10)); | ||
static fromBigInt(bigInt: bigint): Address4 { | ||
return Address4.fromHex(bigInt.toString(16)); | ||
} | ||
@@ -346,3 +345,3 @@ | ||
binaryZeroPad(): string { | ||
return this.bigInteger().toString(2).padStart(constants.BITS, '0'); | ||
return this.bigInt().toString(2).padStart(constants.BITS, '0'); | ||
} | ||
@@ -349,0 +348,0 @@ |
111
src/ipv6.ts
@@ -15,3 +15,3 @@ /* eslint-disable prefer-destructuring */ | ||
import { AddressError } from './address-error'; | ||
import { BigInteger } from 'jsbn'; | ||
import { testBit } from './common'; | ||
@@ -167,14 +167,14 @@ function assert(condition: any): asserts condition { | ||
/** | ||
* Convert a BigInteger to a v6 address object | ||
* Convert a BigInt to a v6 address object | ||
* @memberof Address6 | ||
* @static | ||
* @param {BigInteger} bigInteger - a BigInteger to convert | ||
* @param {bigint} bigInt - a BigInt to convert | ||
* @returns {Address6} | ||
* @example | ||
* var bigInteger = new BigInteger('1000000000000'); | ||
* var address = Address6.fromBigInteger(bigInteger); | ||
* var bigInt = BigInt('1000000000000'); | ||
* var address = Address6.fromBigInt(bigInt); | ||
* address.correctForm(); // '::e8:d4a5:1000' | ||
*/ | ||
static fromBigInteger(bigInteger: BigInteger): Address6 { | ||
const hex = bigInteger.toString(16).padStart(32, '0'); | ||
static fromBigInt(bigInt: bigint): Address6 { | ||
const hex = bigInt.toString(16).padStart(32, '0'); | ||
const groups = []; | ||
@@ -336,3 +336,3 @@ let i; | ||
* @instance | ||
* @param {number} [size=128] - the subnet size | ||
* @param {number} [subnetSize=128] - the subnet size | ||
* @returns {String} | ||
@@ -350,3 +350,3 @@ */ | ||
return addCommas(new BigInteger('2', 10).pow(subnetPowers).toString(10)); | ||
return addCommas((BigInt('2') ** BigInt(subnetPowers)).toString(10)); | ||
} | ||
@@ -358,6 +358,6 @@ | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
_startAddress(): BigInteger { | ||
return new BigInteger(this.mask() + '0'.repeat(constants6.BITS - this.subnetMask), 2); | ||
_startAddress(): bigint { | ||
return BigInt(`0b${this.mask() + '0'.repeat(constants6.BITS - this.subnetMask)}`); | ||
} | ||
@@ -373,3 +373,3 @@ | ||
startAddress(): Address6 { | ||
return Address6.fromBigInteger(this._startAddress()); | ||
return Address6.fromBigInt(this._startAddress()); | ||
} | ||
@@ -385,4 +385,4 @@ | ||
startAddressExclusive(): Address6 { | ||
const adjust = new BigInteger('1'); | ||
return Address6.fromBigInteger(this._startAddress().add(adjust)); | ||
const adjust = BigInt('1'); | ||
return Address6.fromBigInt(this._startAddress() + adjust); | ||
} | ||
@@ -394,6 +394,6 @@ | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
_endAddress(): BigInteger { | ||
return new BigInteger(this.mask() + '1'.repeat(constants6.BITS - this.subnetMask), 2); | ||
_endAddress(): bigint { | ||
return BigInt(`0b${this.mask() + '1'.repeat(constants6.BITS - this.subnetMask)}`); | ||
} | ||
@@ -409,3 +409,3 @@ | ||
endAddress(): Address6 { | ||
return Address6.fromBigInteger(this._endAddress()); | ||
return Address6.fromBigInt(this._endAddress()); | ||
} | ||
@@ -421,4 +421,4 @@ | ||
endAddressExclusive(): Address6 { | ||
const adjust = new BigInteger('1'); | ||
return Address6.fromBigInteger(this._endAddress().subtract(adjust)); | ||
const adjust = BigInt('1'); | ||
return Address6.fromBigInt(this._endAddress() - adjust); | ||
} | ||
@@ -433,3 +433,3 @@ | ||
getScope(): string { | ||
let scope = constants6.SCOPES[this.getBits(12, 16).intValue()]; | ||
let scope = constants6.SCOPES[parseInt(this.getBits(12, 16).toString(10), 10)]; | ||
@@ -460,9 +460,9 @@ if (this.getType() === 'Global unicast' && scope !== 'Link local') { | ||
/** | ||
* Return the bits in the given range as a BigInteger | ||
* Return the bits in the given range as a BigInt | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
getBits(start: number, end: number): BigInteger { | ||
return new BigInteger(this.getBitsBase2(start, end), 2); | ||
getBits(start: number, end: number): bigint { | ||
return BigInt(`0b${this.getBitsBase2(start, end)}`); | ||
} | ||
@@ -616,3 +616,3 @@ | ||
binaryZeroPad(): string { | ||
return this.bigInteger().toString(2).padStart(constants6.BITS, '0'); | ||
return this.bigInt().toString(2).padStart(constants6.BITS, '0'); | ||
} | ||
@@ -749,9 +749,9 @@ | ||
/** | ||
* Return the address as a BigInteger | ||
* Return the address as a BigInt | ||
* @memberof Address6 | ||
* @instance | ||
* @returns {BigInteger} | ||
* @returns {bigint} | ||
*/ | ||
bigInteger(): BigInteger { | ||
return new BigInteger(this.parsedAddress.map(paddedHex).join(''), 16); | ||
bigInt(): bigint { | ||
return BigInt(`0x${this.parsedAddress.map(paddedHex).join('')}`); | ||
} | ||
@@ -771,3 +771,3 @@ | ||
return Address4.fromHex(new BigInteger(binary.slice(96, 128).join(''), 2).toString(16)); | ||
return Address4.fromHex(BigInt(`0b${binary.slice(96, 128).join('')}`).toString(16)); | ||
} | ||
@@ -826,17 +826,19 @@ | ||
const udpPort = this.getBits(80, 96).xor(new BigInteger('ffff', 16)).toString(); | ||
const bitsForUdpPort: bigint = this.getBits(80, 96); | ||
// eslint-disable-next-line no-bitwise | ||
const udpPort = (bitsForUdpPort ^ BigInt('0xffff')).toString(); | ||
const server4 = Address4.fromHex(this.getBitsBase16(32, 64)); | ||
const client4 = Address4.fromHex( | ||
this.getBits(96, 128).xor(new BigInteger('ffffffff', 16)).toString(16), | ||
); | ||
const flags = this.getBits(64, 80); | ||
const bitsForClient4 = this.getBits(96, 128); | ||
// eslint-disable-next-line no-bitwise | ||
const client4 = Address4.fromHex((bitsForClient4 ^ BigInt('0xffffffff')).toString(16)); | ||
const flagsBase2 = this.getBitsBase2(64, 80); | ||
const coneNat = flags.testBit(15); | ||
const reserved = flags.testBit(14); | ||
const groupIndividual = flags.testBit(8); | ||
const universalLocal = flags.testBit(9); | ||
const nonce = new BigInteger(flagsBase2.slice(2, 6) + flagsBase2.slice(8, 16), 2).toString(10); | ||
const coneNat = testBit(flagsBase2, 15); | ||
const reserved = testBit(flagsBase2, 14); | ||
const groupIndividual = testBit(flagsBase2, 8); | ||
const universalLocal = testBit(flagsBase2, 9); | ||
const nonce = BigInt(`0b${flagsBase2.slice(2, 6) + flagsBase2.slice(8, 16)}`).toString(10); | ||
@@ -910,10 +912,13 @@ return { | ||
toByteArray(): number[] { | ||
const byteArray = this.bigInteger().toByteArray(); | ||
const valueWithoutPadding = this.bigInt().toString(16); | ||
const leadingPad = '0'.repeat(valueWithoutPadding.length % 2); | ||
// work around issue where `toByteArray` returns a leading 0 element | ||
if (byteArray.length === 17 && byteArray[0] === 0) { | ||
return byteArray.slice(1); | ||
const value = `${leadingPad}${valueWithoutPadding}`; | ||
const bytes = []; | ||
for (let i = 0, length = value.length; i < length; i += 2) { | ||
bytes.push(parseInt(value.substring(i, i + 2), 16)); | ||
} | ||
return byteArray; | ||
return bytes; | ||
} | ||
@@ -948,13 +953,13 @@ | ||
static fromUnsignedByteArray(bytes: Array<any>): Address6 { | ||
const BYTE_MAX = new BigInteger('256', 10); | ||
let result = new BigInteger('0', 10); | ||
let multiplier = new BigInteger('1', 10); | ||
const BYTE_MAX = BigInt('256'); | ||
let result = BigInt('0'); | ||
let multiplier = BigInt('1'); | ||
for (let i = bytes.length - 1; i >= 0; i--) { | ||
result = result.add(multiplier.multiply(new BigInteger(bytes[i].toString(10), 10))); | ||
result += multiplier * BigInt(bytes[i].toString(10)); | ||
multiplier = multiplier.multiply(BYTE_MAX); | ||
multiplier *= BYTE_MAX; | ||
} | ||
return Address6.fromBigInteger(result); | ||
return Address6.fromBigInt(result); | ||
} | ||
@@ -961,0 +966,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
176520
0
3915
1
106
- Removed@types/jsbn@^1.2.33
- Removedjsbn@1.1.0
- Removed@types/jsbn@1.2.33(transitive)
- Removedjsbn@1.1.0(transitive)