@exodus/bytes 1.12.0 → 1.13.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 +39 -16
- package/base58.js +3 -3
- package/base64.js +7 -6
- package/bech32.js +3 -3
- package/encoding-browser.browser.js +43 -17
- package/fallback/_utils.js +7 -122
- package/fallback/base32.js +3 -3
- package/fallback/base58check.js +3 -3
- package/fallback/base64.js +2 -3
- package/fallback/encoding.api.js +0 -43
- package/fallback/encoding.js +41 -2
- package/fallback/encoding.labels.js +20 -16
- package/fallback/hex.js +3 -4
- package/fallback/latin1.js +5 -6
- package/fallback/percent.js +1 -1
- package/fallback/platform.browser.js +31 -0
- package/fallback/platform.js +2 -0
- package/fallback/platform.native.js +97 -0
- package/fallback/single-byte.encodings.js +40 -49
- package/fallback/single-byte.js +4 -4
- package/fallback/utf16.js +69 -2
- package/fallback/utf8.auto.browser.js +2 -0
- package/fallback/utf8.auto.js +1 -0
- package/fallback/utf8.auto.native.js +1 -0
- package/fallback/utf8.js +25 -3
- package/hex.js +6 -8
- package/hex.node.js +2 -3
- package/multi-byte.js +2 -2
- package/multi-byte.node.js +3 -3
- package/package.json +22 -4
- package/single-byte.js +5 -5
- package/single-byte.node.js +4 -4
- package/utf16.browser.js +8 -0
- package/utf16.js +1 -90
- package/utf16.native.js +22 -0
- package/utf16.node.js +5 -20
- package/utf8.js +9 -28
- package/utf8.node.js +3 -4
- package/whatwg.js +6 -2
package/fallback/hex.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { nativeDecoder, nativeEncoder, decode2string
|
|
1
|
+
import { E_STRING } from './_utils.js'
|
|
2
|
+
import { nativeDecoder, nativeEncoder, decode2string } from './platform.js'
|
|
3
3
|
import { encodeAscii, decodeAscii } from './latin1.js'
|
|
4
4
|
|
|
5
5
|
let hexArray // array of 256 bytes converted to two-char hex strings
|
|
@@ -11,9 +11,8 @@ const allowed = '0123456789ABCDEFabcdef'
|
|
|
11
11
|
|
|
12
12
|
export const E_HEX = 'Input is not a hex string'
|
|
13
13
|
|
|
14
|
+
// Expects a checked Uint8Array
|
|
14
15
|
export function toHex(arr) {
|
|
15
|
-
assertUint8(arr)
|
|
16
|
-
|
|
17
16
|
if (!hexArray) hexArray = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'))
|
|
18
17
|
const length = arr.length // this helps Hermes
|
|
19
18
|
|
package/fallback/latin1.js
CHANGED
|
@@ -6,18 +6,17 @@ import {
|
|
|
6
6
|
isHermes,
|
|
7
7
|
isDeno,
|
|
8
8
|
isLE,
|
|
9
|
-
|
|
10
|
-
} from './_utils.js'
|
|
9
|
+
} from './platform.js'
|
|
11
10
|
|
|
12
|
-
const
|
|
13
|
-
const
|
|
11
|
+
const atob = /* @__PURE__ */ (() => globalThis.atob)()
|
|
12
|
+
const web64 = /* @__PURE__ */ (() => Uint8Array.prototype.toBase64)()
|
|
14
13
|
|
|
15
14
|
// See http://stackoverflow.com/a/22747272/680742, which says that lowest limit is in Chrome, with 0xffff args
|
|
16
15
|
// On Hermes, actual max is 0x20_000 minus current stack depth, 1/16 of that should be safe
|
|
17
16
|
const maxFunctionArgs = 0x20_00
|
|
18
17
|
|
|
19
18
|
// toBase64+atob path is faster on everything where fromBase64 is fast
|
|
20
|
-
const useLatin1atob = web64 && atob
|
|
19
|
+
const useLatin1atob = web64 && atob
|
|
21
20
|
|
|
22
21
|
export function asciiPrefix(arr) {
|
|
23
22
|
let p = 0 // verified ascii bytes
|
|
@@ -147,7 +146,7 @@ export function encodeAsciiPrefix(x, s) {
|
|
|
147
146
|
export const encodeLatin1 = (str) => encodeCharcodes(str, new Uint8Array(str.length))
|
|
148
147
|
|
|
149
148
|
// Expects nativeEncoder to be present
|
|
150
|
-
const useEncodeInto = isHermes && nativeEncoder?.encodeInto
|
|
149
|
+
const useEncodeInto = /* @__PURE__ */ (() => isHermes && nativeEncoder?.encodeInto)()
|
|
151
150
|
export const encodeAscii = useEncodeInto
|
|
152
151
|
? (str, ERR) => {
|
|
153
152
|
// Much faster in Hermes
|
package/fallback/percent.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { decodeAscii, encodeLatin1 } from './latin1.js'
|
|
2
|
-
import { decode2string } from './
|
|
2
|
+
import { decode2string } from './platform.js'
|
|
3
3
|
|
|
4
4
|
const ERR = 'percentEncodeSet must be a string of unique increasing codepoints in range 0x20 - 0x7e'
|
|
5
5
|
const percentMap = new Map()
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { decodePartAddition as decodePart } from './platform.native.js'
|
|
2
|
+
|
|
3
|
+
export const nativeBuffer = null
|
|
4
|
+
export const isHermes = false
|
|
5
|
+
export const isDeno = false
|
|
6
|
+
export const nativeEncoder = /* @__PURE__ */ (() => new TextEncoder())()
|
|
7
|
+
export const nativeDecoder = /* @__PURE__ */ (() => new TextDecoder('utf-8', { ignoreBOM: true }))()
|
|
8
|
+
export const nativeDecoderLatin1 = /* @__PURE__ */ (() =>
|
|
9
|
+
new TextDecoder('latin1', { ignoreBOM: true }))()
|
|
10
|
+
|
|
11
|
+
export { isLE } from './platform.native.js'
|
|
12
|
+
|
|
13
|
+
export function decode2string(arr, start, end, m) {
|
|
14
|
+
if (end - start > 30_000) {
|
|
15
|
+
// Limit concatenation to avoid excessive GC
|
|
16
|
+
// Thresholds checked on Hermes for toHex
|
|
17
|
+
const concat = []
|
|
18
|
+
for (let i = start; i < end; ) {
|
|
19
|
+
const step = i + 500
|
|
20
|
+
const iNext = step > end ? end : step
|
|
21
|
+
concat.push(decodePart(arr, i, iNext, m))
|
|
22
|
+
i = iNext
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const res = concat.join('')
|
|
26
|
+
concat.length = 0
|
|
27
|
+
return res
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return decodePart(arr, start, end, m)
|
|
31
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
const { Buffer } = globalThis
|
|
2
|
+
const haveNativeBuffer = Buffer && !Buffer.TYPED_ARRAY_SUPPORT
|
|
3
|
+
export const nativeBuffer = haveNativeBuffer ? Buffer : null
|
|
4
|
+
export const isHermes = /* @__PURE__ */ (() => !!globalThis.HermesInternal)()
|
|
5
|
+
export const isDeno = /* @__PURE__ */ (() => !!globalThis.Deno)()
|
|
6
|
+
export const isLE = /* @__PURE__ */ (() => new Uint8Array(Uint16Array.of(258).buffer)[0] === 2)()
|
|
7
|
+
|
|
8
|
+
// We consider Node.js TextDecoder/TextEncoder native
|
|
9
|
+
// Still needed in platform.native.js as this is re-exported to platform.js
|
|
10
|
+
let isNative = (x) => x && (haveNativeBuffer || `${x}`.includes('[native code]'))
|
|
11
|
+
if (!haveNativeBuffer && isNative(() => {})) isNative = () => false // e.g. XS, we don't want false positives
|
|
12
|
+
|
|
13
|
+
export const nativeEncoder = /* @__PURE__ */ (() =>
|
|
14
|
+
isNative(globalThis.TextEncoder) ? new TextEncoder() : null)()
|
|
15
|
+
export const nativeDecoder = /* @__PURE__ */ (() =>
|
|
16
|
+
isNative(globalThis.TextDecoder) ? new TextDecoder('utf-8', { ignoreBOM: true }) : null)()
|
|
17
|
+
|
|
18
|
+
// Actually windows-1252, compatible with ascii and latin1 decoding
|
|
19
|
+
// Beware that on non-latin1, i.e. on windows-1252, this is broken in ~all Node.js versions released
|
|
20
|
+
// in 2025 due to a regression, so we call it Latin1 as it's usable only for that
|
|
21
|
+
export const nativeDecoderLatin1 = /* @__PURE__ */ (() => {
|
|
22
|
+
// Not all barebone engines with TextDecoder support something except utf-8, detect
|
|
23
|
+
if (nativeDecoder) {
|
|
24
|
+
try {
|
|
25
|
+
return new TextDecoder('latin1', { ignoreBOM: true })
|
|
26
|
+
} catch {}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return null
|
|
30
|
+
})()
|
|
31
|
+
|
|
32
|
+
export function decodePartAddition(a, start, end, m) {
|
|
33
|
+
let o = ''
|
|
34
|
+
let i = start
|
|
35
|
+
for (const last3 = end - 3; i < last3; i += 4) {
|
|
36
|
+
const x0 = a[i]
|
|
37
|
+
const x1 = a[i + 1]
|
|
38
|
+
const x2 = a[i + 2]
|
|
39
|
+
const x3 = a[i + 3]
|
|
40
|
+
o += m[x0]
|
|
41
|
+
o += m[x1]
|
|
42
|
+
o += m[x2]
|
|
43
|
+
o += m[x3]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
while (i < end) o += m[a[i++]]
|
|
47
|
+
return o
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Decoding with templates is faster on Hermes
|
|
51
|
+
export function decodePartTemplates(a, start, end, m) {
|
|
52
|
+
let o = ''
|
|
53
|
+
let i = start
|
|
54
|
+
for (const last15 = end - 15; i < last15; i += 16) {
|
|
55
|
+
const x0 = a[i]
|
|
56
|
+
const x1 = a[i + 1]
|
|
57
|
+
const x2 = a[i + 2]
|
|
58
|
+
const x3 = a[i + 3]
|
|
59
|
+
const x4 = a[i + 4]
|
|
60
|
+
const x5 = a[i + 5]
|
|
61
|
+
const x6 = a[i + 6]
|
|
62
|
+
const x7 = a[i + 7]
|
|
63
|
+
const x8 = a[i + 8]
|
|
64
|
+
const x9 = a[i + 9]
|
|
65
|
+
const x10 = a[i + 10]
|
|
66
|
+
const x11 = a[i + 11]
|
|
67
|
+
const x12 = a[i + 12]
|
|
68
|
+
const x13 = a[i + 13]
|
|
69
|
+
const x14 = a[i + 14]
|
|
70
|
+
const x15 = a[i + 15]
|
|
71
|
+
o += `${m[x0]}${m[x1]}${m[x2]}${m[x3]}${m[x4]}${m[x5]}${m[x6]}${m[x7]}${m[x8]}${m[x9]}${m[x10]}${m[x11]}${m[x12]}${m[x13]}${m[x14]}${m[x15]}`
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
while (i < end) o += m[a[i++]]
|
|
75
|
+
return o
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const decodePart = isHermes ? decodePartTemplates : decodePartAddition
|
|
79
|
+
export function decode2string(arr, start, end, m) {
|
|
80
|
+
if (end - start > 30_000) {
|
|
81
|
+
// Limit concatenation to avoid excessive GC
|
|
82
|
+
// Thresholds checked on Hermes for toHex
|
|
83
|
+
const concat = []
|
|
84
|
+
for (let i = start; i < end; ) {
|
|
85
|
+
const step = i + 500
|
|
86
|
+
const iNext = step > end ? end : step
|
|
87
|
+
concat.push(decodePart(arr, i, iNext, m))
|
|
88
|
+
i = iNext
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const res = concat.join('')
|
|
92
|
+
concat.length = 0
|
|
93
|
+
return res
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return decodePart(arr, start, end, m)
|
|
97
|
+
}
|
|
@@ -1,57 +1,48 @@
|
|
|
1
1
|
// See tests/encoding/fixtures/single-byte/dump.js for generator
|
|
2
2
|
|
|
3
3
|
const r = 0xff_fd
|
|
4
|
-
const e = (x) => new Array(x).fill(1)
|
|
5
|
-
const h = (x) => new Array(x).fill(r)
|
|
6
4
|
|
|
7
5
|
/* eslint-disable unicorn/numeric-separators-style, @exodus/export-default/named */
|
|
8
6
|
|
|
9
7
|
// Common ranges
|
|
10
8
|
|
|
11
9
|
// prettier-ignore
|
|
12
|
-
const i2 = [
|
|
13
|
-
const i4a = [-75, -63, e(5), 104, -34, -67, 79, -77, 75, -73, 1]
|
|
14
|
-
const i4b = [34, -32, e(5), 73, -34, -36, 48, -46, 44, -42, 1]
|
|
15
|
-
const i7 = [721, 1, 1, -719, 721, -719, 721, e(19), r, 2, e(43), r]
|
|
16
|
-
const i8 = [e(26), r, r, 6692, 1, r]
|
|
17
|
-
const i9 = [79, -77, e(11), 84, 46, -127, e(16), 48, -46, e(11), 53, 46]
|
|
18
|
-
const iB = [3425, e(57), h(4), 5, e(28), h(4)]
|
|
19
|
-
const p2 = [-99, 12, 20, -12, 17, 37, -29, 2]
|
|
20
|
-
const p1 = [8237, -8235, 8089, -7816, 7820, 8, -6, 1]
|
|
21
|
-
const w0 = [8237, -8235, 8089, -8087, 8091, 8, -6, 1, -8089, 8104]
|
|
22
|
-
const w8 = [8072, 1, 3, 1, 5, -15, 1]
|
|
23
|
-
const w1 = [w8, -7480, 7750, -8129, 7897, -7911, -182]
|
|
24
|
-
const w3 = [w8, -8060, 8330, -8328, 8096, -8094]
|
|
25
|
-
const m0 = [8558, -8328, 8374, -66, -8539, 16, 8043, -8070]
|
|
10
|
+
const i2 = [189,148,0,0,63,0,116,64,0,68,0,78,0,78,0,0,63,64,114,117,0,0,123,0,0,128,149,0,149,0,0,132,0,117,0,0,32,0,85,33,0,37,0,47,0,47,0,0,32,33,83,86,0,0,92,0,0,97,118,0,118,0,0,101,474]
|
|
26
11
|
// prettier-ignore
|
|
27
|
-
const
|
|
12
|
+
const iB = [[58,3424],[4,r],[29,3424],[4,r]]
|
|
13
|
+
const i9 = [[47], 78, [12], 83, 128, [17], 47, [12], 52, 97]
|
|
14
|
+
const w1 = [8236, 0, 8088, 0, 8090, 8097, 8090, 8090, 0, 8103]
|
|
15
|
+
const w2 = [8236, 0, 8088, 271, 8090, 8097, 8090, 8090, 574, 8103]
|
|
28
16
|
// prettier-ignore
|
|
29
|
-
const
|
|
17
|
+
const w7 = [64,0,157,[4],39,68,109,62,67,0,0,82,75,68,0,175,75,86,105,92,108,144,114,115,0,120,[3],154,104,128,143,0,158,159,0,37,78,31,36,0,0,51,44,37,0,144,44,55,74,61,77,113,83,84,0,89,[3],123,73,97,112,0,127,128]
|
|
18
|
+
const w8 = [8071, 8071, 8073, 8073, 8077, 8061, 8061]
|
|
30
19
|
// prettier-ignore
|
|
31
|
-
const k8b = [-
|
|
20
|
+
const k8b = [-22,910,879,879,899,880,880,894,876,893,[8,879],894,[4,878],864,859,884,882,861,877,881,876,873,875,846,815,815,835,816,816,830,812,829,[8,815],830,[4,814],800,795,820,818,797,813,817,812,809,811]
|
|
21
|
+
// prettier-ignore
|
|
22
|
+
const k8a = [9344,9345,9354,9357,9360,9363,9366,9373,9380,9387,9394,9461,9464,9467,9470,[4,9473],8845,9484,8580,8580,8625,8652,8652,6,8838,20,21,25,88,[3,9392],942]
|
|
32
23
|
|
|
33
24
|
// prettier-ignore
|
|
34
25
|
const maps = {
|
|
35
|
-
ibm866: [
|
|
36
|
-
'koi8-
|
|
37
|
-
'koi8-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
'windows-874': [
|
|
26
|
+
ibm866: [[48,912],[3,9441],...[29,62,122,122,109,107,120,101,106,111,109,107,31,34,65,56,39,10,69,102,102,96,89,109,105,98,81,108,102,102,97,97,84,82,75,75,98,96,13,0,123,118,125,128,111].map(x=>x+9266),[16,864],785,864,786,865,787,866,792,871,-72,8480,-67,8479,8218,-89,9378,-95],
|
|
27
|
+
'koi8-u': [...k8a,944,9391,944,944,[5,9391],996,944,[4,9391],846,848,9390,848,848,[5,9390],979,848,...k8b],
|
|
28
|
+
'koi8-r': [...k8a,[15,9391],846,[11,9390],...k8b],
|
|
29
|
+
macintosh: [68,68,69,70,77,81,86,90,88,89,90,88,89,90,91,89,90,90,91,89,90,90,91,92,90,91,92,90,94,92,93,93,8064,15,0,0,3,8061,16,56,6,0,8312,9,-4,8627,24,41,8558,0,8626,8626,-15,0,8524,8538,8535,775,8561,-17,-2,748,40,57,-1,-32,-22,8535,206,8579,8512,-28,-13,8029,-42,-11,-9,8,132,132,8003,8003,8010,8010,8004,8004,33,9459,39,159,8042,8145,8029,8029,64035,64035,8001,-42,7992,7995,8012,-35,-28,-38,-29,-33,[3,-29],-33,-27,-27,63503,-31,-24,-24,-27,60,464,485,-73,[3,479],-68,480,477,456],
|
|
30
|
+
'x-mac-cyrillic': [[32,912],8064,15,1006,0,3,8061,16,863,6,0,8312,855,934,8627,853,932,8558,0,8626,8626,930,0,987,849,844,923,845,924,845,924,844,923,920,836,-22,8535,206,8579,8512,-28,-13,8029,-42,832,911,831,910,902,8003,8003,8010,8010,8004,8004,33,8007,822,901,821,900,8250,804,883,880,[31,848],8109],
|
|
31
|
+
'windows-874': [8236,[4],8097,[11],...w8,[9],...iB],
|
|
41
32
|
}
|
|
42
33
|
|
|
43
34
|
// windows-1250 - windows-1258
|
|
44
35
|
// prettier-ignore
|
|
45
36
|
;[
|
|
46
|
-
[
|
|
47
|
-
[
|
|
48
|
-
[
|
|
49
|
-
[
|
|
50
|
-
[
|
|
51
|
-
[
|
|
52
|
-
[
|
|
53
|
-
[
|
|
54
|
-
[
|
|
37
|
+
[...w1,214,8110,206,215,239,234,0,...w8,0,8329,199,8095,191,200,224,219,0,550,566,158,0,95,[4],180,[4],204,0,0,553,143,[5],76,165,0,129,544,128,...i2],
|
|
38
|
+
[898,898,8088,976,8090,8097,8090,8090,8228,8103,895,8110,894,895,893,896,962,...w8,0,8329,959,8095,958,959,957,960,0,877,956,869,0,1003,0,0,857,0,858,[4],856,0,0,852,931,989,[3],921,8285,922,0,924,840,919,920,[64,848]],
|
|
39
|
+
[...w2,214,8110,198,0,239,0,0,...w8,580,8329,199,8095,183,0,224,217],
|
|
40
|
+
[8236,0,8088,271,8090,8097,8090,8090,0,8103,0,8110,[5],...w8,0,8329,0,8095,[5],740,740,[7],r,[4],8038,[4],720,[3],[3,720],0,720,0,[20,720],r,[44,720],r],
|
|
41
|
+
[...w2,214,8110,198,[4],...w8,580,8329,199,8095,183,0,0,217,0,...i9],
|
|
42
|
+
[...w2,0,8110,[5],...w8,580,8329,0,8095,[8],8198,[5],45,[15],61,[5],[20,1264],[5,1308],[7,r],[27,1264],r,r,7953,7953,r],
|
|
43
|
+
[8236,1533,8088,271,8090,8097,8090,8090,574,8103,1519,8110,198,1529,1546,1529,1567,...w8,1553,8329,1527,8095,183,8047,8047,1563,0,1387,[8],1556,[15],1377,[4],1376,1537,[22,1376],0,[4,1375],[4,1380],0,1379,0,[4,1378],[5],1373,1373,0,0,[4,1371],0,1370,1370,0,1369,0,1368,0,0,7953,7953,1491],
|
|
44
|
+
[...w1,0,8110,0,27,569,41,0,...w8,0,8329,0,8095,0,18,573,0,0,r,[3],r,0,0,48,0,172,[4],23,[8],...w7,474],
|
|
45
|
+
[...w2,0,8110,198,[4],...w8,580,8329,0,8095,183,0,0,217,[35],63,[8],564,[3],64,0,567,0,0,203,[7],210,549,[4],32,[8],533,[3],33,0,561,0,0,172,[7],179,8109],
|
|
55
46
|
].forEach((m, i) => {
|
|
56
47
|
maps[`windows-${i + 1250}`] = m
|
|
57
48
|
});
|
|
@@ -60,23 +51,23 @@ const maps = {
|
|
|
60
51
|
// prettier-ignore
|
|
61
52
|
;[
|
|
62
53
|
[], // Actual Latin1 / Unicode subset, non-WHATWG, which maps iso-8859-1 to windows-1252
|
|
63
|
-
[
|
|
64
|
-
[
|
|
65
|
-
[
|
|
66
|
-
[
|
|
67
|
-
[
|
|
68
|
-
[
|
|
69
|
-
[r,
|
|
70
|
-
|
|
71
|
-
[
|
|
54
|
+
[99,566,158,0,152,180,0,0,183,180,185,205,0,207,204,0,84,553,143,0,137,165,528,0,168,165,170,190,544,192,...i2],
|
|
55
|
+
[133,566,0,0,r,126,0,0,135,180,115,136,0,r,204,0,118,[4],111,0,0,120,165,100,121,0,r,189,[3],r,0,69,66,[9],r,[4],75,0,0,68,[4],143,126,[4],r,0,38,35,[9],r,[4],44,0,0,37,[4],112,95,474],
|
|
56
|
+
[99,150,179,0,131,149,0,0,183,104,119,186,0,207,0,0,84,553,164,0,116,134,528,0,168,89,104,171,141,192,140,64,[6],103,68,0,78,0,74,0,0,91,64,116,122,99,[5],153,[3],139,140,0,33,[6],72,37,0,47,0,43,0,0,60,33,85,91,68,[5],122,[3],108,109,474],
|
|
57
|
+
[[12,864],0,[66,864],8230,[12,864],-86,864,864],
|
|
58
|
+
[[3,r],0,[7,r],1376,0,[13,r],1376,[3,r],1376,r,[26,1376],[5,r],[19,1376],[13,r]],
|
|
59
|
+
[8055,8055,0,8200,8202,[4],720,[3],r,8038,[4],[3,720],0,[3,720],0,720,0,[20,720],r,[44,720],r],
|
|
60
|
+
[r,[8],45,[15],61,[4],[32,r],7992,[27,1264],r,r,7953,7953,r],
|
|
61
|
+
i9, // non-WHATWG, which maps iso-8859-9 to windows-1254
|
|
62
|
+
[99,112,127,134,131,144,0,147,103,182,187,209,0,188,155,0,84,97,112,119,116,129,0,132,88,167,172,194,8024,173,140,64,[6],103,68,0,78,0,74,[4],116,122,[4],145,0,153,[6],33,[6],72,37,0,47,0,43,[4],85,91,[4],114,0,122,[5],57],
|
|
72
63
|
iB, // non-WHATWG, which maps iso-8859-11 to windows-874
|
|
73
64
|
null, // no 12
|
|
74
|
-
[
|
|
75
|
-
[
|
|
76
|
-
[
|
|
77
|
-
[
|
|
65
|
+
[8060,[3],8057,0,0,48,0,172,[4],23,[4],8040,[3],...w7,7962],
|
|
66
|
+
[7521,7521,0,102,102,7524,0,7640,0,7640,7520,7750,0,0,201,7534,7534,110,110,7564,7564,0,7583,7625,7582,7625,7589,7735,7623,7623,7586,[16],164,[6],7571,[6],152,[17],133,[6],7540,[6],121],
|
|
67
|
+
[[3],8200,0,186,0,185,[11],201,[3],198,[3],150,150,186],
|
|
68
|
+
[99,99,158,8200,8057,186,0,185,0,366,0,205,0,204,204,0,0,90,143,201,8040,0,0,198,84,351,0,150,150,186,189,[3],63,0,65,[10],64,114,[3],123,0,131,152,[4],59,316,[4],32,0,34,[10],33,83,[3],92,0,100,121,[4],28,285],
|
|
78
69
|
].forEach((m, i) => {
|
|
79
|
-
if (m) maps[`iso-8859-${i + 1}`] = [
|
|
80
|
-
})
|
|
70
|
+
if (m) maps[`iso-8859-${i + 1}`] = [[33], ...m]
|
|
71
|
+
});
|
|
81
72
|
|
|
82
73
|
export default maps
|
package/fallback/single-byte.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { asciiPrefix, decodeAscii, decodeLatin1 } from './latin1.js'
|
|
2
2
|
import encodings from './single-byte.encodings.js'
|
|
3
|
-
import { decode2string, nativeDecoder } from './
|
|
3
|
+
import { decode2string, nativeDecoder } from './platform.js'
|
|
4
4
|
|
|
5
5
|
export const E_STRICT = 'Input is not well-formed for this encoding'
|
|
6
6
|
const xUserDefined = 'x-user-defined'
|
|
@@ -17,9 +17,9 @@ 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'
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
return
|
|
20
|
+
const enc = encodings[encoding]
|
|
21
|
+
const deltas = enc.flatMap((x) => (Array.isArray(x) ? new Array(x[0]).fill(x[1] ?? 0) : x))
|
|
22
|
+
return deltas.map((x, i) => (x === r ? x : x + 128 + i))
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
const mappers = new Map()
|
package/fallback/utf16.js
CHANGED
|
@@ -1,14 +1,81 @@
|
|
|
1
1
|
import { decodeUCS2, encodeCharcodes } from './latin1.js'
|
|
2
|
-
import {
|
|
2
|
+
import { assertU8, E_STRING, E_STRICT_UNICODE } from './_utils.js'
|
|
3
|
+
import { nativeDecoder, isLE } from './platform.js'
|
|
3
4
|
|
|
4
5
|
export const E_STRICT = 'Input is not well-formed utf16'
|
|
5
|
-
|
|
6
|
+
const isWellFormedStr = String.prototype.isWellFormed
|
|
7
|
+
const toWellFormedStr = /* @__PURE__ */ (() => String.prototype.toWellFormed)()
|
|
6
8
|
|
|
7
9
|
const replacementCodepoint = 0xff_fd
|
|
8
10
|
const replacementCodepointSwapped = 0xfd_ff
|
|
9
11
|
|
|
10
12
|
const to16 = (a) => new Uint16Array(a.buffer, a.byteOffset, a.byteLength / 2) // Requires checked length and alignment!
|
|
11
13
|
|
|
14
|
+
export function encodeApi(str, loose, format) {
|
|
15
|
+
if (typeof str !== 'string') throw new TypeError(E_STRING)
|
|
16
|
+
if (format !== 'uint16' && format !== 'uint8-le' && format !== 'uint8-be') {
|
|
17
|
+
throw new TypeError('Unknown format')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// On v8 and SpiderMonkey, check via isWellFormed is faster than js
|
|
21
|
+
// On JSC, check during loop is faster than isWellFormed
|
|
22
|
+
// If isWellFormed is available, we skip check during decoding and recheck after
|
|
23
|
+
// If isWellFormed is unavailable, we check in js during decoding
|
|
24
|
+
if (!loose && isWellFormedStr && !isWellFormedStr.call(str)) throw new TypeError(E_STRICT_UNICODE)
|
|
25
|
+
const shouldSwap = (isLE && format === 'uint8-be') || (!isLE && format === 'uint8-le')
|
|
26
|
+
const u16 = encode(str, loose, !loose && isWellFormedStr, shouldSwap)
|
|
27
|
+
|
|
28
|
+
// Bytes are already swapped and format is already checked, we need to just cast the view
|
|
29
|
+
return format === 'uint16' ? u16 : new Uint8Array(u16.buffer, u16.byteOffset, u16.byteLength)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const fatalLE = nativeDecoder ? new TextDecoder('utf-16le', { ignoreBOM: true, fatal: true }) : null
|
|
33
|
+
const looseLE = nativeDecoder ? new TextDecoder('utf-16le', { ignoreBOM: true }) : null
|
|
34
|
+
const fatalBE = nativeDecoder ? new TextDecoder('utf-16be', { ignoreBOM: true, fatal: true }) : null
|
|
35
|
+
const looseBE = nativeDecoder ? new TextDecoder('utf-16be', { ignoreBOM: true }) : null
|
|
36
|
+
|
|
37
|
+
export function decodeApiDecoders(input, loose, format) {
|
|
38
|
+
if (format === 'uint16') {
|
|
39
|
+
if (!(input instanceof Uint16Array)) throw new TypeError('Expected an Uint16Array')
|
|
40
|
+
} else if (format === 'uint8-le' || format === 'uint8-be') {
|
|
41
|
+
assertU8(input)
|
|
42
|
+
if (input.byteLength % 2 !== 0) throw new TypeError('Expected even number of bytes')
|
|
43
|
+
} else {
|
|
44
|
+
throw new TypeError('Unknown format')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const le = format === 'uint8-le' || (format === 'uint16' && isLE)
|
|
48
|
+
return (le ? (loose ? looseLE : fatalLE) : loose ? looseBE : fatalBE).decode(input)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function decodeApiJS(input, loose, format) {
|
|
52
|
+
let u16
|
|
53
|
+
switch (format) {
|
|
54
|
+
case 'uint16':
|
|
55
|
+
if (!(input instanceof Uint16Array)) throw new TypeError('Expected an Uint16Array')
|
|
56
|
+
u16 = input
|
|
57
|
+
break
|
|
58
|
+
case 'uint8-le':
|
|
59
|
+
assertU8(input)
|
|
60
|
+
if (input.byteLength % 2 !== 0) throw new TypeError('Expected even number of bytes')
|
|
61
|
+
u16 = to16input(input, true)
|
|
62
|
+
break
|
|
63
|
+
case 'uint8-be':
|
|
64
|
+
assertU8(input)
|
|
65
|
+
if (input.byteLength % 2 !== 0) throw new TypeError('Expected even number of bytes')
|
|
66
|
+
u16 = to16input(input, false)
|
|
67
|
+
break
|
|
68
|
+
default:
|
|
69
|
+
throw new TypeError('Unknown format')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const str = decode(u16, loose, (!loose && isWellFormedStr) || (loose && toWellFormedStr))
|
|
73
|
+
if (!loose && isWellFormedStr && !isWellFormedStr.call(str)) throw new TypeError(E_STRICT)
|
|
74
|
+
if (loose && toWellFormedStr) return toWellFormedStr.call(str)
|
|
75
|
+
|
|
76
|
+
return str
|
|
77
|
+
}
|
|
78
|
+
|
|
12
79
|
export function to16input(u8, le) {
|
|
13
80
|
// Assume even number of bytes
|
|
14
81
|
if (le === isLE) return to16(u8.byteOffset % 2 === 0 ? u8 : Uint8Array.from(u8))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { decodeFast, encode } from './utf8.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { decodeFast, encode } from './utf8.js'
|
package/fallback/utf8.js
CHANGED
|
@@ -1,9 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { E_STRICT_UNICODE } from './_utils.js'
|
|
2
|
+
import { isHermes } from './platform.js'
|
|
3
|
+
import { asciiPrefix, decodeLatin1, encodeAsciiPrefix } from './latin1.js'
|
|
2
4
|
|
|
3
5
|
export const E_STRICT = 'Input is not well-formed utf8'
|
|
4
|
-
export const E_STRICT_UNICODE = 'Input is not well-formed Unicode'
|
|
5
6
|
|
|
6
7
|
const replacementPoint = 0xff_fd
|
|
8
|
+
const shouldUseEscapePath = isHermes // faster only on Hermes, js path beats it on normal engines
|
|
9
|
+
const { decodeURIComponent, escape } = globalThis
|
|
10
|
+
|
|
11
|
+
export function decodeFast(arr, loose) {
|
|
12
|
+
// Fast path for ASCII prefix, this is faster than all alternatives below
|
|
13
|
+
const prefix = decodeLatin1(arr, 0, asciiPrefix(arr)) // No native decoder to use, so decodeAscii is useless here
|
|
14
|
+
if (prefix.length === arr.length) return prefix
|
|
15
|
+
|
|
16
|
+
// This codepath gives a ~3x perf boost on Hermes
|
|
17
|
+
if (shouldUseEscapePath && escape && decodeURIComponent) {
|
|
18
|
+
const o = escape(decodeLatin1(arr, prefix.length, arr.length))
|
|
19
|
+
try {
|
|
20
|
+
return prefix + decodeURIComponent(o) // Latin1 to utf8
|
|
21
|
+
} catch {
|
|
22
|
+
if (!loose) throw new TypeError(E_STRICT)
|
|
23
|
+
// Ok, we have to use manual implementation for loose decoder
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return prefix + decode(arr, loose, prefix.length)
|
|
28
|
+
}
|
|
7
29
|
|
|
8
30
|
// https://encoding.spec.whatwg.org/#utf-8-decoder
|
|
9
31
|
// We are most likely in loose mode, for non-loose escape & decodeURIComponent solved everything
|
|
@@ -27,7 +49,7 @@ export function decode(arr, loose, start = 0) {
|
|
|
27
49
|
const byte = arr[i]
|
|
28
50
|
if (byte < 0x80) {
|
|
29
51
|
tmp[ti++] = byte
|
|
30
|
-
// ascii fast path is in
|
|
52
|
+
// ascii fast path is in decodeFast(), this is called only on non-ascii input
|
|
31
53
|
// so we don't unroll this anymore
|
|
32
54
|
} else if (byte < 0xc2) {
|
|
33
55
|
if (!loose) throw new TypeError(E_STRICT)
|
package/hex.js
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
import { assertUint8 } from './assert.js'
|
|
2
1
|
import { typedView } from './array.js'
|
|
3
|
-
import {
|
|
2
|
+
import { assertU8 } from './fallback/_utils.js'
|
|
4
3
|
import * as js from './fallback/hex.js'
|
|
5
4
|
|
|
6
5
|
const { toHex: webHex } = Uint8Array.prototype // Modern engines have this
|
|
7
6
|
|
|
8
7
|
export function toHex(arr) {
|
|
9
|
-
|
|
8
|
+
assertU8(arr)
|
|
10
9
|
if (arr.length === 0) return ''
|
|
11
|
-
if (
|
|
10
|
+
if (webHex && arr.toHex === webHex) return arr.toHex()
|
|
12
11
|
return js.toHex(arr)
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
// Unlike Buffer.from(), throws on invalid input
|
|
16
|
-
export const fromHex =
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
: (str, format = 'uint8') => typedView(js.fromHex(str), format)
|
|
15
|
+
export const fromHex = Uint8Array.fromHex
|
|
16
|
+
? (str, format = 'uint8') => typedView(Uint8Array.fromHex(str), format)
|
|
17
|
+
: (str, format = 'uint8') => typedView(js.fromHex(str), format)
|
package/hex.node.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { assertUint8 } from './assert.js'
|
|
2
1
|
import { typedView } from './array.js'
|
|
3
|
-
import { E_STRING } from './fallback/_utils.js'
|
|
2
|
+
import { assertU8, E_STRING } from './fallback/_utils.js'
|
|
4
3
|
import { E_HEX } from './fallback/hex.js'
|
|
5
4
|
|
|
6
5
|
if (Buffer.TYPED_ARRAY_SUPPORT) throw new Error('Unexpected Buffer polyfill')
|
|
@@ -9,7 +8,7 @@ const { toHex: webHex } = Uint8Array.prototype // Modern engines have this
|
|
|
9
8
|
const denoBug = Buffer.from('ag', 'hex').length > 0
|
|
10
9
|
|
|
11
10
|
export function toHex(arr) {
|
|
12
|
-
|
|
11
|
+
assertU8(arr)
|
|
13
12
|
if (arr.length === 0) return ''
|
|
14
13
|
if (webHex && arr.toHex === webHex) return arr.toHex()
|
|
15
14
|
if (arr.constructor === Buffer && Buffer.isBuffer(arr)) return arr.hexSlice(0, arr.byteLength)
|
package/multi-byte.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { assertU8 } from './fallback/_utils.js'
|
|
2
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
|
|
6
6
|
let streaming = false
|
|
7
7
|
return (arr, stream = false) => {
|
|
8
|
-
|
|
8
|
+
assertU8(arr)
|
|
9
9
|
if (!streaming && arr.byteLength === 0) return ''
|
|
10
10
|
streaming = stream
|
|
11
11
|
return jsDecoder(arr, stream)
|
package/multi-byte.node.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { isDeno
|
|
1
|
+
import { assertU8, toBuf } from './fallback/_utils.js'
|
|
2
|
+
import { isDeno } from './fallback/platform.js'
|
|
3
3
|
import { isAsciiSuperset, multibyteDecoder, multibyteEncoder } from './fallback/multi-byte.js'
|
|
4
4
|
import { isAscii } from 'node:buffer'
|
|
5
5
|
|
|
@@ -8,7 +8,7 @@ export function createMultibyteDecoder(encoding, loose = false) {
|
|
|
8
8
|
let streaming = false
|
|
9
9
|
const asciiSuperset = isAsciiSuperset(encoding)
|
|
10
10
|
return (arr, stream = false) => {
|
|
11
|
-
|
|
11
|
+
assertU8(arr)
|
|
12
12
|
if (!streaming) {
|
|
13
13
|
if (arr.byteLength === 0) return ''
|
|
14
14
|
if (asciiSuperset && isAscii(arr)) {
|