cidr-block 1.3.1 → 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 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
- // or commonjs-style
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 './cidr/ipv4/index';
2
- export * as ipv6 from './cidr/ipv6/index';
3
- export * as errors from './cidr/errors';
1
+ export * as ipv4 from './ipv4/index';
2
+ export * as ipv6 from './ipv6/index';
3
+ export * as errors from './errors';
@@ -16,11 +16,11 @@ const MAX$1 = 2 ** 32 - 1;const MAX_OCTET_SIZE = 255;
16
16
  * checking.
17
17
  *
18
18
  * @remarks
19
- * Direct instantiation should be avoided; use {@link ipv4.address} instead.
19
+ * Avoid direct instantiation; use {@link ipv4.address} instead.
20
20
  */
21
21
  class Ipv4Address {
22
22
  constructor(address) {
23
- this._address = typeof address === 'number' ? address : stringToNum(address);
23
+ this._address = typeof address === 'number' ? address : stringToNum$1(address);
24
24
  }
25
25
  /**
26
26
  * The address as a number
@@ -29,6 +29,8 @@ class Ipv4Address {
29
29
  return this._address;
30
30
  }
31
31
  /**
32
+ * Returns the string representation of the address
33
+ *
32
34
  * @example
33
35
  * ```typescript
34
36
  * import { ipv4 as ip } from 'cidr-block'
@@ -41,10 +43,10 @@ class Ipv4Address {
41
43
  * @returns the IPv4 address as a string
42
44
  */
43
45
  toString() {
44
- return numToString(this._address);
46
+ return numToString$1(this._address);
45
47
  }
46
48
  /**
47
- * Compares if two IP address are the same.
49
+ * Compares if two IPv4 addresses are the same.
48
50
  *
49
51
  * @example
50
52
  * ```typescript
@@ -64,23 +66,26 @@ class Ipv4Address {
64
66
  return this._address === otherIpAddress._address;
65
67
  }
66
68
  else {
67
- return this._address === address(otherIpAddress)._address;
69
+ return this._address === address$1(otherIpAddress)._address;
68
70
  }
69
71
  }
70
72
  /**
73
+ * Calculates the next logical IPv4 address.
74
+ *
71
75
  * @example
72
76
  * ```typescript
73
77
  * import { ipv4 as ip } from 'cidr-block'
74
78
  *
75
79
  * const myIp = ip.address('52.89.32.255')
76
- * myIp.nextIp() // ==> '52.89.33.0
80
+ * myIp.nextIp() // ==> '52.89.33.0'
77
81
  * ```
78
82
  *
79
83
  * @public
80
84
  * @returns the next consecutive IPv4 address
81
85
  */
82
86
  nextIp() {
83
- return address(this._address + 1);
87
+ // TODO: Handle last ip address
88
+ return address$1(this._address + 1);
84
89
  }
85
90
  /**
86
91
  * @example
@@ -88,25 +93,22 @@ class Ipv4Address {
88
93
  * import { ipv4 as ip } from 'cidr-block'
89
94
  *
90
95
  * const myIp = ip.address('52.89.32.19')
91
- * myIp.previousIp() // ==> '52.89.32.18
96
+ * myIp.previousIp() // ==> '52.89.32.18'
92
97
  * ```
93
98
  *
94
99
  * @public
95
100
  * @returns the preceding IPv4 address
96
101
  */
97
102
  previousIp() {
98
- return address(this._address - 1);
103
+ return address$1(this._address - 1);
99
104
  }
100
105
  }
101
106
  /**
102
107
  * Convenience function for creating an IPv4 address instance.
103
108
  *
104
109
  * @remarks
105
- *
106
110
  * In general, you should use this function instead of instantiating an Ipv4Address
107
- * object directly. While there is nothing wrong with direct instantiation, convenience
108
- * methods like these are meant to help reduce the footprint of your code and increase
109
- * readability.
111
+ * object directly.
110
112
  *
111
113
  * @example
112
114
  *
@@ -118,17 +120,18 @@ class Ipv4Address {
118
120
  *
119
121
  * @see {@link Ipv4Address}
120
122
  *
123
+ * @public
121
124
  * @param ip string representation of the IPv4 address
122
125
  * @returns an instance of Ipv4Address
123
126
  */
