@se-oss/ip-address 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,857 @@
1
+ // src/constants.ts
2
+ var BITS = {
3
+ 4: 32n,
4
+ 6: 128n
5
+ };
6
+ var MAX_IP = {
7
+ 4: 0xffffffffn,
8
+ // (2n ** 32n) - 1n
9
+ 6: 0xffffffffffffffffffffffffffffffffn
10
+ // (2n ** 128n) - 1n
11
+ };
12
+ var IPV6_SEGMENT = "[0-9A-Fa-f]{1,4}";
13
+ var IPV4_PART = "(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])";
14
+ var IPV4_ADDRESS_PATTERN = `${IPV4_PART}\\.${IPV4_PART}\\.${IPV4_PART}\\.${IPV4_PART}`;
15
+ var IPV4_ADDRESS_REGEX = new RegExp(`^${IPV4_ADDRESS_PATTERN}$`);
16
+ var IPV6_STANDARD = `(?:${IPV6_SEGMENT}:){7}${IPV6_SEGMENT}`;
17
+ var IPV6_COMPRESSED_1 = `(?:${IPV6_SEGMENT}:){1,7}:`;
18
+ var IPV6_COMPRESSED_2 = `:(?::${IPV6_SEGMENT}){1,7}`;
19
+ var IPV6_COMPRESSED_3 = `(?:${IPV6_SEGMENT}:){1,6}:${IPV6_SEGMENT}`;
20
+ var IPV6_COMPRESSED_4 = `(?:${IPV6_SEGMENT}:){1,5}(?::${IPV6_SEGMENT}){1,2}`;
21
+ var IPV6_COMPRESSED_5 = `(?:${IPV6_SEGMENT}:){1,4}(?::${IPV6_SEGMENT}){1,3}`;
22
+ var IPV6_COMPRESSED_6 = `(?:${IPV6_SEGMENT}:){1,3}(?::${IPV6_SEGMENT}){1,4}`;
23
+ var IPV6_COMPRESSED_7 = `(?:${IPV6_SEGMENT}:){1,2}(?::${IPV6_SEGMENT}){1,5}`;
24
+ var IPV6_COMPRESSED_8 = `${IPV6_SEGMENT}:(?::${IPV6_SEGMENT}){1,6}`;
25
+ var IPV6_COMPRESSED_9 = `::(?:${IPV6_SEGMENT}:){0,5}${IPV6_SEGMENT}`;
26
+ var IPV6_COMPRESSED_10 = "::";
27
+ var IPV6_IPV4_MAPPED = `(?:(?:${IPV6_SEGMENT}:){6}|::(?:${IPV6_SEGMENT}:){0,5}|(?:${IPV6_SEGMENT}:){0,5}::(?:${IPV6_SEGMENT}:){0,5})${IPV4_ADDRESS_PATTERN}`;
28
+ var IPV6_COMBINED = [
29
+ IPV6_STANDARD,
30
+ IPV6_COMPRESSED_1,
31
+ IPV6_COMPRESSED_2,
32
+ IPV6_COMPRESSED_3,
33
+ IPV6_COMPRESSED_4,
34
+ IPV6_COMPRESSED_5,
35
+ IPV6_COMPRESSED_6,
36
+ IPV6_COMPRESSED_7,
37
+ IPV6_COMPRESSED_8,
38
+ IPV6_COMPRESSED_9,
39
+ IPV6_COMPRESSED_10,
40
+ IPV6_IPV4_MAPPED
41
+ ].map((s) => `(?:${s})`).join("|");
42
+ var IPV6_VALIDATION_REGEX = new RegExp(`^(?:${IPV6_COMBINED})$`);
43
+
44
+ // src/errors.ts
45
+ var InvalidIPAddressError = class extends Error {
46
+ constructor(message) {
47
+ super(message);
48
+ this.name = "InvalidIPAddressError";
49
+ }
50
+ };
51
+
52
+ // src/utils.ts
53
+ function fastIpVersion(probablyIp) {
54
+ if (probablyIp.includes(".")) {
55
+ return 4;
56
+ }
57
+ if (probablyIp.includes(":")) {
58
+ return 6;
59
+ }
60
+ throw new InvalidIPAddressError(`Invalid IP address: ${probablyIp}`);
61
+ }
62
+ function ipv4ToBigInt(ip) {
63
+ const parts = ip.split(".");
64
+ if (parts.length !== 4 || parts.some((part) => !/^\d+$/.test(part) || Number(part) > 255)) {
65
+ throw new InvalidIPAddressError(`Invalid IPv4 address: ${ip}`);
66
+ }
67
+ return parts.reduce((acc, octet) => acc << 8n | BigInt(parseInt(octet, 10)), 0n);
68
+ }
69
+ function expandIPv6Shorthand(parts) {
70
+ const index = parts.indexOf("");
71
+ if (index !== -1) {
72
+ const missingParts = 8 - parts.length + 1;
73
+ parts.splice(index, 1, ...Array(missingParts).fill(""));
74
+ }
75
+ return parts;
76
+ }
77
+ function ipv6ToBigInt(ip) {
78
+ if (ip.includes("%")) {
79
+ ip = /(.+)%(.+)/.exec(ip)[1];
80
+ }
81
+ const ipv4MappedMatch = /^(::ffff:)(?:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))$/.exec(ip);
82
+ if (ipv4MappedMatch) {
83
+ const ipv4Part = ipv4MappedMatch[2];
84
+ const ipv4BigInt = ipv4ToBigInt(ipv4Part);
85
+ return 0x00000000000000000000ffffn << 32n | ipv4BigInt;
86
+ }
87
+ let parts = ip.split(":");
88
+ parts = expandIPv6Shorthand(parts);
89
+ if (parts.length !== 8 || parts.some((part) => !/^[0-9a-fA-F]*$/.test(part) || part.length > 4)) {
90
+ throw new InvalidIPAddressError(`Invalid IPv6 address: ${ip}`);
91
+ }
92
+ let number = 0n;
93
+ let exp = 0n;
94
+ const r = parts.map((part) => part === "" ? 0n : BigInt(Number.parseInt(part, 16))).reverse();
95
+ for (const n of r) {
96
+ number += n * 2n ** exp;
97
+ exp += 16n;
98
+ }
99
+ return number;
100
+ }
101
+ function ipToBigInt(ip, version) {
102
+ const $version = version ?? fastIpVersion(ip);
103
+ if ($version === 4) {
104
+ return ipv4ToBigInt(ip);
105
+ }
106
+ return ipv6ToBigInt(ip);
107
+ }
108
+ function bigIntToIp(number, version, compress = true) {
109
+ if (version === 4) {
110
+ return `${number >> 24n & 0xffn}.${number >> 16n & 0xffn}.${number >> 8n & 0xffn}.${number & 0xffn}`;
111
+ }
112
+ if (number >> 32n === 0xffffn) {
113
+ const ipv4Part = number & 0xffffffffn;
114
+ const ipv4String = bigIntToIp(ipv4Part, 4);
115
+ return `::ffff:${ipv4String}`;
116
+ }
117
+ const parts = [];
118
+ for (let i = 0; i < 8; i++) {
119
+ parts.push(number >> BigInt(112 - i * 16) & 0xffffn);
120
+ }
121
+ const to16 = parts.map((n) => n.toString(16));
122
+ if (compress) {
123
+ return compressIPv6(to16);
124
+ }
125
+ return to16.map((part) => part.padStart(4, "0")).join(":");
126
+ }
127
+ function compressIPv6(parts) {
128
+ let longest = null;
129
+ let current = null;
130
+ for (let i = 0, len = parts.length; i < len; i++) {
131
+ const part = parts[i];
132
+ if (part === "0") {
133
+ current ??= /* @__PURE__ */ new Set();
134
+ current.add(i);
135
+ } else if (current) {
136
+ if (!longest || current.size > longest.size) {
137
+ longest = current;
138
+ }
139
+ current = null;
140
+ }
141
+ }
142
+ if (!longest && current || current && longest && current.size > longest.size) {
143
+ longest = current;
144
+ }
145
+ for (const index of longest || []) {
146
+ parts[index] = ":";
147
+ }
148
+ return parts.filter(Boolean).join(":").replace(/:{2,}/, "::");
149
+ }
150
+
151
+ // src/ipv6.ts
152
+ var IPv6 = class _IPv6 {
153
+ #address;
154
+ /**
155
+ * @param address An IPv6 address string (e.g., "2001:db8::1").
156
+ * @throws {InvalidIPAddressError} If the address is not a valid IPv6 address.
157
+ */
158
+ constructor(address) {
159
+ if (!_IPv6.isValid(address)) {
160
+ throw new InvalidIPAddressError(`Invalid IPv6 address: ${address}`);
161
+ }
162
+ this.#address = ipToBigInt(address, 6);
163
+ }
164
+ // --- Static Methods ---
165
+ /**
166
+ * Creates an IPv6 instance from a BigInt.
167
+ * @param value The BigInt representation of an IPv6 address.
168
+ * @returns A new IPv6 instance.
169
+ *
170
+ * @example
171
+ * const ip = IPv6.fromBigInt(1n);
172
+ * console.log(ip.address); // "::1"
173
+ */
174
+ static fromBigInt(value) {
175
+ const ip = bigIntToIp(value, 6);
176
+ return new _IPv6(ip);
177
+ }
178
+ /**
179
+ * Creates an IPv6 instance from an array of 16 bytes.
180
+ * @param bytes An array of 16 numbers (0-255).
181
+ * @returns A new IPv6 instance.
182
+ * @throws {InvalidIPAddressError} If the byte array is not 16 bytes long.
183
+ *
184
+ * @example
185
+ * const bytes = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1];
186
+ * const ip = IPv6.fromBytes(bytes);
187
+ * console.log(ip.address); // "::1"
188
+ */
189
+ static fromBytes(bytes) {
190
+ if (bytes.length !== 16) {
191
+ throw new InvalidIPAddressError("IPv6 addresses must be 16 bytes long");
192
+ }
193
+ const value = bytes.reduce((acc, octet) => acc << 8n | BigInt(octet), 0n);
194
+ return _IPv6.fromBigInt(value);
195
+ }
196
+ /**
197
+ * Checks if a string is a valid IPv6 address.
198
+ * @param address The string to validate.
199
+ * @returns True if the string is a valid IPv6 address.
200
+ *
201
+ * @example
202
+ * IPv6.isValid('2001:db8::1'); // true
203
+ * IPv6.isValid('2001:db8::g'); // false
204
+ */
205
+ static isValid(address) {
206
+ if (address.includes("/")) {
207
+ return false;
208
+ }
209
+ return IPV6_VALIDATION_REGEX.test(address);
210
+ }
211
+ // --- Getters ---
212
+ /**
213
+ * The IP version number.
214
+ * @returns {6}
215
+ */
216
+ get version() {
217
+ return 6;
218
+ }
219
+ /**
220
+ * The compressed, canonical IPv6 address string.
221
+ * @returns {string}
222
+ */
223
+ get address() {
224
+ return bigIntToIp(this.#address, 6, true);
225
+ }
226
+ /**
227
+ * The fully expanded IPv6 address string.
228
+ * @returns {string}
229
+ */
230
+ get expandedAddress() {
231
+ return bigIntToIp(this.#address, 6, false);
232
+ }
233
+ // --- Conversion Methods ---
234
+ /**
235
+ * Converts the IP address to its BigInt representation.
236
+ * @returns {bigint}
237
+ */
238
+ toBigInt() {
239
+ return this.#address;
240
+ }
241
+ /**
242
+ * Converts the IP address to an array of 16 bytes.
243
+ * @returns {number[]}
244
+ */
245
+ toBytes() {
246
+ const bytes = [];
247
+ for (let i = 0; i < 16; i++) {
248
+ bytes.push(Number(this.#address >> BigInt(120 - i * 8) & 0xffn));
249
+ }
250
+ return bytes;
251
+ }
252
+ /**
253
+ * Returns the string representation of the IP address.
254
+ * @returns {string}
255
+ */
256
+ toString() {
257
+ return this.address;
258
+ }
259
+ /**
260
+ * Returns the string representation for JSON serialization.
261
+ * @returns {string}
262
+ */
263
+ toJSON() {
264
+ return this.address;
265
+ }
266
+ /**
267
+ * Converts an IPv4-mapped address to an IPv4 instance, otherwise returns null.
268
+ * @returns {IPv4 | null}
269
+ */
270
+ toIPv4() {
271
+ if (!this.isIPv4Mapped()) {
272
+ return null;
273
+ }
274
+ const ipv4Address = this.#address & 0xffffffffn;
275
+ return IPv4.fromBigInt(ipv4Address);
276
+ }
277
+ /**
278
+ * Returns the reverse DNS (ARPA) name for the IP address.
279
+ * @returns {string}
280
+ *
281
+ * @example
282
+ * const ip = new IPv6('2001:db8::1');
283
+ * console.log(ip.toArpa()); // "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa"
284
+ */
285
+ toArpa() {
286
+ const hex = this.expandedAddress.replace(/:/g, "");
287
+ return `${hex.split("").reverse().join(".")}.ip6.arpa`;
288
+ }
289
+ // --- Classification Methods ---
290
+ /**
291
+ * Checks if the address is the unspecified address (::).
292
+ * @returns {boolean}
293
+ */
294
+ isUnspecified() {
295
+ return this.#address === 0n;
296
+ }
297
+ /**
298
+ * Checks if the address is the loopback address (::1).
299
+ * @returns {boolean}
300
+ */
301
+ isLoopback() {
302
+ return this.#address === 1n;
303
+ }
304
+ /**
305
+ * Checks if the address is a multicast address (ff00::/8).
306
+ * @returns {boolean}
307
+ */
308
+ isMulticast() {
309
+ return this.#address >> 120n === 0xffn;
310
+ }
311
+ /**
312
+ * Checks if the address is a link-local address (fe80::/10).
313
+ * @returns {boolean}
314
+ */
315
+ isLinkLocal() {
316
+ return this.#address >> 118n === 0x3fan;
317
+ }
318
+ /**
319
+ * Checks if the address is a unique local address (fc00::/7).
320
+ * @returns {boolean}
321
+ */
322
+ isUniqueLocal() {
323
+ return this.#address >> 121n === 0x7en;
324
+ }
325
+ /**
326
+ * Checks if the address is an IPv4-mapped address (::ffff:0:0/96).
327
+ * @returns {boolean}
328
+ */
329
+ isIPv4Mapped() {
330
+ return this.#address >> 32n === 0xffffn;
331
+ }
332
+ /**
333
+ * Checks if the address is reserved by IANA. This includes ranges for
334
+ * documentation, benchmarking, and other special purposes.
335
+ * @returns {boolean}
336
+ */
337
+ isReserved() {
338
+ const addr = this.#address;
339
+ if (addr >> 64n === 0x100000000000000n) return true;
340
+ if (addr >> 80n === 0x200100020000n) return true;
341
+ if (addr >> 96n === 0x20010db8n) return true;
342
+ if (addr >> 96n === 0x20010000n) return true;
343
+ if (addr >> 112n === 0x2002n) return true;
344
+ return false;
345
+ }
346
+ /**
347
+ * Checks if the address is a global unicast address.
348
+ * @returns {boolean}
349
+ */
350
+ isGlobalUnicast() {
351
+ const isGlobalRange = this.#address >> 125n === 0x1n;
352
+ return isGlobalRange && !this.isUnspecified() && !this.isLoopback() && !this.isMulticast() && !this.isLinkLocal() && !this.isUniqueLocal() && !this.isIPv4Mapped() && !this.isReserved();
353
+ }
354
+ // --- Operational Methods ---
355
+ /**
356
+ * Checks for exact equality between this and another IPv6 address.
357
+ * @param other The other IPv6 address to compare with.
358
+ * @returns {boolean}
359
+ */
360
+ equals(other) {
361
+ const otherIp = typeof other === "string" ? new _IPv6(other) : other;
362
+ return this.#address === otherIp.#address;
363
+ }
364
+ /**
365
+ * Compares this IP address with another.
366
+ * @param other The other IPv6 address to compare with.
367
+ * @returns `-1` if this address is less than the other, `0` if they are equal, `1` if it is greater.
368
+ */
369
+ compare(other) {
370
+ const otherIp = typeof other === "string" ? new _IPv6(other) : other;
371
+ if (this.#address < otherIp.#address) return -1;
372
+ if (this.#address > otherIp.#address) return 1;
373
+ return 0;
374
+ }
375
+ /**
376
+ * Performs a bitwise AND operation with another IPv6 address.
377
+ * @param other The other IPv6 address.
378
+ * @returns A new IPv6 instance with the result of the AND operation.
379
+ */
380
+ and(other) {
381
+ const otherIp = typeof other === "string" ? new _IPv6(other) : other;
382
+ return _IPv6.fromBigInt(this.#address & otherIp.#address);
383
+ }
384
+ /**
385
+ * Performs a bitwise OR operation with another IPv6 address.
386
+ * @param other The other IPv6 address.
387
+ * @returns A new IPv6 instance with the result of the OR operation.
388
+ */
389
+ or(other) {
390
+ const otherIp = typeof other === "string" ? new _IPv6(other) : other;
391
+ return _IPv6.fromBigInt(this.#address | otherIp.#address);
392
+ }
393
+ /**
394
+ * Performs a bitwise NOT operation on the address.
395
+ * @returns A new IPv6 instance with the result of the NOT operation.
396
+ */
397
+ not() {
398
+ return _IPv6.fromBigInt(~this.#address & MAX_IP[6]);
399
+ }
400
+ /**
401
+ * Returns the next IP address.
402
+ * @param [count=1n] The number of addresses to increment by.
403
+ * @returns {IPv6}
404
+ */
405
+ next(count = 1n) {
406
+ return _IPv6.fromBigInt(this.#address + count & MAX_IP[6]);
407
+ }
408
+ /**
409
+ * Returns the previous IP address.
410
+ * @param [count=1n] The number of addresses to decrement by.
411
+ * @returns {IPv6}
412
+ */
413
+ previous(count = 1n) {
414
+ return _IPv6.fromBigInt(this.#address - count & MAX_IP[6]);
415
+ }
416
+ };
417
+
418
+ // src/ipv4.ts
419
+ var IPv4 = class _IPv4 {
420
+ #address;
421
+ /**
422
+ * @param address An IPv4 address string (e.g., "192.168.1.1").
423
+ * @throws {InvalidIPAddressError} If the address is not a valid IPv4 address.
424
+ */
425
+ constructor(address) {
426
+ if (!_IPv4.isValid(address)) {
427
+ throw new InvalidIPAddressError(`Invalid IPv4 address: ${address}`);
428
+ }
429
+ this.#address = ipToBigInt(address, 4);
430
+ }
431
+ /**
432
+ * Creates an IPv4 instance from a BigInt.
433
+ * @param value The BigInt representation of an IPv4 address.
434
+ * @returns A new IPv4 instance.
435
+ *
436
+ * @example
437
+ * const ip = IPv4.fromBigInt(3232235777n);
438
+ * console.log(ip.address); // "192.168.1.1"
439
+ */
440
+ static fromBigInt(value) {
441
+ const ip = bigIntToIp(value, 4);
442
+ return new _IPv4(ip);
443
+ }
444
+ /**
445
+ * Creates an IPv4 instance from an array of 4 bytes.
446
+ * @param bytes An array of 4 numbers (0-255).
447
+ * @returns A new IPv4 instance.
448
+ *
449
+ * @example
450
+ * const ip = IPv4.fromBytes([192, 168, 1, 1]);
451
+ * console.log(ip.address); // "192.168.1.1"
452
+ */
453
+ static fromBytes(bytes) {
454
+ const value = bytes.reduce((acc, octet) => acc << 8n | BigInt(octet), 0n);
455
+ return _IPv4.fromBigInt(value);
456
+ }
457
+ /**
458
+ * Checks if a string is a valid IPv4 address.
459
+ * @param address The string to validate.
460
+ * @returns True if the string is a valid IPv4 address.
461
+ *
462
+ * @example
463
+ * IPv4.isValid('1.2.3.4'); // true
464
+ * IPv4.isValid('1.2.3.256'); // false
465
+ */
466
+ static isValid(address) {
467
+ if (address.includes("/")) {
468
+ return false;
469
+ }
470
+ return IPV4_ADDRESS_REGEX.test(address);
471
+ }
472
+ // --- Getters ---
473
+ /**
474
+ * The IP version number.
475
+ * @returns {4}
476
+ */
477
+ get version() {
478
+ return 4;
479
+ }
480
+ /**
481
+ * The IP address as a string.
482
+ * @returns {string}
483
+ */
484
+ get address() {
485
+ return bigIntToIp(this.#address, 4);
486
+ }
487
+ // --- Conversion Methods ---
488
+ /**
489
+ * Converts the IP address to its BigInt representation.
490
+ * @returns {bigint}
491
+ */
492
+ toBigInt() {
493
+ return this.#address;
494
+ }
495
+ /**
496
+ * Converts the IP address to an array of 4 bytes.
497
+ * @returns {[number, number, number, number]}
498
+ */
499
+ toBytes() {
500
+ const address = this.#address;
501
+ return [
502
+ Number(address >> 24n & 0xffn),
503
+ Number(address >> 16n & 0xffn),
504
+ Number(address >> 8n & 0xffn),
505
+ Number(address & 0xffn)
506
+ ];
507
+ }
508
+ /**
509
+ * Returns the string representation of the IP address.
510
+ * @returns {string}
511
+ */
512
+ toString() {
513
+ return this.address;
514
+ }
515
+ /**
516
+ * Returns the string representation for JSON serialization.
517
+ * @returns {string}
518
+ */
519
+ toJSON() {
520
+ return this.address;
521
+ }
522
+ /**
523
+ * Converts the IPv4 address to an IPv4-mapped IPv6 address.
524
+ * @returns {IPv6} An IPv6 instance representing the mapped address.
525
+ *
526
+ * @example
527
+ * const ipv4 = new IPv4('192.0.2.1');
528
+ * const ipv6 = ipv4.toIPv4Mapped();
529
+ * console.log(ipv6.address); // "::ffff:192.0.2.1"
530
+ */
531
+ toIPv4Mapped() {
532
+ const mappedBigInt = 0xffffn << 32n | this.#address;
533
+ return IPv6.fromBigInt(mappedBigInt);
534
+ }
535
+ /**
536
+ * Returns the reverse DNS (ARPA) name for the IP address.
537
+ * @returns {string}
538
+ *
539
+ * @example
540
+ * const ip = new IPv4('192.168.1.1');
541
+ * console.log(ip.toArpa()); // "1.1.168.192.in-addr.arpa"
542
+ */
543
+ toArpa() {
544
+ const parts = this.address.split(".").reverse().join(".");
545
+ return `${parts}.in-addr.arpa`;
546
+ }
547
+ // --- Classification Methods ---
548
+ /**
549
+ * Checks if the address is in a private range (RFC 1918).
550
+ * @returns {boolean}
551
+ */
552
+ isPrivate() {
553
+ const addr = this.#address;
554
+ if (addr >= 167772160n && addr <= 184549375n) return true;
555
+ if (addr >= 2886729728n && addr <= 2887778303n) return true;
556
+ if (addr >= 3232235520n && addr <= 3232301055n) return true;
557
+ return false;
558
+ }
559
+ /**
560
+ * Checks if the address is a loopback address (127.0.0.0/8).
561
+ * @returns {boolean}
562
+ */
563
+ isLoopback() {
564
+ return this.#address >= 2130706432n && this.#address <= 2147483647n;
565
+ }
566
+ /**
567
+ * Checks if the address is a multicast address (224.0.0.0/4).
568
+ * @returns {boolean}
569
+ */
570
+ isMulticast() {
571
+ return this.#address >= 3758096384n && this.#address <= 4026531839n;
572
+ }
573
+ /**
574
+ * Checks if the address is a link-local address (169.254.0.0/16).
575
+ * @returns {boolean}
576
+ */
577
+ isLinkLocal() {
578
+ return this.#address >= 2851995648n && this.#address <= 2852061183n;
579
+ }
580
+ /**
581
+ * Checks if the address is the unspecified address (0.0.0.0).
582
+ * @returns {boolean}
583
+ */
584
+ isUnspecified() {
585
+ return this.#address === 0n;
586
+ }
587
+ /**
588
+ * Checks if the address is the limited broadcast address (255.255.255.255).
589
+ * Note: This does not check for subnet-directed broadcasts.
590
+ * @returns {boolean}
591
+ */
592
+ isBroadcast() {
593
+ return this.#address === 0xffffffffn;
594
+ }
595
+ /**
596
+ * Checks if the address is reserved by IANA. This includes ranges for
597
+ * private use, loopback, multicast, and other special purposes.
598
+ * @returns {boolean}
599
+ */
600
+ isReserved() {
601
+ const addr = this.#address;
602
+ if (addr >= 0n && addr <= 16777215n) return true;
603
+ if (addr >= 1681915904n && addr <= 1686110207n) return true;
604
+ if (addr >= 3221225472n && addr <= 3221225727n) return true;
605
+ if (addr >= 3221225984n && addr <= 3221226239n) return true;
606
+ if (addr >= 3227017984n && addr <= 3227018239n) return true;
607
+ if (addr >= 3323068416n && addr <= 3323199487n) return true;
608
+ if (addr >= 3325256704n && addr <= 3325256959n) return true;
609
+ if (addr >= 3405803776n && addr <= 3405804031n) return true;
610
+ if (addr >= 4026531840n && addr <= 4294967295n) return true;
611
+ return this.isPrivate() || this.isLoopback() || this.isLinkLocal() || this.isMulticast();
612
+ }
613
+ /**
614
+ * Checks if the address is a global unicast address (i.e., a public IP).
615
+ * @returns {boolean}
616
+ */
617
+ isGlobalUnicast() {
618
+ return !this.isUnspecified() && !this.isBroadcast() && !this.isReserved();
619
+ }
620
+ // --- Operational Methods ---
621
+ /**
622
+ * Checks for exact equality between this and another IPv4 address.
623
+ * @param other The other IPv4 address to compare with.
624
+ * @returns {boolean}
625
+ */
626
+ equals(other) {
627
+ const otherIp = typeof other === "string" ? new _IPv4(other) : other;
628
+ return this.#address === otherIp.#address;
629
+ }
630
+ /**
631
+ * Compares this IP address with another.
632
+ * @param other The other IPv4 address to compare with.
633
+ * @returns `-1` if this address is less than the other, `0` if they are equal, `1` if it is greater.
634
+ */
635
+ compare(other) {
636
+ const otherIp = typeof other === "string" ? new _IPv4(other) : other;
637
+ if (this.#address < otherIp.#address) return -1;
638
+ if (this.#address > otherIp.#address) return 1;
639
+ return 0;
640
+ }
641
+ /**
642
+ * Performs a bitwise AND operation with another IPv4 address.
643
+ * @param other The other IPv4 address.
644
+ * @returns A new IPv4 instance with the result of the AND operation.
645
+ */
646
+ and(other) {
647
+ const otherIp = typeof other === "string" ? new _IPv4(other) : other;
648
+ return _IPv4.fromBigInt(this.#address & otherIp.#address);
649
+ }
650
+ /**
651
+ * Performs a bitwise OR operation with another IPv4 address.
652
+ * @param other The other IPv4 address.
653
+ * @returns A new IPv4 instance with the result of the OR operation.
654
+ */
655
+ or(other) {
656
+ const otherIp = typeof other === "string" ? new _IPv4(other) : other;
657
+ return _IPv4.fromBigInt(this.#address | otherIp.#address);
658
+ }
659
+ /**
660
+ * Performs a bitwise NOT operation on the address.
661
+ * @returns A new IPv4 instance with the result of the NOT operation.
662
+ */
663
+ not() {
664
+ return _IPv4.fromBigInt(~this.#address & MAX_IP[4]);
665
+ }
666
+ /**
667
+ * Returns the next IP address.
668
+ * @param [count=1n] The number of addresses to increment by.
669
+ * @returns {IPv4}
670
+ */
671
+ next(count = 1n) {
672
+ return _IPv4.fromBigInt(this.#address + count & MAX_IP[4]);
673
+ }
674
+ /**
675
+ * Returns the previous IP address.
676
+ * @param [count=1n] The number of addresses to decrement by.
677
+ * @returns {IPv4}
678
+ */
679
+ previous(count = 1n) {
680
+ return _IPv4.fromBigInt(this.#address - count & MAX_IP[4]);
681
+ }
682
+ };
683
+
684
+ // src/cidr.ts
685
+ var CIDR = class {
686
+ #address;
687
+ #prefix;
688
+ /**
689
+ * @param cidr A CIDR string (e.g., "192.168.1.0/24" or "2001:db8::/32").
690
+ * @throws {InvalidIPAddressError} If the CIDR string is invalid.
691
+ */
692
+ constructor(cidr) {
693
+ const [addressPart, prefixPart] = cidr.split("/");
694
+ if (!prefixPart) {
695
+ throw new InvalidIPAddressError(`Invalid CIDR notation: ${cidr}`);
696
+ }
697
+ const prefix = parseInt(prefixPart, 10);
698
+ if (isNaN(prefix)) {
699
+ throw new InvalidIPAddressError(`Invalid prefix: ${prefixPart}`);
700
+ }
701
+ let address;
702
+ let version;
703
+ if (addressPart.includes(".")) {
704
+ address = new IPv4(addressPart);
705
+ version = 4;
706
+ } else {
707
+ address = new IPv6(addressPart);
708
+ version = 6;
709
+ }
710
+ if (prefix < 0 || prefix > BITS[version]) {
711
+ throw new InvalidIPAddressError(`Prefix out of range for IPv${version}: ${prefix}`);
712
+ }
713
+ this.#address = address;
714
+ this.#prefix = prefix;
715
+ }
716
+ /**
717
+ * The IP address portion of the CIDR.
718
+ * @returns {T}
719
+ */
720
+ get address() {
721
+ return this.#address;
722
+ }
723
+ /**
724
+ * The prefix length of the CIDR.
725
+ * @returns {number}
726
+ */
727
+ get prefix() {
728
+ return this.#prefix;
729
+ }
730
+ /**
731
+ * The IP version.
732
+ * @returns {IPVersion}
733
+ */
734
+ get version() {
735
+ return this.#address.version;
736
+ }
737
+ /**
738
+ * The netmask for the CIDR block.
739
+ * @returns {T}
740
+ */
741
+ get netmask() {
742
+ const version = this.version;
743
+ const mask = MAX_IP[version] << BITS[version] - BigInt(this.prefix) & MAX_IP[version];
744
+ if (version === 4) {
745
+ return IPv4.fromBigInt(mask);
746
+ }
747
+ return IPv6.fromBigInt(mask);
748
+ }
749
+ /**
750
+ * The network address of the CIDR block.
751
+ * @returns {T}
752
+ */
753
+ get network() {
754
+ const networkAddress = this.#address.toBigInt() & this.netmask.toBigInt();
755
+ if (this.version === 4) {
756
+ return IPv4.fromBigInt(networkAddress);
757
+ }
758
+ return IPv6.fromBigInt(networkAddress);
759
+ }
760
+ /**
761
+ * The broadcast address for the CIDR block.
762
+ * @returns {T}
763
+ */
764
+ get broadcast() {
765
+ const networkAddress = this.network.toBigInt();
766
+ const hostmask = MAX_IP[this.version] >> BigInt(this.prefix);
767
+ const broadcastAddress = networkAddress | hostmask;
768
+ if (this.version === 4) {
769
+ return IPv4.fromBigInt(broadcastAddress);
770
+ }
771
+ return IPv6.fromBigInt(broadcastAddress);
772
+ }
773
+ /**
774
+ * The first usable IP address in the CIDR block.
775
+ * For IPv4 /31 and /32, this is the network address.
776
+ * For IPv6, this is the network address (Subnet-Router anycast).
777
+ * @returns {T}
778
+ */
779
+ get first() {
780
+ if (this.version === 4) {
781
+ if (this.prefix >= 31) {
782
+ return this.network;
783
+ }
784
+ return this.network.next();
785
+ }
786
+ return this.network;
787
+ }
788
+ /**
789
+ * The last usable IP address in the CIDR block.
790
+ * For IPv4 /31 and /32, this is the broadcast address.
791
+ * For IPv6, this is the highest address in the range.
792
+ * @returns {T}
793
+ */
794
+ get last() {
795
+ if (this.version === 4) {
796
+ if (this.prefix >= 31) {
797
+ return this.broadcast;
798
+ }
799
+ return this.broadcast.previous();
800
+ }
801
+ return this.broadcast;
802
+ }
803
+ /**
804
+ * Checks if a given IP address is contained within this CIDR block.
805
+ * @param ip The IP address to check.
806
+ * @returns {boolean}
807
+ */
808
+ contains(ip) {
809
+ const ipAddress = typeof ip === "string" ? this.version === 4 ? new IPv4(ip) : new IPv6(ip) : ip;
810
+ if (ipAddress.version !== this.version) {
811
+ return false;
812
+ }
813
+ const networkAddress = this.network.toBigInt();
814
+ const broadcastAddress = this.broadcast.toBigInt();
815
+ const targetAddress = ipAddress.toBigInt();
816
+ return targetAddress >= networkAddress && targetAddress <= broadcastAddress;
817
+ }
818
+ /**
819
+ * Returns the string representation of the CIDR.
820
+ * @returns {string}
821
+ */
822
+ toString() {
823
+ return `${this.address.address}/${this.prefix}`;
824
+ }
825
+ /**
826
+ * Returns the string representation for JSON serialization.
827
+ * @returns {string}
828
+ */
829
+ toJSON() {
830
+ return this.toString();
831
+ }
832
+ };
833
+
834
+ // src/index.ts
835
+ function parseIP(address) {
836
+ if (address.includes(".")) {
837
+ return new IPv4(address);
838
+ }
839
+ if (address.includes(":")) {
840
+ return new IPv6(address);
841
+ }
842
+ throw new InvalidIPAddressError(`Invalid IP address: ${address}`);
843
+ }
844
+ function parseCIDR(cidr) {
845
+ return new CIDR(cidr);
846
+ }
847
+ export {
848
+ CIDR,
849
+ IPv4,
850
+ IPv6,
851
+ InvalidIPAddressError,
852
+ bigIntToIp,
853
+ fastIpVersion,
854
+ ipToBigInt,
855
+ parseCIDR,
856
+ parseIP
857
+ };