@twin.org/core 0.0.2-next.9 → 0.0.3-next.2

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 (187) hide show
  1. package/dist/es/encoding/base32.js +75 -0
  2. package/dist/es/encoding/base32.js.map +1 -0
  3. package/dist/es/encoding/base58.js +124 -0
  4. package/dist/es/encoding/base58.js.map +1 -0
  5. package/dist/es/encoding/base64.js +231 -0
  6. package/dist/es/encoding/base64.js.map +1 -0
  7. package/dist/es/encoding/base64Url.js +42 -0
  8. package/dist/es/encoding/base64Url.js.map +1 -0
  9. package/dist/es/errors/alreadyExistsError.js +22 -0
  10. package/dist/es/errors/alreadyExistsError.js.map +1 -0
  11. package/dist/es/errors/baseError.js +257 -0
  12. package/dist/es/errors/baseError.js.map +1 -0
  13. package/dist/es/errors/conflictError.js +23 -0
  14. package/dist/es/errors/conflictError.js.map +1 -0
  15. package/dist/es/errors/generalError.js +21 -0
  16. package/dist/es/errors/generalError.js.map +1 -0
  17. package/dist/es/errors/guardError.js +27 -0
  18. package/dist/es/errors/guardError.js.map +1 -0
  19. package/dist/es/errors/notFoundError.js +22 -0
  20. package/dist/es/errors/notFoundError.js.map +1 -0
  21. package/dist/es/errors/notImplementedError.js +24 -0
  22. package/dist/es/errors/notImplementedError.js.map +1 -0
  23. package/dist/es/errors/notSupportedError.js +21 -0
  24. package/dist/es/errors/notSupportedError.js.map +1 -0
  25. package/dist/es/errors/unauthorizedError.js +21 -0
  26. package/dist/es/errors/unauthorizedError.js.map +1 -0
  27. package/dist/es/errors/unprocessableError.js +21 -0
  28. package/dist/es/errors/unprocessableError.js.map +1 -0
  29. package/dist/es/errors/validationError.js +23 -0
  30. package/dist/es/errors/validationError.js.map +1 -0
  31. package/dist/es/factories/componentFactory.js +9 -0
  32. package/dist/es/factories/componentFactory.js.map +1 -0
  33. package/dist/es/factories/factory.js +256 -0
  34. package/dist/es/factories/factory.js.map +1 -0
  35. package/dist/es/helpers/arrayHelper.js +44 -0
  36. package/dist/es/helpers/arrayHelper.js.map +1 -0
  37. package/dist/es/helpers/envHelper.js +39 -0
  38. package/dist/es/helpers/envHelper.js.map +1 -0
  39. package/dist/es/helpers/errorHelper.js +100 -0
  40. package/dist/es/helpers/errorHelper.js.map +1 -0
  41. package/dist/es/helpers/filenameHelper.js +32 -0
  42. package/dist/es/helpers/filenameHelper.js.map +1 -0
  43. package/dist/es/helpers/hexHelper.js +45 -0
  44. package/dist/es/helpers/hexHelper.js.map +1 -0
  45. package/dist/es/helpers/jsonHelper.js +163 -0
  46. package/dist/es/helpers/jsonHelper.js.map +1 -0
  47. package/dist/es/helpers/numberHelper.js +30 -0
  48. package/dist/es/helpers/numberHelper.js.map +1 -0
  49. package/dist/es/helpers/objectHelper.js +333 -0
  50. package/dist/es/helpers/objectHelper.js.map +1 -0
  51. package/dist/es/helpers/randomHelper.js +18 -0
  52. package/dist/es/helpers/randomHelper.js.map +1 -0
  53. package/dist/es/helpers/stringHelper.js +241 -0
  54. package/dist/es/helpers/stringHelper.js.map +1 -0
  55. package/dist/es/helpers/uint8ArrayHelper.js +26 -0
  56. package/dist/es/helpers/uint8ArrayHelper.js.map +1 -0
  57. package/dist/es/index.js +57 -0
  58. package/dist/es/index.js.map +1 -0
  59. package/dist/es/models/IComponent.js +4 -0
  60. package/dist/es/models/IComponent.js.map +1 -0
  61. package/dist/es/models/IError.js +4 -0
  62. package/dist/es/models/IError.js.map +1 -0
  63. package/dist/es/models/II18nShared.js +2 -0
  64. package/dist/es/models/II18nShared.js.map +1 -0
  65. package/dist/es/models/IKeyValue.js +2 -0
  66. package/dist/es/models/IKeyValue.js.map +1 -0
  67. package/dist/es/models/ILabelledValue.js +2 -0
  68. package/dist/es/models/ILabelledValue.js.map +1 -0
  69. package/dist/es/models/ILocale.js +2 -0
  70. package/dist/es/models/ILocale.js.map +1 -0
  71. package/dist/es/models/ILocaleDictionary.js +4 -0
  72. package/dist/es/models/ILocaleDictionary.js.map +1 -0
  73. package/dist/es/models/ILocalesIndex.js +2 -0
  74. package/dist/es/models/ILocalesIndex.js.map +1 -0
  75. package/dist/es/models/IPatchOperation.js +4 -0
  76. package/dist/es/models/IPatchOperation.js.map +1 -0
  77. package/dist/es/models/IUrlParts.js +4 -0
  78. package/dist/es/models/IUrlParts.js.map +1 -0
  79. package/dist/es/models/IValidationFailure.js +2 -0
  80. package/dist/es/models/IValidationFailure.js.map +1 -0
  81. package/dist/es/models/coerceType.js +49 -0
  82. package/dist/es/models/coerceType.js.map +1 -0
  83. package/dist/es/models/compressionType.js +17 -0
  84. package/dist/es/models/compressionType.js.map +1 -0
  85. package/dist/es/models/objectOrArray.js +2 -0
  86. package/dist/es/models/objectOrArray.js.map +1 -0
  87. package/dist/es/types/bitString.js +98 -0
  88. package/dist/es/types/bitString.js.map +1 -0
  89. package/dist/es/types/url.js +144 -0
  90. package/dist/es/types/url.js.map +1 -0
  91. package/dist/es/types/urn.js +212 -0
  92. package/dist/es/types/urn.js.map +1 -0
  93. package/dist/es/utils/asyncCache.js +179 -0
  94. package/dist/es/utils/asyncCache.js.map +1 -0
  95. package/dist/es/utils/coerce.js +269 -0
  96. package/dist/es/utils/coerce.js.map +1 -0
  97. package/dist/es/utils/compression.js +49 -0
  98. package/dist/es/utils/compression.js.map +1 -0
  99. package/dist/es/utils/converter.js +256 -0
  100. package/dist/es/utils/converter.js.map +1 -0
  101. package/dist/es/utils/guards.js +369 -0
  102. package/dist/es/utils/guards.js.map +1 -0
  103. package/dist/es/utils/i18n.js +243 -0
  104. package/dist/es/utils/i18n.js.map +1 -0
  105. package/dist/es/utils/is.js +351 -0
  106. package/dist/es/utils/is.js.map +1 -0
  107. package/dist/es/utils/sharedStore.js +48 -0
  108. package/dist/es/utils/sharedStore.js.map +1 -0
  109. package/dist/es/utils/validation.js +774 -0
  110. package/dist/es/utils/validation.js.map +1 -0
  111. package/dist/types/encoding/base32.d.ts +4 -0
  112. package/dist/types/encoding/base58.d.ts +4 -0
  113. package/dist/types/encoding/base64.d.ts +4 -0
  114. package/dist/types/encoding/base64Url.d.ts +4 -0
  115. package/dist/types/errors/alreadyExistsError.d.ts +5 -2
  116. package/dist/types/errors/baseError.d.ts +1 -1
  117. package/dist/types/errors/conflictError.d.ts +5 -2
  118. package/dist/types/errors/generalError.d.ts +1 -1
  119. package/dist/types/errors/guardError.d.ts +1 -1
  120. package/dist/types/errors/notFoundError.d.ts +5 -2
  121. package/dist/types/errors/notImplementedError.d.ts +6 -2
  122. package/dist/types/errors/notSupportedError.d.ts +5 -2
  123. package/dist/types/errors/unauthorizedError.d.ts +5 -2
  124. package/dist/types/errors/unprocessableError.d.ts +1 -1
  125. package/dist/types/errors/validationError.d.ts +2 -2
  126. package/dist/types/factories/componentFactory.d.ts +2 -2
  127. package/dist/types/factories/factory.d.ts +4 -0
  128. package/dist/types/helpers/arrayHelper.d.ts +1 -1
  129. package/dist/types/helpers/errorHelper.d.ts +1 -1
  130. package/dist/types/helpers/jsonHelper.d.ts +5 -1
  131. package/dist/types/helpers/numberHelper.d.ts +17 -0
  132. package/dist/types/helpers/objectHelper.d.ts +14 -0
  133. package/dist/types/helpers/stringHelper.d.ts +6 -6
  134. package/dist/types/index.d.ts +54 -53
  135. package/dist/types/models/IComponent.d.ts +6 -7
  136. package/dist/types/models/ILocalesIndex.d.ts +1 -1
  137. package/dist/types/models/IValidationFailure.d.ts +0 -4
  138. package/dist/types/types/bitString.d.ts +4 -0
  139. package/dist/types/types/url.d.ts +8 -3
  140. package/dist/types/types/urn.d.ts +7 -2
  141. package/dist/types/utils/coerce.d.ts +1 -1
  142. package/dist/types/utils/compression.d.ts +5 -1
  143. package/dist/types/utils/guards.d.ts +2 -3
  144. package/dist/types/utils/i18n.d.ts +16 -1
  145. package/dist/types/utils/is.d.ts +1 -1
  146. package/dist/types/utils/validation.d.ts +1 -1
  147. package/docs/changelog.md +283 -0
  148. package/docs/reference/classes/AlreadyExistsError.md +8 -4
  149. package/docs/reference/classes/AsyncCache.md +5 -5
  150. package/docs/reference/classes/Base32.md +8 -0
  151. package/docs/reference/classes/Base58.md +8 -0
  152. package/docs/reference/classes/Base64.md +8 -0
  153. package/docs/reference/classes/Base64Url.md +8 -0
  154. package/docs/reference/classes/BaseError.md +3 -3
  155. package/docs/reference/classes/BitString.md +8 -0
  156. package/docs/reference/classes/Coerce.md +20 -20
  157. package/docs/reference/classes/Compression.md +8 -0
  158. package/docs/reference/classes/ConflictError.md +8 -4
  159. package/docs/reference/classes/ErrorHelper.md +2 -2
  160. package/docs/reference/classes/Factory.md +11 -3
  161. package/docs/reference/classes/GeneralError.md +3 -3
  162. package/docs/reference/classes/GuardError.md +3 -3
  163. package/docs/reference/classes/Guards.md +8 -4
  164. package/docs/reference/classes/I18n.md +54 -2
  165. package/docs/reference/classes/Is.md +10 -4
  166. package/docs/reference/classes/JsonHelper.md +8 -0
  167. package/docs/reference/classes/NotFoundError.md +8 -4
  168. package/docs/reference/classes/NotImplementedError.md +14 -4
  169. package/docs/reference/classes/NotSupportedError.md +8 -4
  170. package/docs/reference/classes/NumberHelper.md +55 -0
  171. package/docs/reference/classes/ObjectHelper.md +58 -8
  172. package/docs/reference/classes/SharedStore.md +2 -2
  173. package/docs/reference/classes/StringHelper.md +8 -8
  174. package/docs/reference/classes/UnauthorizedError.md +8 -4
  175. package/docs/reference/classes/UnprocessableError.md +3 -3
  176. package/docs/reference/classes/Url.md +17 -3
  177. package/docs/reference/classes/Urn.md +19 -5
  178. package/docs/reference/classes/Validation.md +2 -2
  179. package/docs/reference/classes/ValidationError.md +3 -3
  180. package/docs/reference/index.md +1 -0
  181. package/docs/reference/interfaces/IComponent.md +19 -25
  182. package/docs/reference/interfaces/IValidationFailure.md +0 -8
  183. package/locales/.validate-ignore +1 -0
  184. package/locales/en.json +4 -5
  185. package/package.json +22 -10
  186. package/dist/cjs/index.cjs +0 -5175
  187. package/dist/esm/index.mjs +0 -5133
