diff --git a/.gitignore b/.gitignore index 995c13c0..7260c908 100644 --- a/.gitignore +++ b/.gitignore @@ -18,5 +18,6 @@ todo/ .docs-test-tmp/ src/commands/python3/worker.js src/commands/js-exec/worker.js +src/commands/js-exec/js-exec-worker.js fuzz-*.log .claude/settings.local.json diff --git a/package.json b/package.json index fb4b794b..830de283 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "scripts": { "build": "rm -rf dist && tsc && pnpm build:lib && pnpm build:lib:cjs && pnpm build:browser && pnpm build:cli && pnpm build:shell && pnpm build:worker && pnpm build:clean && cp dist/index.d.ts dist/index.d.cts && sed '1,/^-->/d' AGENTS.npm.md > dist/AGENTS.md", "build:clean": "find dist -name '*.test.js' -delete && find dist -name '*.test.d.ts' -delete", - "build:worker": "esbuild src/commands/python3/worker.ts --bundle --platform=node --format=esm --outfile=src/commands/python3/worker.js --external:../../../vendor/cpython-emscripten/* && cp src/commands/python3/worker.js dist/commands/python3/worker.js && mkdir -p dist/bin/chunks && cp src/commands/python3/worker.js dist/bin/chunks/worker.js && mkdir -p dist/bundle/chunks && cp src/commands/python3/worker.js dist/bundle/chunks/worker.js && esbuild src/commands/js-exec/worker.ts --bundle --platform=node --format=esm --outfile=src/commands/js-exec/worker.js --external:quickjs-emscripten && cp src/commands/js-exec/worker.js dist/commands/js-exec/worker.js && cp src/commands/js-exec/worker.js dist/bin/chunks/js-exec-worker.js && cp src/commands/js-exec/worker.js dist/bundle/chunks/js-exec-worker.js", + "build:worker": "esbuild src/commands/python3/worker.ts --bundle --platform=node --format=esm --outfile=src/commands/python3/worker.js --external:../../../vendor/cpython-emscripten/* && cp src/commands/python3/worker.js dist/commands/python3/worker.js && mkdir -p dist/bin/chunks && cp src/commands/python3/worker.js dist/bin/chunks/worker.js && mkdir -p dist/bundle/chunks && cp src/commands/python3/worker.js dist/bundle/chunks/worker.js && esbuild src/commands/js-exec/worker.ts --bundle --platform=node --format=esm --outfile=src/commands/js-exec/js-exec-worker.js --external:quickjs-emscripten && cp src/commands/js-exec/js-exec-worker.js dist/commands/js-exec/js-exec-worker.js && cp src/commands/js-exec/js-exec-worker.js dist/bin/chunks/js-exec-worker.js && cp src/commands/js-exec/js-exec-worker.js dist/bundle/chunks/js-exec-worker.js", "build:lib": "esbuild dist/index.js --bundle --splitting --platform=node --format=esm --minify --outdir=dist/bundle --chunk-names=chunks/[name]-[hash] --external:diff --external:minimatch --external:sprintf-js --external:turndown --external:sql.js --external:quickjs-emscripten --external:@mongodb-js/zstd --external:node-liblzma --external:compressjs", "build:lib:cjs": "esbuild dist/index.js --bundle --platform=node --format=cjs --minify --outfile=dist/bundle/index.cjs --external:diff --external:minimatch --external:sprintf-js --external:turndown --external:sql.js --external:quickjs-emscripten --external:@mongodb-js/zstd --external:node-liblzma --external:compressjs", "build:browser": "esbuild dist/browser.js --bundle --platform=browser --format=esm --minify --outfile=dist/bundle/browser.js --external:diff --external:minimatch --external:sprintf-js --external:turndown --external:node:zlib --external:@mongodb-js/zstd --external:node-liblzma --external:compressjs --define:__BROWSER__=true --alias:node:dns=./src/shims/browser-unsupported.js", diff --git a/src/commands/js-exec/js-exec.ts b/src/commands/js-exec/js-exec.ts index 66dbd974..0acbdc77 100644 --- a/src/commands/js-exec/js-exec.ts +++ b/src/commands/js-exec/js-exec.ts @@ -223,7 +223,7 @@ type QueuedExecution = { const executionQueue: QueuedExecution[] = []; let currentExecution: QueuedExecution | null = null; -const workerPath = fileURLToPath(new URL("./worker.js", import.meta.url)); +const workerPath = fileURLToPath(new URL("./js-exec-worker.js", import.meta.url)); function processNextExecution(): void { // Skip canceled entries (timed out before execution started) diff --git a/src/commands/js-exec/worker.ts b/src/commands/js-exec/worker.ts index 6749e868..fb10aec0 100644 --- a/src/commands/js-exec/worker.ts +++ b/src/commands/js-exec/worker.ts @@ -9,8 +9,21 @@ * Run: npx esbuild src/commands/js-exec/worker.ts --bundle --platform=node --format=esm --outfile=src/commands/js-exec/worker.js --external:quickjs-emscripten */ -import { stripTypeScriptTypes } from "node:module"; import { parentPort } from "node:worker_threads"; + +// Dynamic require with fallback: stripTypeScriptTypes is a Node.js 23.2+ API +// that is not available in all runtimes (e.g., Bun). When unavailable, TypeScript +// type stripping is disabled but plain JavaScript execution works normally. +// Uses require() instead of await import() because Bun Worker threads load .js +// files in a context where top-level await is not supported. +let stripTypeScriptTypes: (code: string) => string; +try { + // eslint-disable-next-line @typescript-eslint/no-require-imports + const nodeModule = require("node:module"); + stripTypeScriptTypes = nodeModule.stripTypeScriptTypes ?? ((code: string) => code); +} catch { + stripTypeScriptTypes = (code: string) => code; +} import { getQuickJS, type QuickJSContext,