@exodus/bytes 1.1.0 → 1.2.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 +1 -0
- package/package.json +1 -1
- package/utf16.node.js +1 -0
- package/utf8.node.js +16 -3
package/README.md
CHANGED
|
@@ -41,6 +41,7 @@ Spec compliant, passing WPT and covered with extra tests.
|
|
|
41
41
|
Moreover, tests for this library uncovered [bugs in all major implementations](https://docs.google.com/spreadsheets/d/1pdEefRG6r9fZy61WHGz0TKSt8cO4ISWqlpBN5KntIvQ/edit).
|
|
42
42
|
|
|
43
43
|
[Faster than Node.js native implementation on Node.js](https://github.com/nodejs/node/issues/61041#issuecomment-3649242024).
|
|
44
|
+
Runs (and passes WPT) on Node.js built without ICU.
|
|
44
45
|
|
|
45
46
|
### Caveat: `TextDecoder` / `TextEncoder` APIs are lossy by default per spec
|
|
46
47
|
|
package/package.json
CHANGED
package/utf16.node.js
CHANGED
|
@@ -36,6 +36,7 @@ const swapped = (x, swap) =>
|
|
|
36
36
|
swap ? Buffer.from(x).swap16() : Buffer.from(x.buffer, x.byteOffset, x.byteLength)
|
|
37
37
|
|
|
38
38
|
// We skip TextDecoder on Node.js, as it's is somewhy significantly slower than Buffer for utf16
|
|
39
|
+
// Also, it incorrectly misses replacements with Node.js is built without ICU, we fix that
|
|
39
40
|
function decodeNode(input, loose = false, format = 'uint16') {
|
|
40
41
|
let ble
|
|
41
42
|
if (format === 'uint16') {
|
package/utf8.node.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { assertUint8 } from './assert.js'
|
|
2
2
|
import { typedView } from './array.js'
|
|
3
|
-
import { E_STRICT_UNICODE } from './fallback/utf8.js'
|
|
3
|
+
import { E_STRICT, E_STRICT_UNICODE } from './fallback/utf8.js'
|
|
4
4
|
import { isAscii } from 'node:buffer'
|
|
5
5
|
|
|
6
6
|
if (Buffer.TYPED_ARRAY_SUPPORT) throw new Error('Unexpected Buffer polyfill')
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
let decoderFatal
|
|
9
9
|
const decoderLoose = new TextDecoder('utf-8', { ignoreBOM: true })
|
|
10
10
|
const { isWellFormed } = String.prototype
|
|
11
11
|
const isDeno = Boolean(globalThis.Deno)
|
|
12
12
|
|
|
13
|
+
try {
|
|
14
|
+
decoderFatal = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true })
|
|
15
|
+
} catch {
|
|
16
|
+
// Without ICU, Node.js doesn't support fatal option for utf-8
|
|
17
|
+
}
|
|
18
|
+
|
|
13
19
|
function encode(str, loose = false) {
|
|
14
20
|
if (typeof str !== 'string') throw new TypeError('Input is not a string')
|
|
15
21
|
const strLength = str.length
|
|
@@ -45,7 +51,14 @@ function decode(arr, loose = false) {
|
|
|
45
51
|
return buf.latin1Slice(0, arr.byteLength) // .latin1Slice is faster than .asciiSlice
|
|
46
52
|
}
|
|
47
53
|
|
|
48
|
-
|
|
54
|
+
if (loose) return decoderLoose.decode(arr)
|
|
55
|
+
if (decoderFatal) return decoderFatal.decode(arr)
|
|
56
|
+
|
|
57
|
+
// We are in an env without native fatal decoder support (non-fixed Node.js without ICU)
|
|
58
|
+
// Well, just recheck against encode if it contains replacement then, this is still faster than js impl
|
|
59
|
+
const str = decoderLoose.decode(arr)
|
|
60
|
+
if (str.includes('\uFFFD') && !Buffer.from(str).equals(arr)) throw new TypeError(E_STRICT)
|
|
61
|
+
return str
|
|
49
62
|
}
|
|
50
63
|
|
|
51
64
|
export const utf8fromString = (str, format = 'uint8') => typedView(encode(str, false), format)
|