@bigbinary/neeto-molecules 5.1.19 → 5.1.20

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