2026-05-05 22:41:45 +09:00
|
|
|
import { spawn, spawnSync } from "./bun-spawn-shim"
|
2026-01-11 18:21:48 +09:00
|
|
|
import { release } from "os"
|
|
|
|
|
|
2026-04-02 15:01:15 +09:00
|
|
|
import { validateArchiveEntries } from "./archive-entry-validator"
|
2026-04-03 17:23:09 +09:00
|
|
|
import {
|
2026-04-03 17:21:07 +09:00
|
|
|
isPythonZipListingAvailable,
|
|
|
|
|
isZipInfoZipListingAvailable,
|
|
|
|
|
type PowerShellZipExtractor,
|
|
|
|
|
listZipEntriesWithPowerShell,
|
|
|
|
|
listZipEntriesWithPython,
|
|
|
|
|
listZipEntriesWithTar,
|
|
|
|
|
listZipEntriesWithZipInfo,
|
2026-04-03 17:23:09 +09:00
|
|
|
} from "./zip-entry-listing"
|
2026-04-02 15:01:15 +09:00
|
|
|
|
2026-01-11 18:21:48 +09:00
|
|
|
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, "''")
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 17:21:07 +09:00
|
|
|
function getWindowsZipExtractor(): "tar" | PowerShellZipExtractor {
|
2026-01-11 18:21:48 +09:00
|
|
|
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> {
|
2026-04-02 15:01:15 +09:00
|
|
|
const entries = await listZipEntries(archivePath)
|
|
|
|
|
validateArchiveEntries(entries, destDir)
|
|
|
|
|
|
2026-01-11 18:21:48 +09:00
|
|
|
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}`)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-02 15:01:15 +09:00
|
|
|
|
|
|
|
|
async function listZipEntries(archivePath: string) {
|
2026-04-03 17:21:07 +09:00
|
|
|
if (process.platform === "win32") {
|
|
|
|
|
const extractor = getWindowsZipExtractor()
|
2026-04-02 15:01:15 +09:00
|
|
|
if (extractor === "tar") {
|
|
|
|
|
return listZipEntriesWithTar(archivePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return listZipEntriesWithPowerShell(archivePath, escapePowerShellPath, extractor)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 17:21:07 +09:00
|
|
|
if (isPythonZipListingAvailable()) {
|
|
|
|
|
return listZipEntriesWithPython(archivePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isZipInfoZipListingAvailable()) {
|
|
|
|
|
return listZipEntriesWithZipInfo(archivePath)
|
|
|
|
|
}
|
2026-04-03 17:23:09 +09:00
|
|
|
|
2026-04-03 17:21:07 +09:00
|
|
|
throw new Error(
|
|
|
|
|
"zip entry listing requires either python3 or zipinfo to inspect the archive safely"
|
|
|
|
|
)
|
2026-04-02 15:01:15 +09:00
|
|
|
}
|