cidr-block 1.3.2 → 2.0.1

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.
Files changed (77) hide show
  1. package/README.md +661 -34
  2. package/dist/cjs/index.cjs +32 -0
  3. package/dist/cjs/index.cjs.map +1 -0
  4. package/dist/cjs/index.d.cts +1945 -0
  5. package/dist/cjs/ipv4-address.cjs +407 -0
  6. package/dist/cjs/ipv4-address.cjs.map +1 -0
  7. package/dist/cjs/ipv4-cidr.cjs +498 -0
  8. package/dist/cjs/ipv4-cidr.cjs.map +1 -0
  9. package/dist/cjs/ipv4-errors.cjs +25 -0
  10. package/dist/cjs/ipv4-errors.cjs.map +1 -0
  11. package/dist/cjs/ipv4.cjs +344 -0
  12. package/dist/cjs/ipv4.cjs.map +1 -0
  13. package/dist/cjs/ipv6-address.cjs +522 -0
  14. package/dist/cjs/ipv6-address.cjs.map +1 -0
  15. package/dist/cjs/ipv6-cidr.cjs +488 -0
  16. package/dist/cjs/ipv6-cidr.cjs.map +1 -0
  17. package/dist/cjs/ipv6-errors.cjs +25 -0
  18. package/dist/cjs/ipv6-errors.cjs.map +1 -0
  19. package/dist/cjs/ipv6.cjs +392 -0
  20. package/dist/cjs/ipv6.cjs.map +1 -0
  21. package/dist/esm/index.d.ts +3 -0
  22. package/dist/esm/index.d.ts.map +1 -0
  23. package/dist/esm/index.js +9 -0
  24. package/dist/esm/index.js.map +1 -0
  25. package/dist/esm/ipv4-address.d.ts +333 -0
  26. package/dist/esm/ipv4-address.d.ts.map +1 -0
  27. package/dist/esm/ipv4-address.js +405 -0
  28. package/dist/esm/ipv4-address.js.map +1 -0
  29. package/dist/esm/ipv4-cidr.d.ts +376 -0
  30. package/dist/esm/ipv4-cidr.d.ts.map +1 -0
  31. package/dist/esm/ipv4-cidr.js +496 -0
  32. package/dist/esm/ipv4-cidr.js.map +1 -0
  33. package/dist/esm/ipv4-errors.d.ts +12 -0
  34. package/dist/esm/ipv4-errors.d.ts.map +1 -0
  35. package/dist/esm/ipv4-errors.js +21 -0
  36. package/dist/esm/ipv4-errors.js.map +1 -0
  37. package/dist/esm/ipv4-types.d.ts +32 -0
  38. package/dist/esm/ipv4-types.d.ts.map +1 -0
  39. package/dist/esm/ipv4.d.ts +221 -0
  40. package/dist/esm/ipv4.d.ts.map +1 -0
  41. package/dist/esm/ipv4.js +339 -0
  42. package/dist/esm/ipv4.js.map +1 -0
  43. package/dist/esm/ipv6-address.d.ts +385 -0
  44. package/dist/esm/ipv6-address.d.ts.map +1 -0
  45. package/dist/esm/ipv6-address.js +520 -0
  46. package/dist/esm/ipv6-address.js.map +1 -0
  47. package/dist/esm/ipv6-cidr.d.ts +361 -0
  48. package/dist/esm/ipv6-cidr.d.ts.map +1 -0
  49. package/dist/esm/ipv6-cidr.js +486 -0
  50. package/dist/esm/ipv6-cidr.js.map +1 -0
  51. package/dist/esm/ipv6-errors.d.ts +12 -0
  52. package/dist/esm/ipv6-errors.d.ts.map +1 -0
  53. package/dist/esm/ipv6-errors.js +21 -0
  54. package/dist/esm/ipv6-errors.js.map +1 -0
  55. package/dist/esm/ipv6-types.d.ts +24 -0
  56. package/dist/esm/ipv6-types.d.ts.map +1 -0
  57. package/dist/esm/ipv6.d.ts +207 -0
  58. package/dist/esm/ipv6.d.ts.map +1 -0
  59. package/dist/esm/ipv6.js +387 -0
  60. package/dist/esm/ipv6.js.map +1 -0
  61. package/package.json +40 -79
  62. package/build/errors.d.ts +0 -6
  63. package/build/index.d.ts +0 -3
  64. package/build/index.esm.js +0 -500
  65. package/build/index.esm.js.map +0 -1
  66. package/build/index.js +0 -500
  67. package/build/index.js.map +0 -1
  68. package/build/ipv4/constants.d.ts +0 -4
  69. package/build/ipv4/index.d.ts +0 -5
  70. package/build/ipv4/ipv4-address.d.ts +0 -137
  71. package/build/ipv4/ipv4-cidr.d.ts +0 -68
  72. package/build/ipv4/rfc1918.d.ts +0 -8
  73. package/build/ipv4/types.d.ts +0 -9
  74. package/build/ipv6/constants.d.ts +0 -4
  75. package/build/ipv6/index.d.ts +0 -3
  76. package/build/ipv6/ipv6-address.d.ts +0 -109
  77. package/build/ipv6/types.d.ts +0 -9
