Skip to content

Commit fa58c3b

Browse files
committed
fix: work-around workerd broken utf16-le implementation
Refs: cloudflare/workerd#6028
1 parent df54631 commit fa58c3b

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

fallback/_utils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const getNativeLatin1 = () => {
2929
}
3030

3131
export const nativeDecoderLatin1 = /* @__PURE__ */ getNativeLatin1()
32-
export const canDecoders = !!nativeDecoderLatin1
3332

3433
// Block Firefox < 146 specifically from using native hex/base64, as it's very slow there
3534
// Refs: https://bugzilla.mozilla.org/show_bug.cgi?id=1994067 (and linked issues), fixed in 146

single-byte.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertUint8 } from './assert.js'
2-
import { canDecoders, nativeEncoder, skipWeb, E_STRING } from './fallback/_utils.js'
2+
import { nativeDecoderLatin1, nativeEncoder, skipWeb, E_STRING } from './fallback/_utils.js'
33
import { encodeAscii, encodeAsciiPrefix, encodeLatin1 } from './fallback/latin1.js'
44
import { assertEncoding, encodingDecoder, encodeMap, E_STRICT } from './fallback/single-byte.js'
55

@@ -39,7 +39,7 @@ export function createSinglebyteDecoder(encoding, loose = false) {
3939
if (typeof loose !== 'boolean') throw new TypeError('loose option should be boolean')
4040
assertEncoding(encoding)
4141

42-
if (canDecoders && shouldUseNative(encoding)) {
42+
if (nativeDecoderLatin1 && shouldUseNative(encoding)) {
4343
// In try, as not all encodings might be implemented in all engines which have native TextDecoder
4444
try {
4545
const decoder = new TextDecoder(encoding, { fatal: !loose })

utf16.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
import * as js from './fallback/utf16.js'
2-
import { canDecoders, isLE, E_STRING } from './fallback/_utils.js'
2+
import { nativeDecoder, isLE, E_STRING } from './fallback/_utils.js'
33

4-
const { TextDecoder } = globalThis // Buffer is optional
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()
520
const ignoreBOM = true
621
const decoderFatalLE = canDecoders ? new TextDecoder('utf-16le', { ignoreBOM, fatal: true }) : null
722
const decoderLooseLE = canDecoders ? new TextDecoder('utf-16le', { ignoreBOM }) : null

0 commit comments

Comments
 (0)