@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,611 @@
1
+ // Taken from uint8array-extras "^1.4.0" package
2
+ // https://github.com/sindresorhus/uint8array-extras
3
+
4
+ export type TypedArray =
5
+ | Int8Array
6
+ | Uint8Array
7
+ | Uint8ClampedArray
8
+ | Int16Array
9
+ | Uint16Array
10
+ | Int32Array
11
+ | Uint32Array
12
+ | Float32Array
13
+ | Float64Array
14
+ | BigInt64Array
15
+ | BigUint64Array;
16
+
17
+ const objectToString = Object.prototype.toString;
18
+ const uint8ArrayStringified = '[object Uint8Array]';
19
+ const arrayBufferStringified = '[object ArrayBuffer]';
20
+
21
+ function isType(value: unknown, typeConstructor: Function, typeStringified: string): boolean {
22
+ if (!value) {
23
+ return false;
24
+ }
25
+
26
+ if (value.constructor === typeConstructor) {
27
+ return true;
28
+ }
29
+
30
+ return objectToString.call(value) === typeStringified;
31
+ }
32
+
33
+ /**
34
+ Check if the given value is an instance of `Uint8Array`.
35
+
36
+ Replacement for [`Buffer.isBuffer()`](https://nodejs.org/api/buffer.html#static-method-bufferisbufferobj).
37
+
38
+ @example
39
+ ```
40
+ import {isUint8Array} from 'uint8array-extras';
41
+
42
+ console.log(isUint8Array(new Uint8Array()));
43
+ //=> true
44
+
45
+ console.log(isUint8Array(Buffer.from('x')));
46
+ //=> true
47
+
48
+ console.log(isUint8Array(new ArrayBuffer(10)));
49
+ //=> false
50
+ ```
51
+ */
52
+ export function isUint8Array(value: unknown): value is Uint8Array {
53
+ return isType(value, Uint8Array, uint8ArrayStringified);
54
+ }
55
+
56
+ function isArrayBuffer(value: unknown): value is ArrayBuffer {
57
+ return isType(value, ArrayBuffer, arrayBufferStringified);
58
+ }
59
+
60
+ function isUint8ArrayOrArrayBuffer(value: unknown): boolean {
61
+ return isUint8Array(value) || isArrayBuffer(value);
62
+ }
63
+
64
+ /**
65
+ Throw a `TypeError` if the given value is not an instance of `Uint8Array`.
66
+
67
+ @example
68
+ ```
69
+ import {assertUint8Array} from 'uint8array-extras';
70
+
71
+ try {
72
+ assertUint8Array(new ArrayBuffer(10)); // Throws a TypeError
73
+ } catch (error) {
74
+ console.error(error.message);
75
+ }
76
+ ```
77
+ */
78
+ export function assertUint8Array(value: unknown): asserts value is Uint8Array {
79
+ if (!isUint8Array(value)) {
80
+ throw new TypeError(`Expected \`Uint8Array\`, got \`${typeof value}\``);
81
+ }
82
+ }
83
+
84
+ function assertUint8ArrayOrArrayBuffer(value: unknown): asserts value is Uint8Array | ArrayBuffer {
85
+ if (!isUint8ArrayOrArrayBuffer(value)) {
86
+ throw new TypeError(`Expected \`Uint8Array\` or \`ArrayBuffer\`, got \`${typeof value}\``);
87
+ }
88
+ }
89
+
90
+ /**
91
+ Convert a value to a `Uint8Array` without copying its data.
92
+
93
+ This can be useful for converting a `Buffer` to a pure `Uint8Array`. `Buffer` is already an `Uint8Array` subclass, but [`Buffer` alters some behavior](https://sindresorhus.com/blog/goodbye-nodejs-buffer), so it can be useful to cast it to a pure `Uint8Array` before returning it.
94
+
95
+ Tip: If you want a copy, just call `.slice()` on the return value.
96
+ */
97
+ export function toUint8Array(value: TypedArray | ArrayBuffer | DataView): Uint8Array {
98
+ if (value instanceof ArrayBuffer) {
99
+ return new Uint8Array(value);
100
+ }
101
+
102
+ if (ArrayBuffer.isView(value)) {
103
+ return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
104
+ }
105
+
106
+ throw new TypeError(`Unsupported value, got \`${typeof value}\`.`);
107
+ }
108
+
109
+ /**
110
+ Concatenate the given arrays into a new array.
111
+
112
+ If `arrays` is empty, it will return a zero-sized `Uint8Array`.
113
+
114
+ If `totalLength` is not specified, it is calculated from summing the lengths of the given arrays.
115
+
116
+ Replacement for [`Buffer.concat()`](https://nodejs.org/api/buffer.html#static-method-bufferconcatlist-totallength).
117
+
118
+ @example
119
+ ```
120
+ import {concatUint8Arrays} from 'uint8array-extras';
121
+
122
+ const a = new Uint8Array([1, 2, 3]);
123
+ const b = new Uint8Array([4, 5, 6]);
124
+
125
+ console.log(concatUint8Arrays([a, b]));
126
+ //=> Uint8Array [1, 2, 3, 4, 5, 6]
127
+ ```
128
+ */
129
+ export function concatUint8Arrays(arrays: Uint8Array[], totalLength?: number): Uint8Array {
130
+ if (arrays.length === 0) {
131
+ return new Uint8Array(0);
132
+ }
133
+
134
+ totalLength ??= arrays.reduce((accumulator, currentValue) => accumulator + currentValue.length, 0);
135
+
136
+ const returnValue = new Uint8Array(totalLength);
137
+
138
+ let offset = 0;
139
+ for (const array of arrays) {
140
+ assertUint8Array(array);
141
+ returnValue.set(array, offset);
142
+ offset += array.length;
143
+ }
144
+
145
+ return returnValue;
146
+ }
147
+
148
+ /**
149
+ Check if two arrays are identical by verifying that they contain the same bytes in the same sequence.
150
+
151
+ Replacement for [`Buffer#equals()`](https://nodejs.org/api/buffer.html#bufequalsotherbuffer).
152
+
153
+ @example
154
+ ```
155
+ import {areUint8ArraysEqual} from 'uint8array-extras';
156
+
157
+ const a = new Uint8Array([1, 2, 3]);
158
+ const b = new Uint8Array([1, 2, 3]);
159
+ const c = new Uint8Array([4, 5, 6]);
160
+
161
+ console.log(areUint8ArraysEqual(a, b));
162
+ //=> true
163
+
164
+ console.log(areUint8ArraysEqual(a, c));
165
+ //=> false
166
+ ```
167
+ */
168
+ export function areUint8ArraysEqual(a: Uint8Array, b: Uint8Array): boolean {
169
+ assertUint8Array(a);
170
+ assertUint8Array(b);
171
+
172
+ if (a === b) {
173
+ return true;
174
+ }
175
+
176
+ if (a.length !== b.length) {
177
+ return false;
178
+ }
179
+
180
+ // eslint-disable-next-line unicorn/no-for-loop
181
+ for (let index = 0; index < a.length; index++) {
182
+ if (a[index] !== b[index]) {
183
+ return false;
184
+ }
185
+ }
186
+
187
+ return true;
188
+ }
189
+
190
+ /**
191
+ Compare two arrays and indicate their relative order or equality. Useful for sorting.
192
+
193
+ Replacement for [`Buffer.compare()`](https://nodejs.org/api/buffer.html#static-method-buffercomparebuf1-buf2).
194
+
195
+ @example
196
+ ```
197
+ import {compareUint8Arrays} from 'uint8array-extras';
198
+
199
+ const array1 = new Uint8Array([1, 2, 3]);
200
+ const array2 = new Uint8Array([4, 5, 6]);
201
+ const array3 = new Uint8Array([7, 8, 9]);
202
+
203
+ [array3, array1, array2].sort(compareUint8Arrays);
204
+ //=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
205
+ ```
206
+ */
207
+ export function compareUint8Arrays(a: Uint8Array, b: Uint8Array): -1 | 0 | 1 {
208
+ assertUint8Array(a);
209
+ assertUint8Array(b);
210
+
211
+ const length = Math.min(a.length, b.length);
212
+
213
+ for (let index = 0; index < length; index++) {
214
+ const diff = a[index] - b[index];
215
+ if (diff !== 0) {
216
+ return Math.sign(diff) as -1 | 0 | 1;
217
+ }
218
+ }
219
+
220
+ // At this point, all the compared elements are equal.
221
+ // The shorter array should come first if the arrays are of different lengths.
222
+ return Math.sign(a.length - b.length) as -1 | 0 | 1;
223
+ }
224
+
225
+ function miniTextDecoder() {
226
+ return {
227
+ decode(input: Uint8Array | ArrayBuffer): string {
228
+ const uint8Array = input instanceof Uint8Array ? input : new Uint8Array(input);
229
+ let result = '';
230
+ for (let i = 0; i < uint8Array.length; i++) {
231
+ result += String.fromCharCode(uint8Array[i]);
232
+ }
233
+ return result;
234
+ }
235
+ };
236
+ }
237
+
238
+ const cachedDecoders: { [encoding: string]: TextDecoder | { decode: (input: Uint8Array | ArrayBuffer) => string } } = {
239
+ // Use TextDecoder if available (web, Node.js), otherwise use miniTextDecoder for React Native
240
+ utf8: typeof globalThis.TextDecoder !== 'undefined' ? new globalThis.TextDecoder('utf8') : miniTextDecoder(),
241
+ };
242
+
243
+ /**
244
+ Convert a `Uint8Array` to a string.
245
+
246
+ @param encoding - The [encoding](https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings) to convert from. Default: `'utf8'`
247
+
248
+ Replacement for [`Buffer#toString()`](https://nodejs.org/api/buffer.html#buftostringencoding-start-end). For the `encoding` parameter, `latin1` should be used instead of `binary` and `utf-16le` instead of `utf16le`.
249
+
250
+ @example
251
+ ```
252
+ import {uint8ArrayToString} from 'uint8array-extras';
253
+
254
+ const byteArray = new Uint8Array([72, 101, 108, 108, 111]);
255
+ console.log(uint8ArrayToString(byteArray));
256
+ //=> 'Hello'
257
+
258
+ const zh = new Uint8Array([167, 65, 166, 110]);
259
+ console.log(uint8ArrayToString(zh, 'big5'));
260
+ //=> '你好'
261
+
262
+ const ja = new Uint8Array([130, 177, 130, 241, 130, 201, 130, 191, 130, 205]);
263
+ console.log(uint8ArrayToString(ja, 'shift-jis'));
264
+ //=> 'こんにちは'
265
+ ```
266
+ */
267
+ export function uint8ArrayToString(array: Uint8Array | ArrayBuffer, encoding: string = 'utf8'): string {
268
+ assertUint8ArrayOrArrayBuffer(array);
269
+ return cachedDecoders[encoding].decode(array);
270
+ }
271
+
272
+ function assertString(value: unknown): asserts value is string {
273
+ if (typeof value !== 'string') {
274
+ throw new TypeError(`Expected \`string\`, got \`${typeof value}\``);
275
+ }
276
+ }
277
+
278
+ const cachedEncoder: TextEncoder = new globalThis.TextEncoder();
279
+
280
+ /**
281
+ Convert a string to a `Uint8Array` (using UTF-8 encoding).
282
+
283
+ Replacement for [`Buffer.from('Hello')`](https://nodejs.org/api/buffer.html#static-method-bufferfromstring-encoding).
284
+
285
+ @example
286
+ ```
287
+ import {stringToUint8Array} from 'uint8array-extras';
288
+
289
+ console.log(stringToUint8Array('Hello'));
290
+ //=> Uint8Array [72, 101, 108, 108, 111]
291
+ ```
292
+ */
293
+ export function stringToUint8Array(string: string): Uint8Array {
294
+ assertString(string);
295
+ return cachedEncoder.encode(string);
296
+ }
297
+
298
+ function base64ToBase64Url(base64: string): string {
299
+ return base64.replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/, '');
300
+ }
301
+
302
+ function base64UrlToBase64(base64url: string): string {
303
+ return base64url.replaceAll('-', '+').replaceAll('_', '/');
304
+ }
305
+
306
+ // Reference: https://phuoc.ng/collection/this-vs-that/concat-vs-push/
307
+ const MAX_BLOCK_SIZE = 65_535;
308
+
309
+ /**
310
+ Convert a `Uint8Array` to a Base64-encoded string.
311
+
312
+ Specify `{urlSafe: true}` to get a [Base64URL](https://base64.guru/standards/base64url)-encoded string.
313
+
314
+ Replacement for [`Buffer#toString('base64')`](https://nodejs.org/api/buffer.html#buftostringencoding-start-end).
315
+
316
+ @example
317
+ ```
318
+ import {uint8ArrayToBase64} from 'uint8array-extras';
319
+
320
+ const byteArray = new Uint8Array([72, 101, 108, 108, 111]);
321
+
322
+ console.log(uint8ArrayToBase64(byteArray));
323
+ //=> 'SGVsbG8='
324
+ ```
325
+ */
326
+ export function uint8ArrayToBase64(array: Uint8Array, {urlSafe = false}: {urlSafe?: boolean} = {}): string {
327
+ assertUint8Array(array);
328
+
329
+ let base64: string;
330
+
331
+ if (array.length < MAX_BLOCK_SIZE) {
332
+ // Required as `btoa` and `atob` don't properly support Unicode: https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
333
+ base64 = globalThis.btoa(String.fromCodePoint.apply(this, array as unknown as number[]));
334
+ } else {
335
+ base64 = '';
336
+ for (const value of array) {
337
+ base64 += String.fromCodePoint(value);
338
+ }
339
+
340
+ base64 = globalThis.btoa(base64);
341
+ }
342
+
343
+ return urlSafe ? base64ToBase64Url(base64) : base64;
344
+ }
345
+
346
+ /**
347
+ Convert a Base64-encoded or [Base64URL](https://base64.guru/standards/base64url)-encoded string to a `Uint8Array`.
348
+
349
+ Replacement for [`Buffer.from('SGVsbG8=', 'base64')`](https://nodejs.org/api/buffer.html#static-method-bufferfromstring-encoding).
350
+
351
+ @example
352
+ ```
353
+ import {base64ToUint8Array} from 'uint8array-extras';
354
+
355
+ console.log(base64ToUint8Array('SGVsbG8='));
356
+ //=> Uint8Array [72, 101, 108, 108, 111]
357
+ ```
358
+ */
359
+ export function base64ToUint8Array(base64String: string): Uint8Array {
360
+ assertString(base64String);
361
+ return Uint8Array.from(globalThis.atob(base64UrlToBase64(base64String)), x => x.codePointAt(0)!);
362
+ }
363
+
364
+ /**
365
+ Encode a string to Base64-encoded string.
366
+
367
+ Specify `{urlSafe: true}` to get a [Base64URL](https://base64.guru/standards/base64url)-encoded string.
368
+
369
+ Replacement for `Buffer.from('Hello').toString('base64')` and [`btoa()`](https://developer.mozilla.org/en-US/docs/Web/API/btoa).
370
+
371
+ @example
372
+ ```
373
+ import {stringToBase64} from 'uint8array-extras';
374
+
375
+ console.log(stringToBase64('Hello'));
376
+ //=> 'SGVsbG8='
377
+ ```
378
+ */
379
+ export function stringToBase64(string: string, {urlSafe = false}: {urlSafe?: boolean} = {}): string {
380
+ assertString(string);
381
+ return uint8ArrayToBase64(stringToUint8Array(string), {urlSafe});
382
+ }
383
+
384
+ /**
385
+ Decode a Base64-encoded or [Base64URL](https://base64.guru/standards/base64url)-encoded string to a string.
386
+
387
+ Replacement for `Buffer.from('SGVsbG8=', 'base64').toString()` and [`atob()`](https://developer.mozilla.org/en-US/docs/Web/API/atob).
388
+
389
+ @example
390
+ ```
391
+ import {base64ToString} from 'uint8array-extras';
392
+
393
+ console.log(base64ToString('SGVsbG8='));
394
+ //=> 'Hello'
395
+ ```
396
+ */
397
+ export function base64ToString(base64String: string): string {
398
+ assertString(base64String);
399
+ return uint8ArrayToString(base64ToUint8Array(base64String));
400
+ }
401
+
402
+ const byteToHexLookupTable: string[] = Array.from({length: 256}, (_, index) => index.toString(16).padStart(2, '0'));
403
+
404
+ /**
405
+ Convert a `Uint8Array` to a Hex string.
406
+
407
+ Replacement for [`Buffer#toString('hex')`](https://nodejs.org/api/buffer.html#buftostringencoding-start-end).
408
+
409
+ @example
410
+ ```
411
+ import {uint8ArrayToHex} from 'uint8array-extras';
412
+
413
+ const byteArray = new Uint8Array([72, 101, 108, 108, 111]);
414
+
415
+ console.log(uint8ArrayToHex(byteArray));
416
+ //=> '48656c6c6f'
417
+ ```
418
+ */
419
+ export function uint8ArrayToHex(array: Uint8Array): string {
420
+ assertUint8Array(array);
421
+
422
+ // Concatenating a string is faster than using an array.
423
+ let hexString = '';
424
+
425
+ // eslint-disable-next-line unicorn/no-for-loop -- Max performance is critical.
426
+ for (let index = 0; index < array.length; index++) {
427
+ hexString += byteToHexLookupTable[array[index]];
428
+ }
429
+
430
+ return hexString;
431
+ }
432
+
433
+ const hexToDecimalLookupTable: { [key: string]: number } = {
434
+ 0: 0,
435
+ 1: 1,
436
+ 2: 2,
437
+ 3: 3,
438
+ 4: 4,
439
+ 5: 5,
440
+ 6: 6,
441
+ 7: 7,
442
+ 8: 8,
443
+ 9: 9,
444
+ a: 10,
445
+ b: 11,
446
+ c: 12,
447
+ d: 13,
448
+ e: 14,
449
+ f: 15,
450
+ A: 10,
451
+ B: 11,
452
+ C: 12,
453
+ D: 13,
454
+ E: 14,
455
+ F: 15,
456
+ };
457
+
458
+ /**
459
+ Convert a Hex string to a `Uint8Array`.
460
+
461
+ Replacement for [`Buffer.from('48656c6c6f', 'hex')`](https://nodejs.org/api/buffer.html#static-method-bufferfromstring-encoding).
462
+
463
+ @example
464
+ ```
465
+ import {hexToUint8Array} from 'uint8array-extras';
466
+
467
+ console.log(hexToUint8Array('48656c6c6f'));
468
+ //=> Uint8Array [72, 101, 108, 108, 111]
469
+ ```
470
+ */
471
+ export function hexToUint8Array(hexString: string): Uint8Array {
472
+ assertString(hexString);
473
+
474
+ if (hexString.length % 2 !== 0) {
475
+ throw new Error('Invalid Hex string length.');
476
+ }
477
+
478
+ const resultLength = hexString.length / 2;
479
+ const bytes = new Uint8Array(resultLength);
480
+
481
+ for (let index = 0; index < resultLength; index++) {
482
+ const highNibble = hexToDecimalLookupTable[hexString[index * 2]];
483
+ const lowNibble = hexToDecimalLookupTable[hexString[(index * 2) + 1]];
484
+
485
+ if (highNibble === undefined || lowNibble === undefined) {
486
+ throw new Error(`Invalid Hex character encountered at position ${index * 2}`);
487
+ }
488
+
489
+ bytes[index] = (highNibble << 4) | lowNibble; // eslint-disable-line no-bitwise
490
+ }
491
+
492
+ return bytes;
493
+ }
494
+
495
+ /**
496
+ Read `DataView#byteLength` number of bytes from the given view, up to 48-bit.
497
+
498
+ Replacement for [`Buffer#readUintBE`](https://nodejs.org/api/buffer.html#bufreadintbeoffset-bytelength)
499
+
500
+ @example
501
+ ```
502
+ import {getUintBE} from 'uint8array-extras';
503
+
504
+ const byteArray = new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
505
+
506
+ console.log(getUintBE(new DataView(byteArray.buffer)));
507
+ //=> 20015998341291
508
+ ```
509
+ */
510
+ export function getUintBE(view: DataView): number {
511
+ const {byteLength} = view;
512
+
513
+ if (byteLength === 6) {
514
+ return (view.getUint16(0) * (2 ** 32)) + view.getUint32(2);
515
+ }
516
+
517
+ if (byteLength === 5) {
518
+ return (view.getUint8(0) * (2 ** 32)) + view.getUint32(1);
519
+ }
520
+
521
+ if (byteLength === 4) {
522
+ return view.getUint32(0);
523
+ }
524
+
525
+ if (byteLength === 3) {
526
+ return (view.getUint8(0) * (2 ** 16)) + view.getUint16(1);
527
+ }
528
+
529
+ if (byteLength === 2) {
530
+ return view.getUint16(0);
531
+ }
532
+
533
+ if (byteLength === 1) {
534
+ return view.getUint8(0);
535
+ }
536
+ // At this point, none of the conditions matched.
537
+ throw new Error('Unsupported DataView byteLength');
538
+ }
539
+
540
+ /**
541
+ Find the index of the first occurrence of the given sequence of bytes (`value`) within the given `Uint8Array` (`array`).
542
+
543
+ Replacement for [`Buffer#indexOf`](https://nodejs.org/api/buffer.html#bufindexofvalue-byteoffset-encoding). `Uint8Array#indexOf` only takes a number which is different from Buffer's `indexOf` implementation.
544
+
545
+ @example
546
+ ```
547
+ import {indexOf} from 'uint8array-extras';
548
+
549
+ const byteArray = new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef]);
550
+
551
+ console.log(indexOf(byteArray, new Uint8Array([0x78, 0x90])));
552
+ //=> 3
553
+ ```
554
+ */
555
+ export function indexOf(array: Uint8Array, value: Uint8Array): number {
556
+ const arrayLength = array.length;
557
+ const valueLength = value.length;
558
+
559
+ if (valueLength === 0) {
560
+ return -1;
561
+ }
562
+
563
+ if (valueLength > arrayLength) {
564
+ return -1;
565
+ }
566
+
567
+ const validOffsetLength = arrayLength - valueLength;
568
+
569
+ for (let index = 0; index <= validOffsetLength; index++) {
570
+ let isMatch = true;
571
+ for (let index2 = 0; index2 < valueLength; index2++) {
572
+ if (array[index + index2] !== value[index2]) {
573
+ isMatch = false;
574
+ break;
575
+ }
576
+ }
577
+
578
+ if (isMatch) {
579
+ return index;
580
+ }
581
+ }
582
+
583
+ return -1;
584
+ }
585
+
586
+ /**
587
+ Checks if the given sequence of bytes (`value`) is within the given `Uint8Array` (`array`).
588
+
589
+ Returns true if the value is included, otherwise false.
590
+
591
+ Replacement for [`Buffer#includes`](https://nodejs.org/api/buffer.html#bufincludesvalue-byteoffset-encoding). `Uint8Array#includes` only takes a number which is different from Buffer's `includes` implementation.
592
+
593
+ import {includes} from 'uint8array-extras';
594
+
595
+ const byteArray = new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef]);
596
+
597
+ console.log(includes(byteArray, new Uint8Array([0x78, 0x90])));
598
+ //=> true
599
+ ```
600
+ */
601
+ export function includes(array: Uint8Array, value: Uint8Array): boolean {
602
+ return indexOf(array, value) !== -1;
603
+ }
604
+
605
+
606
+ // concatUint8Arrays
607
+ // stringToUint8Array
608
+ // toUint8Array
609
+ // hexToUint8Array
610
+ // uint8ArrayToHex
611
+