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