package/README.md CHANGED
@@ -1,68 +1,695 @@
1
- # cidr-block
2
-
3
- ipv4 and ipv6 address and cidr range utilities
1
+ IPv4 and IPv6 address and CIDR range utilities for JavaScript and TypeScript.
4
2
 
5
3
  ## Installation
6
4
 
7
- To install npm package, run the following in your project:
8
-
9
5
  ```bash
10
6
  npm install cidr-block
11
7
  ```
12
8
 
13
- or if you're using yarn instead of npm
9
+ ## Features
14
10
 
15
- ```bash
16
- yarn add cidr-block
11
+ - Full IPv4 and IPv6 address support
12
+ - CIDR block creation and manipulation
13
+ - Address validation and parsing
14
+ - Network calculations (netmask, address count, usable addresses)
15
+ - Subnetting operations
16
+ - Address type detection (private, loopback, multicast, etc.)
17
+ - Full TypeScript support with comprehensive type definitions
18
+ - Zero dependencies
19
+ - Works with ESM and CommonJS
20
+
21
+ ## Quick Start
22
+
23
+ ```typescript
24
+ import { ipv4, ipv6 } from 'cidr-block';
25
+
26
+ // Create and work with IPv4 addresses
27
+ const addr = ipv4.address('192.168.1.1');
28
+ console.log(addr.isPrivateAddress()); // true
29
+
30
+ // Create and work with CIDR blocks
31
+ const cidr = ipv4.cidr('10.0.0.0/16');
32
+ console.log(cidr.addressCount()); // 65536
33
+
34
+ // IPv6 works the same way
35
+ const v6addr = ipv6.address('2001:db8::1');
36
+ console.log(v6addr.toString()); // "2001:db8::1"
37
+ ```
38
+
39
+ ## API Reference
40
+
41
+ Full documentation can be found at [cidr-block.com](https://cidr-block.com).
42
+
43
+ ### IPv4
44
+
45
+ #### Creating Addresses
46
+
47
+ ```typescript
48
+ import { ipv4 } from 'cidr-block';
49
+
50
+ // From string
51
+ const addr1 = ipv4.address('192.168.1.1');
52
+
53
+ // From number
54
+ const addr2 = ipv4.address(3232235777);
55
+
56
+ // From octet array
57
+ const addr3 = ipv4.address([192, 168, 1, 1]);
58
+ ```
59
+
60
+ #### Address Validation
61
+
62
+ ```typescript
63
+ import { ipv4 } from 'cidr-block';
64
+
65
+ ipv4.isValidAddress('192.168.1.1'); // true
66
+ ipv4.isValidAddress('256.1.1.1'); // false
67
+ ipv4.isValidAddress([10, 0, 0, 1]); // true
68
+ ipv4.isValidAddress(4294967295); // true (255.255.255.255)
69
+ ipv4.isValidAddress(4294967296); // false (exceeds max)
70
+ ```
71
+
72
+ #### Address Conversion
73
+
74
+ ```typescript
75
+ import { ipv4 } from 'cidr-block';
76
+
77
+ const addr = ipv4.address('192.168.1.1');
78
+
79
+ addr.toString(); // "192.168.1.1"
80
+ addr.toNumber(); // 3232235777
81
+ addr.octets(); // [192, 168, 1, 1]
82
+ addr.toBinaryString(); // "11000000.10101000.00000001.00000001"
83
+ ```
84
+
85
+ #### Address Type Detection
86
+
87
+ ```typescript
88
+ import { ipv4 } from 'cidr-block';
89
+
90
+ // Private addresses (RFC 1918)
91
+ ipv4.address('10.0.0.1').isPrivateAddress(); // true
92
+ ipv4.address('172.16.0.1').isPrivateAddress(); // true
93
+ ipv4.address('192.168.0.1').isPrivateAddress(); // true
94
+ ipv4.address('8.8.8.8').isPrivateAddress(); // false
95
+
96
+ // Loopback addresses
97
+ ipv4.address('127.0.0.1').isLoopbackAddress(); // true
98
+ ipv4.address('127.255.255.255').isLoopbackAddress(); // true
99
+
100
+ // Link-local addresses
101
+ ipv4.address('169.254.1.1').isLocalLinkAddress(); // true
102
+
103
+ // Multicast addresses
104
+ ipv4.address('224.0.0.1').isMulticastAddress(); // true
105
+ ipv4.address('239.255.255.255').isMulticastAddress(); // true
106
+ ```
107
+
108
+ #### Address Comparison
109
+
110
+ ```typescript
111
+ import { ipv4 } from 'cidr-block';
112
+
113
+ const addr1 = ipv4.address('192.168.1.1');
114
+ const addr2 = ipv4.address('192.168.1.2');
115
+
116
+ addr1.equals('192.168.1.1'); // true
117
+ addr1.equals(addr2); // false
118
+ addr1.isLessThan(addr2); // true
119
+ addr1.isGreaterThan(addr2); // false
120
+ addr1.isLessThanOrEqual(addr2); // true
121
+ addr1.isGreaterThanOrEqual(addr2); // false
122
+ ```
123
+
124
+ #### Address Navigation
125
+
126
+ ```typescript
127
+ import { ipv4 } from 'cidr-block';
128
+
129
+ const addr = ipv4.address('192.168.1.100');
130
+
131
+ addr.hasNextAddress(); // true
132
+ addr.nextAddress()?.toString(); // "192.168.1.101"
133
+
134
+ addr.hasPreviousAddress(); // true
135
+ addr.previousAddress()?.toString(); // "192.168.1.99"
136
+
137
+ // Edge cases
138
+ const maxAddr = ipv4.address('255.255.255.255');
139
+ maxAddr.hasNextAddress(); // false
140
+ maxAddr.nextAddress(); // undefined
141
+
142
+ const minAddr = ipv4.address('0.0.0.0');
143
+ minAddr.hasPreviousAddress(); // false
144
+ minAddr.previousAddress(); // undefined
145
+ ```
146
+
147
+ #### Creating CIDR Blocks
148
+
149
+ ```typescript
150
+ import { ipv4 } from 'cidr-block';
151
+
152
+ // From string
153
+ const cidr1 = ipv4.cidr('192.168.0.0/24');
154
+
155
+ // From object
156
+ const cidr2 = ipv4.cidr({ address: '192.168.0.0', range: 24 });
157
+
158
+ // From tuple
159
+ const cidr3 = ipv4.cidr(['192.168.0.0', 24]);
160
+
161
+ // Mixed formats work too
162
+ const cidr4 = ipv4.cidr({ address: [192, 168, 0, 0], range: 24 });
163
+ ```
164
+
165
+ #### CIDR Validation
166
+
167
+ ```typescript
168
+ import { ipv4 } from 'cidr-block';
169
+
170
+ ipv4.isValidCIDR('192.168.0.0/24'); // true
171
+ ipv4.isValidCIDR('192.168.0.0/33'); // false (range exceeds 32)
172
+ ipv4.isValidCIDR('256.0.0.0/24'); // false (invalid address)
173
+ ipv4.isValidCIDR(['10.0.0.0', 8]); // true
174
+ ```
175
+
176
+ #### CIDR Properties
177
+
178
+ ```typescript
179
+ import { ipv4 } from 'cidr-block';
180
+
181
+ const cidr = ipv4.cidr('192.168.0.0/24');
182
+
183
+ cidr.toString(); // "192.168.0.0/24"
184
+ cidr.baseAddress().toString(); // "192.168.0.0"
185
+ cidr.range(); // 24
186
+ cidr.netmask().toString(); // "255.255.255.0"
187
+ cidr.addressCount(); // 256
188
+ cidr.rangeParts(); // [Ipv4Address, 24]
189
+ ```
190
+
191
+ #### Usable Addresses
192
+
193
+ ```typescript
194
+ import { ipv4 } from 'cidr-block';
195
+
196
+ const cidr = ipv4.cidr('192.168.1.0/24');
197
+
198
+ // First usable (excludes network address)
199
+ cidr.getFirstUsableAddress()?.toString(); // "192.168.1.1"
200
+
201
+ // Last usable (excludes broadcast address)
202
+ cidr.getLastUsableAddress()?.toString(); // "192.168.1.254"
203
+
204
+ // For /32, there are no usable addresses
205
+ const hostCidr = ipv4.cidr('192.168.1.1/32');
206
+ hostCidr.getFirstUsableAddress(); // undefined
207
+ hostCidr.getLastUsableAddress(); // undefined
208
+ ```
209
+
210
+ #### Iterating Addresses
211
+
212
+ ```typescript
213
+ import { ipv4 } from 'cidr-block';
214
+
215
+ const cidr = ipv4.cidr('192.168.1.0/30');
216
+
217
+ // Using the generator
218
+ for (const addr of cidr.addresses()) {
219
+ console.log(addr.toString());
220
+ }
221
+ // Output:
222
+ // 192.168.1.0
223
+ // 192.168.1.1
224
+ // 192.168.1.2
225
+ // 192.168.1.3
226
+
227
+ // Convert to array
228
+ const allAddresses = [...cidr.addresses()];
229
+ console.log(allAddresses.length); // 4
230
+ ```
231
+
232
+ #### Address Containment
233
+
234
+ ```typescript
235
+ import { ipv4 } from 'cidr-block';
236
+
237
+ const cidr = ipv4.cidr('192.168.0.0/24');
238
+
239
+ cidr.includes(ipv4.address('192.168.0.100')); // true
240
+ cidr.includes(ipv4.address('192.168.1.1')); // false
241
+ cidr.includes(ipv4.address('192.168.0.0')); // true
242
+ cidr.includes(ipv4.address('192.168.0.255')); // true
243
+ ```
244
+
245
+ #### CIDR Overlap Detection
246
+
247
+ ```typescript
248
+ import { ipv4 } from 'cidr-block';
249
+
250
+ const cidr1 = ipv4.cidr('192.168.0.0/24');
251
+ const cidr2 = ipv4.cidr('192.168.0.128/25');
252
+ const cidr3 = ipv4.cidr('192.168.1.0/24');
253
+
254
+ cidr1.overlaps(cidr2); // true (cidr2 is a subnet of cidr1)
255
+ cidr1.overlaps(cidr3); // false (different networks)
256
+
257
+ // Also accepts string format
258
+ cidr1.overlaps('10.0.0.0/8'); // false
259
+ ```
260
+
261
+ #### Subnetting
262
+
263
+ ```typescript
264
+ import { ipv4 } from 'cidr-block';
265
+
266
+ const cidr = ipv4.cidr('192.168.0.0/24');
267
+
268
+ // Split into equal subnets
269
+ const subnets = cidr.subnet(26);
270
+ subnets.forEach(s => console.log(s.toString()));
271
+ // Output:
272
+ // 192.168.0.0/26
273
+ // 192.168.0.64/26
274
+ // 192.168.0.128/26
275
+ // 192.168.0.192/26
276
+
277
+ // Split into variable-sized subnets
278
+ const varSubnets = cidr.subnetBy([26, 27, 27, 26]);
279
+ varSubnets.forEach(s => console.log(s.toString()));
280
+ // Output:
281
+ // 192.168.0.0/26
282
+ // 192.168.0.64/27
283
+ // 192.168.0.96/27
284
+ // 192.168.0.128/26
285
+ ```
286
+
287
+ #### CIDR Navigation
288
+
289
+ ```typescript
290
+ import { ipv4 } from 'cidr-block';
291
+
292
+ const cidr = ipv4.cidr('192.168.0.0/24');
293
+
294
+ cidr.hasNextCIDR(); // true
295
+ cidr.nextCIDR()?.toString(); // "192.168.1.0/24"
296
+
297
+ cidr.hasPreviousCIDR(); // true
298
+ cidr.previousCIDR()?.toString(); // "192.167.255.0/24"
299
+ ```
300
+
301
+ ### IPv6
302
+
303
+ #### Creating Addresses
304
+
305
+ ```typescript
306
+ import { ipv6 } from 'cidr-block';
307
+
308
+ // From string (with :: compression)
309
+ const addr1 = ipv6.address('2001:db8::1');
310
+
311
+ // From full string
312
+ const addr2 = ipv6.address('2001:0db8:0000:0000:0000:0000:0000:0001');
313
+
314
+ // From BigInt
315
+ const addr3 = ipv6.address(42540766411282592856903984951653826561n);
316
+
317
+ // From hextets array
318
+ const addr4 = ipv6.address([0x2001, 0x0db8, 0, 0, 0, 0, 0, 1]);
17
319
  ```
18
320
 
19
- The package is written completely in TypeScript and exports all of it's types automatically,
20
- meaning you don't need to install any additional `@types` typings.
321
+ #### Address Validation
21
322
 
22
- ## Getting Started
323
+ ```typescript
324
+ import { ipv6 } from 'cidr-block';
23
325
 
