@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/README.md +370 -90
- package/array.d.ts +41 -4
- package/base32.d.ts +83 -0
- package/base58.d.ts +62 -0
- package/base58.js +1 -1
- package/base58check.d.ts +131 -0
- package/base58check.js +3 -3
- package/base64.d.ts +40 -20
- package/bech32.d.ts +76 -0
- package/bigint.d.ts +48 -0
- package/encoding-browser.browser.js +29 -0
- package/encoding-browser.d.ts +24 -0
- package/encoding-browser.js +1 -0
- package/encoding-browser.native.js +1 -0
- package/encoding-lite.d.ts +61 -0
- package/encoding.d.ts +93 -11
- package/encoding.js +4 -3
- package/fallback/_utils.js +15 -11
- package/fallback/encoding.api.js +81 -0
- package/fallback/encoding.js +37 -121
- package/fallback/encoding.util.js +34 -0
- package/fallback/latin1.js +1 -0
- package/fallback/multi-byte.encodings.json +1 -0
- package/fallback/multi-byte.js +527 -71
- package/fallback/multi-byte.table.js +23 -15
- package/fallback/single-byte.js +1 -1
- package/fallback/utf16.js +45 -26
- package/fallback/utf8.js +1 -1
- package/hex.d.ts +22 -9
- package/index.d.ts +43 -0
- package/index.js +5 -0
- package/multi-byte.d.ts +57 -0
- package/multi-byte.js +7 -1
- package/multi-byte.node.js +7 -1
- package/package.json +83 -10
- package/single-byte.d.ts +149 -0
- package/single-byte.js +9 -11
- package/single-byte.node.js +29 -26
- package/utf16.d.ts +92 -0
- package/utf16.js +1 -0
- package/utf16.node.js +6 -2
- package/utf8.d.ts +52 -18
- package/utf8.js +7 -2
- package/utf8.node.js +1 -1
- package/wif.d.ts +76 -0
|
@@ -40,7 +40,7 @@ function loadBase64(str) {
|
|
|
40
40
|
return y
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
function unwrap(res, t, pos,
|
|
43
|
+
function unwrap(res, t, pos, highMode = false) {
|
|
44
44
|
let code = 0
|
|
45
45
|
for (let i = 0; i < t.length; i++) {
|
|
46
46
|
let x = t[i]
|
|
@@ -55,25 +55,30 @@ function unwrap(res, t, pos, stringMode = false) {
|
|
|
55
55
|
code += t[++i]
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
if (
|
|
58
|
+
if (highMode) {
|
|
59
59
|
for (let k = 0; k < x; k++, pos++, code++) {
|
|
60
|
-
|
|
60
|
+
if (code <= 0xff_ff) {
|
|
61
|
+
res[pos] = code
|
|
62
|
+
} else {
|
|
63
|
+
const c = String.fromCodePoint(code)
|
|
64
|
+
res[pos] = (c.charCodeAt(0) << 16) | c.charCodeAt(1)
|
|
65
|
+
}
|
|
61
66
|
}
|
|
62
67
|
} else {
|
|
63
68
|
for (let k = 0; k < x; k++, pos++, code++) res[pos] = code
|
|
64
69
|
}
|
|
65
70
|
}
|
|
66
71
|
} else if (x[0] === '$' && Object.hasOwn(indices, x)) {
|
|
67
|
-
pos = unwrap(res, indices[x], pos,
|
|
68
|
-
} else if (
|
|
72
|
+
pos = unwrap(res, indices[x], pos, highMode) // self-reference using shared chunks
|
|
73
|
+
} else if (highMode) {
|
|
69
74
|
const s = [...utf16toString(loadBase64(x), 'uint8-le')] // splits by codepoints
|
|
70
|
-
let
|
|
75
|
+
let c
|
|
71
76
|
for (let i = 0; i < s.length; ) {
|
|
72
|
-
|
|
73
|
-
res[pos++] =
|
|
77
|
+
c = s[i++]
|
|
78
|
+
res[pos++] = c.length === 1 ? c.charCodeAt(0) : (c.charCodeAt(0) << 16) | c.charCodeAt(1)
|
|
74
79
|
}
|
|
75
80
|
|
|
76
|
-
code =
|
|
81
|
+
code = c.codePointAt(0) + 1
|
|
77
82
|
} else {
|
|
78
83
|
const u16 = to16input(loadBase64(x), true) // data is little-endian
|
|
79
84
|
res.set(u16, pos)
|
|
@@ -99,19 +104,22 @@ export function getTable(id) {
|
|
|
99
104
|
let a = 0, b = 0 // prettier-ignore
|
|
100
105
|
const idx = indices[id]
|
|
101
106
|
while (idx.length > 0) res.push([(a += idx.shift()), (b += idx.shift())]) // destroying, we remove it later anyway
|
|
107
|
+
} else if (id.endsWith('-katakana')) {
|
|
108
|
+
let a = -1
|
|
109
|
+
res = new Uint16Array(indices[id].map((x) => (a += x + 1)))
|
|
102
110
|
} else if (id === 'big5') {
|
|
103
111
|
if (!Object.hasOwn(sizes, id)) throw new Error('Unknown encoding')
|
|
104
|
-
res = new
|
|
112
|
+
res = new Uint32Array(sizes[id]) // array of strings or undefined
|
|
105
113
|
unwrap(res, indices[id], 0, true)
|
|
106
114
|
// Pointer code updates are embedded into the table
|
|
107
|
-
|
|
108
|
-
res[
|
|
109
|
-
res[
|
|
110
|
-
res[
|
|
115
|
+
// These are skipped in encoder as encoder uses only pointers >= (0xA1 - 0x81) * 157
|
|
116
|
+
res[1133] = 0xca_03_04
|
|
117
|
+
res[1135] = 0xca_03_0c
|
|
118
|
+
res[1164] = 0xea_03_04
|
|
119
|
+
res[1166] = 0xea_03_0c
|
|
111
120
|
} else {
|
|
112
121
|
if (!Object.hasOwn(sizes, id)) throw new Error('Unknown encoding')
|
|
113
122
|
res = new Uint16Array(sizes[id])
|
|
114
|
-
res.fill(0xff_fd)
|
|
115
123
|
unwrap(res, indices[id], 0, false)
|
|
116
124
|
}
|
|
117
125
|
|
package/fallback/single-byte.js
CHANGED
|
@@ -13,7 +13,7 @@ export const assertEncoding = (encoding) => {
|
|
|
13
13
|
|
|
14
14
|
const r = 0xff_fd
|
|
15
15
|
|
|
16
|
-
function getEncoding(encoding) {
|
|
16
|
+
export function getEncoding(encoding) {
|
|
17
17
|
assertEncoding(encoding)
|
|
18
18
|
if (encoding === xUserDefined) return Array.from({ length: 128 }, (_, i) => 0xf7_80 + i)
|
|
19
19
|
if (encoding === iso8i) encoding = 'iso-8859-8'
|
package/fallback/utf16.js
CHANGED
|
@@ -12,45 +12,48 @@ const to16 = (a) => new Uint16Array(a.buffer, a.byteOffset, a.byteLength / 2) //
|
|
|
12
12
|
export function to16input(u8, le) {
|
|
13
13
|
// Assume even number of bytes
|
|
14
14
|
if (le === isLE) return to16(u8.byteOffset % 2 === 0 ? u8 : Uint8Array.from(u8))
|
|
15
|
+
return to16(swap16(Uint8Array.from(u8)))
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const decode = (u16, loose = false, checked = false) => {
|
|
19
|
+
if (checked || isWellFormed(u16)) return decodeUCS2(u16)
|
|
20
|
+
if (!loose) throw new TypeError(E_STRICT)
|
|
21
|
+
return decodeUCS2(toWellFormed(Uint16Array.from(u16))) // cloned for replacement
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function encode(str, loose = false, checked = false, swapped = false) {
|
|
25
|
+
const arr = new Uint16Array(str.length)
|
|
26
|
+
if (checked) return swapped ? encodeCheckedSwapped(str, arr) : encodeChecked(str, arr)
|
|
27
|
+
return swapped ? encodeUncheckedSwapped(str, arr, loose) : encodeUnchecked(str, arr, loose)
|
|
28
|
+
}
|
|
15
29
|
|
|
16
|
-
|
|
30
|
+
/* eslint-disable @exodus/mutable/no-param-reassign-prop-only */
|
|
17
31
|
|
|
32
|
+
// Assumes checked length % 2 === 0, otherwise does not swap tail
|
|
33
|
+
function swap16(u8) {
|
|
18
34
|
let i = 0
|
|
19
35
|
for (const last3 = u8.length - 3; i < last3; i += 4) {
|
|
20
36
|
const x0 = u8[i]
|
|
21
37
|
const x1 = u8[i + 1]
|
|
22
38
|
const x2 = u8[i + 2]
|
|
23
39
|
const x3 = u8[i + 3]
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
40
|
+
u8[i] = x1
|
|
41
|
+
u8[i + 1] = x0
|
|
42
|
+
u8[i + 2] = x3
|
|
43
|
+
u8[i + 3] = x2
|
|
28
44
|
}
|
|
29
45
|
|
|
30
46
|
for (const last = u8.length - 1; i < last; i += 2) {
|
|
31
47
|
const x0 = u8[i]
|
|
32
48
|
const x1 = u8[i + 1]
|
|
33
|
-
|
|
34
|
-
|
|
49
|
+
u8[i] = x1
|
|
50
|
+
u8[i + 1] = x0
|
|
35
51
|
}
|
|
36
52
|
|
|
37
|
-
return
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export const decode = (u16, loose = false, checked = false) => {
|
|
41
|
-
if (checked || isWellFormed(u16)) return decodeUCS2(u16)
|
|
42
|
-
if (!loose) throw new TypeError(E_STRICT)
|
|
43
|
-
return decodeUCS2(toWellFormed(Uint16Array.from(u16))) // cloned for replacement
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function encode(str, loose = false, checked = false, swapped = false) {
|
|
47
|
-
const arr = new Uint16Array(str.length)
|
|
48
|
-
if (checked) return swapped ? encodeCheckedSwapped(str, arr) : encodeChecked(str, arr)
|
|
49
|
-
return swapped ? encodeUncheckedSwapped(str, arr, loose) : encodeUnchecked(str, arr, loose)
|
|
53
|
+
return u8
|
|
50
54
|
}
|
|
51
55
|
|
|
52
56
|
// Splitting paths into small functions helps (at least on SpiderMonkey)
|
|
53
|
-
/* eslint-disable @exodus/mutable/no-param-reassign-prop-only */
|
|
54
57
|
|
|
55
58
|
const encodeChecked = (str, arr) => encodeCharcodes(str, arr) // Same as encodeLatin1, but with Uint16Array
|
|
56
59
|
|
|
@@ -121,6 +124,7 @@ function encodeUncheckedSwapped(str, arr, loose = false) {
|
|
|
121
124
|
return arr
|
|
122
125
|
}
|
|
123
126
|
|
|
127
|
+
// Only needed on Hermes, everything else has native impl
|
|
124
128
|
export function toWellFormed(u16) {
|
|
125
129
|
const length = u16.length
|
|
126
130
|
for (let i = 0; i < length; i++) {
|
|
@@ -143,10 +147,15 @@ export function toWellFormed(u16) {
|
|
|
143
147
|
return u16
|
|
144
148
|
}
|
|
145
149
|
|
|
150
|
+
// Only needed on Hermes, everything else has native impl
|
|
146
151
|
export function isWellFormed(u16) {
|
|
147
152
|
const length = u16.length
|
|
148
153
|
let i = 0
|
|
149
154
|
|
|
155
|
+
const m = 0x80_00_80_00
|
|
156
|
+
const l = 0xd8_00
|
|
157
|
+
const h = 0xe0_00
|
|
158
|
+
|
|
150
159
|
// Speedup with u32, by skipping to the first surrogate
|
|
151
160
|
// Only implemented for aligned input for now, but almost all input is aligned (pooled Buffer or 0 offset)
|
|
152
161
|
if (length > 32 && u16.byteOffset % 4 === 0) {
|
|
@@ -158,21 +167,31 @@ export function isWellFormed(u16) {
|
|
|
158
167
|
const b = u32[i + 1]
|
|
159
168
|
const c = u32[i + 2]
|
|
160
169
|
const d = u32[i + 3]
|
|
161
|
-
if (a &
|
|
170
|
+
if (a & m || b & m || c & m || d & m) break // bitwise OR does not make this faster on Hermes
|
|
162
171
|
}
|
|
163
172
|
|
|
164
|
-
for (; i < u32length; i++) if (u32[i] &
|
|
173
|
+
for (; i < u32length; i++) if (u32[i] & m) break
|
|
165
174
|
i *= 2
|
|
166
175
|
}
|
|
167
176
|
|
|
177
|
+
// An extra loop gives ~30-40% speedup e.g. on English text without surrogates but with other symbols above 0x80_00
|
|
178
|
+
for (const last3 = length - 3; ; i += 4) {
|
|
179
|
+
if (i >= last3) break
|
|
180
|
+
const a = u16[i]
|
|
181
|
+
const b = u16[i + 1]
|
|
182
|
+
const c = u16[i + 2]
|
|
183
|
+
const d = u16[i + 3]
|
|
184
|
+
if ((a >= l && a < h) || (b >= l && b < h) || (c >= l && c < h) || (d >= l && d < h)) break
|
|
185
|
+
}
|
|
186
|
+
|
|
168
187
|
for (; i < length; i++) {
|
|
169
188
|
const code = u16[i]
|
|
170
|
-
if (code >=
|
|
189
|
+
if (code >= l && code < h) {
|
|
171
190
|
// An unexpected trail or a lead at the very end of input
|
|
172
|
-
if (code
|
|
191
|
+
if (code >= 0xdc_00 || i + 1 >= length) return false
|
|
173
192
|
i++ // consume next
|
|
174
193
|
const next = u16[i] // Process valid pairs immediately
|
|
175
|
-
if (next < 0xdc_00 || next >=
|
|
194
|
+
if (next < 0xdc_00 || next >= h) return false
|
|
176
195
|
}
|
|
177
196
|
}
|
|
178
197
|
|
package/fallback/utf8.js
CHANGED
|
@@ -191,7 +191,7 @@ export function encode(string, loose) {
|
|
|
191
191
|
|
|
192
192
|
if (small) {
|
|
193
193
|
// TODO: use resizable array buffers? will have to return a non-resizeable one
|
|
194
|
-
if (p !== i) throw new Error('Unreachable') // Here, p === i (only when small is still true)
|
|
194
|
+
if (p !== i) /* c8 ignore next */ throw new Error('Unreachable') // Here, p === i (only when small is still true)
|
|
195
195
|
const bytesNew = new Uint8Array(p + (length - i) * 3) // maximium can be 3x of the string length in charcodes
|
|
196
196
|
bytesNew.set(bytes)
|
|
197
197
|
bytes = bytesNew
|
package/hex.d.ts
CHANGED
|
@@ -1,22 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implements Base16 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 { fromHex, toHex } from '@exodus/bytes/hex.js'
|
|
7
|
+
* ```
|
|
8
|
+
*
|
|
9
|
+
* @module @exodus/bytes/hex.js
|
|
10
|
+
*/
|
|
11
|
+
|
|
1
12
|
/// <reference types="node" />
|
|
2
13
|
|
|
3
14
|
import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
|
|
4
15
|
|
|
5
16
|
/**
|
|
6
|
-
*
|
|
17
|
+
* Encode a `Uint8Array` to a lowercase hex string
|
|
18
|
+
*
|
|
7
19
|
* @param arr - The input bytes
|
|
8
20
|
* @returns The hex encoded string
|
|
9
21
|
*/
|
|
10
|
-
export function toHex(arr:
|
|
22
|
+
export function toHex(arr: Uint8Array): string;
|
|
11
23
|
|
|
12
24
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
25
|
+
* Decode a hex string to bytes
|
|
26
|
+
*
|
|
27
|
+
* Unlike `Buffer.from()`, throws on invalid input
|
|
28
|
+
*
|
|
29
|
+
* @param string - The hex encoded string (case-insensitive)
|
|
16
30
|
* @param format - Output format (default: 'uint8')
|
|
17
31
|
* @returns The decoded bytes
|
|
18
32
|
*/
|
|
19
|
-
export function fromHex(
|
|
20
|
-
export function fromHex(
|
|
21
|
-
export function fromHex(
|
|
22
|
-
|
|
33
|
+
export function fromHex(string: string, format?: 'uint8'): Uint8ArrayBuffer;
|
|
34
|
+
export function fromHex(string: string, format: 'buffer'): Buffer;
|
|
35
|
+
export function fromHex(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ### The `@exodus/bytes` package consists of submodules, there is no single export.
|
|
3
|
+
* Import specific submodules instead.
|
|
4
|
+
*
|
|
5
|
+
* See [README](https://github.com/ExodusOSS/bytes/blob/main/README.md).
|
|
6
|
+
*
|
|
7
|
+
* Example:
|
|
8
|
+
* ```js
|
|
9
|
+
* import { fromHex, toHex } from '@exodus/bytes/hex.js'
|
|
10
|
+
* import { fromBase64, toBase64, fromBase64url, toBase64url, fromBase64any } from '@exodus/bytes/base64.js'
|
|
11
|
+
* import { fromBase32, toBase32, fromBase32hex, toBase32hex } from '@exodus/bytes/base32.js'
|
|
12
|
+
* import { fromBase58, toBase58, fromBase58xrp, toBase58xrp } from '@exodus/bytes/base58.js'
|
|
13
|
+
* import { fromBech32, toBech32, fromBech32m, toBech32m, getPrefix } from '@exodus/bytes/bech32.js'
|
|
14
|
+
* import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
|
|
15
|
+
*
|
|
16
|
+
* import { utf8fromString, utf8toString, utf8fromStringLoose, utf8toStringLoose } from '@exodus/bytes/utf8.js'
|
|
17
|
+
* import { utf16fromString, utf16toString, utf16fromStringLoose, utf16toStringLoose } from '@exodus/bytes/utf16.js'
|
|
18
|
+
* import {
|
|
19
|
+
* createSinglebyteDecoder, createSinglebyteEncoder,
|
|
20
|
+
* windows1252toString, windows1252fromString,
|
|
21
|
+
* latin1toString, latin1fromString } from '@exodus/bytes/single-byte.js'
|
|
22
|
+
* import { createMultibyteDecoder, createMultibyteEncoder } from '@exodus/bytes/multi-byte.js'
|
|
23
|
+
*
|
|
24
|
+
* import {
|
|
25
|
+
* fromBase58check, toBase58check,
|
|
26
|
+
* fromBase58checkSync, toBase58checkSync,
|
|
27
|
+
* makeBase58check } from '@exodus/bytes/base58check.js'
|
|
28
|
+
* import { fromWifString, toWifString, fromWifStringSync, toWifStringSync } from '@exodus/bytes/wif.js'
|
|
29
|
+
*
|
|
30
|
+
* // All encodings from the WHATWG Encoding spec
|
|
31
|
+
* import { TextDecoder, TextEncoder, TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding.js'
|
|
32
|
+
* import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding.js'
|
|
33
|
+
*
|
|
34
|
+
* // Omits legacy multi-byte decoders to save bundle size
|
|
35
|
+
* import { TextDecoder, TextEncoder, TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-lite.js'
|
|
36
|
+
* import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-lite.js'
|
|
37
|
+
*
|
|
38
|
+
* // In browser bundles, uses built-in TextDecoder / TextEncoder to save bundle size
|
|
39
|
+
* import { TextDecoder, TextEncoder, TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-browser.js'
|
|
40
|
+
* import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-browser.js'
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare module '@exodus/bytes' {}
|
package/index.js
ADDED
package/multi-byte.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decode / encode the legacy multi-byte encodings according to the
|
|
3
|
+
* [Encoding standard](https://encoding.spec.whatwg.org/)
|
|
4
|
+
* ([§10](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(simplified)-encodings),
|
|
5
|
+
* [§11](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(traditional)-encodings),
|
|
6
|
+
* [§12](https://encoding.spec.whatwg.org/#legacy-multi-byte-japanese-encodings),
|
|
7
|
+
* [§13](https://encoding.spec.whatwg.org/#legacy-multi-byte-korean-encodings)).
|
|
8
|
+
*
|
|
9
|
+
* ```js
|
|
10
|
+
* import { createMultibyteDecoder, createMultibyteEncoder } from '@exodus/bytes/multi-byte.js'
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* Supports all legacy multi-byte encodings listed in the WHATWG Encoding standard:
|
|
14
|
+
* `gbk`, `gb18030`, `big5`, `euc-jp`, `iso-2022-jp`, `shift_jis`, `euc-kr`.
|
|
15
|
+
*
|
|
16
|
+
* @module @exodus/bytes/multi-byte.js
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/// <reference types="node" />
|
|
20
|
+
|
|
21
|
+
import type { Uint8ArrayBuffer } from './array.js';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Create a decoder for a supported legacy multi-byte `encoding`, given its lowercased name `encoding`.
|
|
25
|
+
*
|
|
26
|
+
* Returns a function `decode(arr, stream = false)` that decodes bytes to a string.
|
|
27
|
+
*
|
|
28
|
+
* The returned function will maintain internal state while `stream = true` is used, allowing it to
|
|
29
|
+
* handle incomplete multi-byte sequences across multiple calls.
|
|
30
|
+
* State is reset when `stream = false` or when the function is called without the `stream` parameter.
|
|
31
|
+
*
|
|
32
|
+
* @param encoding - The encoding name (e.g., 'gbk', 'gb18030', 'big5', 'euc-jp', 'iso-2022-jp', 'shift_jis', 'euc-kr')
|
|
33
|
+
* @param loose - If true, replaces unmapped bytes with replacement character instead of throwing (default: false)
|
|
34
|
+
* @returns A function that decodes bytes to string, with optional streaming support
|
|
35
|
+
*/
|
|
36
|
+
export function createMultibyteDecoder(
|
|
37
|
+
encoding: string,
|
|
38
|
+
loose?: boolean
|
|
39
|
+
): (arr: Uint8Array, stream?: boolean) => string;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Create an encoder for a supported legacy multi-byte `encoding`, given its lowercased name `encoding`.
|
|
43
|
+
*
|
|
44
|
+
* Returns a function `encode(string)` that encodes a string to bytes.
|
|
45
|
+
*
|
|
46
|
+
* In `'fatal'` mode (default), will throw on non well-formed strings or any codepoints which could
|
|
47
|
+
* not be encoded in the target encoding.
|
|
48
|
+
*
|
|
49
|
+
* @param encoding - The encoding name (e.g., 'gbk', 'gb18030', 'big5', 'euc-jp', 'iso-2022-jp', 'shift_jis', 'euc-kr')
|
|
50
|
+
* @param options - Encoding options
|
|
51
|
+
* @param options.mode - Encoding mode (default: 'fatal'). Currently, only 'fatal' mode is supported.
|
|
52
|
+
* @returns A function that encodes string to bytes
|
|
53
|
+
*/
|
|
54
|
+
export function createMultibyteEncoder(
|
|
55
|
+
encoding: string,
|
|
56
|
+
options?: { mode?: 'fatal' }
|
|
57
|
+
): (string: string) => Uint8ArrayBuffer;
|
package/multi-byte.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { assertUint8 } from './assert.js'
|
|
2
|
-
import { multibyteDecoder } from './fallback/multi-byte.js'
|
|
2
|
+
import { multibyteDecoder, multibyteEncoder } from './fallback/multi-byte.js'
|
|
3
3
|
|
|
4
4
|
export function createMultibyteDecoder(encoding, loose = false) {
|
|
5
5
|
const jsDecoder = multibyteDecoder(encoding, loose) // asserts
|
|
@@ -11,3 +11,9 @@ export function createMultibyteDecoder(encoding, loose = false) {
|
|
|
11
11
|
return jsDecoder(arr, stream)
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
+
|
|
15
|
+
export function createMultibyteEncoder(encoding, { mode = 'fatal' } = {}) {
|
|
16
|
+
// TODO: replacement, truncate (replacement will need varying length)
|
|
17
|
+
if (mode !== 'fatal') throw new Error('Unsupported mode')
|
|
18
|
+
return multibyteEncoder(encoding) // asserts
|
|
19
|
+
}
|
package/multi-byte.node.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { assertUint8 } from './assert.js'
|
|
2
2
|
import { isDeno, toBuf } from './fallback/_utils.js'
|
|
3
|
-
import { isAsciiSuperset, multibyteDecoder } from './fallback/multi-byte.js'
|
|
3
|
+
import { isAsciiSuperset, multibyteDecoder, multibyteEncoder } from './fallback/multi-byte.js'
|
|
4
4
|
import { isAscii } from 'node:buffer'
|
|
5
5
|
|
|
6
6
|
export function createMultibyteDecoder(encoding, loose = false) {
|
|
@@ -21,3 +21,9 @@ export function createMultibyteDecoder(encoding, loose = false) {
|
|
|
21
21
|
return jsDecoder(arr, stream)
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
+
|
|
25
|
+
export function createMultibyteEncoder(encoding, { mode = 'fatal' } = {}) {
|
|
26
|
+
// TODO: replacement, truncate (replacement will need varying length)
|
|
27
|
+
if (mode !== 'fatal') throw new Error('Unsupported mode')
|
|
28
|
+
return multibyteEncoder(encoding) // asserts
|
|
29
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/bytes",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "Various operations on Uint8Array data",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"encoding",
|
|
7
|
+
"uint8array",
|
|
8
|
+
"textdecoder",
|
|
9
|
+
"textencoder",
|
|
10
|
+
"utf8",
|
|
11
|
+
"utf16",
|
|
12
|
+
"hex",
|
|
13
|
+
"base64",
|
|
14
|
+
"base32",
|
|
15
|
+
"base58",
|
|
16
|
+
"base58check",
|
|
17
|
+
"bech32",
|
|
18
|
+
"bech32m",
|
|
19
|
+
"wif"
|
|
20
|
+
],
|
|
5
21
|
"scripts": {
|
|
6
22
|
"lint": "eslint .",
|
|
7
23
|
"test:javascriptcore": "npm run test:jsc --",
|
|
@@ -23,6 +39,7 @@
|
|
|
23
39
|
"test:firefox:playwright": "exodus-test --engine=firefox:playwright",
|
|
24
40
|
"test:servo:bundle": "exodus-test --engine=servo:bundle",
|
|
25
41
|
"test": "exodus-test",
|
|
42
|
+
"size": "esbuild --minify --bundle",
|
|
26
43
|
"jsvu": "jsvu",
|
|
27
44
|
"playwright": "exodus-test --playwright",
|
|
28
45
|
"benchmark": "exodus-test --concurrency=1 benchmarks/*.bench.js",
|
|
@@ -37,7 +54,7 @@
|
|
|
37
54
|
"bugs": {
|
|
38
55
|
"url": "https://github.com/ExodusOSS/bytes/issues"
|
|
39
56
|
},
|
|
40
|
-
"homepage": "https://github.com/ExodusOSS/bytes
|
|
57
|
+
"homepage": "https://github.com/ExodusOSS/bytes",
|
|
41
58
|
"engines": {
|
|
42
59
|
"node": "^20.19.0 || ^22.12.0 || >=24.0.0"
|
|
43
60
|
},
|
|
@@ -48,6 +65,7 @@
|
|
|
48
65
|
"/fallback/base58check.js",
|
|
49
66
|
"/fallback/base64.js",
|
|
50
67
|
"/fallback/encoding.js",
|
|
68
|
+
"/fallback/encoding.api.js",
|
|
51
69
|
"/fallback/encoding.labels.js",
|
|
52
70
|
"/fallback/encoding.util.js",
|
|
53
71
|
"/fallback/hex.js",
|
|
@@ -64,13 +82,22 @@
|
|
|
64
82
|
"/array.d.ts",
|
|
65
83
|
"/assert.js",
|
|
66
84
|
"/base32.js",
|
|
85
|
+
"/base32.d.ts",
|
|
67
86
|
"/base58.js",
|
|
87
|
+
"/base58.d.ts",
|
|
68
88
|
"/base58check.js",
|
|
89
|
+
"/base58check.d.ts",
|
|
69
90
|
"/base58check.node.js",
|
|
70
91
|
"/base64.js",
|
|
71
92
|
"/base64.d.ts",
|
|
72
93
|
"/bech32.js",
|
|
94
|
+
"/bech32.d.ts",
|
|
73
95
|
"/bigint.js",
|
|
96
|
+
"/bigint.d.ts",
|
|
97
|
+
"/encoding-browser.js",
|
|
98
|
+
"/encoding-browser.browser.js",
|
|
99
|
+
"/encoding-browser.native.js",
|
|
100
|
+
"/encoding-browser.d.ts",
|
|
74
101
|
"/encoding.js",
|
|
75
102
|
"/encoding.d.ts",
|
|
76
103
|
"/encoding-lite.js",
|
|
@@ -78,25 +105,45 @@
|
|
|
78
105
|
"/hex.js",
|
|
79
106
|
"/hex.d.ts",
|
|
80
107
|
"/hex.node.js",
|
|
108
|
+
"/index.js",
|
|
109
|
+
"/index.d.ts",
|
|
81
110
|
"/multi-byte.js",
|
|
111
|
+
"/multi-byte.d.ts",
|
|
82
112
|
"/multi-byte.node.js",
|
|
83
113
|
"/single-byte.js",
|
|
114
|
+
"/single-byte.d.ts",
|
|
84
115
|
"/single-byte.node.js",
|
|
85
116
|
"/utf16.js",
|
|
117
|
+
"/utf16.d.ts",
|
|
86
118
|
"/utf16.node.js",
|
|
87
119
|
"/utf8.js",
|
|
88
120
|
"/utf8.d.ts",
|
|
89
121
|
"/utf8.node.js",
|
|
90
|
-
"/wif.js"
|
|
122
|
+
"/wif.js",
|
|
123
|
+
"/wif.d.ts"
|
|
91
124
|
],
|
|
125
|
+
"main": "index.js",
|
|
126
|
+
"module": "index.js",
|
|
127
|
+
"types": "index.d.ts",
|
|
92
128
|
"exports": {
|
|
129
|
+
".": {
|
|
130
|
+
"types": "./index.d.ts",
|
|
131
|
+
"default": "./index.js"
|
|
132
|
+
},
|
|
93
133
|
"./array.js": {
|
|
94
134
|
"types": "./array.d.ts",
|
|
95
135
|
"default": "./array.js"
|
|
96
136
|
},
|
|
97
|
-
"./base32.js":
|
|
98
|
-
|
|
137
|
+
"./base32.js": {
|
|
138
|
+
"types": "./base32.d.ts",
|
|
139
|
+
"default": "./base32.js"
|
|
140
|
+
},
|
|
141
|
+
"./base58.js": {
|
|
142
|
+
"types": "./base58.d.ts",
|
|
143
|
+
"default": "./base58.js"
|
|
144
|
+
},
|
|
99
145
|
"./base58check.js": {
|
|
146
|
+
"types": "./base58check.d.ts",
|
|
100
147
|
"node": "./base58check.node.js",
|
|
101
148
|
"default": "./base58check.js"
|
|
102
149
|
},
|
|
@@ -104,18 +151,26 @@
|
|
|
104
151
|
"types": "./base64.d.ts",
|
|
105
152
|
"default": "./base64.js"
|
|
106
153
|
},
|
|
107
|
-
"./bech32.js":
|
|
108
|
-
|
|
154
|
+
"./bech32.js": {
|
|
155
|
+
"types": "./bech32.d.ts",
|
|
156
|
+
"default": "./bech32.js"
|
|
157
|
+
},
|
|
158
|
+
"./bigint.js": {
|
|
159
|
+
"types": "./bigint.d.ts",
|
|
160
|
+
"default": "./bigint.js"
|
|
161
|
+
},
|
|
109
162
|
"./hex.js": {
|
|
110
163
|
"types": "./hex.d.ts",
|
|
111
164
|
"node": "./hex.node.js",
|
|
112
165
|
"default": "./hex.js"
|
|
113
166
|
},
|
|
114
167
|
"./multi-byte.js": {
|
|
168
|
+
"types": "./multi-byte.d.ts",
|
|
115
169
|
"node": "./multi-byte.node.js",
|
|
116
170
|
"default": "./multi-byte.js"
|
|
117
171
|
},
|
|
118
172
|
"./single-byte.js": {
|
|
173
|
+
"types": "./single-byte.d.ts",
|
|
119
174
|
"node": "./single-byte.node.js",
|
|
120
175
|
"default": "./single-byte.js"
|
|
121
176
|
},
|
|
@@ -127,7 +182,15 @@
|
|
|
127
182
|
"types": "./encoding-lite.d.ts",
|
|
128
183
|
"default": "./encoding-lite.js"
|
|
129
184
|
},
|
|
185
|
+
"./encoding-browser.js": {
|
|
186
|
+
"types": "./encoding-browser.d.ts",
|
|
187
|
+
"node": "./encoding-browser.js",
|
|
188
|
+
"react-native": "./encoding-browser.native.js",
|
|
189
|
+
"browser": "./encoding-browser.browser.js",
|
|
190
|
+
"default": "./encoding-browser.js"
|
|
191
|
+
},
|
|
130
192
|
"./utf16.js": {
|
|
193
|
+
"types": "./utf16.d.ts",
|
|
131
194
|
"node": "./utf16.node.js",
|
|
132
195
|
"default": "./utf16.js"
|
|
133
196
|
},
|
|
@@ -136,13 +199,19 @@
|
|
|
136
199
|
"node": "./utf8.node.js",
|
|
137
200
|
"default": "./utf8.js"
|
|
138
201
|
},
|
|
139
|
-
"./wif.js":
|
|
202
|
+
"./wif.js": {
|
|
203
|
+
"types": "./wif.d.ts",
|
|
204
|
+
"default": "./wif.js"
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
"react-native": {
|
|
208
|
+
"./encoding-browser.js": "./encoding-browser.native.js"
|
|
140
209
|
},
|
|
141
210
|
"peerDependencies": {
|
|
142
|
-
"@
|
|
211
|
+
"@noble/hashes": "^1.8.0 || ^2.0.0"
|
|
143
212
|
},
|
|
144
213
|
"peerDependenciesMeta": {
|
|
145
|
-
"@
|
|
214
|
+
"@noble/hashes": {
|
|
146
215
|
"optional": true
|
|
147
216
|
}
|
|
148
217
|
},
|
|
@@ -153,6 +222,7 @@
|
|
|
153
222
|
"@exodus/prettier": "^1.0.0",
|
|
154
223
|
"@exodus/test": "^1.0.0-rc.109",
|
|
155
224
|
"@noble/hashes": "^2.0.1",
|
|
225
|
+
"@oslojs/encoding": "^1.1.0",
|
|
156
226
|
"@petamoriken/float16": "^3.9.3",
|
|
157
227
|
"@scure/base": "^1.2.6",
|
|
158
228
|
"@stablelib/base64": "^2.0.1",
|
|
@@ -167,9 +237,11 @@
|
|
|
167
237
|
"bs58check": "^4.0.0",
|
|
168
238
|
"bstring": "^0.3.9",
|
|
169
239
|
"buffer": "^6.0.3",
|
|
240
|
+
"c8": "^10.1.3",
|
|
170
241
|
"decode-utf8": "^1.0.1",
|
|
171
242
|
"electron": "36.5.0",
|
|
172
243
|
"encode-utf8": "^2.0.0",
|
|
244
|
+
"esbuild": "^0.27.2",
|
|
173
245
|
"eslint": "^8.44.0",
|
|
174
246
|
"fast-base64-decode": "^2.0.0",
|
|
175
247
|
"fast-base64-encode": "^1.0.0",
|
|
@@ -177,6 +249,7 @@
|
|
|
177
249
|
"hi-base32": "^0.5.1",
|
|
178
250
|
"iconv-lite": "^0.7.0",
|
|
179
251
|
"jsvu": "^3.0.3",
|
|
252
|
+
"punycode": "^2.3.1",
|
|
180
253
|
"text-encoding": "^0.7.0",
|
|
181
254
|
"typescript": "^5.9.3",
|
|
182
255
|
"uint8array-tools": "^0.0.9",
|