@qrkit/bc-ur 2.0.0-beta.9-qrkit.1

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 (229) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +996 -0
  3. package/dist/commonjs/classes/FountainDecoder.d.ts +125 -0
  4. package/dist/commonjs/classes/FountainDecoder.js +453 -0
  5. package/dist/commonjs/classes/FountainDecoder.js.map +1 -0
  6. package/dist/commonjs/classes/FountainEncoder.d.ts +63 -0
  7. package/dist/commonjs/classes/FountainEncoder.js +168 -0
  8. package/dist/commonjs/classes/FountainEncoder.js.map +1 -0
  9. package/dist/commonjs/classes/RegistryItem.d.ts +104 -0
  10. package/dist/commonjs/classes/RegistryItem.js +172 -0
  11. package/dist/commonjs/classes/RegistryItem.js.map +1 -0
  12. package/dist/commonjs/classes/UR.d.ts +89 -0
  13. package/dist/commonjs/classes/UR.js +243 -0
  14. package/dist/commonjs/classes/UR.js.map +1 -0
  15. package/dist/commonjs/classes/UrFountainDecoder.d.ts +15 -0
  16. package/dist/commonjs/classes/UrFountainDecoder.js +127 -0
  17. package/dist/commonjs/classes/UrFountainDecoder.js.map +1 -0
  18. package/dist/commonjs/classes/UrFountainEncoder.d.ts +42 -0
  19. package/dist/commonjs/classes/UrFountainEncoder.js +92 -0
  20. package/dist/commonjs/classes/UrFountainEncoder.js.map +1 -0
  21. package/dist/commonjs/classes/key.helper.d.ts +27 -0
  22. package/dist/commonjs/classes/key.helper.js +70 -0
  23. package/dist/commonjs/classes/key.helper.js.map +1 -0
  24. package/dist/commonjs/encodingMethods/BytewordEncoding.d.ts +11 -0
  25. package/dist/commonjs/encodingMethods/BytewordEncoding.js +23 -0
  26. package/dist/commonjs/encodingMethods/BytewordEncoding.js.map +1 -0
  27. package/dist/commonjs/encodingMethods/CborEncoding.d.ts +44 -0
  28. package/dist/commonjs/encodingMethods/CborEncoding.js +151 -0
  29. package/dist/commonjs/encodingMethods/CborEncoding.js.map +1 -0
  30. package/dist/commonjs/encodingMethods/HexEncoding.d.ts +8 -0
  31. package/dist/commonjs/encodingMethods/HexEncoding.js +24 -0
  32. package/dist/commonjs/encodingMethods/HexEncoding.js.map +1 -0
  33. package/dist/commonjs/encodingMethods/UrEncoding.d.ts +10 -0
  34. package/dist/commonjs/encodingMethods/UrEncoding.js +19 -0
  35. package/dist/commonjs/encodingMethods/UrEncoding.js.map +1 -0
  36. package/dist/commonjs/encodingMethods/bytewords.d.ts +27 -0
  37. package/dist/commonjs/encodingMethods/bytewords.js +152 -0
  38. package/dist/commonjs/encodingMethods/bytewords.js.map +1 -0
  39. package/dist/commonjs/encodingMethods/index.d.ts +19 -0
  40. package/dist/commonjs/encodingMethods/index.js +27 -0
  41. package/dist/commonjs/encodingMethods/index.js.map +1 -0
  42. package/dist/commonjs/encodingMethods/pipeline.d.ts +19 -0
  43. package/dist/commonjs/encodingMethods/pipeline.js +80 -0
  44. package/dist/commonjs/encodingMethods/pipeline.js.map +1 -0
  45. package/dist/commonjs/enums/EncodingMethodName.d.ts +10 -0
  46. package/dist/commonjs/enums/EncodingMethodName.js +15 -0
  47. package/dist/commonjs/enums/EncodingMethodName.js.map +1 -0
  48. package/dist/commonjs/errors.d.ts +15 -0
  49. package/dist/commonjs/errors.js +39 -0
  50. package/dist/commonjs/errors.js.map +1 -0
  51. package/dist/commonjs/helpers/aliasSampling.d.ts +6 -0
  52. package/dist/commonjs/helpers/aliasSampling.js +73 -0
  53. package/dist/commonjs/helpers/aliasSampling.js.map +1 -0
  54. package/dist/commonjs/helpers/crc32.d.ts +1 -0
  55. package/dist/commonjs/helpers/crc32.js +19 -0
  56. package/dist/commonjs/helpers/crc32.js.map +1 -0
  57. package/dist/commonjs/helpers/fountainUtils.d.ts +40 -0
  58. package/dist/commonjs/helpers/fountainUtils.js +124 -0
  59. package/dist/commonjs/helpers/fountainUtils.js.map +1 -0
  60. package/dist/commonjs/helpers/sha256.d.ts +1 -0
  61. package/dist/commonjs/helpers/sha256.js +78 -0
  62. package/dist/commonjs/helpers/sha256.js.map +1 -0
  63. package/dist/commonjs/helpers/type.helper.d.ts +3 -0
  64. package/dist/commonjs/helpers/type.helper.js +3 -0
  65. package/dist/commonjs/helpers/type.helper.js.map +1 -0
  66. package/dist/commonjs/helpers/uintArrayHelper.d.ts +287 -0
  67. package/dist/commonjs/helpers/uintArrayHelper.js +545 -0
  68. package/dist/commonjs/helpers/uintArrayHelper.js.map +1 -0
  69. package/dist/commonjs/helpers/utils.d.ts +55 -0
  70. package/dist/commonjs/helpers/utils.js +123 -0
  71. package/dist/commonjs/helpers/utils.js.map +1 -0
  72. package/dist/commonjs/index-react-native.d.ts +9 -0
  73. package/dist/commonjs/index-react-native.js +15 -0
  74. package/dist/commonjs/index-react-native.js.map +1 -0
  75. package/dist/commonjs/index.d.ts +21 -0
  76. package/dist/commonjs/index.js +34 -0
  77. package/dist/commonjs/index.js.map +1 -0
  78. package/dist/commonjs/interfaces/IEncodingMethod.d.ts +9 -0
  79. package/dist/commonjs/interfaces/IEncodingMethod.js +3 -0
  80. package/dist/commonjs/interfaces/IEncodingMethod.js.map +1 -0
  81. package/dist/commonjs/package.json +3 -0
  82. package/dist/commonjs/registry.d.ts +26 -0
  83. package/dist/commonjs/registry.js +118 -0
  84. package/dist/commonjs/registry.js.map +1 -0
  85. package/dist/commonjs/test.utils.d.ts +31 -0
  86. package/dist/commonjs/test.utils.js +88 -0
  87. package/dist/commonjs/test.utils.js.map +1 -0
  88. package/dist/commonjs/wrappers/cbor2-cjs.cjs.map +1 -0
  89. package/dist/commonjs/wrappers/cbor2.d.ts +2 -0
  90. package/dist/commonjs/wrappers/cbor2.js +14 -0
  91. package/dist/commonjs/wrappers/cbor2Wrapper.d.ts +14 -0
  92. package/dist/commonjs/wrappers/cbor2Wrapper.js +49 -0
  93. package/dist/commonjs/wrappers/cbor2Wrapper.js.map +1 -0
  94. package/dist/commonjs/xoshiro.d.ts +12 -0
  95. package/dist/commonjs/xoshiro.js +52 -0
  96. package/dist/commonjs/xoshiro.js.map +1 -0
  97. package/dist/esm/classes/FountainDecoder.d.ts +125 -0
  98. package/dist/esm/classes/FountainDecoder.js +447 -0
  99. package/dist/esm/classes/FountainDecoder.js.map +1 -0
  100. package/dist/esm/classes/FountainEncoder.d.ts +63 -0
  101. package/dist/esm/classes/FountainEncoder.js +164 -0
  102. package/dist/esm/classes/FountainEncoder.js.map +1 -0
  103. package/dist/esm/classes/RegistryItem.d.ts +104 -0
  104. package/dist/esm/classes/RegistryItem.js +166 -0
  105. package/dist/esm/classes/RegistryItem.js.map +1 -0
  106. package/dist/esm/classes/UR.d.ts +89 -0
  107. package/dist/esm/classes/UR.js +239 -0
  108. package/dist/esm/classes/UR.js.map +1 -0
  109. package/dist/esm/classes/UrFountainDecoder.d.ts +15 -0
  110. package/dist/esm/classes/UrFountainDecoder.js +123 -0
  111. package/dist/esm/classes/UrFountainDecoder.js.map +1 -0
  112. package/dist/esm/classes/UrFountainEncoder.d.ts +42 -0
  113. package/dist/esm/classes/UrFountainEncoder.js +88 -0
  114. package/dist/esm/classes/UrFountainEncoder.js.map +1 -0
  115. package/dist/esm/classes/key.helper.d.ts +27 -0
  116. package/dist/esm/classes/key.helper.js +66 -0
  117. package/dist/esm/classes/key.helper.js.map +1 -0
  118. package/dist/esm/encodingMethods/BytewordEncoding.d.ts +11 -0
  119. package/dist/esm/encodingMethods/BytewordEncoding.js +19 -0
  120. package/dist/esm/encodingMethods/BytewordEncoding.js.map +1 -0
  121. package/dist/esm/encodingMethods/CborEncoding.d.ts +44 -0
  122. package/dist/esm/encodingMethods/CborEncoding.js +147 -0
  123. package/dist/esm/encodingMethods/CborEncoding.js.map +1 -0
  124. package/dist/esm/encodingMethods/HexEncoding.d.ts +8 -0
  125. package/dist/esm/encodingMethods/HexEncoding.js +20 -0
  126. package/dist/esm/encodingMethods/HexEncoding.js.map +1 -0
  127. package/dist/esm/encodingMethods/UrEncoding.d.ts +10 -0
  128. package/dist/esm/encodingMethods/UrEncoding.js +15 -0
  129. package/dist/esm/encodingMethods/UrEncoding.js.map +1 -0
  130. package/dist/esm/encodingMethods/bytewords.d.ts +27 -0
  131. package/dist/esm/encodingMethods/bytewords.js +147 -0
  132. package/dist/esm/encodingMethods/bytewords.js.map +1 -0
  133. package/dist/esm/encodingMethods/index.d.ts +19 -0
  134. package/dist/esm/encodingMethods/index.js +24 -0
  135. package/dist/esm/encodingMethods/index.js.map +1 -0
  136. package/dist/esm/encodingMethods/pipeline.d.ts +19 -0
  137. package/dist/esm/encodingMethods/pipeline.js +76 -0
  138. package/dist/esm/encodingMethods/pipeline.js.map +1 -0
  139. package/dist/esm/enums/EncodingMethodName.d.ts +10 -0
  140. package/dist/esm/enums/EncodingMethodName.js +12 -0
  141. package/dist/esm/enums/EncodingMethodName.js.map +1 -0
  142. package/dist/esm/errors.d.ts +15 -0
  143. package/dist/esm/errors.js +31 -0
  144. package/dist/esm/errors.js.map +1 -0
  145. package/dist/esm/helpers/aliasSampling.d.ts +6 -0
  146. package/dist/esm/helpers/aliasSampling.js +70 -0
  147. package/dist/esm/helpers/aliasSampling.js.map +1 -0
  148. package/dist/esm/helpers/crc32.d.ts +1 -0
  149. package/dist/esm/helpers/crc32.js +16 -0
  150. package/dist/esm/helpers/crc32.js.map +1 -0
  151. package/dist/esm/helpers/fountainUtils.d.ts +40 -0
  152. package/dist/esm/helpers/fountainUtils.js +114 -0
  153. package/dist/esm/helpers/fountainUtils.js.map +1 -0
  154. package/dist/esm/helpers/sha256.d.ts +1 -0
  155. package/dist/esm/helpers/sha256.js +75 -0
  156. package/dist/esm/helpers/sha256.js.map +1 -0
  157. package/dist/esm/helpers/type.helper.d.ts +3 -0
  158. package/dist/esm/helpers/type.helper.js +2 -0
  159. package/dist/esm/helpers/type.helper.js.map +1 -0
  160. package/dist/esm/helpers/uintArrayHelper.d.ts +287 -0
  161. package/dist/esm/helpers/uintArrayHelper.js +526 -0
  162. package/dist/esm/helpers/uintArrayHelper.js.map +1 -0
  163. package/dist/esm/helpers/utils.d.ts +55 -0
  164. package/dist/esm/helpers/utils.js +102 -0
  165. package/dist/esm/helpers/utils.js.map +1 -0
  166. package/dist/esm/index-react-native.d.ts +9 -0
  167. package/dist/esm/index-react-native.js +12 -0
  168. package/dist/esm/index-react-native.js.map +1 -0
  169. package/dist/esm/index.d.ts +21 -0
  170. package/dist/esm/index.js +16 -0
  171. package/dist/esm/index.js.map +1 -0
  172. package/dist/esm/interfaces/IEncodingMethod.d.ts +9 -0
  173. package/dist/esm/interfaces/IEncodingMethod.js +2 -0
  174. package/dist/esm/interfaces/IEncodingMethod.js.map +1 -0
  175. package/dist/esm/package.json +3 -0
  176. package/dist/esm/registry.d.ts +26 -0
  177. package/dist/esm/registry.js +114 -0
  178. package/dist/esm/registry.js.map +1 -0
  179. package/dist/esm/test.utils.d.ts +31 -0
  180. package/dist/esm/test.utils.js +83 -0
  181. package/dist/esm/test.utils.js.map +1 -0
  182. package/dist/esm/wrappers/cbor2-deno.d.mts +2 -0
  183. package/dist/esm/wrappers/cbor2-deno.mjs +5 -0
  184. package/dist/esm/wrappers/cbor2-deno.mjs.map +1 -0
  185. package/dist/esm/wrappers/cbor2.d.ts +2 -0
  186. package/dist/esm/wrappers/cbor2.js +5 -0
  187. package/dist/esm/wrappers/cbor2.js.map +1 -0
  188. package/dist/esm/wrappers/cbor2Wrapper.d.ts +5 -0
  189. package/dist/esm/wrappers/cbor2Wrapper.js +6 -0
  190. package/dist/esm/wrappers/cbor2Wrapper.js.map +1 -0
  191. package/dist/esm/xoshiro.d.ts +12 -0
  192. package/dist/esm/xoshiro.js +47 -0
  193. package/dist/esm/xoshiro.js.map +1 -0
  194. package/dist/web/bytewords.js +335 -0
  195. package/index.html +98 -0
  196. package/package.json +94 -0
  197. package/src/classes/FountainDecoder.ts +539 -0
  198. package/src/classes/FountainEncoder.ts +211 -0
  199. package/src/classes/RegistryItem.ts +240 -0
  200. package/src/classes/UR.ts +308 -0
  201. package/src/classes/UrFountainDecoder.ts +142 -0
  202. package/src/classes/UrFountainEncoder.ts +103 -0
  203. package/src/classes/key.helper.ts +85 -0
  204. package/src/encodingMethods/BytewordEncoding.ts +23 -0
  205. package/src/encodingMethods/CborEncoding.ts +196 -0
  206. package/src/encodingMethods/HexEncoding.ts +23 -0
  207. package/src/encodingMethods/UrEncoding.ts +19 -0
  208. package/src/encodingMethods/bytewords.ts +215 -0
  209. package/src/encodingMethods/index.ts +26 -0
  210. package/src/encodingMethods/pipeline.ts +103 -0
  211. package/src/enums/EncodingMethodName.ts +10 -0
  212. package/src/errors.ts +34 -0
  213. package/src/helpers/aliasSampling.ts +87 -0
  214. package/src/helpers/crc32.ts +19 -0
  215. package/src/helpers/fountainUtils.ts +157 -0
  216. package/src/helpers/sha256.ts +88 -0
  217. package/src/helpers/type.helper.ts +1 -0
  218. package/src/helpers/uintArrayHelper.ts +611 -0
  219. package/src/helpers/utils.ts +135 -0
  220. package/src/index-react-native.ts +12 -0
  221. package/src/index.ts +44 -0
  222. package/src/interfaces/IEncodingMethod.ts +10 -0
  223. package/src/registry.ts +146 -0
  224. package/src/test.utils.ts +105 -0
  225. package/src/wrappers/cbor2-cjs.cts +6 -0
  226. package/src/wrappers/cbor2-deno.mts +6 -0
  227. package/src/wrappers/cbor2.ts +7 -0
  228. package/src/wrappers/cbor2Wrapper.ts +14 -0
  229. package/src/xoshiro.ts +66 -0
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BytewordEncoding = void 0;
4
+ const bytewords_js_1 = require("./bytewords.js");
5
+ const EncodingMethodName_js_1 = require("../enums/EncodingMethodName.js");
6
+ class BytewordEncoding {
7
+ _name = EncodingMethodName_js_1.EncodingMethodName.bytewords;
8
+ _style;
9
+ get name() {
10
+ return this._name;
11
+ }
12
+ constructor(style = bytewords_js_1.STYLES.MINIMAL) {
13
+ this._style = style;
14
+ }
15
+ encode(payload) {
16
+ return (0, bytewords_js_1.encode)(payload, this._style);
17
+ }
18
+ decode(payload) {
19
+ return (0, bytewords_js_1.decode)(payload, this._style);
20
+ }
21
+ }
22
+ exports.BytewordEncoding = BytewordEncoding;
23
+ //# sourceMappingURL=BytewordEncoding.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BytewordEncoding.js","sourceRoot":"","sources":["../../../src/encodingMethods/BytewordEncoding.ts"],"names":[],"mappings":";;;AAAA,iDAAwD;AACxD,0EAAoE;AAGpE,MAAa,gBAAgB;IACnB,KAAK,GAAuB,0CAAkB,CAAC,SAAS,CAAC;IAExD,MAAM,CAAS;IAExB,IAAI,IAAI;QACJ,OAAO,IAAI,CAAC,KAAK,CAAC;IACtB,CAAC;IAED,YAAY,QAAgB,qBAAM,CAAC,OAAO;QACxC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IACD,MAAM,CAAC,OAAe;QACpB,OAAO,IAAA,qBAAM,EAAC,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IACtC,CAAC;IACD,MAAM,CAAC,OAAe;QACpB,OAAO,IAAA,qBAAM,EAAC,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IACtC,CAAC;CACF;AAlBD,4CAkBC"}
@@ -0,0 +1,44 @@
1
+ import { URRegistryClass } from "../registry.js";
2
+ import { RegistryItem, RegistryItemClass } from "../classes/RegistryItem.js";
3
+ import { EncodingMethodName } from "../enums/EncodingMethodName.js";
4
+ import { IEncodingMethod } from "../interfaces/IEncodingMethod.js";
5
+ import { DecodeOptions, EncodeOptions } from "../wrappers/cbor2.js";
6
+ interface inputOptions {
7
+ registry?: URRegistryClass;
8
+ cborLibEncoderOptions?: EncodeOptions & {
9
+ ignoreTopLevelTag?: boolean;
10
+ };
11
+ cborLibDecoderOptions?: DecodeOptions;
12
+ }
13
+ interface decodeOptions {
14
+ enforceType?: RegistryItemClass | string;
15
+ cborLibOptions?: DecodeOptions;
16
+ }
17
+ export declare class CborEncoding<T extends RegistryItem> implements IEncodingMethod<T, Uint8Array> {
18
+ private _name;
19
+ registry: URRegistryClass;
20
+ /** Decoding options for CBOR2 library */
21
+ cborLibEncoderOptions: EncodeOptions;
22
+ cborLibDecoderOptions: DecodeOptions;
23
+ constructor(options?: inputOptions);
24
+ get name(): EncodingMethodName;
25
+ /**
26
+ * Encode the given payload to CBOR
27
+ *
28
+ * @param payload @type RegistryItem
29
+ * @param cborLibOptions @type EncodeOptions
30
+ * @returns @type Uint8Array
31
+ */
32
+ encode(payload: any, cborLibOptions?: EncodeOptions & {
33
+ ignoreTopLevelTag?: boolean;
34
+ }): Uint8Array;
35
+ /**
36
+ * Decode the CBOR encoded payload to the given type
37
+ * @param payload @type Uint8Array
38
+ * @param enforceType Forces decoding into given type or throws error if it cannot be decoded @type RegistryItemClass
39
+ * @param cborLibOptions @type DecodeOptions
40
+ * @returns @type T
41
+ */
42
+ decode(payload: Uint8Array, options?: decodeOptions): T;
43
+ }
44
+ export {};
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CborEncoding = void 0;
4
+ const registry_js_1 = require("../registry.js");
5
+ const RegistryItem_js_1 = require("../classes/RegistryItem.js");
6
+ const EncodingMethodName_js_1 = require("../enums/EncodingMethodName.js");
7
+ const cbor2_js_1 = require("../wrappers/cbor2.js");
8
+ class CborEncoding {
9
+ _name = EncodingMethodName_js_1.EncodingMethodName.cbor;
10
+ registry = registry_js_1.UrRegistry;
11
+ /** Decoding options for CBOR2 library */
12
+ cborLibEncoderOptions;
13
+ cborLibDecoderOptions;
14
+ constructor(options) {
15
+ this.cborLibEncoderOptions = options?.cborLibEncoderOptions;
16
+ this.cborLibDecoderOptions = options?.cborLibDecoderOptions;
17
+ }
18
+ get name() {
19
+ return this._name;
20
+ }
21
+ /**
22
+ * Encode the given payload to CBOR
23
+ *
24
+ * @param payload @type RegistryItem
25
+ * @param cborLibOptions @type EncodeOptions
26
+ * @returns @type Uint8Array
27
+ */
28
+ encode(payload, cborLibOptions) {
29
+ // Combine instance cborLibOptions with the given cborLibOptions
30
+ const combinedOptions = {
31
+ ...this.cborLibEncoderOptions,
32
+ ...cborLibOptions,
33
+ };
34
+ // By default encode return Uint8Array
35
+ const encoded = (0, cbor2_js_1.encode)(payload, combinedOptions);
36
+ return encoded;
37
+ }
38
+ /**
39
+ * Decode the CBOR encoded payload to the given type
40
+ * @param payload @type Uint8Array
41
+ * @param enforceType Forces decoding into given type or throws error if it cannot be decoded @type RegistryItemClass
42
+ * @param cborLibOptions @type DecodeOptions
43
+ * @returns @type T
44
+ */
45
+ decode(payload, options) {
46
+ // Combine instance cborLibOptions with the given cborLibOptions
47
+ const { cborLibOptions, enforceType } = options || {};
48
+ const combinedOptions = {
49
+ ...this.cborLibDecoderOptions,
50
+ ...cborLibOptions,
51
+ };
52
+ const decoded = (0, cbor2_js_1.decode)(payload, combinedOptions);
53
+ // Check if enforce type is given, if so then give the value to the enforced type
54
+ if (enforceType) {
55
+ let foundType;
56
+ // If enforceType is string then find the type from the registry
57
+ if (typeof enforceType === "string") {
58
+ foundType = this.registry.queryByURType(enforceType);
59
+ }
60
+ else {
61
+ foundType = enforceType;
62
+ }
63
+ // If we have a registry item then force the type
64
+ if (foundType) {
65
+ return forceType(decoded, foundType);
66
+ }
67
+ }
68
+ // If we still have a cbor tag but not registry item then return try to convert it to registry item
69
+ if (decoded instanceof cbor2_js_1.Tag) {
70
+ return tag2registryItem(decoded);
71
+ }
72
+ // Otherwise just return the decoded value
73
+ // TODO: fix as unknown as T;
74
+ return decoded;
75
+ }
76
+ }
77
+ exports.CborEncoding = CborEncoding;
78
+ /**
79
+ * Force the data to be of the enforced type registry item
80
+ * @param data
81
+ * @param enforceType
82
+ * @returns
83
+ */
84
+ function forceType(data, enforceType) {
85
+ // If its already the enforced type then return
86
+ if (data instanceof enforceType) {
87
+ return data;
88
+ }
89
+ // If data is CBOR tag item, try to convert it to enforced type
90
+ if (data instanceof cbor2_js_1.Tag) {
91
+ if (data.tag !== enforceType.tag) {
92
+ throw new Error(`Enforced type does not match the tag of ${enforceType.URType}:${enforceType.tag} !== ${data.tag}`);
93
+ }
94
+ return enforceType.fromCBORData(data.contents, enforceType.allowKeysNotInMap);
95
+ }
96
+ // If data is raw then try to convert it to enforced type
97
+ return enforceType.fromCBORData(data, enforceType.allowKeysNotInMap);
98
+ }
99
+ /**
100
+ * Converts a CBOR tag to a registry item
101
+ * @param unkownTag
102
+ * @returns
103
+ */
104
+ function tag2registryItem(unkownTag) {
105
+ const tag = toRegistryTagNumber(unkownTag.tag);
106
+ const data = unkownTag.contents;
107
+ const registryItem = registry_js_1.UrRegistry.queryByTag(tag);
108
+ if (registryItem) {
109
+ return registryItem.fromCBORData(data, registryItem.allowKeysNotInMap);
110
+ }
111
+ // If we dont have the tag in the registry return unknown item
112
+ const UnknownTag = class extends (0, RegistryItem_js_1.registryItemFactory)({
113
+ tag: tag,
114
+ URType: "unknown-tag",
115
+ CDDL: ``,
116
+ }) {
117
+ };
118
+ const unknownItem = new UnknownTag(data);
119
+ // Search through all the children in unknownItem.data if any one of them are tags
120
+ deepSearchObject(unknownItem.data);
121
+ return unknownItem;
122
+ }
123
+ function toRegistryTagNumber(tag) {
124
+ if (typeof tag === "bigint") {
125
+ const converted = Number(tag);
126
+ if (!Number.isSafeInteger(converted)) {
127
+ throw new Error(`CBOR tag ${tag.toString()} exceeds JavaScript safe integer range`);
128
+ }
129
+ return converted;
130
+ }
131
+ return Number(tag);
132
+ }
133
+ /**
134
+ * Search through the object and convert tags to registry items
135
+ * @param obj
136
+ * @returns
137
+ */
138
+ function deepSearchObject(obj) {
139
+ if (typeof obj !== "object") {
140
+ return obj;
141
+ }
142
+ for (const key in obj) {
143
+ if (obj[key] instanceof cbor2_js_1.Tag) {
144
+ obj[key] = tag2registryItem(obj[key]);
145
+ }
146
+ else if (typeof obj[key] === "object") {
147
+ deepSearchObject(obj[key]);
148
+ }
149
+ }
150
+ }
151
+ //# sourceMappingURL=CborEncoding.js.map
@@ -0,0 +1 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,CAAC,GAAG,CAAC,CAAC,CAAC;QACxC,CAAC;aAAM,IAAI,OAAO,GAAG,CAAC,GAAG,CAAC,KAAK,QAAQ,EAAE,CAAC;YACxC,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { EncodingMethodName } from "../enums/EncodingMethodName.js";
2
+ import { IEncodingMethod } from "../interfaces/IEncodingMethod.js";
3
+ export declare class HexEncoding implements IEncodingMethod<Uint8Array, string> {
4
+ private _name;
5
+ get name(): EncodingMethodName;
6
+ encode(payload: Uint8Array): string;
7
+ decode(payload: string): Uint8Array;
8
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HexEncoding = void 0;
4
+ const uintArrayHelper_js_1 = require("../helpers/uintArrayHelper.js");
5
+ const EncodingMethodName_js_1 = require("../enums/EncodingMethodName.js");
6
+ class HexEncoding {
7
+ _name = EncodingMethodName_js_1.EncodingMethodName.hex;
8
+ get name() {
9
+ return this._name;
10
+ }
11
+ encode(payload) {
12
+ // return payload.toString("hex");
13
+ return (0, uintArrayHelper_js_1.uint8ArrayToHex)(payload);
14
+ }
15
+ decode(payload) {
16
+ const hexRegex = /^[0-9a-fA-F]+$/;
17
+ if (!hexRegex.test(payload)) {
18
+ throw new Error("Invalid hex string");
19
+ }
20
+ return (0, uintArrayHelper_js_1.hexToUint8Array)(payload);
21
+ }
22
+ }
23
+ exports.HexEncoding = HexEncoding;
24
+ //# sourceMappingURL=HexEncoding.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HexEncoding.js","sourceRoot":"","sources":["../../../src/encodingMethods/HexEncoding.ts"],"names":[],"mappings":";;;AAAA,sEAAiF;AACjF,0EAAoE;AAGpE,MAAa,WAAW;IACd,KAAK,GAAuB,0CAAkB,CAAC,GAAG,CAAC;IAE3D,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,MAAM,CAAC,OAAmB;QACxB,kCAAkC;QAClC,OAAO,IAAA,oCAAe,EAAC,OAAO,CAAC,CAAC;IAClC,CAAC;IACD,MAAM,CAAC,OAAe;QACpB,MAAM,QAAQ,GAAG,gBAAgB,CAAC;QAClC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;QACxC,CAAC;QACD,OAAO,IAAA,oCAAe,EAAC,OAAO,CAAC,CAAC;IAClC,CAAC;CACF;AAlBD,kCAkBC"}
@@ -0,0 +1,10 @@
1
+ import { EncodingMethodName } from "../enums/EncodingMethodName.js";
2
+ import { IEncodingMethod } from "../interfaces/IEncodingMethod.js";
3
+ import { UR } from "../classes/UR.js";
4
+ import { RegistryItem } from "../classes/RegistryItem.js";
5
+ export declare class UrEncoding implements IEncodingMethod<RegistryItem, UR> {
6
+ private _name;
7
+ get name(): EncodingMethodName;
8
+ encode(payload: RegistryItem): UR;
9
+ decode(payload: UR): RegistryItem;
10
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UrEncoding = void 0;
4
+ const EncodingMethodName_js_1 = require("../enums/EncodingMethodName.js");
5
+ const UR_js_1 = require("../classes/UR.js");
6
+ class UrEncoding {
7
+ _name = EncodingMethodName_js_1.EncodingMethodName.ur;
8
+ get name() {
9
+ return this._name;
10
+ }
11
+ encode(payload) {
12
+ return UR_js_1.UR.fromRegistryItem(payload);
13
+ }
14
+ decode(payload) {
15
+ return payload.toRegistryItem();
16
+ }
17
+ }
18
+ exports.UrEncoding = UrEncoding;
19
+ //# sourceMappingURL=UrEncoding.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UrEncoding.js","sourceRoot":"","sources":["../../../src/encodingMethods/UrEncoding.ts"],"names":[],"mappings":";;;AAAA,0EAAoE;AAEpE,4CAAsC;AAGtC,MAAa,UAAU;IACb,KAAK,GAAuB,0CAAkB,CAAC,EAAE,CAAC;IAE1D,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,MAAM,CAAC,OAAqB;QAC1B,OAAO,UAAE,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;IACtC,CAAC;IACD,MAAM,CAAC,OAAW;QAChB,OAAO,OAAO,CAAC,cAAc,EAAE,CAAC;IAClC,CAAC;CACF;AAbD,gCAaC"}
@@ -0,0 +1,27 @@
1
+ export declare enum STYLES {
2
+ STANDARD = "standard",
3
+ URI = "uri",
4
+ MINIMAL = "minimal"
5
+ }
6
+ /**
7
+ * Decode a string of bytewords into a hex string.
8
+ * @param string string of Bytewords
9
+ * @param style style of Bytewords
10
+ * @param includeChecksum whether to include checksum in the decoding.
11
+ * @returns hex string
12
+ */
13
+ export declare const decode: (string: string, style?: STYLES, includeChecksum?: boolean) => string;
14
+ /**
15
+ * Encodes a string (hex representation of a buffer) into bytewords.
16
+ * @param string string to encode.
17
+ * @param style style to use for the encoding.
18
+ * @param includeChecksum whether to include checksum in the encoding.
19
+ * @returns the byteword encoded string
20
+ */
21
+ export declare const encode: (string: string, style?: STYLES, includeChecksum?: boolean) => string;
22
+ declare const _default: {
23
+ decode: (string: string, style?: STYLES, includeChecksum?: boolean) => string;
24
+ encode: (string: string, style?: STYLES, includeChecksum?: boolean) => string;
25
+ STYLES: typeof STYLES;
26
+ };
27
+ export default _default;
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.encode = exports.decode = exports.STYLES = void 0;
4
+ const utils_js_1 = require("../helpers/utils.js");
5
+ const uintArrayHelper_js_1 = require("../helpers/uintArrayHelper.js");
6
+ const bytewords = "ableacidalsoapexaquaarchatomauntawayaxisbackbaldbarnbeltbetabiasbluebodybragbrewbulbbuzzcalmcashcatschefcityclawcodecolacookcostcruxcurlcuspcyandarkdatadaysdelidicedietdoordowndrawdropdrumdulldutyeacheasyechoedgeepicevenexamexiteyesfactfairfernfigsfilmfishfizzflapflewfluxfoxyfreefrogfuelfundgalagamegeargemsgiftgirlglowgoodgraygrimgurugushgyrohalfhanghardhawkheathelphighhillholyhopehornhutsicedideaidleinchinkyintoirisironitemjadejazzjoinjoltjowljudojugsjumpjunkjurykeepkenokeptkeyskickkilnkingkitekiwiknoblamblavalazyleaflegsliarlimplionlistlogoloudloveluaulucklungmainmanymathmazememomenumeowmildmintmissmonknailnavyneednewsnextnoonnotenumbobeyoboeomitonyxopenovalowlspaidpartpeckplaypluspoempoolposepuffpumapurrquadquizraceramprealredorichroadrockroofrubyruinrunsrustsafesagascarsetssilkskewslotsoapsolosongstubsurfswantacotasktaxitenttiedtimetinytoiltombtoystriptunatwinuglyundouniturgeuservastveryvetovialvibeviewvisavoidvowswallwandwarmwaspwavewaxywebswhatwhenwhizwolfworkyankyawnyellyogayurtzapszerozestzinczonezoom";
7
+ let bytewordsLookUpTable = [];
8
+ const BYTEWORDS_NUM = 256;
9
+ const BYTEWORD_LENGTH = 4;
10
+ const MINIMAL_BYTEWORD_LENGTH = 2;
11
+ var STYLES;
12
+ (function (STYLES) {
13
+ STYLES["STANDARD"] = "standard";
14
+ STYLES["URI"] = "uri";
15
+ STYLES["MINIMAL"] = "minimal";
16
+ })(STYLES || (exports.STYLES = STYLES = {}));
17
+ const getWord = (index) => {
18
+ return bytewords.slice(index * BYTEWORD_LENGTH, index * BYTEWORD_LENGTH + BYTEWORD_LENGTH);
19
+ };
20
+ const getMinimalWord = (index) => {
21
+ const byteword = getWord(index);
22
+ return `${byteword[0]}${byteword[BYTEWORD_LENGTH - 1]}`;
23
+ };
24
+ const addCRC = (string) => {
25
+ const crc = (0, utils_js_1.getCRCHex)((0, uintArrayHelper_js_1.hexToUint8Array)(string));
26
+ return `${string}${crc}`;
27
+ };
28
+ const encodeWithSeparator = (word, separator, includeChecksum) => {
29
+ const crcAppendedWord = includeChecksum ? addCRC(word) : word;
30
+ const crcWordArray = (0, uintArrayHelper_js_1.hexToUint8Array)(crcAppendedWord);
31
+ const result = crcWordArray.reduce((result, w) => [...result, getWord(w)], []);
32
+ return result.join(separator);
33
+ };
34
+ const encodeMinimal = (word, includeChecksum) => {
35
+ const crcAppendedWord = includeChecksum ? addCRC(word) : word;
36
+ const crcWordArray = (0, uintArrayHelper_js_1.hexToUint8Array)(crcAppendedWord);
37
+ const result = crcWordArray.reduce((result, w) => result + getMinimalWord(w), "");
38
+ return result;
39
+ };
40
+ const decodeWord = (word, wordLength) => {
41
+ (0, utils_js_1.assert)(word.length === wordLength, "Invalid Bytewords: word.length does not match wordLength provided");
42
+ const dim = 26;
43
+ // Since the first and last letters of each Byteword are unique,
44
+ // we can use them as indexes into a two-dimensional lookup table.
45
+ // This table is generated lazily.
46
+ if (bytewordsLookUpTable.length === 0) {
47
+ const array_len = dim * dim;
48
+ bytewordsLookUpTable = [...new Array(array_len)].map(() => -1);
49
+ for (let i = 0; i < BYTEWORDS_NUM; i++) {
50
+ const byteword = getWord(i);
51
+ let x = byteword[0].charCodeAt(0) - "a".charCodeAt(0);
52
+ let y = byteword[3].charCodeAt(0) - "a".charCodeAt(0);
53
+ let offset = y * dim + x;
54
+ bytewordsLookUpTable[offset] = i;
55
+ }
56
+ }
57
+ // If the coordinates generated by the first and last letters are out of bounds,
58
+ // or the lookup table contains -1 at the coordinates, then the word is not valid.
59
+ let x = word[0].toLowerCase().charCodeAt(0) - "a".charCodeAt(0);
60
+ let y = word[wordLength == 4 ? 3 : 1].toLowerCase().charCodeAt(0) -
61
+ "a".charCodeAt(0);
62
+ (0, utils_js_1.assert)(0 <= x && x < dim && 0 <= y && y < dim, "Invalid Bytewords: invalid word");
63
+ let offset = y * dim + x;
64
+ let value = bytewordsLookUpTable[offset];
65
+ (0, utils_js_1.assert)(value !== -1, "Invalid Bytewords: value not in lookup table");
66
+ // If we're decoding a full four-letter word, verify that the two middle letters are correct.
67
+ if (wordLength == BYTEWORD_LENGTH) {
68
+ const byteword = getWord(value);
69
+ let c1 = word[1].toLowerCase();
70
+ let c2 = word[2].toLowerCase();
71
+ (0, utils_js_1.assert)(c1 === byteword[1] && c2 === byteword[2], "Invalid Bytewords: invalid middle letters of word");
72
+ }
73
+ // Successful decode.
74
+ return value.toString(16).padStart(2, "0");
75
+ };
76
+ /**
77
+ * Decode a string of Bytewords into a hex string.
78
+ * @param string string of Bytewords. e.g. "lpamchcfatttcyclehgsdphdhgehfghkkkdl..."
79
+ * @param separator e.g. " " or "-" or "" or any other separator
80
+ * @param wordLength e.g. 4 or 2 or any other length
81
+ * @param includeChecksum whether to include checksum in the decoding.
82
+ * @returns hex string
83
+ */
84
+ const _decode = (string, separator, wordLength, includeChecksum) => {
85
+ // Split the string into words. e.g. ["lp", "am", "ch", "cf", "at", "tt", "cy", "cl", "eh", "gs", "dp", "hd", "hg", "eh", "fg", "hk", "kk", "dl", ...]
86
+ const words = wordLength == BYTEWORD_LENGTH
87
+ ? string.split(separator)
88
+ : (0, utils_js_1.partition)(string, 2);
89
+ // Decode each word. e.g. ["85", "06", "17", "19", "07", "d1", "1a", "21", "31", "4c", "2d", "58", "57", "31", "46", "59", "79", "2f", ...]
90
+ const decodedWords = words.map((word) => decodeWord(word, wordLength));
91
+ // e.g. "8506171907d11a21314c2d5857314659792f..."
92
+ const decodedString = decodedWords.join("");
93
+ if (includeChecksum) {
94
+ // 4 bytes for checksum, at least 1 byte for body
95
+ (0, utils_js_1.assert)(decodedString.length >= 5, "Invalid Bytewords: invalid decoded string length");
96
+ // decoded string consists of `body` and `checksum`
97
+ const decodedArray = (0, uintArrayHelper_js_1.hexToUint8Array)(decodedString);
98
+ const [body, bodyChecksum] = (0, utils_js_1.split)(decodedArray, 4);
99
+ const checksum = (0, utils_js_1.getCRCHex)(body); // convert to hex
100
+ (0, utils_js_1.assert)(checksum === (0, uintArrayHelper_js_1.uint8ArrayToHex)(bodyChecksum), "Invalid Checksum");
101
+ return (0, uintArrayHelper_js_1.uint8ArrayToHex)(body);
102
+ }
103
+ else {
104
+ return decodedString;
105
+ }
106
+ };
107
+ /**
108
+ * Decode a string of bytewords into a hex string.
109
+ * @param string string of Bytewords
110
+ * @param style style of Bytewords
111
+ * @param includeChecksum whether to include checksum in the decoding.
112
+ * @returns hex string
113
+ */
114
+ const decode = (string, style = STYLES.MINIMAL, includeChecksum = true) => {
115
+ switch (style) {
116
+ case STYLES.STANDARD:
117
+ return _decode(string, " ", BYTEWORD_LENGTH, includeChecksum);
118
+ case STYLES.URI:
119
+ return _decode(string, "-", BYTEWORD_LENGTH, includeChecksum);
120
+ case STYLES.MINIMAL:
121
+ return _decode(string, "", MINIMAL_BYTEWORD_LENGTH, includeChecksum);
122
+ default:
123
+ throw new Error(`Invalid style ${style}`);
124
+ }
125
+ };
126
+ exports.decode = decode;
127
+ /**
128
+ * Encodes a string (hex representation of a buffer) into bytewords.
129
+ * @param string string to encode.
130
+ * @param style style to use for the encoding.
131
+ * @param includeChecksum whether to include checksum in the encoding.
132
+ * @returns the byteword encoded string
133
+ */
134
+ const encode = (string, style = STYLES.MINIMAL, includeChecksum = true) => {
135
+ switch (style) {
136
+ case STYLES.STANDARD:
137
+ return encodeWithSeparator(string, " ", includeChecksum);
138
+ case STYLES.URI:
139
+ return encodeWithSeparator(string, "-", includeChecksum);
140
+ case STYLES.MINIMAL:
141
+ return encodeMinimal(string, includeChecksum);
142
+ default:
143
+ throw new Error(`Invalid style ${style}`);
144
+ }
145
+ };
146
+ exports.encode = encode;
147
+ exports.default = {
148
+ decode: exports.decode,
149
+ encode: exports.encode,
150
+ STYLES,
151
+ };
152
+ //# sourceMappingURL=bytewords.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bytewords.js","sourceRoot":"","sources":["../../../src/encodingMethods/bytewords.ts"],"names":[],"mappings":";;;AAAA,kDAA0E;AAC1E,sEAAiF;AAEjF,MAAM,SAAS,GACb,kgCAAkgC,CAAC;AACrgC,IAAI,oB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E,MAAM,IAAI,KAAK,CAAC,iBAAiB,KAAK,EAAE,CAAC,CAAC;IAC9C,CAAC;AACH,CAAC,CAAC;AAfW,QAAA,MAAM,UAejB;AAEF,kBAAe;IACb,MAAM,EAAN,cAAM;IACN,MAAM,EAAN,cAAM;IACN,MAAM;CACP,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { BytewordEncoding } from "./BytewordEncoding.js";
2
+ import { CborEncoding } from "./CborEncoding.js";
3
+ import { HexEncoding } from "./HexEncoding.js";
4
+ import { EncodingPipeline } from "./pipeline.js";
5
+ /**
6
+ * Pipeline that encodes registry registryItem | any -> cbor -> hex -> bytewords
7
+ *
8
+ * Uses default encoding options for each encoding method
9
+ * 1. Cbor: CBOR2 library
10
+ * 2. Hex: Uint8Array -> hex string
11
+ * 3. Bytewords: Uint8Array -> bytewords string with checksum and minimal style
12
+ *
13
+ */
14
+ export declare const defaultEncoders: {
15
+ cbor: CborEncoding<import("../index.js").RegistryItemBase>;
16
+ hex: HexEncoding;
17
+ bytewords: BytewordEncoding;
18
+ };
19
+ export declare const dataPipeline: EncodingPipeline<any, string>;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dataPipeline = exports.defaultEncoders = void 0;
4
+ const BytewordEncoding_js_1 = require("./BytewordEncoding.js");
5
+ const CborEncoding_js_1 = require("./CborEncoding.js");
6
+ const HexEncoding_js_1 = require("./HexEncoding.js");
7
+ const pipeline_js_1 = require("./pipeline.js");
8
+ /**
9
+ * Pipeline that encodes registry registryItem | any -> cbor -> hex -> bytewords
10
+ *
11
+ * Uses default encoding options for each encoding method
12
+ * 1. Cbor: CBOR2 library
13
+ * 2. Hex: Uint8Array -> hex string
14
+ * 3. Bytewords: Uint8Array -> bytewords string with checksum and minimal style
15
+ *
16
+ */
17
+ exports.defaultEncoders = {
18
+ cbor: new CborEncoding_js_1.CborEncoding(),
19
+ hex: new HexEncoding_js_1.HexEncoding(),
20
+ bytewords: new BytewordEncoding_js_1.BytewordEncoding(),
21
+ };
22
+ exports.dataPipeline = new pipeline_js_1.EncodingPipeline([
23
+ exports.defaultEncoders.cbor,
24
+ exports.defaultEncoders.hex,
25
+ exports.defaultEncoders.bytewords,
26
+ ]);
27
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/encodingMethods/index.ts"],"names":[],"mappings":";;;AAAA,+DAAyD;AACzD,uDAAiD;AACjD,qDAA+C;AAC/C,+CAAiD;AAEjD;;;;;;;;GAQG;AAEU,QAAA,eAAe,GAAG;IAC7B,IAAI,EAAE,IAAI,8BAAY,EAAE;IACxB,GAAG,EAAE,IAAI,4BAAW,EAAE;IACtB,SAAS,EAAE,IAAI,sCAAgB,EAAE;CAClC,CAAA;AAEY,QAAA,YAAY,GAAG,IAAI,8BAAgB,CAAc;IAC5D,uBAAe,CAAC,IAAI;IACpB,uBAAe,CAAC,GAAG;IACnB,uBAAe,CAAC,SAAS;CAC1B,CAAC,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { EncodingMethodName } from "../enums/EncodingMethodName.js";
2
+ import { IEncodingMethod } from "../interfaces/IEncodingMethod.js";
3
+ interface pipelineConfig {
4
+ from?: EncodingMethodName;
5
+ until?: EncodingMethodName;
6
+ [key: string]: any;
7
+ }
8
+ export declare class EncodingPipeline<T, U> implements IEncodingMethod<T, U> {
9
+ private _name;
10
+ private _encodingMethods;
11
+ constructor(encodingMethods: IEncodingMethod<any, any>[]);
12
+ get name(): EncodingMethodName;
13
+ get encodingMethods(): IEncodingMethod<any, any>[];
14
+ get decodingMethods(): IEncodingMethod<any, any>[];
15
+ encode<O = U>(payload: T, config?: pipelineConfig): O;
16
+ decode<O = T>(payload: U, config?: pipelineConfig): O;
17
+ getMethod(methodName: EncodingMethodName): IEncodingMethod<any, any>;
18
+ }
19
+ export {};
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EncodingPipeline = void 0;
4
+ const EncodingMethodName_js_1 = require("../enums/EncodingMethodName.js");
5
+ class EncodingPipeline {
6
+ _name = EncodingMethodName_js_1.EncodingMethodName.pipe;
7
+ _encodingMethods;
8
+ constructor(encodingMethods) {
9
+ this._encodingMethods = encodingMethods;
10
+ }
11
+ get name() {
12
+ return this._name;
13
+ }
14
+ get encodingMethods() {
15
+ return this._encodingMethods;
16
+ }
17
+ get decodingMethods() {
18
+ return this._encodingMethods.slice().reverse();
19
+ }
20
+ // TODO: change until to "to"
21
+ encode(payload, config = { from: undefined, until: undefined }) {
22
+ let encodedValue = payload;
23
+ let fromIndex = 0;
24
+ let untilIndex = this.encodingMethods.length;
25
+ const { from, until, ...rest } = config;
26
+ // Find the index of the specified start and until encoding methods
27
+ if (from) {
28
+ fromIndex = this.encodingMethods.findIndex((method) => method.name === from);
29
+ if (fromIndex === -1) {
30
+ throw new Error(`Encoding method ${from} not found`);
31
+ }
32
+ }
33
+ if (until) {
34
+ untilIndex = this.encodingMethods.findIndex((method) => method.name === until);
35
+ if (untilIndex === -1) {
36
+ throw new Error(`Encoding method ${until} not found`);
37
+ }
38
+ }
39
+ if (fromIndex > untilIndex) {
40
+ throw new Error("Invalid encoding method range");
41
+ }
42
+ // Apply each encoding method in sequence until the specified encoding method or the end
43
+ for (let i = fromIndex; i < untilIndex; i++) {
44
+ encodedValue = this.encodingMethods[i].encode(encodedValue, rest);
45
+ }
46
+ return encodedValue;
47
+ }
48
+ decode(payload, config = { from: undefined, until: undefined }) {
49
+ let decodedValue = payload;
50
+ let fromIndex = 0;
51
+ let untilIndex = this.decodingMethods.length;
52
+ const { from, until, ...rest } = config;
53
+ // Find the index of the specified start and until encoding methods
54
+ if (from) {
55
+ fromIndex = this.decodingMethods.findIndex((method) => method.name === from);
56
+ if (fromIndex === -1) {
57
+ throw new Error(`Encoding method ${from} not found`);
58
+ }
59
+ }
60
+ if (until) {
61
+ untilIndex = this.decodingMethods.findIndex((method) => method.name === until);
62
+ if (untilIndex === -1) {
63
+ throw new Error(`Encoding method ${until} not found`);
64
+ }
65
+ }
66
+ if (fromIndex > untilIndex) {
67
+ throw new Error("Invalid encoding method range");
68
+ }
69
+ // Apply each encoding method in sequence until the specified encoding method or the end
70
+ for (let i = fromIndex; i < untilIndex; i++) {
71
+ decodedValue = this.decodingMethods[i].decode(decodedValue, rest);
72
+ }
73
+ return decodedValue;
74
+ }
75
+ getMethod(methodName) {
76
+ return this.encodingMethods.find((method) => method.name === methodName);
77
+ }
78
+ }
79
+ exports.EncodingPipeline = EncodingPipeline;
80
+ //# sourceMappingURL=pipeline.js.map