24
- Start by defining a cidr range
326
+ ipv6.isValidAddress('2001:db8::1'); // true
327
+ ipv6.isValidAddress('::1'); // true
328
+ ipv6.isValidAddress('::'); // true
329
+ ipv6.isValidAddress('::ffff:192.168.1.1'); // true (IPv4-mapped)
330
+ ipv6.isValidAddress('2001:db8::g'); // false (invalid hex)
331
+ ipv6.isValidAddress('2001:db8:::1'); // false (multiple ::)
332
+ ```
333
+
334
+ #### Address Conversion
25
335
 
26
336
  ```typescript
27
- import { ipv4 as ip } from 'cidr-block'
337
+ import { ipv6 } from 'cidr-block';
338
+
339
+ const addr = ipv6.address('2001:db8::1');
28
340
 
29
- const myCidr = ip.cidr('10.0.0.0/24')
341
+ addr.toString(); // "2001:db8::1" (compressed)
342
+ addr.toFullString(); // "2001:0db8:0000:0000:0000:0000:0000:0001"
343
+ addr.toBigInt(); // 42540766411282592856903984951653826561n
344
+ addr.hextets(); // [8193, 3512, 0, 0, 0, 0, 0, 1]
345
+ addr.toBinaryString(); // Binary representation with colons
30
346
  ```
31
347
 
32
- To get the next logical cidr block
348
+ #### Address Type Detection
33
349
 
34
350
  ```typescript
