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
119 lines
3.2 KiB
TypeScript
119 lines
3.2 KiB
TypeScript
import { spawn, spawnSync } from "./bun-spawn-shim"
|
|
import { release } from "os"
|
|
|
|
import { validateArchiveEntries } from "./archive-entry-validator"
|
|
import {
|
|
isPythonZipListingAvailable,
|
|
isZipInfoZipListingAvailable,
|
|
type PowerShellZipExtractor,
|
|
listZipEntriesWithPowerShell,
|
|
listZipEntriesWithPython,
|
|
listZipEntriesWithTar,
|
|
listZipEntriesWithZipInfo,
|
|
} from "./zip-entry-listing"
|
|
|
|
const WINDOWS_BUILD_WITH_TAR = 17134
|
|
|
|
function getWindowsBuildNumber(): number | null {
|
|
if (process.platform !== "win32") return null
|
|
|
|
const parts = release().split(".")
|
|
if (parts.length >= 3) {
|
|
const build = parseInt(parts[2], 10)
|
|
if (!isNaN(build)) return build
|
|
}
|
|
return null
|
|
}
|
|
|
|
function isPwshAvailable(): boolean {
|
|
if (process.platform !== "win32") return false
|
|
const result = spawnSync(["where", "pwsh"], { stdout: "pipe", stderr: "pipe" })
|
|
return result.exitCode === 0
|
|
}
|
|
|
|
function escapePowerShellPath(path: string): string {
|
|
return path.replace(/'/g, "''")
|
|
}
|
|
|
|
function getWindowsZipExtractor(): "tar" | PowerShellZipExtractor {
|
|
const buildNumber = getWindowsBuildNumber()
|
|
|
|
if (buildNumber !== null && buildNumber >= WINDOWS_BUILD_WITH_TAR) {
|
|
return "tar"
|
|
}
|
|
|
|
if (isPwshAvailable()) {
|
|
return "pwsh"
|
|
}
|
|
|
|
return "powershell"
|
|
}
|
|
|
|
export async function extractZip(archivePath: string, destDir: string): Promise<void> {
|
|
const entries = await listZipEntries(archivePath)
|
|
validateArchiveEntries(entries, destDir)
|
|
|
|
let proc
|
|
|
|
if (process.platform === "win32") {
|
|
const extractor = getWindowsZipExtractor()
|
|
|
|
switch (extractor) {
|
|
case "tar":
|
|
proc = spawn(["tar", "-xf", archivePath, "-C", destDir], {
|
|
stdout: "ignore",
|
|
stderr: "pipe",
|
|
})
|
|
break
|
|
case "pwsh":
|
|
proc = spawn(["pwsh", "-Command", `Expand-Archive -Path '${escapePowerShellPath(archivePath)}' -DestinationPath '${escapePowerShellPath(destDir)}' -Force`], {
|
|
stdout: "ignore",
|
|
stderr: "pipe",
|
|
})
|
|
break
|
|
case "powershell":
|
|
default:
|
|
proc = spawn(["powershell", "-Command", `Expand-Archive -Path '${escapePowerShellPath(archivePath)}' -DestinationPath '${escapePowerShellPath(destDir)}' -Force`], {
|
|
stdout: "ignore",
|
|
stderr: "pipe",
|
|
})
|
|
break
|
|
}
|
|
} else {
|
|
proc = spawn(["unzip", "-o", archivePath, "-d", destDir], {
|
|
stdout: "ignore",
|
|
stderr: "pipe",
|
|
})
|
|
}
|
|
|
|
const exitCode = await proc.exited
|
|
|
|
if (exitCode !== 0) {
|
|
const stderr = await new Response(proc.stderr).text()
|
|
throw new Error(`zip extraction failed (exit ${exitCode}): ${stderr}`)
|
|
}
|
|
}
|
|
|
|
async function listZipEntries(archivePath: string) {
|
|
if (process.platform === "win32") {
|
|
const extractor = getWindowsZipExtractor()
|
|
if (extractor === "tar") {
|
|
return listZipEntriesWithTar(archivePath)
|
|
}
|
|
|
|
return listZipEntriesWithPowerShell(archivePath, escapePowerShellPath, extractor)
|
|
}
|
|
|
|
if (isPythonZipListingAvailable()) {
|
|
return listZipEntriesWithPython(archivePath)
|
|
}
|
|
|
|
if (isZipInfoZipListingAvailable()) {
|
|
return listZipEntriesWithZipInfo(archivePath)
|
|
}
|
|
|
|
throw new Error(
|
|
"zip entry listing requires either python3 or zipinfo to inspect the archive safely"
|
|
)
|
|
}
|