@bigbinary/neeto-molecules 3.2.2 → 3.2.4

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 (65) hide show
  1. package/dist/BrowserSupport.js +2 -1
  2. package/dist/BrowserSupport.js.map +1 -1
  3. package/dist/Builder.js +3 -3
  4. package/dist/Builder.js.map +1 -1
  5. package/dist/DownloadMobileAppCallout.js +2 -1
  6. package/dist/DownloadMobileAppCallout.js.map +1 -1
  7. package/dist/EmailForm.js +2 -1
  8. package/dist/EmailForm.js.map +1 -1
  9. package/dist/FloatingActionMenu.js +2 -1
  10. package/dist/FloatingActionMenu.js.map +1 -1
  11. package/dist/IconPicker.js +916 -4
  12. package/dist/IconPicker.js.map +1 -1
  13. package/dist/IpRestriction.js +3093 -7
  14. package/dist/IpRestriction.js.map +1 -1
  15. package/dist/KeyboardShortcuts.js +2 -1
  16. package/dist/KeyboardShortcuts.js.map +1 -1
  17. package/dist/MenuBar.js +1804 -12
  18. package/dist/MenuBar.js.map +1 -1
  19. package/dist/NeetoWidget.js +1957 -3
  20. package/dist/NeetoWidget.js.map +1 -1
  21. package/dist/PhoneNumber.js +12 -4
  22. package/dist/PhoneNumber.js.map +1 -1
  23. package/dist/SendToFields.js +1919 -1
  24. package/dist/SendToFields.js.map +1 -1
  25. package/dist/ShareViaEmail.js +2 -1
  26. package/dist/ShareViaEmail.js.map +1 -1
  27. package/dist/ShareViaLink.js +860 -2
  28. package/dist/ShareViaLink.js.map +1 -1
  29. package/dist/_commonjsHelpers-1c8beb5f.js +4 -0
  30. package/dist/_commonjsHelpers-1c8beb5f.js.map +1 -0
  31. package/dist/cjs/BrowserSupport.js +3 -3
  32. package/dist/cjs/BrowserSupport.js.map +1 -1
  33. package/dist/cjs/Builder.js +3 -3
  34. package/dist/cjs/Builder.js.map +1 -1
  35. package/dist/cjs/DownloadMobileAppCallout.js +3 -3
  36. package/dist/cjs/DownloadMobileAppCallout.js.map +1 -1
  37. package/dist/cjs/EmailForm.js +2 -1
  38. package/dist/cjs/EmailForm.js.map +1 -1
  39. package/dist/cjs/FloatingActionMenu.js +2 -1
  40. package/dist/cjs/FloatingActionMenu.js.map +1 -1
  41. package/dist/cjs/IconPicker.js +920 -5
  42. package/dist/cjs/IconPicker.js.map +1 -1
  43. package/dist/cjs/IpRestriction.js +3101 -15
  44. package/dist/cjs/IpRestriction.js.map +1 -1
  45. package/dist/cjs/KeyboardShortcuts.js +3 -3
  46. package/dist/cjs/KeyboardShortcuts.js.map +1 -1
  47. package/dist/cjs/MenuBar.js +1807 -10
  48. package/dist/cjs/MenuBar.js.map +1 -1
  49. package/dist/cjs/NeetoWidget.js +1958 -5
  50. package/dist/cjs/NeetoWidget.js.map +1 -1
  51. package/dist/cjs/PhoneNumber.js +12 -4
  52. package/dist/cjs/PhoneNumber.js.map +1 -1
  53. package/dist/cjs/SendToFields.js +1921 -3
  54. package/dist/cjs/SendToFields.js.map +1 -1
  55. package/dist/cjs/ShareViaEmail.js +2 -1
  56. package/dist/cjs/ShareViaEmail.js.map +1 -1
  57. package/dist/cjs/ShareViaLink.js +860 -3
  58. package/dist/cjs/ShareViaLink.js.map +1 -1
  59. package/dist/cjs/_commonjsHelpers-68cdf74f.js +6 -0
  60. package/dist/cjs/_commonjsHelpers-68cdf74f.js.map +1 -0
  61. package/dist/cjs/platform-628b3bd8.js +1252 -0
  62. package/dist/cjs/platform-628b3bd8.js.map +1 -0
  63. package/dist/platform-e221afea.js +1250 -0
  64. package/dist/platform-e221afea.js.map +1 -0
  65. package/package.json +14 -16
@@ -18,7 +18,7 @@ import Input from '@bigbinary/neetoui/formik/Input';
18
18
  import Select from '@bigbinary/neetoui/formik/Select';
19
19
  import { t } from 'i18next';
20
20
  import * as yup from 'yup';
21
- import { Validator, IPv4, IPv6 } from 'ip-num';
21
+ import { c as commonjsGlobal } from './_commonjsHelpers-1c8beb5f.js';
22
22
  import { isNil, isNotNil, assoc } from 'ramda';
23
23
  import { useQuery } from '@tanstack/react-query';
24
24
  import axios from 'axios';
@@ -28,6 +28,3092 @@ import '@babel/runtime/helpers/extends';
28
28
  import '@babel/runtime/helpers/objectWithoutProperties';
29
29
  import './inject-css-c86de496.js';
30
30
 
