@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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/bytes",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.0",
|
|
4
4
|
"description": "Various operations on Uint8Array data",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"encoding",
|
|
@@ -76,6 +76,9 @@
|
|
|
76
76
|
"/fallback/hex.js",
|
|
77
77
|
"/fallback/latin1.js",
|
|
78
78
|
"/fallback/percent.js",
|
|
79
|
+
"/fallback/platform.js",
|
|
80
|
+
"/fallback/platform.browser.js",
|
|
81
|
+
"/fallback/platform.native.js",
|
|
79
82
|
"/fallback/multi-byte.encodings.cjs",
|
|
80
83
|
"/fallback/multi-byte.encodings.json",
|
|
81
84
|
"/fallback/multi-byte.js",
|
|
@@ -84,6 +87,9 @@
|
|
|
84
87
|
"/fallback/single-byte.js",
|
|
85
88
|
"/fallback/utf16.js",
|
|
86
89
|
"/fallback/utf8.js",
|
|
90
|
+
"/fallback/utf8.auto.js",
|
|
91
|
+
"/fallback/utf8.auto.browser.js",
|
|
92
|
+
"/fallback/utf8.auto.native.js",
|
|
87
93
|
"/array.js",
|
|
88
94
|
"/array.d.ts",
|
|
89
95
|
"/assert.js",
|
|
@@ -121,6 +127,8 @@
|
|
|
121
127
|
"/single-byte.node.js",
|
|
122
128
|
"/utf16.js",
|
|
123
129
|
"/utf16.d.ts",
|
|
130
|
+
"/utf16.browser.js",
|
|
131
|
+
"/utf16.native.js",
|
|
124
132
|
"/utf16.node.js",
|
|
125
133
|
"/utf8.js",
|
|
126
134
|
"/utf8.d.ts",
|
|
@@ -200,6 +208,8 @@
|
|
|
200
208
|
"./utf16.js": {
|
|
201
209
|
"types": "./utf16.d.ts",
|
|
202
210
|
"node": "./utf16.node.js",
|
|
211
|
+
"react-native": "./utf16.native.js",
|
|
212
|
+
"browser": "./utf16.browser.js",
|
|
203
213
|
"default": "./utf16.js"
|
|
204
214
|
},
|
|
205
215
|
"./utf8.js": {
|
|
@@ -216,8 +226,16 @@
|
|
|
216
226
|
"default": "./wif.js"
|
|
217
227
|
}
|
|
218
228
|
},
|
|
229
|
+
"browser": {
|
|
230
|
+
"./utf16.js": "./utf16.browser.js",
|
|
231
|
+
"./fallback/platform.js": "./fallback/platform.browser.js",
|
|
232
|
+
"./fallback/utf8.auto.js": "./fallback/utf8.auto.browser.js"
|
|
233
|
+
},
|
|
219
234
|
"react-native": {
|
|
220
|
-
"./encoding-browser.js": "./encoding-browser.native.js"
|
|
235
|
+
"./encoding-browser.js": "./encoding-browser.native.js",
|
|
236
|
+
"./utf16.js": "./utf16.native.js",
|
|
237
|
+
"./fallback/platform.js": "./fallback/platform.native.js",
|
|
238
|
+
"./fallback/utf8.auto.js": "./fallback/utf8.auto.native.js"
|
|
221
239
|
},
|
|
222
240
|
"peerDependencies": {
|
|
223
241
|
"@noble/hashes": "^1.8.0 || ^2.0.0"
|
|
@@ -232,7 +250,7 @@
|
|
|
232
250
|
"@exodus/crypto": "^1.0.0-rc.30",
|
|
233
251
|
"@exodus/eslint-config": "^5.24.0",
|
|
234
252
|
"@exodus/prettier": "^1.0.0",
|
|
235
|
-
"@exodus/test": "1.0.0-rc.
|
|
253
|
+
"@exodus/test": "1.0.0-rc.115",
|
|
236
254
|
"@hexagon/base64": "^2.0.4",
|
|
237
255
|
"@noble/hashes": "^2.0.1",
|
|
238
256
|
"@oslojs/encoding": "^1.1.0",
|
|
@@ -254,7 +272,7 @@
|
|
|
254
272
|
"decode-utf8": "^1.0.1",
|
|
255
273
|
"electron": "36.5.0",
|
|
256
274
|
"encode-utf8": "^2.0.0",
|
|
257
|
-
"esbuild": "^0.27.
|
|
275
|
+
"esbuild": "^0.27.3",
|
|
258
276
|
"eslint": "^8.44.0",
|
|
259
277
|
"fast-base64-decode": "^2.0.0",
|
|
260
278
|
"fast-base64-encode": "^1.0.0",
|
package/single-byte.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { nativeDecoderLatin1, nativeEncoder
|
|
1
|
+
import { assertU8, E_STRING } from './fallback/_utils.js'
|
|
2
|
+
import { nativeDecoderLatin1, nativeEncoder } from './fallback/platform.js'
|
|
3
3
|
import { encodeAscii, encodeAsciiPrefix, encodeLatin1 } from './fallback/latin1.js'
|
|
4
4
|
import { assertEncoding, encodingDecoder, encodeMap, E_STRICT } from './fallback/single-byte.js'
|
|
5
5
|
|
|
@@ -44,7 +44,7 @@ export function createSinglebyteDecoder(encoding, loose = false) {
|
|
|
44
44
|
try {
|
|
45
45
|
const decoder = new TextDecoder(encoding, { fatal: !loose })
|
|
46
46
|
return (arr) => {
|
|
47
|
-
|
|
47
|
+
assertU8(arr)
|
|
48
48
|
if (arr.byteLength === 0) return ''
|
|
49
49
|
return decoder.decode(arr)
|
|
50
50
|
}
|
|
@@ -53,7 +53,7 @@ export function createSinglebyteDecoder(encoding, loose = false) {
|
|
|
53
53
|
|
|
54
54
|
const jsDecoder = encodingDecoder(encoding)
|
|
55
55
|
return (arr) => {
|
|
56
|
-
|
|
56
|
+
assertU8(arr)
|
|
57
57
|
if (arr.byteLength === 0) return ''
|
|
58
58
|
return jsDecoder(arr, loose)
|
|
59
59
|
}
|
|
@@ -88,7 +88,7 @@ function encode(s, m) {
|
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
// fromBase64+btoa path is faster on everything where fromBase64 is fast
|
|
91
|
-
const useLatin1btoa = Uint8Array.fromBase64 && btoa
|
|
91
|
+
const useLatin1btoa = Uint8Array.fromBase64 && btoa
|
|
92
92
|
|
|
93
93
|
export function createSinglebyteEncoder(encoding, { mode = 'fatal' } = {}) {
|
|
94
94
|
// TODO: replacement, truncate (replacement will need varying length)
|
package/single-byte.node.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { assertUint8 } from './assert.js'
|
|
2
1
|
import { isAscii } from 'node:buffer'
|
|
3
|
-
import {
|
|
2
|
+
import { assertU8, toBuf, E_STRING } from './fallback/_utils.js'
|
|
3
|
+
import { isDeno, isLE } from './fallback/platform.js'
|
|
4
4
|
import { asciiPrefix } from './fallback/latin1.js'
|
|
5
5
|
import { encodingMapper, encodingDecoder, encodeMap, E_STRICT } from './fallback/single-byte.js'
|
|
6
6
|
|
|
@@ -26,7 +26,7 @@ export function createSinglebyteDecoder(encoding, loose = false) {
|
|
|
26
26
|
if (isDeno) {
|
|
27
27
|
const jsDecoder = encodingDecoder(encoding) // asserts
|
|
28
28
|
return (arr) => {
|
|
29
|
-
|
|
29
|
+
assertU8(arr)
|
|
30
30
|
if (arr.byteLength === 0) return ''
|
|
31
31
|
if (isAscii(arr)) return toBuf(arr).toString()
|
|
32
32
|
return jsDecoder(arr, loose) // somewhy faster on Deno anyway, TODO: optimize?
|
|
@@ -37,7 +37,7 @@ export function createSinglebyteDecoder(encoding, loose = false) {
|
|
|
37
37
|
const latin1path = encoding === 'windows-1252'
|
|
38
38
|
const { incomplete, mapper } = encodingMapper(encoding) // asserts
|
|
39
39
|
return (arr) => {
|
|
40
|
-
|
|
40
|
+
assertU8(arr)
|
|
41
41
|
if (arr.byteLength === 0) return ''
|
|
42
42
|
if (isLatin1 || isAscii(arr)) return toBuf(arr).latin1Slice() // .latin1Slice is faster than .asciiSlice
|
|
43
43
|
|
package/utf16.browser.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// We trust browsers to always have correct TextDecoder for utf-16le/utf-16be with ignoreBOM without streaming
|
|
2
|
+
|
|
3
|
+
import { encodeApi, decodeApiDecoders } from './fallback/utf16.js'
|
|
4
|
+
|
|
5
|
+
export const utf16fromString = (str, format = 'uint16') => encodeApi(str, false, format)
|
|
6
|
+
export const utf16fromStringLoose = (str, format = 'uint16') => encodeApi(str, true, format)
|
|
7
|
+
export const utf16toString = (arr, format = 'uint16') => decodeApiDecoders(arr, false, format)
|
|
8
|
+
export const utf16toStringLoose = (arr, format = 'uint16') => decodeApiDecoders(arr, true, format)
|
package/utf16.js
CHANGED
|
@@ -1,90 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { nativeDecoder, isLE, E_STRING } from './fallback/_utils.js'
|
|
3
|
-
|
|
4
|
-
const { TextDecoder } = globalThis
|
|
5
|
-
|
|
6
|
-
function checkDecoders() {
|
|
7
|
-
// Not all barebone engines with TextDecoder support something except utf-8
|
|
8
|
-
// Also workerd specifically has a broken utf-16le implementation
|
|
9
|
-
if (!nativeDecoder) return false
|
|
10
|
-
try {
|
|
11
|
-
const a = new TextDecoder('utf-16le').decode(Uint8Array.of(1, 2, 3, 0xd8))
|
|
12
|
-
const b = new TextDecoder('utf-16be').decode(Uint8Array.of(2, 1, 0xd8, 3))
|
|
13
|
-
return a === b && a === '\u0201\uFFFD'
|
|
14
|
-
} catch {}
|
|
15
|
-
|
|
16
|
-
return false
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const canDecoders = checkDecoders()
|
|
20
|
-
const ignoreBOM = true
|
|
21
|
-
const decoderFatalLE = canDecoders ? new TextDecoder('utf-16le', { ignoreBOM, fatal: true }) : null
|
|
22
|
-
const decoderLooseLE = canDecoders ? new TextDecoder('utf-16le', { ignoreBOM }) : null
|
|
23
|
-
const decoderFatalBE = canDecoders ? new TextDecoder('utf-16be', { ignoreBOM, fatal: true }) : null
|
|
24
|
-
const decoderLooseBE = canDecoders ? new TextDecoder('utf-16be', { ignoreBOM }) : null
|
|
25
|
-
const decoderFatal16 = isLE ? decoderFatalLE : decoderFatalBE
|
|
26
|
-
const decoderLoose16 = isLE ? decoderLooseLE : decoderLooseBE
|
|
27
|
-
const { isWellFormed, toWellFormed } = String.prototype
|
|
28
|
-
|
|
29
|
-
const { E_STRICT, E_STRICT_UNICODE } = js
|
|
30
|
-
|
|
31
|
-
// Unlike utf8, operates on Uint16Arrays by default
|
|
32
|
-
|
|
33
|
-
const to8 = (a) => new Uint8Array(a.buffer, a.byteOffset, a.byteLength)
|
|
34
|
-
|
|
35
|
-
function encode(str, loose = false, format = 'uint16') {
|
|
36
|
-
if (typeof str !== 'string') throw new TypeError(E_STRING)
|
|
37
|
-
if (format !== 'uint16' && format !== 'uint8-le' && format !== 'uint8-be') {
|
|
38
|
-
throw new TypeError('Unknown format')
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const shouldSwap = (isLE && format === 'uint8-be') || (!isLE && format === 'uint8-le')
|
|
42
|
-
|
|
43
|
-
// On v8 and SpiderMonkey, check via isWellFormed is faster than js
|
|
44
|
-
// On JSC, check during loop is faster than isWellFormed
|
|
45
|
-
// If isWellFormed is available, we skip check during decoding and recheck after
|
|
46
|
-
// If isWellFormed is unavailable, we check in js during decoding
|
|
47
|
-
if (!loose && isWellFormed && !isWellFormed.call(str)) throw new TypeError(E_STRICT_UNICODE)
|
|
48
|
-
const u16 = js.encode(str, loose, !loose && isWellFormed, shouldSwap)
|
|
49
|
-
|
|
50
|
-
if (format === 'uint8-le' || format === 'uint8-be') return to8(u16) // Already swapped
|
|
51
|
-
if (format === 'uint16') return u16
|
|
52
|
-
/* c8 ignore next */
|
|
53
|
-
throw new Error('Unreachable')
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function decode(input, loose = false, format = 'uint16') {
|
|
57
|
-
let u16
|
|
58
|
-
switch (format) {
|
|
59
|
-
case 'uint16':
|
|
60
|
-
if (!(input instanceof Uint16Array)) throw new TypeError('Expected an Uint16Array')
|
|
61
|
-
if (canDecoders) return loose ? decoderLoose16.decode(input) : decoderFatal16.decode(input)
|
|
62
|
-
u16 = input
|
|
63
|
-
break
|
|
64
|
-
case 'uint8-le':
|
|
65
|
-
if (!(input instanceof Uint8Array)) throw new TypeError('Expected an Uint8Array')
|
|
66
|
-
if (input.byteLength % 2 !== 0) throw new TypeError('Expected even number of bytes')
|
|
67
|
-
if (canDecoders) return loose ? decoderLooseLE.decode(input) : decoderFatalLE.decode(input)
|
|
68
|
-
u16 = js.to16input(input, true)
|
|
69
|
-
break
|
|
70
|
-
case 'uint8-be':
|
|
71
|
-
if (!(input instanceof Uint8Array)) throw new TypeError('Expected an Uint8Array')
|
|
72
|
-
if (input.byteLength % 2 !== 0) throw new TypeError('Expected even number of bytes')
|
|
73
|
-
if (canDecoders) return loose ? decoderLooseBE.decode(input) : decoderFatalBE.decode(input)
|
|
74
|
-
u16 = js.to16input(input, false)
|
|
75
|
-
break
|
|
76
|
-
default:
|
|
77
|
-
throw new TypeError('Unknown format')
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const str = js.decode(u16, loose, (!loose && isWellFormed) || (loose && toWellFormed))
|
|
81
|
-
if (!loose && isWellFormed && !isWellFormed.call(str)) throw new TypeError(E_STRICT)
|
|
82
|
-
if (loose && toWellFormed) return toWellFormed.call(str)
|
|
83
|
-
|
|
84
|
-
return str
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export const utf16fromString = (str, format = 'uint16') => encode(str, false, format)
|
|
88
|
-
export const utf16fromStringLoose = (str, format = 'uint16') => encode(str, true, format)
|
|
89
|
-
export const utf16toString = (arr, format = 'uint16') => decode(arr, false, format)
|
|
90
|
-
export const utf16toStringLoose = (arr, format = 'uint16') => decode(arr, true, format)
|
|
1
|
+
export * from './utf16.native.js'
|
package/utf16.native.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { encodeApi, decodeApiDecoders, decodeApiJS } from './fallback/utf16.js'
|
|
2
|
+
import { nativeDecoder } from './fallback/platform.native.js'
|
|
3
|
+
|
|
4
|
+
function checkDecoders() {
|
|
5
|
+
// Not all barebone engines with TextDecoder support something except utf-8
|
|
6
|
+
// Also workerd specifically has a broken utf-16le implementation
|
|
7
|
+
if (!nativeDecoder) return false
|
|
8
|
+
try {
|
|
9
|
+
const a = new TextDecoder('utf-16le').decode(Uint8Array.of(1, 2, 3, 0xd8))
|
|
10
|
+
const b = new TextDecoder('utf-16be').decode(Uint8Array.of(2, 1, 0xd8, 3))
|
|
11
|
+
return a === b && a === '\u0201\uFFFD'
|
|
12
|
+
} catch {}
|
|
13
|
+
|
|
14
|
+
return false
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const decode = checkDecoders() ? decodeApiDecoders : decodeApiJS
|
|
18
|
+
|
|
19
|
+
export const utf16fromString = (str, format = 'uint16') => encodeApi(str, false, format)
|
|
20
|
+
export const utf16fromStringLoose = (str, format = 'uint16') => encodeApi(str, true, format)
|
|
21
|
+
export const utf16toString = (arr, format = 'uint16') => decode(arr, false, format)
|
|
22
|
+
export const utf16toStringLoose = (arr, format = 'uint16') => decode(arr, true, format)
|
package/utf16.node.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { assertU8, E_STRING, E_STRICT_UNICODE } from './fallback/_utils.js'
|
|
2
|
+
import { isDeno, isLE } from './fallback/platform.js'
|
|
3
|
+
import { E_STRICT, decodeApiDecoders } from './fallback/utf16.js'
|
|
3
4
|
|
|
4
5
|
if (Buffer.TYPED_ARRAY_SUPPORT) throw new Error('Unexpected Buffer polyfill')
|
|
5
6
|
|
|
@@ -48,7 +49,7 @@ function decodeNode(input, loose = false, format = 'uint16') {
|
|
|
48
49
|
if (!(input instanceof Uint16Array)) throw new TypeError('Expected an Uint16Array')
|
|
49
50
|
ble = swapped(input, !isLE)
|
|
50
51
|
} else if (format === 'uint8-le' || format === 'uint8-be') {
|
|
51
|
-
|
|
52
|
+
assertU8(input)
|
|
52
53
|
if (input.byteLength % 2 !== 0) throw new TypeError('Expected even number of bytes')
|
|
53
54
|
ble = swapped(input, format === 'uint8-be')
|
|
54
55
|
} else {
|
|
@@ -61,23 +62,7 @@ function decodeNode(input, loose = false, format = 'uint16') {
|
|
|
61
62
|
throw new TypeError(E_STRICT)
|
|
62
63
|
}
|
|
63
64
|
|
|
64
|
-
|
|
65
|
-
let encoding
|
|
66
|
-
if (format === 'uint16') {
|
|
67
|
-
if (!(input instanceof Uint16Array)) throw new TypeError('Expected an Uint16Array')
|
|
68
|
-
encoding = isLE ? 'utf-16le' : 'utf-16be'
|
|
69
|
-
} else if (format === 'uint8-le' || format === 'uint8-be') {
|
|
70
|
-
if (!(input instanceof Uint8Array)) throw new TypeError('Expected an Uint8Array')
|
|
71
|
-
if (input.byteLength % 2 !== 0) throw new TypeError('Expected even number of bytes')
|
|
72
|
-
encoding = format === 'uint8-le' ? 'utf-16le' : 'utf-16be'
|
|
73
|
-
} else {
|
|
74
|
-
throw new TypeError('Unknown format')
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return new TextDecoder(encoding, { ignoreBOM: true, fatal: !loose }).decode(input) // TODO: cache decoder?
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const decode = isDeno ? decodeDecoder : decodeNode
|
|
65
|
+
const decode = isDeno ? decodeApiDecoders : decodeNode
|
|
81
66
|
|
|
82
67
|
export const utf16fromString = (str, format = 'uint16') => encode(str, false, format)
|
|
83
68
|
export const utf16fromStringLoose = (str, format = 'uint16') => encode(str, true, format)
|
package/utf8.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { assertUint8 } from './assert.js'
|
|
2
1
|
import { typedView } from './array.js'
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import * as js from './fallback/utf8.js'
|
|
2
|
+
import { assertU8, E_STRING, E_STRICT_UNICODE } from './fallback/_utils.js'
|
|
3
|
+
import { nativeDecoder, nativeEncoder } from './fallback/platform.js'
|
|
4
|
+
import * as js from './fallback/utf8.auto.js'
|
|
6
5
|
|
|
7
|
-
const { TextDecoder, decodeURIComponent, escape } = globalThis // Buffer is optional
|
|
8
6
|
// ignoreBOM: true means that BOM will be left as-is, i.e. will be present in the output
|
|
9
7
|
// We don't want to strip anything unexpectedly
|
|
10
8
|
const decoderLoose = nativeDecoder
|
|
@@ -13,10 +11,6 @@ const decoderFatal = nativeDecoder
|
|
|
13
11
|
: null
|
|
14
12
|
const { isWellFormed } = String.prototype
|
|
15
13
|
|
|
16
|
-
const { E_STRICT, E_STRICT_UNICODE } = js
|
|
17
|
-
|
|
18
|
-
const shouldUseEscapePath = isHermes // faster only on Hermes, js path beats it on normal engines
|
|
19
|
-
|
|
20
14
|
function deLoose(str, loose, res) {
|
|
21
15
|
if (loose || str.length === res.length) return res // length is equal only for ascii, which is automatically fine
|
|
22
16
|
if (isWellFormed) {
|
|
@@ -35,7 +29,7 @@ function deLoose(str, loose, res) {
|
|
|
35
29
|
start = pos + 1
|
|
36
30
|
if (res[pos + 1] === 0xbf && res[pos + 2] === 0xbd) {
|
|
37
31
|
// Found a replacement char in output, need to recheck if we encoded the input correctly
|
|
38
|
-
if (!nativeDecoder && str.length < 1e7) {
|
|
32
|
+
if (js.decodeFast && !nativeDecoder && str.length < 1e7) {
|
|
39
33
|
// This is ~2x faster than decode in Hermes
|
|
40
34
|
try {
|
|
41
35
|
if (encodeURI(str) !== null) return res // guard against optimizing out
|
|
@@ -51,32 +45,19 @@ function deLoose(str, loose, res) {
|
|
|
51
45
|
function encode(str, loose = false) {
|
|
52
46
|
if (typeof str !== 'string') throw new TypeError(E_STRING)
|
|
53
47
|
if (str.length === 0) return new Uint8Array() // faster than Uint8Array.of
|
|
54
|
-
if (nativeEncoder) return deLoose(str, loose, nativeEncoder.encode(str))
|
|
48
|
+
if (nativeEncoder || !js.encode) return deLoose(str, loose, nativeEncoder.encode(str))
|
|
55
49
|
// No reason to use unescape + encodeURIComponent: it's slower than JS on normal engines, and modern Hermes already has TextEncoder
|
|
56
50
|
return js.encode(str, loose)
|
|
57
51
|
}
|
|
58
52
|
|
|
59
53
|
function decode(arr, loose = false) {
|
|
60
|
-
|
|
54
|
+
assertU8(arr)
|
|
61
55
|
if (arr.byteLength === 0) return ''
|
|
62
|
-
if (nativeDecoder
|
|
63
|
-
|
|
64
|
-
// Fast path for ASCII prefix, this is faster than all alternatives below
|
|
65
|
-
const prefix = decodeLatin1(arr, 0, asciiPrefix(arr)) // No native decoder to use, so decodeAscii is useless here
|
|
66
|
-
if (prefix.length === arr.length) return prefix
|
|
67
|
-
|
|
68
|
-
// This codepath gives a ~3x perf boost on Hermes
|
|
69
|
-
if (shouldUseEscapePath && escape && decodeURIComponent) {
|
|
70
|
-
const o = escape(decodeLatin1(arr, prefix.length, arr.length))
|
|
71
|
-
try {
|
|
72
|
-
return prefix + decodeURIComponent(o) // Latin1 to utf8
|
|
73
|
-
} catch {
|
|
74
|
-
if (!loose) throw new TypeError(E_STRICT)
|
|
75
|
-
// Ok, we have to use manual implementation for loose decoder
|
|
76
|
-
}
|
|
56
|
+
if (nativeDecoder || !js.decodeFast) {
|
|
57
|
+
return loose ? decoderLoose.decode(arr) : decoderFatal.decode(arr) // Node.js and browsers
|
|
77
58
|
}
|
|
78
59
|
|
|
79
|
-
return
|
|
60
|
+
return js.decodeFast(arr, loose)
|
|
80
61
|
}
|
|
81
62
|
|
|
82
63
|
export const utf8fromString = (str, format = 'uint8') => typedView(encode(str, false), format)
|
package/utf8.node.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { assertUint8 } from './assert.js'
|
|
2
1
|
import { typedView } from './array.js'
|
|
3
|
-
import { E_STRING } from './fallback/_utils.js'
|
|
4
|
-
import { E_STRICT
|
|
2
|
+
import { assertU8, E_STRING, E_STRICT_UNICODE } from './fallback/_utils.js'
|
|
3
|
+
import { E_STRICT } from './fallback/utf8.js'
|
|
5
4
|
import { isAscii } from 'node:buffer'
|
|
6
5
|
|
|
7
6
|
if (Buffer.TYPED_ARRAY_SUPPORT) throw new Error('Unexpected Buffer polyfill')
|
|
@@ -40,7 +39,7 @@ function encode(str, loose = false) {
|
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
function decode(arr, loose = false) {
|
|
43
|
-
|
|
42
|
+
assertU8(arr)
|
|
44
43
|
const byteLength = arr.byteLength
|
|
45
44
|
if (byteLength === 0) return ''
|
|
46
45
|
if (byteLength > 0x6_00 && !(isDeno && loose) && isAscii(arr)) {
|
package/whatwg.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { utf8fromStringLoose } from '@exodus/bytes/utf8.js'
|
|
2
2
|
import { createSinglebyteEncoder } from '@exodus/bytes/single-byte.js'
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
isMultibyte,
|
|
5
|
+
getMultibyteEncoder,
|
|
6
|
+
normalizeEncoding,
|
|
7
|
+
E_ENCODING,
|
|
8
|
+
} from './fallback/encoding.js'
|
|
5
9
|
import { percentEncoder } from './fallback/percent.js'
|
|
6
10
|
import { encodeMap } from './fallback/single-byte.js'
|
|
7
11
|
import { E_STRING } from './fallback/_utils.js'
|