|
1 | 1 | const { Buffer, TextEncoder, TextDecoder } = globalThis |
2 | 2 | const haveNativeBuffer = Buffer && !Buffer.TYPED_ARRAY_SUPPORT |
3 | | -let isNative = (x) => x && (haveNativeBuffer || `${x}`.includes('[native code]')) // we consider Node.js TextDecoder/TextEncoder native |
| 3 | +export const nativeBuffer = haveNativeBuffer ? Buffer : null |
| 4 | +export const isHermes = Boolean(globalThis.HermesInternal) |
| 5 | +export const isDeno = Boolean(globalThis.Deno) |
| 6 | +export const isLE = new Uint8Array(Uint16Array.of(258).buffer)[0] === 2 |
| 7 | + |
| 8 | +let isNative = (x) => { |
| 9 | + if (!x) return false |
| 10 | + if (haveNativeBuffer) return true // we consider Node.js TextDecoder/TextEncoder native |
| 11 | + const s = `${x}` |
| 12 | + // See https://github.com/facebook/hermes/pull/1855#issuecomment-3659386410 |
| 13 | + return s.includes('[native code]') || s.includes(`[bytecode]`) // Static Hermes has [bytecode] for contrib, which includes TextEncoder/TextDecoder |
| 14 | +} |
| 15 | + |
4 | 16 | if (!haveNativeBuffer && isNative(() => {})) isNative = () => false // e.g. XS, we don't want false positives |
5 | 17 |
|
6 | 18 | export const nativeEncoder = isNative(TextEncoder) ? new TextEncoder() : null |
7 | 19 | export const nativeDecoder = isNative(TextDecoder) |
8 | 20 | ? new TextDecoder('utf-8', { ignoreBOM: true }) |
9 | 21 | : null |
10 | | -export const nativeBuffer = haveNativeBuffer ? Buffer : null |
11 | | -export const isHermes = Boolean(globalThis.HermesInternal) |
12 | | -export const isDeno = Boolean(globalThis.Deno) |
13 | | -export const isLE = new Uint8Array(Uint16Array.of(258).buffer)[0] === 2 |
14 | 22 |
|
15 | 23 | // Actually windows-1252, compatible with ascii and latin1 decoding |
16 | 24 | // Beware that on non-latin1, i.e. on windows-1252, this is broken in ~all Node.js versions released |
17 | 25 | // in 2025 due to a regression, so we call it Latin1 as it's usable only for that |
18 | 26 | let nativeDecoderLatin1impl = null |
19 | | -if (isNative(TextDecoder)) { |
| 27 | +if (nativeDecoder) { |
20 | 28 | // Not all barebone engines with TextDecoder support something except utf-8, detect |
21 | 29 | try { |
22 | 30 | nativeDecoderLatin1impl = new TextDecoder('latin1', { ignoreBOM: true }) |
|
0 commit comments