@bigbinary/neeto-molecules 5.1.19 → 5.1.21

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