cidr-block 1.2.0 → 1.3.2
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 +2 -5
- package/build/{cidr/errors.d.ts → errors.d.ts} +0 -0
- package/build/index.d.ts +3 -3
- package/build/index.esm.js +234 -40
- package/build/index.esm.js.map +1 -1
- package/build/index.js +234 -40
- package/build/index.js.map +1 -1
- package/build/{cidr/ipv4 → ipv4}/constants.d.ts +0 -0
- package/build/{cidr/ipv4 → ipv4}/index.d.ts +1 -0
- package/build/{cidr/ipv4 → ipv4}/ipv4-address.d.ts +18 -18
- package/build/{cidr/ipv4 → ipv4}/ipv4-cidr.d.ts +0 -0
- package/build/ipv4/rfc1918.d.ts +8 -0
- package/build/{cidr/ipv4 → ipv4}/types.d.ts +0 -0
- package/build/{cidr/ipv6 → ipv6}/constants.d.ts +0 -0
- package/build/ipv6/index.d.ts +3 -0
- package/build/ipv6/ipv6-address.d.ts +109 -0
- package/build/ipv6/types.d.ts +9 -0
- package/package.json +1 -1
- package/build/cidr/ipv6/index.d.ts +0 -1
package/README.md
CHANGED
|
@@ -2,10 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
ipv4 and ipv6 address and cidr range utilities
|
|
4
4
|
|
|
5
|
-
## WARNING
|
|
6
|
-
|
|
7
|
-
This package is still in _very_ early stages and should **NOT** be used in production code!
|
|
8
|
-
|
|
9
5
|
## Installation
|
|
10
6
|
|
|
11
7
|
To install npm package, run the following in your project:
|
|
@@ -62,8 +58,9 @@ Q: Why are the imports in all the example code like that?
|
|
|
62
58
|
A: The imports in all example code are formatted as the following:
|
|
63
59
|
|
|
64
60
|
```typescript
|
|
61
|
+
// esm
|
|
65
62
|
import { ipv4 as ip } from 'cidr-block'
|
|
66
|
-
//
|
|
63
|
+
// commonjs
|
|
67
64
|
const { ipv4: ip } = require('cidr-block')
|
|
68
65
|
```
|
|
69
66
|
|
|
File without changes
|
package/build/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * as ipv4 from './
|
|
2
|
-
export * as ipv6 from './
|
|
3
|
-
export * as errors from './
|
|
1
|
+
export * as ipv4 from './ipv4/index';
|
|
2
|
+
export * as ipv6 from './ipv6/index';
|
|
3
|
+
export * as errors from './errors';
|
package/build/index.esm.js
CHANGED
|
@@ -16,12 +16,11 @@ const MAX$1 = 2 ** 32 - 1;const MAX_OCTET_SIZE = 255;
|
|
|
16
16
|
* checking.
|
|
17
17
|
*
|
|
18
18
|
* @remarks
|
|
19
|
-
*
|
|
19
|
+
* Avoid direct instantiation; use {@link ipv4.address} instead.
|
|
20
20
|
*/
|
|
21
21
|
class Ipv4Address {
|
|
22
|
-
_address;
|
|
23
22
|
constructor(address) {
|
|
24
|
-
this._address = typeof address === 'number' ? address : stringToNum(address);
|
|
23
|
+
this._address = typeof address === 'number' ? address : stringToNum$1(address);
|
|
25
24
|
}
|
|
26
25
|
/**
|
|
27
26
|
* The address as a number
|
|
@@ -30,22 +29,24 @@ class Ipv4Address {
|
|
|
30
29
|
return this._address;
|
|
31
30
|
}
|
|
32
31
|
/**
|
|
32
|
+
* Returns the string representation of the address
|
|
33
|
+
*
|
|
33
34
|
* @example
|
|
34
35
|
* ```typescript
|
|
35
36
|
* import { ipv4 as ip } from 'cidr-block'
|
|
36
37
|
*
|
|
37
|
-
* ip.address(
|
|
38
|
-
* ip.address(
|
|
38
|
+
* ip.address(255) // ==> '0.0.0.255'
|
|
39
|
+
* ip.address(0b11111111_00000000_11111111_00000000) // ==> '255.0.255.0'
|
|
39
40
|
* ````
|
|
40
41
|
*
|
|
41
42
|
* @public
|
|
42
43
|
* @returns the IPv4 address as a string
|
|
43
44
|
*/
|
|
44
45
|
toString() {
|
|
45
|
-
return numToString(this._address);
|
|
46
|
+
return numToString$1(this._address);
|
|
46
47
|
}
|
|
47
48
|
/**
|
|
48
|
-
* Compares if two
|
|
49
|
+
* Compares if two IPv4 addresses are the same.
|
|
49
50
|
*
|
|
50
51
|
* @example
|
|
51
52
|
* ```typescript
|
|
@@ -65,23 +66,26 @@ class Ipv4Address {
|
|
|
65
66
|
return this._address === otherIpAddress._address;
|
|
66
67
|
}
|
|
67
68
|
else {
|
|
68
|
-
return this._address === address(otherIpAddress)._address;
|
|
69
|
+
return this._address === address$1(otherIpAddress)._address;
|
|
69
70
|
}
|
|
70
71
|
}
|
|
71
72
|
/**
|
|
73
|
+
* Calculates the next logical IPv4 address.
|
|
74
|
+
*
|
|
72
75
|
* @example
|
|
73
76
|
* ```typescript
|
|
74
77
|
* import { ipv4 as ip } from 'cidr-block'
|
|
75
78
|
*
|
|
76
79
|
* const myIp = ip.address('52.89.32.255')
|
|
77
|
-
* myIp.nextIp() // ==> '52.89.33.0
|
|
80
|
+
* myIp.nextIp() // ==> '52.89.33.0'
|
|
78
81
|
* ```
|
|
79
82
|
*
|
|
80
83
|
* @public
|
|
81
84
|
* @returns the next consecutive IPv4 address
|
|
82
85
|
*/
|
|
83
86
|
nextIp() {
|
|
84
|
-
|
|
87
|
+
// TODO: Handle last ip address
|
|
88
|
+
return address$1(this._address + 1);
|
|
85
89
|
}
|
|
86
90
|
/**
|
|
87
91
|
* @example
|
|
@@ -89,25 +93,22 @@ class Ipv4Address {
|
|
|
89
93
|
* import { ipv4 as ip } from 'cidr-block'
|
|
90
94
|
*
|
|
91
95
|
* const myIp = ip.address('52.89.32.19')
|
|
92
|
-
* myIp.previousIp() // ==> '52.89.32.18
|
|
96
|
+
* myIp.previousIp() // ==> '52.89.32.18'
|
|
93
97
|
* ```
|
|
94
98
|
*
|
|
95
99
|
* @public
|
|
96
100
|
* @returns the preceding IPv4 address
|
|
97
101
|
*/
|
|
98
102
|
previousIp() {
|
|
99
|
-
return address(this._address - 1);
|
|
103
|
+
return address$1(this._address - 1);
|
|
100
104
|
}
|
|
101
105
|
}
|
|
102
106
|
/**
|
|
103
107
|
* Convenience function for creating an IPv4 address instance.
|
|
104
108
|
*
|
|
105
109
|
* @remarks
|
|
106
|
-
*
|
|
107
110
|
* In general, you should use this function instead of instantiating an Ipv4Address
|
|
108
|
-
* object directly.
|
|
109
|
-
* methods like these are meant to help reduce the footprint of your code and increase
|
|
110
|
-
* readability.
|
|
111
|
+
* object directly.
|
|
111
112
|
*
|
|
112
113
|
* @example
|
|
113
114
|
*
|
|
@@ -119,22 +120,23 @@ class Ipv4Address {
|
|
|
119
120
|
*
|
|
120
121
|
* @see {@link Ipv4Address}
|
|
121
122
|
*
|
|
123
|
+
* @public
|
|
122
124
|
* @param ip string representation of the IPv4 address
|
|
123
125
|
* @returns an instance of Ipv4Address
|
|
124
126
|
*/
|
|
125
|
-
function address(ip) {
|
|
127
|
+
function address$1(ip) {
|
|
128
|
+
// TODO: Implement memoization
|
|
126
129
|
return new Ipv4Address(ip);
|
|
127
130
|
}
|
|
128
131
|
/**
|
|
129
132
|
* Converts the string representation of an IPv4 address to a number.
|
|
130
133
|
*
|
|
131
134
|
* @example
|
|
132
|
-
*
|
|
133
135
|
* ```typescript
|
|
134
136
|
* import * as cidr from 'cidr-block'
|
|
135
137
|
*
|
|
136
|
-
* cidr.ipv4.stringToNum('255.255.255.255') ===
|
|
137
|
-
* cidr.ipv4.stringToNum('0.0.0.255') ===
|
|
138
|
+
* cidr.ipv4.stringToNum('255.255.255.255') === 4_294_967_295 // ==> true
|
|
139
|
+
* cidr.ipv4.stringToNum('0.0.0.255') === 255 // ==> true
|
|
138
140
|
* ```
|
|
139
141
|
*
|
|
140
142
|
* @see This method is the inverse of {@link ipv4.numToString}
|
|
@@ -144,7 +146,7 @@ function address(ip) {
|
|
|
144
146
|
* @param address IPv4 address represented as a string
|
|
145
147
|
* @returns numerical number representation of the address
|
|
146
148
|
*/
|
|
147
|
-
function stringToNum(address) {
|
|
149
|
+
function stringToNum$1(address) {
|
|
148
150
|
try {
|
|
149
151
|
if (address.length < 7) {
|
|
150
152
|
throw new Error();
|
|
@@ -164,16 +166,15 @@ function stringToNum(address) {
|
|
|
164
166
|
}
|
|
165
167
|
}
|
|
166
168
|
/**
|
|
167
|
-
* Converts the numerical
|
|
169
|
+
* Converts the numerical representation of an IPv4 address to its string representation.
|
|
168
170
|
*
|
|
169
171
|
* @example
|
|
170
|
-
*
|
|
171
172
|
* ```typescript
|
|
172
173
|
* import * as cidr from 'cidr-block'
|
|
173
174
|
*
|
|
174
|
-
* cidr.ipv4.numToString(
|
|
175
|
-
* cidr.ipv4.numToString(
|
|
176
|
-
* cidr.ipv4.numToString(
|
|
175
|
+
* cidr.ipv4.numToString(0) === '0.0.0.0' // ==> true
|
|
176
|
+
* cidr.ipv4.numToString(65_280) === '0.0.255.0' // ==> true
|
|
177
|
+
* cidr.ipv4.numToString(4_294_967_295) === '255.255.255.255' // ==> true
|
|
177
178
|
* ```
|
|
178
179
|
*
|
|
179
180
|
* @see This method is the inverse of {@link ipv4.stringToNum}
|
|
@@ -183,7 +184,7 @@ function stringToNum(address) {
|
|
|
183
184
|
* @param ip IPv4 address as a number
|
|
184
185
|
* @returns string representation of the address
|
|
185
186
|
*/
|
|
186
|
-
function numToString(ip) {
|
|
187
|
+
function numToString$1(ip) {
|
|
187
188
|
try {
|
|
188
189
|
if (ip < 0 || ip > MAX$1) {
|
|
189
190
|
throw new Error();
|
|
@@ -198,13 +199,11 @@ function numToString(ip) {
|
|
|
198
199
|
throw new InvalidIpAddressError(ip.toString());
|
|
199
200
|
}
|
|
200
201
|
}class Ipv4Cidr {
|
|
201
|
-
_ipAddress;
|
|
202
|
-
_maskSize;
|
|
203
202
|
// TODO: Allow wider-range of values that can be used to create a cidr
|
|
204
203
|
constructor(cidrRange) {
|
|
205
204
|
try {
|
|
206
|
-
const [address
|
|
207
|
-
this._ipAddress = address(address
|
|
205
|
+
const [address, subnetMask] = cidrRange.split('/');
|
|
206
|
+
this._ipAddress = address$1(address);
|
|
208
207
|
this._maskSize = Number(subnetMask);
|
|
209
208
|
}
|
|
210
209
|
catch {
|
|
@@ -227,20 +226,20 @@ function numToString(ip) {
|
|
|
227
226
|
* The actual IPv4 netmask address
|
|
228
227
|
*/
|
|
229
228
|
get netmask() {
|
|
230
|
-
return address((2 ** this.maskSize - 1) << this.addressLength);
|
|
229
|
+
return address$1((2 ** this.maskSize - 1) << this.addressLength);
|
|
231
230
|
}
|
|
232
231
|
/**
|
|
233
232
|
* The first IPv4 address that is usable within the given cidr range
|
|
234
233
|
*/
|
|
235
234
|
get firstUsableIp() {
|
|
236
|
-
return address(this._ipAddress.address);
|
|
235
|
+
return address$1(this._ipAddress.address);
|
|
237
236
|
}
|
|
238
237
|
/**
|
|
239
238
|
* The last IPv4 address that is usable within the given cidr range
|
|
240
239
|
*/
|
|
241
240
|
get lastUsableIp() {
|
|
242
241
|
// FIXME: Handle edge case of when cidr range goes outside valid ip range
|
|
243
|
-
return address(this._ipAddress.address + 2 ** this.addressLength - 1);
|
|
242
|
+
return address$1(this._ipAddress.address + 2 ** this.addressLength - 1);
|
|
244
243
|
}
|
|
245
244
|
get addressLength() {
|
|
246
245
|
return Math.abs(32 - this._maskSize);
|
|
@@ -256,20 +255,20 @@ function numToString(ip) {
|
|
|
256
255
|
*/
|
|
257
256
|
nextBlock(ofSize) {
|
|
258
257
|
const nextIp = this._ipAddress.address + 2 ** this.addressLength;
|
|
259
|
-
return cidr(`${numToString(nextIp)}/${ofSize ?? this._maskSize}`);
|
|
258
|
+
return cidr(`${numToString$1(nextIp)}/${ofSize ?? this._maskSize}`);
|
|
260
259
|
}
|
|
261
260
|
/**
|
|
262
261
|
* @returns the previous cidr block
|
|
263
262
|
*/
|
|
264
263
|
previousBlock() {
|
|
265
264
|
const nextIp = this._ipAddress.address - 2 ** this.addressLength;
|
|
266
|
-
return cidr(`${numToString(nextIp)}/${this._maskSize}`);
|
|
265
|
+
return cidr(`${numToString$1(nextIp)}/${this._maskSize}`);
|
|
267
266
|
}
|
|
268
267
|
/**
|
|
269
268
|
* @returns if the given IPv4 address is within the cidr range
|
|
270
269
|
*/
|
|
271
|
-
includes(address
|
|
272
|
-
const ip = address
|
|
270
|
+
includes(address) {
|
|
271
|
+
const ip = address instanceof Ipv4Address ? address : address$1(address);
|
|
273
272
|
return (
|
|
274
273
|
// FIXME: How to handle edge case of next block erroring out?
|
|
275
274
|
ip.address >= this._ipAddress.address && ip.address <= this.nextBlock()._ipAddress.address);
|
|
@@ -300,7 +299,202 @@ function numToString(ip) {
|
|
|
300
299
|
*/
|
|
301
300
|
function cidr(cidrRange) {
|
|
302
301
|
return new Ipv4Cidr(cidrRange);
|
|
303
|
-
}
|
|
302
|
+
}const RFC_1918_CIDRS = [cidr('10.0.0.0/8'), cidr('172.16.0.0/12'), cidr('192.168.0.0/16')];
|
|
303
|
+
/**
|
|
304
|
+
* Predicate function that will return true if the given
|
|
305
|
+
* address is in the private RFC 1918 ipv4 address space.
|
|
306
|
+
*
|
|
307
|
+
* See more {@link https://datatracker.ietf.org/doc/html/rfc1918}
|
|
308
|
+
*/
|
|
309
|
+
function isPrivateRFC1918(address) {
|
|
310
|
+
for (const rfcCidr of RFC_1918_CIDRS) {
|
|
311
|
+
if (rfcCidr.includes(address)) {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return false;
|
|
316
|
+
}var rfc1918=/*#__PURE__*/Object.freeze({__proto__:null,isPrivateRFC1918:isPrivateRFC1918});var index$1=/*#__PURE__*/Object.freeze({__proto__:null,rfc1918:rfc1918,Ipv4Address:Ipv4Address,address:address$1,stringToNum:stringToNum$1,numToString:numToString$1,Ipv4Cidr:Ipv4Cidr,cidr:cidr,MAX:MAX$1});const MAX_HEXTET_SIZE = 65535n;
|
|
317
|
+
/**
|
|
318
|
+
* Representation of an IPv6 address. Provides various utilities methods like equality
|
|
319
|
+
* checking.
|
|
320
|
+
*
|
|
321
|
+
* @remarks
|
|
322
|
+
* Avoid direct instantiation; use {@link ipv6.address} instead.
|
|
323
|
+
*/
|
|
324
|
+
class Ipv6Address {
|
|
325
|
+
constructor(address) {
|
|
326
|
+
this._address = typeof address === 'bigint' ? address : stringToNum(address);
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* The address as a bigint
|
|
330
|
+
*
|
|
331
|
+
* @remarks
|
|
332
|
+
* Because the representation of an IPv6 address is too large to fit into a typical
|
|
333
|
+
* JavaScript integer, a
|
|
334
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt}
|
|
335
|
+
* is used instead.
|
|
336
|
+
*/
|
|
337
|
+
get address() {
|
|
338
|
+
return this._address;
|
|
339
|
+
}
|
|
340
|
+
// TODO: Add example code
|
|
341
|
+
/**
|
|
342
|
+
* Returns the string representation of the address
|
|
343
|
+
*/
|
|
344
|
+
toString() {
|
|
345
|
+
return numToString(this._address);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Compares if two IPv6 addresses are the same.
|
|
349
|
+
*
|
|
350
|
+
* @example
|
|
351
|
+
* ```typescript
|
|
352
|
+
* import { ipv6 as ip } from 'cidr-block'
|
|
353
|
+
*
|
|
354
|
+
* function isLoopback(address: Ipv6Representable) {
|
|
355
|
+
* return ip.address(address).equals('::1')
|
|
356
|
+
* }
|
|
357
|
+
*
|
|
358
|
+
* @public
|
|
359
|
+
* @param otherIpAddress the other Ipv6 address to compare
|
|
360
|
+
* @returns if the other IP address is the same
|
|
361
|
+
* ```
|
|
362
|
+
*/
|
|
363
|
+
equals(otherIpAddress) {
|
|
364
|
+
if (otherIpAddress instanceof Ipv6Address) {
|
|
365
|
+
return this._address === otherIpAddress._address;
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
return this._address === address(otherIpAddress)._address;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Calculates the next logical IPv6 address.
|
|
373
|
+
*
|
|
374
|
+
* @example
|
|
375
|
+
* ```typescript
|
|
376
|
+
* import { ipv6 as ip } from 'cidr-block'
|
|
377
|
+
*
|
|
378
|
+
* const myIp = ip.address('2001:0db8::ac10')
|
|
379
|
+
* myIp.nextIp() // ==> '2001:0db8::ac11'
|
|
380
|
+
* ```
|
|
381
|
+
*/
|
|
382
|
+
nextIp() {
|
|
383
|
+
// TODO: Handle last ip address
|
|
384
|
+
return address(this._address + 1n);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Calculates the previous logical IPv6 address.
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* ```typescript
|
|
391
|
+
* import { ipv6 as ip } from 'cidr-block'
|
|
392
|
+
*
|
|
393
|
+
* const myIp = ip.address('2001:0db8::ac10')
|
|
394
|
+
* myIp.previousIp() // ==> '2001:0db8::ac09'
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
previousIp() {
|
|
398
|
+
return address(this._address - 1n);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Convenience function for creating an IPv6 address instance.
|
|
403
|
+
*
|
|
404
|
+
* @remarks
|
|
405
|
+
* In general, you should use this function instead of instantiating an Ipv6Address
|
|
406
|
+
* object directly.
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
*
|
|
410
|
+
* ```typescript
|
|
411
|
+
* import { ipv6 as ip } from 'cidr-block'
|
|
412
|
+
*
|
|
413
|
+
* const localhost = ip.address('::1')
|
|
414
|
+
* ```
|
|
415
|
+
*
|
|
416
|
+
* @see {@link Ipv6Address}
|
|
417
|
+
*
|
|
418
|
+
* @param ip string representation of the IPv6 address
|
|
419
|
+
* @returns an instance of Ipv6Address
|
|
420
|
+
*/
|
|
421
|
+
function address(ip) {
|
|
422
|
+
// TODO: Implement memoization
|
|
423
|
+
return new Ipv6Address(ip);
|
|
424
|
+
}
|
|
425
|
+
// TODO: Add code example
|
|
426
|
+
/**
|
|
427
|
+
* Converts the string representation of an IPv6 address to a bigint.
|
|
428
|
+
*
|
|
429
|
+
* @see {@link Ipv6Address}
|
|
430
|
+
*
|
|
431
|
+
* @public
|
|
432
|
+
* @param ip string representation of the IPv6 address
|
|
433
|
+
* @returns an instance of Ipv6Address
|
|
434
|
+
*/
|
|
435
|
+
function stringToNum(address) {
|
|
436
|
+
if (address === '::') {
|
|
437
|
+
return 0n;
|
|
438
|
+
}
|
|
439
|
+
let ipv6 = 0n;
|
|
440
|
+
const rawHextets = [];
|
|
441
|
+
const [leftAddress, rightAddress] = address.split('::');
|
|
442
|
+
for (const hextet of leftAddress.split(':')) {
|
|
443
|
+
rawHextets.push(hextet || '0');
|
|
444
|
+
}
|
|
445
|
+
if (rightAddress !== undefined) {
|
|
446
|
+
const rightHextets = rightAddress.split(':');
|
|
447
|
+
const emptyFillCount = 8 - (rawHextets.length + rightHextets.length);
|
|
448
|
+
for (let i = 0; i < emptyFillCount; i++) {
|
|
449
|
+
rawHextets.push('0');
|
|
450
|
+
}
|
|
451
|
+
for (const hextet of rightHextets) {
|
|
452
|
+
rawHextets.push(hextet || '0');
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
const decimals = rawHextets.map(hextet => parseInt(hextet, 16));
|
|
456
|
+
let shiftSize = 0n;
|
|
457
|
+
let binHex = 0n;
|
|
458
|
+
for (const pos in decimals) {
|
|
459
|
+
const num = decimals[pos];
|
|
460
|
+
if (num === 0) {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
shiftSize = BigInt(Math.abs(parseInt(pos) - 7) * 16);
|
|
464
|
+
binHex = BigInt(num) << shiftSize;
|
|
465
|
+
ipv6 |= binHex;
|
|
466
|
+
}
|
|
467
|
+
return ipv6;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Converts the numerical representation of an IPv6 address to its string representation.
|
|
471
|
+
*
|
|
472
|
+
* @see This method is the inverse of {@link ipv6.stringToNum}
|
|
473
|
+
* @throws {@link InvalidIpAddressError}
|
|
474
|
+
*
|
|
475
|
+
* @public
|
|
476
|
+
* @param ip IPv6 address as a number
|
|
477
|
+
* @returns string representation of the address
|
|
478
|
+
*/
|
|
479
|
+
function numToString(num) {
|
|
480
|
+
if (num === 0n) {
|
|
481
|
+
return '::';
|
|
482
|
+
}
|
|
483
|
+
const hextets = [];
|
|
484
|
+
for (let n = 0; n < 8; n++) {
|
|
485
|
+
const bitOffset = BigInt(Math.abs(n - 7) * 16);
|
|
486
|
+
hextets.push(((num >> bitOffset) & MAX_HEXTET_SIZE).toString(16));
|
|
487
|
+
}
|
|
488
|
+
const dropStartIdx = hextets.indexOf('0');
|
|
489
|
+
if (dropStartIdx >= 0) {
|
|
490
|
+
let dropCount = 1;
|
|
491
|
+
for (let i = dropStartIdx + 1; hextets[i] === '0'; i++) {
|
|
492
|
+
dropCount++;
|
|
493
|
+
}
|
|
494
|
+
hextets.splice(dropStartIdx, dropCount, dropStartIdx === 0 || dropStartIdx + dropCount === 8 ? ':' : '');
|
|
495
|
+
}
|
|
496
|
+
return hextets.join(':');
|
|
497
|
+
}/**
|
|
304
498
|
* The numerical maximum size an IPv6 address can be
|
|
305
499
|
*/
|
|
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
|
|
500
|
+
const MAX = 2n ** 128n;var index=/*#__PURE__*/Object.freeze({__proto__:null,Ipv6Address:Ipv6Address,address:address,stringToNum:stringToNum,numToString:numToString,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/src/
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/src/errors.ts","../src/src/ipv4/constants.ts","../src/src/ipv4/ipv4-address.ts","../src/src/ipv4/ipv4-cidr.ts","../src/src/ipv4/rfc1918.ts","../src/src/ipv6/ipv6-address.ts","../src/src/ipv6/constants.ts"],"sourcesContent":[null,null,null,null,null,null,null],"names":["MAX","stringToNum","numToString","address","ipAddress"],"mappings":"MAAa,qBAAsB,SAAQ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E,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE;QAC1B,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,CAAA;QAC9C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,SAAS,IAAI,eAAe,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAA;KAClE;IACD,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IACzC,IAAI,YAAY,IAAI,CAAC,EAAE;QACrB,IAAI,SAAS,GAAG,CAAC,CAAA;QACjB,KAAK,IAAI,CAAC,GAAG,YAAY,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC,EAAE,EAAE;YACtD,SAAS,EAAE,CAAA;SACZ;QACD,OAAO,CAAC,MAAM,CACZ,YAAY,EACZ,SAAS,EACT,YAAY,KAAK,CAAC,IAAI,YAAY,GAAG,SAAS,KAAK,CAAC,GAAG,GAAG,GAAG,EAAE,CAChE,CAAA;KACF;IACD,OAAO,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AAC1B,CC5MA;;;AAGO,MAAM,GAAG,GAAG,EAAE,IAAI,IAAI"}
|
package/build/index.js
CHANGED
|
@@ -16,12 +16,11 @@ const MAX$1 = 2 ** 32 - 1;const MAX_OCTET_SIZE = 255;
|
|
|
16
16
|
* checking.
|
|
17
17
|
*
|
|
18
18
|
* @remarks
|
|
19
|
-
*
|
|
19
|
+
* Avoid direct instantiation; use {@link ipv4.address} instead.
|
|
20
20
|
*/
|
|
21
21
|
class Ipv4Address {
|
|
22
|
-
_address;
|
|
23
22
|
constructor(address) {
|
|
24
|
-
this._address = typeof address === 'number' ? address : stringToNum(address);
|
|
23
|
+
this._address = typeof address === 'number' ? address : stringToNum$1(address);
|
|
25
24
|
}
|
|
26
25
|
/**
|
|
27
26
|
* The address as a number
|
|
@@ -30,22 +29,24 @@ class Ipv4Address {
|
|
|
30
29
|
return this._address;
|
|
31
30
|
}
|
|
32
31
|
/**
|
|
32
|
+
* Returns the string representation of the address
|
|
33
|
+
*
|
|
33
34
|
* @example
|
|
34
35
|
* ```typescript
|
|
35
36
|
* import { ipv4 as ip } from 'cidr-block'
|
|
36
37
|
*
|
|
37
|
-
* ip.address(
|
|
38
|
-
* ip.address(
|
|
38
|
+
* ip.address(255) // ==> '0.0.0.255'
|
|
39
|
+
* ip.address(0b11111111_00000000_11111111_00000000) // ==> '255.0.255.0'
|
|
39
40
|
* ````
|
|
40
41
|
*
|
|
41
42
|
* @public
|
|
42
43
|
* @returns the IPv4 address as a string
|
|
43
44
|
*/
|
|
44
45
|
toString() {
|
|
45
|
-
return numToString(this._address);
|
|
46
|
+
return numToString$1(this._address);
|
|
46
47
|
}
|
|
47
48
|
/**
|
|
48
|
-
* Compares if two
|
|
49
|
+
* Compares if two IPv4 addresses are the same.
|
|
49
50
|
*
|
|
50
51
|
* @example
|
|
51
52
|
* ```typescript
|
|
@@ -65,23 +66,26 @@ class Ipv4Address {
|
|
|
65
66
|
return this._address === otherIpAddress._address;
|
|
66
67
|
}
|
|
67
68
|
else {
|
|
68
|
-
return this._address === address(otherIpAddress)._address;
|
|
69
|
+
return this._address === address$1(otherIpAddress)._address;
|
|
69
70
|
}
|
|
70
71
|
}
|
|
71
72
|
/**
|
|
73
|
+
* Calculates the next logical IPv4 address.
|
|
74
|
+
*
|
|
72
75
|
* @example
|
|
73
76
|
* ```typescript
|
|
74
77
|
* import { ipv4 as ip } from 'cidr-block'
|
|
75
78
|
*
|
|
76
79
|
* const myIp = ip.address('52.89.32.255')
|
|
77
|
-
* myIp.nextIp() // ==> '52.89.33.0
|
|
80
|
+
* myIp.nextIp() // ==> '52.89.33.0'
|
|
78
81
|
* ```
|
|
79
82
|
*
|
|
80
83
|
* @public
|
|
81
84
|
* @returns the next consecutive IPv4 address
|
|
82
85
|
*/
|
|
83
86
|
nextIp() {
|
|
84
|
-
|
|
87
|
+
// TODO: Handle last ip address
|
|
88
|
+
return address$1(this._address + 1);
|
|
85
89
|
}
|
|
86
90
|
/**
|
|
87
91
|
* @example
|
|
@@ -89,25 +93,22 @@ class Ipv4Address {
|
|
|
89
93
|
* import { ipv4 as ip } from 'cidr-block'
|
|
90
94
|
*
|
|
91
95
|
* const myIp = ip.address('52.89.32.19')
|
|
92
|
-
* myIp.previousIp() // ==> '52.89.32.18
|
|
96
|
+
* myIp.previousIp() // ==> '52.89.32.18'
|
|
93
97
|
* ```
|
|
94
98
|
*
|
|
95
99
|
* @public
|
|
96
100
|
* @returns the preceding IPv4 address
|
|
97
101
|
*/
|
|
98
102
|
previousIp() {
|
|
99
|
-
return address(this._address - 1);
|
|
103
|
+
return address$1(this._address - 1);
|
|
100
104
|
}
|
|
101
105
|
}
|
|
102
106
|
/**
|
|
103
107
|
* Convenience function for creating an IPv4 address instance.
|
|
104
108
|
*
|
|
105
109
|
* @remarks
|
|
106
|
-
*
|
|
107
110
|
* In general, you should use this function instead of instantiating an Ipv4Address
|
|
108
|
-
* object directly.
|
|
109
|
-
* methods like these are meant to help reduce the footprint of your code and increase
|
|
110
|
-
* readability.
|
|
111
|
+
* object directly.
|
|
111
112
|
*
|
|
112
113
|
* @example
|
|
113
114
|
*
|
|
@@ -119,22 +120,23 @@ class Ipv4Address {
|
|
|
119
120
|
*
|
|
120
121
|
* @see {@link Ipv4Address}
|
|
121
122
|
*
|
|
123
|
+
* @public
|
|
122
124
|
* @param ip string representation of the IPv4 address
|
|
123
125
|
* @returns an instance of Ipv4Address
|
|
124
126
|
*/
|
|
125
|
-
function address(ip) {
|
|
127
|
+
function address$1(ip) {
|
|
128
|
+
// TODO: Implement memoization
|
|
126
129
|
return new Ipv4Address(ip);
|
|
127
130
|
}
|
|
128
131
|
/**
|
|
129
132
|
* Converts the string representation of an IPv4 address to a number.
|
|
130
133
|
*
|
|
131
134
|
* @example
|
|
132
|
-
*
|
|
133
135
|
* ```typescript
|
|
134
136
|
* import * as cidr from 'cidr-block'
|
|
135
137
|
*
|
|
136
|
-
* cidr.ipv4.stringToNum('255.255.255.255') ===
|
|
137
|
-
* cidr.ipv4.stringToNum('0.0.0.255') ===
|
|
138
|
+
* cidr.ipv4.stringToNum('255.255.255.255') === 4_294_967_295 // ==> true
|
|
139
|
+
* cidr.ipv4.stringToNum('0.0.0.255') === 255 // ==> true
|
|
138
140
|
* ```
|
|
139
141
|
*
|
|
140
142
|
* @see This method is the inverse of {@link ipv4.numToString}
|
|
@@ -144,7 +146,7 @@ function address(ip) {
|
|
|
144
146
|
* @param address IPv4 address represented as a string
|
|
145
147
|
* @returns numerical number representation of the address
|
|
146
148
|
*/
|
|
147
|
-
function stringToNum(address) {
|
|
149
|
+
function stringToNum$1(address) {
|
|
148
150
|
try {
|
|
149
151
|
if (address.length < 7) {
|
|
150
152
|
throw new Error();
|
|
@@ -164,16 +166,15 @@ function stringToNum(address) {
|
|
|
164
166
|
}
|
|
165
167
|
}
|
|
166
168
|
/**
|
|
167
|
-
* Converts the numerical
|
|
169
|
+
* Converts the numerical representation of an IPv4 address to its string representation.
|
|
168
170
|
*
|
|
169
171
|
* @example
|
|
170
|
-
*
|
|
171
172
|
* ```typescript
|
|
172
173
|
* import * as cidr from 'cidr-block'
|
|
173
174
|
*
|
|
174
|
-
* cidr.ipv4.numToString(
|
|
175
|
-
* cidr.ipv4.numToString(
|
|
176
|
-
* cidr.ipv4.numToString(
|
|
175
|
+
* cidr.ipv4.numToString(0) === '0.0.0.0' // ==> true
|
|
176
|
+
* cidr.ipv4.numToString(65_280) === '0.0.255.0' // ==> true
|
|
177
|
+
* cidr.ipv4.numToString(4_294_967_295) === '255.255.255.255' // ==> true
|
|
177
178
|
* ```
|
|
178
179
|
*
|
|
179
180
|
* @see This method is the inverse of {@link ipv4.stringToNum}
|
|
@@ -183,7 +184,7 @@ function stringToNum(address) {
|
|
|
183
184
|
* @param ip IPv4 address as a number
|
|
184
185
|
* @returns string representation of the address
|
|
185
186
|
*/
|
|
186
|
-
function numToString(ip) {
|
|
187
|
+
function numToString$1(ip) {
|
|
187
188
|
try {
|
|
188
189
|
if (ip < 0 || ip > MAX$1) {
|
|
189
190
|
throw new Error();
|
|
@@ -198,13 +199,11 @@ function numToString(ip) {
|
|
|
198
199
|
throw new InvalidIpAddressError(ip.toString());
|
|
199
200
|
}
|
|
200
201
|
}class Ipv4Cidr {
|
|
201
|
-
_ipAddress;
|
|
202
|
-
_maskSize;
|
|
203
202
|
// TODO: Allow wider-range of values that can be used to create a cidr
|
|
204
203
|
constructor(cidrRange) {
|
|
205
204
|
try {
|
|
206
|
-
const [address
|
|
207
|
-
this._ipAddress = address(address
|
|
205
|
+
const [address, subnetMask] = cidrRange.split('/');
|
|
206
|
+
this._ipAddress = address$1(address);
|
|
208
207
|
this._maskSize = Number(subnetMask);
|
|
209
208
|
}
|
|
210
209
|
catch {
|
|
@@ -227,20 +226,20 @@ function numToString(ip) {
|
|
|
227
226
|
* The actual IPv4 netmask address
|
|
228
227
|
*/
|
|
229
228
|
get netmask() {
|
|
230
|
-
return address((2 ** this.maskSize - 1) << this.addressLength);
|
|
229
|
+
return address$1((2 ** this.maskSize - 1) << this.addressLength);
|
|
231
230
|
}
|
|
232
231
|
/**
|
|
233
232
|
* The first IPv4 address that is usable within the given cidr range
|
|
234
233
|
*/
|
|
235
234
|
get firstUsableIp() {
|
|
236
|
-
return address(this._ipAddress.address);
|
|
235
|
+
return address$1(this._ipAddress.address);
|
|
237
236
|
}
|
|
238
237
|
/**
|
|
239
238
|
* The last IPv4 address that is usable within the given cidr range
|
|
240
239
|
*/
|
|
241
240
|
get lastUsableIp() {
|
|
242
241
|
// FIXME: Handle edge case of when cidr range goes outside valid ip range
|
|
243
|
-
return address(this._ipAddress.address + 2 ** this.addressLength - 1);
|
|
242
|
+
return address$1(this._ipAddress.address + 2 ** this.addressLength - 1);
|
|
244
243
|
}
|
|
245
244
|
get addressLength() {
|
|
246
245
|
return Math.abs(32 - this._maskSize);
|
|
@@ -256,20 +255,20 @@ function numToString(ip) {
|
|
|
256
255
|
*/
|
|
257
256
|
nextBlock(ofSize) {
|
|
258
257
|
const nextIp = this._ipAddress.address + 2 ** this.addressLength;
|
|
259
|
-
return cidr(`${numToString(nextIp)}/${ofSize ?? this._maskSize}`);
|
|
258
|
+
return cidr(`${numToString$1(nextIp)}/${ofSize ?? this._maskSize}`);
|
|
260
259
|
}
|
|
261
260
|
/**
|
|
262
261
|
* @returns the previous cidr block
|
|
263
262
|
*/
|
|
264
263
|
previousBlock() {
|
|
265
264
|
const nextIp = this._ipAddress.address - 2 ** this.addressLength;
|
|
266
|
-
return cidr(`${numToString(nextIp)}/${this._maskSize}`);
|
|
265
|
+
return cidr(`${numToString$1(nextIp)}/${this._maskSize}`);
|
|
267
266
|
}
|
|
268
267
|
/**
|
|
269
268
|
* @returns if the given IPv4 address is within the cidr range
|
|
270
269
|
*/
|
|
271
|
-
includes(address
|
|
272
|
-
const ip = address
|
|
270
|
+
includes(address) {
|
|
271
|
+
const ip = address instanceof Ipv4Address ? address : address$1(address);
|
|
273
272
|
return (
|
|
274
273
|
// FIXME: How to handle edge case of next block erroring out?
|
|
275
274
|
ip.address >= this._ipAddress.address && ip.address <= this.nextBlock()._ipAddress.address);
|
|
@@ -300,7 +299,202 @@ function numToString(ip) {
|
|
|
300
299
|
*/
|
|
301
300
|
function cidr(cidrRange) {
|
|
302
301
|
return new Ipv4Cidr(cidrRange);
|
|
303
|
-
}
|
|
302
|
+
}const RFC_1918_CIDRS = [cidr('10.0.0.0/8'), cidr('172.16.0.0/12'), cidr('192.168.0.0/16')];
|
|
303
|
+
/**
|
|
304
|
+
* Predicate function that will return true if the given
|
|
305
|
+
* address is in the private RFC 1918 ipv4 address space.
|
|
306
|
+
*
|
|
307
|
+
* See more {@link https://datatracker.ietf.org/doc/html/rfc1918}
|
|
308
|
+
*/
|
|
309
|
+
function isPrivateRFC1918(address) {
|
|
310
|
+
for (const rfcCidr of RFC_1918_CIDRS) {
|
|
311
|
+
if (rfcCidr.includes(address)) {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return false;
|
|
316
|
+
}var rfc1918=/*#__PURE__*/Object.freeze({__proto__:null,isPrivateRFC1918:isPrivateRFC1918});var index$1=/*#__PURE__*/Object.freeze({__proto__:null,rfc1918:rfc1918,Ipv4Address:Ipv4Address,address:address$1,stringToNum:stringToNum$1,numToString:numToString$1,Ipv4Cidr:Ipv4Cidr,cidr:cidr,MAX:MAX$1});const MAX_HEXTET_SIZE = 65535n;
|
|
317
|
+
/**
|
|
318
|
+
* Representation of an IPv6 address. Provides various utilities methods like equality
|
|
319
|
+
* checking.
|
|
320
|
+
*
|
|
321
|
+
* @remarks
|
|
322
|
+
* Avoid direct instantiation; use {@link ipv6.address} instead.
|
|
323
|
+
*/
|
|
324
|
+
class Ipv6Address {
|
|
325
|
+
constructor(address) {
|
|
326
|
+
this._address = typeof address === 'bigint' ? address : stringToNum(address);
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* The address as a bigint
|
|
330
|
+
*
|
|
331
|
+
* @remarks
|
|
332
|
+
* Because the representation of an IPv6 address is too large to fit into a typical
|
|
333
|
+
* JavaScript integer, a
|
|
334
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt}
|
|
335
|
+
* is used instead.
|
|
336
|
+
*/
|
|
337
|
+
get address() {
|
|
338
|
+
return this._address;
|
|
339
|
+
}
|
|
340
|
+
// TODO: Add example code
|
|
341
|
+
/**
|
|
342
|
+
* Returns the string representation of the address
|
|
343
|
+
*/
|
|
344
|
+
toString() {
|
|
345
|
+
return numToString(this._address);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Compares if two IPv6 addresses are the same.
|
|
349
|
+
*
|
|
350
|
+
* @example
|
|
351
|
+
* ```typescript
|
|
352
|
+
* import { ipv6 as ip } from 'cidr-block'
|
|
353
|
+
*
|
|
354
|
+
* function isLoopback(address: Ipv6Representable) {
|
|
355
|
+
* return ip.address(address).equals('::1')
|
|
356
|
+
* }
|
|
357
|
+
*
|
|
358
|
+
* @public
|
|
359
|
+
* @param otherIpAddress the other Ipv6 address to compare
|
|
360
|
+
* @returns if the other IP address is the same
|
|
361
|
+
* ```
|
|
362
|
+
*/
|
|
363
|
+
equals(otherIpAddress) {
|
|
364
|
+
if (otherIpAddress instanceof Ipv6Address) {
|
|
365
|
+
return this._address === otherIpAddress._address;
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
return this._address === address(otherIpAddress)._address;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Calculates the next logical IPv6 address.
|
|
373
|
+
*
|
|
374
|
+
* @example
|
|
375
|
+
* ```typescript
|
|
376
|
+
* import { ipv6 as ip } from 'cidr-block'
|
|
377
|
+
*
|
|
378
|
+
* const myIp = ip.address('2001:0db8::ac10')
|
|
379
|
+
* myIp.nextIp() // ==> '2001:0db8::ac11'
|
|
380
|
+
* ```
|
|
381
|
+
*/
|
|
382
|
+
nextIp() {
|
|
383
|
+
// TODO: Handle last ip address
|
|
384
|
+
return address(this._address + 1n);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Calculates the previous logical IPv6 address.
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* ```typescript
|
|
391
|
+
* import { ipv6 as ip } from 'cidr-block'
|
|
392
|
+
*
|
|
393
|
+
* const myIp = ip.address('2001:0db8::ac10')
|
|
394
|
+
* myIp.previousIp() // ==> '2001:0db8::ac09'
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
previousIp() {
|
|
398
|
+
return address(this._address - 1n);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Convenience function for creating an IPv6 address instance.
|
|
403
|
+
*
|
|
404
|
+
* @remarks
|
|
405
|
+
* In general, you should use this function instead of instantiating an Ipv6Address
|
|
406
|
+
* object directly.
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
*
|
|
410
|
+
* ```typescript
|
|
411
|
+
* import { ipv6 as ip } from 'cidr-block'
|
|
412
|
+
*
|
|
413
|
+
* const localhost = ip.address('::1')
|
|
414
|
+
* ```
|
|
415
|
+
*
|
|
416
|
+
* @see {@link Ipv6Address}
|
|
417
|
+
*
|
|
418
|
+
* @param ip string representation of the IPv6 address
|
|
419
|
+
* @returns an instance of Ipv6Address
|
|
420
|
+
*/
|
|
421
|
+
function address(ip) {
|
|
422
|
+
// TODO: Implement memoization
|
|
423
|
+
return new Ipv6Address(ip);
|
|
424
|
+
}
|
|
425
|
+
// TODO: Add code example
|
|
426
|
+
/**
|
|
427
|
+
* Converts the string representation of an IPv6 address to a bigint.
|
|
428
|
+
*
|
|
429
|
+
* @see {@link Ipv6Address}
|
|
430
|
+
*
|
|
431
|
+
* @public
|
|
432
|
+
* @param ip string representation of the IPv6 address
|
|
433
|
+
* @returns an instance of Ipv6Address
|
|
434
|
+
*/
|
|
435
|
+
function stringToNum(address) {
|
|
436
|
+
if (address === '::') {
|
|
437
|
+
return 0n;
|
|
438
|
+
}
|
|
439
|
+
let ipv6 = 0n;
|
|
440
|
+
const rawHextets = [];
|
|
441
|
+
const [leftAddress, rightAddress] = address.split('::');
|
|
442
|
+
for (const hextet of leftAddress.split(':')) {
|
|
443
|
+
rawHextets.push(hextet || '0');
|
|
444
|
+
}
|
|
445
|
+
if (rightAddress !== undefined) {
|
|
446
|
+
const rightHextets = rightAddress.split(':');
|
|
447
|
+
const emptyFillCount = 8 - (rawHextets.length + rightHextets.length);
|
|
448
|
+
for (let i = 0; i < emptyFillCount; i++) {
|
|
449
|
+
rawHextets.push('0');
|
|
450
|
+
}
|
|
451
|
+
for (const hextet of rightHextets) {
|
|
452
|
+
rawHextets.push(hextet || '0');
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
const decimals = rawHextets.map(hextet => parseInt(hextet, 16));
|
|
456
|
+
let shiftSize = 0n;
|
|
457
|
+
let binHex = 0n;
|
|
458
|
+
for (const pos in decimals) {
|
|
459
|
+
const num = decimals[pos];
|
|
460
|
+
if (num === 0) {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
shiftSize = BigInt(Math.abs(parseInt(pos) - 7) * 16);
|
|
464
|
+
binHex = BigInt(num) << shiftSize;
|
|
465
|
+
ipv6 |= binHex;
|
|
466
|
+
}
|
|
467
|
+
return ipv6;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Converts the numerical representation of an IPv6 address to its string representation.
|
|
471
|
+
*
|
|
472
|
+
* @see This method is the inverse of {@link ipv6.stringToNum}
|
|
473
|
+
* @throws {@link InvalidIpAddressError}
|
|
474
|
+
*
|
|
475
|
+
* @public
|
|
476
|
+
* @param ip IPv6 address as a number
|
|
477
|
+
* @returns string representation of the address
|
|
478
|
+
*/
|
|
479
|
+
function numToString(num) {
|
|
480
|
+
if (num === 0n) {
|
|
481
|
+
return '::';
|
|
482
|
+
}
|
|
483
|
+
const hextets = [];
|
|
484
|
+
for (let n = 0; n < 8; n++) {
|
|
485
|
+
const bitOffset = BigInt(Math.abs(n - 7) * 16);
|
|
486
|
+
hextets.push(((num >> bitOffset) & MAX_HEXTET_SIZE).toString(16));
|
|
487
|
+
}
|
|
488
|
+
const dropStartIdx = hextets.indexOf('0');
|
|
489
|
+
if (dropStartIdx >= 0) {
|
|
490
|
+
let dropCount = 1;
|
|
491
|
+
for (let i = dropStartIdx + 1; hextets[i] === '0'; i++) {
|
|
492
|
+
dropCount++;
|
|
493
|
+
}
|
|
494
|
+
hextets.splice(dropStartIdx, dropCount, dropStartIdx === 0 || dropStartIdx + dropCount === 8 ? ':' : '');
|
|
495
|
+
}
|
|
496
|
+
return hextets.join(':');
|
|
497
|
+
}/**
|
|
304
498
|
* The numerical maximum size an IPv6 address can be
|
|
305
499
|
*/
|
|
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
|
|
500
|
+
const MAX = 2n ** 128n;var index=/*#__PURE__*/Object.freeze({__proto__:null,Ipv6Address:Ipv6Address,address:address,stringToNum:stringToNum,numToString:numToString,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/src/
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/src/errors.ts","../src/src/ipv4/constants.ts","../src/src/ipv4/ipv4-address.ts","../src/src/ipv4/ipv4-cidr.ts","../src/src/ipv4/rfc1918.ts","../src/src/ipv6/ipv6-address.ts","../src/src/ipv6/constants.ts"],"sourcesContent":[null,null,null,null,null,null,null],"names":["MAX","stringToNum","numToString","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;IAGtB,YAAmB,OAAoB;QACrC,IAAI,CAAC,QAAQ,GAAG,OAAO,OAAO,KAAK,QAAQ,GAAG,OAAO,GAAGC,aAAW,CAAC,OAAO,CAAC,CAAA;KAC7E;;;;IAKD,IAAW,OAAO;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAA;KACrB;;;;;;;;;;;;;;;IAgBM,QAAQ;QACb,OAAOC,aAAW,CAAC,IAAI,CAAC,QAAQ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ACZ,SAAS,EACT,YAAY,KAAK,CAAC,IAAI,YAAY,GAAG,SAAS,KAAK,CAAC,GAAG,GAAG,GAAG,EAAE,CAChE,CAAA;KACF;IACD,OAAO,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AAC1B,CC5MA;;;AAGO,MAAM,GAAG,GAAG,EAAE,IAAI,IAAI"}
|
|
File without changes
|
|
@@ -4,7 +4,7 @@ import { Ipv4Literal, Ipv4Representable } from './types';
|
|
|
4
4
|
* checking.
|
|
5
5
|
*
|
|
6
6
|
* @remarks
|
|
7
|
-
*
|
|
7
|
+
* Avoid direct instantiation; use {@link ipv4.address} instead.
|
|
8
8
|
*/
|
|
9
9
|
export declare class Ipv4Address {
|
|
10
10
|
private _address;
|
|
@@ -14,12 +14,14 @@ export declare class Ipv4Address {
|
|
|
14
14
|
*/
|
|
15
15
|
get address(): number;
|
|
16
16
|
/**
|
|
17
|
+
* Returns the string representation of the address
|
|
18
|
+
*
|
|
17
19
|
* @example
|
|
18
20
|
* ```typescript
|
|
19
21
|
* import { ipv4 as ip } from 'cidr-block'
|
|
20
22
|
*
|
|
21
|
-
* ip.address(
|
|
22
|
-
* ip.address(
|
|
23
|
+
* ip.address(255) // ==> '0.0.0.255'
|
|
24
|
+
* ip.address(0b11111111_00000000_11111111_00000000) // ==> '255.0.255.0'
|
|
23
25
|
* ````
|
|
24
26
|
*
|
|
25
27
|
* @public
|
|
@@ -27,7 +29,7 @@ export declare class Ipv4Address {
|
|
|
27
29
|
*/
|
|
28
30
|
toString(): string;
|
|
29
31
|
/**
|
|
30
|
-
* Compares if two
|
|
32
|
+
* Compares if two IPv4 addresses are the same.
|
|
31
33
|
*
|
|
32
34
|
* @example
|
|
33
35
|
* ```typescript
|
|
@@ -44,12 +46,14 @@ export declare class Ipv4Address {
|
|
|
44
46
|
*/
|
|
45
47
|
equals(otherIpAddress: Ipv4Representable): boolean;
|
|
46
48
|
/**
|
|
49
|
+
* Calculates the next logical IPv4 address.
|
|
50
|
+
*
|
|
47
51
|
* @example
|
|
48
52
|
* ```typescript
|
|
49
53
|
* import { ipv4 as ip } from 'cidr-block'
|
|
50
54
|
*
|
|
51
55
|
* const myIp = ip.address('52.89.32.255')
|
|
52
|
-
* myIp.nextIp() // ==> '52.89.33.0
|
|
56
|
+
* myIp.nextIp() // ==> '52.89.33.0'
|
|
53
57
|
* ```
|
|
54
58
|
*
|
|
55
59
|
* @public
|
|
@@ -62,7 +66,7 @@ export declare class Ipv4Address {
|
|
|
62
66
|
* import { ipv4 as ip } from 'cidr-block'
|
|
63
67
|
*
|
|
64
68
|
* const myIp = ip.address('52.89.32.19')
|
|
65
|
-
* myIp.previousIp() // ==> '52.89.32.18
|
|
69
|
+
* myIp.previousIp() // ==> '52.89.32.18'
|
|
66
70
|
* ```
|
|
67
71
|
*
|
|
68
72
|
* @public
|
|
@@ -74,11 +78,8 @@ export declare class Ipv4Address {
|
|
|
74
78
|
* Convenience function for creating an IPv4 address instance.
|
|
75
79
|
*
|
|
76
80
|
* @remarks
|
|
77
|
-
*
|
|
78
81
|
* In general, you should use this function instead of instantiating an Ipv4Address
|
|
79
|
-
* object directly.
|
|
80
|
-
* methods like these are meant to help reduce the footprint of your code and increase
|
|
81
|
-
* readability.
|
|
82
|
+
* object directly.
|
|
82
83
|
*
|
|
83
84
|
* @example
|
|
84
85
|
*
|
|
@@ -90,6 +91,7 @@ export declare class Ipv4Address {
|
|
|
90
91
|
*
|
|
91
92
|
* @see {@link Ipv4Address}
|
|
92
93
|
*
|
|
94
|
+
* @public
|
|
93
95
|
* @param ip string representation of the IPv4 address
|
|
94
96
|
* @returns an instance of Ipv4Address
|
|
95
97
|
*/
|
|
@@ -98,12 +100,11 @@ export declare function address(ip: Ipv4Literal): Ipv4Address;
|
|
|
98
100
|
* Converts the string representation of an IPv4 address to a number.
|
|
99
101
|
*
|
|
100
102
|
* @example
|
|
101
|
-
*
|
|
102
103
|
* ```typescript
|
|
103
104
|
* import * as cidr from 'cidr-block'
|
|
104
105
|
*
|
|
105
|
-
* cidr.ipv4.stringToNum('255.255.255.255') ===
|
|
106
|
-
* cidr.ipv4.stringToNum('0.0.0.255') ===
|
|
106
|
+
* cidr.ipv4.stringToNum('255.255.255.255') === 4_294_967_295 // ==> true
|
|
107
|
+
* cidr.ipv4.stringToNum('0.0.0.255') === 255 // ==> true
|
|
107
108
|
* ```
|
|
108
109
|
*
|
|
109
110
|
* @see This method is the inverse of {@link ipv4.numToString}
|
|
@@ -115,16 +116,15 @@ export declare function address(ip: Ipv4Literal): Ipv4Address;
|
|
|
115
116
|
*/
|
|
116
117
|
export declare function stringToNum(address: string): number;
|
|
117
118
|
/**
|
|
118
|
-
* Converts the numerical
|
|
119
|
+
* Converts the numerical representation of an IPv4 address to its string representation.
|
|
119
120
|
*
|
|
120
121
|
* @example
|
|
121
|
-
*
|
|
122
122
|
* ```typescript
|
|
123
123
|
* import * as cidr from 'cidr-block'
|
|
124
124
|
*
|
|
125
|
-
* cidr.ipv4.numToString(
|
|
126
|
-
* cidr.ipv4.numToString(
|
|
127
|
-
* cidr.ipv4.numToString(
|
|
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
128
|
* ```
|
|
129
129
|
*
|
|
130
130
|
* @see This method is the inverse of {@link ipv4.stringToNum}
|
|
File without changes
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Ipv4Address } from './ipv4-address';
|
|
2
|
+
/**
|
|
3
|
+
* Predicate function that will return true if the given
|
|
4
|
+
* address is in the private RFC 1918 ipv4 address space.
|
|
5
|
+
*
|
|
6
|
+
* See more {@link https://datatracker.ietf.org/doc/html/rfc1918}
|
|
7
|
+
*/
|
|
8
|
+
export declare function isPrivateRFC1918(address: Ipv4Address): boolean;
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { Ipv6Literal, Ipv6Representable } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Representation of an IPv6 address. Provides various utilities methods like equality
|
|
4
|
+
* checking.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Avoid direct instantiation; use {@link ipv6.address} instead.
|
|
8
|
+
*/
|
|
9
|
+
export declare class Ipv6Address {
|
|
10
|
+
private _address;
|
|
11
|
+
constructor(address: Ipv6Literal);
|
|
12
|
+
/**
|
|
13
|
+
* The address as a bigint
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* Because the representation of an IPv6 address is too large to fit into a typical
|
|
17
|
+
* JavaScript integer, a
|
|
18
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt}
|
|
19
|
+
* is used instead.
|
|
20
|
+
*/
|
|
21
|
+
get address(): bigint;
|
|
22
|
+
/**
|
|
23
|
+
* Returns the string representation of the address
|
|
24
|
+
*/
|
|
25
|
+
toString(): string;
|
|
26
|
+
/**
|
|
27
|
+
* Compares if two IPv6 addresses are the same.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { ipv6 as ip } from 'cidr-block'
|
|
32
|
+
*
|
|
33
|
+
* function isLoopback(address: Ipv6Representable) {
|
|
34
|
+
* return ip.address(address).equals('::1')
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* @public
|
|
38
|
+
* @param otherIpAddress the other Ipv6 address to compare
|
|
39
|
+
* @returns if the other IP address is the same
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
equals(otherIpAddress: Ipv6Representable): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Calculates the next logical IPv6 address.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* import { ipv6 as ip } from 'cidr-block'
|
|
49
|
+
*
|
|
50
|
+
* const myIp = ip.address('2001:0db8::ac10')
|
|
51
|
+
* myIp.nextIp() // ==> '2001:0db8::ac11'
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
nextIp(): Ipv6Address;
|
|
55
|
+
/**
|
|
56
|
+
* Calculates the previous logical IPv6 address.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* import { ipv6 as ip } from 'cidr-block'
|
|
61
|
+
*
|
|
62
|
+
* const myIp = ip.address('2001:0db8::ac10')
|
|
63
|
+
* myIp.previousIp() // ==> '2001:0db8::ac09'
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
previousIp(): Ipv6Address;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Convenience function for creating an IPv6 address instance.
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* In general, you should use this function instead of instantiating an Ipv6Address
|
|
73
|
+
* object directly.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
*
|
|
77
|
+
* ```typescript
|
|
78
|
+
* import { ipv6 as ip } from 'cidr-block'
|
|
79
|
+
*
|
|
80
|
+
* const localhost = ip.address('::1')
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @see {@link Ipv6Address}
|
|
84
|
+
*
|
|
85
|
+
* @param ip string representation of the IPv6 address
|
|
86
|
+
* @returns an instance of Ipv6Address
|
|
87
|
+
*/
|
|
88
|
+
export declare function address(ip: Ipv6Literal): Ipv6Address;
|
|
89
|
+
/**
|
|
90
|
+
* Converts the string representation of an IPv6 address to a bigint.
|
|
91
|
+
*
|
|
92
|
+
* @see {@link Ipv6Address}
|
|
93
|
+
*
|
|
94
|
+
* @public
|
|
95
|
+
* @param ip string representation of the IPv6 address
|
|
96
|
+
* @returns an instance of Ipv6Address
|
|
97
|
+
*/
|
|
98
|
+
export declare function stringToNum(address: string): bigint;
|
|
99
|
+
/**
|
|
100
|
+
* Converts the numerical representation of an IPv6 address to its string representation.
|
|
101
|
+
*
|
|
102
|
+
* @see This method is the inverse of {@link ipv6.stringToNum}
|
|
103
|
+
* @throws {@link InvalidIpAddressError}
|
|
104
|
+
*
|
|
105
|
+
* @public
|
|
106
|
+
* @param ip IPv6 address as a number
|
|
107
|
+
* @returns string representation of the address
|
|
108
|
+
*/
|
|
109
|
+
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/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './constants';
|