124
- function address(ip) {
127
+ function address$1(ip) {
128
+ // TODO: Implement memoization
125
129
  return new Ipv4Address(ip);
126
130
  }
127
131
  /**
128
132
  * Converts the string representation of an IPv4 address to a number.
129
133
  *
130
134
  * @example
131
- *
132
135
  * ```typescript
133
136
  * import * as cidr from 'cidr-block'
134
137
  *
@@ -143,7 +146,7 @@ function address(ip) {
143
146
  * @param address IPv4 address represented as a string
144
147
  * @returns numerical number representation of the address
145
148
  */
146
- function stringToNum(address) {
149
+ function stringToNum$1(address) {
147
150
  try {
148
151
  if (address.length < 7) {
149
152
  throw new Error();
@@ -163,10 +166,9 @@ function stringToNum(address) {
163
166
  }
164
167
  }
165
168
  /**
166
- * Converts the numerical number representation of an IPv4 address to its string representation.
169
+ * Converts the numerical representation of an IPv4 address to its string representation.
167
170
  *
168
171
  * @example
169
- *
170
172
  * ```typescript
171
173
  * import * as cidr from 'cidr-block'
172
174
  *
@@ -182,7 +184,7 @@ function stringToNum(address) {
182
184
  * @param ip IPv4 address as a number
183
185
  * @returns string representation of the address
184
186
  */
185
- function numToString(ip) {
187
+ function numToString$1(ip) {
186
188
  try {
187
189
  if (ip < 0 || ip > MAX$1) {
188
190
  throw new Error();
@@ -200,8 +202,8 @@ function numToString(ip) {
200
202
  // TODO: Allow wider-range of values that can be used to create a cidr
201
203
  constructor(cidrRange) {
202
204
  try {
203
- const [address$1, subnetMask] = cidrRange.split('/');
204
- this._ipAddress = address(address$1);
205
+ const [address, subnetMask] = cidrRange.split('/');
206
+ this._ipAddress = address$1(address);
205
207
  this._maskSize = Number(subnetMask);
206
208
  }
207
209
  catch {
@@ -224,20 +226,20 @@ function numToString(ip) {
224
226
  * The actual IPv4 netmask address
225
227
  */
226
228
  get netmask() {
227
- return address((2 ** this.maskSize - 1) << this.addressLength);
229
+ return address$1((2 ** this.maskSize - 1) << this.addressLength);
228
230
  }
229
231
  /**
230
232
  * The first IPv4 address that is usable within the given cidr range
231
233
  */
232
234
  get firstUsableIp() {
233
- return address(this._ipAddress.address);
235
+ return address$1(this._ipAddress.address);
234
236
  }
235
237
  /**
236
238
  * The last IPv4 address that is usable within the given cidr range
237
239
  */
238
240
  get lastUsableIp() {
239
241
  // FIXME: Handle edge case of when cidr range goes outside valid ip range
240
- return address(this._ipAddress.address + 2 ** this.addressLength - 1);
242
+ return address$1(this._ipAddress.address + 2 ** this.addressLength - 1);
241
243
  }
242
244
  get addressLength() {
243
245
  return Math.abs(32 - this._maskSize);
@@ -253,20 +255,20 @@ function numToString(ip) {
253
255
  */
254
256
  nextBlock(ofSize) {
255
257
  const nextIp = this._ipAddress.address + 2 ** this.addressLength;
256
- return cidr(`${numToString(nextIp)}/${ofSize ?? this._maskSize}`);
258
+ return cidr(`${numToString$1(nextIp)}/${ofSize ?? this._maskSize}`);
257
259
  }
258
260
  /**
259
261
  * @returns the previous cidr block
260
262
  */
261
263
  previousBlock() {
262
264
  const nextIp = this._ipAddress.address - 2 ** this.addressLength;
263
- return cidr(`${numToString(nextIp)}/${this._maskSize}`);
265
+ return cidr(`${numToString$1(nextIp)}/${this._maskSize}`);
264
266
  }
265
267
  /**
266
268
  * @returns if the given IPv4 address is within the cidr range
267
269
  */
268
- includes(address$1) {
269
- const ip = address$1 instanceof Ipv4Address ? address$1 : address(address$1);
270
+ includes(address) {
271
+ const ip = address instanceof Ipv4Address ? address : address$1(address);
270
272
  return (
271
273
  // FIXME: How to handle edge case of next block erroring out?
272
274
  ip.address >= this._ipAddress.address && ip.address <= this.nextBlock()._ipAddress.address);
@@ -297,7 +299,202 @@ function numToString(ip) {
297
299
  */
298
300
  function cidr(cidrRange) {
299
301
  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});/**
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
+ }/**
301
498
  * The numerical maximum size an IPv6 address can be
302
499
  */
303
- 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
@@ -1 +1 @@
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,CAAA;AAE1B;;;;;;;MAOa,WAAW;IAGtB,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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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"}
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,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,CAAC,CAAA;KAClC;;;;;;;;;;;;;;;;;IAkBM,MAAM,CAAC,cAAiC;QAC7C,IAAI,cAAc,YAAY,WAAW,EAAE;YACzC,OAAO,IAAI,CAAC,QAAQ,KAAK,cAAc,CAAC,Q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AAC,CAAA;AAC1B,CC5MA;;;AAGO,MAAM,GAAG,GAAG,EAAE,IAAI,IAAI"}
package/build/index.js CHANGED
@@ -16,11 +16,11 @@ const MAX$1 = 2 ** 32 - 1;const MAX_OCTET_SIZE = 255;
16
16
  * checking.
17
17
  *
18
18
  * @remarks
19
- * Direct instantiation should be avoided; use {@link ipv4.address} instead.
19
+ * Avoid direct instantiation; use {@link ipv4.address} instead.
20
20
  */
21
21
  class Ipv4Address {
22
22
  constructor(address) {
23
- this._address = typeof address === 'number' ? address : stringToNum(address);
23
+ this._address = typeof address === 'number' ? address : stringToNum$1(address);
24
24
  }
25
25
  /**
26
26
  * The address as a number
@@ -29,6 +29,8 @@ class Ipv4Address {
29
29
  return this._address;
30
30
  }
31
31
  /**
32
+ * Returns the string representation of the address
33
+ *
32
34
  * @example
33
35
  * ```typescript
34
36
  * import { ipv4 as ip } from 'cidr-block'
@@ -41,10 +43,10 @@ class Ipv4Address {
41
43
  * @returns the IPv4 address as a string
42
44
  */
43
45
  toString() {
44
- return numToString(this._address);
46
+ return numToString$1(this._address);
45
47
  }
46
48
  /**
47
- * Compares if two IP address are the same.
49
+ * Compares if two IPv4 addresses are the same.
48
50
  *
49
51
  * @example
50
52
  * ```typescript
@@ -64,23 +66,26 @@ class Ipv4Address {
64
66
  return this._address === otherIpAddress._address;
65
67
  }
66
68
  else {
67
- return this._address === address(otherIpAddress)._address;
69
+ return this._address === address$1(otherIpAddress)._address;
68
70
  }
69
71
  }
70
72
  /**
73
+ * Calculates the next logical IPv4 address.
74
+ *
71
75
  * @example
72
76
  * ```typescript
73
77
  * import { ipv4 as ip } from 'cidr-block'
74
78
  *
75
79
  * const myIp = ip.address('52.89.32.255')
76
- * myIp.nextIp() // ==> '52.89.33.0
80
+ * myIp.nextIp() // ==> '52.89.33.0'
77
81
  * ```
78
82
  *
79
83
  * @public
80
84
  * @returns the next consecutive IPv4 address
81
85
  */
82
86
  nextIp() {
83
- return address(this._address + 1);
87
+ // TODO: Handle last ip address
88
+ return address$1(this._address + 1);
84
89
  }
85
90
  /**
86
91
  * @example
@@ -88,25 +93,22 @@ class Ipv4Address {
88
93
  * import { ipv4 as ip } from 'cidr-block'
89
94
  *
90
95
  * const myIp = ip.address('52.89.32.19')
91
- * myIp.previousIp() // ==> '52.89.32.18
96
+ * myIp.previousIp() // ==> '52.89.32.18'
92
97
  * ```
93
98
  *
94
99
  * @public
95
100
  * @returns the preceding IPv4 address
96
101
  */
97
102
  previousIp() {
98
- return address(this._address - 1);
103
+ return address$1(this._address - 1);
99
104
  }
100
105
  }
101
106
  /**
102
107
  * Convenience function for creating an IPv4 address instance.
103
108
  *
104
109
  * @remarks
105
- *
106
110
  * In general, you should use this function instead of instantiating an Ipv4Address
107
- * object directly. While there is nothing wrong with direct instantiation, convenience
108
- * methods like these are meant to help reduce the footprint of your code and increase
109
- * readability.
111
+ * object directly.
110
112
  *
111
113
  * @example
112
114
  *
@@ -118,17 +120,18 @@ class Ipv4Address {
118
120
  *
119
121
  * @see {@link Ipv4Address}
120
122
  *
123
+ * @public
121
124
  * @param ip string representation of the IPv4 address
122
125
  * @returns an instance of Ipv4Address
123
126
  */
124
- function address(ip) {
127
+ function address$1(ip) {
128
+ // TODO: Implement memoization
125
129
  return new Ipv4Address(ip);
126
130
  }
127
131
  /**
128
132
  * Converts the string representation of an IPv4 address to a number.
129
133
  *
130
134
  * @example
131
- *
132
135
  * ```typescript
133
136
  * import * as cidr from 'cidr-block'
134
137
  *
@@ -143,7 +146,7 @@ function address(ip) {
143
146
  * @param address IPv4 address represented as a string
144
147
  * @returns numerical number representation of the address
145
148
  */
146
- function stringToNum(address) {
149
+ function stringToNum$1(address) {
147
150
  try {
148
151
  if (address.length < 7) {
149
152
  throw new Error();
@@ -163,10 +166,9 @@ function stringToNum(address) {
163
166
  }
164
167
  }
165
168
  /**
166
- * Converts the numerical number representation of an IPv4 address to its string representation.
169
+ * Converts the numerical representation of an IPv4 address to its string representation.
167
170
  *
168
171
  * @example
169
- *
170
172
  * ```typescript
171
173
  * import * as cidr from 'cidr-block'
172
174
  *
@@ -182,7 +184,7 @@ function stringToNum(address) {
182
184
  * @param ip IPv4 address as a number
183
185
  * @returns string representation of the address
184
186
  */
185
- function numToString(ip) {
187
+ function numToString$1(ip) {
186
188
  try {
187
189
  if (ip < 0 || ip > MAX$1) {
188
190
  throw new Error();
@@ -200,8 +202,8 @@ function numToString(ip) {
200
202
  // TODO: Allow wider-range of values that can be used to create a cidr
201
203
  constructor(cidrRange) {
202
204
  try {
203
- const [address$1, subnetMask] = cidrRange.split('/');
204
- this._ipAddress = address(address$1);
205
+ const [address, subnetMask] = cidrRange.split('/');
206
+ this._ipAddress = address$1(address);
205
207
  this._maskSize = Number(subnetMask);
206
208
  }
207
209
  catch {
@@ -224,20 +226,20 @@ function numToString(ip) {
224
226
  * The actual IPv4 netmask address
225
227
  */
226
228
  get netmask() {
227
- return address((2 ** this.maskSize - 1) << this.addressLength);
229
+ return address$1((2 ** this.maskSize - 1) << this.addressLength);
228
230
  }
229
231
  /**
230
232
  * The first IPv4 address that is usable within the given cidr range
231
233
  */
232
234
  get firstUsableIp() {
233
- return address(this._ipAddress.address);
235
+ return address$1(this._ipAddress.address);
234
236
  }
235
237
  /**
236
238
  * The last IPv4 address that is usable within the given cidr range
237
239
  */
238
240
  get lastUsableIp() {
239
241
  // FIXME: Handle edge case of when cidr range goes outside valid ip range
240
- return address(this._ipAddress.address + 2 ** this.addressLength - 1);
242
+ return address$1(this._ipAddress.address + 2 ** this.addressLength - 1);
241
243
  }
242
244
  get addressLength() {
243
245
  return Math.abs(32 - this._maskSize);
@@ -253,20 +255,20 @@ function numToString(ip) {
253
255
  */
254
256
  nextBlock(ofSize) {
255
257
  const nextIp = this._ipAddress.address + 2 ** this.addressLength;
256
- return cidr(`${numToString(nextIp)}/${ofSize ?? this._maskSize}`);
258
+ return cidr(`${numToString$1(nextIp)}/${ofSize ?? this._maskSize}`);
257
259
  }
258
260
  /**
259
261
  * @returns the previous cidr block
260
262
  */
261
263
  previousBlock() {
262
264
  const nextIp = this._ipAddress.address - 2 ** this.addressLength;
263
- return cidr(`${numToString(nextIp)}/${this._maskSize}`);
265
+ return cidr(`${numToString$1(nextIp)}/${this._maskSize}`);
264
266
  }
265
267
  /**
266
268
  * @returns if the given IPv4 address is within the cidr range
267
269
  */
268
- includes(address$1) {
269
- const ip = address$1 instanceof Ipv4Address ? address$1 : address(address$1);
270
+ includes(address) {
271
+ const ip = address instanceof Ipv4Address ? address : address$1(address);
270
272
  return (
271
273
  // FIXME: How to handle edge case of next block erroring out?
272
274
  ip.address >= this._ipAddress.address && ip.address <= this.nextBlock()._ipAddress.address);
@@ -297,7 +299,202 @@ function numToString(ip) {
297
299
  */
298
300
  function cidr(cidrRange) {
299
301
  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});/**
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
+ }/**
301
498
  * The numerical maximum size an IPv6 address can be
302
499
  */
303
- 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
@@ -1 +1 @@
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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GAAG,IAAI,CAAC,UAAU,CAAC,OAAO,GAAG,CAAC,IAAI,IAAI,CAAC,aAAa,CAAA;QAChE,OAAO,IAAI,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC,CAAA;KAClE;;;;IAKM,aAAa;QAClB,MAAM,MAAM,GAAG,IAAI,CAAC,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"}
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,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C,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"}
File without changes
@@ -2,3 +2,4 @@ export * from './ipv4-address';
2
2
  export * from './ipv4-cidr';
3
3
  export * from './constants';
4
4
  export * from './types';
5
+ export * as rfc1918 from './rfc1918';
@@ -4,7 +4,7 @@ import { Ipv4Literal, Ipv4Representable } from './types';
4
4
  * checking.
5
5
  *
6
6
  * @remarks
7
- * Direct instantiation should be avoided; use {@link ipv4.address} instead.
7
+ * Avoid direct instantiation; use {@link ipv4.address} instead.
8
8
  */
9
9
  export declare class Ipv4Address {
10
10
  private _address;
@@ -14,6 +14,8 @@ 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'
@@ -27,7 +29,7 @@ export declare class Ipv4Address {
27
29
  */
28
30
  toString(): string;
29
31
  /**
30
- * Compares if two IP address are the same.
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. 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
+ * 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,7 +100,6 @@ 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
  *
@@ -115,10 +116,9 @@ export declare function address(ip: Ipv4Literal): Ipv4Address;
115
116
  */
116
117
  export declare function stringToNum(address: string): number;
117
118
  /**
118
- * Converts the numerical number representation of an IPv4 address to its string representation.
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
  *
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,3 @@
1
+ export * from './ipv6-address';
2
+ export * from './constants';
3
+ export * from './types';
@@ -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;
File without changes
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cidr-block",
3
3
  "description": "ipv4 and ipv6 address and cidr range utilities",
4
- "version": "1.3.1",
4
+ "version": "1.3.2",
5
5
  "license": "MIT",
6
6
  "author": "Brandon Burrus <brandon@burrus.io>",
7
7
  "homepage": "https://cidr-block.com",
@@ -1 +0,0 @@
1
- export * from './constants';
@@ -1,13 +0,0 @@
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;