31
+ var ipNum = {};
32
+
33
+ var BinaryUtils = {};
34
+
35
+ (function (exports) {
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ exports.matchingBitCount = exports.intLog2 = exports.cidrPrefixToMaskBinaryString = exports.leftPadWithZeroBit = exports.dottedDecimalNotationToBinaryString = exports.parseBinaryStringToBigInt = exports.decimalNumberToOctetString = exports.numberToBinaryString = void 0;
38
+ /**
39
+ * Converts a decimal number to binary string
40
+ *
41
+ * @param num number to parse
42
+ * @returns {string} the binary string representation of number
43
+ */
44
+ exports.numberToBinaryString = (num) => {
45
+ return num.toString(2);
46
+ };
47
+ /**
48
+ * Converts a decimal number to binary octet (8 bit) string. If needed the octet will be padded with zeros
49
+ * to make it up to 8 bits
50
+ *
51
+ * @param {number} num to convert to octet string
52
+ * @returns {string} the octet string representation of given number
53
+ */
54
+ exports.decimalNumberToOctetString = (num) => {
55
+ let binaryString = exports.numberToBinaryString(num);
56
+ let length = binaryString.length;
57
+ if (length > 8) {
58
+ throw new Error("Given decimal in binary contains digits greater than an octet");
59
+ }
60
+ return exports.leftPadWithZeroBit(binaryString, 8);
61
+ };
62
+ /**
63
+ * Parses number in binary to number in BigInt
64
+ *
65
+ * @param num binary number in string to parse
66
+ * @returns {number} binary number in BigInt
67
+ */
68
+ exports.parseBinaryStringToBigInt = (num) => {
69
+ return BigInt(`0b${num}`);
70
+ };
71
+ /**
72
+ * Given an IPv4 number in dot-decimal notated string, e.g 192.168.0.1 converts it to
73
+ * binary string, e.g. '11000000101010000000000000000001'
74
+ *
75
+ * @param dottedDecimal IPv4 string in dot-decimal notation
76
+ * @returns {string} the binary value of the given ipv4 number in string
77
+ */
78
+ exports.dottedDecimalNotationToBinaryString = (dottedDecimal) => {
79
+ let stringOctets = dottedDecimal.split(".");
80
+ return stringOctets.reduce((binaryAsString, octet) => {
81
+ return binaryAsString.concat(exports.decimalNumberToOctetString(parseInt(octet)));
82
+ }, '');
83
+ };
84
+ /**
85
+ * Given a binary string, adds a number of zero to the left until string is as long as the given string length
86
+ * @param {string} binaryString the string to pad
87
+ * @param {number} finalStringLength the final length of string after padding
88
+ * @returns {string}
89
+ */
90
+ exports.leftPadWithZeroBit = (binaryString, finalStringLength) => {
91
+ if (binaryString.length > finalStringLength) {
92
+ throw new Error(`Given string is already longer than given final length after padding: ${finalStringLength}`);
93
+ }
94
+ return "0".repeat(finalStringLength - binaryString.length).concat(binaryString);
95
+ };
96
+ /**
97
+ * Given the prefix portion of a cidr notation and the type of IP number, returns the mask in binary string
98
+ *
99
+ * @param {number} cidrPrefix the prefix part of a cidr notation
100
+ * @param {IPNumType.IPv4 | IPNumType.IPv6} ipType the type of the ip number in the range the cidr represents
101
+ */
102
+ exports.cidrPrefixToMaskBinaryString = (cidrPrefix, ipType) => {
103
+ let cidrUpperValue;
104
+ if (ipType == "IPv4" /* IPv4 */) {
105
+ cidrUpperValue = 32;
106
+ }
107
+ else {
108
+ cidrUpperValue = 128;
109
+ }
110
+ if (cidrPrefix > cidrUpperValue)
111
+ throw Error(`Value is greater than ${cidrUpperValue}`);
112
+ let onBits = '1'.repeat(cidrPrefix);
113
+ let offBits = '0'.repeat(cidrUpperValue - cidrPrefix);
114
+ return `${onBits}${offBits}`;
115
+ };
116
+ /**
117
+ * Calculates the log, to base 2 of given number.
118
+ *
119
+ * @throws Error if number cannot be converted to log base 2
120
+ * @param givenNumber the number to calculate log base 2
121
+ * @return the log base 2 of given number
122
+ */
123
+ exports.intLog2 = (givenNumber) => {
124
+ let result = 0;
125
+ while (givenNumber % 2n === 0n) {
126
+ if (givenNumber === 2n) {
127
+ result++;
128
+ break;
129
+ }
130
+ givenNumber = givenNumber >> 1n;
131
+ if (givenNumber % 2n !== 0n) {
132
+ result = 0;
133
+ break;
134
+ }
135
+ result++;
136
+ }
137
+ if (result == 0) {
138
+ throw new Error(`The value of log2 for ${givenNumber.toString()} is not an integer`);
139
+ }
140
+ return result;
141
+ };
142
+ /**
143
+ * Starting from the most significant bit (from left) returns the number of first bits from both string that are equal
144
+ * @param firstBinaryString first binary string
145
+ * @param secondBinaryString second binary string
146
+ */
147
+ exports.matchingBitCount = (firstBinaryString, secondBinaryString) => {
148
+ let longerString;
149
+ let otherString;
150
+ if (firstBinaryString.length >= secondBinaryString.length) {
151
+ longerString = firstBinaryString;
152
+ otherString = secondBinaryString;
153
+ }
154
+ else {
155
+ longerString = secondBinaryString;
156
+ otherString = firstBinaryString;
157
+ }
158
+ let count = 0;
159
+ for (; count < longerString.length; count++) {
160
+ if (longerString.charAt(count) === otherString.charAt(count)) {
161
+ continue;
162
+ }
163
+ break;
164
+ }
165
+ return count;
166
+ };
167
+
168
+ } (BinaryUtils));
169
+
170
+ var Hexadecatet$1 = {};
171
+
172
+ var Validator = {};
173
+
174
+ var IPv6Utils = {};
175
+
176
+ var hasRequiredIPv6Utils;
177
+
178
+ function requireIPv6Utils () {
179
+ if (hasRequiredIPv6Utils) return IPv6Utils;
180
+ hasRequiredIPv6Utils = 1;
181
+ Object.defineProperty(IPv6Utils, "__esModule", { value: true });
182
+ IPv6Utils.collapseIPv6Number = IPv6Utils.expandIPv6Number = void 0;
183
+ const BinaryUtils_1 = BinaryUtils;
184
+ const Validator_1 = requireValidator();
185
+ let extractPrefix = (ipv6String) => {
186
+ return ipv6String.includes("/") ? `/${ipv6String.split("/")[1]}` : "";
187
+ };
188
+ /**
189
+ * Expands an IPv6 number in abbreviated format into its full form
190
+ *
191
+ * {@see https://en.wikipedia.org/wiki/IPv6_address#Representation} for more on the representation of IPv6 addresses
192
+ *
193
+ * @param {string} ipv6String the abbreviated IPv6 address to expand
194
+ * @returns {string} the expanded IPv6 address
195
+ */
196
+ IPv6Utils.expandIPv6Number = (ipv6String) => {
197
+ let expandWithZero = (hexadecimalArray) => {
198
+ let paddedArray = hexadecimalArray.map((hexadecimal) => {
199
+ return BinaryUtils_1.leftPadWithZeroBit(hexadecimal, 4);
200
+ });
201
+ return paddedArray.join(":");
202
+ };
203
+ let expandDoubleColon = (gapCount) => {
204
+ let pads = [];
205
+ for (let count = 0; count < gapCount; count++) {
206
+ pads.push("0000");
207
+ }
208
+ return pads.join(":");
209
+ };
210
+ if (/(:){3,}/.test(ipv6String))
211
+ throw "given IPv6 contains consecutive : more than two";
212
+ const prefix = extractPrefix(ipv6String);
213
+ if (ipv6String.includes("/")) {
214
+ ipv6String = ipv6String.split("/")[0];
215
+ }
216
+ let isValid = Validator_1.Validator.IPV6_PATTERN.test(ipv6String);
217
+ if (!isValid) {
218
+ throw Error(Validator_1.Validator.invalidIPv6PatternMessage);
219
+ }
220
+ if (ipv6String.includes("::")) {
221
+ let split = ipv6String.split("::");
222
+ let leftPortion = split[0];
223
+ let rightPortion = split[1];
224
+ let leftPortionSplit = leftPortion.split(":").filter(hexadecimal => { return hexadecimal !== ""; });
225
+ let rightPortionSplit = rightPortion.split(":").filter(hexadecimal => { return hexadecimal !== ""; });
226
+ let doublePortion = expandDoubleColon(8 - (leftPortionSplit.length + rightPortionSplit.length));
227
+ let leftString = expandWithZero(leftPortionSplit);
228
+ if (leftString !== "") {
229
+ leftString += ":";
230
+ }
231
+ let rightString = expandWithZero(rightPortionSplit);
232
+ if (rightString !== "") {
233
+ rightString = ":" + rightString;
234
+ }
235
+ return `${leftString}${doublePortion}${rightString}${prefix}`;
236
+ }
237
+ else {
238
+ return `${expandWithZero(ipv6String.split(":"))}${prefix}`;
239
+ }
240
+ };
241
+ /**
242
+ * Collapses an IPv6 number in full format into its abbreviated form
243
+ *
244
+ * {@see https://en.wikipedia.org/wiki/IPv6_address#Representation} for more on the representation of IPv6 addresses
245
+ *
246
+ * @param {string} ipv6String the full form IPv6 number to collapse
247
+ * @returns {string} the collapsed IPv6 number
248
+ */
249
+ IPv6Utils.collapseIPv6Number = (ipv6String) => {
250
+ const prefix = extractPrefix(ipv6String);
251
+ if (ipv6String.includes("/")) {
252
+ ipv6String = ipv6String.split("/")[0];
253
+ }
254
+ let isValid = Validator_1.Validator.IPV6_PATTERN.test(ipv6String);
255
+ if (!isValid) {
256
+ throw Error(Validator_1.Validator.invalidIPv6PatternMessage);
257
+ }
258
+ let hexadecimals = ipv6String.split(":");
259
+ let hexadecimalsWithoutLeadingZeros = hexadecimals.map((hexidecimal) => {
260
+ let withoutLeadingZero = hexidecimal.replace(/^0+/, '');
261
+ if (withoutLeadingZero !== '') {
262
+ return withoutLeadingZero;
263
+ }
264
+ else {
265
+ return "0";
266
+ }
267
+ });
268
+ let contracted = hexadecimalsWithoutLeadingZeros.join(":").replace(/((^0)?(:0){2,}|(^0)(:0){1,})/, ':');
269
+ if (contracted.slice(-1) === ":") {
270
+ return `${contracted}:${prefix}`;
271
+ }
272
+ contracted = contracted.replace(":0:", "::");
273
+ return `${contracted}${prefix}`;
274
+ };
275
+
276
+ return IPv6Utils;
277
+ }
278
+
279
+ var HexadecimalUtils = {};
280
+
281
+ var hasRequiredHexadecimalUtils;
282
+
283
+ function requireHexadecimalUtils () {
284
+ if (hasRequiredHexadecimalUtils) return HexadecimalUtils;
285
+ hasRequiredHexadecimalUtils = 1;
286
+ (function (exports) {
287
+ Object.defineProperty(exports, "__esModule", { value: true });
288
+ exports.hexadectetNotationToBinaryString = exports.binaryStringToHexadecimalString = exports.colonHexadecimalNotationToBinaryString = exports.hexadecimalStringToHexadecatetString = exports.hexadecimalStringToBinaryString = exports.bigIntToHexadecimalString = void 0;
289
+ const IPv6Utils_1 = requireIPv6Utils();
290
+ const BinaryUtils_1 = BinaryUtils;
291
+ /**
292
+ * Converts a given bigint number to a hexadecimal string
293
+ * @param num the bigint number
294
+ * @returns {string} the hexadeciaml string
295
+ */
296
+ exports.bigIntToHexadecimalString = (num) => {
297
+ return num.toString(16);
298
+ };
299
+ /**
300
+ * Converts a number in hexadecimal (base 16) to binary string
301
+ * @param {string} hexadecimalString the number in base 16
302
+ * @returns {string} the number converted to base 2
303
+ */
304
+ exports.hexadecimalStringToBinaryString = (hexadecimalString) => {
305
+ let inDecimal = BigInt(`0x${hexadecimalString}`);
306
+ return inDecimal.toString(2);
307
+ };
308
+ /**
309
+ * Converts a number in hexadecimal (base 16) to binary hexadecatet string.
310
+ * This means the bits in the output cannot be more than 16
311
+ *
312
+ * @param hexadecimalString {string} the number converted to binary hexadecatet string
313
+ */
314
+ exports.hexadecimalStringToHexadecatetString = (hexadecimalString) => {
315
+ let binaryString = exports.hexadecimalStringToBinaryString(hexadecimalString);
316
+ let length = binaryString.length;
317
+ if (length > 16) {
318
+ throw new Error("Given decimal in binary contains digits greater than an Hexadecatet");
319
+ }
320
+ return BinaryUtils_1.leftPadWithZeroBit(binaryString, 16);
321
+ };
322
+ /**
323
+ * Given an IPv6 number in hexadecimal notated string, e.g 2001:0db8:0000:0000:0000:0000:0000:0000 converts it to
324
+ * binary string
325
+ *
326
+ * @param hexadecimalString IPv6 string
327
+ * @returns {string} the binary value of the given ipv6 number in string
328
+ */
329
+ exports.colonHexadecimalNotationToBinaryString = (hexadecimalString) => {
330
+ let expandedIPv6 = IPv6Utils_1.expandIPv6Number(hexadecimalString);
331
+ let stringHexadecimal = expandedIPv6.split(":");
332
+ return stringHexadecimal.reduce((binaryAsString, hexidecimal) => {
333
+ return binaryAsString.concat(exports.hexadecimalStringToHexadecatetString(hexidecimal));
334
+ }, '');
335
+ };
336
+ /**
337
+ * Converts number in binary string to hexadecimal string
338
+ * @param {string} num in binary string
339
+ * @returns {string} num in hexadecimal string
340
+ */
341
+ exports.binaryStringToHexadecimalString = (num) => {
342
+ // first convert to binary string to decimal (big Integer)
343
+ let inDecimal = BigInt(`0b${num}`);
344
+ return inDecimal.toString(16);
345
+ };
346
+ /**
347
+ * Converts a given IPv6 number expressed in the hexadecimal string notation into a 16 bit binary number in string
348
+ * @param {string} hexadectetString the IPv6 number
349
+ * @returns {string} the IPv6 number converted to binary string
350
+ */
351
+ exports.hexadectetNotationToBinaryString = (hexadectetString) => {
352
+ let expand = IPv6Utils_1.expandIPv6Number(hexadectetString);
353
+ let hexadecimals = expand.split(":");
354
+ return hexadecimals.reduce((hexadecimalAsString, hexavalue) => {
355
+ return hexadecimalAsString.concat(BinaryUtils_1.leftPadWithZeroBit(exports.hexadecimalStringToBinaryString(hexavalue), 16));
356
+ }, '');
357
+ };
358
+
359
+ } (HexadecimalUtils));
360
+ return HexadecimalUtils;
361
+ }
362
+
363
+ var hasRequiredValidator;
364
+
365
+ function requireValidator () {
366
+ if (hasRequiredValidator) return Validator;
367
+ hasRequiredValidator = 1;
368
+ Object.defineProperty(Validator, "__esModule", { value: true });
369
+ Validator.Validator = void 0;
370
+ const BinaryUtils_1 = BinaryUtils;
371
+ const BinaryUtils_2 = BinaryUtils;
372
+ const IPv6Utils_1 = requireIPv6Utils();
373
+ const HexadecimalUtils_1 = requireHexadecimalUtils();
374
+ const HexadecimalUtils_2 = requireHexadecimalUtils();
375
+ class Validator$1 {
376
+ /**
377
+ * Checks if given ipNumber is in between the given lower and upper bound
378
+ *
379
+ * @param ipNumber ipNumber to check
380
+ * @param lowerBound lower bound
381
+ * @param upperBound upper bound
382
+ * @returns {boolean} true if ipNumber is between lower and upper bound
383
+ */
384
+ static isWithinRange(ipNumber, lowerBound, upperBound) {
385
+ return ipNumber >= lowerBound && ipNumber <= upperBound;
386
+ }
387
+ /**
388
+ * Checks if the number given is within the value considered valid for an ASN number
389
+ *
390
+ * @param asnNumber the asn number to validate
391
+ * @returns {[boolean , string]} first value is true if valid ASN, false otherwise. Second value contains
392
+ * "valid" or an error message when value is invalid
393
+ */
394
+ static isValidAsnNumber(asnNumber) {
395
+ let isValid = this.isWithinRange(asnNumber, 0n, this.THIRTY_TWO_BIT_SIZE);
396
+ return [isValid, isValid ? [] : [Validator$1.invalidAsnRangeMessage]];
397
+ }
398
+ /**
399
+ * Checks if the given ASN number is a 16bit ASN number
400
+ *
401
+ * @param {bigint} asnNumber to check if 16bit or not
402
+ * @returns {[boolean , string]} first value is true if valid 16bit ASN, false otherwise. Second value contains
403
+ * "valid" or an error message when value is invalid
404
+ */
405
+ static isValid16BitAsnNumber(asnNumber) {
406
+ let isValid = Validator$1.isWithinRange(asnNumber, 0n, Validator$1.SIXTEEN_BIT_SIZE);
407
+ return [isValid, isValid ? [] : [Validator$1.invalid16BitAsnRangeMessage]];
408
+ }
409
+ /**
410
+ * Checks if the number given is within the value considered valid for an IPv4 number
411
+ *
412
+ * @param ipv4Number the asn number to validate
413
+ * @returns {[boolean , string]} first value is true if valid IPv4 number, false otherwise. Second value contains
414
+ * "valid" or an error message when value is invalid
415
+ */
416
+ static isValidIPv4Number(ipv4Number) {
417
+ ipv4Number = typeof ipv4Number === "bigint" ? ipv4Number : BigInt(ipv4Number);
418
+ let isValid = this.isWithinRange(ipv4Number, 0n, this.THIRTY_TWO_BIT_SIZE);
419
+ return isValid ? [isValid, []] : [isValid, [Validator$1.invalidIPv4NumberMessage]];
420
+ }
421
+ /**
422
+ * Checks if the number given is within the value considered valid for an IPv6 number
423
+ *
424
+ * @param ipv6Number the asn number to validate
425
+ * @returns {[boolean , string]} first value is true if valid IPv6 number, false otherwise. Second value contains
426
+ * "valid" or an error message when value is invalid
427
+ */
428
+ static isValidIPv6Number(ipv6Number) {
429
+ let isValid = this.isWithinRange(ipv6Number, 0n, this.ONE_HUNDRED_AND_TWENTY_EIGHT_BIT_SIZE);
430
+ return isValid ? [isValid, []] : [isValid, [Validator$1.invalidIPv6NumberMessage]];
431
+ }
432
+ /**
433
+ * Checks if the number given is valid for an IPv4 octet
434
+ *
435
+ * @param octetNumber the octet value
436
+ * @returns {boolean} true if valid octet, false otherwise
437
+ */
438
+ static isValidIPv4Octet(octetNumber) {
439
+ let withinRange = this.isWithinRange(octetNumber, 0n, this.EIGHT_BIT_SIZE);
440
+ return [withinRange, withinRange ? [] : [Validator$1.invalidOctetRangeMessage]];
441
+ }
442
+ /**
443
+ * Checks if the number given is valid for an IPv6 hexadecatet
444
+ *
445
+ * @param {bigint} hexadecatetNum the hexadecatet value
446
+ * @returns {[boolean , string]} first value is true if valid hexadecatet, false otherwise. Second value contains
447
+ * "valid" or an error message when value is invalid
448
+ */
449
+ static isValidIPv6Hexadecatet(hexadecatetNum) {
450
+ let isValid = this.isWithinRange(hexadecatetNum, 0n, this.SIXTEEN_BIT_SIZE);
451
+ return isValid ? [isValid, []] : [isValid, [Validator$1.invalidHexadecatetMessage]];
452
+ }
453
+ /**
454
+ * Checks if given string is a valid IPv4 value.
455
+ *
456
+ * @param {string} ipv4String the IPv4 string to validate
457
+ * @returns {[boolean , string]} result of validation, first value represents if is valid IPv4, second value
458
+ * contains error message if invalid IPv4
459
+ */
460
+ static isValidIPv4String(ipv4String) {
461
+ let rawOctets = ipv4String.split(".");
462
+ if (rawOctets.length != 4 || rawOctets.includes('')) {
463
+ return [false, [Validator$1.invalidOctetCountMessage]];
464
+ }
465
+ let isValid = rawOctets.every(octet => {
466
+ return Validator$1.isNumeric(octet) ? Validator$1.isValidIPv4Octet(BigInt(octet))[0] : false;
467
+ });
468
+ if (!isValid) {
469
+ return [false, [Validator$1.invalidOctetRangeMessage]];
470
+ }
471
+ isValid = Validator$1.IPV4_PATTERN.test(ipv4String);
472
+ return [isValid, isValid ? [] : [Validator$1.invalidIPv4PatternMessage]];
473
+ }
474
+ /**
475
+ * Checks if given string is a valid IPv6 value.
476
+ *
477
+ * @param {string} ipv6String the IPv6 string to validate
478
+ * @returns {[boolean , string]} result of validation, first value represents if is valid IPv6, second value
479
+ * contains error message if invalid IPv6
480
+ */
481
+ static isValidIPv6String(ipv6String) {
482
+ try {
483
+ let hexadecimals = IPv6Utils_1.expandIPv6Number(ipv6String).split(":");
484
+ if (hexadecimals.length != 8) {
485
+ return [false, [Validator$1.invalidHexadecatetCountMessage]];
486
+ }
487
+ let isValid = hexadecimals.every(hexadecimal => {
488
+ return Validator$1.isHexadecatet(hexadecimal) ?
489
+ Validator$1.isValidIPv6Hexadecatet(BigInt(`0x${hexadecimal}`))[0] : false;
490
+ });
491
+ if (!isValid) {
492
+ return [false, [Validator$1.invalidHexadecatetMessage]];
493
+ }
494
+ isValid = Validator$1.IPV6_PATTERN.test(ipv6String);
495
+ return [isValid, isValid ? [] : [Validator$1.invalidIPv6PatternMessage]];
496
+ }
497
+ catch (error) {
498
+ return [false, [error]];
499
+ }
500
+ }
501
+ /**
502
+ * Checks if given value is a valid prefix value
503
+ *
504
+ * @param prefixValue value to check
505
+ * @param ipNumType The type of IP number
506
+ * @returns {(boolean|string)[]} a tuple representing if valid or not and corresponding message
507
+ */
508
+ static isValidPrefixValue(prefixValue, ipNumType) {
509
+ if ("IPv4" /* IPv4 */ === ipNumType) {
510
+ let withinRange = Validator$1.isWithinRange(BigInt(prefixValue), 0n, 32n);
511
+ return [withinRange, withinRange ? [] : [Validator$1.invalidPrefixValueMessage]];
512
+ }
513
+ if ("IPv6" /* IPv6 */ === ipNumType) {
514
+ let withinRange = Validator$1.isWithinRange(BigInt(prefixValue), 0n, 128n);
515
+ return [withinRange, withinRange ? [] : [Validator$1.invalidPrefixValueMessage]];
516
+ }
517
+ return [false, [Validator$1.invalidInetNumType]];
518
+ }
519
+ /**
520
+ * Checks if given string is a valid IPv4 mask
521
+ *
522
+ * @param {string} ipv4MaskString the given IPv4 mask string
523
+ * @returns {[boolean , string]} first value is true if valid IPv4 mask string, false otherwise. Second value
524
+ * contains "valid" or an error message when value is invalid
525
+ */
526
+ static isValidIPv4Mask(ipv4MaskString) {
527
+ let ipv4InBinary = BinaryUtils_1.dottedDecimalNotationToBinaryString(ipv4MaskString);
528
+ let isValid = Validator$1.IPV4_CONTIGUOUS_MASK_BIT_PATTERN.test(ipv4InBinary);
529
+ return isValid ? [isValid, []] : [isValid, [Validator$1.invalidMaskMessage]];
530
+ }
531
+ /**
532
+ * Checks if given string is a valid IPv6 mask
533
+ *
534
+ * @param {string} ipv6MaskString the given IPv6 mask string
535
+ * @returns {[boolean , string]} first value is true if valid IPv6 mask string, false otherwise. Second value
536
+ * contains "valid" or an error message when value is invalid
537
+ */
538
+ static isValidIPv6Mask(ipv6MaskString) {
539
+ let ipv6InBinary = HexadecimalUtils_2.hexadectetNotationToBinaryString(ipv6MaskString);
540
+ let isValid = Validator$1.IPV6_CONTIGUOUS_MASK_BIT_PATTERN.test(ipv6InBinary);
541
+ return isValid ? [isValid, []] : [isValid, [Validator$1.invalidMaskMessage]];
542
+ }
543
+ /**
544
+ * Checks if the given string is a valid IPv4 range in Cidr notation
545
+ *
546
+ * @param {string} ipv4RangeAsCidrString the IPv4 range in Cidr notation
547
+ *
548
+ * @returns {[boolean , string[]]} first value is true if valid IPv4 range in Cidr notation, false otherwise. Second
549
+ * value contains "valid" or an error message when value is invalid
550
+ */
551
+ static isValidIPv4CidrNotation(ipv4RangeAsCidrString) {
552
+ let cidrComponents = ipv4RangeAsCidrString.split("/");
553
+ if (cidrComponents.length !== 2 || (cidrComponents[0].length === 0 || cidrComponents[1].length === 0)) {
554
+ return [false, [Validator$1.invalidIPv4CidrNotationMessage]];
555
+ }
556
+ let ip = cidrComponents[0];
557
+ let range = cidrComponents[1];
558
+ if (isNaN(Number(range))) {
559
+ return [false, [Validator$1.invalidIPv4CidrNotationMessage]];
560
+ }
561
+ let [validIpv4, invalidIpv4Message] = Validator$1.isValidIPv4String(ip);
562
+ let [validPrefix, invalidPrefixMessage] = Validator$1.isValidPrefixValue(BigInt(range), "IPv4" /* IPv4 */);
563
+ let isValid = validIpv4 && validPrefix;
564
+ let invalidMessage = invalidIpv4Message.concat(invalidPrefixMessage);
565
+ return isValid ? [isValid, []] : [isValid, invalidMessage];
566
+ }
567
+ /**
568
+ * Checks if the given string is a valid IPv4 range in Cidr notation, with the ip number in the cidr notation
569
+ * being the start of the range
570
+ *
571
+ * @param {string} ipv4CidrNotation the IPv4 range in Cidr notation
572
+ *
573
+ * * @returns {[boolean , string[]]} first value is true if valid Cidr notation, false otherwise. Second
574
+ * value contains [] or an array of error message when invalid
575
+ */
576
+ static isValidIPv4CidrRange(ipv4CidrNotation) {
577
+ return Validator$1.isValidCidrRange(ipv4CidrNotation, Validator$1.isValidIPv4CidrNotation, BinaryUtils_1.dottedDecimalNotationToBinaryString, (value) => BinaryUtils_2.cidrPrefixToMaskBinaryString(value, "IPv4" /* IPv4 */));
578
+ }
579
+ /**
580
+ * Checks if the given string is a valid IPv6 range in Cidr notation, with the ip number in the cidr notation
581
+ * being the start of the range
582
+ *
583
+ * @param {string} ipv6CidrNotation the IPv6 range in Cidr notation
584
+ *
585
+ * * @returns {[boolean , string[]]} first value is true if valid Cidr notation, false otherwise. Second
586
+ * value contains [] or an array of error message when invalid
587
+ */
588
+ static isValidIPv6CidrRange(ipv6CidrNotation) {
589
+ return Validator$1.isValidCidrRange(ipv6CidrNotation, Validator$1.isValidIPv6CidrNotation, HexadecimalUtils_1.colonHexadecimalNotationToBinaryString, (value) => BinaryUtils_2.cidrPrefixToMaskBinaryString(value, "IPv6" /* IPv6 */));
590
+ }
591
+ static isValidCidrRange(rangeString, cidrNotationValidator, toBinaryStringConverter, prefixFactory) {
592
+ let validationResult = cidrNotationValidator(rangeString);
593
+ if (!validationResult[0]) {
594
+ return validationResult;
595
+ }
596
+ let cidrComponents = rangeString.split("/");
597
+ let ip = cidrComponents[0];
598
+ let range = cidrComponents[1];
599
+ let ipNumber = BigInt(`0b${toBinaryStringConverter(ip)}`);
600
+ let mask = BigInt(`0b${prefixFactory(parseInt(range))}`);
601
+ let isValid = (ipNumber & (mask)) === (ipNumber);
602
+ return isValid ? [isValid, []] : [isValid, [Validator$1.InvalidIPCidrRangeMessage]];
603
+ }
604
+ static isValidIPv4RangeString(ipv4RangeString) {
605
+ let firstLastValidator = (firstIP, lastIP) => BigInt(`0b${BinaryUtils_1.dottedDecimalNotationToBinaryString(firstIP)}`)
606
+ >= BigInt(`0b${BinaryUtils_1.dottedDecimalNotationToBinaryString(lastIP)}`);
607
+ return this.isValidRange(ipv4RangeString, Validator$1.isValidIPv4String, firstLastValidator);
608
+ }
609
+ static isValidIPv6RangeString(ipv6RangeString) {
610
+ let firstLastValidator = (firstIP, lastIP) => BigInt(`0b${HexadecimalUtils_2.hexadectetNotationToBinaryString(firstIP)}`)
611
+ >= BigInt(`0b${HexadecimalUtils_2.hexadectetNotationToBinaryString(lastIP)}`);
612
+ return this.isValidRange(ipv6RangeString, Validator$1.isValidIPv6String, firstLastValidator);
613
+ }
614
+ static isValidRange(rangeString, validator, firstLastValidator) {
615
+ let rangeComponents = rangeString.split("-").map(component => component.trim());
616
+ if (rangeComponents.length !== 2 || (rangeComponents[0].length === 0 || rangeComponents[1].length === 0)) {
617
+ return [false, [Validator$1.invalidRangeNotationMessage]];
618
+ }
619
+ let firstIP = rangeComponents[0];
620
+ let lastIP = rangeComponents[1];
621
+ let [validFirstIP, invalidFirstIPMessage] = validator(firstIP);
622
+ let [validLastIP, invalidLastIPMessage] = validator(lastIP);
623
+ let isValid = validFirstIP && validLastIP;
624
+ if (isValid && firstLastValidator(firstIP, lastIP)) {
625
+ return [false, [Validator$1.invalidRangeFirstNotGreaterThanLastMessage]];
626
+ }
627
+ let invalidMessage = invalidFirstIPMessage.concat(invalidLastIPMessage);
628
+ return isValid ? [isValid, []] : [isValid, invalidMessage];
629
+ }
630
+ /**
631
+ * Checks if the given string is a valid IPv6 range in Cidr notation
632
+ *
633
+ * @param {string} ipv6RangeAsCidrString the IPv6 range in Cidr notation
634
+ *
635
+ * @returns {[boolean , string]} first value is true if valid IPv6 range in Cidr notation, false otherwise.
636
+ * Second value contains "valid" or an error message when value is invalid
637
+ */
638
+ // TODO change to be like isValidIPv4CidrNotation where validation is done on the component of the cidr notation
639
+ // instead of a single regex check
640
+ static isValidIPv6CidrNotation(ipv6RangeAsCidrString) {
641
+ let isValid = Validator$1.IPV6_RANGE_PATTERN.test(ipv6RangeAsCidrString);
642
+ return isValid ? [isValid, []] : [isValid, [Validator$1.invalidIPv6CidrNotationString]];
643
+ }
644
+ /**
645
+ * Checks if the given string is a binary string. That is contains only contiguous 1s and 0s
646
+ *
647
+ * @param {string} binaryString the binary string
648
+ * @returns {(boolean|string)[]} a tuple representing if valid or not and corresponding message
649
+ */
650
+ static isValidBinaryString(binaryString) {
651
+ if (/^([10])+$/.test(binaryString)) {
652
+ return [true, []];
653
+ }
654
+ else {
655
+ return [false, [Validator$1.invalidBinaryStringErrorMessage]];
656
+ }
657
+ }
658
+ static isNumeric(value) {
659
+ return /^(\d+)$/.test(value);
660
+ }
661
+ static isHexadecatet(value) {
662
+ return /^[0-9A-Fa-f]{4}$/.test(value);
663
+ }
664
+ }
665
+ Validator.Validator = Validator$1;
666
+ Validator$1.IPV4_PATTERN = new RegExp(/^(0?[0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(0?[0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(0?[0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(0?[0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/);
667
+ // source: https://community.helpsystems.com/forums/intermapper/miscellaneous-topics/5acc4fcf-fa83-e511-80cf-0050568460e4
668
+ Validator$1.IPV6_PATTERN = new RegExp(/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/);
669
+ Validator$1.IPV4_RANGE_PATTERN = new RegExp(/^(0?[0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(0?[0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(0?[0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(0?[0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\/)([1-9]|[1-2][0-9]|3[0-2])$/);
670
+ Validator$1.IPV6_RANGE_PATTERN = new RegExp(/^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/);
671
+ Validator$1.IPV4_CONTIGUOUS_MASK_BIT_PATTERN = new RegExp(/^(1){0,32}(0){0,32}$/);
672
+ Validator$1.IPV6_CONTIGUOUS_MASK_BIT_PATTERN = new RegExp(/^(1){0,128}(0){0,128}$/);
673
+ Validator$1.EIGHT_BIT_SIZE = BigInt(`0b${"1".repeat(8)}`);
674
+ Validator$1.SIXTEEN_BIT_SIZE = BigInt(`0b${"1".repeat(16)}`);
675
+ Validator$1.THIRTY_TWO_BIT_SIZE = BigInt(`0b${"1".repeat(32)}`);
676
+ Validator$1.ONE_HUNDRED_AND_TWENTY_EIGHT_BIT_SIZE = BigInt(`0b${"1".repeat(128)}`);
677
+ Validator$1.IPV4_SIZE = BigInt("4294967296");
678
+ Validator$1.IPV6_SIZE = BigInt("340282366920938463463374607431768211456");
679
+ Validator$1.invalidAsnRangeMessage = "ASN number given less than zero or is greater than 32bit";
680
+ Validator$1.invalid16BitAsnRangeMessage = "ASN number given less than zero or is greater than 16bit";
681
+ Validator$1.invalidIPv4NumberMessage = "IPv4 number given less than zero or is greater than 32bit";
682
+ Validator$1.invalidIPv6NumberMessage = "IPv6 number given less than zero or is greater than 128bit";
683
+ Validator$1.invalidOctetRangeMessage = "Value given contains an invalid Octet; Value is less than zero or is greater than 8bit";
684
+ Validator$1.invalidHexadecatetMessage = "The value given is less than zero or is greater than 16bit";
685
+ Validator$1.invalidOctetCountMessage = "An IP4 number cannot have less or greater than 4 octets";
686
+ Validator$1.invalidHexadecatetCountMessage = "An IP6 number must have exactly 8 hexadecatet";
687
+ Validator$1.invalidMaskMessage = "The Mask is invalid";
688
+ Validator$1.invalidPrefixValueMessage = "A Prefix value cannot be less than 0 or greater than 32";
689
+ Validator$1.invalidIPv4CidrNotationMessage = "Cidr notation should be in the form [ip number]/[range]";
690
+ Validator$1.InvalidIPCidrRangeMessage = "Given IP number portion must is not the start of the range";
691
+ Validator$1.invalidRangeNotationMessage = "Range notation should be in the form [first ip]-[last ip]";
692
+ Validator$1.invalidRangeFirstNotGreaterThanLastMessage = "First IP in [first ip]-[last ip] must be less than Last IP";
693
+ Validator$1.invalidIPv6CidrNotationString = "A Cidr notation string should contain an IPv6 number and prefix";
694
+ Validator$1.takeOutOfRangeSizeMessage = "$count is greater than $size, the size of the range";
695
+ Validator$1.cannotSplitSingleRangeErrorMessage = "Cannot split an IP range with a single IP number";
696
+ Validator$1.invalidInetNumType = "Given ipNumType must be either InetNumType.IPv4 or InetNumType.IPv6";
697
+ Validator$1.invalidBinaryStringErrorMessage = "Binary string should contain only contiguous 1s and 0s";
698
+ Validator$1.invalidIPRangeSizeMessage = "Given size is zero or greater than maximum size of $iptype";
699
+ Validator$1.invalidIPRangeSizeForCidrMessage = "Given size can't be created via cidr prefix";
700
+ Validator$1.invalidIPv4PatternMessage = "Given IPv4 is not confirm to a valid IPv6 address";
701
+ Validator$1.invalidIPv6PatternMessage = "Given IPv6 is not confirm to a valid IPv6 address";
702
+
703
+ return Validator;
704
+ }
705
+
706
+ Object.defineProperty(Hexadecatet$1, "__esModule", { value: true });
707
+ Hexadecatet$1.Hexadecatet = void 0;
708
+ const Validator_1$4 = requireValidator();
709
+ /**
710
+ * A representation of a 4-digit hexadecimal number.
711
+ *
712
+ * It consists of four (base 16) number. ie FFFF
713
+ *
714
+ * It is used to represents the components of an IPv6 address
715
+ */
716
+ class Hexadecatet {
717
+ /**
718
+ * Constructor for creating an instance of {@link Hexadecatet}
719
+ *
720
+ * @param {string | number} givenValue a string or numeric value. If given value is a string then it should be a
721
+ * four (base 16) number representation of a 16bit value. If it is a number, then it should be a decimal number
722
+ * representation of a 16 bit value
723
+ */
724
+ constructor(givenValue) {
725
+ let hexadecatetValue;
726
+ if (typeof givenValue === 'string') {
727
+ hexadecatetValue = parseInt(givenValue, 16);
728
+ }
729
+ else {
730
+ hexadecatetValue = parseInt(String(givenValue), 16);
731
+ }
732
+ let [isValid, message] = Validator_1$4.Validator.isValidIPv6Hexadecatet(BigInt(hexadecatetValue));
733
+ if (!isValid) {
734
+ throw Error(message.filter(msg => { return msg !== ''; }).toString());
735
+ }
736
+ this.value = hexadecatetValue;
737
+ }
738
+ /**
739
+ * A convenience method for constructing an instance of {@link Hexadecatet} from a four (base 16) number
740
+ * representation of a 16bit value.
741
+ *
742
+ * @param {string} rawValue the four (base 16) number
743
+ * @returns {Hexadecatet} an instance of {@link Hexadecatet}
744
+ */
745
+ static fromString(rawValue) {
746
+ return new Hexadecatet(rawValue);
747
+ }
748
+ ;
749
+ /**
750
+ * A convenience method for constructing an instance of {@link Hexadecatet} from a decimal number representation
751
+ * of a 16 bit value
752
+ *
753
+ * @param {number} rawValue decimal number representation of a 16 bit value
754
+ * @returns {Hexadecatet} an instance of {@link Hexadecatet}
755
+ */
756
+ static fromNumber(rawValue) {
757
+ return new Hexadecatet(rawValue);
758
+ }
759
+ ;
760
+ /**
761
+ * Returns the numeric value in base 10 (ie decimal)
762
+ *
763
+ * @returns {number} the numeric value in base 10 (ie decimal)
764
+ */
765
+ getValue() {
766
+ return this.value;
767
+ }
768
+ /**
769
+ * Returns the string representation of the base 16 representation of the value
770
+ * @returns {string} the string representation of the base 16 representation of the value
771
+ */
772
+ // TODO pad with a zero if digit is less than 4
773
+ toString() {
774
+ return this.value.toString(16);
775
+ }
776
+ }
777
+ Hexadecatet$1.Hexadecatet = Hexadecatet;
778
+
779
+ var IPNumber = {};
780
+
781
+ var Octet$1 = {};
782
+
783
+ Object.defineProperty(Octet$1, "__esModule", { value: true });
784
+ Octet$1.Octet = void 0;
785
+ const Validator_1$3 = requireValidator();
786
+ /**
787
+ * A binary representation of a 8 bit value.
788
+ *
789
+ * {@see https://en.wikipedia.org/wiki/Octet_(computing)} for more information on Octets
790
+ *
791
+ * An octet is used in the textual representation of an {@link IPv4} number, where the IP number value is divided
792
+ * into 4 octets
793
+ */
794
+ class Octet {
795
+ /**
796
+ * Constructor for creating an instance of an Octet.
797
+ *
798
+ * The constructor parameter given could either be a string or number.
799
+ *
800
+ * If a string, it is the string representation of the numeric value of the octet
801
+ * If a number, it is the numeric representation of the value of the octet
802
+ *
803
+ * @param {string | number} givenValue value of the octet to be created.
804
+ */
805
+ constructor(givenValue) {
806
+ let octetValue;
807
+ if (typeof givenValue === 'string') {
808
+ octetValue = parseInt(givenValue);
809
+ }
810
+ else {
811
+ octetValue = givenValue;
812
+ }
813
+ let [isValid, message] = Validator_1$3.Validator.isValidIPv4Octet(BigInt(octetValue));
814
+ if (!isValid) {
815
+ throw Error(message.filter(msg => { return msg !== ''; }).toString());
816
+ }
817
+ this.value = octetValue;
818
+ }
819
+ /**
820
+ * Convenience method for creating an Octet out of a string value representing the value of the octet
821
+ *
822
+ * @param {string} rawValue the octet value in string
823
+ * @returns {Octet} the Octet instance
824
+ */
825
+ static fromString(rawValue) {
826
+ return new Octet(rawValue);
827
+ }
828
+ ;
829
+ /**
830
+ * Convenience method for creating an Octet out of a numeric value representing the value of the octet
831
+ *
832
+ * @param {number} rawValue the octet value in number
833
+ * @returns {Octet} the Octet instance
834
+ */
835
+ static fromNumber(rawValue) {
836
+ return new Octet(rawValue);
837
+ }
838
+ ;
839
+ /**
840
+ * Method to get the numeric value of the octet
841
+ *
842
+ * @returns {number} the numeric value of the octet
843
+ */
844
+ getValue() {
845
+ return this.value;
846
+ }
847
+ /**
848
+ * Returns a decimal representation of the value of the octet in string
849
+ *
850
+ * @returns {string} a decimal representation of the value of the octet in string
851
+ */
852
+ toString() {
853
+ return this.value.toString(10);
854
+ }
855
+ }
856
+ Octet$1.Octet = Octet;
857
+
858
+ Object.defineProperty(IPNumber, "__esModule", { value: true });
859
+ IPNumber.isIPv4 = IPNumber.IPv6Mask = IPNumber.IPv4Mask = IPNumber.IPv6 = IPNumber.Asn = IPNumber.IPv4 = IPNumber.AbstractIPNum = void 0;
860
+ const Octet_1 = Octet$1;
861
+ const Validator_1$2 = requireValidator();
862
+ const BinaryUtils_1$2 = BinaryUtils;
863
+ const BinaryUtils_2 = BinaryUtils;
864
+ const BinaryUtils_3 = BinaryUtils;
865
+ const BinaryUtils_4 = BinaryUtils;
866
+ const Hexadecatet_1$1 = Hexadecatet$1;
867
+ const HexadecimalUtils_1$1 = requireHexadecimalUtils();
868
+ const IPv6Utils_1 = requireIPv6Utils();
869
+ const HexadecimalUtils_2 = requireHexadecimalUtils();
870
+ /**
871
+ * Provides the implementation of functionality that are common
872
+ * to {@link IPv4}, {@link IPv6}, {@link IPv4Mask} and {@link IPv6Mask}
873
+ */
874
+ class AbstractIPNum {
875
+ /**
876
+ * Gets the numeric value of an IP number as {@link BigInt}
877
+ *
878
+ * @returns bigInt the numeric value of an IP number.
879
+ */
880
+ getValue() {
881
+ return this.value;
882
+ }
883
+ /**
884
+ * Gets the binary string representation of an IP number.
885
+ *
886
+ * @returns {string} the string binary representation.
887
+ */
888
+ toBinaryString() {
889
+ return BinaryUtils_3.leftPadWithZeroBit(this.value.toString(2), this.bitSize);
890
+ }
891
+ /**
892
+ * Checks if an IP number has a value greater than the present value
893
+ * @returns {boolean} true, if there is a value greater than the present value. Returns false otherwise.
894
+ */
895
+ hasNext() {
896
+ return this.value < this.maximumBitSize;
897
+ }
898
+ /**
899
+ * Checks if an IP number has a value lesser than the present value
900
+ * @returns {boolean} true, if there is a value lesser than the present value. Returns false otherwise.
901
+ */
902
+ hasPrevious() {
903
+ return this.value > 0n;
904
+ }
905
+ /**
906
+ * Checks if the given IP number, is equals to the current IP number
907
+ *
908
+ * @param {AbstractIPNum} anotherIPNum the other IP number to compare with
909
+ * @returns {boolean} true if the given IP number is equals
910
+ */
911
+ isEquals(anotherIPNum) {
912
+ return this.value === anotherIPNum.value;
913
+ }
914
+ /**
915
+ * Checks if the given IP number is lesser than this current IP number
916
+ *
917
+ * @param {AbstractIPNum} anotherIPNum the other IP number to compare with
918
+ * @returns {boolean} true if the given IP number is less than this current one. False otherwise.
919
+ */
920
+ isLessThan(anotherIPNum) {
921
+ return this.value < anotherIPNum.value;
922
+ }
923
+ /**
924
+ * Checks if the given IP number is greater than this current IP number
925
+ *
926
+ * @param {AbstractIPNum} anotherIPNum the other IP number to compare with
927
+ * @returns {boolean} true if the given IP number is greater than this current one. False otherwise.
928
+ */
929
+ isGreaterThan(anotherIPNum) {
930
+ return this.value > anotherIPNum.value;
931
+ }
932
+ /**
933
+ * Checks if the given IP number is less than or equals to this current IP number
934
+ *
935
+ * @param {AbstractIPNum} anotherIPNum the other IP number to compare with
936
+ * @returns {boolean} true if the given IP number is less than or equals to this current one. False otherwise.
937
+ */
938
+ isLessThanOrEquals(anotherIPNum) {
939
+ return this.value <= anotherIPNum.value;
940
+ }
941
+ /**
942
+ * Checks if the given IP number is greater than or equals to this current IP number
943
+ *
944
+ * @param {AbstractIPNum} anotherIPNum the other IP number to compare with
945
+ * @returns {boolean} {boolean} true if the given IP number is greater than or equals to this current one. False
946
+ * otherwise.
947
+ */
948
+ isGreaterThanOrEquals(anotherIPNum) {
949
+ return this.value >= anotherIPNum.value;
950
+ }
951
+ }
952
+ IPNumber.AbstractIPNum = AbstractIPNum;
953
+ /**
954
+ * Represents an IPv4 number. A 32 bit number that is used to uniquely identify a device that is part of a computer
955
+ * network that uses the internet protocol for communication.
956
+ *
957
+ * @see https://en.wikipedia.org/wiki/IPv4
958
+ * @see https://www.rfc-editor.org/info/rfc791
959
+ */
960
+ class IPv4 extends AbstractIPNum {
961
+ /**
962
+ * Constructor for an IPv4 number.
963
+ *
964
+ * @param {string | bigint} ipValue value to construct an IPv4 from. The given value can either be
965
+ * numeric or string. If a string is given then it needs to be in dot-decimal notation
966
+ */
967
+ constructor(ipValue) {
968
+ super();
969
+ /**
970
+ * The number of bits needed to represents the value of the IPv4 number
971
+ */
972
+ this.bitSize = 32;
973
+ /**
974
+ * The maximum bit size (i.e. binary value) of the IPv4 number in BigInt
975
+ */
976
+ this.maximumBitSize = Validator_1$2.Validator.THIRTY_TWO_BIT_SIZE;
977
+ /**
978
+ * The type of IP number. Value is one of the values of the {@link IPNumType} enum
979
+ * @type {IPNumType} the type of IP number
980
+ */
981
+ this.type = "IPv4" /* IPv4 */;
982
+ /**
983
+ * An array of {@link Octet}'s
984
+ *
985
+ * @type {Array} the octets that makes up the IPv4 number
986
+ */
987
+ this.octets = [];
988
+ /**
989
+ * The string character used to separate the individual octets when the IPv4 is rendered as strings
990
+ *
991
+ * @type {string} The string character used to separate the individual octets when rendered as strings
992
+ */
993
+ this.separator = ".";
994
+ if (typeof ipValue === "string") {
995
+ let [value, octets] = this.constructFromDecimalDottedString(ipValue);
996
+ this.value = value;
997
+ this.octets = octets;
998
+ }
999
+ else {
1000
+ let [value, octets] = this.constructFromBigIntValue(ipValue);
1001
+ this.value = value;
1002
+ this.octets = octets;
1003
+ }
1004
+ }
1005
+ /**
1006
+ * A convenience method for creating an {@link IPv4} by providing the decimal value of the IP number in BigInt
1007
+ *
1008
+ * @param {bigint} bigIntValue the decimal value of the IP number in BigInt
1009
+ * @returns {IPv4} the IPv4 instance
1010
+ */
1011
+ static fromNumber(bigIntValue) {
1012
+ return new IPv4(bigIntValue);
1013
+ }
1014
+ /**
1015
+ * A convenience method for creating an {@link IPv4} by providing the IP number in dot-decimal notation. E.g
1016
+ * "10.1.1.10"
1017
+ *
1018
+ * {@see https://en.wikipedia.org/wiki/Dot-decimal_notation} for more information on dot-decimal notation.
1019
+ *
1020
+ * @param {string} ipString the IP number in dot-decimal notation
1021
+ * @returns {IPv4} the IPv4 instance
1022
+ */
1023
+ static fromDecimalDottedString(ipString) {
1024
+ return new IPv4(ipString);
1025
+ }
1026
+ /**
1027
+ * Alias for IPv4.fromDecimalDottedString.
1028
+ *
1029
+ * @param {string} ipString the IP number in dot-decimal notation
1030
+ * @returns {IPv4} the IPv4 instance
1031
+ */
1032
+ static fromString(ipString) {
1033
+ return IPv4.fromDecimalDottedString(ipString);
1034
+ }
1035
+ /**
1036
+ * A convenience method for creating an {@link IPv4} from binary string
1037
+ *
1038
+ * @param {string} ipBinaryString the binary string representing the IPv4 number to be created
1039
+ * @returns {IPv4} the IPv4 instance
1040
+ */
1041
+ static fromBinaryString(ipBinaryString) {
1042
+ let validationResult = Validator_1$2.Validator.isValidBinaryString(ipBinaryString);
1043
+ if (validationResult[0]) {
1044
+ return new IPv4(BinaryUtils_2.parseBinaryStringToBigInt(ipBinaryString));
1045
+ }
1046
+ else {
1047
+ throw Error(validationResult[1].join(','));
1048
+ }
1049
+ }
1050
+ /**
1051
+ * A string representation of the IPv4 number. The string representation is in dot-decimal notation
1052
+ *
1053
+ * @returns {string} The string representation in dot-decimal notation
1054
+ */
1055
+ toString() {
1056
+ return this.octets.map((value) => { return value.toString(); }).join(this.separator);
1057
+ }
1058
+ /**
1059
+ * Gets the individual {@link Octet} that makes up the IPv4 number
1060
+ *
1061
+ * @returns {Array<Octet>} The individual {@link Octet} that makes up the IPv4 number
1062
+ */
1063
+ getOctets() {
1064
+ return this.octets;
1065
+ }
1066
+ /**
1067
+ * Returns the next IPv4 number
1068
+ *
1069
+ * @returns {IPv4} the next IPv4 number
1070
+ */
1071
+ nextIPNumber() {
1072
+ return IPv4.fromNumber(this.getValue() + 1n);
1073
+ }
1074
+ /**
1075
+ * Returns the previous IPv4 number
1076
+ *
1077
+ * @returns {IPv4} the previous IPv4 number
1078
+ */
1079
+ previousIPNumber() {
1080
+ return IPv4.fromNumber(this.getValue() - 1n);
1081
+ }
1082
+ /**
1083
+ * Returns this IPv4 number as a IPv4-Mapped IPv6 Address
1084
+ *
1085
+ * The IPv4-Mapped IPv6 Address allows an IPv4 number to be embedded within an IPv6 number
1086
+ *
1087
+ * {@see https://tools.ietf.org/html/rfc4291#section-2.5.5} for more information on the IPv4-Mapped IPv6 Address
1088
+ *
1089
+ * @returns {IPv6} an IPv6 number with the IPv4 embedded within it
1090
+ */
1091
+ toIPv4MappedIPv6() {
1092
+ let binary = '1'.repeat(16) + this.toBinaryString();
1093
+ return IPv6.fromBinaryString(binary);
1094
+ }
1095
+ constructFromDecimalDottedString(ipString) {
1096
+ let octets;
1097
+ let value;
1098
+ let [isValid, message] = Validator_1$2.Validator.isValidIPv4String(ipString);
1099
+ if (!isValid) {
1100
+ throw new Error(message.filter(msg => { return msg !== ''; }).toString());
1101
+ }
1102
+ let stringOctets = ipString.split(".");
1103
+ octets = stringOctets.map((rawOctet) => {
1104
+ return Octet_1.Octet.fromString(rawOctet);
1105
+ });
1106
+ value = BigInt(`0b${BinaryUtils_1$2.dottedDecimalNotationToBinaryString(ipString)}`);
1107
+ return [value, octets];
1108
+ }
1109
+ constructFromBigIntValue(ipv4Number) {
1110
+ let [isValid, message] = Validator_1$2.Validator.isValidIPv4Number(ipv4Number);
1111
+ if (!isValid) {
1112
+ throw new Error(message.filter(msg => { return msg !== ''; }).toString());
1113
+ }
1114
+ let binaryString = BinaryUtils_4.numberToBinaryString(ipv4Number);
1115
+ ipv4Number = typeof ipv4Number === "bigint" ? ipv4Number : BigInt(ipv4Number);
1116
+ return [ipv4Number, this.binaryStringToDecimalOctets(binaryString)];
1117
+ }
1118
+ binaryStringToDecimalOctets(ipv4BinaryString) {
1119
+ if (ipv4BinaryString.length < 32) {
1120
+ ipv4BinaryString = BinaryUtils_3.leftPadWithZeroBit(ipv4BinaryString, 32);
1121
+ }
1122
+ let octets = ipv4BinaryString.match(/.{1,8}/g);
1123
+ return octets.map((octet) => {
1124
+ return Octet_1.Octet.fromString(BinaryUtils_2.parseBinaryStringToBigInt(octet).toString());
1125
+ });
1126
+ }
1127
+ }
1128
+ IPNumber.IPv4 = IPv4;
1129
+ /**
1130
+ * Represents an Autonomous System Number. Which is a number that is used to identify
1131
+ * a group of IP addresses with a common, clearly defined routing policy.
1132
+ *
1133
+ * @see https://en.wikipedia.org/wiki/Autonomous_system_(Internet)
1134
+ * @see https://tools.ietf.org/html/rfc5396
1135
+ */
1136
+ class Asn extends AbstractIPNum {
1137
+ /**
1138
+ * Constructor for an instance of {@link ASN}
1139
+ *
1140
+ * @param {string | number} rawValue value to construct an ASN from. The given value can either be numeric or
1141
+ * string. If in string then it can be in asplain, asdot or asdot+ string representation format
1142
+ */
1143
+ constructor(rawValue) {
1144
+ super();
1145
+ /**
1146
+ * The number of bits needed to represents the value of the ASN number
1147
+ */
1148
+ this.bitSize = 32;
1149
+ /**
1150
+ * The maximum bit size (i.e. binary value) of the ASN number in BigInt
1151
+ */
1152
+ this.maximumBitSize = Validator_1$2.Validator.THIRTY_TWO_BIT_SIZE;
1153
+ this.type = "ASN" /* ASN */;
1154
+ if (typeof rawValue === 'string') {
1155
+ if (Asn.startWithASPrefix(rawValue)) {
1156
+ this.value = BigInt(parseInt(rawValue.substring(2)));
1157
+ }
1158
+ else if (rawValue.indexOf(".") != -1) {
1159
+ this.value = BigInt(this.parseFromDotNotation(rawValue));
1160
+ }
1161
+ else {
1162
+ this.value = BigInt(parseInt(rawValue));
1163
+ }
1164
+ }
1165
+ else {
1166
+ let valueAsBigInt = BigInt(rawValue);
1167
+ let [isValid, message] = Validator_1$2.Validator.isValidAsnNumber(valueAsBigInt);
1168
+ if (!isValid) {
1169
+ throw Error(message.filter(msg => { return msg !== ''; }).toString());
1170
+ }
1171
+ this.value = valueAsBigInt;
1172
+ }
1173
+ }
1174
+ /**
1175
+ * A convenience method for creating an instance of {@link Asn} from a string
1176
+ *
1177
+ * The given string can be in asplain, asdot or asdot+ representation format.
1178
+ * {@see https://tools.ietf.org/html/rfc5396} for more information on
1179
+ * the different ASN string representation
1180
+ *
1181
+ * @param {string} rawValue the asn string. In either asplain, asdot or asdot+ format
1182
+ * @returns {Asn} the constructed ASN instance
1183
+ */
1184
+ static fromString(rawValue) {
1185
+ return new Asn(rawValue);
1186
+ }
1187
+ ;
1188
+ /**
1189
+ * A convenience method for creating an instance of {@link Asn} from a numeric value
1190
+ *
1191
+ * @param {number} rawValue the asn numeric value
1192
+ * @returns {Asn} the constructed ASN instance
1193
+ */
1194
+ static fromNumber(rawValue) {
1195
+ return new Asn(rawValue);
1196
+ }
1197
+ ;
1198
+ /**
1199
+ * A convenience method for creating an instance of {@link Asn} from a binary string
1200
+ *
1201
+ * @param {string} binaryString to create an ASN instance from
1202
+ * @returns {Asn} the constructed ASN instance
1203
+ */
1204
+ static fromBinaryString(binaryString) {
1205
+ let validationResult = Validator_1$2.Validator.isValidBinaryString(binaryString);
1206
+ if (validationResult[0]) {
1207
+ return new Asn(parseInt(binaryString, 2));
1208
+ }
1209
+ else {
1210
+ throw Error(validationResult[1].join(','));
1211
+ }
1212
+ }
1213
+ /**
1214
+ * A string representation where the asn value is prefixed by "ASN". For example "AS65526"
1215
+ *
1216
+ * @returns {string} A string representation where the asn value is prefixed by "ASN"
1217
+ */
1218
+ toString() {
1219
+ let stringValue = this.value.toString();
1220
+ return `${Asn.AS_PREFIX}${stringValue}`;
1221
+ }
1222
+ /**
1223
+ * A string representation where the ASN numeric value of is represented as a string. For example "65526"
1224
+ *
1225
+ * @returns {string} A string representation where the ASN numeric value of is represented as a string
1226
+ */
1227
+ toASPlain() {
1228
+ return this.value.toString();
1229
+ }
1230
+ /**
1231
+ * A string representation where the ASN value is represented using the asplain notation if the ASN value is
1232
+ * less than 65536 and uses asdot+ notation when the value is greater than 65536.
1233
+ *
1234
+ * For example 65526 will be represented as "65526" while 65546 will be represented as "1.10"
1235
+ *
1236
+ *
1237
+ * @returns {string} A string representation of the ASN in either asplain or asdot+ notation depending on
1238
+ * whether the numeric value of the ASN number is greater than 65526 or not.
1239
+ */
1240
+ toASDot() {
1241
+ if (this.value.valueOf() >= 65536n) {
1242
+ return this.toASDotPlus();
1243
+ }
1244
+ return this.toASPlain();
1245
+ }
1246
+ /**
1247
+ * A string representation where the ASN value is represented using the asdot+ notation
1248
+ *
1249
+ * @returns {string} A string representation where the ASN value is represented using the asdot+ notation
1250
+ *
1251
+ */
1252
+ toASDotPlus() {
1253
+ let high = this.value.valueOf() / 65535n;
1254
+ let low = (this.value.valueOf() % 65535n) - high;
1255
+ return `${high}.${low}`;
1256
+ }
1257
+ /**
1258
+ * Converts the ASN value to binary numbers represented with strings
1259
+ *
1260
+ * @returns {string} a binary string representation of the value of the ASN number
1261
+ */
1262
+ toBinaryString() {
1263
+ return BinaryUtils_4.numberToBinaryString(this.value);
1264
+ }
1265
+ /**
1266
+ * Checks if the ASN value is 16bit
1267
+ *
1268
+ * @returns {boolean} true if the ASN is a 16bit value. False otherwise.
1269
+ */
1270
+ is16Bit() {
1271
+ let [valid16BitAsnNumber,] = Validator_1$2.Validator.isValid16BitAsnNumber(this.value);
1272
+ return valid16BitAsnNumber;
1273
+ }
1274
+ /**
1275
+ * Checks if the ASN value is 32bit
1276
+ *
1277
+ * @returns {boolean} true if the ASN is a 32bit value. False otherwise.
1278
+ */
1279
+ is32Bit() {
1280
+ return !this.is16Bit();
1281
+ }
1282
+ /**
1283
+ * Returns the next ASN number
1284
+ *
1285
+ * @returns {AbstractIPNum} the next ASN number
1286
+ */
1287
+ nextIPNumber() {
1288
+ return new Asn(this.value.valueOf() + 1n);
1289
+ }
1290
+ /**
1291
+ * Returns the previous ASN number
1292
+ *
1293
+ * @returns {AbstractIPNum} the previous ASN number
1294
+ */
1295
+ previousIPNumber() {
1296
+ return new Asn(this.value.valueOf() - 1n);
1297
+ }
1298
+ static startWithASPrefix(word) {
1299
+ return word.indexOf(Asn.AS_PREFIX) === 0;
1300
+ }
1301
+ parseFromDotNotation(rawValue) {
1302
+ let values = rawValue.split(".");
1303
+ let high = parseInt(values[0]);
1304
+ let low = parseInt(values[1]);
1305
+ return (high * 65535) + (low + high);
1306
+ }
1307
+ }
1308
+ IPNumber.Asn = Asn;
1309
+ Asn.AS_PREFIX = "AS";
1310
+ /**
1311
+ * Represents an IPv6 number. A 128 bit number that is used to uniquely identify a device that is part of a computer
1312
+ * network that uses the internet protocol for communication.
1313
+ *
1314
+ * @see https://en.wikipedia.org/wiki/IPv6
1315
+ * @see https://www.rfc-editor.org/info/rfc8200
1316
+ */
1317
+ class IPv6 extends AbstractIPNum {
1318
+ /**
1319
+ * Constructor for an IPv6 number.
1320
+ *
1321
+ * @param {string | bigint} ipValue value to construct an IPv6 from. The given value can either be
1322
+ * numeric or string. If a string is given then it needs to be in hexadecatet string notation
1323
+ */
1324
+ constructor(ipValue) {
1325
+ super();
1326
+ /**
1327
+ * The number of bits needed to represents the value of the IPv6 number
1328
+ */
1329
+ this.bitSize = 128;
1330
+ /**
1331
+ * The maximum bit size (i.e. binary value) of the IPv6 number in BigInt
1332
+ */
1333
+ this.maximumBitSize = Validator_1$2.Validator.ONE_HUNDRED_AND_TWENTY_EIGHT_BIT_SIZE;
1334
+ /**
1335
+ * The type of IP number. Value is one of the values of the {@link IPNumType} enum
1336
+ * @type {IPNumType} the type of IP number
1337
+ */
1338
+ this.type = "IPv6" /* IPv6 */;
1339
+ /**
1340
+ * An array of {@link Hexadecatet}'s
1341
+ *
1342
+ * @type {Array} the hexadecatet that makes up the IPv6 number
1343
+ */
1344
+ this.hexadecatet = [];
1345
+ /**
1346
+ * The string character used to separate the individual hexadecatet when the IPv6 is rendered as strings
1347
+ *
1348
+ * @type {string} The string character used to separate the individual hexadecatet when rendered as strings
1349
+ */
1350
+ this.separator = ":";
1351
+ if (typeof ipValue === "string") {
1352
+ let expandedIPv6 = IPv6Utils_1.expandIPv6Number(ipValue);
1353
+ let [value, hexadecatet] = this.constructFromHexadecimalDottedString(expandedIPv6);
1354
+ this.value = value;
1355
+ this.hexadecatet = hexadecatet;
1356
+ }
1357
+ else {
1358
+ let [value, hexadecatet] = this.constructFromBigIntValue(ipValue);
1359
+ this.value = value;
1360
+ this.hexadecatet = hexadecatet;
1361
+ }
1362
+ }
1363
+ /**
1364
+ * A convenience method for creating an {@link IPv6} by providing the decimal value of the IP number in BigInt
1365
+ *
1366
+ * @param {bigint} bigIntValue the decimal value of the IP number in BigInt
1367
+ * @returns {IPv6} the IPv6 instance
1368
+ */
1369
+ static fromBigInt(bigIntValue) {
1370
+ return new IPv6(bigIntValue);
1371
+ }
1372
+ /**
1373
+ * A convenience method for creating an {@link IPv6} by providing the IP number in hexadecatet notation. E.g
1374
+ * "2001:800:0:0:0:0:0:2002"
1375
+ *
1376
+ * {@see https://en.wikipedia.org/wiki/IPv6_address#Representation} for more information on hexadecatet notation.
1377
+ *
1378
+ * @param {string} ipString the IP number in hexadecatet
1379
+ * @returns {IPv6} the IPv6 instance
1380
+ */
1381
+ static fromHexadecatet(ipString) {
1382
+ return new IPv6(ipString);
1383
+ }
1384
+ /**
1385
+ * Alias for IPv6.fromHexadecimalString
1386
+ *
1387
+ * @param {string} ipString the IP number in hexadecatet
1388
+ * @returns {IPv6} the IPv6 instance
1389
+ */
1390
+ static fromString(ipString) {
1391
+ return IPv6.fromHexadecatet(ipString);
1392
+ }
1393
+ /**
1394
+ * A convenience method for creating an {@link IPv6} from binary string
1395
+ *
1396
+ * @param {string} ipBinaryString the binary string representing the IPv6 number to be created
1397
+ * @returns {IPv6} the IPv6 instance
1398
+ */
1399
+ static fromBinaryString(ipBinaryString) {
1400
+ let validationResult = Validator_1$2.Validator.isValidBinaryString(ipBinaryString);
1401
+ if (validationResult[0]) {
1402
+ let paddedBinaryString = BinaryUtils_3.leftPadWithZeroBit(ipBinaryString, 128);
1403
+ return new IPv6(BinaryUtils_2.parseBinaryStringToBigInt(paddedBinaryString));
1404
+ }
1405
+ else {
1406
+ throw Error(validationResult[1].join(','));
1407
+ }
1408
+ }
1409
+ /**
1410
+ * A convenience method for creating an IPv4-Compatible {@link IPv6} Address from an instance of {@link IPv4}
1411
+ *
1412
+ * @param {IPv4} ipv4 to create an IPv4-Compatible {@link IPv6} Address
1413
+ * @returns {IPv6} the IPv4-Compatible {@link IPv6} Address
1414
+ */
1415
+ static fromIPv4(ipv4) {
1416
+ return ipv4.toIPv4MappedIPv6();
1417
+ }
1418
+ /**
1419
+ * A convenience method for creating an IPv4-Compatible {@link IPv6} Address from a IPv4 represented in
1420
+ * dot-decimal notation i.e. 127.0.0.1
1421
+ *
1422
+ * @param {IPv4} ip4DotDecimalString string represented in a dot decimal string
1423
+ * @returns {IPv6} the IPv4-Compatible {@link IPv6} Address
1424
+ */
1425
+ static fromIPv4DotDecimalString(ip4DotDecimalString) {
1426
+ return new IPv4(ip4DotDecimalString).toIPv4MappedIPv6();
1427
+ }
1428
+ /**
1429
+ * A string representation of the IPv6 number.
1430
+ *
1431
+ * @returns {string} The string representation of IPv6
1432
+ */
1433
+ toString() {
1434
+ let ipv6String = this.hexadecatet.map((value) => { return value.toString(); }).join(":");
1435
+ if (this.hexadecatet.length < 8) {
1436
+ return "::" + ipv6String;
1437
+ }
1438
+ else {
1439
+ return ipv6String;
1440
+ }
1441
+ }
1442
+ /**
1443
+ * Gets the individual {@link Hexadecatet} that makes up the IPv6 number
1444
+ *
1445
+ * @returns {Array<Hexadecatet>} The individual {@link Hexadecatet} that makes up the IPv6 number
1446
+ */
1447
+ //TODO maybe rename to something like getSegments? so it can be same with getOctet
1448
+ getHexadecatet() {
1449
+ return this.hexadecatet;
1450
+ }
1451
+ /**
1452
+ * Returns the next IPv6 number
1453
+ *
1454
+ * @returns {IPv6} the next IPv6 number
1455
+ */
1456
+ nextIPNumber() {
1457
+ return IPv6.fromBigInt(this.getValue() + 1n);
1458
+ }
1459
+ /**
1460
+ * Returns the previous IPv6 number
1461
+ *
1462
+ * @returns {IPv6} the previous IPv6 number
1463
+ */
1464
+ previousIPNumber() {
1465
+ return IPv6.fromBigInt(this.getValue() - 1n);
1466
+ }
1467
+ constructFromBigIntValue(ipv6Number) {
1468
+ let [isValid, message] = Validator_1$2.Validator.isValidIPv6Number(ipv6Number);
1469
+ if (!isValid) {
1470
+ throw new Error(message.filter(msg => { return msg !== ''; }).toString());
1471
+ }
1472
+ let binaryString = BinaryUtils_4.numberToBinaryString(ipv6Number);
1473
+ return [ipv6Number, this.binaryStringToHexadecatets(binaryString)];
1474
+ }
1475
+ constructFromHexadecimalDottedString(expandedIPv6) {
1476
+ let [isValid, message] = Validator_1$2.Validator.isValidIPv6String(expandedIPv6);
1477
+ if (!isValid) {
1478
+ throw new Error(message.filter(msg => { return msg !== ''; }).toString());
1479
+ }
1480
+ let stringHexadecimals = expandedIPv6.split(":");
1481
+ let hexadecatet = stringHexadecimals.map((stringHexadecatet) => {
1482
+ return Hexadecatet_1$1.Hexadecatet.fromString(stringHexadecatet);
1483
+ });
1484
+ let value = BigInt(`0b${HexadecimalUtils_2.hexadectetNotationToBinaryString(expandedIPv6)}`);
1485
+ return [value, hexadecatet];
1486
+ }
1487
+ binaryStringToHexadecatets(binaryString) {
1488
+ let hexadecimalString = HexadecimalUtils_1$1.binaryStringToHexadecimalString(binaryString);
1489
+ while (hexadecimalString.length % 4 != 0) {
1490
+ hexadecimalString = '0' + hexadecimalString;
1491
+ }
1492
+ let hexadecimalStrings = hexadecimalString.match(/.{1,4}/g);
1493
+ return hexadecimalStrings.map((stringHexadecatet) => {
1494
+ return Hexadecatet_1$1.Hexadecatet.fromString(stringHexadecatet);
1495
+ });
1496
+ }
1497
+ }
1498
+ IPNumber.IPv6 = IPv6;
1499
+ /**
1500
+ * The IPv4Mask can be seen as a specialized IPv4 number where, in a 32 bit number, starting from the left, you
1501
+ * have continuous bits turned on (with 1 value) followed by bits turned off (with 0 value). In networking, it is used
1502
+ * to demarcate which bits are used to identify a network, and the ones that are used to identify hosts on the network
1503
+ */
1504
+ class IPv4Mask extends IPv4 {
1505
+ /**
1506
+ * Constructor for creating an instance of IPv4Mask.
1507
+ * The passed strings need to be a valid IPv4 mask number in dot-decimal notation.
1508
+ *
1509
+ * @param {string} ipString The passed string in dot-decimal notation
1510
+ */
1511
+ constructor(ipString) {
1512
+ super(ipString);
1513
+ /**
1514
+ * An array of {@link Octet}'s
1515
+ *
1516
+ * @type {Array} the octets that makes up the IPv4Mask
1517
+ */
1518
+ this.octets = [];
1519
+ let isValid;
1520
+ let message;
1521
+ [isValid, message] = Validator_1$2.Validator.isValidIPv4Mask(ipString);
1522
+ if (!isValid) {
1523
+ throw new Error(message.filter(msg => { return msg !== ''; }).toString());
1524
+ }
1525
+ let stringOctets = ipString.split(".");
1526
+ this.octets = stringOctets.map((rawOctet) => {
1527
+ return Octet_1.Octet.fromString(rawOctet);
1528
+ });
1529
+ let binaryString = BinaryUtils_1$2.dottedDecimalNotationToBinaryString(ipString);
1530
+ this.prefix = (binaryString.match(/1/g) || []).length;
1531
+ this.value = BigInt(`0b${binaryString}`);
1532
+ }
1533
+ /**
1534
+ * A convenience method for creating an instance of IPv4Mask. The passed strings need to be a valid IPv4
1535
+ * number in dot-decimal notation.
1536
+ *
1537
+ * @param {string} rawValue The passed string in dot-decimal notation
1538
+ * @returns {IPv4Mask} the instance of IPv4Mask
1539
+ */
1540
+ static fromDecimalDottedString(rawValue) {
1541
+ return new IPv4Mask(rawValue);
1542
+ }
1543
+ ;
1544
+ }
1545
+ IPNumber.IPv4Mask = IPv4Mask;
1546
+ /**
1547
+ * The IPv6Mask can be seen as a specialized IPv4 number where, in a 128 bit number, starting from the left,
1548
+ * you have continuous bits turned on (with 1 value) followed by bits turned off (with 0 value). In networking, it
1549
+ * is used to demarcate which bits are used to identify a network, and the ones that are used to identify hosts
1550
+ * on the network
1551
+ */
1552
+ class IPv6Mask extends IPv6 {
1553
+ /**
1554
+ * Constructor for creating an instance of IPv6Mask.
1555
+ * The passed strings need to be a valid IPv6 mask number in dot-decimal notation
1556
+ *
1557
+ * @param {string} ipString The passed IPv6 string
1558
+ */
1559
+ constructor(ipString) {
1560
+ super(ipString);
1561
+ /**
1562
+ * An array of {@link Hexadecatet}'s
1563
+ *
1564
+ * @type {Array} the hexadecatet that makes up the IPv6 number
1565
+ */
1566
+ this.hexadecatet = [];
1567
+ let isValid;
1568
+ let message;
1569
+ let expandedIPv6 = IPv6Utils_1.expandIPv6Number(ipString);
1570
+ [isValid, message] = Validator_1$2.Validator.isValidIPv6Mask(expandedIPv6);
1571
+ if (!isValid) {
1572
+ throw new Error(message.filter(msg => { return msg !== ''; }).toString());
1573
+ }
1574
+ let stringHexadecimals = expandedIPv6.split(":");
1575
+ this.hexadecatet = stringHexadecimals.map((stringHexadecatet) => {
1576
+ return Hexadecatet_1$1.Hexadecatet.fromString(stringHexadecatet);
1577
+ });
1578
+ let binaryString = HexadecimalUtils_2.hexadectetNotationToBinaryString(expandedIPv6);
1579
+ this.prefix = (binaryString.match(/1/g) || []).length;
1580
+ this.value = BigInt(`0b${binaryString}`);
1581
+ this.value = BigInt(`0b${HexadecimalUtils_2.hexadectetNotationToBinaryString(expandedIPv6)}`);
1582
+ }
1583
+ /**
1584
+ * A convenience method for creating an instance of IPv6Mask.
1585
+ * The passed strings need to be a valid IPv4 mask number in dot-decimal notation.
1586
+ *
1587
+ * @param {string} rawValue The passed string in textual notation
1588
+ * @returns {IPv6Mask} the instance of IPv6Mask
1589
+ */
1590
+ static fromHexadecatet(rawValue) {
1591
+ return new IPv6Mask(rawValue);
1592
+ }
1593
+ ;
1594
+ }
1595
+ IPNumber.IPv6Mask = IPv6Mask;
1596
+ /**
1597
+ * Check is the given IP number is an {@link IPv4} or not
1598
+ * @param ip the IP number to check if it is IPv4.
1599
+ */
1600
+ function isIPv4(ip) {
1601
+ return ip.bitSize === 32;
1602
+ }
1603
+ IPNumber.isIPv4 = isIPv4;
1604
+
1605
+ var IPNumType = {};
1606
+
1607
+ Object.defineProperty(IPNumType, "__esModule", { value: true });
1608
+
1609
+ var IPPool = {};
1610
+
1611
+ var IPRange = {};
1612
+
1613
+ var Prefix = {};
1614
+
1615
+ Object.defineProperty(Prefix, "__esModule", { value: true });
1616
+ Prefix.isIPv4Prefix = Prefix.IPv6Prefix = Prefix.IPv4Prefix = void 0;
1617
+ const Validator_1$1 = requireValidator();
1618
+ const IPNumber_1$1 = IPNumber;
1619
+ const BinaryUtils_1$1 = BinaryUtils;
1620
+ const HexadecimalUtils_1 = requireHexadecimalUtils();
1621
+ const Hexadecatet_1 = Hexadecatet$1;
1622
+ /**
1623
+ * Represents the prefix portion in the CIDR notation for representing IP ranges
1624
+ *
1625
+ * The IPv4 prefix portion represents the mask. It is the number of continuous bits turned on (with value 1)
1626
+ * counting from the left side of an 8 bit value.
1627
+ *
1628
+ * {@see https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing} for more information on CIDR
1629
+ */
1630
+ class IPv4Prefix {
1631
+ /**
1632
+ * Constructor for an instance of IPv4 prefix from a decimal number
1633
+ *
1634
+ * @param {number} rawValue the decimal value to construct the IPv4 prefix from.
1635
+ * @returns {IPv4Prefix} the instance of an IPv4 prefix
1636
+ */
1637
+ constructor(rawValue) {
1638
+ this.type = "IPv4";
1639
+ this.bitValue = 32n;
1640
+ let isValid;
1641
+ let message;
1642
+ [isValid, message] = Validator_1$1.Validator.isValidPrefixValue(rawValue, "IPv4" /* IPv4 */);
1643
+ if (!isValid) {
1644
+ throw new Error(message.filter(msg => { return msg !== ''; }).toString());
1645
+ }
1646
+ this.value = rawValue;
1647
+ }
1648
+ /**
1649
+ * Convenience method for constructing an instance of IPv4 prefix from a decimal number
1650
+ *
1651
+ * @param {number} rawValue the decimal value to construct the IPv4 prefix from.
1652
+ * @returns {IPv4Prefix} the instance of an IPv4 prefix
1653
+ */
1654
+ static fromNumber(rawValue) {
1655
+ return new IPv4Prefix(rawValue);
1656
+ }
1657
+ ;
1658
+ static fromRangeSize(rangeSize) {
1659
+ let prefixNumber = rangeSize === (1n) ? 32 : 32 - rangeSizeToPrefix(rangeSize, Validator_1$1.Validator.IPV4_SIZE);
1660
+ return IPv4Prefix.fromNumber(BigInt(prefixNumber));
1661
+ }
1662
+ ;
1663
+ /**
1664
+ * Gets the decimal value of the IPv4 prefix
1665
+ *
1666
+ * @returns {number} the decimal value of the IPv4 prefix
1667
+ */
1668
+ getValue() {
1669
+ return this.value;
1670
+ }
1671
+ /**
1672
+ * Gets the decimal value of the IPv4 prefix as string
1673
+ * @returns {string} The decimal value of the IPv4 prefix as string
1674
+ */
1675
+ toString() {
1676
+ return this.value.toString();
1677
+ }
1678
+ /**
1679
+ * Converts the IPv4 prefix to a {@link IPv4Mask}
1680
+ *
1681
+ * The IPv4 mask is the representation of the prefix in the dot-decimal notation
1682
+ *
1683
+ * @returns {IPv4Mask} the mask representation of the prefix
1684
+ */
1685
+ toMask() {
1686
+ let onBits = '1'.repeat(Number(this.value));
1687
+ let offBits = '0'.repeat(Number(32n - this.value));
1688
+ return IPNumber_1$1.IPv4Mask.fromDecimalDottedString(this.toDecimalNotation(`${onBits}${offBits}`));
1689
+ }
1690
+ /**
1691
+ * Returns the size (number of IP numbers) of range of this prefix
1692
+ *
1693
+ * @return {bigint} the size (number of IP numbers) of range of this prefix
1694
+ */
1695
+ toRangeSize() {
1696
+ /**
1697
+ * Using bitwise shift operation this will be
1698
+ * 1 << (this.bitValue - this.getValue())
1699
+ * Since left shift a number by x is equivalent to multiplying the number by the power x raised to 2
1700
+ * 2 << 4 = 2 * (2 raised to 4)
1701
+ */
1702
+ return 1n << (this.bitValue - this.getValue());
1703
+ }
1704
+ /**
1705
+ * Returns a prefix for when this prefix is merged
1706
+ * with another prefix of the same size
1707
+ */
1708
+ merge() {
1709
+ return new IPv4Prefix(this.value - 1n);
1710
+ }
1711
+ /**
1712
+ * Returns a prefix for when this prefix is split
1713
+ * into two equal halves
1714
+ */
1715
+ split() {
1716
+ return new IPv4Prefix(this.value + 1n);
1717
+ }
1718
+ toDecimalNotation(bits) {
1719
+ return `${BinaryUtils_1$1.parseBinaryStringToBigInt(bits.substr(0, 8))}.${BinaryUtils_1$1.parseBinaryStringToBigInt(bits.substr(8, 8))}.${BinaryUtils_1$1.parseBinaryStringToBigInt(bits.substr(16, 8))}.${BinaryUtils_1$1.parseBinaryStringToBigInt(bits.substr(24, 8))}`;
1720
+ }
1721
+ }
1722
+ Prefix.IPv4Prefix = IPv4Prefix;
1723
+ /**
1724
+ * Represents the prefix portion in the CIDR notation for representing IP ranges
1725
+ *
1726
+ * The IPv6 prefix portion represents the mask. It is the number of continuous bits turned on (with value 1)
1727
+ * counting from the left side of an 128 bit value.
1728
+ *
1729
+ * {@see https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing} for more information on CIDR
1730
+ */
1731
+ class IPv6Prefix {
1732
+ /**
1733
+ * Constructor for an instance of IPv6 prefix from a decimal number
1734
+ *
1735
+ * @param {number} rawValue the decimal value to construct the IPv6 prefix from.
1736
+ * @returns {IPv4Prefix} the instance of an IPv6 prefix
1737
+ */
1738
+ constructor(rawValue) {
1739
+ this.type = "IPv6";
1740
+ this.bitValue = 128n;
1741
+ let isValid;
1742
+ let message;
1743
+ [isValid, message] = Validator_1$1.Validator.isValidPrefixValue(rawValue, "IPv6" /* IPv6 */);
1744
+ if (!isValid) {
1745
+ throw new Error(message.filter(msg => { return msg !== ''; }).toString());
1746
+ }
1747
+ this.value = rawValue;
1748
+ }
1749
+ /**
1750
+ * Convenience method for constructing an instance of IPv46 prefix from a decimal number
1751
+ *
1752
+ * @param {number} rawValue the decimal value to construct the IPv6 prefix from.
1753
+ * @returns {IPv4Prefix} the instance of an IPv6 prefix
1754
+ */
1755
+ static fromNumber(rawValue) {
1756
+ return new IPv6Prefix(rawValue);
1757
+ }
1758
+ ;
1759
+ static fromRangeSize(rangeSize) {
1760
+ let prefixNumber = rangeSize === (1n) ? 128 : 128 - rangeSizeToPrefix(rangeSize, Validator_1$1.Validator.IPV6_SIZE);
1761
+ return IPv6Prefix.fromNumber(BigInt(prefixNumber));
1762
+ }
1763
+ /**
1764
+ * Gets the decimal value of the IPv6 prefix
1765
+ *
1766
+ * @returns {number} the decimal value of the IPv6 prefix
1767
+ */
1768
+ getValue() {
1769
+ return this.value;
1770
+ }
1771
+ /**
1772
+ * Gets the decimal value of the IPv4 prefix as string
1773
+ * @returns {string} he decimal value of the IPv4 prefix as string
1774
+ */
1775
+ toString() {
1776
+ return this.value.toString();
1777
+ }
1778
+ /**
1779
+ * Converts the IPv6 prefix to a {@link IPv6Mask}
1780
+ *
1781
+ * The IPv6 mask is the representation of the prefix in 8 groups of 16 bit values represented in hexadecimal
1782
+ *
1783
+ * @returns {IPv6Mask} the mask representation of the prefix
1784
+ */
1785
+ toMask() {
1786
+ let onBits = '1'.repeat(Number(this.value));
1787
+ let offBits = '0'.repeat(128 - Number(this.value));
1788
+ return IPNumber_1$1.IPv6Mask.fromHexadecatet(this.toHexadecatetNotation(`${onBits}${offBits}`));
1789
+ }
1790
+ /**
1791
+ * Returns the size (number of IP numbers) of range of this prefix
1792
+ *
1793
+ * @return {bigint} the size (number of IP numbers) of range of this prefix
1794
+ */
1795
+ toRangeSize() {
1796
+ /**
1797
+ * Using bitwise shift operation this will be
1798
+ * 1 << (this.bitValue - this.getValue())
1799
+ * Since left shift a number by x is equivalent to multiplying the number by the power x raised to 2
1800
+ * 2 << 4 = 2 * (2 raised to 4)
1801
+ */
1802
+ return 1n << (this.bitValue - this.getValue());
1803
+ }
1804
+ /**
1805
+ * Returns a prefix for when this prefix is merged
1806
+ * with another prefix of the same size
1807
+ */
1808
+ merge() {
1809
+ return new IPv6Prefix(this.value - 1n);
1810
+ }
1811
+ /**
1812
+ * Returns a prefix for when this prefix is split
1813
+ * into two equal halves
1814
+ */
1815
+ split() {
1816
+ return new IPv6Prefix(this.value + 1n);
1817
+ }
1818
+ toHexadecatetNotation(bits) {
1819
+ let binaryStrings = bits.match(/.{1,16}/g);
1820
+ let hexadecimalStrings = binaryStrings.map((binaryString) => {
1821
+ return Hexadecatet_1.Hexadecatet.fromString(HexadecimalUtils_1.binaryStringToHexadecimalString(binaryString));
1822
+ });
1823
+ return hexadecimalStrings.map((value) => { return value.toString(); }).join(":");
1824
+ }
1825
+ }
1826
+ Prefix.IPv6Prefix = IPv6Prefix;
1827
+ function rangeSizeToPrefix(rangeSize, rangeMaxSize) {
1828
+ let ipType = rangeMaxSize > (Validator_1$1.Validator.IPV4_SIZE) ? "IPv6" : "IPv4";
1829
+ if (rangeSize > (rangeMaxSize) || rangeSize === (0n)) {
1830
+ throw new Error(Validator_1$1.Validator.invalidIPRangeSizeMessage.replace("$iptype", ipType));
1831
+ }
1832
+ try {
1833
+ return BinaryUtils_1$1.intLog2(rangeSize);
1834
+ }
1835
+ catch (e) {
1836
+ throw new Error(Validator_1$1.Validator.invalidIPRangeSizeForCidrMessage);
1837
+ }
1838
+ }
1839
+ /**
1840
+ * Check is the given Prefix is an {@link IPv4Prefix} or not
1841
+ * @param prefix the IP prefix to check if it is IPv4Prefix.
1842
+ */
1843
+ function isIPv4Prefix(prefix) {
1844
+ return prefix.type === "IPv4";
1845
+ }
1846
+ Prefix.isIPv4Prefix = isIPv4Prefix;
1847
+
1848
+ Object.defineProperty(IPRange, "__esModule", { value: true });
1849
+ IPRange.isIPv4CidrRange = IPRange.IPv6CidrRange = IPRange.IPv4CidrRange = IPRange.AbstractIPRange = IPRange.RangedSet = void 0;
1850
+ const IPNumber_1 = IPNumber;
1851
+ const Prefix_1$1 = Prefix;
1852
+ const BinaryUtils_1 = BinaryUtils;
1853
+ const Validator_1 = requireValidator();
1854
+ /**
1855
+ * Represents a continuous segment of either IPv4 or IPv6 numbers
1856
+ * without adhering to classless inter-domain routing scheme
1857
+ * for allocating IP addresses.
1858
+ */
1859
+ class RangedSet {
1860
+ /**
1861
+ * Constructor for an instance of {@link RangedSet} from an
1862
+ * instance of either {@link IPv4CidrRange} or {@link IPv6CidrRange}
1863
+ *
1864
+ * Throws an exception if first IP number is not less than given last IP number
1865
+ *
1866
+ * @param first the first IP number of the range
1867
+ * @param last the last IP number of the range
1868
+ */
1869
+ constructor(first, last) {
1870
+ this.first = first;
1871
+ this.last = last;
1872
+ if (first.isGreaterThan(last)) {
1873
+ throw new Error(`${first.toString()} should be lower than ${last.toString()}`);
1874
+ }
1875
+ this.currentValue = first;
1876
+ this.bitValue = BigInt(first.bitSize);
1877
+ }
1878
+ /**
1879
+ * Convenience method for constructing an instance of {@link RangedSet} from a
1880
+ * single IP number.
1881
+ *
1882
+ * @param ip The IP number, either IPv4 or IPv6 to construct the range from.
1883
+ */
1884
+ static fromSingleIP(ip) {
1885
+ return new RangedSet(ip, ip);
1886
+ }
1887
+ /**
1888
+ * Convenience method for constructing an instance of {@link RangedSet} from an
1889
+ * instance of either {@link IPv4CidrRange} or {@link IPv6CidrRange}
1890
+ *
1891
+ * @param cidrRange an instance of {@link RangedSet}
1892
+ */
1893
+ static fromCidrRange(cidrRange) {
1894
+ return new RangedSet(cidrRange.getFirst(), cidrRange.getLast());
1895
+ }
1896
+ /**
1897
+ * Convenience method for constructing an instance of {@link RangedSet} from
1898
+ * a range string in the form of firstIp-lastIp
1899
+ *
1900
+ * @param rangeString string in the form of firstIp-lastIp
1901
+ */
1902
+ static fromRangeString(rangeString) {
1903
+ let ips = rangeString.split("-").map(ip => ip.trim());
1904
+ if (ips.length !== 2) {
1905
+ throw new Error("Argument should be in the format firstip-lastip");
1906
+ }
1907
+ let [firstIPString, lastIPString] = ips;
1908
+ let [isValidFirstIPv4,] = Validator_1.Validator.isValidIPv4String(firstIPString);
1909
+ let [isValidSecondIPv4,] = Validator_1.Validator.isValidIPv4String(lastIPString);
1910
+ let [isValidFirstIPv6,] = Validator_1.Validator.isValidIPv6String(firstIPString);
1911
+ let [isValidLastIPv6,] = Validator_1.Validator.isValidIPv6String(lastIPString);
1912
+ if (isValidFirstIPv4 && isValidSecondIPv4) {
1913
+ return new RangedSet(IPNumber_1.IPv4.fromDecimalDottedString(firstIPString), IPNumber_1.IPv4.fromDecimalDottedString(lastIPString));
1914
+ }
1915
+ else if (isValidFirstIPv6 && isValidLastIPv6) {
1916
+ return new RangedSet(IPNumber_1.IPv6.fromHexadecatet(firstIPString), IPNumber_1.IPv6.fromHexadecatet(lastIPString));
1917
+ }
1918
+ else {
1919
+ throw new Error("First IP and Last IP should be valid and same type");
1920
+ }
1921
+ }
1922
+ /**
1923
+ * Returns the first IP number in the range
1924
+ */
1925
+ getFirst() {
1926
+ return this.first;
1927
+ }
1928
+ /**
1929
+ * Returns the last IP number in the range
1930
+ */
1931
+ getLast() {
1932
+ return this.last;
1933
+ }
1934
+ /**
1935
+ * Returns the size, which is the number of IP numbers in the range.
1936
+ */
1937
+ getSize() {
1938
+ return this.last.getValue() - (this.first.getValue()) + 1n;
1939
+ }
1940
+ /**
1941
+ * Converts to a string representation of the range in the form of:
1942
+ * <first-ip>-<last-ip>
1943
+ */
1944
+ toRangeString() {
1945
+ return `${this.getFirst()}-${this.getLast()}`;
1946
+ }
1947
+ /**
1948
+ * Checks if this range is inside another range.
1949
+ *
1950
+ * @param otherRange the other range to check if this range is inside of.
1951
+ */
1952
+ inside(otherRange) {
1953
+ return otherRange.contains(this);
1954
+ }
1955
+ /**
1956
+ * Checks if this range contains the given other range.
1957
+ *
1958
+ * @param otherRange the other range to check if this range contains
1959
+ */
1960
+ contains(otherRange) {
1961
+ let thisFirst = this.getFirst();
1962
+ let thisLast = this.getLast();
1963
+ let otherFirst = otherRange.getFirst();
1964
+ let otherLast = otherRange.getLast();
1965
+ return (thisFirst.isLessThanOrEquals(otherFirst) && thisLast.isGreaterThanOrEquals(otherLast));
1966
+ }
1967
+ /**
1968
+ * Check if this range is equal to the given other range.
1969
+ *
1970
+ * @param otherRange the other range to check if equal to this range.
1971
+ */
1972
+ isEquals(otherRange) {
1973
+ return this.getFirst().isEquals(otherRange.getFirst())
1974
+ && this.getLast().isEquals(otherRange.getLast());
1975
+ }
1976
+ ;
1977
+ /**
1978
+ * Check if this range is less than the given range.
1979
+ *
1980
+ * @param otherRange the other range to check if less than.
1981
+ */
1982
+ isLessThan(otherRange) {
1983
+ if (this.isEquals(otherRange)) {
1984
+ return false;
1985
+ }
1986
+ else {
1987
+ if (this.getFirst().isEquals(otherRange.getFirst())) {
1988
+ return this.getSize() < (otherRange.getSize());
1989
+ }
1990
+ return this.getFirst().isLessThan(otherRange.getFirst());
1991
+ }
1992
+ }
1993
+ /**
1994
+ * Check if this range is greater than the given range.
1995
+ *
1996
+ * @param otherRange the other range to check if greater than.
1997
+ */
1998
+ isGreaterThan(otherRange) {
1999
+ if (this.isEquals(otherRange)) {
2000
+ return false;
2001
+ }
2002
+ else {
2003
+ if (this.getFirst().isEquals(otherRange.getFirst())) {
2004
+ return this.getSize() > (otherRange.getSize());
2005
+ }
2006
+ return this.getFirst().isGreaterThan(otherRange.getFirst());
2007
+ }
2008
+ }
2009
+ /**
2010
+ * Checks of this range overlaps with a given other range.
2011
+ *
2012
+ * This means it checks if part of a range is part of another range without
2013
+ * being totally contained in the other range. Hence Equal or ranges contained inside one
2014
+ * another are not considered as overlapping.
2015
+ *
2016
+ * @param otherRange the other range to check if it overlaps with this range.
2017
+ */
2018
+ isOverlapping(otherRange) {
2019
+ let thisFirst = this.getFirst();
2020
+ let thisLast = this.getLast();
2021
+ let otherFirst = otherRange.getFirst();
2022
+ let otherLast = otherRange.getLast();
2023
+ return (thisLast.isGreaterThan(otherFirst) && thisLast.isLessThanOrEquals(otherLast) && thisFirst.isLessThan(otherFirst)
2024
+ ||
2025
+ otherLast.isGreaterThan(thisFirst) && otherLast.isLessThanOrEquals(thisLast) && otherFirst.isLessThan(thisFirst));
2026
+ }
2027
+ /**
2028
+ * Check if this range can be converted to a CIDR range.
2029
+ */
2030
+ isCidrAble() {
2031
+ if (this.getSize() === 1n) {
2032
+ return true;
2033
+ }
2034
+ try {
2035
+ let prefix = BinaryUtils_1.intLog2(this.getSize());
2036
+ let netmask = BinaryUtils_1.parseBinaryStringToBigInt(BinaryUtils_1.cidrPrefixToMaskBinaryString(prefix, IPNumber_1.isIPv4(this.currentValue) ? "IPv4" /* IPv4 */ : "IPv6" /* IPv6 */));
2037
+ return (this.first.getValue()) === (netmask & (this.first.getValue()));
2038
+ }
2039
+ catch (e) {
2040
+ return false;
2041
+ }
2042
+ }
2043
+ /**
2044
+ * Converts an instance of range to an instance of CIDR range
2045
+ */
2046
+ toCidrRange() {
2047
+ if (IPNumber_1.isIPv4(this.currentValue)) {
2048
+ return this.toIPv4CidrRange();
2049
+ }
2050
+ else {
2051
+ return this.toIPv6CidrRange();
2052
+ }
2053
+ }
2054
+ ;
2055
+ /**
2056
+ * Checks if this range is consecutive with another range.
2057
+ *
2058
+ * This means if the two ranges can be placed side by side, without any gap. Hence Equal
2059
+ * or ranges contained inside one another, or overlapping ranges are not considered as consecutive.
2060
+ *
2061
+ * @param otherRange the other range to check if this range is consecutive to.
2062
+ */
2063
+ isConsecutive(otherRange) {
2064
+ let thisFirst = this.getFirst();
2065
+ let thisLast = this.getLast();
2066
+ let otherFirst = otherRange.getFirst();
2067
+ let otherLast = otherRange.getLast();
2068
+ return (thisLast.hasNext() && thisLast.nextIPNumber().isEquals(otherFirst)
2069
+ ||
2070
+ otherLast.hasNext() && otherLast.nextIPNumber().isEquals(thisFirst));
2071
+ }
2072
+ /**
2073
+ * Creates a range that is a union of this range and the given other range.
2074
+ *
2075
+ * @param otherRange the other range to combine with this range
2076
+ */
2077
+ union(otherRange) {
2078
+ if (this.isEquals(otherRange)) {
2079
+ return new RangedSet(otherRange.getFirst(), otherRange.getLast());
2080
+ }
2081
+ if (this.isOverlapping(otherRange)) {
2082
+ if (this.getFirst().isLessThan(otherRange.getFirst())) {
2083
+ return new RangedSet(this.getFirst(), otherRange.getLast());
2084
+ }
2085
+ else {
2086
+ return new RangedSet(otherRange.getFirst(), this.getLast());
2087
+ }
2088
+ }
2089
+ if (this.contains(otherRange)) {
2090
+ return new RangedSet(this.getFirst(), this.getLast());
2091
+ }
2092
+ else if (otherRange.contains(this)) {
2093
+ return new RangedSet(otherRange.getFirst(), otherRange.getLast());
2094
+ }
2095
+ throw new Error("Ranges do not overlap nor are equal");
2096
+ }
2097
+ /**
2098
+ * Prepends given range with this range.
2099
+ * The last IP in the given range should be adjacent to the first IP in this range
2100
+ *
2101
+ * @param otherRange the other range to prepend
2102
+ */
2103
+ prepend(otherRange) {
2104
+ if (otherRange.getLast().nextIPNumber().isEquals(this.getFirst())) {
2105
+ return new RangedSet(otherRange.getFirst(), this.getLast());
2106
+ }
2107
+ else {
2108
+ throw new Error("Range to prepend must be adjacent");
2109
+ }
2110
+ }
2111
+ /**
2112
+ * Appends given range with this range.
2113
+ * The last IP in this range should be adjacent to the first IP in range to append
2114
+ *
2115
+ * @param otherRange the other range to append
2116
+ */
2117
+ append(otherRange) {
2118
+ if (this.getLast().nextIPNumber().isEquals(otherRange.getFirst())) {
2119
+ return new RangedSet(this.getFirst(), otherRange.getLast());
2120
+ }
2121
+ else {
2122
+ throw new Error("Range to append must be adjacent");
2123
+ }
2124
+ }
2125
+ subtract(otherRange) {
2126
+ if (!this.isOverlapping(otherRange)) {
2127
+ throw new Error("Cannot subtract ranges that are not overlapping");
2128
+ }
2129
+ if (!this.isLessThan(otherRange)) {
2130
+ throw new Error("Cannot subtract a larger range from this range");
2131
+ }
2132
+ return new RangedSet(this.getFirst(), otherRange.getLast());
2133
+ }
2134
+ /**
2135
+ * Returns a sub range of a given size from this range.
2136
+ *
2137
+ * @param offset offset from this range where the subrange should begin
2138
+ * @param size the size of the range
2139
+ */
2140
+ takeSubRange(offset, size) {
2141
+ if (offset + (size) > (this.getSize())) {
2142
+ throw new RangeError("Requested range is greater than what can be taken");
2143
+ }
2144
+ if (size === (0n)) {
2145
+ throw new Error("Sub range cannot be zero");
2146
+ }
2147
+ let valueOfFirstIp = this.getFirst().value + (offset);
2148
+ let firstIp = IPNumber_1.isIPv4(this.getFirst()) ?
2149
+ IPNumber_1.IPv4.fromNumber(valueOfFirstIp) : IPNumber_1.IPv6.fromBigInt(valueOfFirstIp);
2150
+ let valueOfLastIp = firstIp.value + (size - 1n);
2151
+ let lastIp = IPNumber_1.isIPv4(firstIp) ? IPNumber_1.IPv4.fromNumber(valueOfLastIp) : IPNumber_1.IPv6.fromBigInt(valueOfLastIp);
2152
+ return new RangedSet(firstIp, lastIp);
2153
+ }
2154
+ /**
2155
+ * Performs a subtraction operation, where the passed range is removed from the original range.
2156
+ *
2157
+ * The return range from the subtraction operation could be a single or multiple ranges
2158
+ *
2159
+ * @param range
2160
+ */
2161
+ difference(range) {
2162
+ if (range.getSize() > (this.getSize())) {
2163
+ throw new Error("Range is greater than range to be subtracted from");
2164
+ }
2165
+ if (!this.contains(range)) {
2166
+ throw new Error("Range to subtract is not contained in this range");
2167
+ }
2168
+ let reminders = [];
2169
+ try {
2170
+ reminders.push(new RangedSet(this.getFirst(), range.getFirst().previousIPNumber()));
2171
+ }
2172
+ catch (e) { }
2173
+ try {
2174
+ reminders.push(new RangedSet(range.getLast().nextIPNumber(), this.getLast()));
2175
+ }
2176
+ catch (e) { }
2177
+ return reminders;
2178
+ }
2179
+ *take(count) {
2180
+ let computed = this.getFirst();
2181
+ let returnCount = count === undefined ? this.getSize().valueOf() : count;
2182
+ while (returnCount > 0) {
2183
+ returnCount--;
2184
+ yield computed;
2185
+ computed = computed.nextIPNumber();
2186
+ }
2187
+ }
2188
+ *[Symbol.iterator]() {
2189
+ yield* this.take();
2190
+ }
2191
+ toIPv4CidrRange() {
2192
+ let candidateRange = new IPv4CidrRange(IPNumber_1.IPv4.fromNumber(this.getFirst().getValue()), Prefix_1$1.IPv4Prefix.fromRangeSize(this.getSize()));
2193
+ if (candidateRange.getFirst().isEquals(this.getFirst())) {
2194
+ return candidateRange;
2195
+ }
2196
+ else {
2197
+ throw new Error("Range cannot be converted to CIDR");
2198
+ }
2199
+ }
2200
+ toIPv6CidrRange() {
2201
+ let candidateRange = new IPv6CidrRange(IPNumber_1.IPv6.fromBigInt(this.getFirst().getValue()), Prefix_1$1.IPv6Prefix.fromRangeSize(this.getSize()));
2202
+ if (candidateRange.getFirst().isEquals(this.getFirst())) {
2203
+ return candidateRange;
2204
+ }
2205
+ else {
2206
+ throw new Error("Range cannot be converted to CIDR");
2207
+ }
2208
+ }
2209
+ }
2210
+ IPRange.RangedSet = RangedSet;
2211
+ /**
2212
+ * Provides the implementation of functionality that are common to {@link IPv4CidrRange} and {@link IPv6CidrRange}
2213
+ */
2214
+ class AbstractIPRange {
2215
+ hasNextRange() {
2216
+ let sizeOfCurrentRange = this.getSize();
2217
+ return ((2n ** this.bitValue) - sizeOfCurrentRange) >= (this.getFirst().getValue() + (sizeOfCurrentRange));
2218
+ }
2219
+ hasPreviousRange() {
2220
+ return this.getSize() <= (this.getFirst().getValue());
2221
+ }
2222
+ toRangeSet() {
2223
+ return new RangedSet(this.getFirst(), this.getLast());
2224
+ }
2225
+ inside(otherRange) {
2226
+ return this.toRangeSet().inside(otherRange.toRangeSet());
2227
+ }
2228
+ contains(otherRange) {
2229
+ return this.toRangeSet().contains(otherRange.toRangeSet());
2230
+ }
2231
+ toRangeString() {
2232
+ return this.toRangeSet().toRangeString();
2233
+ }
2234
+ isOverlapping(otherRange) {
2235
+ return this.toRangeSet().isOverlapping(otherRange.toRangeSet());
2236
+ }
2237
+ isConsecutive(otherRange) {
2238
+ return this.toRangeSet().isConsecutive(otherRange.toRangeSet());
2239
+ }
2240
+ isCidrMergeable(otherRange) {
2241
+ const count = BigInt(BinaryUtils_1.matchingBitCount(this.getFirst().toBinaryString(), otherRange.getFirst().toBinaryString()));
2242
+ if (this.getPrefix().value - count !== 1n) {
2243
+ return false;
2244
+ }
2245
+ return this.isConsecutive(otherRange) && this.getSize() === (otherRange.getSize());
2246
+ }
2247
+ isMergeable(otherRange) {
2248
+ return this.isCidrMergeable(otherRange)
2249
+ || this.contains(otherRange)
2250
+ || this.inside(otherRange);
2251
+ }
2252
+ isEquals(otherRange) {
2253
+ return this.toRangeSet().isEquals(otherRange.toRangeSet());
2254
+ }
2255
+ merge(otherRange) {
2256
+ if (!this.isCidrMergeable(otherRange)) {
2257
+ throw new Error(`Cannot merge. Ranges (${this.toRangeString()},${otherRange.toRangeString()}) are not consecutive and/or of same size`);
2258
+ }
2259
+ return this.newInstance(this.getFirst(), this.getPrefix().merge());
2260
+ }
2261
+ /**
2262
+ * Returns a lazily evaluated representation of the IP range that produces IP numbers by either:
2263
+ *
2264
+ * - iterating over using the for of syntax
2265
+ * - converting to array using spread syntax
2266
+ * - or assigning values to variables using deconstruction
2267
+ *
2268
+ * @param count the number of IP numbers to lazily evaluate.
2269
+ * If none is given, the whole IP range is lazily returned.
2270
+ */
2271
+ *takeStream(count) {
2272
+ return this.toRangeSet().take(count);
2273
+ }
2274
+ *[Symbol.iterator]() {
2275
+ yield* this.toRangeSet();
2276
+ }
2277
+ }
2278
+ IPRange.AbstractIPRange = AbstractIPRange;
2279
+ /**
2280
+ * Represents a continuous segment of IPv4 numbers following the
2281
+ * classless inter-domain routing scheme for allocating IP addresses.
2282
+ *
2283
+ * @see https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
2284
+ */
2285
+ class IPv4CidrRange extends AbstractIPRange {
2286
+ /**
2287
+ * Constructor for creating an instance of an IPv4 range.
2288
+ *
2289
+ * The arguments taken by the constructor is inspired by the CIDR notation which basically consists of the IP
2290
+ * number and the prefix.
2291
+ *
2292
+ * @param {IPv4} ipv4 the IP number used to construct the range. By convention this is the first IP number in
2293
+ * the range, but it could also be any IP number within the range
2294
+ * @param {IPv4Prefix} cidrPrefix the prefix which is a representation of the number of bits used to mask the
2295
+ * given IP number in other to create the range
2296
+ */
2297
+ constructor(ipv4, cidrPrefix) {
2298
+ super();
2299
+ this.ipv4 = ipv4;
2300
+ this.cidrPrefix = cidrPrefix;
2301
+ this.bitValue = 32n;
2302
+ }
2303
+ /**
2304
+ * Convenience method for constructing an instance of an IPv4CidrRange from an IP range represented in CIDR notation
2305
+ *
2306
+ * @param {string} rangeIncidrNotation the range of the IPv4 number in CIDR notation
2307
+ * @returns {IPv4CidrRange} the IPv4CidrRange
2308
+ */
2309
+ static fromCidr(rangeIncidrNotation) {
2310
+ let [isValid, errorMessages] = Validator_1.Validator.isValidIPv4CidrNotation(rangeIncidrNotation);
2311
+ if (!isValid) {
2312
+ let messages = errorMessages.filter(message => { return message !== ''; });
2313
+ throw new Error(messages.join(' and '));
2314
+ }
2315
+ let cidrComponents = rangeIncidrNotation.split("/");
2316
+ let ipString = cidrComponents[0];
2317
+ let prefix = BigInt(parseInt(cidrComponents[1]));
2318
+ return new IPv4CidrRange(IPNumber_1.IPv4.fromDecimalDottedString(ipString), Prefix_1$1.IPv4Prefix.fromNumber(prefix));
2319
+ }
2320
+ /**
2321
+ * Gets the size of IPv4 numbers contained within the IPv4 range
2322
+ *
2323
+ * @returns {bigint} the amount of IPv4 numbers in the range
2324
+ */
2325
+ getSize() {
2326
+ return this.cidrPrefix.toRangeSize();
2327
+ }
2328
+ /**
2329
+ * Method that returns the IPv4 range in CIDR (Classless Inter-Domain Routing) notation.
2330
+ *
2331
+ * See {@link https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation} for more information
2332
+ * on the Classless Inter-Domain Routing notation
2333
+ *
2334
+ * @returns {string} the IPv4 range in CIDR (Classless Inter-Domain Routing) notation
2335
+ */
2336
+ toCidrString() {
2337
+ let first = this.ipv4.toString();
2338
+ return `${first.toString()}/${this.cidrPrefix.toString()}`;
2339
+ }
2340
+ /**
2341
+ * Method that returns the IPv4 range in string notation where the first IPv4 number and last IPv4 number are
2342
+ * separated by an hyphen. eg. 192.198.0.0-192.198.0.255
2343
+ *
2344
+ * @returns {string} the range in [first IPv4 number] - [last IPv4 number] format
2345
+ */
2346
+ toRangeString() {
2347
+ return super.toRangeString();
2348
+ }
2349
+ /**
2350
+ * Method that returns the first IPv4 number in the IPv4 range
2351
+ *
2352
+ * @returns {IPv4} the first IPv4 number in the IPv4 range
2353
+ */
2354
+ getFirst() {
2355
+ return IPNumber_1.IPv4.fromNumber(this.ipv4.getValue() & (this.cidrPrefix.toMask().getValue()));
2356
+ }
2357
+ /**
2358
+ * Method that returns the last IPv4 number in the IPv4 range
2359
+ *
2360
+ * @returns {IPv4} the last IPv4 number in the IPv4 range
2361
+ */
2362
+ getLast() {
2363
+ return last(this, this.ipv4);
2364
+ }
2365
+ newInstance(num, prefix) {
2366
+ return new IPv4CidrRange(num, prefix);
2367
+ }
2368
+ getPrefix() {
2369
+ return this.cidrPrefix;
2370
+ }
2371
+ /**
2372
+ * Indicates whether the given IPv4 range is an adjacent range.
2373
+ *
2374
+ * An adjacent range being one where the end of the given range, when incremented by one marks the start of the
2375
+ * other range. Or where the start of the given range, when decreased by one, marks the end of the other range
2376
+ *
2377
+ * @param {IPv4CidrRange} otherRange the other IPv4 range to compare with
2378
+ * @returns {boolean} true if the two IPv4 ranges are consecutive, false otherwise
2379
+ */
2380
+ isConsecutive(otherRange) {
2381
+ return super.isConsecutive(otherRange);
2382
+ }
2383
+ /**
2384
+ * Indicates if the given IPv4 range is a subset.
2385
+ *
2386
+ * By a subset range, it means all the values of the given range are contained by this IPv4 range
2387
+ *
2388
+ * @param {IPv4CidrRange} otherRange the other IPv4 range
2389
+ * @returns {boolean} true if the other Ipv4 range is a subset. False otherwise.
2390
+ */
2391
+ contains(otherRange) {
2392
+ return super.contains(otherRange);
2393
+ }
2394
+ /**
2395
+ * Indicate if the given range is a container range.
2396
+ *
2397
+ * By container range, it means all the IP number in this current range can be found within the given range.
2398
+ *
2399
+ * @param {IPv4CidrRange} otherRange he other IPv4 range
2400
+ * @returns {boolean} true if the other Ipv4 range is a container range. False otherwise.
2401
+ */
2402
+ inside(otherRange) {
2403
+ return super.inside(otherRange);
2404
+ }
2405
+ /**
2406
+ * Checks if two IPv4 ranges overlap
2407
+ * @param {IPv4CidrRange} otherRange the other IPv4 range
2408
+ * @returns {boolean} true if the ranges overlap, false otherwise
2409
+ */
2410
+ isOverlapping(otherRange) {
2411
+ return super.isOverlapping(otherRange);
2412
+ }
2413
+ /**
2414
+ * Method that takes IPv4 number from within an IPv4 range, starting from the first IPv4 number
2415
+ *
2416
+ * @param {bigint} count the amount of IPv4 number to take from the IPv4 range
2417
+ * @returns {Array<IPv4>} an array of IPv4 number, taken from the IPv4 range
2418
+ */
2419
+ take(count) {
2420
+ let ipv4s = [this.getFirst()];
2421
+ let iteratingIPv4 = this.getFirst();
2422
+ if (count > (this.getSize())) {
2423
+ let errMessage = Validator_1.Validator.takeOutOfRangeSizeMessage
2424
+ .replace("$count", count.toString())
2425
+ .replace("$size", this.getSize().toString());
2426
+ throw new Error(errMessage);
2427
+ }
2428
+ for (let counter = 0; counter < count - 1n; counter++) {
2429
+ ipv4s.push(iteratingIPv4.nextIPNumber());
2430
+ iteratingIPv4 = iteratingIPv4.nextIPNumber();
2431
+ }
2432
+ return ipv4s;
2433
+ }
2434
+ /**
2435
+ * Method that splits an IPv4 range into two halves
2436
+ *
2437
+ * @returns {Array<IPv4CidrRange>} An array of two {@link IPv4CidrRange}
2438
+ */
2439
+ split() {
2440
+ let prefixToSplit = this.cidrPrefix.getValue();
2441
+ if (prefixToSplit === 32n) {
2442
+ throw new Error("Cannot split an IP range with a single IP number");
2443
+ }
2444
+ let splitCidr = Prefix_1$1.IPv4Prefix.fromNumber(prefixToSplit + 1n);
2445
+ let firstIPOfFirstRange = this.getFirst();
2446
+ let firstRange = new IPv4CidrRange(firstIPOfFirstRange, splitCidr);
2447
+ let firstIPOfSecondRange = firstRange.getLast().nextIPNumber();
2448
+ let secondRange = new IPv4CidrRange(firstIPOfSecondRange, splitCidr);
2449
+ return [firstRange, secondRange];
2450
+ }
2451
+ /**
2452
+ * Method that split prefix into ranges of the given prefix,
2453
+ * throws an exception if the size of the given prefix is larger than target prefix
2454
+ *
2455
+ * @param prefix the prefix to use to split
2456
+ * @returns {Array<IPv4CidrRange>} An array of two {@link IPv4CidrRange}
2457
+ */
2458
+ splitInto(prefix) {
2459
+ let splitCount = prefix.getValue() - this.cidrPrefix.getValue();
2460
+ if (splitCount < 0) {
2461
+ throw new Error("Prefix to split into is larger than source prefix");
2462
+ }
2463
+ else if (splitCount === 0n) {
2464
+ return [new IPv4CidrRange(this.getFirst(), prefix)];
2465
+ }
2466
+ else if (splitCount === 1n) {
2467
+ return this.split();
2468
+ }
2469
+ else {
2470
+ let results = this.split();
2471
+ while (splitCount > 1) {
2472
+ results = results.flatMap(result => result.split());
2473
+ splitCount = splitCount - 1n;
2474
+ }
2475
+ return results;
2476
+ }
2477
+ }
2478
+ /**
2479
+ * Returns true if there is an adjacent IPv4 cidr range of exactly the same size next to this range
2480
+ */
2481
+ hasNextRange() {
2482
+ return super.hasNextRange();
2483
+ }
2484
+ /**
2485
+ * Returns true if there is an adjacent IPv4 cidr range of exactly the same size previous to this range
2486
+ */
2487
+ hasPreviousRange() {
2488
+ return super.hasPreviousRange();
2489
+ }
2490
+ /**
2491
+ * Return the next IPv6 cidr range, or undefined if no next range
2492
+ */
2493
+ nextRange() {
2494
+ if (this.hasNextRange()) {
2495
+ let sizeOfCurrentRange = this.getSize();
2496
+ let startOfNextRange = this.getFirst().getValue() + (sizeOfCurrentRange);
2497
+ return new IPv4CidrRange(new IPNumber_1.IPv4(startOfNextRange), this.cidrPrefix);
2498
+ }
2499
+ return;
2500
+ }
2501
+ /**
2502
+ * Return the previous IPv6 cidr range, or undefined if no next range
2503
+ */
2504
+ previousRange() {
2505
+ if (this.hasPreviousRange()) {
2506
+ let sizeOfCurrentRange = this.getSize();
2507
+ let startOfPreviousRange = this.getFirst().getValue() - (sizeOfCurrentRange);
2508
+ return new IPv4CidrRange(new IPNumber_1.IPv4(startOfPreviousRange), this.cidrPrefix);
2509
+ }
2510
+ return;
2511
+ }
2512
+ }
2513
+ IPRange.IPv4CidrRange = IPv4CidrRange;
2514
+ /**
2515
+ * Represents a continuous segment of IPv6 number following the
2516
+ * classless inter-domain routing scheme for allocating IP addresses.
2517
+ *
2518
+ * @see https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
2519
+ */
2520
+ class IPv6CidrRange extends AbstractIPRange {
2521
+ /**
2522
+ * Constructor for creating an instance of an IPv6 range.
2523
+ *
2524
+ * The arguments taken by the constructor is inspired by the CIDR notation which basically consists of the IP
2525
+ * number and the prefix.
2526
+ *
2527
+ * @param {IPv6} ipv6 the IP number used to construct the range. By convention this is the first IP number in
2528
+ * the range, but it could also be any IP number within the range
2529
+ * @param {IPv6Prefix} cidrPrefix the prefix which is a representation of the number of bits used to mask the
2530
+ * given IPv6 number in other to create the range
2531
+ */
2532
+ constructor(ipv6, cidrPrefix) {
2533
+ super();
2534
+ this.ipv6 = ipv6;
2535
+ this.cidrPrefix = cidrPrefix;
2536
+ this.bitValue = 128n;
2537
+ }
2538
+ /**
2539
+ * Convenience method for constructing an instance of an IPV6Range from an IP range represented in CIDR notation
2540
+ *
2541
+ * @param {string} rangeInCidrNotation the range of the IPv6 number in CIDR notation
2542
+ * @returns {IPv6CidrRange} the IPV6Range
2543
+ */
2544
+ static fromCidr(rangeInCidrNotation) {
2545
+ let [isValid, message] = Validator_1.Validator.isValidIPv6CidrNotation(rangeInCidrNotation);
2546
+ if (!isValid) {
2547
+ throw new Error(message.filter(msg => { return msg !== ''; }).toString());
2548
+ }
2549
+ let cidrComponents = rangeInCidrNotation.split("/");
2550
+ let ipString = cidrComponents[0];
2551
+ let prefix = BigInt(parseInt(cidrComponents[1]));
2552
+ return new IPv6CidrRange(IPNumber_1.IPv6.fromHexadecatet(ipString), Prefix_1$1.IPv6Prefix.fromNumber(prefix));
2553
+ }
2554
+ ;
2555
+ /**
2556
+ * Gets the size of IPv6 numbers contained within the IPv6 range
2557
+ *
2558
+ * @returns {bigint} the amount of IPv6 numbers in the range
2559
+ */
2560
+ getSize() {
2561
+ return this.cidrPrefix.toRangeSize();
2562
+ }
2563
+ /**
2564
+ * Method that returns the IPv6 range in CIDR (Classless Inter-Domain Routing) notation.
2565
+ *
2566
+ * See {@link https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation} for more information
2567
+ * on the Classless Inter-Domain Routing notation
2568
+ *
2569
+ * @returns {string} the IPv6 range in CIDR (Classless Inter-Domain Routing) notation
2570
+ */
2571
+ toCidrString() {
2572
+ let first = this.ipv6.toString();
2573
+ return `${first.toString()}/${this.cidrPrefix.toString()}`;
2574
+ }
2575
+ /**
2576
+ * Method that returns the IPv6 range in string notation where the first IPv6 number and last IPv6 number are
2577
+ * separated by an hyphen. eg. "2001:db8:0:0:0:0:0:0-2001:db8:0:ffff:ffff:ffff:ffff:ffff"
2578
+ *
2579
+ * @returns {string} the range in [first IPv6 number] - [last IPv6 number] format
2580
+ */
2581
+ toRangeString() {
2582
+ return super.toRangeString();
2583
+ }
2584
+ /**
2585
+ * Method that returns the first IPv6 number in the IPv6 range
2586
+ *
2587
+ * @returns {IPv6} the first IPv6 number in the IPv6 range
2588
+ */
2589
+ getFirst() {
2590
+ return IPNumber_1.IPv6.fromBigInt(this.ipv6.getValue() & (this.cidrPrefix.toMask().getValue()));
2591
+ }
2592
+ /**
2593
+ * Method that returns the last IPv6 number in the IPv6 range
2594
+ *
2595
+ * @returns {IPv6} the last IPv6 number in the IPv6 range
2596
+ */
2597
+ getLast() {
2598
+ return last(this, this.ipv6);
2599
+ }
2600
+ newInstance(num, prefix) {
2601
+ return new IPv6CidrRange(num, prefix);
2602
+ }
2603
+ getPrefix() {
2604
+ return this.cidrPrefix;
2605
+ }
2606
+ /**
2607
+ * Indicates whether the given IPv6 range is an adjacent range.
2608
+ *
2609
+ * An adjacent range being one where the end of the given range, when incremented by one marks the start of the
2610
+ * other range. Or where the start of the given range, when decreased by one, marks the end of the other range
2611
+ *
2612
+ * @param {IPv6CidrRange} otherRange the other IPv6 range to compare with
2613
+ * @returns {boolean} true if the two IPv6 ranges are consecutive, false otherwise
2614
+ */
2615
+ isConsecutive(otherRange) {
2616
+ return super.isConsecutive(otherRange);
2617
+ }
2618
+ /**
2619
+ * Indicates if the given IPv6 range is a subset.
2620
+ *
2621
+ * By a subset range, it means all the values of the given range are contained by this IPv6 range
2622
+ *
2623
+ * @param {IPv6CidrRange} otherRange the other IPv6 range
2624
+ * @returns {boolean} true if the other Ipv6 range is a subset. False otherwise.
2625
+ */
2626
+ contains(otherRange) {
2627
+ return super.contains(otherRange);
2628
+ }
2629
+ /**
2630
+ * Indicate if the given range is a container range.
2631
+ *
2632
+ * By container range, it means all the IP number in this current range can be found within the given range.
2633
+ *
2634
+ * @param {IPv6CidrRange} otherRange he other IPv6 range
2635
+ * @returns {boolean} true if the other Ipv6 range is a container range. False otherwise.
2636
+ */
2637
+ inside(otherRange) {
2638
+ return super.inside(otherRange);
2639
+ }
2640
+ /**
2641
+ * Checks if two IPv6 ranges overlap
2642
+ * @param {IPv6CidrRange} otherRange the other IPv6 range
2643
+ * @returns {boolean} true if the ranges overlap, false otherwise
2644
+ */
2645
+ isOverlapping(otherRange) {
2646
+ return super.isOverlapping(otherRange);
2647
+ }
2648
+ /**
2649
+ * Method that takes IPv6 number from within an IPv6 range, starting from the first IPv6 number
2650
+ *
2651
+ * @param {bigint} count the amount of IPv6 number to take from the IPv6 range
2652
+ * @returns {Array<IPv6>} an array of IPv6 number, taken from the IPv6 range
2653
+ */
2654
+ take(count) {
2655
+ let iPv6s = [this.getFirst()];
2656
+ let iteratingIPv6 = this.getFirst();
2657
+ if (count > (this.getSize())) {
2658
+ throw new Error(`${count.toString()} is greater than ${this.getSize().toString()}, the size of the range`);
2659
+ }
2660
+ for (var counter = 0; counter < count - 1n; counter++) {
2661
+ iPv6s.push(iteratingIPv6.nextIPNumber());
2662
+ iteratingIPv6 = iteratingIPv6.nextIPNumber();
2663
+ }
2664
+ return iPv6s;
2665
+ }
2666
+ /**
2667
+ * Method that splits an IPv6 cidr range into two halves
2668
+ *
2669
+ * @returns {Array<IPv6CidrRange>} An array of two {@link IPv6CidrRange}
2670
+ */
2671
+ split() {
2672
+ let prefixToSplit = this.cidrPrefix.getValue();
2673
+ if (prefixToSplit === 128n) {
2674
+ throw new Error("Cannot split an IP range with a single IP number");
2675
+ }
2676
+ let splitCidr = Prefix_1$1.IPv6Prefix.fromNumber(prefixToSplit + 1n);
2677
+ let firstIPOfFirstRange = this.getFirst();
2678
+ let firstRange = new IPv6CidrRange(firstIPOfFirstRange, splitCidr);
2679
+ let firstIPOfSecondRange = firstRange.getLast().nextIPNumber();
2680
+ let secondRange = new IPv6CidrRange(firstIPOfSecondRange, splitCidr);
2681
+ return [firstRange, secondRange];
2682
+ }
2683
+ /**
2684
+ * Method that split prefix into ranges of the given prefix,
2685
+ * throws an exception if the size of the given prefix is larger than target prefix
2686
+ *
2687
+ * @param prefix the prefix to use to split
2688
+ * @returns {Array<IPv6CidrRange>} An array of two {@link IPv6CidrRange}
2689
+ */
2690
+ splitInto(prefix) {
2691
+ let splitCount = prefix.getValue() - this.cidrPrefix.getValue();
2692
+ if (splitCount < 0) {
2693
+ throw new Error("Prefix to split into is larger than source prefix");
2694
+ }
2695
+ else if (splitCount === 0n) {
2696
+ return [new IPv6CidrRange(this.getFirst(), prefix)];
2697
+ }
2698
+ else if (splitCount === 1n) {
2699
+ return this.split();
2700
+ }
2701
+ else {
2702
+ let results = this.split();
2703
+ while (splitCount > 1) {
2704
+ results = results.flatMap(result => result.split());
2705
+ splitCount = splitCount - 1n;
2706
+ }
2707
+ return results;
2708
+ }
2709
+ }
2710
+ /**
2711
+ * Returns true if there is an adjacent IPv6 cidr range of exactly the same size next to this range
2712
+ */
2713
+ hasNextRange() {
2714
+ return super.hasNextRange();
2715
+ }
2716
+ /**
2717
+ * Returns true if there is an adjacent IPv6 cidr range of exactly the same size previous to this range
2718
+ */
2719
+ hasPreviousRange() {
2720
+ return super.hasPreviousRange();
2721
+ }
2722
+ /**
2723
+ * Return the next IPv6 cidr range, or undefined if no next range
2724
+ */
2725
+ nextRange() {
2726
+ if (this.hasNextRange()) {
2727
+ let sizeOfCurrentRange = this.getSize();
2728
+ let startOfNextRange = this.getFirst().getValue() + (sizeOfCurrentRange);
2729
+ return new IPv6CidrRange(new IPNumber_1.IPv6(startOfNextRange), this.cidrPrefix);
2730
+ }
2731
+ return;
2732
+ }
2733
+ /**
2734
+ * Return the previous IPv6 cidr range, or undefined if no next range
2735
+ */
2736
+ previousRange() {
2737
+ if (this.hasPreviousRange()) {
2738
+ let sizeOfCurrentRange = this.getSize();
2739
+ let startOfPreviousRange = this.getFirst().getValue() - sizeOfCurrentRange;
2740
+ return new IPv6CidrRange(new IPNumber_1.IPv6(startOfPreviousRange), this.cidrPrefix);
2741
+ }
2742
+ return;
2743
+ }
2744
+ }
2745
+ IPRange.IPv6CidrRange = IPv6CidrRange;
2746
+ // utility functions shared by both IPv6CidrRange and IPv4CidrRange
2747
+ let last = (range, ip) => {
2748
+ let bitValue = Number(range.bitValue.valueOf());
2749
+ let maskSize = BigInt(`0b${"1".repeat(bitValue)}`);
2750
+ let maskAsBigInteger = range.cidrPrefix.toMask().getValue();
2751
+ let invertedMask = BinaryUtils_1.leftPadWithZeroBit((maskAsBigInteger ^ (maskSize)).toString(2), bitValue);
2752
+ if (isIPv4CidrRange(range)) {
2753
+ return IPNumber_1.IPv4.fromNumber((ip.getValue()) | (BinaryUtils_1.parseBinaryStringToBigInt(invertedMask)));
2754
+ }
2755
+ else {
2756
+ return IPNumber_1.IPv6.fromBigInt((ip.getValue()) | (BinaryUtils_1.parseBinaryStringToBigInt(invertedMask)));
2757
+ }
2758
+ };
2759
+ function isIPv4CidrRange(ip) {
2760
+ return ip.bitValue.valueOf() === 32n;
2761
+ }
2762
+ IPRange.isIPv4CidrRange = isIPv4CidrRange;
2763
+
2764
+ Object.defineProperty(IPPool, "__esModule", { value: true });
2765
+ IPPool.Pool = void 0;
2766
+ const IPRange_1 = IPRange;
2767
+ const Prefix_1 = Prefix;
2768
+ /**
2769
+ * Represents a collection of IP {@link RangedSet}'s
2770
+ */
2771
+ class Pool {
2772
+ /**
2773
+ * Constructor for an IP pool.
2774
+ *
2775
+ * Creates a Pool of IP ranges from supplied {@link RangedSet}'s
2776
+ *
2777
+ * @param ranges the array of IP ranges that would make up the pool.
2778
+ */
2779
+ constructor(ranges) {
2780
+ this.backingSet = new SortedSet();
2781
+ ranges.forEach(range => {
2782
+ this.backingSet.add(range);
2783
+ });
2784
+ }
2785
+ /**
2786
+ * Convenient method for creating an instance from arrays of {@link IPv4} or {@link IPv6}
2787
+ * @param ipNumbers the arrays of {@link IPv4} or {@link IPv6} that will make up the pool.
2788
+ */
2789
+ static fromIP(ipNumbers) {
2790
+ let ranges = ipNumbers.map((ip) => {
2791
+ return IPRange_1.RangedSet.fromSingleIP(ip);
2792
+ });
2793
+ return new Pool(ranges);
2794
+ }
2795
+ /**
2796
+ * Convenient method for creating an instance from arrays of {@link RangedSet}.
2797
+ *
2798
+ * @param ipRanges the arrays of {@link RangedSet}'s that will make up the pool.
2799
+ */
2800
+ // TODO: TSE: This makes it possible to create an instance containing both Range set of IPv4 and IPv6
2801
+ static fromRangeSet(ipRanges) {
2802
+ return new Pool(ipRanges);
2803
+ }
2804
+ /**
2805
+ * Convenient method for creating an instance from arrays of {@link IPv4CidrRange} or {@link IPv6CidrRange}.
2806
+ *
2807
+ * @param cidrRanges the arrays of {@link IPv4CidrRange} or {@link IPv6CidrRange} that will make up the pool.
2808
+ */
2809
+ static fromCidrRanges(cidrRanges) {
2810
+ let cidr = cidrRanges;
2811
+ let rangeSet = cidr.map((range) => {
2812
+ return range.toRangeSet();
2813
+ });
2814
+ return new Pool(rangeSet);
2815
+ }
2816
+ /**
2817
+ * Returns an array of {@link RangedSet}'s that is contained within the pool
2818
+ */
2819
+ getRanges() {
2820
+ return this.backingSet.asArray();
2821
+ }
2822
+ /**
2823
+ * Returns an new {@link Pool} with all the IP ranges aggregated
2824
+ */
2825
+ aggregate() {
2826
+ let sortedRanges = this.backingSet.asArray();
2827
+ let mergedRanges = sortedRanges.reduce((accumulator, currentRange, currentIndex, array) => {
2828
+ if (accumulator.length == 0) {
2829
+ accumulator.push(currentRange);
2830
+ return accumulator;
2831
+ }
2832
+ else {
2833
+ let previous = accumulator.pop();
2834
+ let previousCidrRange = previous.toCidrRange();
2835
+ let currentCidrRange = currentRange.toCidrRange();
2836
+ if (previousCidrRange.isCidrMergeable(currentCidrRange)) {
2837
+ let merged = previousCidrRange.merge(currentCidrRange);
2838
+ accumulator.push(merged.toRangeSet());
2839
+ return accumulator;
2840
+ }
2841
+ else {
2842
+ if (!previous.contains(currentRange)) {
2843
+ accumulator.push(previous);
2844
+ accumulator.push(currentRange);
2845
+ }
2846
+ else {
2847
+ accumulator.push(previous);
2848
+ }
2849
+ return accumulator;
2850
+ }
2851
+ }
2852
+ }, []);
2853
+ let aggregatedPool = Pool.fromRangeSet(mergedRanges);
2854
+ if (aggregatedPool.getRanges().length !== this.getRanges().length) {
2855
+ return aggregatedPool.aggregate();
2856
+ }
2857
+ else {
2858
+ return aggregatedPool;
2859
+ }
2860
+ }
2861
+ /**
2862
+ * Gets a single range of size of the given prefix from pool.
2863
+ * Only returns a range if there is a single range in the pool of same size or greater than given prefix.
2864
+ *
2865
+ * throws exception if the requested range cannot be got from the pool.
2866
+ *
2867
+ * @param prefix prefix range to retrieve
2868
+ * TODO TSE
2869
+ */
2870
+ getCidrRange(prefix) {
2871
+ if (prefix.toRangeSize() > (this.getSize())) {
2872
+ throw new Error(`Not enough IP number in the pool for requested prefix: ${prefix}`);
2873
+ }
2874
+ let selectedCidrRange;
2875
+ let error;
2876
+ loop: for (let range of this.getRanges()) {
2877
+ for (let offset = 0n; offset + (prefix.toRangeSize()) <= (range.getSize()); offset = offset + 1n)
2878
+ try {
2879
+ let selectedRange = range.takeSubRange(offset, prefix.toRangeSize());
2880
+ selectedCidrRange = selectedRange.toCidrRange();
2881
+ let remaining = range.difference(selectedRange);
2882
+ this.removeExact(range);
2883
+ this.add(remaining);
2884
+ break loop;
2885
+ }
2886
+ catch (e) {
2887
+ if (e instanceof RangeError) {
2888
+ continue loop;
2889
+ }
2890
+ error = e;
2891
+ }
2892
+ }
2893
+ if (selectedCidrRange) {
2894
+ return selectedCidrRange;
2895
+ }
2896
+ else {
2897
+ throw (error === undefined ? new Error(`No range big enough in the pool for requested prefix: ${prefix}`) : error);
2898
+ }
2899
+ }
2900
+ /**
2901
+ * Gets a single or multiple ranges that fulfils the given prefix from the pool.
2902
+ *
2903
+ * throws exception if the requested range cannot be got from the pool.
2904
+ *
2905
+ * @param reqprefix prefix range to retrieve
2906
+ */
2907
+ getCidrRanges(reqprefix) {
2908
+ if (reqprefix.toRangeSize() > (this.getSize())) {
2909
+ throw new Error("Prefix greater than pool");
2910
+ }
2911
+ let go = (reqprefix, prefix, accummulated) => {
2912
+ try {
2913
+ let singleCidrRange = this.getCidrRange(prefix);
2914
+ accummulated.push(singleCidrRange);
2915
+ let currentSize = accummulated.reduce((previous, current) => {
2916
+ return previous + (current.getSize());
2917
+ }, 0n);
2918
+ if (reqprefix.toRangeSize() === (currentSize)) {
2919
+ return accummulated;
2920
+ }
2921
+ else {
2922
+ return go(reqprefix, prefix, accummulated);
2923
+ }
2924
+ }
2925
+ catch (e) {
2926
+ let lowerPrefix = Prefix_1.isIPv4Prefix(prefix) ?
2927
+ Prefix_1.IPv4Prefix.fromNumber(prefix.getValue() + 1n) : Prefix_1.IPv6Prefix.fromNumber(prefix.getValue() + 1n);
2928
+ return go(reqprefix, lowerPrefix, accummulated);
2929
+ }
2930
+ };
2931
+ return go(reqprefix, reqprefix, []);
2932
+ }
2933
+ /**
2934
+ * Returns the size of IP numbers in the pool
2935
+ */
2936
+ getSize() {
2937
+ return this
2938
+ .aggregate()
2939
+ .getRanges()
2940
+ .reduce((previous, current) => {
2941
+ return previous + current.getSize();
2942
+ }, 0n);
2943
+ }
2944
+ /**
2945
+ * Empties the pool and fill it with given ranges
2946
+ *
2947
+ * @param ipRanges the range to fill the pool with after emptying
2948
+ */
2949
+ resetWith(ipRanges) {
2950
+ this.backingSet.clear();
2951
+ this.backingSet = this.backingSet.add(ipRanges);
2952
+ }
2953
+ /**
2954
+ * Removes the given range from the pool. It only removes if the exact range exist in the pool.
2955
+ * It is a Noop and returns false, if the given range does not exist in the pool. Returns true otherwise
2956
+ *
2957
+ * @param rangeToRemove range to remove from ppol
2958
+ */
2959
+ removeExact(rangeToRemove) {
2960
+ let updatedSet = this.backingSet.removeExact(rangeToRemove);
2961
+ let isUpdated = !this.backingSet.isEquals(updatedSet);
2962
+ this.backingSet = updatedSet;
2963
+ return isUpdated;
2964
+ }
2965
+ /**
2966
+ * Removes the given range from the pool. If the given range overlaps, then it removes the overlapping portion.
2967
+ * It is a Noop and returns false, if the given range does not exist in the pool. Returns true otherwise
2968
+ *
2969
+ * @param rangeToRemove range to remove from ppol
2970
+ */
2971
+ removeOverlapping(rangeToRemove) {
2972
+ let updatedSet = this.backingSet.removeOverlapping(rangeToRemove);
2973
+ let isUpdated = !this.backingSet.isEquals(updatedSet);
2974
+ this.backingSet = updatedSet;
2975
+ return isUpdated;
2976
+ }
2977
+ /**
2978
+ * Adds the given range to the pool.
2979
+ *
2980
+ * @param range to add to pool.
2981
+ */
2982
+ add(range) {
2983
+ this.backingSet = this.backingSet.add(range);
2984
+ }
2985
+ /**
2986
+ * Removes all ranges from pool
2987
+ */
2988
+ clear() {
2989
+ this.backingSet.clear();
2990
+ }
2991
+ }
2992
+ IPPool.Pool = Pool;
2993
+ class SortedSet {
2994
+ constructor(array) {
2995
+ if (array) {
2996
+ this.backingArray = this.sortArray(array);
2997
+ }
2998
+ else {
2999
+ this.backingArray = new Array();
3000
+ }
3001
+ }
3002
+ sortArray(array) {
3003
+ array.sort((a, b) => {
3004
+ if (a.isLessThan(b)) {
3005
+ return -1;
3006
+ }
3007
+ if (a.isGreaterThan(b)) {
3008
+ return 1;
3009
+ }
3010
+ return 0;
3011
+ });
3012
+ return array;
3013
+ }
3014
+ asArray() {
3015
+ return this.backingArray;
3016
+ }
3017
+ isEquals(other) {
3018
+ if (this.backingArray.length !== other.asArray().length) {
3019
+ return false;
3020
+ }
3021
+ return this.backingArray.every((value, index) => {
3022
+ return value.getSize() === (other.asArray()[index].getSize());
3023
+ });
3024
+ }
3025
+ add(item) {
3026
+ let array = this.backingArray;
3027
+ if ("push" in item) {
3028
+ array = array.concat(item);
3029
+ }
3030
+ else {
3031
+ array.push(item);
3032
+ }
3033
+ return new SortedSet(this.sortArray(array));
3034
+ }
3035
+ removeExact(items) {
3036
+ let filtered = this.backingArray
3037
+ .filter(currentItem => {
3038
+ if ("push" in items) {
3039
+ return items.find(item => item.isEquals(currentItem)) !== undefined;
3040
+ }
3041
+ else {
3042
+ return !items.isEquals(currentItem);
3043
+ }
3044
+ });
3045
+ return new SortedSet(this.sortArray(filtered));
3046
+ }
3047
+ removeOverlapping(items) {
3048
+ let filtered = this.backingArray
3049
+ .flatMap(backingItem => {
3050
+ if ("push" in items) {
3051
+ return items.flatMap(item => {
3052
+ if (backingItem.contains(item)) {
3053
+ return backingItem.difference(item);
3054
+ }
3055
+ else if (backingItem.inside(item)) {
3056
+ return new Array();
3057
+ }
3058
+ else if (backingItem.isOverlapping(item)) {
3059
+ return [backingItem.subtract(item)];
3060
+ }
3061
+ else {
3062
+ return [item];
3063
+ }
3064
+ });
3065
+ }
3066
+ else {
3067
+ try {
3068
+ return backingItem.difference(items);
3069
+ }
3070
+ catch (e) {
3071
+ return backingItem;
3072
+ }
3073
+ }
3074
+ });
3075
+ return new SortedSet(this.sortArray(filtered));
3076
+ }
3077
+ clear() {
3078
+ this.backingArray = [];
3079
+ }
3080
+ }
3081
+
3082
+ (function (exports) {
3083
+ var __createBinding = (commonjsGlobal && commonjsGlobal.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3084
+ if (k2 === undefined) k2 = k;
3085
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
3086
+ }) : (function(o, m, k, k2) {
3087
+ if (k2 === undefined) k2 = k;
3088
+ o[k2] = m[k];
3089
+ }));
3090
+ var __exportStar = (commonjsGlobal && commonjsGlobal.__exportStar) || function(m, exports) {
3091
+ for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
3092
+ };
3093
+ Object.defineProperty(exports, "__esModule", { value: true });
3094
+ /**
3095
+ * Exposes all the library's modules making them available from the "ip-num" module.
3096
+ * Making it possible to get access to all any of the modules by doing require("ip-num")
3097
+ *
3098
+ * @example
3099
+ * let ipNum = require("ip-num")
3100
+ * ipNum.IPv4CidrRange // gets access to IPv4CidrRange
3101
+ * ipNum.Asn // gets access to Asn
3102
+ */
3103
+ __exportStar(BinaryUtils, exports);
3104
+ __exportStar(Hexadecatet$1, exports);
3105
+ __exportStar(requireHexadecimalUtils(), exports);
3106
+ __exportStar(IPNumber, exports);
3107
+ __exportStar(IPNumType, exports);
3108
+ __exportStar(IPPool, exports);
3109
+ __exportStar(IPRange, exports);
3110
+ __exportStar(requireIPv6Utils(), exports);
3111
+ __exportStar(Octet$1, exports);
3112
+ __exportStar(Prefix, exports);
3113
+ __exportStar(requireValidator(), exports);
3114
+
3115
+ } (ipNum));
3116
+
31
3117
  var toLabelAndValue = function toLabelAndValue(value) {
32
3118
  return {
33
3119
  label: humanizeIpAddressFormat(value),
@@ -35,10 +3121,10 @@ var toLabelAndValue = function toLabelAndValue(value) {
35
3121
  };
36
3122
  };
37
3123
  var isValidIPv4String = function isValidIPv4String(ip) {
38
- return isNotNil(ip) && Validator.isValidIPv4String(ip)[0];
3124
+ return isNotNil(ip) && ipNum.Validator.isValidIPv4String(ip)[0];
39
3125
  };
40
3126
  var isValidIPv6String = function isValidIPv6String(ip) {
41
- return isNotNil(ip) && Validator.isValidIPv6String(ip)[0];
3127
+ return isNotNil(ip) && ipNum.Validator.isValidIPv6String(ip)[0];
42
3128
  };
43
3129
  var isValidIpString = function isValidIpString(ip) {
44
3130
  return isValidIPv4String(ip) || isValidIPv6String(ip);
@@ -47,13 +3133,13 @@ var areIpAddressesInSameFormat = function areIpAddressesInSameFormat(ipStart, ip
47
3133
  return isValidIPv4String(ipStart) && isValidIPv4String(ipEnd) || isValidIPv6String(ipStart) && isValidIPv6String(ipEnd);
48
3134
  };
49
3135
  var isValidIPv4Range = function isValidIPv4Range(ipStart, ipEnd) {
50
- var ipStartAddr = IPv4.fromDecimalDottedString(ipStart);
51
- var ipEndAddr = IPv4.fromDecimalDottedString(ipEnd);
3136
+ var ipStartAddr = ipNum.IPv4.fromDecimalDottedString(ipStart);
3137
+ var ipEndAddr = ipNum.IPv4.fromDecimalDottedString(ipEnd);
52
3138
  return ipStartAddr.isLessThanOrEquals(ipEndAddr);
53
3139
  };
54
3140
  var isValidIPv6Range = function isValidIPv6Range(ipStart, ipEnd) {
55
- var ipStartAddr = IPv6.fromString(ipStart);
56
- var ipEndAddr = IPv6.fromString(ipEnd);
3141
+ var ipStartAddr = ipNum.IPv6.fromString(ipStart);
3142
+ var ipEndAddr = ipNum.IPv6.fromString(ipEnd);
57
3143
  return ipStartAddr.isLessThanOrEquals(ipEndAddr);
58
3144
  };
59
3145
  var isValidIPRange = function isValidIPRange(ipStart, ipEnd) {