3ddc757b15
Root cause: bun build --target bun inlines top-level
var { spawn } = globalThis.Bun;
for every file that contains 'import { spawn } from "bun"'. On Node/Electron
where globalThis.Bun is undefined, this crashes with
Cannot destructure property 'spawn' of 'globalThis.Bun' as it is undefined.
26 source files had this import; the bundled output had 25 top-level destructures.
Fix:
- Add src/shared/bun-spawn-shim.ts: a thin wrapper that
- delegates to Bun.spawn/spawnSync when globalThis.Bun is present (real Bun)
- falls back to static ESM imports of node:child_process otherwise
- uses static 'import { spawn } from "node:child_process"' so Bun bundler
does NOT emit any globalThis.Bun destructures for this module
- Replace all 26 'from "bun"' spawn/spawnSync imports with relative paths to shim
- Replace 4 direct Bun.spawn() call sites with shim's spawn()
- Remove src/electron-compat.ts and script/prepend-electron-shim.ts (no longer needed)
- Update src/electron-compat.test.ts to assert 0 top-level globalThis.Bun destructures
Verification: grep -c '} = globalThis.Bun;' dist/index.js → 0 (was 25)
All 5921 tests pass (1 pre-existing timeout failure unrelated to this change).
Fixes #3797
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import { spawn, spawnSync } from "../bun-spawn-shim"
|
|
|
|
import type { ArchiveEntry } from "../archive-entry-validator"
|
|
|
|
export function isPythonZipListingAvailable(): boolean {
|
|
const proc = spawnSync(["python3", "--version"], {
|
|
stdout: "ignore",
|
|
stderr: "ignore",
|
|
})
|
|
|
|
return proc.exitCode === 0
|
|
}
|
|
|
|
export async function listZipEntriesWithPython(
|
|
archivePath: string
|
|
): Promise<ArchiveEntry[]> {
|
|
const script = [
|
|
"import json, stat, sys, zipfile",
|
|
"entries = []",
|
|
"with zipfile.ZipFile(sys.argv[1], 'r') as archive:",
|
|
" for info in archive.infolist():",
|
|
" mode = (info.external_attr >> 16) & 0xFFFF",
|
|
" if stat.S_ISLNK(mode):",
|
|
" entry_type = 'symlink'",
|
|
" link_path = archive.read(info).decode('utf-8', 'surrogateescape')",
|
|
" elif info.filename.endswith('/'):",
|
|
" entry_type = 'directory'",
|
|
" link_path = None",
|
|
" else:",
|
|
" entry_type = 'file'",
|
|
" link_path = None",
|
|
" entry = {'path': info.filename, 'type': entry_type}",
|
|
" if link_path is not None:",
|
|
" entry['linkPath'] = link_path",
|
|
" entries.append(entry)",
|
|
"print(json.dumps(entries))",
|
|
].join("\n")
|
|
|
|
const proc = spawn(["python3", "-c", script, archivePath], {
|
|
stdout: "pipe",
|
|
stderr: "pipe",
|
|
})
|
|
|
|
const [exitCode, stdout, stderr] = await Promise.all([
|
|
proc.exited,
|
|
new Response(proc.stdout).text(),
|
|
new Response(proc.stderr).text(),
|
|
])
|
|
|
|
if (exitCode !== 0) {
|
|
throw new Error(`zip entry listing failed (exit ${exitCode}): ${stderr}`)
|
|
}
|
|
|
|
return JSON.parse(stdout) as ArchiveEntry[]
|
|
}
|