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