@@ -0,0 +1,75 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ /* eslint-disable no-bitwise */
4
+ import { GeneralError } from "../errors/generalError.js";
5
+ import { Guards } from "../utils/guards.js";
6
+ /**
7
+ * Class to help with base63 Encoding/Decoding.
8
+ */
9
+ export class Base32 {
10
+ /**
11
+ * Runtime name for the class.
12
+ */
13
+ static CLASS_NAME = "Base32";
14
+ /**
15
+ * Alphabet table for encoding.
16
+ * @internal
17
+ */
18
+ static _ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
19
+ /**
20
+ * Convert the base 32 string to a byte array.
21
+ * @param base32 The base32 string to convert.
22
+ * @returns The byte array.
23
+ * @throws If the input string contains a character not in the Base32 alphabet.
24
+ */
25
+ static decode(base32) {
26
+ Guards.string(Base32.CLASS_NAME, "base32", base32);
27
+ let bits = 0;
28
+ let value = 0;
29
+ base32 = base32.replace(/=+$/, "");
30
+ let index = 0;
31
+ const output = new Uint8Array(Math.trunc((base32.length * 5) / 8));
32
+ for (let i = 0; i < base32.length; i++) {
33
+ const idx = Base32._ALPHABET.indexOf(base32[i]);
34
+ if (idx === -1) {
35
+ throw new GeneralError(Base32.CLASS_NAME, "invalidCharacter", {
36
+ invalidCharacter: base32[i]
37
+ });
38
+ }
39
+ value = (value << 5) | idx;
40
+ bits += 5;
41
+ if (bits >= 8) {
42
+ output[index++] = (value >>> (bits - 8)) & 255;
43
+ bits -= 8;
44
+ }
45
+ }
46
+ return output;
47
+ }
48
+ /**
49
+ * Convert a byte array to base 32.
50
+ * @param bytes The byte array to convert.
51
+ * @returns The data as base32 string.
52
+ */
53
+ static encode(bytes) {
54
+ Guards.uint8Array(Base32.CLASS_NAME, "bytes", bytes);
55
+ let bits = 0;
56
+ let value = 0;
57
+ let output = "";
58
+ for (let i = 0; i < bytes.byteLength; i++) {
59
+ value = (value << 8) | bytes[i];
60
+ bits += 8;
61
+ while (bits >= 5) {
62
+ output += Base32._ALPHABET[(value >>> (bits - 5)) & 31];
63
+ bits -= 5;
64
+ }
65
+ }
66
+ if (bits > 0) {
67
+ output += Base32._ALPHABET[(value << (5 - bits)) & 31];
68
+ }
69
+ while (output.length % 8 !== 0) {
70
+ output += "=";
71
+ }
72
+ return output;
73
+ }
74
+ }
75
+ //# sourceMappingURL=base32.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base32.js","sourceRoot":"","sources":["../../../src/encoding/base32.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,+BAA+B;AAG/B,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;OAGG;IACK,MAAM,CAAU,SAAS,GAAW,kCAAkC,CAAC;IAE/E;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,MAAc;QAClC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,YAAkB,MAAM,CAAC,CAAC;QAEzD,IAAI,IAAI,GAAG,CAAC,CAAC;QACb,IAAI,KAAK,GAAG,CAAC,CAAC;QAEd,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QAEnC,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAEnE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACxC,MAAM,GAAG,GAAG,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;YAEhD,IAAI,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC;gBAChB,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AAC,KAAK,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;QACxD,CAAC;QAED,OAAO,MAAM,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;YAChC,MAAM,IAAI,GAAG,CAAC;QACf,CAAC;QAED,OAAO,MAAM,CAAC;IACf,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable no-bitwise */\n\nimport { nameof } from \"@twin.org/nameof\";\nimport { GeneralError } from \"../errors/generalError.js\";\nimport { Guards } from \"../utils/guards.js\";\n\n/**\n * Class to help with base63 Encoding/Decoding.\n */\nexport class Base32 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Base32>();\n\n\t/**\n\t * Alphabet table for encoding.\n\t * @internal\n\t */\n\tprivate static readonly _ALPHABET: string = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567\";\n\n\t/**\n\t * Convert the base 32 string to a byte array.\n\t * @param base32 The base32 string to convert.\n\t * @returns The byte array.\n\t * @throws If the input string contains a character not in the Base32 alphabet.\n\t */\n\tpublic static decode(base32: string): Uint8Array {\n\t\tGuards.string(Base32.CLASS_NAME, nameof(base32), base32);\n\n\t\tlet bits = 0;\n\t\tlet value = 0;\n\n\t\tbase32 = base32.replace(/=+$/, \"\");\n\n\t\tlet index = 0;\n\t\tconst output = new Uint8Array(Math.trunc((base32.length * 5) / 8));\n\n\t\tfor (let i = 0; i < base32.length; i++) {\n\t\t\tconst idx = Base32._ALPHABET.indexOf(base32[i]);\n\n\t\t\tif (idx === -1) {\n\t\t\t\tthrow new GeneralError(Base32.CLASS_NAME, \"invalidCharacter\", {\n\t\t\t\t\tinvalidCharacter: base32[i]\n\t\t\t\t});\n\t\t\t}\n\t\t\tvalue = (value << 5) | idx;\n\t\t\tbits += 5;\n\n\t\t\tif (bits >= 8) {\n\t\t\t\toutput[index++] = (value >>> (bits - 8)) & 255;\n\t\t\t\tbits -= 8;\n\t\t\t}\n\t\t}\n\n\t\treturn output;\n\t}\n\n\t/**\n\t * Convert a byte array to base 32.\n\t * @param bytes The byte array to convert.\n\t * @returns The data as base32 string.\n\t */\n\tpublic static encode(bytes: Uint8Array): string {\n\t\tGuards.uint8Array(Base32.CLASS_NAME, nameof(bytes), bytes);\n\n\t\tlet bits = 0;\n\t\tlet value = 0;\n\t\tlet output = \"\";\n\n\t\tfor (let i = 0; i < bytes.byteLength; i++) {\n\t\t\tvalue = (value << 8) | bytes[i];\n\t\t\tbits += 8;\n\n\t\t\twhile (bits >= 5) {\n\t\t\t\toutput += Base32._ALPHABET[(value >>> (bits - 5)) & 31];\n\t\t\t\tbits -= 5;\n\t\t\t}\n\t\t}\n\n\t\tif (bits > 0) {\n\t\t\toutput += Base32._ALPHABET[(value << (5 - bits)) & 31];\n\t\t}\n\n\t\twhile (output.length % 8 !== 0) {\n\t\t\toutput += \"=\";\n\t\t}\n\n\t\treturn output;\n\t}\n}\n"]}
@@ -0,0 +1,124 @@
1
+ import { GeneralError } from "../errors/generalError.js";
2
+ import { Guards } from "../utils/guards.js";
3
+ /**
4
+ * Class to help with base58 Encoding/Decoding.
5
+ */
6
+ export class Base58 {
7
+ /**
8
+ * Runtime name for the class.
9
+ */
10
+ static CLASS_NAME = "Base58";
11
+ /**
12
+ * Alphabet table for encoding.
13
+ * @internal
14
+ */
15
+ static _ALPHABET =
16
+ // cspell:disable-next-line
17
+ "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
18
+ /**
19
+ * Reverse map for decoding.
20
+ * @internal
21
+ */
22
+ static _ALPHABET_REVERSE = [
23
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
24
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
25
+ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1, -1, 9, 10, 11, 12, 13, 14, 15, 16, -1,
26
+ 17, 18, 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -1, 33,
27
+ 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
28
+ 57, -1, -1, -1, -1, -1
29
+ ];
30
+ /**
31
+ * Convert the base 58 string to a byte array.
32
+ * @param base58 The base58 string to convert.
33
+ * @returns The byte array.
34
+ * @throws If the input string contains a character not in the Base58 alphabet.
35
+ */
36
+ static decode(base58) {
37
+ Guards.string(Base58.CLASS_NAME, "base58", base58);
38
+ let zeroes = 0;
39
+ for (let i = 0; i < base58.length; i++) {
40
+ if (base58[i] !== "1") {
41
+ break;
42
+ }
43
+ zeroes += 1;
44
+ }
45
+ const size = Math.trunc((base58.length * 733) / 1000) + 1;
46
+ const b256 = new Uint8Array(size).fill(0);
47
+ let length = 0;
48
+ for (let i = zeroes; i < base58.length; i++) {
49
+ const ch = base58.charCodeAt(i);
50
+ if (ch & 0xff80) {
51
+ throw new GeneralError(Base58.CLASS_NAME, "invalidCharacter", { invalidCharacter: ch });
52
+ }
53
+ const val = Base58._ALPHABET_REVERSE[ch];
54
+ if (val === -1) {
55
+ throw new GeneralError(Base58.CLASS_NAME, "invalidCharacter", { invalidCharacter: ch });
56
+ }
57
+ let carry = val;
58
+ let j = 0;
59
+ for (let k = size - 1; k >= 0; k--, j++) {
60
+ if (carry === 0 && j >= length) {
61
+ break;
62
+ }
63
+ carry += b256[k] * 58;
64
+ b256[k] = carry;
65
+ carry >>>= 8;
66
+ }
67
+ length = j;
68
+ }
69
+ const out = new Uint8Array(zeroes + length);
70
+ let j;
71
+ for (j = 0; j < zeroes; j++) {
72
+ out[j] = 0;
73
+ }
74
+ let i = size - length;
75
+ while (i < size) {
76
+ out[j++] = b256[i++];
77
+ }
78
+ return out;
79
+ }
80
+ /**
81
+ * Convert a byte array to base 58.
82
+ * @param bytes The byte array to encode.
83
+ * @returns The data as base58 string.
84
+ */
85
+ static encode(bytes) {
86
+ Guards.uint8Array(Base58.CLASS_NAME, "bytes", bytes);
87
+ let zeroes = 0;
88
+ for (let i = 0; i < bytes.length; i++) {
89
+ if (bytes[i] !== 0) {
90
+ break;
91
+ }
92
+ zeroes += 1;
93
+ }
94
+ const size = Math.trunc(((bytes.length - zeroes) * 138) / 100) + 1;
95
+ const b58 = new Uint8Array(size).fill(0);
96
+ let length = 0;
97
+ for (let i = zeroes; i < bytes.length; i++) {
98
+ let carry = bytes[i];
99
+ let j = 0;
100
+ for (let k = size - 1; k >= 0; k--, j++) {
101
+ if (carry === 0 && j >= length) {
102
+ break;
103
+ }
104
+ carry += b58[k] * 256;
105
+ b58[k] = carry % 58;
106
+ carry = Math.trunc(carry / 58);
107
+ }
108
+ length = j;
109
+ }
110
+ let i = size - length;
111
+ while (i < size && b58[i] === 0) {
112
+ i += 1;
113
+ }
114
+ let str = "";
115
+ for (let j = 0; j < zeroes; j++) {
116
+ str += "1";
117
+ }
118
+ while (i < size) {
119
+ str += Base58._ALPHABET[b58[i++]];
120
+ }
121
+ return str;
122
+ }
123
+ }
124
+ //# sourceMappingURL=base58.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base58.js","sourceRoot":"","sources":["../../../src/encoding/base58.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;OAGG;IACK,MAAM,CAAU,SAAS;IAChC,2BAA2B;IAC3B,4DAA4D,CAAC;IAE9D;;;OAGG;IACK,MAAM,CAAU,iBAAiB,GAA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AAK,CAAC,CAAC,CAAC,CAAC;YACrB,IAAI,CAAC,GAAG,CAAC,CAAC;YAEV,KAAK,IAAI,CAAC,GAAG,IAAI,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC;gBACzC,IAAI,KAAK,KAAK,CAAC,IAAI,CAAC,IAAI,MAAM,EAAE,CAAC;oBAChC,MAAM;gBACP,CAAC;gBAED,KAAK,IAAI,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;gBACtB,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,EAAE,CAAC;gBACpB,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,EAAE,CAAC,CAAC;YAChC,CAAC;YAED,MAAM,GAAG,CAAC,CAAC;QACZ,CAAC;QAED,IAAI,CAAC,GAAG,IAAI,GAAG,MAAM,CAAC;QACtB,OAAO,CAAC,GAAG,IAAI,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;YACjC,CAAC,IAAI,CAAC,CAAC;QACR,CAAC;QAED,IAAI,GAAG,GAAG,EAAE,CAAC;QAEb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACjC,GAAG,IAAI,GAAG,CAAC;QACZ,CAAC;QAED,OAAO,CAAC,GAAG,IAAI,EAAE,CAAC;YACjB,GAAG,IAAI,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACnC,CAAC;QAED,OAAO,GAAG,CAAC;IACZ,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable no-bitwise */\nimport { nameof } from \"@twin.org/nameof\";\nimport { GeneralError } from \"../errors/generalError.js\";\nimport { Guards } from \"../utils/guards.js\";\n\n/**\n * Class to help with base58 Encoding/Decoding.\n */\nexport class Base58 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Base58>();\n\n\t/**\n\t * Alphabet table for encoding.\n\t * @internal\n\t */\n\tprivate static readonly _ALPHABET: string =\n\t\t// cspell:disable-next-line\n\t\t\"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz\";\n\n\t/**\n\t * Reverse map for decoding.\n\t * @internal\n\t */\n\tprivate static readonly _ALPHABET_REVERSE: number[] = [\n\t\t-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,\n\t\t-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,\n\t\t-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1, -1, 9, 10, 11, 12, 13, 14, 15, 16, -1,\n\t\t17, 18, 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -1, 33,\n\t\t34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,\n\t\t57, -1, -1, -1, -1, -1\n\t];\n\n\t/**\n\t * Convert the base 58 string to a byte array.\n\t * @param base58 The base58 string to convert.\n\t * @returns The byte array.\n\t * @throws If the input string contains a character not in the Base58 alphabet.\n\t */\n\tpublic static decode(base58: string): Uint8Array {\n\t\tGuards.string(Base58.CLASS_NAME, nameof(base58), base58);\n\n\t\tlet zeroes = 0;\n\t\tfor (let i = 0; i < base58.length; i++) {\n\t\t\tif (base58[i] !== \"1\") {\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tzeroes += 1;\n\t\t}\n\n\t\tconst size = Math.trunc((base58.length * 733) / 1000) + 1;\n\t\tconst b256 = new Uint8Array(size).fill(0);\n\n\t\tlet length = 0;\n\n\t\tfor (let i = zeroes; i < base58.length; i++) {\n\t\t\tconst ch = base58.charCodeAt(i);\n\n\t\t\tif (ch & 0xff80) {\n\t\t\t\tthrow new GeneralError(Base58.CLASS_NAME, \"invalidCharacter\", { invalidCharacter: ch });\n\t\t\t}\n\n\t\t\tconst val = Base58._ALPHABET_REVERSE[ch];\n\t\t\tif (val === -1) {\n\t\t\t\tthrow new GeneralError(Base58.CLASS_NAME, \"invalidCharacter\", { invalidCharacter: ch });\n\t\t\t}\n\n\t\t\tlet carry = val;\n\t\t\tlet j = 0;\n\n\t\t\tfor (let k = size - 1; k >= 0; k--, j++) {\n\t\t\t\tif (carry === 0 && j >= length) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tcarry += b256[k] * 58;\n\t\t\t\tb256[k] = carry;\n\t\t\t\tcarry >>>= 8;\n\t\t\t}\n\n\t\t\tlength = j;\n\t\t}\n\n\t\tconst out = new Uint8Array(zeroes + length);\n\n\t\tlet j;\n\t\tfor (j = 0; j < zeroes; j++) {\n\t\t\tout[j] = 0;\n\t\t}\n\n\t\tlet i = size - length;\n\t\twhile (i < size) {\n\t\t\tout[j++] = b256[i++];\n\t\t}\n\n\t\treturn out;\n\t}\n\n\t/**\n\t * Convert a byte array to base 58.\n\t * @param bytes The byte array to encode.\n\t * @returns The data as base58 string.\n\t */\n\tpublic static encode(bytes: Uint8Array): string {\n\t\tGuards.uint8Array(Base58.CLASS_NAME, nameof(bytes), bytes);\n\n\t\tlet zeroes = 0;\n\n\t\tfor (let i = 0; i < bytes.length; i++) {\n\t\t\tif (bytes[i] !== 0) {\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tzeroes += 1;\n\t\t}\n\n\t\tconst size = Math.trunc(((bytes.length - zeroes) * 138) / 100) + 1;\n\t\tconst b58 = new Uint8Array(size).fill(0);\n\n\t\tlet length = 0;\n\n\t\tfor (let i = zeroes; i < bytes.length; i++) {\n\t\t\tlet carry = bytes[i];\n\t\t\tlet j = 0;\n\n\t\t\tfor (let k = size - 1; k >= 0; k--, j++) {\n\t\t\t\tif (carry === 0 && j >= length) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tcarry += b58[k] * 256;\n\t\t\t\tb58[k] = carry % 58;\n\t\t\t\tcarry = Math.trunc(carry / 58);\n\t\t\t}\n\n\t\t\tlength = j;\n\t\t}\n\n\t\tlet i = size - length;\n\t\twhile (i < size && b58[i] === 0) {\n\t\t\ti += 1;\n\t\t}\n\n\t\tlet str = \"\";\n\n\t\tfor (let j = 0; j < zeroes; j++) {\n\t\t\tstr += \"1\";\n\t\t}\n\n\t\twhile (i < size) {\n\t\t\tstr += Base58._ALPHABET[b58[i++]];\n\t\t}\n\n\t\treturn str;\n\t}\n}\n"]}
@@ -0,0 +1,231 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ /* eslint-disable no-bitwise */
4
+ /* eslint-disable no-mixed-operators */
5
+ import { GeneralError } from "../errors/generalError.js";
6
+ import { Guards } from "../utils/guards.js";
7
+ /**
8
+ * Class to help with base64 Encoding/Decoding.
9
+ * Sourced from https://github.com/beatgammit/base64-js.
10
+ */
11
+ export class Base64 {
12
+ /**
13
+ * Runtime name for the class.
14
+ */
15
+ static CLASS_NAME = "Base64";
16
+ /**
17
+ * Alphabet table for encoding.
18
+ * @internal
19
+ */
20
+ static _LOOKUP = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
21
+ /**
22
+ * Alphabet table for decoding.
23
+ * @internal
24
+ */
25
+ static _REVERSE_LOOKUP = {
26
+ "43": 62,
27
+ "45": 62,
28
+ "47": 63,
29
+ "48": 52,
30
+ "49": 53,
31
+ "50": 54,
32
+ "51": 55,
33
+ "52": 56,
34
+ "53": 57,
35
+ "54": 58,
36
+ "55": 59,
37
+ "56": 60,
38
+ "57": 61,
39
+ "65": 0,
40
+ "66": 1,
41
+ "67": 2,
42
+ "68": 3,
43
+ "69": 4,
44
+ "70": 5,
45
+ "71": 6,
46
+ "72": 7,
47
+ "73": 8,
48
+ "74": 9,
49
+ "75": 10,
50
+ "76": 11,
51
+ "77": 12,
52
+ "78": 13,
53
+ "79": 14,
54
+ "80": 15,
55
+ "81": 16,
56
+ "82": 17,
57
+ "83": 18,
58
+ "84": 19,
59
+ "85": 20,
60
+ "86": 21,
61
+ "87": 22,
62
+ "88": 23,
63
+ "89": 24,
64
+ "90": 25,
65
+ "95": 63,
66
+ "97": 26,
67
+ "98": 27,
68
+ "99": 28,
69
+ "100": 29,
70
+ "101": 30,
71
+ "102": 31,
72
+ "103": 32,
73
+ "104": 33,
74
+ "105": 34,
75
+ "106": 35,
76
+ "107": 36,
77
+ "108": 37,
78
+ "109": 38,
79
+ "110": 39,
80
+ "111": 40,
81
+ "112": 41,
82
+ "113": 42,
83
+ "114": 43,
84
+ "115": 44,
85
+ "116": 45,
86
+ "117": 46,
87
+ "118": 47,
88
+ "119": 48,
89
+ "120": 49,
90
+ "121": 50,
91
+ "122": 51
92
+ };
93
+ /**
94
+ * Get the byte length of the data.
95
+ * @param base64 The base64 string.
96
+ * @returns The byte length of the data.
97
+ */
98
+ static byteLength(base64) {
99
+ const lens = Base64.getLengths(base64);
100
+ return Base64.calcByteLength(lens[0], lens[1]);
101
+ }
102
+ /**
103
+ * Convert the base 64 string to a byte array.
104
+ * @param base64 The base64 string to convert.
105
+ * @returns The byte array.
106
+ */
107
+ static decode(base64) {
108
+ Guards.string(Base64.CLASS_NAME, "base64", base64);
109
+ let tmp;
110
+ const lens = Base64.getLengths(base64);
111
+ const validLen = lens[0];
112
+ const placeHoldersLen = lens[1];
113
+ const arr = new Uint8Array(Base64.calcByteLength(validLen, placeHoldersLen));
114
+ let curByte = 0;
115
+ // if there are placeholders, only get up to the last complete 4 chars
116
+ const len = placeHoldersLen > 0 ? validLen - 4 : validLen;
117
+ let i;
118
+ for (i = 0; i < len; i += 4) {
119
+ tmp =
120
+ (Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 18) |
121
+ (Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 12) |
122
+ (Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] << 6) |
123
+ Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 3)];
124
+ arr[curByte++] = (tmp >> 16) & 0xff;
125
+ arr[curByte++] = (tmp >> 8) & 0xff;
126
+ arr[curByte++] = tmp & 0xff;
127
+ }
128
+ if (placeHoldersLen === 2) {
129
+ tmp =
130
+ (Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 2) |
131
+ (Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] >> 4);
132
+ arr[curByte++] = tmp & 0xff;
133
+ }
134
+ if (placeHoldersLen === 1) {
135
+ tmp =
136
+ (Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 10) |
137
+ (Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 4) |
138
+ (Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] >> 2);
139
+ arr[curByte++] = (tmp >> 8) & 0xff;
140
+ arr[curByte++] = tmp & 0xff;
141
+ }
142
+ return arr;
143
+ }
144
+ /**
145
+ * Convert a byte array to base 64.
146
+ * @param bytes The byte array to convert.
147
+ * @returns The data as base64 string.
148
+ */
149
+ static encode(bytes) {
150
+ Guards.uint8Array(Base64.CLASS_NAME, "bytes", bytes);
151
+ let tmp;
152
+ const len = bytes.length;
153
+ const extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
154
+ const parts = [];
155
+ const maxChunkLength = 16383; // must be multiple of 3
156
+ // go through the array every three bytes, we'll deal with trailing stuff later
157
+ for (let i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
158
+ parts.push(Base64.encodeChunk(bytes, i, Math.min(i + maxChunkLength, len2)));
159
+ }
160
+ // pad the end with zeros, but make sure to not forget the extra bytes
161
+ if (extraBytes === 1) {
162
+ tmp = bytes[len - 1];
163
+ parts.push(`${Base64._LOOKUP[tmp >> 2] + Base64._LOOKUP[(tmp << 4) & 0x3f]}==`);
164
+ }
165
+ else if (extraBytes === 2) {
166
+ tmp = (bytes[len - 2] << 8) + bytes[len - 1];
167
+ parts.push(`${Base64._LOOKUP[tmp >> 10] + Base64._LOOKUP[(tmp >> 4) & 0x3f] + Base64._LOOKUP[(tmp << 2) & 0x3f]}=`);
168
+ }
169
+ return parts.join("");
170
+ }
171
+ /**
172
+ * Calculate the byte length.
173
+ * @param validLen The valid length.
174
+ * @param placeHoldersLen The placeholder length.
175
+ * @returns The length.
176
+ * @internal
177
+ */
178
+ static calcByteLength(validLen, placeHoldersLen) {
179
+ return ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen;
180
+ }
181
+ /**
182
+ * Get the valid and placeholder lengths from a bas64 string.
183
+ * @param base64 The base64 string.
184
+ * @returns The lengths.
185
+ * @internal
186
+ */
187
+ static getLengths(base64) {
188
+ const len = base64.length;
189
+ if (len % 4 > 0) {
190
+ throw new GeneralError(Base64.CLASS_NAME, "length4Multiple", { value: len });
191
+ }
192
+ // Trim off extra bytes after placeholder bytes are found
193
+ // See: https://github.com/beatgammit/base64-js/issues/42
194
+ let validLen = base64.indexOf("=");
195
+ if (validLen === -1) {
196
+ validLen = len;
197
+ }
198
+ const placeHoldersLen = validLen === len ? 0 : 4 - (validLen % 4);
199
+ return [validLen, placeHoldersLen];
200
+ }
201
+ /**
202
+ * Convert the triplet to base 64.
203
+ * @param num The number to convert.
204
+ * @returns The base64 encoding.
205
+ * @internal
206
+ */
207
+ static tripletToBase64(num) {
208
+ return (Base64._LOOKUP[(num >> 18) & 0x3f] +
209
+ Base64._LOOKUP[(num >> 12) & 0x3f] +
210
+ Base64._LOOKUP[(num >> 6) & 0x3f] +
211
+ Base64._LOOKUP[num & 0x3f]);
212
+ }
213
+ /**
214
+ * Encode a chunk.
215
+ * @param bytes The byte array.
216
+ * @param start The start index in the buffer.
217
+ * @param end The end index in the buffer.
218
+ * @returns The encoded chunk.
219
+ * @internal
220
+ */
221
+ static encodeChunk(bytes, start, end) {
222
+ let tmp;
223
+ const output = [];
224
+ for (let i = start; i < end; i += 3) {
225
+ tmp = ((bytes[i] << 16) & 0xff0000) + ((bytes[i + 1] << 8) & 0xff00) + (bytes[i + 2] & 0xff);
226
+ output.push(Base64.tripletToBase64(tmp));
227
+ }
228
+ return output.join("");
229
+ }
230
+ }
231
+ //# sourceMappingURL=base64.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.js","sourceRoot":"","sources":["../../../src/encoding/base64.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,+BAA+B;AAC/B,uCAAuC;AAGvC,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;;GAGG;AACH,MAAM,OAAO,MAAM;IAClB;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;OAGG;IACK,MAAM,CAAU,OAAO,GAC9B,kEAAkE,CAAC;IAEpE;;;OAGG;IACK,MAAM,CAAU,eAAe,GAA6B;QACnE,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,C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AAI,CAAC;YACjC,MAAM,CAAC,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC,CAC1B,CAAC;IACH,CAAC;IAED;;;;;;;OAOG;IACK,MAAM,CAAC,WAAW,CAAC,KAAiB,EAAE,KAAa,EAAE,GAAW;QACvE,IAAI,GAAG,CAAC;QACR,MAAM,MAAM,GAAG,EAAE,CAAC;QAClB,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;YACrC,GAAG,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;YAC7F,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC;QAC1C,CAAC;QACD,OAAO,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACxB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable no-bitwise */\n/* eslint-disable no-mixed-operators */\n\nimport { nameof } from \"@twin.org/nameof\";\nimport { GeneralError } from \"../errors/generalError.js\";\nimport { Guards } from \"../utils/guards.js\";\n\n/**\n * Class to help with base64 Encoding/Decoding.\n * Sourced from https://github.com/beatgammit/base64-js.\n */\nexport class Base64 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Base64>();\n\n\t/**\n\t * Alphabet table for encoding.\n\t * @internal\n\t */\n\tprivate static readonly _LOOKUP: string =\n\t\t\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\";\n\n\t/**\n\t * Alphabet table for decoding.\n\t * @internal\n\t */\n\tprivate static readonly _REVERSE_LOOKUP: { [id: number]: number } = {\n\t\t\"43\": 62,\n\t\t\"45\": 62,\n\t\t\"47\": 63,\n\t\t\"48\": 52,\n\t\t\"49\": 53,\n\t\t\"50\": 54,\n\t\t\"51\": 55,\n\t\t\"52\": 56,\n\t\t\"53\": 57,\n\t\t\"54\": 58,\n\t\t\"55\": 59,\n\t\t\"56\": 60,\n\t\t\"57\": 61,\n\t\t\"65\": 0,\n\t\t\"66\": 1,\n\t\t\"67\": 2,\n\t\t\"68\": 3,\n\t\t\"69\": 4,\n\t\t\"70\": 5,\n\t\t\"71\": 6,\n\t\t\"72\": 7,\n\t\t\"73\": 8,\n\t\t\"74\": 9,\n\t\t\"75\": 10,\n\t\t\"76\": 11,\n\t\t\"77\": 12,\n\t\t\"78\": 13,\n\t\t\"79\": 14,\n\t\t\"80\": 15,\n\t\t\"81\": 16,\n\t\t\"82\": 17,\n\t\t\"83\": 18,\n\t\t\"84\": 19,\n\t\t\"85\": 20,\n\t\t\"86\": 21,\n\t\t\"87\": 22,\n\t\t\"88\": 23,\n\t\t\"89\": 24,\n\t\t\"90\": 25,\n\t\t\"95\": 63,\n\t\t\"97\": 26,\n\t\t\"98\": 27,\n\t\t\"99\": 28,\n\t\t\"100\": 29,\n\t\t\"101\": 30,\n\t\t\"102\": 31,\n\t\t\"103\": 32,\n\t\t\"104\": 33,\n\t\t\"105\": 34,\n\t\t\"106\": 35,\n\t\t\"107\": 36,\n\t\t\"108\": 37,\n\t\t\"109\": 38,\n\t\t\"110\": 39,\n\t\t\"111\": 40,\n\t\t\"112\": 41,\n\t\t\"113\": 42,\n\t\t\"114\": 43,\n\t\t\"115\": 44,\n\t\t\"116\": 45,\n\t\t\"117\": 46,\n\t\t\"118\": 47,\n\t\t\"119\": 48,\n\t\t\"120\": 49,\n\t\t\"121\": 50,\n\t\t\"122\": 51\n\t};\n\n\t/**\n\t * Get the byte length of the data.\n\t * @param base64 The base64 string.\n\t * @returns The byte length of the data.\n\t */\n\tpublic static byteLength(base64: string): number {\n\t\tconst lens = Base64.getLengths(base64);\n\t\treturn Base64.calcByteLength(lens[0], lens[1]);\n\t}\n\n\t/**\n\t * Convert the base 64 string to a byte array.\n\t * @param base64 The base64 string to convert.\n\t * @returns The byte array.\n\t */\n\tpublic static decode(base64: string): Uint8Array {\n\t\tGuards.string(Base64.CLASS_NAME, nameof(base64), base64);\n\n\t\tlet tmp;\n\t\tconst lens = Base64.getLengths(base64);\n\t\tconst validLen = lens[0];\n\t\tconst placeHoldersLen = lens[1];\n\n\t\tconst arr = new Uint8Array(Base64.calcByteLength(validLen, placeHoldersLen));\n\n\t\tlet curByte = 0;\n\n\t\t// if there are placeholders, only get up to the last complete 4 chars\n\t\tconst len = placeHoldersLen > 0 ? validLen - 4 : validLen;\n\n\t\tlet i;\n\t\tfor (i = 0; i < len; i += 4) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 18) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 12) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] << 6) |\n\t\t\t\tBase64._REVERSE_LOOKUP[base64.charCodeAt(i + 3)];\n\t\t\tarr[curByte++] = (tmp >> 16) & 0xff;\n\t\t\tarr[curByte++] = (tmp >> 8) & 0xff;\n\t\t\tarr[curByte++] = tmp & 0xff;\n\t\t}\n\n\t\tif (placeHoldersLen === 2) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 2) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] >> 4);\n\t\t\tarr[curByte++] = tmp & 0xff;\n\t\t}\n\n\t\tif (placeHoldersLen === 1) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 10) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 4) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] >> 2);\n\t\t\tarr[curByte++] = (tmp >> 8) & 0xff;\n\t\t\tarr[curByte++] = tmp & 0xff;\n\t\t}\n\n\t\treturn arr;\n\t}\n\n\t/**\n\t * Convert a byte array to base 64.\n\t * @param bytes The byte array to convert.\n\t * @returns The data as base64 string.\n\t */\n\tpublic static encode(bytes: Uint8Array): string {\n\t\tGuards.uint8Array(Base64.CLASS_NAME, nameof(bytes), bytes);\n\n\t\tlet tmp;\n\t\tconst len = bytes.length;\n\t\tconst extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes\n\t\tconst parts = [];\n\t\tconst maxChunkLength = 16383; // must be multiple of 3\n\n\t\t// go through the array every three bytes, we'll deal with trailing stuff later\n\t\tfor (let i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {\n\t\t\tparts.push(Base64.encodeChunk(bytes, i, Math.min(i + maxChunkLength, len2)));\n\t\t}\n\n\t\t// pad the end with zeros, but make sure to not forget the extra bytes\n\t\tif (extraBytes === 1) {\n\t\t\ttmp = bytes[len - 1];\n\t\t\tparts.push(`${Base64._LOOKUP[tmp >> 2] + Base64._LOOKUP[(tmp << 4) & 0x3f]}==`);\n\t\t} else if (extraBytes === 2) {\n\t\t\ttmp = (bytes[len - 2] << 8) + bytes[len - 1];\n\t\t\tparts.push(\n\t\t\t\t`${Base64._LOOKUP[tmp >> 10] + Base64._LOOKUP[(tmp >> 4) & 0x3f] + Base64._LOOKUP[(tmp << 2) & 0x3f]}=`\n\t\t\t);\n\t\t}\n\n\t\treturn parts.join(\"\");\n\t}\n\n\t/**\n\t * Calculate the byte length.\n\t * @param validLen The valid length.\n\t * @param placeHoldersLen The placeholder length.\n\t * @returns The length.\n\t * @internal\n\t */\n\tprivate static calcByteLength(validLen: number, placeHoldersLen: number): number {\n\t\treturn ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen;\n\t}\n\n\t/**\n\t * Get the valid and placeholder lengths from a bas64 string.\n\t * @param base64 The base64 string.\n\t * @returns The lengths.\n\t * @internal\n\t */\n\tprivate static getLengths(base64: string): number[] {\n\t\tconst len = base64.length;\n\n\t\tif (len % 4 > 0) {\n\t\t\tthrow new GeneralError(Base64.CLASS_NAME, \"length4Multiple\", { value: len });\n\t\t}\n\n\t\t// Trim off extra bytes after placeholder bytes are found\n\t\t// See: https://github.com/beatgammit/base64-js/issues/42\n\t\tlet validLen = base64.indexOf(\"=\");\n\t\tif (validLen === -1) {\n\t\t\tvalidLen = len;\n\t\t}\n\n\t\tconst placeHoldersLen = validLen === len ? 0 : 4 - (validLen % 4);\n\n\t\treturn [validLen, placeHoldersLen];\n\t}\n\n\t/**\n\t * Convert the triplet to base 64.\n\t * @param num The number to convert.\n\t * @returns The base64 encoding.\n\t * @internal\n\t */\n\tprivate static tripletToBase64(num: number): string {\n\t\treturn (\n\t\t\tBase64._LOOKUP[(num >> 18) & 0x3f] +\n\t\t\tBase64._LOOKUP[(num >> 12) & 0x3f] +\n\t\t\tBase64._LOOKUP[(num >> 6) & 0x3f] +\n\t\t\tBase64._LOOKUP[num & 0x3f]\n\t\t);\n\t}\n\n\t/**\n\t * Encode a chunk.\n\t * @param bytes The byte array.\n\t * @param start The start index in the buffer.\n\t * @param end The end index in the buffer.\n\t * @returns The encoded chunk.\n\t * @internal\n\t */\n\tprivate static encodeChunk(bytes: Uint8Array, start: number, end: number): string {\n\t\tlet tmp;\n\t\tconst output = [];\n\t\tfor (let i = start; i < end; i += 3) {\n\t\t\ttmp = ((bytes[i] << 16) & 0xff0000) + ((bytes[i + 1] << 8) & 0xff00) + (bytes[i + 2] & 0xff);\n\t\t\toutput.push(Base64.tripletToBase64(tmp));\n\t\t}\n\t\treturn output.join(\"\");\n\t}\n}\n"]}
@@ -0,0 +1,42 @@
1
+ import { Base64 } from "./base64.js";
2
+ import { Guards } from "../utils/guards.js";
3
+ /**
4
+ * Class to help with base64 URL Encoding/Decoding.
5
+ * https://www.rfc-editor.org/rfc/rfc4648#section-5.
6
+ */
7
+ export class Base64Url {
8
+ /**
9
+ * Runtime name for the class.
10
+ */
11
+ static CLASS_NAME = "Base64";
12
+ /**
13
+ * Convert the base 64 string to a byte array.
14
+ * @param base64Url The base64 url string to convert.
15
+ * @returns The byte array.
16
+ */
17
+ static decode(base64Url) {
18
+ Guards.string(Base64Url.CLASS_NAME, "base64Url", base64Url);
19
+ let base64 = base64Url;
20
+ // Base 64 url can have padding removed, so add it back if it is missing.
21
+ if (base64.length > 0 && !base64.endsWith("=")) {
22
+ const placeHoldersLen = 4 - (base64.length % 4);
23
+ if (placeHoldersLen > 0 && placeHoldersLen < 4) {
24
+ base64 = base64.padEnd(base64.length + placeHoldersLen, "=");
25
+ }
26
+ }
27
+ base64 = base64.replace(/-/g, "+").replace(/_/g, "/");
28
+ return Base64.decode(base64);
29
+ }
30
+ /**
31
+ * Convert a byte array to base 64 url.
32
+ * @param bytes The byte array to convert.
33
+ * @returns The data as base64 url string.
34
+ */
35
+ static encode(bytes) {
36
+ Guards.uint8Array(Base64Url.CLASS_NAME, "bytes", bytes);
37
+ const base64 = Base64.encode(bytes);
38
+ // Base 64 url can have padding removed, so remove it.
39
+ return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
40
+ }
41
+ }
42
+ //# sourceMappingURL=base64Url.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64Url.js","sourceRoot":"","sources":["../../../src/encoding/base64Url.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;;GAGG;AACH,MAAM,OAAO,SAAS;IACrB;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,SAAiB;QACrC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,UAAU,eAAqB,SAAS,CAAC,CAAC;QAElE,IAAI,MAAM,GAAG,SAAS,CAAC;QAEvB,yEAAyE;QACzE,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YAChD,MAAM,eAAe,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;YAChD,IAAI,eAAe,GAAG,CAAC,IAAI,eAAe,GAAG,CAAC,EAAE,CAAC;gBAChD,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,eAAe,EAAE,GAAG,CAAC,CAAC;YAC9D,CAAC;QACF,CAAC;QAED,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QACtD,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC9B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,KAAiB;QACrC,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAE9D,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACpC,sDAAsD;QACtD,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACzE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { Base64 } from \"./base64.js\";\nimport { Guards } from \"../utils/guards.js\";\n\n/**\n * Class to help with base64 URL Encoding/Decoding.\n * https://www.rfc-editor.org/rfc/rfc4648#section-5.\n */\nexport class Base64Url {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Base64>();\n\n\t/**\n\t * Convert the base 64 string to a byte array.\n\t * @param base64Url The base64 url string to convert.\n\t * @returns The byte array.\n\t */\n\tpublic static decode(base64Url: string): Uint8Array {\n\t\tGuards.string(Base64Url.CLASS_NAME, nameof(base64Url), base64Url);\n\n\t\tlet base64 = base64Url;\n\n\t\t// Base 64 url can have padding removed, so add it back if it is missing.\n\t\tif (base64.length > 0 && !base64.endsWith(\"=\")) {\n\t\t\tconst placeHoldersLen = 4 - (base64.length % 4);\n\t\t\tif (placeHoldersLen > 0 && placeHoldersLen < 4) {\n\t\t\t\tbase64 = base64.padEnd(base64.length + placeHoldersLen, \"=\");\n\t\t\t}\n\t\t}\n\n\t\tbase64 = base64.replace(/-/g, \"+\").replace(/_/g, \"/\");\n\t\treturn Base64.decode(base64);\n\t}\n\n\t/**\n\t * Convert a byte array to base 64 url.\n\t * @param bytes The byte array to convert.\n\t * @returns The data as base64 url string.\n\t */\n\tpublic static encode(bytes: Uint8Array): string {\n\t\tGuards.uint8Array(Base64Url.CLASS_NAME, nameof(bytes), bytes);\n\n\t\tconst base64 = Base64.encode(bytes);\n\t\t// Base 64 url can have padding removed, so remove it.\n\t\treturn base64.replace(/\\+/g, \"-\").replace(/\\//g, \"_\").replace(/=/g, \"\");\n\t}\n}\n"]}
@@ -0,0 +1,22 @@
1
+ import { BaseError } from "./baseError.js";
2
+ /**
3
+ * Class to handle errors which are triggered by data already existing.
4
+ */
5
+ export class AlreadyExistsError extends BaseError {
6
+ /**
7
+ * Runtime name for the class.
8
+ */
9
+ static CLASS_NAME = "AlreadyExistsError";
10
+ /**
11
+ * Create a new instance of AlreadyExistsError.
12
+ * @param source The source of the error.
13
+ * @param message The message as a code.
14
+ * @param existingId The id for the item.
15
+ * @param properties Any additional information for the error.
16
+ * @param cause The cause of the error if we have wrapped another error.
17
+ */
18
+ constructor(source, message, existingId, properties, cause) {
19
+ super(AlreadyExistsError.CLASS_NAME, source, message, { existingId, ...properties }, cause);
20
+ }
21
+ }
22
+ //# sourceMappingURL=alreadyExistsError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"alreadyExistsError.js","sourceRoot":"","sources":["../../../src/errors/alreadyExistsError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,kBAAmB,SAAQ,SAAS;IAChD;;OAEG;IACI,MAAM,CAAU,UAAU,wBAAwC;IAEzE;;;;;;;OAOG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAmB,EACnB,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,kBAAkB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,UAAU,EAAE,GAAG,UAAU,EAAE,EAAE,KAAK,CAAC,CAAC;IAC7F,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors which are triggered by data already existing.\n */\nexport class AlreadyExistsError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<AlreadyExistsError>();\n\n\t/**\n\t * Create a new instance of AlreadyExistsError.\n\t * @param source The source of the error.\n\t * @param message The message as a code.\n\t * @param existingId The id for the item.\n\t * @param properties Any additional information for the error.\n\t * @param cause The cause of the error if we have wrapped another error.\n\t */\n\tconstructor(\n\t\tsource: string,\n\t\tmessage: string,\n\t\texistingId?: string,\n\t\tproperties?: { [id: string]: unknown },\n\t\tcause?: unknown\n\t) {\n\t\tsuper(AlreadyExistsError.CLASS_NAME, source, message, { existingId, ...properties }, cause);\n\t}\n}\n"]}