35
- console.log(myCidr.nextBlock().toString()) // 10.0.1.0/24
351
+ import { ipv6 } from 'cidr-block';
352
+
353
+ // Loopback
354
+ ipv6.address('::1').isLoopbackAddress(); // true
355
+
356
+ // Unspecified
357
+ ipv6.address('::').isUnspecifiedAddress(); // true
358
+
359
+ // Unique local (private equivalent)
360
+ ipv6.address('fc00::1').isUniqueLocalAddress(); // true
361
+ ipv6.address('fd00::1').isUniqueLocalAddress(); // true
362
+
363
+ // Link-local
364
+ ipv6.address('fe80::1').isLinkLocalAddress(); // true
365
+
366
+ // Multicast
367
+ ipv6.address('ff02::1').isMulticastAddress(); // true
368
+
369
+ // IPv4-mapped
370
+ ipv6.address('::ffff:192.168.1.1').isIPv4MappedAddress(); // true
371
+
372
+ // Documentation
373
+ ipv6.address('2001:db8::1').isDocumentationAddress(); // true
36
374
  ```
37
375
 
38
- All `cidr-block` functions and methods are immutable, meaning a new instance will always be
39
- returned instead of trying to modify the current value.
376
+ #### Address Comparison
377
+
378
+ ```typescript
379
+ import { ipv6 } from 'cidr-block';
380
+
381
+ const addr1 = ipv6.address('2001:db8::1');
382
+ const addr2 = ipv6.address('2001:db8::2');
383
+
384
+ addr1.equals('2001:db8::1'); // true
385
+ addr1.equals(addr2); // false
386
+ addr1.isLessThan(addr2); // true
387
+ addr1.isGreaterThan(addr2); // false
388
+ addr1.isLessThanOrEqual(addr2); // true
389
+ addr1.isGreaterThanOrEqual(addr2); // false
390
+ ```
40
391
 
41
- Once you have a cidr, you have access to all of it's related utilities:
392
+ #### Address Navigation
42
393
 
43
394
  ```typescript
