@exodus/bytes 1.8.0 → 1.10.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/array.d.ts CHANGED
@@ -1,24 +1,61 @@
1
+ /**
2
+ * TypedArray utils and conversions.
3
+ *
4
+ * ```js
5
+ * import { typedView } from '@exodus/bytes/array.js'
6
+ * ```
7
+ *
8
+ * @module @exodus/bytes/array.js
9
+ */
10
+
1
11
  /// <reference types="node" />
2
12
 
3
13
  // >= TypeScript 5.9 made Uint8Array templated with <> and defaulted to ArrayBufferLike
4
14
  // which would incorrectly accept SharedArrayBuffer instances.
5
15
  // < TypeScript 5.7 doesn't support templates for Uint8Array.
6
16
  // So this type is defined as a workaround to evaluate to Uint8Array<ArrayBuffer> on all versions of TypeScript.
17
+
18
+ /**
19
+ * This is `Uint8Array<ArrayBuffer>`
20
+ * (as opposed to `Uint8Array<SharedArrayBuffer>` and `Uint8Array<ArrayBufferLike>`)
21
+ * on TypeScript versions that support that distinction.
22
+ *
23
+ * On TypeScript < 5.7, this is just `Uint8Array`, as it's not a template there.
24
+ */
7
25
  export type Uint8ArrayBuffer = ReturnType<typeof Uint8Array.from>;
8
26
 
27
+ /**
28
+ * This is `Uint16Array<ArrayBuffer>`
29
+ * (as opposed to `Uint16Array<SharedArrayBuffer>` and `Uint16Array<ArrayBufferLike>`)
30
+ * on TypeScript versions that support that distinction.
31
+ *
32
+ * On TypeScript < 5.7, this is just `Uint16Array`, as it's not a template there.
33
+ */
34
+ export type Uint16ArrayBuffer = ReturnType<typeof Uint16Array.from>;
35
+
36
+ /**
37
+ * This is `Uint32Array<ArrayBuffer>`
38
+ * (as opposed to `Uint32Array<SharedArrayBuffer>` and `Uint32Array<ArrayBufferLike>`)
39
+ * on TypeScript versions that support that distinction.
40
+ *
41
+ * On TypeScript < 5.7, this is just `Uint32Array`, as it's not a template there.
42
+ */
43
+ export type Uint32ArrayBuffer = ReturnType<typeof Uint32Array.from>;
44
+
9
45
  /**
10
46
  * Output format for typed array conversions
11
47
  */
12
48
  export type OutputFormat = 'uint8' | 'buffer';
13
49
 
14
50
  /**
15
- * Creates a view of a TypedArray in the specified format
16
- * Note: This does not copy data - returns a view on the same underlying buffer
51
+ * Create a view of a TypedArray in the specified format (`'uint8'` or `'buffer'`)
52
+ *
53
+ * Important: does not copy data, returns a view on the same underlying buffer
54
+ *
17
55
  * @param arr - The input TypedArray
18
- * @param format - The desired output format ('uint8' or 'buffer')
56
+ * @param format - The desired output format (`'uint8'` or `'buffer'`)
19
57
  * @returns A view on the same underlying buffer
20
58
  */
21
59
  export function typedView(arr: ArrayBufferView, format: 'uint8'): Uint8Array;
22
60
  export function typedView(arr: ArrayBufferView, format: 'buffer'): Buffer;
23
61
  export function typedView(arr: ArrayBufferView, format: OutputFormat): Uint8Array | Buffer;
