cidr-block 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -1
- package/build/{src/cidr → cidr}/errors.d.ts +1 -1
- package/build/cidr/ipv4/constants.d.ts +4 -0
- package/build/cidr/ipv4/index.d.ts +4 -0
- package/build/cidr/ipv4/ipv4-address.d.ts +137 -0
- package/build/cidr/ipv4/ipv4-cidr.d.ts +68 -0
- package/build/cidr/ipv4/types.d.ts +9 -0
- package/build/cidr/ipv6/constants.d.ts +4 -0
- package/build/cidr/ipv6/index.d.ts +1 -0
- package/build/index.d.ts +3 -0
- package/build/index.esm.js +200 -46
- package/build/index.esm.js.map +1 -1
- package/build/index.js +200 -46
- package/build/index.js.map +1 -1
- package/package.json +17 -3
- package/build/src/cidr/constants.d.ts +0 -8
- package/build/src/cidr/ipv4/index.d.ts +0 -1
- package/build/src/cidr/ipv4/ipv4-address.d.ts +0 -101
- package/build/src/index.d.ts +0 -3
- package/build/tests/cidr/ipv4/ipv4-address.test.d.ts +0 -1
package/README.md
CHANGED
|
@@ -1,2 +1,71 @@
|
|
|
1
1
|
# cidr-block
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
ipv4 and ipv6 address and cidr range utilities
|
|
4
|
+
|
|
5
|
+
## WARNING
|
|
6
|
+
|
|
7
|
+
This package is still in _very_ early stages and should **NOT** be used in production code!
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
To install npm package, run the following in your project:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install cidr-block
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
or if you're using yarn instead of npm
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
yarn add cidr-block
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The package is written completely in TypeScript and exports all of it's types automatically,
|
|
24
|
+
meaning you don't need to install any additional `@types` typings.
|
|
25
|
+
|
|
26
|
+
## Getting Started
|
|
27
|
+
|
|
28
|
+
Start by defining a cidr range
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { ipv4 as ip } from 'cidr-block'
|
|
32
|
+
|
|
33
|
+
const myCidr = ip.cidr('10.0.0.0/24')
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
To get the next logical cidr block
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
console.log(myCidr.nextBlock().toString()) // 10.0.1.0/24
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
All `cidr-block` functions and methods are immutable, meaning a new instance will always be
|
|
43
|
+
returned instead of trying to modify the current value.
|
|
44
|
+
|
|
45
|
+
Once you have a cidr, you have access to all of it's related utilities:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
myCidr.netmask // 255.255.255.0
|
|
49
|
+
myCidr.firstUsableIp // 10.0.0.0 (remember that methods act immutable, so this is still at 10.0.0.0)
|
|
50
|
+
myCidr.lastUsableIp // 10.0.0.254
|
|
51
|
+
myCidr.includes(ip.address('10.0.0.128')) // true
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Documentation and API Reference
|
|
55
|
+
|
|
56
|
+
The full documentation and API reference can be found at https://cidr-block.com
|
|
57
|
+
|
|
58
|
+
## FAQ
|
|
59
|
+
|
|
60
|
+
Q: Why are the imports in all the example code like that?
|
|
61
|
+
|
|
62
|
+
A: The imports in all example code are formatted as the following:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { ipv4 as ip } from 'cidr-block'
|
|
66
|
+
// or commonjs-style
|
|
67
|
+
const { ipv4: ip } = require('cidr-block')
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
While you don't have to follow this convention, the API is design like this on purpose to help speed
|
|
71
|
+
up a refactoring of ipv4 to ipv6, as you would only need to change the number on the import.
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { Ipv4Literal, Ipv4Representable } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Representation of an IPv4 address. Provides various utility methods like equality
|
|
4
|
+
* checking.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Direct instantiation should be avoided; use {@link ipv4.address|ipv4.address()} instead.
|
|
8
|
+
*/
|
|
9
|
+
export declare class Ipv4Address {
|
|
10
|
+
private _address;
|
|
11
|
+
constructor(address: Ipv4Literal);
|
|
12
|
+
/**
|
|
13
|
+
* The address as a big integer
|
|
14
|
+
*/
|
|
15
|
+
get address(): bigint;
|
|
16
|
+
/**
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
20
|
+
*
|
|
21
|
+
* ip.address(255n) // ==> '0.0.0.255'
|
|
22
|
+
* ip.address(0b11111111_00000000_11111111_00000000n) // ==> '255.0.255.0'
|
|
23
|
+
* ````
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
* @returns the IPv4 address as a string
|
|
27
|
+
*/
|
|
28
|
+
toString(): string;
|
|
29
|
+
/**
|
|
30
|
+
* Compares if two IP address are the same.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
35
|
+
*
|
|
36
|
+
* function isLoopback(address: Ipv4Representable) {
|
|
37
|
+
* return ip.address(address).equals('127.0.0.1')
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @public
|
|
42
|
+
* @param otherIpAddress the other IPv4 address to compare
|
|
43
|
+
* @returns if the other IP address is the same
|
|
44
|
+
*/
|
|
45
|
+
equals(otherIpAddress: Ipv4Representable): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
50
|
+
*
|
|
51
|
+
* const myIp = ip.address('52.89.32.255')
|
|
52
|
+
* myIp.nextIp() // ==> '52.89.33.0
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @public
|
|
56
|
+
* @returns the next consecutive IPv4 address
|
|
57
|
+
*/
|
|
58
|
+
nextIp(): Ipv4Address;
|
|
59
|
+
/**
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
63
|
+
*
|
|
64
|
+
* const myIp = ip.address('52.89.32.19')
|
|
65
|
+
* myIp.previousIp() // ==> '52.89.32.18
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @public
|
|
69
|
+
* @returns the preceding IPv4 address
|
|
70
|
+
*/
|
|
71
|
+
previousIp(): Ipv4Address;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Convenience function for creating an IPv4 address instance.
|
|
75
|
+
*
|
|
76
|
+
* @remarks
|
|
77
|
+
*
|
|
78
|
+
* In general, you should use this function instead of instantiating an Ipv4Address
|
|
79
|
+
* object directly. While there is nothing wrong with direct instantiation, convenience
|
|
80
|
+
* methods like these are meant to help reduce the footprint of your code and increase
|
|
81
|
+
* readability.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
*
|
|
85
|
+
* ```typescript
|
|
86
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
87
|
+
*
|
|
88
|
+
* const localhost = ip.address('127.0.0.1')
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @see {@link Ipv4Address}
|
|
92
|
+
*
|
|
93
|
+
* @param ip string representation of the IPv4 address
|
|
94
|
+
* @returns an instance of Ipv4Address
|
|
95
|
+
*/
|
|
96
|
+
export declare function address(ip: Ipv4Literal): Ipv4Address;
|
|
97
|
+
/**
|
|
98
|
+
* Converts the string representation of an IPv4 address to a BigInt.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
*
|
|
102
|
+
* ```typescript
|
|
103
|
+
* import * as cidr from 'cidr-block'
|
|
104
|
+
*
|
|
105
|
+
* cidr.ipv4.stringToNum('255.255.255.255') === 4_294_967_295n // ==> true
|
|
106
|
+
* cidr.ipv4.stringToNum('0.0.0.255') === 255n // ==> true
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* @see This method is the inverse of {@link ipv4.numToString}
|
|
110
|
+
* @throws {@link InvalidIpAddressError}
|
|
111
|
+
*
|
|
112
|
+
* @public
|
|
113
|
+
* @param address IPv4 address represented as a string
|
|
114
|
+
* @returns numerical BigInt representation of the address
|
|
115
|
+
*/
|
|
116
|
+
export declare function stringToNum(address: string): bigint;
|
|
117
|
+
/**
|
|
118
|
+
* Converts the numerical BigInt representation of an IPv4 address to its string representation.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
*
|
|
122
|
+
* ```typescript
|
|
123
|
+
* import * as cidr from 'cidr-block'
|
|
124
|
+
*
|
|
125
|
+
* cidr.ipv4.numToString(0n) === '0.0.0.0' // ==> true
|
|
126
|
+
* cidr.ipv4.numToString(65_280n) === '0.0.255.0' // ==> true
|
|
127
|
+
* cidr.ipv4.numToString(4_294_967_295n) === '255.255.255.255' // ==> true
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* @see This method is the inverse of {@link ipv4.stringToNum}
|
|
131
|
+
* @throws {@link InvalidIpAddressError}
|
|
132
|
+
*
|
|
133
|
+
* @public
|
|
134
|
+
* @param ip IPv4 address as a BigInt
|
|
135
|
+
* @returns string representation of the address
|
|
136
|
+
*/
|
|
137
|
+
export declare function numToString(ip: bigint): string;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Ipv4Representable } from './types';
|
|
2
|
+
import { Ipv4Address } from './ipv4-address';
|
|
3
|
+
export declare class Ipv4Cidr {
|
|
4
|
+
private _ipAddress;
|
|
5
|
+
private _maskSize;
|
|
6
|
+
constructor(cidrRange: string);
|
|
7
|
+
/**
|
|
8
|
+
* The size of the cidr netmask (the number after the slash in cidr notation)
|
|
9
|
+
*/
|
|
10
|
+
get maskSize(): number;
|
|
11
|
+
/**
|
|
12
|
+
* Number of IP addresses within the cidr range
|
|
13
|
+
*/
|
|
14
|
+
get allocatableIpCount(): bigint;
|
|
15
|
+
/**
|
|
16
|
+
* The actual IPv4 netmask address
|
|
17
|
+
*/
|
|
18
|
+
get netmask(): Ipv4Address;
|
|
19
|
+
/**
|
|
20
|
+
* The first IPv4 address that is usable within the given cidr range
|
|
21
|
+
*/
|
|
22
|
+
get firstUsableIp(): Ipv4Address;
|
|
23
|
+
/**
|
|
24
|
+
* The last IPv4 address that is usable within the given cidr range
|
|
25
|
+
*/
|
|
26
|
+
get lastUsableIp(): Ipv4Address;
|
|
27
|
+
private get addressLength();
|
|
28
|
+
/**
|
|
29
|
+
* @returns string representation of the cidr range
|
|
30
|
+
*/
|
|
31
|
+
toString(): string;
|
|
32
|
+
/**
|
|
33
|
+
* @returns the next consecutive cidr block
|
|
34
|
+
*/
|
|
35
|
+
nextBlock(ofSize?: number): Ipv4Cidr;
|
|
36
|
+
/**
|
|
37
|
+
* @returns the previous cidr block
|
|
38
|
+
*/
|
|
39
|
+
previousBlock(): Ipv4Cidr;
|
|
40
|
+
/**
|
|
41
|
+
* @returns if the given IPv4 address is within the cidr range
|
|
42
|
+
*/
|
|
43
|
+
includes(address: Ipv4Representable): boolean;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Convenience function for creating an IPv4 cidr range instance.
|
|
47
|
+
*
|
|
48
|
+
* @remarks
|
|
49
|
+
*
|
|
50
|
+
* In general, you should use this function instead of instantiating an Ipv4Cidr
|
|
51
|
+
* object directly. While there is nothing wrong with direct instantiation, convenience
|
|
52
|
+
* methods like these are meant to help reduce the footprint of your code and increase
|
|
53
|
+
* readability.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
*
|
|
57
|
+
* ```typescript
|
|
58
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
59
|
+
*
|
|
60
|
+
* const vpcCidrRange = ip.cidr('10.0.0.0/16')
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @see {@link Ipv4Cidr}
|
|
64
|
+
*
|
|
65
|
+
* @param cidrRange string representation of the cidr range
|
|
66
|
+
* @returns an instance of Ipv4Cidr
|
|
67
|
+
*/
|
|
68
|
+
export declare function cidr(cidrRange: string): Ipv4Cidr;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Ipv4Address } from './ipv4-address';
|
|
2
|
+
/**
|
|
3
|
+
* Type that indicates a literal string or bigint value that represents an IPv4 address
|
|
4
|
+
*/
|
|
5
|
+
export declare type Ipv4Literal = string | bigint;
|
|
6
|
+
/**
|
|
7
|
+
* Type that indicates either a literal value or an address instance that is an IPv4
|
|
8
|
+
*/
|
|
9
|
+
export declare type Ipv4Representable = Ipv4Address | Ipv4Literal;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './constants';
|
package/build/index.d.ts
ADDED
package/build/index.esm.js
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
* The numerical maximum size an IPv4 address can be
|
|
3
|
-
*/
|
|
4
|
-
const IPv4_MAX = 2n ** 32n;
|
|
5
|
-
/**
|
|
6
|
-
* The numerical maximum size an IPv6 address can be
|
|
7
|
-
*/
|
|
8
|
-
const IPv6_MAX = 2n ** 64n;class InvalidIpAddressError extends Error {
|
|
1
|
+
class InvalidIpAddressError extends Error {
|
|
9
2
|
constructor(badIp) {
|
|
10
|
-
super(`${badIp} is not a valid
|
|
3
|
+
super(`${badIp} is not a valid IPv4 address.`);
|
|
11
4
|
}
|
|
12
5
|
}
|
|
13
|
-
class
|
|
6
|
+
class InvalidCidrBlockError extends Error {
|
|
14
7
|
constructor(badCidr) {
|
|
15
|
-
super(`${badCidr} is not a valid cidr block
|
|
8
|
+
super(`${badCidr} is not a valid IPv4 cidr block.`);
|
|
16
9
|
}
|
|
17
|
-
}
|
|
10
|
+
}var errors=/*#__PURE__*/Object.freeze({__proto__:null,InvalidIpAddressError:InvalidIpAddressError,InvalidCidrBlockError:InvalidCidrBlockError});/**
|
|
11
|
+
* The numerical maximum size an IPv4 address can be
|
|
12
|
+
*/
|
|
13
|
+
const MAX$1 = 2n ** 32n - 1n;const MAX_OCTET_SIZE = 255n;
|
|
18
14
|
/**
|
|
19
15
|
* Representation of an IPv4 address. Provides various utility methods like equality
|
|
20
16
|
* checking.
|
|
@@ -23,26 +19,84 @@ class InvalidCidrError extends Error {
|
|
|
23
19
|
* Direct instantiation should be avoided; use {@link ipv4.address|ipv4.address()} instead.
|
|
24
20
|
*/
|
|
25
21
|
class Ipv4Address {
|
|
26
|
-
|
|
22
|
+
_address;
|
|
27
23
|
constructor(address) {
|
|
28
|
-
this.
|
|
24
|
+
this._address = typeof address === 'bigint' ? address : stringToNum(address);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The address as a big integer
|
|
28
|
+
*/
|
|
29
|
+
get address() {
|
|
30
|
+
return this._address;
|
|
29
31
|
}
|
|
30
32
|
/**
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
36
|
+
*
|
|
37
|
+
* ip.address(255n) // ==> '0.0.0.255'
|
|
38
|
+
* ip.address(0b11111111_00000000_11111111_00000000n) // ==> '255.0.255.0'
|
|
39
|
+
* ````
|
|
40
|
+
*
|
|
31
41
|
* @public
|
|
32
42
|
* @returns the IPv4 address as a string
|
|
33
43
|
*/
|
|
34
44
|
toString() {
|
|
35
|
-
return
|
|
45
|
+
return numToString(this._address);
|
|
36
46
|
}
|
|
37
47
|
/**
|
|
38
48
|
* Compares if two IP address are the same.
|
|
39
49
|
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
53
|
+
*
|
|
54
|
+
* function isLoopback(address: Ipv4Representable) {
|
|
55
|
+
* return ip.address(address).equals('127.0.0.1')
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
40
59
|
* @public
|
|
41
60
|
* @param otherIpAddress the other IPv4 address to compare
|
|
42
61
|
* @returns if the other IP address is the same
|
|
43
62
|
*/
|
|
44
63
|
equals(otherIpAddress) {
|
|
45
|
-
|
|
64
|
+
if (otherIpAddress instanceof Ipv4Address) {
|
|
65
|
+
return this._address === otherIpAddress._address;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
return this._address === address(otherIpAddress)._address;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
75
|
+
*
|
|
76
|
+
* const myIp = ip.address('52.89.32.255')
|
|
77
|
+
* myIp.nextIp() // ==> '52.89.33.0
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @public
|
|
81
|
+
* @returns the next consecutive IPv4 address
|
|
82
|
+
*/
|
|
83
|
+
nextIp() {
|
|
84
|
+
return address(this._address + 1n);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
90
|
+
*
|
|
91
|
+
* const myIp = ip.address('52.89.32.19')
|
|
92
|
+
* myIp.previousIp() // ==> '52.89.32.18
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @public
|
|
96
|
+
* @returns the preceding IPv4 address
|
|
97
|
+
*/
|
|
98
|
+
previousIp() {
|
|
99
|
+
return address(this._address - 1n);
|
|
46
100
|
}
|
|
47
101
|
}
|
|
48
102
|
/**
|
|
@@ -58,9 +112,9 @@ class Ipv4Address {
|
|
|
58
112
|
* @example
|
|
59
113
|
*
|
|
60
114
|
* ```typescript
|
|
61
|
-
* import
|
|
115
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
62
116
|
*
|
|
63
|
-
* const localhost =
|
|
117
|
+
* const localhost = ip.address('127.0.0.1')
|
|
64
118
|
* ```
|
|
65
119
|
*
|
|
66
120
|
* @see {@link Ipv4Address}
|
|
@@ -74,29 +128,23 @@ function address(ip) {
|
|
|
74
128
|
/**
|
|
75
129
|
* Converts the string representation of an IPv4 address to a BigInt.
|
|
76
130
|
*
|
|
77
|
-
* @remarks
|
|
78
|
-
*
|
|
79
|
-
* Conversion must be between string and bigint instead of integer as
|
|
80
|
-
* on 32-bit systems, Node.js may allocate the number as a 32-bit integer.
|
|
81
|
-
* To avoid this behavior, a BigInt is always used instead.
|
|
82
|
-
*
|
|
83
131
|
* @example
|
|
84
132
|
*
|
|
85
133
|
* ```typescript
|
|
86
134
|
* import * as cidr from 'cidr-block'
|
|
87
135
|
*
|
|
88
|
-
* cidr.ipv4.
|
|
89
|
-
* cidr.ipv4.
|
|
136
|
+
* cidr.ipv4.stringToNum('255.255.255.255') === 4_294_967_295n // ==> true
|
|
137
|
+
* cidr.ipv4.stringToNum('0.0.0.255') === 255n // ==> true
|
|
90
138
|
* ```
|
|
91
139
|
*
|
|
92
|
-
* @see This method is the inverse of {@link ipv4.
|
|
140
|
+
* @see This method is the inverse of {@link ipv4.numToString}
|
|
93
141
|
* @throws {@link InvalidIpAddressError}
|
|
94
142
|
*
|
|
95
143
|
* @public
|
|
96
144
|
* @param address IPv4 address represented as a string
|
|
97
145
|
* @returns numerical BigInt representation of the address
|
|
98
146
|
*/
|
|
99
|
-
function
|
|
147
|
+
function stringToNum(address) {
|
|
100
148
|
try {
|
|
101
149
|
let [firstOctet, secondOctet, thirdOctet, fourthOctet] = address
|
|
102
150
|
.split('.')
|
|
@@ -115,36 +163,142 @@ function fromString(address) {
|
|
|
115
163
|
/**
|
|
116
164
|
* Converts the numerical BigInt representation of an IPv4 address to its string representation.
|
|
117
165
|
*
|
|
118
|
-
* @remarks
|
|
119
|
-
*
|
|
120
|
-
* Conversion must be between string and bigint instead of integer as
|
|
121
|
-
* on 32-bit systems, Node.js may allocate the number as a 32-bit integer.
|
|
122
|
-
* To avoid this behavior, a BigInt is always used instead.
|
|
123
|
-
*
|
|
124
166
|
* @example
|
|
125
167
|
*
|
|
126
168
|
* ```typescript
|
|
127
169
|
* import * as cidr from 'cidr-block'
|
|
128
170
|
*
|
|
129
|
-
* cidr.ipv4.
|
|
130
|
-
* cidr.ipv4.
|
|
131
|
-
* cidr.ipv4.
|
|
171
|
+
* cidr.ipv4.numToString(0n) === '0.0.0.0' // ==> true
|
|
172
|
+
* cidr.ipv4.numToString(65_280n) === '0.0.255.0' // ==> true
|
|
173
|
+
* cidr.ipv4.numToString(4_294_967_295n) === '255.255.255.255' // ==> true
|
|
132
174
|
* ```
|
|
133
175
|
*
|
|
134
|
-
* @see This method is the inverse of {@link ipv4.
|
|
176
|
+
* @see This method is the inverse of {@link ipv4.stringToNum}
|
|
135
177
|
* @throws {@link InvalidIpAddressError}
|
|
136
178
|
*
|
|
137
179
|
* @public
|
|
138
180
|
* @param ip IPv4 address as a BigInt
|
|
139
181
|
* @returns string representation of the address
|
|
140
182
|
*/
|
|
141
|
-
function
|
|
142
|
-
|
|
183
|
+
function numToString(ip) {
|
|
184
|
+
try {
|
|
185
|
+
if (ip < 0 || ip > MAX$1) {
|
|
186
|
+
throw new Error();
|
|
187
|
+
}
|
|
188
|
+
const firstOctet = (ip >> 24n) & MAX_OCTET_SIZE;
|
|
189
|
+
const secondOctet = (ip >> 16n) & MAX_OCTET_SIZE;
|
|
190
|
+
const thirdOctet = (ip >> 8n) & MAX_OCTET_SIZE;
|
|
191
|
+
const fourthOctet = ip & MAX_OCTET_SIZE;
|
|
192
|
+
return `${firstOctet}.${secondOctet}.${thirdOctet}.${fourthOctet}`;
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
143
195
|
throw new InvalidIpAddressError(ip.toString());
|
|
144
196
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
197
|
+
}class Ipv4Cidr {
|
|
198
|
+
_ipAddress;
|
|
199
|
+
_maskSize;
|
|
200
|
+
// TODO: Allow wider-range of values that can be used to create a cidr
|
|
201
|
+
constructor(cidrRange) {
|
|
202
|
+
try {
|
|
203
|
+
const [address$1, subnetMask] = cidrRange.split('/');
|
|
204
|
+
this._ipAddress = address(address$1);
|
|
205
|
+
this._maskSize = Number(subnetMask);
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
throw new InvalidCidrBlockError(cidrRange);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* The size of the cidr netmask (the number after the slash in cidr notation)
|
|
213
|
+
*/
|
|
214
|
+
get maskSize() {
|
|
215
|
+
return this._maskSize;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Number of IP addresses within the cidr range
|
|
219
|
+
*/
|
|
220
|
+
get allocatableIpCount() {
|
|
221
|
+
return 2n ** this.addressLength;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* The actual IPv4 netmask address
|
|
225
|
+
*/
|
|
226
|
+
get netmask() {
|
|
227
|
+
return address((2n ** BigInt(this.maskSize) - 1n) << this.addressLength);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* The first IPv4 address that is usable within the given cidr range
|
|
231
|
+
*/
|
|
232
|
+
get firstUsableIp() {
|
|
233
|
+
return address(this._ipAddress.address);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* The last IPv4 address that is usable within the given cidr range
|
|
237
|
+
*/
|
|
238
|
+
get lastUsableIp() {
|
|
239
|
+
// FIXME: Handle edge case of when cidr range goes outside valid ip range
|
|
240
|
+
return address(this._ipAddress.address + 2n ** this.addressLength - 1n);
|
|
241
|
+
}
|
|
242
|
+
get addressLength() {
|
|
243
|
+
return BigInt(Math.abs(32 - this._maskSize));
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* @returns string representation of the cidr range
|
|
247
|
+
*/
|
|
248
|
+
toString() {
|
|
249
|
+
return `${this._ipAddress.toString()}/${this._maskSize}`;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* @returns the next consecutive cidr block
|
|
253
|
+
*/
|
|
254
|
+
nextBlock(ofSize) {
|
|
255
|
+
const nextIp = this._ipAddress.address + 2n ** this.addressLength;
|
|
256
|
+
return cidr(`${numToString(nextIp)}/${ofSize ?? this._maskSize}`);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* @returns the previous cidr block
|
|
260
|
+
*/
|
|
261
|
+
previousBlock() {
|
|
262
|
+
const nextIp = this._ipAddress.address - 2n ** this.addressLength;
|
|
263
|
+
return cidr(`${numToString(nextIp)}/${this._maskSize}`);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* @returns if the given IPv4 address is within the cidr range
|
|
267
|
+
*/
|
|
268
|
+
includes(address$1) {
|
|
269
|
+
const ip = address$1 instanceof Ipv4Address ? address$1 : address(address$1);
|
|
270
|
+
return (
|
|
271
|
+
// FIXME: How to handle edge case of next block erroring out?
|
|
272
|
+
ip.address >= this._ipAddress.address && ip.address <= this.nextBlock()._ipAddress.address);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Convenience function for creating an IPv4 cidr range instance.
|
|
277
|
+
*
|
|
278
|
+
* @remarks
|
|
279
|
+
*
|
|
280
|
+
* In general, you should use this function instead of instantiating an Ipv4Cidr
|
|
281
|
+
* object directly. While there is nothing wrong with direct instantiation, convenience
|
|
282
|
+
* methods like these are meant to help reduce the footprint of your code and increase
|
|
283
|
+
* readability.
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
*
|
|
287
|
+
* ```typescript
|
|
288
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
289
|
+
*
|
|
290
|
+
* const vpcCidrRange = ip.cidr('10.0.0.0/16')
|
|
291
|
+
* ```
|
|
292
|
+
*
|
|
293
|
+
* @see {@link Ipv4Cidr}
|
|
294
|
+
*
|
|
295
|
+
* @param cidrRange string representation of the cidr range
|
|
296
|
+
* @returns an instance of Ipv4Cidr
|
|
297
|
+
*/
|
|
298
|
+
function cidr(cidrRange) {
|
|
299
|
+
return new Ipv4Cidr(cidrRange);
|
|
300
|
+
}var index$1=/*#__PURE__*/Object.freeze({__proto__:null,Ipv4Address:Ipv4Address,address:address,stringToNum:stringToNum,numToString:numToString,Ipv4Cidr:Ipv4Cidr,cidr:cidr,MAX:MAX$1});/**
|
|
301
|
+
* The numerical maximum size an IPv6 address can be
|
|
302
|
+
*/
|
|
303
|
+
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
|
304
|
+
const MAX = 0xffffffffffffffffn;var index=/*#__PURE__*/Object.freeze({__proto__:null,MAX:MAX});export{errors,index$1 as ipv4,index as ipv6};//# sourceMappingURL=index.esm.js.map
|
package/build/index.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":["../src/cidr/
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/src/cidr/errors.ts","../src/src/cidr/ipv4/constants.ts","../src/src/cidr/ipv4/ipv4-address.ts","../src/src/cidr/ipv4/ipv4-cidr.ts","../src/src/cidr/ipv6/constants.ts"],"sourcesContent":[null,null,null,null,null],"names":["MAX","address","ipAddress"],"mappings":"MAAa,qBAAsB,SAAQ,KAAK;IAC9C,YAAY,KAAa;QACvB,KAAK,CAAC,GAAG,KAAK,+BAA+B,CAAC,CAAA;KAC/C;CACF;MAEY,qBAAsB,SAAQ,KAAK;IAC9C,YAAY,OAAe;QACzB,KAAK,CAAC,GAAG,OAAO,kCAAkC,CAAC,CAAA;KACpD;iJCTH;;;AAGO,MAAMA,KAAG,GAAG,EAAE,IAAI,GAAG,GAAG,EAAE,CCCjC,MAAM,cAAc,GAAG,IAAI,CAAA;AAE3B;;;;;;;MAOa,WAAW;IACd,QAAQ,CAAQ;IAExB,YAAmB,OAAoB;QACrC,IAAI,CAAC,QAAQ,GAAG,OAAO,OAAO,KAAK,QAAQ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AAW,GAAGA,SAAO,GAAGC,OAAS,CAACD,SAAO,CAAC,CAAA;QACxE;;QAEE,EAAE,CAAC,OAAO,IAAI,IAAI,CAAC,UAAU,CAAC,OAAO,IAAI,EAAE,CAAC,OAAO,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC,UAAU,CAAC,OAAO,EAC3F;KACF;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;SAuBgB,IAAI,CAAC,SAAiB;IACpC,OAAO,IAAI,QAAQ,CAAC,SAAS,CAAC,CAAA;AAChC,wLCvHA;;;AAGA;AACO,MAAM,GAAG,GAAW,mBAAsB"}
|
package/build/index.js
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
|
-
'use strict';Object.defineProperty(exports,'__esModule',{value:true})
|
|
2
|
-
* The numerical maximum size an IPv4 address can be
|
|
3
|
-
*/
|
|
4
|
-
const IPv4_MAX = 2n ** 32n;
|
|
5
|
-
/**
|
|
6
|
-
* The numerical maximum size an IPv6 address can be
|
|
7
|
-
*/
|
|
8
|
-
const IPv6_MAX = 2n ** 64n;class InvalidIpAddressError extends Error {
|
|
1
|
+
'use strict';Object.defineProperty(exports,'__esModule',{value:true});class InvalidIpAddressError extends Error {
|
|
9
2
|
constructor(badIp) {
|
|
10
|
-
super(`${badIp} is not a valid
|
|
3
|
+
super(`${badIp} is not a valid IPv4 address.`);
|
|
11
4
|
}
|
|
12
5
|
}
|
|
13
|
-
class
|
|
6
|
+
class InvalidCidrBlockError extends Error {
|
|
14
7
|
constructor(badCidr) {
|
|
15
|
-
super(`${badCidr} is not a valid cidr block
|
|
8
|
+
super(`${badCidr} is not a valid IPv4 cidr block.`);
|
|
16
9
|
}
|
|
17
|
-
}
|
|
10
|
+
}var errors=/*#__PURE__*/Object.freeze({__proto__:null,InvalidIpAddressError:InvalidIpAddressError,InvalidCidrBlockError:InvalidCidrBlockError});/**
|
|
11
|
+
* The numerical maximum size an IPv4 address can be
|
|
12
|
+
*/
|
|
13
|
+
const MAX$1 = 2n ** 32n - 1n;const MAX_OCTET_SIZE = 255n;
|
|
18
14
|
/**
|
|
19
15
|
* Representation of an IPv4 address. Provides various utility methods like equality
|
|
20
16
|
* checking.
|
|
@@ -23,26 +19,84 @@ class InvalidCidrError extends Error {
|
|
|
23
19
|
* Direct instantiation should be avoided; use {@link ipv4.address|ipv4.address()} instead.
|
|
24
20
|
*/
|
|
25
21
|
class Ipv4Address {
|
|
26
|
-
|
|
22
|
+
_address;
|
|
27
23
|
constructor(address) {
|
|
28
|
-
this.
|
|
24
|
+
this._address = typeof address === 'bigint' ? address : stringToNum(address);
|
|
29
25
|
}
|
|
30
26
|
/**
|
|
27
|
+
* The address as a big integer
|
|
28
|
+
*/
|
|
29
|
+
get address() {
|
|
30
|
+
return this._address;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
36
|
+
*
|
|
37
|
+
* ip.address(255n) // ==> '0.0.0.255'
|
|
38
|
+
* ip.address(0b11111111_00000000_11111111_00000000n) // ==> '255.0.255.0'
|
|
39
|
+
* ````
|
|
40
|
+
*
|
|
31
41
|
* @public
|
|
32
42
|
* @returns the IPv4 address as a string
|
|
33
43
|
*/
|
|
34
44
|
toString() {
|
|
35
|
-
return
|
|
45
|
+
return numToString(this._address);
|
|
36
46
|
}
|
|
37
47
|
/**
|
|
38
48
|
* Compares if two IP address are the same.
|
|
39
49
|
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
53
|
+
*
|
|
54
|
+
* function isLoopback(address: Ipv4Representable) {
|
|
55
|
+
* return ip.address(address).equals('127.0.0.1')
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
40
59
|
* @public
|
|
41
60
|
* @param otherIpAddress the other IPv4 address to compare
|
|
42
61
|
* @returns if the other IP address is the same
|
|
43
62
|
*/
|
|
44
63
|
equals(otherIpAddress) {
|
|
45
|
-
|
|
64
|
+
if (otherIpAddress instanceof Ipv4Address) {
|
|
65
|
+
return this._address === otherIpAddress._address;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
return this._address === address(otherIpAddress)._address;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
75
|
+
*
|
|
76
|
+
* const myIp = ip.address('52.89.32.255')
|
|
77
|
+
* myIp.nextIp() // ==> '52.89.33.0
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @public
|
|
81
|
+
* @returns the next consecutive IPv4 address
|
|
82
|
+
*/
|
|
83
|
+
nextIp() {
|
|
84
|
+
return address(this._address + 1n);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
90
|
+
*
|
|
91
|
+
* const myIp = ip.address('52.89.32.19')
|
|
92
|
+
* myIp.previousIp() // ==> '52.89.32.18
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @public
|
|
96
|
+
* @returns the preceding IPv4 address
|
|
97
|
+
*/
|
|
98
|
+
previousIp() {
|
|
99
|
+
return address(this._address - 1n);
|
|
46
100
|
}
|
|
47
101
|
}
|
|
48
102
|
/**
|
|
@@ -58,9 +112,9 @@ class Ipv4Address {
|
|
|
58
112
|
* @example
|
|
59
113
|
*
|
|
60
114
|
* ```typescript
|
|
61
|
-
* import
|
|
115
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
62
116
|
*
|
|
63
|
-
* const localhost =
|
|
117
|
+
* const localhost = ip.address('127.0.0.1')
|
|
64
118
|
* ```
|
|
65
119
|
*
|
|
66
120
|
* @see {@link Ipv4Address}
|
|
@@ -74,29 +128,23 @@ function address(ip) {
|
|
|
74
128
|
/**
|
|
75
129
|
* Converts the string representation of an IPv4 address to a BigInt.
|
|
76
130
|
*
|
|
77
|
-
* @remarks
|
|
78
|
-
*
|
|
79
|
-
* Conversion must be between string and bigint instead of integer as
|
|
80
|
-
* on 32-bit systems, Node.js may allocate the number as a 32-bit integer.
|
|
81
|
-
* To avoid this behavior, a BigInt is always used instead.
|
|
82
|
-
*
|
|
83
131
|
* @example
|
|
84
132
|
*
|
|
85
133
|
* ```typescript
|
|
86
134
|
* import * as cidr from 'cidr-block'
|
|
87
135
|
*
|
|
88
|
-
* cidr.ipv4.
|
|
89
|
-
* cidr.ipv4.
|
|
136
|
+
* cidr.ipv4.stringToNum('255.255.255.255') === 4_294_967_295n // ==> true
|
|
137
|
+
* cidr.ipv4.stringToNum('0.0.0.255') === 255n // ==> true
|
|
90
138
|
* ```
|
|
91
139
|
*
|
|
92
|
-
* @see This method is the inverse of {@link ipv4.
|
|
140
|
+
* @see This method is the inverse of {@link ipv4.numToString}
|
|
93
141
|
* @throws {@link InvalidIpAddressError}
|
|
94
142
|
*
|
|
95
143
|
* @public
|
|
96
144
|
* @param address IPv4 address represented as a string
|
|
97
145
|
* @returns numerical BigInt representation of the address
|
|
98
146
|
*/
|
|
99
|
-
function
|
|
147
|
+
function stringToNum(address) {
|
|
100
148
|
try {
|
|
101
149
|
let [firstOctet, secondOctet, thirdOctet, fourthOctet] = address
|
|
102
150
|
.split('.')
|
|
@@ -115,36 +163,142 @@ function fromString(address) {
|
|
|
115
163
|
/**
|
|
116
164
|
* Converts the numerical BigInt representation of an IPv4 address to its string representation.
|
|
117
165
|
*
|
|
118
|
-
* @remarks
|
|
119
|
-
*
|
|
120
|
-
* Conversion must be between string and bigint instead of integer as
|
|
121
|
-
* on 32-bit systems, Node.js may allocate the number as a 32-bit integer.
|
|
122
|
-
* To avoid this behavior, a BigInt is always used instead.
|
|
123
|
-
*
|
|
124
166
|
* @example
|
|
125
167
|
*
|
|
126
168
|
* ```typescript
|
|
127
169
|
* import * as cidr from 'cidr-block'
|
|
128
170
|
*
|
|
129
|
-
* cidr.ipv4.
|
|
130
|
-
* cidr.ipv4.
|
|
131
|
-
* cidr.ipv4.
|
|
171
|
+
* cidr.ipv4.numToString(0n) === '0.0.0.0' // ==> true
|
|
172
|
+
* cidr.ipv4.numToString(65_280n) === '0.0.255.0' // ==> true
|
|
173
|
+
* cidr.ipv4.numToString(4_294_967_295n) === '255.255.255.255' // ==> true
|
|
132
174
|
* ```
|
|
133
175
|
*
|
|
134
|
-
* @see This method is the inverse of {@link ipv4.
|
|
176
|
+
* @see This method is the inverse of {@link ipv4.stringToNum}
|
|
135
177
|
* @throws {@link InvalidIpAddressError}
|
|
136
178
|
*
|
|
137
179
|
* @public
|
|
138
180
|
* @param ip IPv4 address as a BigInt
|
|
139
181
|
* @returns string representation of the address
|
|
140
182
|
*/
|
|
141
|
-
function
|
|
142
|
-
|
|
183
|
+
function numToString(ip) {
|
|
184
|
+
try {
|
|
185
|
+
if (ip < 0 || ip > MAX$1) {
|
|
186
|
+
throw new Error();
|
|
187
|
+
}
|
|
188
|
+
const firstOctet = (ip >> 24n) & MAX_OCTET_SIZE;
|
|
189
|
+
const secondOctet = (ip >> 16n) & MAX_OCTET_SIZE;
|
|
190
|
+
const thirdOctet = (ip >> 8n) & MAX_OCTET_SIZE;
|
|
191
|
+
const fourthOctet = ip & MAX_OCTET_SIZE;
|
|
192
|
+
return `${firstOctet}.${secondOctet}.${thirdOctet}.${fourthOctet}`;
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
143
195
|
throw new InvalidIpAddressError(ip.toString());
|
|
144
196
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
197
|
+
}class Ipv4Cidr {
|
|
198
|
+
_ipAddress;
|
|
199
|
+
_maskSize;
|
|
200
|
+
// TODO: Allow wider-range of values that can be used to create a cidr
|
|
201
|
+
constructor(cidrRange) {
|
|
202
|
+
try {
|
|
203
|
+
const [address$1, subnetMask] = cidrRange.split('/');
|
|
204
|
+
this._ipAddress = address(address$1);
|
|
205
|
+
this._maskSize = Number(subnetMask);
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
throw new InvalidCidrBlockError(cidrRange);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* The size of the cidr netmask (the number after the slash in cidr notation)
|
|
213
|
+
*/
|
|
214
|
+
get maskSize() {
|
|
215
|
+
return this._maskSize;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Number of IP addresses within the cidr range
|
|
219
|
+
*/
|
|
220
|
+
get allocatableIpCount() {
|
|
221
|
+
return 2n ** this.addressLength;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* The actual IPv4 netmask address
|
|
225
|
+
*/
|
|
226
|
+
get netmask() {
|
|
227
|
+
return address((2n ** BigInt(this.maskSize) - 1n) << this.addressLength);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* The first IPv4 address that is usable within the given cidr range
|
|
231
|
+
*/
|
|
232
|
+
get firstUsableIp() {
|
|
233
|
+
return address(this._ipAddress.address);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* The last IPv4 address that is usable within the given cidr range
|
|
237
|
+
*/
|
|
238
|
+
get lastUsableIp() {
|
|
239
|
+
// FIXME: Handle edge case of when cidr range goes outside valid ip range
|
|
240
|
+
return address(this._ipAddress.address + 2n ** this.addressLength - 1n);
|
|
241
|
+
}
|
|
242
|
+
get addressLength() {
|
|
243
|
+
return BigInt(Math.abs(32 - this._maskSize));
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* @returns string representation of the cidr range
|
|
247
|
+
*/
|
|
248
|
+
toString() {
|
|
249
|
+
return `${this._ipAddress.toString()}/${this._maskSize}`;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* @returns the next consecutive cidr block
|
|
253
|
+
*/
|
|
254
|
+
nextBlock(ofSize) {
|
|
255
|
+
const nextIp = this._ipAddress.address + 2n ** this.addressLength;
|
|
256
|
+
return cidr(`${numToString(nextIp)}/${ofSize ?? this._maskSize}`);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* @returns the previous cidr block
|
|
260
|
+
*/
|
|
261
|
+
previousBlock() {
|
|
262
|
+
const nextIp = this._ipAddress.address - 2n ** this.addressLength;
|
|
263
|
+
return cidr(`${numToString(nextIp)}/${this._maskSize}`);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* @returns if the given IPv4 address is within the cidr range
|
|
267
|
+
*/
|
|
268
|
+
includes(address$1) {
|
|
269
|
+
const ip = address$1 instanceof Ipv4Address ? address$1 : address(address$1);
|
|
270
|
+
return (
|
|
271
|
+
// FIXME: How to handle edge case of next block erroring out?
|
|
272
|
+
ip.address >= this._ipAddress.address && ip.address <= this.nextBlock()._ipAddress.address);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Convenience function for creating an IPv4 cidr range instance.
|
|
277
|
+
*
|
|
278
|
+
* @remarks
|
|
279
|
+
*
|
|
280
|
+
* In general, you should use this function instead of instantiating an Ipv4Cidr
|
|
281
|
+
* object directly. While there is nothing wrong with direct instantiation, convenience
|
|
282
|
+
* methods like these are meant to help reduce the footprint of your code and increase
|
|
283
|
+
* readability.
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
*
|
|
287
|
+
* ```typescript
|
|
288
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
289
|
+
*
|
|
290
|
+
* const vpcCidrRange = ip.cidr('10.0.0.0/16')
|
|
291
|
+
* ```
|
|
292
|
+
*
|
|
293
|
+
* @see {@link Ipv4Cidr}
|
|
294
|
+
*
|
|
295
|
+
* @param cidrRange string representation of the cidr range
|
|
296
|
+
* @returns an instance of Ipv4Cidr
|
|
297
|
+
*/
|
|
298
|
+
function cidr(cidrRange) {
|
|
299
|
+
return new Ipv4Cidr(cidrRange);
|
|
300
|
+
}var index$1=/*#__PURE__*/Object.freeze({__proto__:null,Ipv4Address:Ipv4Address,address:address,stringToNum:stringToNum,numToString:numToString,Ipv4Cidr:Ipv4Cidr,cidr:cidr,MAX:MAX$1});/**
|
|
301
|
+
* The numerical maximum size an IPv6 address can be
|
|
302
|
+
*/
|
|
303
|
+
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
|
304
|
+
const MAX = 0xffffffffffffffffn;var index=/*#__PURE__*/Object.freeze({__proto__:null,MAX:MAX});exports.errors=errors;exports.ipv4=index$1;exports.ipv6=index;//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/cidr/
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/src/cidr/errors.ts","../src/src/cidr/ipv4/constants.ts","../src/src/cidr/ipv4/ipv4-address.ts","../src/src/cidr/ipv4/ipv4-cidr.ts","../src/src/cidr/ipv6/constants.ts"],"sourcesContent":[null,null,null,null,null],"names":["MAX","address","ipAddress"],"mappings":"4EAAa,qBAAsB,SAAQ,KAAK;IAC9C,YAAY,KAAa;QACvB,KAAK,CAAC,GAAG,KAAK,+BAA+B,CAAC,CAAA;KAC/C;CACF;MAEY,qBAAsB,SAAQ,KAAK;IAC9C,YAAY,OAAe;QACzB,KAAK,CAAC,GAAG,OAAO,kCAAkC,CAAC,CAAA;KACpD;iJCTH;;;AAGO,MAAMA,KAAG,GAAG,EAAE,IAAI,GAAG,GAAG,EAAE,CCCjC,MAAM,cAAc,GAAG,IAAI,CAAA;AAE3B;;;;;;;MAOa,WAAW;IACd,QAAQ,CAAQ;IAExB,YAAmB,OAAoB;QACrC,IAAI,CAAC,QAAQ,GAAG,OAAO,OAAO,KAAK,QAAQ,GAAG,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,CAAA;KAC7E;;;;IAKD,IAAW,OAAO;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAA;KAC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AO,IAAI,EAAE,CAAC,OAAO,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC,UAAU,CAAC,OAAO,EAC3F;KACF;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;SAuBgB,IAAI,CAAC,SAAiB;IACpC,OAAO,IAAI,QAAQ,CAAC,SAAS,CAAC,CAAA;AAChC,wLCvHA;;;AAGA;AACO,MAAM,GAAG,GAAW,mBAAsB"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cidr-block",
|
|
3
|
-
"description": "
|
|
4
|
-
"version": "1.0
|
|
3
|
+
"description": "ipv4 and ipv6 address and cidr range utilities",
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Brandon Burrus <brandon@burrus.io>",
|
|
7
7
|
"homepage": "https://cidr-block.com",
|
|
@@ -14,13 +14,14 @@
|
|
|
14
14
|
},
|
|
15
15
|
"main": "build/index.js",
|
|
16
16
|
"module": "build/index.esm.js",
|
|
17
|
-
"types": "build/
|
|
17
|
+
"types": "build/index.d.ts",
|
|
18
18
|
"files": [
|
|
19
19
|
"build"
|
|
20
20
|
],
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "rollup -c",
|
|
23
23
|
"build:watch": "rollup -cw",
|
|
24
|
+
"predocs": "rimraf docs",
|
|
24
25
|
"docs": "typedoc src/index.ts",
|
|
25
26
|
"lint": "eslint . --ext .ts",
|
|
26
27
|
"lint:fix": "eslint . --fix --ext .ts",
|
|
@@ -28,6 +29,9 @@
|
|
|
28
29
|
"format:fix": "prettier --write **/*.ts",
|
|
29
30
|
"prebuild": "rimraf build",
|
|
30
31
|
"test": "jest",
|
|
32
|
+
"test:ci": "jest --verbose --coverage",
|
|
33
|
+
"test:cov": "jest --coverage",
|
|
34
|
+
"test:verbose": "jest --verbose",
|
|
31
35
|
"test:watch": "jest --watch",
|
|
32
36
|
"test:ui": "majestic"
|
|
33
37
|
},
|
|
@@ -67,6 +71,16 @@
|
|
|
67
71
|
"testEnvironment": "node",
|
|
68
72
|
"transform": {
|
|
69
73
|
"^.+\\.ts$": "ts-jest"
|
|
74
|
+
},
|
|
75
|
+
"collectCoverageFrom": [
|
|
76
|
+
"<rootDir>/src/**/*"
|
|
77
|
+
],
|
|
78
|
+
"coverageThreshold": {
|
|
79
|
+
"global": {
|
|
80
|
+
"branches": 100,
|
|
81
|
+
"functions": 100,
|
|
82
|
+
"lines": 100
|
|
83
|
+
}
|
|
70
84
|
}
|
|
71
85
|
},
|
|
72
86
|
"eslintConfig": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './ipv4-address';
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Representation of an IPv4 address. Provides various utility methods like equality
|
|
3
|
-
* checking.
|
|
4
|
-
*
|
|
5
|
-
* @remarks
|
|
6
|
-
* Direct instantiation should be avoided; use {@link ipv4.address|ipv4.address()} instead.
|
|
7
|
-
*/
|
|
8
|
-
export declare class Ipv4Address {
|
|
9
|
-
private address;
|
|
10
|
-
constructor(address: string);
|
|
11
|
-
/**
|
|
12
|
-
* @public
|
|
13
|
-
* @returns the IPv4 address as a string
|
|
14
|
-
*/
|
|
15
|
-
toString(): string;
|
|
16
|
-
/**
|
|
17
|
-
* Compares if two IP address are the same.
|
|
18
|
-
*
|
|
19
|
-
* @public
|
|
20
|
-
* @param otherIpAddress the other IPv4 address to compare
|
|
21
|
-
* @returns if the other IP address is the same
|
|
22
|
-
*/
|
|
23
|
-
equals(otherIpAddress: Ipv4Address): boolean;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Convenience function for creating an IPv4 address instance.
|
|
27
|
-
*
|
|
28
|
-
* @remarks
|
|
29
|
-
*
|
|
30
|
-
* In general, you should use this function instead of instantiating an Ipv4Address
|
|
31
|
-
* object directly. While there is nothing wrong with direct instantiation, convenience
|
|
32
|
-
* methods like these are meant to help reduce the footprint of your code and increase
|
|
33
|
-
* readability.
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
*
|
|
37
|
-
* ```typescript
|
|
38
|
-
* import * as cidr from 'cidr-block'
|
|
39
|
-
*
|
|
40
|
-
* const localhost = cidr.ipv4.address('172.0.0.0')
|
|
41
|
-
* ```
|
|
42
|
-
*
|
|
43
|
-
* @see {@link Ipv4Address}
|
|
44
|
-
*
|
|
45
|
-
* @param ip string representation of the IPv4 address
|
|
46
|
-
* @returns an instance of Ipv4Address
|
|
47
|
-
*/
|
|
48
|
-
export declare function address(ip: string): Ipv4Address;
|
|
49
|
-
/**
|
|
50
|
-
* Converts the string representation of an IPv4 address to a BigInt.
|
|
51
|
-
*
|
|
52
|
-
* @remarks
|
|
53
|
-
*
|
|
54
|
-
* Conversion must be between string and bigint instead of integer as
|
|
55
|
-
* on 32-bit systems, Node.js may allocate the number as a 32-bit integer.
|
|
56
|
-
* To avoid this behavior, a BigInt is always used instead.
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
*
|
|
60
|
-
* ```typescript
|
|
61
|
-
* import * as cidr from 'cidr-block'
|
|
62
|
-
*
|
|
63
|
-
* cidr.ipv4.fromString('255.255.255.255') === 4_294_967_295n // ==> true
|
|
64
|
-
* cidr.ipv4.fromString('0.0.0.255') === 255n // ==> true
|
|
65
|
-
* ```
|
|
66
|
-
*
|
|
67
|
-
* @see This method is the inverse of {@link ipv4.toString|ipv4.toString()}
|
|
68
|
-
* @throws {@link InvalidIpAddressError}
|
|
69
|
-
*
|
|
70
|
-
* @public
|
|
71
|
-
* @param address IPv4 address represented as a string
|
|
72
|
-
* @returns numerical BigInt representation of the address
|
|
73
|
-
*/
|
|
74
|
-
export declare function fromString(address: string): bigint;
|
|
75
|
-
/**
|
|
76
|
-
* Converts the numerical BigInt representation of an IPv4 address to its string representation.
|
|
77
|
-
*
|
|
78
|
-
* @remarks
|
|
79
|
-
*
|
|
80
|
-
* Conversion must be between string and bigint instead of integer as
|
|
81
|
-
* on 32-bit systems, Node.js may allocate the number as a 32-bit integer.
|
|
82
|
-
* To avoid this behavior, a BigInt is always used instead.
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
*
|
|
86
|
-
* ```typescript
|
|
87
|
-
* import * as cidr from 'cidr-block'
|
|
88
|
-
*
|
|
89
|
-
* cidr.ipv4.toString(0n) === '0.0.0.0' // ==> true
|
|
90
|
-
* cidr.ipv4.toString(65_280n) === '0.0.255.0' // ==> true
|
|
91
|
-
* cidr.ipv4.toString(4_294_967_295n) === '255.255.255.255' // ==> true
|
|
92
|
-
* ```
|
|
93
|
-
*
|
|
94
|
-
* @see This method is the inverse of {@link ipv4.fromString|ipv4.fromString()}
|
|
95
|
-
* @throws {@link InvalidIpAddressError}
|
|
96
|
-
*
|
|
97
|
-
* @public
|
|
98
|
-
* @param ip IPv4 address as a BigInt
|
|
99
|
-
* @returns string representation of the address
|
|
100
|
-
*/
|
|
101
|
-
export declare function toString(ip: bigint): string;
|
package/build/src/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|