@bigbinary/neeto-molecules 5.1.19 → 5.1.21

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