Skip to content

Commit db44651

Browse files
committed
fix(napi): disable reuseWorker in browser bindings (#25640)
Fixes #25610 AI assistance: Codex was used to implement this change.
1 parent d55b76c commit db44651

16 files changed

Lines changed: 62 additions & 11 deletions

File tree

‎napi/disable-reused-workers.mjs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import fs from "node:fs";
2+
3+
const REUSED_WORKERS = "reuseWorker: { size: __asyncWorkPoolSize + __workerPoolSize },";
4+
const DISABLED_REUSED_WORKERS = "reuseWorker: false,";
5+
6+
export function disableReusedWorkers(path) {
7+
let data = fs.readFileSync(path, "utf-8");
8+
9+
// The pool is initialized eagerly, but browsers reject its worker URL when a binding is loaded
10+
// from a cross-origin CDN because workers must be loaded from the page's origin.
11+
if (data.includes(REUSED_WORKERS)) {
12+
data = data.replace(REUSED_WORKERS, DISABLED_REUSED_WORKERS);
13+
fs.writeFileSync(path, data);
14+
} else if (!data.includes(DISABLED_REUSED_WORKERS)) {
15+
throw new Error(`Could not find the reuseWorker option in ${path}`);
16+
}
17+
}

‎napi/minify/minify.wasi-browser.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ try {
565565
} = await __emnapiInstantiateNapiModule(__wasmFile, {
566566
context: __emnapiContext,
567567
asyncWorkPoolSize: __asyncWorkPoolSize,
568-
reuseWorker: { size: __asyncWorkPoolSize + __workerPoolSize },
568+
reuseWorker: false,
569569
plugins: [__emnapiAsyncWorkPlugin, __emnapiTSFNPlugin],
570570
wasi: __wasi,
571571
onCreateWorker() {

‎napi/minify/scripts/patch.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import fs from "node:fs";
22
import { join as pathJoin } from "node:path";
33

4-
const path = pathJoin(import.meta.dirname, "../index.js");
4+
import { disableReusedWorkers } from "../../disable-reused-workers.mjs";
5+
6+
const packageDir = pathJoin(import.meta.dirname, "..");
7+
disableReusedWorkers(pathJoin(packageDir, "minify.wasi-browser.js"));
8+
9+
const path = pathJoin(packageDir, "index.js");
510

611
let data = fs.readFileSync(path, "utf-8");
712
data = data.replace(

‎napi/parser/scripts/build-browser-bundle.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { rolldown } from "rolldown";
66
// Rollup-compatible bundlers reserve `\0`-prefixed IDs for plugin virtual modules,
77
// so Rolldown will not try to resolve these generated `?url` asset modules as files.
88
const NEW_URL_ASSET_PREFIX = "\0new-url-asset:";
9+
const WASI_BROWSER_BINDING = path.resolve("./src-js/parser.wasi-browser.js");
910

1011
async function main() {
1112
const args = parseArgs({
@@ -23,7 +24,7 @@ async function main() {
2324
platform: "browser",
2425
resolve: {
2526
alias: {
26-
"@oxc-parser/binding-wasm32-wasi": path.resolve("./src-js/parser.wasi-browser.js"),
27+
"@oxc-parser/binding-wasm32-wasi": WASI_BROWSER_BINDING,
2728
},
2829
},
2930
plugins: [

‎napi/parser/scripts/patch.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import fs from "node:fs";
2+
import { join as pathJoin } from "node:path";
23

3-
const filename = "./src-js/bindings.js";
4+
import { disableReusedWorkers } from "../../disable-reused-workers.mjs";
5+
6+
const packageDir = pathJoin(import.meta.dirname, "..");
7+
disableReusedWorkers(pathJoin(packageDir, "src-js/parser.wasi-browser.js"));
8+
9+
const filename = pathJoin(packageDir, "src-js/bindings.js");
410
let data = fs.readFileSync(filename, "utf-8");
511

612
data = data.replace(

‎napi/parser/src-js/parser.wasi-browser.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ try {
565565
} = await __emnapiInstantiateNapiModule(__wasmFile, {
566566
context: __emnapiContext,
567567
asyncWorkPoolSize: __asyncWorkPoolSize,
568-
reuseWorker: { size: __asyncWorkPoolSize + __workerPoolSize },
568+
reuseWorker: false,
569569
plugins: [__emnapiAsyncWorkPlugin, __emnapiTSFNPlugin],
570570
wasi: __wasi,
571571
onCreateWorker() {

‎napi/playground/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"build-dev": "pnpm run build-wasm-dev && node scripts/patch.js",
2929
"build": "pnpm run build-wasm-dev --release && node scripts/patch.js",
3030
"build-wasm-dev": "napi build --platform --esm --target wasm32-wasip1-threads --dts playground.wasi.d.cts",
31+
"postbuild-wasm-dev": "node scripts/patch-wasi-browser.js",
3132
"postbuild": "publint"
3233
},
3334
"dependencies": {

‎napi/playground/playground.wasi-browser.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ try {
565565
} = await __emnapiInstantiateNapiModule(__wasmFile, {
566566
context: __emnapiContext,
567567
asyncWorkPoolSize: __asyncWorkPoolSize,
568-
reuseWorker: { size: __asyncWorkPoolSize + __workerPoolSize },
568+
reuseWorker: false,
569569
plugins: [__emnapiAsyncWorkPlugin, __emnapiTSFNPlugin],
570570
wasi: __wasi,
571571
onCreateWorker() {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { join as pathJoin } from "node:path";
2+
3+
import { disableReusedWorkers } from "../../disable-reused-workers.mjs";
4+
5+
disableReusedWorkers(pathJoin(import.meta.dirname, "../playground.wasi-browser.js"));

‎napi/playground/scripts/patch.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import fs from "node:fs";
22
import { join as pathJoin } from "node:path";
33

4-
const path = pathJoin(import.meta.dirname, "../playground.wasi-browser.js");
4+
import "./patch-wasi-browser.js";
5+
6+
const packageDir = pathJoin(import.meta.dirname, "..");
7+
const path = pathJoin(packageDir, "playground.wasi-browser.js");
58

69
let data = fs.readFileSync(path, "utf-8");
710
data = data.replace(

0 commit comments

Comments
 (0)