Refactor zstd decompression logic in expand-windows-installer.mjs
All checks were successful
farmcontrol/farmcontrol-server/pipeline/head This commit looks good

- Introduced a new approach for finding zstd decompression commands, prioritizing a zig-zstd executable over the system zstd for improved compatibility.
- Enhanced error messages during decompression to include command details for better debugging.
- Streamlined the process of copying and decompressing archives, ensuring clearer path management and improved reliability on Windows platforms.
This commit is contained in:
Tom Butcher 2026-08-01 22:16:27 +01:00
parent 4307d74e92
commit 0e19d25a92

View File

@ -1,4 +1,5 @@
import { import {
cpSync,
existsSync, existsSync,
mkdirSync, mkdirSync,
readdirSync, readdirSync,
@ -13,27 +14,8 @@ const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..")
const electrobunDir = path.join(rootDir, "node_modules/electrobun"); const electrobunDir = path.join(rootDir, "node_modules/electrobun");
function findZstdDecompressCommand() { function findZstdDecompressCommand() {
const spawnOpts = process.platform === "win32" ? { shell: true } : {};
const systemZstd = spawnSync("zstd", ["--version"], {
encoding: "utf8",
...spawnOpts,
});
if (systemZstd.status === 0) {
return {
command: "zstd",
args: (inputPath, outputPath) => [
"-d",
"-f",
"-o",
outputPath,
inputPath,
],
};
}
const hostArch = process.arch === "arm64" ? "arm64" : "x64"; const hostArch = process.arch === "arm64" ? "arm64" : "x64";
const candidates = [ const zigCandidates = [
path.join(electrobunDir, `dist-win-${hostArch}`, "zig-zstd.exe"), path.join(electrobunDir, `dist-win-${hostArch}`, "zig-zstd.exe"),
path.join(electrobunDir, "dist-win-x64", "zig-zstd.exe"), path.join(electrobunDir, "dist-win-x64", "zig-zstd.exe"),
path.join(electrobunDir, `dist-macos-${hostArch}`, "zig-zstd"), path.join(electrobunDir, `dist-macos-${hostArch}`, "zig-zstd"),
@ -44,12 +26,13 @@ function findZstdDecompressCommand() {
path.join(electrobunDir, "vendors", "zig-zstd", "arm64", "zig-zstd.exe"), path.join(electrobunDir, "vendors", "zig-zstd", "arm64", "zig-zstd.exe"),
]; ];
for (const candidate of candidates) { for (const candidate of zigCandidates) {
if (!existsSync(candidate)) { if (!existsSync(candidate)) {
continue; continue;
} }
return { return {
kind: "zig-zstd",
command: candidate, command: candidate,
args: (inputPath, outputPath) => [ args: (inputPath, outputPath) => [
"decompress", "decompress",
@ -62,6 +45,24 @@ function findZstdDecompressCommand() {
}; };
} }
const systemZstd = spawnSync("zstd", ["--version"], {
encoding: "utf8",
shell: process.platform === "win32",
});
if (systemZstd.status === 0) {
return {
kind: "system-zstd",
command: "zstd",
args: (inputPath, outputPath) => [
"-d",
"-f",
"-o",
outputPath,
inputPath,
],
};
}
return null; return null;
} }
@ -73,14 +74,23 @@ function decompressTarZst(inputPath, outputPath) {
); );
} }
const result = spawnSync(zstd.command, zstd.args(inputPath, outputPath), { const resolvedInput = path.resolve(inputPath);
const resolvedOutput = path.resolve(outputPath);
const args = zstd.args(resolvedInput, resolvedOutput);
console.log(
`expand-windows-installer: decompressing with ${zstd.command} (${zstd.kind})`,
);
const result = spawnSync(zstd.command, args, {
stdio: "inherit", stdio: "inherit",
shell: process.platform === "win32", shell: false,
windowsHide: true,
}); });
if (result.status !== 0) { if (result.status !== 0) {
throw new Error( throw new Error(
`expand-windows-installer: decompression failed (exit ${result.status ?? 1})`, `expand-windows-installer: decompression failed (exit ${result.status ?? 1}) using ${zstd.command} ${args.join(" ")}`,
); );
} }
} }
@ -97,11 +107,14 @@ export function expandWindowsAppFromArchive(setupArchivePath, parentDir) {
rmSync(workDir, { recursive: true, force: true }); rmSync(workDir, { recursive: true, force: true });
mkdirSync(workDir, { recursive: true }); mkdirSync(workDir, { recursive: true });
decompressTarZst(resolvedArchive, tarPath); const archiveForDecompress = path.join(workDir, "setup.tar.zst");
cpSync(resolvedArchive, archiveForDecompress);
decompressTarZst(archiveForDecompress, tarPath);
const extractTar = spawnSync("tar", ["-xf", tarPath, "-C", workDir], { const extractTar = spawnSync("tar", ["-xf", tarPath, "-C", workDir], {
stdio: "inherit", stdio: "inherit",
shell: process.platform === "win32", shell: false,
windowsHide: true,
}); });
if (extractTar.status !== 0) { if (extractTar.status !== 0) {