Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions fallback/utf8.auto.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const decodeFast = null
export const encode = null
1 change: 1 addition & 0 deletions fallback/utf8.auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { decodeFast, encode } from './utf8.js'
1 change: 1 addition & 0 deletions fallback/utf8.auto.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { decodeFast, encode } from './utf8.js'
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
"/fallback/single-byte.js",
"/fallback/utf16.js",
"/fallback/utf8.js",
"/fallback/utf8.auto.js",
"/fallback/utf8.auto.browser.js",
"/fallback/utf8.auto.native.js",
"/array.js",
"/array.d.ts",
"/assert.js",
Expand Down Expand Up @@ -216,8 +219,12 @@
"default": "./wif.js"
}
},
"browser": {
"./fallback/utf8.auto.js": "./fallback/utf8.auto.browser.js"
},
"react-native": {
"./encoding-browser.js": "./encoding-browser.native.js"
"./encoding-browser.js": "./encoding-browser.native.js",
"./fallback/utf8.auto.js": "./fallback/utf8.auto.native.js"
},
"peerDependencies": {
"@noble/hashes": "^1.8.0 || ^2.0.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/utf8.hermes.test.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
delete globalThis.TextDecoder
if (!process.env.EXODUS_TEST_IS_BROWSER) delete globalThis.TextDecoder
delete String.prototype.isWellFormed
delete String.prototype.toWellFormed

Expand Down
10 changes: 7 additions & 3 deletions tests/utf8.noenc.test.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
delete globalThis.TextEncoder
delete globalThis.TextDecoder
require('./utf8.lib.test.js')
if (process.env.EXODUS_TEST_IS_BROWSER) {
require('node:test').test.skip('Under browsers, TextEncoder / TextDecoder is required')
} else {
delete globalThis.TextEncoder
delete globalThis.TextDecoder
require('./utf8.lib.test.js')
}
5 changes: 3 additions & 2 deletions tests/wpt/fallback.test.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Drop available native implementations so fallbacks will be used
// The only exception is *.node.js impls, but tests in other contexts cover that
if (!globalThis.Buffer || globalThis.Buffer.TYPED_ARRAY_SUPPORT) {
// The exceptions for that are *.node.js impls and browser bundles, but tests in other contexts cover that
const isNode = globalThis.Buffer && !globalThis.Buffer.TYPED_ARRAY_SUPPORT
if (!process.env.EXODUS_TEST_IS_BROWSER && !isNode) {
delete globalThis.TextEncoder
delete globalThis.TextDecoder
}
Expand Down
4 changes: 2 additions & 2 deletions utf8.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { typedView } from './array.js'
import { nativeDecoder, nativeEncoder, E_STRING, E_STRICT_UNICODE } from './fallback/_utils.js'
import * as js from './fallback/utf8.js'
import * as js from './fallback/utf8.auto.js'

const { TextDecoder } = globalThis
// ignoreBOM: true means that BOM will be left as-is, i.e. will be present in the output
Expand Down Expand Up @@ -29,7 +29,7 @@ function deLoose(str, loose, res) {
start = pos + 1
if (res[pos + 1] === 0xbf && res[pos + 2] === 0xbd) {
// Found a replacement char in output, need to recheck if we encoded the input correctly
if (!nativeDecoder && str.length < 1e7) {
if (js.decodeFast && !nativeDecoder && str.length < 1e7) {
// This is ~2x faster than decode in Hermes
try {
if (encodeURI(str) !== null) return res // guard against optimizing out
Expand Down