44
- myCidr.netmask // 255.255.255.0
45
- myCidr.firstUsableIp // 10.0.0.0 (remember that methods act immutable, so this is still at 10.0.0.0)
46
- myCidr.lastUsableIp // 10.0.0.254
47
- myCidr.includes(ip.address('10.0.0.128')) // true
395
+ import { ipv6 } from 'cidr-block';
396
+
397
+ const addr = ipv6.address('2001:db8::1');
398
+
399
+ addr.hasNextAddress(); // true
400
+ addr.nextAddress()?.toString(); // "2001:db8::2"
401
+
402
+ addr.hasPreviousAddress(); // true
403
+ addr.previousAddress()?.toString(); // "2001:db8::"
48
404
  ```
49
405
 
50
- ## Documentation and API Reference
406
+ #### Creating CIDR Blocks
51
407
 
52
- The full documentation and API reference can be found at https://cidr-block.com
408
+ ```typescript
409
+ import { ipv6 } from 'cidr-block';
53
410
 
54
- ## FAQ
411
+ // From string
412
+ const cidr1 = ipv6.cidr('2001:db8::/32');
55
413
 
56
- Q: Why are the imports in all the example code like that?
414
+ // From object
415
+ const cidr2 = ipv6.cidr({ address: '2001:db8::', range: 32 });
57
416
 
