cidr-block 1.0.0 → 1.3.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/cidr/errors.d.ts +6 -0
- 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/cidr/ipv6/ipv6-address.d.ts +13 -0
- package/build/cidr/ipv6/types.d.ts +9 -0
- package/build/index.d.ts +3 -0
- package/build/index.esm.js +306 -1
- package/build/index.esm.js.map +1 -1
- package/build/index.js +306 -1
- package/build/index.js.map +1 -1
- package/package.json +22 -7
- package/build/src/index.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} instead.
|
|
8
|
+
*/
|
|
9
|
+
export declare class Ipv4Address {
|
|
10
|
+
private _address;
|
|
11
|
+
constructor(address: Ipv4Literal);
|
|
12
|
+
/**
|
|
13
|
+
* The address as a number
|
|
14
|
+
*/
|
|
15
|
+
get address(): number;
|
|
16
|
+
/**
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
20
|
+
*
|
|
21
|
+
* ip.address(255) // ==> '0.0.0.255'
|
|
22
|
+
* ip.address(0b11111111_00000000_11111111_00000000) // ==> '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 number.
|
|
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_295 // ==> true
|
|
106
|
+
* cidr.ipv4.stringToNum('0.0.0.255') === 255 // ==> 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 number representation of the address
|
|
115
|
+
*/
|
|
116
|
+
export declare function stringToNum(address: string): number;
|
|
117
|
+
/**
|
|
118
|
+
* Converts the numerical number 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(0) === '0.0.0.0' // ==> true
|
|
126
|
+
* cidr.ipv4.numToString(65_280) === '0.0.255.0' // ==> true
|
|
127
|
+
* cidr.ipv4.numToString(4_294_967_295) === '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 number
|
|
135
|
+
* @returns string representation of the address
|
|
136
|
+
*/
|
|
137
|
+
export declare function numToString(ip: number): 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(): number;
|
|
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 number value that represents an IPv4 address
|
|
4
|
+
*/
|
|
5
|
+
export declare type Ipv4Literal = string | number;
|
|
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';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Ipv6Literal, Ipv6Representable } from './types';
|
|
2
|
+
export declare class Ipv6Address {
|
|
3
|
+
private _address;
|
|
4
|
+
constructor(address: Ipv6Literal);
|
|
5
|
+
get address(): bigint;
|
|
6
|
+
toString(): string;
|
|
7
|
+
equals(otherIpAddress: Ipv6Representable): boolean;
|
|
8
|
+
nextIp(): Ipv6Address;
|
|
9
|
+
previousIp(): Ipv6Address;
|
|
10
|
+
}
|
|
11
|
+
export declare function address(ip: Ipv6Literal): Ipv6Address;
|
|
12
|
+
export declare function stringToNum(address: string): bigint;
|
|
13
|
+
export declare function numToString(num: bigint): string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Ipv6Address } from './ipv6-address';
|
|
2
|
+
/**
|
|
3
|
+
* Type that indicates a literal string or number value that represents an IPv6 address
|
|
4
|
+
*/
|
|
5
|
+
export declare type Ipv6Literal = string | bigint;
|
|
6
|
+
/**
|
|
7
|
+
* Type that indicates either a literal value or an address instance that is an IPv6
|
|
8
|
+
*/
|
|
9
|
+
export declare type Ipv6Representable = Ipv6Address | Ipv6Literal;
|
package/build/index.d.ts
ADDED
package/build/index.esm.js
CHANGED
|
@@ -1 +1,306 @@
|
|
|
1
|
-
|
|
1
|
+
class InvalidIpAddressError extends Error {
|
|
2
|
+
constructor(badIp) {
|
|
3
|
+
super(`${badIp} is not a valid IPv4 address.`);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
class InvalidCidrBlockError extends Error {
|
|
7
|
+
constructor(badCidr) {
|
|
8
|
+
super(`${badCidr} is not a valid IPv4 cidr block.`);
|
|
9
|
+
}
|
|
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 = 2 ** 32 - 1;const MAX_OCTET_SIZE = 255;
|
|
14
|
+
/**
|
|
15
|
+
* Representation of an IPv4 address. Provides various utility methods like equality
|
|
16
|
+
* checking.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* Direct instantiation should be avoided; use {@link ipv4.address} instead.
|
|
20
|
+
*/
|
|
21
|
+
class Ipv4Address {
|
|
22
|
+
_address;
|
|
23
|
+
constructor(address) {
|
|
24
|
+
this._address = typeof address === 'number' ? address : stringToNum(address);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The address as a number
|
|
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(255) // ==> '0.0.0.255'
|
|
38
|
+
* ip.address(0b11111111_00000000_11111111_00000000) // ==> '255.0.255.0'
|
|
39
|
+
* ````
|
|
40
|
+
*
|
|
41
|
+
* @public
|
|
42
|
+
* @returns the IPv4 address as a string
|
|
43
|
+
*/
|
|
44
|
+
toString() {
|
|
45
|
+
return numToString(this._address);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Compares if two IP address are the same.
|
|
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
|
+
*
|
|
59
|
+
* @public
|
|
60
|
+
* @param otherIpAddress the other IPv4 address to compare
|
|
61
|
+
* @returns if the other IP address is the same
|
|
62
|
+
*/
|
|
63
|
+
equals(otherIpAddress) {
|
|
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 + 1);
|
|
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 - 1);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Convenience function for creating an IPv4 address instance.
|
|
104
|
+
*
|
|
105
|
+
* @remarks
|
|
106
|
+
*
|
|
107
|
+
* In general, you should use this function instead of instantiating an Ipv4Address
|
|
108
|
+
* object directly. While there is nothing wrong with direct instantiation, convenience
|
|
109
|
+
* methods like these are meant to help reduce the footprint of your code and increase
|
|
110
|
+
* readability.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
*
|
|
114
|
+
* ```typescript
|
|
115
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
116
|
+
*
|
|
117
|
+
* const localhost = ip.address('127.0.0.1')
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* @see {@link Ipv4Address}
|
|
121
|
+
*
|
|
122
|
+
* @param ip string representation of the IPv4 address
|
|
123
|
+
* @returns an instance of Ipv4Address
|
|
124
|
+
*/
|
|
125
|
+
function address(ip) {
|
|
126
|
+
return new Ipv4Address(ip);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Converts the string representation of an IPv4 address to a number.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
*
|
|
133
|
+
* ```typescript
|
|
134
|
+
* import * as cidr from 'cidr-block'
|
|
135
|
+
*
|
|
136
|
+
* cidr.ipv4.stringToNum('255.255.255.255') === 4_294_967_295 // ==> true
|
|
137
|
+
* cidr.ipv4.stringToNum('0.0.0.255') === 255 // ==> true
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @see This method is the inverse of {@link ipv4.numToString}
|
|
141
|
+
* @throws {@link InvalidIpAddressError}
|
|
142
|
+
*
|
|
143
|
+
* @public
|
|
144
|
+
* @param address IPv4 address represented as a string
|
|
145
|
+
* @returns numerical number representation of the address
|
|
146
|
+
*/
|
|
147
|
+
function stringToNum(address) {
|
|
148
|
+
try {
|
|
149
|
+
if (address.length < 7) {
|
|
150
|
+
throw new Error();
|
|
151
|
+
}
|
|
152
|
+
let octets = address.split('.').map(Number);
|
|
153
|
+
if (octets.some(octet => octet < 0 || octet > MAX_OCTET_SIZE)) {
|
|
154
|
+
throw new Error();
|
|
155
|
+
}
|
|
156
|
+
let [firstOctet, secondOctet, thirdOctet, fourthOctet] = octets;
|
|
157
|
+
firstOctet = (firstOctet << 24) >>> 0;
|
|
158
|
+
secondOctet = (secondOctet << 16) >>> 0;
|
|
159
|
+
thirdOctet = (thirdOctet << 8) >>> 0;
|
|
160
|
+
return firstOctet + secondOctet + thirdOctet + fourthOctet;
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
throw new InvalidIpAddressError(address);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Converts the numerical number representation of an IPv4 address to its string representation.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
*
|
|
171
|
+
* ```typescript
|
|
172
|
+
* import * as cidr from 'cidr-block'
|
|
173
|
+
*
|
|
174
|
+
* cidr.ipv4.numToString(0) === '0.0.0.0' // ==> true
|
|
175
|
+
* cidr.ipv4.numToString(65_280) === '0.0.255.0' // ==> true
|
|
176
|
+
* cidr.ipv4.numToString(4_294_967_295) === '255.255.255.255' // ==> true
|
|
177
|
+
* ```
|
|
178
|
+
*
|
|
179
|
+
* @see This method is the inverse of {@link ipv4.stringToNum}
|
|
180
|
+
* @throws {@link InvalidIpAddressError}
|
|
181
|
+
*
|
|
182
|
+
* @public
|
|
183
|
+
* @param ip IPv4 address as a number
|
|
184
|
+
* @returns string representation of the address
|
|
185
|
+
*/
|
|
186
|
+
function numToString(ip) {
|
|
187
|
+
try {
|
|
188
|
+
if (ip < 0 || ip > MAX$1) {
|
|
189
|
+
throw new Error();
|
|
190
|
+
}
|
|
191
|
+
const firstOctet = (ip >>> 24) & MAX_OCTET_SIZE;
|
|
192
|
+
const secondOctet = (ip >>> 16) & MAX_OCTET_SIZE;
|
|
193
|
+
const thirdOctet = (ip >>> 8) & MAX_OCTET_SIZE;
|
|
194
|
+
const fourthOctet = ip & MAX_OCTET_SIZE;
|
|
195
|
+
return `${firstOctet}.${secondOctet}.${thirdOctet}.${fourthOctet}`;
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
throw new InvalidIpAddressError(ip.toString());
|
|
199
|
+
}
|
|
200
|
+
}class Ipv4Cidr {
|
|
201
|
+
_ipAddress;
|
|
202
|
+
_maskSize;
|
|
203
|
+
// TODO: Allow wider-range of values that can be used to create a cidr
|
|
204
|
+
constructor(cidrRange) {
|
|
205
|
+
try {
|
|
206
|
+
const [address$1, subnetMask] = cidrRange.split('/');
|
|
207
|
+
this._ipAddress = address(address$1);
|
|
208
|
+
this._maskSize = Number(subnetMask);
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
throw new InvalidCidrBlockError(cidrRange);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* The size of the cidr netmask (the number after the slash in cidr notation)
|
|
216
|
+
*/
|
|
217
|
+
get maskSize() {
|
|
218
|
+
return this._maskSize;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Number of IP addresses within the cidr range
|
|
222
|
+
*/
|
|
223
|
+
get allocatableIpCount() {
|
|
224
|
+
return 2 ** this.addressLength;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* The actual IPv4 netmask address
|
|
228
|
+
*/
|
|
229
|
+
get netmask() {
|
|
230
|
+
return address((2 ** this.maskSize - 1) << this.addressLength);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* The first IPv4 address that is usable within the given cidr range
|
|
234
|
+
*/
|
|
235
|
+
get firstUsableIp() {
|
|
236
|
+
return address(this._ipAddress.address);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* The last IPv4 address that is usable within the given cidr range
|
|
240
|
+
*/
|
|
241
|
+
get lastUsableIp() {
|
|
242
|
+
// FIXME: Handle edge case of when cidr range goes outside valid ip range
|
|
243
|
+
return address(this._ipAddress.address + 2 ** this.addressLength - 1);
|
|
244
|
+
}
|
|
245
|
+
get addressLength() {
|
|
246
|
+
return Math.abs(32 - this._maskSize);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* @returns string representation of the cidr range
|
|
250
|
+
*/
|
|
251
|
+
toString() {
|
|
252
|
+
return `${this._ipAddress.toString()}/${this._maskSize}`;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* @returns the next consecutive cidr block
|
|
256
|
+
*/
|
|
257
|
+
nextBlock(ofSize) {
|
|
258
|
+
const nextIp = this._ipAddress.address + 2 ** this.addressLength;
|
|
259
|
+
return cidr(`${numToString(nextIp)}/${ofSize ?? this._maskSize}`);
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* @returns the previous cidr block
|
|
263
|
+
*/
|
|
264
|
+
previousBlock() {
|
|
265
|
+
const nextIp = this._ipAddress.address - 2 ** this.addressLength;
|
|
266
|
+
return cidr(`${numToString(nextIp)}/${this._maskSize}`);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* @returns if the given IPv4 address is within the cidr range
|
|
270
|
+
*/
|
|
271
|
+
includes(address$1) {
|
|
272
|
+
const ip = address$1 instanceof Ipv4Address ? address$1 : address(address$1);
|
|
273
|
+
return (
|
|
274
|
+
// FIXME: How to handle edge case of next block erroring out?
|
|
275
|
+
ip.address >= this._ipAddress.address && ip.address <= this.nextBlock()._ipAddress.address);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Convenience function for creating an IPv4 cidr range instance.
|
|
280
|
+
*
|
|
281
|
+
* @remarks
|
|
282
|
+
*
|
|
283
|
+
* In general, you should use this function instead of instantiating an Ipv4Cidr
|
|
284
|
+
* object directly. While there is nothing wrong with direct instantiation, convenience
|
|
285
|
+
* methods like these are meant to help reduce the footprint of your code and increase
|
|
286
|
+
* readability.
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
*
|
|
290
|
+
* ```typescript
|
|
291
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
292
|
+
*
|
|
293
|
+
* const vpcCidrRange = ip.cidr('10.0.0.0/16')
|
|
294
|
+
* ```
|
|
295
|
+
*
|
|
296
|
+
* @see {@link Ipv4Cidr}
|
|
297
|
+
*
|
|
298
|
+
* @param cidrRange string representation of the cidr range
|
|
299
|
+
* @returns an instance of Ipv4Cidr
|
|
300
|
+
*/
|
|
301
|
+
function cidr(cidrRange) {
|
|
302
|
+
return new Ipv4Cidr(cidrRange);
|
|
303
|
+
}var index$1=/*#__PURE__*/Object.freeze({__proto__:null,Ipv4Address:Ipv4Address,address:address,stringToNum:stringToNum,numToString:numToString,Ipv4Cidr:Ipv4Cidr,cidr:cidr,MAX:MAX$1});/**
|
|
304
|
+
* The numerical maximum size an IPv6 address can be
|
|
305
|
+
*/
|
|
306
|
+
const MAX = 2n ** 128n;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/
|
|
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,GAAW,CAAC,IAAI,EAAE,GAAG,CAAC,CCCtC,MAAM,cAAc,GAAG,GAAG,CAA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AAC,UAAU,CAAC,OAAO,GAAG,CAAC,IAAI,IAAI,CAAC,aAAa,CAAA;QAChE,OAAO,IAAI,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC,CAAA;KACxD;;;;IAKM,QAAQ,CAACD,SAA0B;QACxC,MAAM,EAAE,GAAGA,SAAO,YAAY,WAAW,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;;;AAGO,MAAM,GAAG,GAAG,EAAE,IAAI,IAAI"}
|
package/build/index.js
CHANGED
|
@@ -1 +1,306 @@
|
|
|
1
|
-
'use strict';Object.defineProperty(exports,'__esModule',{value:true});
|
|
1
|
+
'use strict';Object.defineProperty(exports,'__esModule',{value:true});class InvalidIpAddressError extends Error {
|
|
2
|
+
constructor(badIp) {
|
|
3
|
+
super(`${badIp} is not a valid IPv4 address.`);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
class InvalidCidrBlockError extends Error {
|
|
7
|
+
constructor(badCidr) {
|
|
8
|
+
super(`${badCidr} is not a valid IPv4 cidr block.`);
|
|
9
|
+
}
|
|
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 = 2 ** 32 - 1;const MAX_OCTET_SIZE = 255;
|
|
14
|
+
/**
|
|
15
|
+
* Representation of an IPv4 address. Provides various utility methods like equality
|
|
16
|
+
* checking.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* Direct instantiation should be avoided; use {@link ipv4.address} instead.
|
|
20
|
+
*/
|
|
21
|
+
class Ipv4Address {
|
|
22
|
+
_address;
|
|
23
|
+
constructor(address) {
|
|
24
|
+
this._address = typeof address === 'number' ? address : stringToNum(address);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The address as a number
|
|
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(255) // ==> '0.0.0.255'
|
|
38
|
+
* ip.address(0b11111111_00000000_11111111_00000000) // ==> '255.0.255.0'
|
|
39
|
+
* ````
|
|
40
|
+
*
|
|
41
|
+
* @public
|
|
42
|
+
* @returns the IPv4 address as a string
|
|
43
|
+
*/
|
|
44
|
+
toString() {
|
|
45
|
+
return numToString(this._address);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Compares if two IP address are the same.
|
|
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
|
+
*
|
|
59
|
+
* @public
|
|
60
|
+
* @param otherIpAddress the other IPv4 address to compare
|
|
61
|
+
* @returns if the other IP address is the same
|
|
62
|
+
*/
|
|
63
|
+
equals(otherIpAddress) {
|
|
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 + 1);
|
|
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 - 1);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Convenience function for creating an IPv4 address instance.
|
|
104
|
+
*
|
|
105
|
+
* @remarks
|
|
106
|
+
*
|
|
107
|
+
* In general, you should use this function instead of instantiating an Ipv4Address
|
|
108
|
+
* object directly. While there is nothing wrong with direct instantiation, convenience
|
|
109
|
+
* methods like these are meant to help reduce the footprint of your code and increase
|
|
110
|
+
* readability.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
*
|
|
114
|
+
* ```typescript
|
|
115
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
116
|
+
*
|
|
117
|
+
* const localhost = ip.address('127.0.0.1')
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* @see {@link Ipv4Address}
|
|
121
|
+
*
|
|
122
|
+
* @param ip string representation of the IPv4 address
|
|
123
|
+
* @returns an instance of Ipv4Address
|
|
124
|
+
*/
|
|
125
|
+
function address(ip) {
|
|
126
|
+
return new Ipv4Address(ip);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Converts the string representation of an IPv4 address to a number.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
*
|
|
133
|
+
* ```typescript
|
|
134
|
+
* import * as cidr from 'cidr-block'
|
|
135
|
+
*
|
|
136
|
+
* cidr.ipv4.stringToNum('255.255.255.255') === 4_294_967_295 // ==> true
|
|
137
|
+
* cidr.ipv4.stringToNum('0.0.0.255') === 255 // ==> true
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @see This method is the inverse of {@link ipv4.numToString}
|
|
141
|
+
* @throws {@link InvalidIpAddressError}
|
|
142
|
+
*
|
|
143
|
+
* @public
|
|
144
|
+
* @param address IPv4 address represented as a string
|
|
145
|
+
* @returns numerical number representation of the address
|
|
146
|
+
*/
|
|
147
|
+
function stringToNum(address) {
|
|
148
|
+
try {
|
|
149
|
+
if (address.length < 7) {
|
|
150
|
+
throw new Error();
|
|
151
|
+
}
|
|
152
|
+
let octets = address.split('.').map(Number);
|
|
153
|
+
if (octets.some(octet => octet < 0 || octet > MAX_OCTET_SIZE)) {
|
|
154
|
+
throw new Error();
|
|
155
|
+
}
|
|
156
|
+
let [firstOctet, secondOctet, thirdOctet, fourthOctet] = octets;
|
|
157
|
+
firstOctet = (firstOctet << 24) >>> 0;
|
|
158
|
+
secondOctet = (secondOctet << 16) >>> 0;
|
|
159
|
+
thirdOctet = (thirdOctet << 8) >>> 0;
|
|
160
|
+
return firstOctet + secondOctet + thirdOctet + fourthOctet;
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
throw new InvalidIpAddressError(address);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Converts the numerical number representation of an IPv4 address to its string representation.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
*
|
|
171
|
+
* ```typescript
|
|
172
|
+
* import * as cidr from 'cidr-block'
|
|
173
|
+
*
|
|
174
|
+
* cidr.ipv4.numToString(0) === '0.0.0.0' // ==> true
|
|
175
|
+
* cidr.ipv4.numToString(65_280) === '0.0.255.0' // ==> true
|
|
176
|
+
* cidr.ipv4.numToString(4_294_967_295) === '255.255.255.255' // ==> true
|
|
177
|
+
* ```
|
|
178
|
+
*
|
|
179
|
+
* @see This method is the inverse of {@link ipv4.stringToNum}
|
|
180
|
+
* @throws {@link InvalidIpAddressError}
|
|
181
|
+
*
|
|
182
|
+
* @public
|
|
183
|
+
* @param ip IPv4 address as a number
|
|
184
|
+
* @returns string representation of the address
|
|
185
|
+
*/
|
|
186
|
+
function numToString(ip) {
|
|
187
|
+
try {
|
|
188
|
+
if (ip < 0 || ip > MAX$1) {
|
|
189
|
+
throw new Error();
|
|
190
|
+
}
|
|
191
|
+
const firstOctet = (ip >>> 24) & MAX_OCTET_SIZE;
|
|
192
|
+
const secondOctet = (ip >>> 16) & MAX_OCTET_SIZE;
|
|
193
|
+
const thirdOctet = (ip >>> 8) & MAX_OCTET_SIZE;
|
|
194
|
+
const fourthOctet = ip & MAX_OCTET_SIZE;
|
|
195
|
+
return `${firstOctet}.${secondOctet}.${thirdOctet}.${fourthOctet}`;
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
throw new InvalidIpAddressError(ip.toString());
|
|
199
|
+
}
|
|
200
|
+
}class Ipv4Cidr {
|
|
201
|
+
_ipAddress;
|
|
202
|
+
_maskSize;
|
|
203
|
+
// TODO: Allow wider-range of values that can be used to create a cidr
|
|
204
|
+
constructor(cidrRange) {
|
|
205
|
+
try {
|
|
206
|
+
const [address$1, subnetMask] = cidrRange.split('/');
|
|
207
|
+
this._ipAddress = address(address$1);
|
|
208
|
+
this._maskSize = Number(subnetMask);
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
throw new InvalidCidrBlockError(cidrRange);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* The size of the cidr netmask (the number after the slash in cidr notation)
|
|
216
|
+
*/
|
|
217
|
+
get maskSize() {
|
|
218
|
+
return this._maskSize;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Number of IP addresses within the cidr range
|
|
222
|
+
*/
|
|
223
|
+
get allocatableIpCount() {
|
|
224
|
+
return 2 ** this.addressLength;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* The actual IPv4 netmask address
|
|
228
|
+
*/
|
|
229
|
+
get netmask() {
|
|
230
|
+
return address((2 ** this.maskSize - 1) << this.addressLength);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* The first IPv4 address that is usable within the given cidr range
|
|
234
|
+
*/
|
|
235
|
+
get firstUsableIp() {
|
|
236
|
+
return address(this._ipAddress.address);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* The last IPv4 address that is usable within the given cidr range
|
|
240
|
+
*/
|
|
241
|
+
get lastUsableIp() {
|
|
242
|
+
// FIXME: Handle edge case of when cidr range goes outside valid ip range
|
|
243
|
+
return address(this._ipAddress.address + 2 ** this.addressLength - 1);
|
|
244
|
+
}
|
|
245
|
+
get addressLength() {
|
|
246
|
+
return Math.abs(32 - this._maskSize);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* @returns string representation of the cidr range
|
|
250
|
+
*/
|
|
251
|
+
toString() {
|
|
252
|
+
return `${this._ipAddress.toString()}/${this._maskSize}`;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* @returns the next consecutive cidr block
|
|
256
|
+
*/
|
|
257
|
+
nextBlock(ofSize) {
|
|
258
|
+
const nextIp = this._ipAddress.address + 2 ** this.addressLength;
|
|
259
|
+
return cidr(`${numToString(nextIp)}/${ofSize ?? this._maskSize}`);
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* @returns the previous cidr block
|
|
263
|
+
*/
|
|
264
|
+
previousBlock() {
|
|
265
|
+
const nextIp = this._ipAddress.address - 2 ** this.addressLength;
|
|
266
|
+
return cidr(`${numToString(nextIp)}/${this._maskSize}`);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* @returns if the given IPv4 address is within the cidr range
|
|
270
|
+
*/
|
|
271
|
+
includes(address$1) {
|
|
272
|
+
const ip = address$1 instanceof Ipv4Address ? address$1 : address(address$1);
|
|
273
|
+
return (
|
|
274
|
+
// FIXME: How to handle edge case of next block erroring out?
|
|
275
|
+
ip.address >= this._ipAddress.address && ip.address <= this.nextBlock()._ipAddress.address);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Convenience function for creating an IPv4 cidr range instance.
|
|
280
|
+
*
|
|
281
|
+
* @remarks
|
|
282
|
+
*
|
|
283
|
+
* In general, you should use this function instead of instantiating an Ipv4Cidr
|
|
284
|
+
* object directly. While there is nothing wrong with direct instantiation, convenience
|
|
285
|
+
* methods like these are meant to help reduce the footprint of your code and increase
|
|
286
|
+
* readability.
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
*
|
|
290
|
+
* ```typescript
|
|
291
|
+
* import { ipv4 as ip } from 'cidr-block'
|
|
292
|
+
*
|
|
293
|
+
* const vpcCidrRange = ip.cidr('10.0.0.0/16')
|
|
294
|
+
* ```
|
|
295
|
+
*
|
|
296
|
+
* @see {@link Ipv4Cidr}
|
|
297
|
+
*
|
|
298
|
+
* @param cidrRange string representation of the cidr range
|
|
299
|
+
* @returns an instance of Ipv4Cidr
|
|
300
|
+
*/
|
|
301
|
+
function cidr(cidrRange) {
|
|
302
|
+
return new Ipv4Cidr(cidrRange);
|
|
303
|
+
}var index$1=/*#__PURE__*/Object.freeze({__proto__:null,Ipv4Address:Ipv4Address,address:address,stringToNum:stringToNum,numToString:numToString,Ipv4Cidr:Ipv4Cidr,cidr:cidr,MAX:MAX$1});/**
|
|
304
|
+
* The numerical maximum size an IPv6 address can be
|
|
305
|
+
*/
|
|
306
|
+
const MAX = 2n ** 128n;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/
|
|
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,GAAW,CAAC,IAAI,EAAE,GAAG,CAAC,CCCtC,MAAM,cAAc,GAAG,GAAG,CAAA;AAE1B;;;;;;;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;KACrB;;;;;;;;;;;;;IAcM,QAAQ;QACb,OAAO,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;KAClC;;;;;;;;;;;;;;;;;IAkBM,MAAM,CAAC,cAAiC;QAC7C,IAAI,cAAc,YAAY,WAAW,EAAE;YACzC,OAAO,IAAI,CAAC,QAAQ,KAAK,cAA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;;;;;;;;;;;;;;;;;;;;;;;SAuBgB,IAAI,CAAC,SAAiB;IACpC,OAAO,IAAI,QAAQ,CAAC,SAAS,CAAC,CAAA;AAChC,wLCvHA;;;AAGO,MAAM,GAAG,GAAG,EAAE,IAAI,IAAI"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cidr-block",
|
|
3
|
-
"description": "
|
|
4
|
-
"version": "1.
|
|
3
|
+
"description": "ipv4 and ipv6 address and cidr range utilities",
|
|
4
|
+
"version": "1.3.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",
|
|
33
|
+
"test:cov": "jest --coverage",
|
|
34
|
+
"test:verbose": "jest --verbose",
|
|
31
35
|
"test:watch": "jest --watch",
|
|
32
36
|
"test:ui": "majestic"
|
|
33
37
|
},
|
|
@@ -65,11 +69,18 @@
|
|
|
65
69
|
"jest": {
|
|
66
70
|
"preset": "ts-jest",
|
|
67
71
|
"testEnvironment": "node",
|
|
68
|
-
"roots": [
|
|
69
|
-
"<rootDir>/tests"
|
|
70
|
-
],
|
|
71
72
|
"transform": {
|
|
72
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
|
+
}
|
|
73
84
|
}
|
|
74
85
|
},
|
|
75
86
|
"eslintConfig": {
|
|
@@ -81,7 +92,11 @@
|
|
|
81
92
|
"extends": [
|
|
82
93
|
"eslint:recommended",
|
|
83
94
|
"plugin:@typescript-eslint/recommended"
|
|
84
|
-
]
|
|
95
|
+
],
|
|
96
|
+
"rules": {
|
|
97
|
+
"@typescript-eslint/no-non-null-assertion": "off",
|
|
98
|
+
"prefer-const": "off"
|
|
99
|
+
}
|
|
85
100
|
},
|
|
86
101
|
"prettier": {
|
|
87
102
|
"arrowParens": "avoid",
|
package/build/src/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const hello = "world";
|