24
-
package/base32.d.ts ADDED
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Implements base32 and base32hex from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
3
+ * (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
4
+ *
5
+ * ```js
6
+ * import { fromBase32, toBase32 } from '@exodus/bytes/base32.js'
7
+ * import { fromBase32hex, toBase32hex } from '@exodus/bytes/base32.js'
8
+ * ```
9
+ *
10
+ * @module @exodus/bytes/base32.js
11
+ */
12
+
13
+ /// <reference types="node" />
14
+
15
+ import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
16
+
17
+ /**
18
+ * Options for base32 encoding
19
+ */
20
+ export interface ToBase32Options {
21
+ /** Whether to include padding characters (default: false) */
22
+ padding?: boolean;
23
+ }
24
+
25
+ /**
26
+ * Padding mode for base32 decoding
27
+ * - `true`: padding is required
28
+ * - `false`: padding is not allowed
29
+ * - `'both'`: padding is optional (default)
30
+ */
31
+ export type PaddingMode = boolean | 'both';
32
+
33
+ /**
34
+ * Options for base32 decoding
35
+ */
36
+ export interface FromBase32Options {
37
+ /** Output format (default: 'uint8') */
38
+ format?: OutputFormat;
39
+ /** Padding mode */
40
+ padding?: PaddingMode;
41
+ }
42
+
43
+ /**
44
+ * Encode a `Uint8Array` to a base32 string (RFC 4648)
45
+ *
46
+ * @param arr - The input bytes
47
+ * @param options - Encoding options
48
+ * @returns The base32 encoded string
49
+ */
50
+ export function toBase32(arr: Uint8Array, options?: ToBase32Options): string;
51
+
52
+ /**
53
+ * Encode a `Uint8Array` to a base32hex string (RFC 4648)
54
+ *
55
+ * @param arr - The input bytes
56
+ * @param options - Encoding options (padding defaults to false)
57
+ * @returns The base32hex encoded string
58
+ */
59
+ export function toBase32hex(arr: Uint8Array, options?: ToBase32Options): string;
60
+
61
+ /**
62
+ * Decode a base32 string to bytes
63
+ *
64
+ * Operates in strict mode for last chunk, does not allow whitespace
65
+ *
66
+ * @param string - The base32 encoded string
67
+ * @param options - Decoding options
68
+ * @returns The decoded bytes
69
+ */
70
+ export function fromBase32(string: string, options?: FromBase32Options): Uint8ArrayBuffer;
71
+ export function fromBase32(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;
72
+
73
+ /**
74
+ * Decode a base32hex string to bytes
75
+ *
76
+ * Operates in strict mode for last chunk, does not allow whitespace
77
+ *
78
+ * @param string - The base32hex encoded string
79
+ * @param options - Decoding options
80
+ * @returns The decoded bytes
81
+ */
82
+ export function fromBase32hex(string: string, options?: FromBase32Options): Uint8ArrayBuffer;
83
+ export function fromBase32hex(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;
package/base58.d.ts ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Implements [base58](https://www.ietf.org/archive/id/draft-msporny-base58-03.txt) encoding.
3
+ *
4
+ * Supports both standard base58 and XRP variant alphabets.
5
+ *
6
+ * ```js
7
+ * import { fromBase58, toBase58 } from '@exodus/bytes/base58.js'
8
+ * import { fromBase58xrp, toBase58xrp } from '@exodus/bytes/base58.js'
9
+ * ```
10
+ *
11
+ * @module @exodus/bytes/base58.js
12
+ */
13
+
14
+ /// <reference types="node" />
15
+
16
+ import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
17
+
18
+ /**
19
+ * Encode a `Uint8Array` to a base58 string
20
+ *
21
+ * Uses the standard Bitcoin base58 alphabet
22
+ *
23
+ * @param arr - The input bytes
24
+ * @returns The base58 encoded string
25
+ */
26
+ export function toBase58(arr: Uint8Array): string;
27
+
28
+ /**
29
+ * Decode a base58 string to bytes
30
+ *
31
+ * Uses the standard Bitcoin base58 alphabet
32
+ *
33
+ * @param string - The base58 encoded string
34
+ * @param format - Output format (default: 'uint8')
35
+ * @returns The decoded bytes
36
+ */
37
+ export function fromBase58(string: string, format?: 'uint8'): Uint8ArrayBuffer;
38
+ export function fromBase58(string: string, format: 'buffer'): Buffer;
39
+ export function fromBase58(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
40
+
41
+ /**
42
+ * Encode a `Uint8Array` to a base58 string using XRP alphabet
43
+ *
44
+ * Uses the XRP variant base58 alphabet
45
+ *
46
+ * @param arr - The input bytes
47
+ * @returns The base58 encoded string
48
+ */
49
+ export function toBase58xrp(arr: Uint8Array): string;
50
+
51
+ /**
52
+ * Decode a base58 string to bytes using XRP alphabet
53
+ *
54
+ * Uses the XRP variant base58 alphabet
55
+ *
56
+ * @param string - The base58 encoded string
57
+ * @param format - Output format (default: 'uint8')
58
+ * @returns The decoded bytes
59
+ */
60
+ export function fromBase58xrp(string: string, format?: 'uint8'): Uint8ArrayBuffer;
61
+ export function fromBase58xrp(string: string, format: 'buffer'): Buffer;
62
+ export function fromBase58xrp(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
package/base58.js CHANGED
@@ -207,7 +207,7 @@ function fromBase58core(str, alphabet, codes, format = 'uint8') {
207
207
  }
208
208
 
209
209
  at = k + 1
210
- if (c !== 0 || at < zeros) throw new Error('Unexpected') // unreachable
210
+ if (c !== 0 || at < zeros) /* c8 ignore next */ throw new Error('Unexpected') // unreachable
211
211
  }
212
212
  }
213
213
 
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Implements [base58check](https://en.bitcoin.it/wiki/Base58Check_encoding) encoding.
3
+ *
4
+ * ```js
5
+ * import { fromBase58check, toBase58check } from '@exodus/bytes/base58check.js'
6
+ * import { fromBase58checkSync, toBase58checkSync } from '@exodus/bytes/base58check.js'
7
+ * import { makeBase58check } from '@exodus/bytes/base58check.js'
8
+ * ```
9
+ *
10
+ * On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.
11
+ *
12
+ * @module @exodus/bytes/base58check.js
13
+ */
14
+
15
+ /// <reference types="node" />
16
+
17
+ import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
18
+
19
+ /**
20
+ * Hash function type that takes Uint8Array and returns a Promise of Uint8Array
21
+ */
22
+ export type HashFunction = (data: Uint8Array) => Promise<Uint8Array>;
23
+
24
+ /**
25
+ * Synchronous hash function type that takes Uint8Array and returns Uint8Array
26
+ */
27
+ export type HashFunctionSync = (data: Uint8Array) => Uint8Array;
28
+
29
+ /**
30
+ * Base58Check encoder/decoder instance with async methods
31
+ */
32
+ export interface Base58CheckAsync {
33
+ /**
34
+ * Encode bytes to base58check string asynchronously
35
+ *
36
+ * @param arr - The input bytes to encode
37
+ * @returns A Promise that resolves to the base58check encoded string
38
+ */
39
+ encode(arr: Uint8Array): Promise<string>;
40
+
41
+ /**
42
+ * Decode a base58check string to bytes asynchronously
43
+ *
44
+ * @param string - The base58check encoded string
45
+ * @param format - Output format (default: 'uint8')
46
+ * @returns A Promise that resolves to the decoded bytes
47
+ */
48
+ decode(string: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>;
49
+ decode(string: string, format: 'buffer'): Promise<Buffer>;
50
+ decode(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | Buffer>;
51
+ }
52
+
53
+ /**
54
+ * Base58Check encoder/decoder instance with both async and sync methods
55
+ */
56
+ export interface Base58CheckSync extends Base58CheckAsync {
57
+ /**
58
+ * Encode bytes to base58check string synchronously
59
+ *
60
+ * @param arr - The input bytes to encode
61
+ * @returns The base58check encoded string
62
+ */
63
+ encodeSync(arr: Uint8Array): string;
64
+
65
+ /**
66
+ * Decode a base58check string to bytes synchronously
67
+ *
68
+ * @param string - The base58check encoded string
69
+ * @param format - Output format (default: 'uint8')
70
+ * @returns The decoded bytes
71
+ */
72
+ decodeSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
73
+ decodeSync(string: string, format: 'buffer'): Buffer;
74
+ decodeSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
75
+ }
76
+
77
+ /**
78
+ * Create a base58check encoder/decoder with custom hash functions
79
+ *
80
+ * @param hashAlgo - Async hash function (typically double SHA-256)
81
+ * @param hashAlgoSync - Optional sync hash function
82
+ * @returns Base58Check encoder/decoder instance
83
+ */
84
+ export function makeBase58check(hashAlgo: HashFunction, hashAlgoSync?: HashFunctionSync): Base58CheckSync;
85
+ export function makeBase58check(hashAlgo: HashFunction): Base58CheckAsync;
86
+
87
+ /**
88
+ * Encode bytes to base58check string asynchronously
89
+ *
90
+ * Uses double SHA-256 for checksum calculation
91
+ *
92
+ * @param arr - The input bytes to encode
93
+ * @returns A Promise that resolves to the base58check encoded string
94
+ */
95
+ export function toBase58check(arr: Uint8Array): Promise<string>;
96
+
97
+ /**
98
+ * Decode a base58check string to bytes asynchronously
99
+ *
100
+ * Validates the checksum using double SHA-256
101
+ *
102
+ * @param string - The base58check encoded string
103
+ * @param format - Output format (default: 'uint8')
104
+ * @returns A Promise that resolves to the decoded bytes
105
+ */
106
+ export function fromBase58check(string: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>;
107
+ export function fromBase58check(string: string, format: 'buffer'): Promise<Buffer>;
108
+ export function fromBase58check(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | Buffer>;
109
+
110
+ /**
111
+ * Encode bytes to base58check string synchronously
112
+ *
113
+ * Uses double SHA-256 for checksum calculation
114
+ *
115
+ * @param arr - The input bytes to encode
116
+ * @returns The base58check encoded string
117
+ */
118
+ export function toBase58checkSync(arr: Uint8Array): string;
119
+
120
+ /**
121
+ * Decode a base58check string to bytes synchronously
122
+ *
123
+ * Validates the checksum using double SHA-256
124
+ *
125
+ * @param string - The base58check encoded string
126
+ * @param format - Output format (default: 'uint8')
127
+ * @returns The decoded bytes
128
+ */
129
+ export function fromBase58checkSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
130
+ export function fromBase58checkSync(string: string, format: 'buffer'): Buffer;
131
+ export function fromBase58checkSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
package/base58check.js CHANGED
@@ -1,11 +1,11 @@
1
- import { hashSync } from '@exodus/crypto/hash' // eslint-disable-line @exodus/import/no-deprecated
1
+ import { sha256 } from '@noble/hashes/sha2.js'
2
2
  import { makeBase58check } from './fallback/base58check.js'
3
3
 
4
4
  // Note: while API is async, we use hashSync for now until we improve webcrypto perf for hash256
5
5
  // Inputs to base58 are typically very small, and that makes a difference
6
6
 
7
- // eslint-disable-next-line @exodus/import/no-deprecated
8
- const sha256 = (x) => hashSync('sha256', x, 'uint8')
7
+ // Note: using native WebCrypto will have to have account for SharedArrayBuffer
8
+
9
9
  const hash256sync = (x) => sha256(sha256(x))
10
10
  const hash256 = hash256sync // See note at the top
11
11
  const {
package/base64.d.ts CHANGED
@@ -1,3 +1,16 @@
1
+ /**
2
+ * Implements base64 and base64url from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
3
+ * (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
4
+ *
5
+ * ```js
6
+ * import { fromBase64, toBase64 } from '@exodus/bytes/base64.js'
7
+ * import { fromBase64url, toBase64url } from '@exodus/bytes/base64.js'
8
+ * import { fromBase64any } from '@exodus/bytes/base64.js'
9
+ * ```
10
+ *
11
+ * @module @exodus/bytes/base64.js
12
+ */
13
+
1
14
  /// <reference types="node" />
2
15
 
3
16
  import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
@@ -12,9 +25,9 @@ export interface ToBase64Options {
12
25
 
13
26
  /**
14
27
  * Padding mode for base64 decoding
15
- * - true: padding is required
16
- * - false: padding is not allowed
17
- * - 'both': padding is optional (default for base64)
28
+ * - `true`: padding is required
29
+ * - `false`: padding is not allowed (default for base64url)
30
+ * - `'both'`: padding is optional (default for base64)
18
31
  */
19
32
  export type PaddingMode = boolean | 'both';
20
33
 
@@ -29,48 +42,55 @@ export interface FromBase64Options {
29
42
  }
30
43
 
31
44
  /**
32
- * Encodes a Uint8Array to a base64 string (RFC 4648)
45
+ * Encode a `Uint8Array` to a base64 string (RFC 4648)
46
+ *
33
47
  * @param arr - The input bytes
34
48
  * @param options - Encoding options
35
49
  * @returns The base64 encoded string
36
50
  */
37
- export function toBase64(arr: Uint8ArrayBuffer, options?: ToBase64Options): string;
51
+ export function toBase64(arr: Uint8Array, options?: ToBase64Options): string;
38
52
 
39
53
  /**
40
- * Encodes a Uint8Array to a base64url string (RFC 4648)
54
+ * Encode a `Uint8Array` to a base64url string (RFC 4648)
55
+ *
41
56
  * @param arr - The input bytes
42
57
  * @param options - Encoding options (padding defaults to false)
43
58
  * @returns The base64url encoded string
44
59
  */
45
- export function toBase64url(arr: Uint8ArrayBuffer, options?: ToBase64Options): string;
60
+ export function toBase64url(arr: Uint8Array, options?: ToBase64Options): string;
46
61
 
47
62
  /**
48
- * Decodes a base64 string to bytes
63
+ * Decode a base64 string to bytes
64
+ *
49
65
  * Operates in strict mode for last chunk, does not allow whitespace
50
- * @param str - The base64 encoded string
66
+ *
67
+ * @param string - The base64 encoded string
51
68
  * @param options - Decoding options
52
69
  * @returns The decoded bytes
53
70
  */
54
- export function fromBase64(str: string, options?: FromBase64Options): Uint8ArrayBuffer;
55
- export function fromBase64(str: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
71
+ export function fromBase64(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
72
+ export function fromBase64(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
56
73
 
57
74
  /**
58
- * Decodes a base64url string to bytes
75
+ * Decode a base64url string to bytes
76
+ *
59
77
  * Operates in strict mode for last chunk, does not allow whitespace
60
- * @param str - The base64url encoded string
78
+ *
79
+ * @param string - The base64url encoded string
61
80
  * @param options - Decoding options (padding defaults to false)
62
81
  * @returns The decoded bytes
63
82
  */
64
- export function fromBase64url(str: string, options?: FromBase64Options): Uint8ArrayBuffer;
65
- export function fromBase64url(str: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
83
+ export function fromBase64url(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
84
+ export function fromBase64url(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
66
85
 
67
86
  /**
68
- * Decodes either base64 or base64url string to bytes
87
+ * Decode either base64 or base64url string to bytes
88
+ *
69
89
  * Automatically detects the variant based on characters present
70
- * @param str - The base64 or base64url encoded string
90
+ *
91
+ * @param string - The base64 or base64url encoded string
71
92
  * @param options - Decoding options
72
93
  * @returns The decoded bytes
73
94
  */
74
- export function fromBase64any(str: string, options?: FromBase64Options): Uint8ArrayBuffer;
75
- export function fromBase64any(str: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
76
-
95
+ export function fromBase64any(string: string, options?: FromBase64Options): Uint8ArrayBuffer;
96
+ export function fromBase64any(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
package/bech32.d.ts ADDED
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Implements bech32 and bech32m from
3
+ * [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#specification)
4
+ * and [BIP-0350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki#specification).
5
+ *
6
+ * ```js
7
+ * import { fromBech32, toBech32 } from '@exodus/bytes/bech32.js'
8
+ * import { fromBech32m, toBech32m } from '@exodus/bytes/bech32.js'
9
+ * import { getPrefix } from '@exodus/bytes/bech32.js'
10
+ * ```
11
+ *
12
+ * @module @exodus/bytes/bech32.js
13
+ */
14
+
15
+ /// <reference types="node" />
16
+
17
+ import type { Uint8ArrayBuffer } from './array.js';
18
+
19
+ /**
20
+ * Result of decoding a bech32 or bech32m string
21
+ */
22
+ export interface Bech32DecodeResult {
23
+ /** The human-readable prefix */
24
+ prefix: string;
25
+ /** The decoded bytes */
26
+ bytes: Uint8ArrayBuffer;
27
+ }
28
+
29
+ /**
30
+ * Encode bytes to a bech32 string
31
+ *
32
+ * @param prefix - The human-readable prefix (e.g., 'bc' for Bitcoin)
33
+ * @param bytes - The input bytes to encode
34
+ * @param limit - Maximum length of the encoded string (default: 90)
35
+ * @returns The bech32 encoded string
36
+ */
37
+ export function toBech32(prefix: string, bytes: Uint8Array, limit?: number): string;
38
+
39
+ /**
40
+ * Decode a bech32 string to bytes
41
+ *
42
+ * @param string - The bech32 encoded string
43
+ * @param limit - Maximum length of the input string (default: 90)
44
+ * @returns The decoded prefix and bytes
45
+ */
46
+ export function fromBech32(string: string, limit?: number): Bech32DecodeResult;
47
+
48
+ /**
49
+ * Encode bytes to a bech32m string
50
+ *
51
+ * @param prefix - The human-readable prefix (e.g., 'bc' for Bitcoin)
52
+ * @param bytes - The input bytes to encode
53
+ * @param limit - Maximum length of the encoded string (default: 90)
54
+ * @returns The bech32m encoded string
55
+ */
56
+ export function toBech32m(prefix: string, bytes: Uint8Array, limit?: number): string;
57
+
58
+ /**
59
+ * Decode a bech32m string to bytes
60
+ *
61
+ * @param string - The bech32m encoded string
62
+ * @param limit - Maximum length of the input string (default: 90)
63
+ * @returns The decoded prefix and bytes
64
+ */
65
+ export function fromBech32m(string: string, limit?: number): Bech32DecodeResult;
66
+
67
+ /**
68
+ * Extract the prefix from a bech32 or bech32m string without full validation
69
+ *
70
+ * This is a quick check that skips most validation.
71
+ *
72
+ * @param string - The bech32/bech32m encoded string
73
+ * @param limit - Maximum length of the input string (default: 90)
74
+ * @returns The lowercase prefix
75
+ */
76
+ export function getPrefix(string: string, limit?: number): string;
package/bigint.d.ts ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Convert between BigInt and Uint8Array
3
+ *
4
+ * ```js
5
+ * import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
6
+ * ```
7
+ *
8
+ * @module @exodus/bytes/bigint.js
9
+ */
10
+
11
+ /// <reference types="node" />
12
+
13
+ import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
14
+
15
+ /**
16
+ * Options for converting BigInt to bytes
17
+ */
18
+ export interface FromBigIntOptions {
19
+ /** The length in bytes of the output array */
20
+ length: number;
21
+ /** Output format (default: 'uint8') */
22
+ format?: OutputFormat;
23
+ }
24
+
25
+ /**
26
+ * Convert a BigInt to a Uint8Array or Buffer
27
+ *
28
+ * The output bytes are in big-endian format.
29
+ *
30
+ * Throws if the BigInt is negative or cannot fit into the specified length.
31
+ *
32
+ * @param bigint - The BigInt to convert (must be non-negative)
33
+ * @param options - Conversion options
34
+ * @returns The converted bytes in big-endian format
35
+ */
36
+ export function fromBigInt(bigint: bigint, options: { length: number; format?: 'uint8' }): Uint8ArrayBuffer;
37
+ export function fromBigInt(bigint: bigint, options: { length: number; format: 'buffer' }): Buffer;
38
+ export function fromBigInt(bigint: bigint, options: FromBigIntOptions): Uint8ArrayBuffer | Buffer;
39
+
40
+ /**
41
+ * Convert a Uint8Array or Buffer to a BigInt
42
+ *
43
+ * The bytes are interpreted as a big-endian unsigned integer.
44
+ *
45
+ * @param arr - The bytes to convert
46
+ * @returns The BigInt representation
47
+ */
48
+ export function toBigInt(arr: Uint8Array): bigint;
@@ -0,0 +1,29 @@
1
+ import {
2
+ fromSource,
3
+ getBOMEncoding,
4
+ normalizeEncoding,
5
+ E_ENCODING,
6
+ } from './fallback/encoding.api.js'
7
+ import labels from './fallback/encoding.labels.js'
8
+
9
+ // Lite-weight version which re-exports existing implementations on browsers,
10
+ // while still being aliased to the full impl in RN and Node.js
11
+
12
+ // WARNING: Note that browsers have bugs (which hopefully will get fixed soon)
13
+
14
+ const { TextDecoder, TextEncoder, TextDecoderStream, TextEncoderStream } = globalThis
15
+
16
+ export { normalizeEncoding, getBOMEncoding, labelToName } from './fallback/encoding.api.js'
17
+ export { TextDecoder, TextEncoder, TextDecoderStream, TextEncoderStream }
18
+
19
+ // https://encoding.spec.whatwg.org/#decode
20
+ export function legacyHookDecode(input, fallbackEncoding = 'utf-8') {
21
+ let u8 = fromSource(input)
22
+ const bomEncoding = getBOMEncoding(u8)
23
+ if (bomEncoding) u8 = u8.subarray(bomEncoding === 'utf-8' ? 3 : 2)
24
+ const enc = bomEncoding ?? normalizeEncoding(fallbackEncoding) // "the byte order mark is more authoritative than anything else"
25
+ if (enc === 'utf-8') return new TextDecoder('utf-8', { ignoreBOM: true }).decode(u8) // fast path
26
+ if (enc === 'replacement') return u8.byteLength > 0 ? '\uFFFD' : ''
27
+ if (!Object.hasOwn(labels, enc)) throw new RangeError(E_ENCODING)
28
+ return new TextDecoder(enc, { ignoreBOM: true }).decode(u8)
29
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Same as `@exodus/bytes/encoding.js`, but in browsers instead of polyfilling just uses whatever the
3
+ * browser provides, drastically reducing the bundle size (to less than 2 KiB gzipped).
4
+ *
5
+ * ```js
6
+ * import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-browser.js'
7
+ * import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-browser.js' // Requires Streams
8
+ *
9
+ * // Hooks for standards
10
+ * import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-browser.js'
11
+ * ```
12
+ *
13
+ * Under non-browser engines (Node.js, React Native, etc.) a full polyfill is used as those platforms
14
+ * do not provide sufficiently complete / non-buggy `TextDecoder` APIs.
15
+ *
16
+ * > [!NOTE]
17
+ * > Implementations in browsers [have bugs](https://docs.google.com/spreadsheets/d/1pdEefRG6r9fZy61WHGz0TKSt8cO4ISWqlpBN5KntIvQ/edit),
18
+ * > but they are fixing them and the expected update window is short.\
19
+ * > If you want to circumvent browser bugs, use full `@exodus/bytes/encoding.js` import.
20
+ *
21
+ * @module @exodus/bytes/encoding-browser.js
22
+ */
23
+
24
+ export * from './encoding.js'
@@ -0,0 +1 @@
1
+ export * from './encoding.js'
@@ -0,0 +1 @@
1
+ export * from './encoding.js'