58
- A: The imports in all example code are formatted as the following:
417
+ // From tuple
418
+ const cidr3 = ipv6.cidr(['2001:db8::', 32]);
419
+ ```
420
+
421
+ #### CIDR Validation
59
422
 
60
423
  ```typescript
61
- // esm
62
- import { ipv4 as ip } from 'cidr-block'
63
- // commonjs
64
- const { ipv4: ip } = require('cidr-block')
424
+ import { ipv6 } from 'cidr-block';
425
+
426
+ ipv6.isValidCIDR('2001:db8::/32'); // true
427
+ ipv6.isValidCIDR('2001:db8::/129'); // false (range exceeds 128)
428
+ ipv6.isValidCIDR(['::1', 128]); // true
65
429
  ```
66
430
 
67
- While you don't have to follow this convention, the API is design like this on purpose to help speed
68
- up a refactoring of ipv4 to ipv6, as you would only need to change the number on the import.
431
+ #### CIDR Properties
432
+
433
+ ```typescript
434
+ import { ipv6 } from 'cidr-block';
435
+
436
+ const cidr = ipv6.cidr('2001:db8::/32');
437
+
438
+ cidr.toString(); // "2001:db8::/32"
439
+ cidr.baseAddress().toString(); // "2001:db8::"
440
+ cidr.range(); // 32
441
+ cidr.netmask().toString(); // "ffff:ffff::"
442
+ cidr.addressCount(); // 79228162514264337593543950336n (BigInt)
443
+ ```
444
+
445
+ #### Usable Addresses
446
+
447
+ ```typescript
448
+ import { ipv6 } from 'cidr-block';
449
+
450
+ const cidr = ipv6.cidr('2001:db8::/126');
451
+
452
+ cidr.getFirstUsableAddress()?.toString(); // "2001:db8::1"
453
+ cidr.getLastUsableAddress()?.toString(); // "2001:db8::2"
454
+
455
+ // For /128, there are no usable addresses
456
+ const hostCidr = ipv6.cidr('2001:db8::1/128');
457
+ hostCidr.getFirstUsableAddress(); // undefined
458
+ hostCidr.getLastUsableAddress(); // undefined
459
+ ```
460
+
461
+ #### Iterating Addresses
462
+
463
+ ```typescript
464
+ import { ipv6 } from 'cidr-block';
465
+
466
+ const cidr = ipv6.cidr('2001:db8::/126');
467
+
468
+ for (const addr of cidr.addresses()) {
469
+ console.log(addr.toString());
470
+ }
471
+ // Output:
472
+ // 2001:db8::
473
+ // 2001:db8::1
474
+ // 2001:db8::2
475
+ // 2001:db8::3
476
+
477
+ // With limit (useful for large ranges)
478
+ for (const addr of cidr.addresses(2n)) {
479
+ console.log(addr.toString());
480
+ }
481
+ // Output:
482
+ // 2001:db8::
483
+ // 2001:db8::1
484
+ ```
485
+
486
+ #### Address Containment and Overlap
487
+
488
+ ```typescript
489
+ import { ipv6 } from 'cidr-block';
490
+
491
+ const cidr = ipv6.cidr('2001:db8::/32');
492
+
493
+ cidr.includes(ipv6.address('2001:db8::1')); // true
494
+ cidr.includes(ipv6.address('2001:db9::1')); // false
495
+
496
+ const cidr2 = ipv6.cidr('2001:db8:1::/48');
497
+ cidr.overlaps(cidr2); // true
498
+ ```
499
+
500
+ #### Subnetting
501
+
502
+ ```typescript
503
+ import { ipv6 } from 'cidr-block';
504
+
505
+ const cidr = ipv6.cidr('2001:db8::/32');
506
+
507
+ // Split into /34 subnets
508
+ const subnets = cidr.subnet(34);
509
+ subnets.forEach(s => console.log(s.toString()));
510
+ // Output:
511
+ // 2001:db8::/34
512
+ // 2001:db8:4000::/34
513
+ // 2001:db8:8000::/34
514
+ // 2001:db8:c000::/34
515
+
516
+ // Variable-sized subnets
517
+ const varSubnets = cidr.subnetBy([34, 34, 33]);
518
+ varSubnets.forEach(s => console.log(s.toString()));
519
+ ```
520
+
521
+ #### CIDR Navigation
522
+
523
+ ```typescript
524
+ import { ipv6 } from 'cidr-block';
525
+
526
+ const cidr = ipv6.cidr('2001:db8::/32');
527
+
528
+ cidr.hasNextCIDR(); // true
529
+ cidr.nextCIDR()?.toString(); // "2001:db9::/32"
530
+
531
+ cidr.hasPreviousCIDR(); // true
532
+ cidr.previousCIDR()?.toString(); // "2001:db7::/32"
533
+ ```
534
+
535
+ ### Error Handling
536
+
537
+ ```typescript
538
+ import {
539
+ ipv4,
540
+ ipv6,
541
+ InvalidIpv4AddressError,
542
+ InvalidIpv4CidrError,
543
+ InvalidIpv4CidrRangeError,
544
+ InvalidIpv6AddressError,
545
+ InvalidIpv6CidrError,
546
+ InvalidIpv6CidrRangeError
547
+ } from 'cidr-block';
548
+
549
+ // Invalid address throws error
550
+ try {
551
+ ipv4.address('256.0.0.1');
552
+ } catch (e) {
553
+ if (e instanceof InvalidIpv4AddressError) {
554
+ console.log(e.message); // "256.0.0.1 is not a valid IPv4 address"
555
+ }
556
+ }
557
+
558
+ // Invalid CIDR throws error
559
+ try {
560
+ ipv4.cidr('192.168.0.0/33');
561
+ } catch (e) {
562
+ if (e instanceof InvalidIpv4CidrError) {
563
+ console.log(e.message); // "192.168.0.0/33 is not a valid IPv4 CIDR range"
564
+ }
565
+ }
566
+
567
+ // Invalid subnet operation throws error
568
+ try {
569
+ const cidr = ipv4.cidr('192.168.0.0/24');
570
+ cidr.subnet(20); // Can't create larger subnets
571
+ } catch (e) {
572
+ if (e instanceof InvalidIpv4CidrRangeError) {
573
+ console.log('Invalid subnet range');
574
+ }
575
+ }
576
+
577
+ // Use validation to avoid exceptions
578
+ if (ipv4.isValidAddress(userInput)) {
579
+ const addr = ipv4.address(userInput);
580
+ // Safe to use
581
+ }
582
+ ```
583
+
584
+ ### Constants
585
+
586
+ ```typescript
587
+ import { ipv4, ipv6 } from 'cidr-block';
588
+
589
+ // IPv4 constants
590
+ ipv4.MAX_SIZE; // 0xffffffff (4294967295)
591
+ ipv4.MIN_SIZE; // 0x00000000
592
+ ipv4.MAX_RANGE; // 32
593
+ ipv4.MIN_RANGE; // 0
594
+
595
+ // IPv6 constants
596
+ ipv6.MAX_SIZE; // (1n << 128n) - 1n
597
+ ipv6.MIN_SIZE; // 0n
598
+ ipv6.MAX_RANGE; // 128
599
+ ipv6.MIN_RANGE; // 0
600
+ ```
601
+
602
+ ### Type Definitions
603
+
604
+ ```typescript
605
+ import type {
606
+ // IPv4 types
607
+ Ipv4AddressLiteral, // string | number | number[]
608
+ Ipv4AddressString, // "${number}.${number}.${number}.${number}"
609
+ Ipv4AddressOctets, // [number, number, number, number]
610
+ Ipv4CidrLiteral, // string | { address, range } | [address, range]
611
+ Ipv4CidrString, // "${number}.${number}.${number}.${number}/${number}"
612
+
613
+ // IPv6 types
614
+ Ipv6AddressLiteral, // string | bigint | number[]
615
+ Ipv6AddressHextets, // [number, number, number, number, number, number, number, number]
616
+ Ipv6CidrLiteral, // string | { address, range } | [address, range]
617
+ } from 'cidr-block';
618
+ ```
619
+
620
+ ## Common Use Cases
621
+
622
+ ### Checking if an IP is in a Private Network
623
+
624
+ ```typescript
625
+ import { ipv4 } from 'cidr-block';
626
+
627
+ function isInternalIP(ip: string): boolean {
628
+ if (!ipv4.isValidAddress(ip)) return false;
629
+ const addr = ipv4.address(ip);
630
+ return addr.isPrivateAddress() || addr.isLoopbackAddress();
631
+ }
632
+
633
+ isInternalIP('192.168.1.1'); // true
634
+ isInternalIP('10.0.0.1'); // true
635
+ isInternalIP('8.8.8.8'); // false
636
+ ```
637
+
638
+ ### Allocating Subnets from a Pool
639
+
640
+ ```typescript
641
+ import { ipv4 } from 'cidr-block';
642
+
643
+ function allocateSubnets(poolCidr: string, subnetSize: number, count: number) {
644
+ const pool = ipv4.cidr(poolCidr);
645
+ const subnets = pool.subnet(subnetSize);
646
+ return subnets.slice(0, count);
647
+ }
648
+
649
+ const allocated = allocateSubnets('10.0.0.0/16', 24, 3);
650
+ allocated.forEach(s => console.log(s.toString()));
651
+ // 10.0.0.0/24
652
+ // 10.0.1.0/24
653
+ // 10.0.2.0/24
654
+ ```
655
+
656
+ ### Checking for CIDR Conflicts
657
+
658
+ ```typescript
659
+ import { ipv4, Ipv4Cidr } from 'cidr-block';
660
+
661
+ function findConflicts(newCidr: string, existing: string[]): string[] {
662
+ const cidr = ipv4.cidr(newCidr);
663
+ return existing.filter(e => cidr.overlaps(e));
664
+ }
665
+
666
+ const existingRanges = ['10.0.0.0/24', '10.0.1.0/24', '192.168.0.0/16'];
667
+ const conflicts = findConflicts('10.0.0.0/16', existingRanges);
668
+ // ['10.0.0.0/24', '10.0.1.0/24']
669
+ ```
670
+
671
+ ### Generating IP Addresses in a Range
672
+
673
+ ```typescript
674
+ import { ipv4 } from 'cidr-block';
675
+
676
+ function getUsableIPs(cidrStr: string): string[] {
677
+ const cidr = ipv4.cidr(cidrStr);
678
+ const ips: string[] = [];
679
+
680
+ for (const addr of cidr.addresses()) {
681
+ // Skip network and broadcast addresses for practical use
682
+ if (addr.equals(cidr.baseAddress())) continue;
683
+
684
+ const lastOctet = addr.octets()[3];
685
+ if (lastOctet === 255) continue; // Skip broadcast
686
+
687
+ ips.push(addr.toString());
688
+ }
689
+
690
+ return ips;
691
+ }
692
+
693
+ const usable = getUsableIPs('192.168.1.0/29');
694
+ // ['192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4', '192.168.1.5', '192.168.1.6']
695
+ ```