@exodus/bytes 1.14.0 → 1.15.0

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.
package/README.md CHANGED
@@ -5,6 +5,7 @@
5
5
  [![](https://img.shields.io/npm/dm/@exodus/bytes?style=flat-square)](https://www.npmcharts.com/compare/@exodus/bytes?minimal=true)
6
6
  [![](https://img.shields.io/npm/l/@exodus/bytes?style=flat-square&color=blue)](https://github.com/ExodusOSS/bytes/blob/HEAD/LICENSE)
7
7
  [![](https://img.shields.io/github/check-runs/ExodusOSS/bytes/main?style=flat-square&logo=github)](https://github.com/ExodusOSS/bytes/actions/workflows/test.yml?query=branch%3Amain)
8
+ [![](https://img.shields.io/badge/Documentation-3178c6?style=flat-square&logo=TypeScript&logoColor=fff)](https://exodusoss.github.io/bytes/)
8
9
 
9
10
  `Uint8Array` conversion to and from `base64`, `base32`, `base58`, `hex`, `utf8`, `utf16`, `bech32` and `wif`
10
11
 
@@ -63,7 +64,7 @@ Less than half the bundle size of [text-encoding](https://npmjs.com/text-encodin
63
64
  Also [much faster](#fast) than all of those.
64
65
 
65
66
  > [!TIP]
66
- > See also the [lite version](#lite-version) to get this down to 10 KiB gzipped.
67
+ > See also the [lite version](#lite-version) to get this down to 8 KiB gzipped.
67
68
 
68
69
  Spec compliant, passing WPT and covered with extra tests.\
69
70
  Moreover, tests for this library uncovered [bugs in all major implementations](https://docs.google.com/spreadsheets/d/1pdEefRG6r9fZy61WHGz0TKSt8cO4ISWqlpBN5KntIvQ/edit).\
@@ -140,7 +141,7 @@ For non-browsers (Node.js, React Native), loads a full implementation.
140
141
 
141
142
  ## API
142
143
 
143
- ### @exodus/bytes/utf8.js <sub>![](https://img.shields.io/bundlejs/size/@exodus/bytes/utf8.js?style=flat-square)<sub>
144
+ ### @exodus/bytes/utf8.js <sub>![](https://img.shields.io/bundlejs/size/@exodus/bytes/utf8.js?style=flat-square)</sub>
144
145
 
145
146
  UTF-8 encoding/decoding
146
147
 
@@ -206,7 +207,7 @@ Prefer using strict throwing methods for cryptography applications._
206
207
  This is similar to `new TextDecoder('utf-8', { ignoreBOM: true }).decode(arr)`,
207
208
  but works on all engines.
208
209
 
209
- ### @exodus/bytes/utf16.js <sub>![](https://img.shields.io/bundlejs/size/@exodus/bytes/utf16.js?style=flat-square)<sub>
210
+ ### @exodus/bytes/utf16.js <sub>![](https://img.shields.io/bundlejs/size/@exodus/bytes/utf16.js?style=flat-square)</sub>
210
211
 
211
212
  UTF-16 encoding/decoding
212
213
 
@@ -256,169 +257,6 @@ Prefer using strict throwing methods for cryptography applications._
256
257
 
257
258
  Throws on non-even byte length.
258
259
 
259
- ### @exodus/bytes/single-byte.js <sub>![](https://img.shields.io/bundlejs/size/@exodus/bytes/single-byte.js?style=flat-square)</sub>
260
-
261
- Decode / encode the legacy single-byte encodings according to the
262
- [Encoding standard](https://encoding.spec.whatwg.org/)
263
- ([§9](https://encoding.spec.whatwg.org/#legacy-single-byte-encodings),
264
- [§14.5](https://encoding.spec.whatwg.org/#x-user-defined)),
265
- and [unicode.org](https://unicode.org/Public/MAPPINGS/ISO8859) `iso-8859-*` mappings.
266
-
267
- ```js
268
- import { createSinglebyteDecoder, createSinglebyteEncoder } from '@exodus/bytes/single-byte.js'
269
- import { windows1252toString, windows1252fromString } from '@exodus/bytes/single-byte.js'
270
- import { latin1toString, latin1fromString } from '@exodus/bytes/single-byte.js'
271
- ```
272
-
273
- > [!WARNING]
274
- > This is a lower-level API for single-byte encodings.
275
- > It might not match what you expect, as it supports both WHATWG and unicode.org encodings under
276
- > different names, with the main intended usecase for the latter being either non-web or legacy contexts.
277
- >
278
- > For a safe WHATWG Encoding-compatible API, see `@exodus/bytes/encoding.js` import (and variants of it).
279
- >
280
- > Be sure to know what you are doing and check documentation when directly using encodings from this file.
281
-
282
- Supports all single-byte encodings listed in the WHATWG Encoding standard:
283
- `ibm866`, `iso-8859-2`, `iso-8859-3`, `iso-8859-4`, `iso-8859-5`, `iso-8859-6`, `iso-8859-7`, `iso-8859-8`,
284
- `iso-8859-8-i`, `iso-8859-10`, `iso-8859-13`, `iso-8859-14`, `iso-8859-15`, `iso-8859-16`, `koi8-r`, `koi8-u`,
285
- `macintosh`, `windows-874`, `windows-1250`, `windows-1251`, `windows-1252`, `windows-1253`, `windows-1254`,
286
- `windows-1255`, `windows-1256`, `windows-1257`, `windows-1258`, `x-mac-cyrillic` and `x-user-defined`.
287
-
288
- Also supports `iso-8859-1`, `iso-8859-9`, `iso-8859-11` as defined at
289
- [unicode.org](https://unicode.org/Public/MAPPINGS/ISO8859)
290
- (and all other `iso-8859-*` encodings there as they match WHATWG).
291
-
292
- > [!NOTE]
293
- > While all `iso-8859-*` encodings supported by the [WHATWG Encoding standard](https://encoding.spec.whatwg.org/) match
294
- > [unicode.org](https://unicode.org/Public/MAPPINGS/ISO8859), the WHATWG Encoding spec doesn't support
295
- > `iso-8859-1`, `iso-8859-9`, `iso-8859-11`, and instead maps them as labels to `windows-1252`, `windows-1254`, `windows-874`.\
296
- > `createSinglebyteDecoder()` (unlike `TextDecoder` or `legacyHookDecode()`) does not do such mapping,
297
- > so its results will differ from `TextDecoder` for those encoding names.
298
-
299
- ```js
300
- > new TextDecoder('iso-8859-1').encoding
301
- 'windows-1252'
302
- > new TextDecoder('iso-8859-9').encoding
303
- 'windows-1254'
304
- > new TextDecoder('iso-8859-11').encoding
305
- 'windows-874'
306
- > new TextDecoder('iso-8859-9').decode(Uint8Array.of(0x80, 0x81, 0xd0))
307
- '€\x81Ğ' // this is actually decoded according to windows-1254 per TextDecoder spec
308
- > createSinglebyteDecoder('iso-8859-9')(Uint8Array.of(0x80, 0x81, 0xd0))
309
- '\x80\x81Ğ' // this is iso-8859-9 as defined at https://unicode.org/Public/MAPPINGS/ISO8859/8859-9.txt
310
- ```
311
-
312
- All WHATWG Encoding spec [`windows-*` encodings](https://encoding.spec.whatwg.org/#windows-874) are supersets of
313
- corresponding [unicode.org encodings](https://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/), meaning that
314
- they encode/decode all the old valid (non-replacement) strings / byte sequences identically, but can also support
315
- a wider range of inputs.
316
-
317
- #### `createSinglebyteDecoder(encoding, loose = false)`
318
-
319
- Create a decoder for a supported one-byte `encoding`, given its lowercased name `encoding`.
320
-
321
- Returns a function `decode(arr)` that decodes bytes to a string.
322
-
323
- #### `createSinglebyteEncoder(encoding, { mode = 'fatal' })`
324
-
325
- Create an encoder for a supported one-byte `encoding`, given its lowercased name `encoding`.
326
-
327
- Returns a function `encode(string)` that encodes a string to bytes.
328
-
329
- In `'fatal'` mode (default), will throw on non well-formed strings or any codepoints which could
330
- not be encoded in the target encoding.
331
-
332
- #### `latin1toString(arr)`
333
-
334
- Decode `iso-8859-1` bytes to a string.
335
-
336
- There is no loose variant for this encoding, all bytes can be decoded.
337
-
338
- Same as:
339
- ```js
340
- const latin1toString = createSinglebyteDecoder('iso-8859-1')
341
- ```
342
-
343
- > [!NOTE]
344
- > This is different from `new TextDecoder('iso-8859-1')` and `new TextDecoder('latin1')`, as those
345
- > alias to `new TextDecoder('windows-1252')`.
346
-
347
- #### `latin1fromString(string)`
348
-
349
- Encode a string to `iso-8859-1` bytes.
350
-
351
- Throws on non well-formed strings or any codepoints which could not be encoded in `iso-8859-1`.
352
-
353
- Same as:
354
- ```js
355
- const latin1fromString = createSinglebyteEncoder('iso-8859-1', { mode: 'fatal' })
356
- ```
357
-
358
- #### `windows1252toString(arr)`
359
-
360
- Decode `windows-1252` bytes to a string.
361
-
362
- There is no loose variant for this encoding, all bytes can be decoded.
363
-
364
- Same as:
365
- ```js
366
- const windows1252toString = createSinglebyteDecoder('windows-1252')
367
- ```
368
-
369
- #### `windows1252fromString(string)`
370
-
371
- Encode a string to `windows-1252` bytes.
372
-
373
- Throws on non well-formed strings or any codepoints which could not be encoded in `windows-1252`.
374
-
375
- Same as:
376
- ```js
377
- const windows1252fromString = createSinglebyteEncoder('windows-1252', { mode: 'fatal' })
378
- ```
379
-
380
- ### @exodus/bytes/multi-byte.js <sub>![](https://img.shields.io/bundlejs/size/@exodus/bytes/multi-byte.js?style=flat-square)</sub>
381
-
382
- Decode / encode the legacy multi-byte encodings according to the
383
- [Encoding standard](https://encoding.spec.whatwg.org/)
384
- ([§10](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(simplified)-encodings),
385
- [§11](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(traditional)-encodings),
386
- [§12](https://encoding.spec.whatwg.org/#legacy-multi-byte-japanese-encodings),
387
- [§13](https://encoding.spec.whatwg.org/#legacy-multi-byte-korean-encodings)).
388
-
389
- ```js
390
- import { createMultibyteDecoder, createMultibyteEncoder } from '@exodus/bytes/multi-byte.js'
391
- ```
392
-
393
- > [!WARNING]
394
- > This is a lower-level API for legacy multi-byte encodings.
395
- >
396
- > For a safe WHATWG Encoding-compatible API, see `@exodus/bytes/encoding.js` import (and variants of it).
397
- >
398
- > Be sure to know what you are doing and check documentation when directly using encodings from this file.
399
-
400
- Supports all legacy multi-byte encodings listed in the WHATWG Encoding standard:
401
- `gbk`, `gb18030`, `big5`, `euc-jp`, `iso-2022-jp`, `shift_jis`, `euc-kr`.
402
-
403
- #### `createMultibyteDecoder(encoding, loose = false)`
404
-
405
- Create a decoder for a supported legacy multi-byte `encoding`, given its lowercased name `encoding`.
406
-
407
- Returns a function `decode(arr, stream = false)` that decodes bytes to a string.
408
-
409
- The returned function will maintain internal state while `stream = true` is used, allowing it to
410
- handle incomplete multi-byte sequences across multiple calls.
411
- State is reset when `stream = false` or when the function is called without the `stream` parameter.
412
-
413
- #### `createMultibyteEncoder(encoding, { mode = 'fatal' })`
414
-
415
- Create an encoder for a supported legacy multi-byte `encoding`, given its lowercased name `encoding`.
416
-
417
- Returns a function `encode(string)` that encodes a string to bytes.
418
-
419
- In `'fatal'` mode (default), will throw on non well-formed strings or any codepoints which could
420
- not be encoded in the target encoding.
421
-
422
260
  ### @exodus/bytes/bigint.js <sub>![](https://img.shields.io/bundlejs/size/@exodus/bytes/bigint.js?style=flat-square)</sub>
423
261
 
424
262
  Convert between BigInt and Uint8Array
@@ -519,6 +357,14 @@ Decode a base32hex string to bytes
519
357
 
520
358
  Operates in strict mode for last chunk, does not allow whitespace
521
359
 
360
+ #### `fromBase32crockford(string, options)`
361
+
362
+ Decode a Crockford base32 string to bytes
363
+
364
+ Operates in strict mode for last chunk, does not allow whitespace
365
+
366
+ Crockford base32 decoding follows extra mapping per spec: `LIli -> 1, Oo -> 0`
367
+
522
368
  #### `toBase32(arr, { padding = false })`
523
369
 
524
370
  Encode a `Uint8Array` to a base32 string (RFC 4648)
@@ -527,6 +373,10 @@ Encode a `Uint8Array` to a base32 string (RFC 4648)
527
373
 
528
374
  Encode a `Uint8Array` to a base32hex string (RFC 4648)
529
375
 
376
+ #### `toBase32crockford(arr, options)`
377
+
378
+ Encode a `Uint8Array` to a Crockford base32 string
379
+
530
380
  ### @exodus/bytes/bech32.js <sub>![](https://img.shields.io/bundlejs/size/@exodus/bytes/bech32.js?style=flat-square)</sub>
531
381
 
532
382
  Implements bech32 and bech32m from
@@ -680,9 +530,24 @@ Encode WIF data to a WIF string (synchronous)
680
530
  TypedArray utils and conversions.
681
531
 
682
532
  ```js
683
- import { typedView } from '@exodus/bytes/array.js'
533
+ import { typedCopyBytes, typedView } from '@exodus/bytes/array.js'
684
534
  ```
685
535
 
536
+ #### `typedCopyBytes(arr, format = 'uint8')`
537
+
538
+ Create a copy of TypedArray underlying bytes in the specified format (`'uint8'`, `'buffer'`, or `'arraybuffer'`)
539
+
540
+ This does not copy _values_, but copies the underlying bytes.
541
+ The result is similar to that of `typedView()`, but this function provides a copy, not a view of the same memory.
542
+
543
+ > [!WARNING]
544
+ > Copying underlying bytes from `Uint16Array` (or other with `BYTES_PER_ELEMENT > 1`)
545
+ > is platform endianness-dependent.
546
+
547
+ > [!NOTE]
548
+ > Buffer might be pooled.
549
+ > Uint8Array return values are not pooled and match their underlying ArrayBuffer.
550
+
686
551
  #### `typedView(arr, format = 'uint8')`
687
552
 
688
553
  Create a view of a TypedArray in the specified format (`'uint8'` or `'buffer'`)
@@ -690,6 +555,10 @@ Create a view of a TypedArray in the specified format (`'uint8'` or `'buffer'`)
690
555
  > [!IMPORTANT]
691
556
  > Does not copy data, returns a view on the same underlying buffer
692
557
 
558
+ > [!WARNING]
559
+ > Viewing `Uint16Array` (or other with `BYTES_PER_ELEMENT > 1`) as bytes
560
+ > is platform endianness-dependent.
561
+
693
562
  ### @exodus/bytes/encoding.js <sub>![](https://img.shields.io/bundlejs/size/@exodus/bytes/encoding.js?style=flat-square)</sub>
694
563
 
695
564
  Implements the [Encoding standard](https://encoding.spec.whatwg.org/):
@@ -702,6 +571,7 @@ some [hooks](https://encoding.spec.whatwg.org/#specification-hooks).
702
571
  ```js
703
572
  import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding.js'
704
573
  import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding.js' // Requires Streams
574
+ import { isomorphicDecode, isomorphicEncode } from '@exodus/bytes/encoding.js'
705
575
 
706
576
  // Hooks for standards
707
577
  import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding.js'
@@ -737,6 +607,26 @@ A [Streams](https://streams.spec.whatwg.org/) wrapper for `TextEncoder`.
737
607
  Requires [Streams](https://streams.spec.whatwg.org/) to be either supported by the platform or
738
608
  [polyfilled](https://npmjs.com/package/web-streams-polyfill).
739
609
 
610
+ #### `isomorphicDecode(input)`
611
+
612
+ Implements [isomorphic decode](https://infra.spec.whatwg.org/#isomorphic-decode).
613
+
614
+ Given a `TypedArray` or an `ArrayBuffer` instance `input`, creates a string of the same length
615
+ as input byteLength, using bytes from input as codepoints.
616
+
617
+ E.g. for `Uint8Array` input, this is similar to `String.fromCodePoint(...input)`.
618
+
619
+ Wider `TypedArray` inputs, e.g. `Uint16Array`, are interpreted as underlying _bytes_.
620
+
621
+ #### `isomorphicEncode(str)`
622
+
623
+ Implements [isomorphic encode](https://infra.spec.whatwg.org/#isomorphic-encode).
624
+
625
+ Given a string, creates an `Uint8Array` of the same length with the string codepoints as byte values.
626
+
627
+ Accepts only [isomorphic string](https://infra.spec.whatwg.org/#isomorphic-string) input
628
+ and asserts that, throwing on any strings containing codepoints higher than `U+00FF`.
629
+
740
630
  #### `labelToName(label)`
741
631
 
742
632
  Implements [get an encoding from a string `label`](https://encoding.spec.whatwg.org/#concept-encoding-get).
@@ -810,6 +700,7 @@ multi-byte `TextDecoder` encodings by default to reduce bundle size ~12x.
810
700
  ```js
811
701
  import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-lite.js'
812
702
  import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-lite.js' // Requires Streams
703
+ import { isomorphicDecode, isomorphicEncode } from '@exodus/bytes/encoding-lite.js'
813
704
 
814
705
  // Hooks for standards
815
706
  import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-lite.js'
@@ -860,11 +751,13 @@ true
860
751
  '%'
861
752
  ```
862
753
 
863
- ### @exodus/bytes/encoding-browser.js <sub>![](https://img.shields.io/bundlejs/size/@exodus/bytes/encoding-browser.js?style=flat-square)<sub>
754
+ ### @exodus/bytes/encoding-browser.js <sub>![](https://img.shields.io/bundlejs/size/@exodus/bytes/encoding-browser.js?style=flat-square)</sub>
864
755
 
865
756
  Same as `@exodus/bytes/encoding.js`, but in browsers instead of polyfilling just uses whatever the
866
757
  browser provides, drastically reducing the bundle size (to less than 2 KiB gzipped).
867
758
 
759
+ Does not provide `isomorphicDecode` and `isomorphicEncode` exports.
760
+
868
761
  ```js
869
762
  import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-browser.js'
870
763
  import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-browser.js' // Requires Streams
package/array.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * TypedArray utils and conversions.
3
3
  *
4
4
  * ```js
5
- * import { typedView } from '@exodus/bytes/array.js'
5
+ * import { typedCopyBytes, typedView } from '@exodus/bytes/array.js'
6
6
  * ```
7
7
  *
8
8
  * @module @exodus/bytes/array.js
@@ -45,7 +45,7 @@ export type Uint32ArrayBuffer = ReturnType<typeof Uint32Array.from>;
45
45
  /**
46
46
  * Output format for typed array conversions
47
47
  */
48
- export type OutputFormat = 'uint8' | 'buffer';
48
+ export type OutputFormat = 'uint8' | 'arraybuffer' | 'buffer';
49
49
 
50
50
  /**
51
51
  * Create a view of a TypedArray in the specified format (`'uint8'` or `'buffer'`)
@@ -53,10 +53,37 @@ export type OutputFormat = 'uint8' | 'buffer';
53
53
  * > [!IMPORTANT]
54
54
  * > Does not copy data, returns a view on the same underlying buffer
55
55
  *
56
+ * > [!WARNING]
57
+ * > Viewing `Uint16Array` (or other with `BYTES_PER_ELEMENT > 1`) as bytes
58
+ * > is platform endianness-dependent.
59
+ *
56
60
  * @param arr - The input TypedArray
57
61
  * @param format - The desired output format (`'uint8'` or `'buffer'`)
58
62
  * @returns A view on the same underlying buffer
59
63
  */
60
64
  export function typedView(arr: ArrayBufferView, format: 'uint8'): Uint8Array;
61
65
  export function typedView(arr: ArrayBufferView, format: 'buffer'): Buffer;
62
- export function typedView(arr: ArrayBufferView, format: OutputFormat): Uint8Array | Buffer;
66
+ export function typedView(arr: ArrayBufferView, format: 'uint8' | 'buffer'): Uint8Array | Buffer;
67
+
68
+ /**
69
+ * Create a copy of TypedArray underlying bytes in the specified format (`'uint8'`, `'buffer'`, or `'arraybuffer'`)
70
+ *
71
+ * This does not copy _values_, but copies the underlying bytes.
72
+ * The result is similar to that of `typedView()`, but this function provides a copy, not a view of the same memory.
73
+ *
74
+ * > [!WARNING]
75
+ * > Copying underlying bytes from `Uint16Array` (or other with `BYTES_PER_ELEMENT > 1`)
76
+ * > is platform endianness-dependent.
77
+ *
78
+ * > [!NOTE]
79
+ * > Buffer might be pooled.
80
+ * > Uint8Array return values are not pooled and match their underlying ArrayBuffer.
81
+ *
82
+ * @param arr - The input TypedArray
83
+ * @param format - The desired output format (`'uint8'`, `'buffer'`, or `'arraybuffer'`)
84
+ * @returns A copy of the underlying buffer
85
+ */
86
+ export function typedCopyBytes(arr: ArrayBufferView, format: 'uint8'): Uint8Array;
87
+ export function typedCopyBytes(arr: ArrayBufferView, format: 'arraybuffer'): ArrayBuffer;
88
+ export function typedCopyBytes(arr: ArrayBufferView, format: 'buffer'): Buffer;
89
+ export function typedCopyBytes(arr: ArrayBufferView, format: OutputFormat): Uint8Array | ArrayBuffer | Buffer;
package/array.js CHANGED
@@ -15,3 +15,21 @@ export function typedView(arr, format) {
15
15
 
16
16
  throw new TypeError('Unexpected format')
17
17
  }
18
+
19
+ export function typedCopyBytes(arr, format) {
20
+ assertTypedArray(arr)
21
+ if (!(arr instanceof Uint8Array)) {
22
+ arr = new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength)
23
+ }
24
+
25
+ switch (format) {
26
+ case 'uint8':
27
+ return Uint8Array.from(arr) // never pooled
28
+ case 'buffer':
29
+ return Buffer.from(arr)
30
+ case 'arraybuffer':
31
+ return Uint8Array.from(arr).buffer
32
+ }
33
+
34
+ throw new TypeError('Unexpected format')
35
+ }
package/base32.d.ts CHANGED
@@ -58,6 +58,15 @@ export function toBase32(arr: Uint8Array, options?: ToBase32Options): string;
58
58
  */
59
59
  export function toBase32hex(arr: Uint8Array, options?: ToBase32Options): string;
60
60
 
61
+ /**
62
+ * Encode a `Uint8Array` to a Crockford base32 string
63
+ *
64
+ * @param arr - The input bytes
65
+ * @param options - Encoding options (padding defaults to false)
66
+ * @returns The Crockford base32 encoded string
67
+ */
68
+ export function toBase32crockford(arr: Uint8Array, options?: ToBase32Options): string;
69
+
61
70
  /**
62
71
  * Decode a base32 string to bytes
63
72
  *
@@ -67,8 +76,10 @@ export function toBase32hex(arr: Uint8Array, options?: ToBase32Options): string;
67
76
  * @param options - Decoding options
68
77
  * @returns The decoded bytes
69
78
  */
70
- export function fromBase32(string: string, options?: FromBase32Options): Uint8ArrayBuffer;
79
+ export function fromBase32(string: string, options?: FromBase32Options & { format?: 'uint8' }): Uint8ArrayBuffer;
80
+ export function fromBase32(string: string, options: FromBase32Options & { format: 'arraybuffer' }): ArrayBuffer;
71
81
  export function fromBase32(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;
82
+ export function fromBase32(string: string, options?: FromBase32Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;
72
83
 
73
84
  /**
74
85
  * Decode a base32hex string to bytes
@@ -79,5 +90,23 @@ export function fromBase32(string: string, options: FromBase32Options & { format
79
90
  * @param options - Decoding options
80
91
  * @returns The decoded bytes
81
92
  */
82
- export function fromBase32hex(string: string, options?: FromBase32Options): Uint8ArrayBuffer;
93
+ export function fromBase32hex(string: string, options?: FromBase32Options & { format?: 'uint8' }): Uint8ArrayBuffer;
94
+ export function fromBase32hex(string: string, options: FromBase32Options & { format: 'arraybuffer' }): ArrayBuffer;
83
95
  export function fromBase32hex(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;
96
+ export function fromBase32hex(string: string, options?: FromBase32Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;
97
+
98
+ /**
99
+ * Decode a Crockford base32 string to bytes
100
+ *
101
+ * Operates in strict mode for last chunk, does not allow whitespace
102
+ *
103
+ * Crockford base32 decoding follows extra mapping per spec: `LIli -> 1, Oo -> 0`
104
+ *
105
+ * @param string - The Crockford base32 encoded string
106
+ * @param options - Decoding options
107
+ * @returns The decoded bytes
108
+ */
109
+ export function fromBase32crockford(string: string, options?: FromBase32Options & { format?: 'uint8' }): Uint8ArrayBuffer;
110
+ export function fromBase32crockford(string: string, options: FromBase32Options & { format: 'arraybuffer' }): ArrayBuffer;
111
+ export function fromBase32crockford(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;
112
+ export function fromBase32crockford(string: string, options?: FromBase32Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;
package/base32.js CHANGED
@@ -1,29 +1,12 @@
1
1
  import { assertEmptyRest } from './assert.js'
2
- import { typedView } from './array.js'
3
- import { E_STRING } from './fallback/_utils.js'
2
+ import { fromUint8, E_STRING } from './fallback/_utils.js'
4
3
  import * as js from './fallback/base32.js'
5
4
 
6
5
  // See https://datatracker.ietf.org/doc/html/rfc4648
7
6
 
8
7
  // 8 chars per 5 bytes
9
8
 
10
- export const toBase32 = (arr, { padding = false } = {}) => js.toBase32(arr, false, padding)
11
- export const toBase32hex = (arr, { padding = false } = {}) => js.toBase32(arr, true, padding)
12
-
13
- // By default, valid padding is accepted but not required
14
- export function fromBase32(str, options) {
15
- if (!options) return fromBase32common(str, false, 'both', 'uint8', null)
16
- const { format = 'uint8', padding = 'both', ...rest } = options
17
- return fromBase32common(str, false, padding, format, rest)
18
- }
19
-
20
- export function fromBase32hex(str, options) {
21
- if (!options) return fromBase32common(str, true, 'both', 'uint8', null)
22
- const { format = 'uint8', padding = 'both', ...rest } = options
23
- return fromBase32common(str, true, padding, format, rest)
24
- }
25
-
26
- function fromBase32common(str, isBase32Hex, padding, format, rest) {
9
+ function fromBase32common(str, mode, padding, format, rest) {
27
10
  if (typeof str !== 'string') throw new TypeError(E_STRING)
28
11
  if (rest !== null) assertEmptyRest(rest)
29
12
 
@@ -35,5 +18,20 @@ function fromBase32common(str, isBase32Hex, padding, format, rest) {
35
18
  throw new TypeError('Invalid padding option')
36
19
  }
37
20
 
38
- return typedView(js.fromBase32(str, isBase32Hex), format)
21
+ return fromUint8(js.fromBase32(str, mode), format)
39
22
  }
23
+
24
+ // By default, valid padding is accepted but not required
25
+ const fromBase32wrap = (mode) => (str, options) => {
26
+ if (!options) return fromBase32common(str, mode, 'both', 'uint8', null)
27
+ const { format = 'uint8', padding = 'both', ...rest } = options
28
+ return fromBase32common(str, mode, padding, format, rest)
29
+ }
30
+
31
+ export const fromBase32 = fromBase32wrap(0)
32
+ export const fromBase32hex = fromBase32wrap(1)
33
+ export const fromBase32crockford = fromBase32wrap(2)
34
+
35
+ export const toBase32 = (arr, { padding = false } = {}) => js.toBase32(arr, 0, padding)
36
+ export const toBase32hex = (arr, { padding = false } = {}) => js.toBase32(arr, 1, padding)
37
+ export const toBase32crockford = (arr, { padding = false } = {}) => js.toBase32(arr, 2, padding)
package/base58.d.ts CHANGED
@@ -35,8 +35,9 @@ export function toBase58(arr: Uint8Array): string;
35
35
  * @returns The decoded bytes
36
36
  */
37
37
  export function fromBase58(string: string, format?: 'uint8'): Uint8ArrayBuffer;
38
+ export function fromBase58(string: string, format: 'arraybuffer'): ArrayBuffer;
38
39
  export function fromBase58(string: string, format: 'buffer'): Buffer;
39
- export function fromBase58(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
40
+ export function fromBase58(string: string, format?: OutputFormat): Uint8ArrayBuffer | ArrayBuffer | Buffer;
40
41
 
41
42
  /**
42
43
  * Encode a `Uint8Array` to a base58 string using XRP alphabet
@@ -58,5 +59,6 @@ export function toBase58xrp(arr: Uint8Array): string;
58
59
  * @returns The decoded bytes
59
60
  */
60
61
  export function fromBase58xrp(string: string, format?: 'uint8'): Uint8ArrayBuffer;
62
+ export function fromBase58xrp(string: string, format: 'arraybuffer'): ArrayBuffer;
61
63
  export function fromBase58xrp(string: string, format: 'buffer'): Buffer;
62
- export function fromBase58xrp(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
64
+ export function fromBase58xrp(string: string, format?: OutputFormat): Uint8ArrayBuffer | ArrayBuffer | Buffer;
package/base58.js CHANGED
@@ -1,5 +1,4 @@
1
- import { typedView } from './array.js'
2
- import { assertU8, E_STRING } from './fallback/_utils.js'
1
+ import { assertU8, fromUint8, E_STRING } from './fallback/_utils.js'
3
2
  import { nativeDecoder, nativeEncoder, isHermes } from './fallback/platform.js'
4
3
  import { encodeAscii, decodeAscii } from './fallback/latin1.js'
5
4
 
@@ -124,7 +123,7 @@ function toBase58core(arr, alphabet, codes) {
124
123
  function fromBase58core(str, alphabet, codes, format = 'uint8') {
125
124
  if (typeof str !== 'string') throw new TypeError(E_STRING)
126
125
  const length = str.length
127
- if (length === 0) return typedView(new Uint8Array(), format)
126
+ if (length === 0) return fromUint8(new Uint8Array(), format)
128
127
 
129
128
  const zeroC = codes[0]
130
129
  let zeros = 0
@@ -211,7 +210,7 @@ function fromBase58core(str, alphabet, codes, format = 'uint8') {
211
210
  }
212
211
  }
213
212
 
214
- return typedView(res.slice(at - zeros), format) // slice is faster for small sizes than subarray
213
+ return fromUint8(res.slice(at - zeros), format)
215
214
  }
216
215
 
217
216
  export const toBase58 = (arr) => toBase58core(arr, alphabet58, codes58)
package/base58check.d.ts CHANGED
@@ -46,8 +46,9 @@ export interface Base58CheckAsync {
46
46
  * @returns A Promise that resolves to the decoded bytes
47
47
  */
48
48
  decode(string: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>;
49
+ decode(string: string, format: 'arraybuffer'): Promise<ArrayBuffer>;
49
50
  decode(string: string, format: 'buffer'): Promise<Buffer>;
50
- decode(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | Buffer>;
51
+ decode(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | ArrayBuffer | Buffer>;
51
52
  }
52
53
 
53
54
  /**
@@ -70,8 +71,9 @@ export interface Base58CheckSync extends Base58CheckAsync {
70
71
  * @returns The decoded bytes
71
72
  */
72
73
  decodeSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
74
+ decodeSync(string: string, format: 'arraybuffer'): ArrayBuffer;
73
75
  decodeSync(string: string, format: 'buffer'): Buffer;
74
- decodeSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
76
+ decodeSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | ArrayBuffer | Buffer;
75
77
  }
76
78
 
77
79
  /**
@@ -104,8 +106,9 @@ export function toBase58check(arr: Uint8Array): Promise<string>;
104
106
  * @returns A Promise that resolves to the decoded bytes
105
107
  */
106
108
  export function fromBase58check(string: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>;
109
+ export function fromBase58check(string: string, format: 'arraybuffer'): Promise<ArrayBuffer>;
107
110
  export function fromBase58check(string: string, format: 'buffer'): Promise<Buffer>;
108
- export function fromBase58check(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | Buffer>;
111
+ export function fromBase58check(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | ArrayBuffer | Buffer>;
109
112
 
110
113
  /**
111
114
  * Encode bytes to base58check string synchronously
@@ -127,5 +130,6 @@ export function toBase58checkSync(arr: Uint8Array): string;
127
130
  * @returns The decoded bytes
128
131
  */
129
132
  export function fromBase58checkSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
133
+ export function fromBase58checkSync(string: string, format: 'arraybuffer'): ArrayBuffer;
130
134
  export function fromBase58checkSync(string: string, format: 'buffer'): Buffer;
131
- export function fromBase58checkSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
135
+ export function fromBase58checkSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | ArrayBuffer | Buffer;
package/base64.d.ts CHANGED
@@ -68,8 +68,10 @@ export function toBase64url(arr: Uint8Array, options?: ToBase64Options): string;
68
68
  * @param options - Decoding options
69
69
  * @returns The decoded bytes
70
70
  */
71
- export function fromBase64(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
71
+ export function fromBase64(string: string, options?: FromBase64Options & { format?: 'uint8' }): Uint8ArrayBuffer;
72
+ export function fromBase64(string: string, options: FromBase64Options & { format: 'arraybuffer' }): ArrayBuffer;
72
73
  export function fromBase64(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
74
+ export function fromBase64(string: string, options?: FromBase64Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;
73
75
 
74
76
  /**
75
77
  * Decode a base64url string to bytes
@@ -80,8 +82,10 @@ export function fromBase64(string: string, options: FromBase64Options & { format
80
82
  * @param options - Decoding options (padding defaults to false)
81
83
  * @returns The decoded bytes
82
84
  */
83
- export function fromBase64url(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
85
+ export function fromBase64url(string: string, options?: FromBase64Options & { format?: 'uint8' }): Uint8ArrayBuffer;
86
+ export function fromBase64url(string: string, options: FromBase64Options & { format: 'arraybuffer' }): ArrayBuffer;
84
87
  export function fromBase64url(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
88
+ export function fromBase64url(string: string, options?: FromBase64Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;
85
89
 
86
90
  /**
87
91
  * Decode either base64 or base64url string to bytes
@@ -92,5 +96,7 @@ export function fromBase64url(string: string, options: FromBase64Options & { for
92
96
  * @param options - Decoding options
93
97
  * @returns The decoded bytes
94
98
  */
95
- export function fromBase64any(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
99
+ export function fromBase64any(string: string, options?: FromBase64Options & { format?: 'uint8' }): Uint8ArrayBuffer;
100
+ export function fromBase64any(string: string, options: FromBase64Options & { format: 'arraybuffer' }): ArrayBuffer;
96
101
  export function fromBase64any(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
102
+ export function fromBase64url(string: string, options?: FromBase64Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;