mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 07:04:52 +09:00
* Replace jszip with yauzl and yazl jszip loads a whole archive into memory to read it and buffers the output when writing. Saves and mods can be several hundred megabytes, so read zips with yauzl, which only reads the central directory and streams entries on demand, and write them with yazl, which streams the output. lib gains a ZipArchive wrapper around yauzl with the lookup helpers the callers need, findRoot() now takes it instead of a JSZip. Saves are patched by streaming every kept entry into a new zip, mod info.json is read straight from the file, exports are streamed to the controller and Factorio zip downloads go through a temp file instead of a buffer. Patching now inflates and deflates each entry again where jszip reused the compressed data, on a 336 MB synthetic save that is 7 s instead of 0.7 s, with peak memory going from 416 MB to 151 MB. Closes #1002 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * Copy save entries without recompressing them yazl inflates and deflates every entry it is given, so patching a save took ten times longer than with jszip, which reused the compressed data. Add a ZipWriter to lib that streams entries out as they are added and copies entries from a ZipArchive byte for byte, with ZIP64 structures when sizes or offsets need them, and use it in patch(). On a 336 MB synthetic save patching now takes 0.4 s at 143 MB peak RSS against 0.75 s at 417 MB with jszip. Factorio loads the result. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * Handle save write errors before the patch loop awaits them A write error on the temporary save, such as a full disk, rejected the pipeline promise while addEntry() or addBuffer() was still pending. Nothing was listening yet, so the host's unhandledRejection handler exited the process instead of failing the instance start. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep the export zip cache local to each export The cache was module level, so when two instances on one host exported at once, the first to finish closed archives the other was still reading. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Start writing zips before adding files from disk yazl reads files passed to addFile() as soon as they are added. An unreadable file emitted an error while the directory traversal was still running, before the output pipeline existed, and the process exited on the uncaught error. build_mod and zipDirectory now start the pipeline first and reject instead. build_mod also removes the partial zip on failure. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
163 lines
4.8 KiB
JavaScript
163 lines
4.8 KiB
JavaScript
/* eslint-disable no-console */
|
|
import fs from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { pipeline } from "node:stream/promises";
|
|
import yargs from "yargs";
|
|
import yazl from "yazl";
|
|
|
|
|
|
async function addModFiles(args, info, modName, zip) {
|
|
for await (const dirent of await fs.opendir(args.sourceDir, { recursive: true })) {
|
|
if (dirent.isFile()) {
|
|
const itemPath = path.join(dirent.parentPath, dirent.name);
|
|
// On Windows the path created uses backslashes as the directory sepparator
|
|
// but the zip file needs to use forward slashes. We can't use the posix
|
|
// version of relative here as it doesn't work with Windows style paths.
|
|
let basePath = path.relative(args.sourceDir, itemPath).replace(/\\/g, "/");
|
|
if (basePath === "info.json") {
|
|
// The info.json file is overridden later.
|
|
continue;
|
|
}
|
|
zip.addFile(itemPath, path.posix.join(modName, basePath));
|
|
}
|
|
}
|
|
|
|
for (let [fileName, pathParts] of Object.entries(info.additional_files || {})) {
|
|
let filePath = path.join(args.sourceDir, ...pathParts);
|
|
try {
|
|
await fs.access(filePath);
|
|
} catch (err) {
|
|
throw new Error(`Error reading additional file ${filePath}`, { cause: err });
|
|
}
|
|
zip.addFile(filePath, path.posix.join(modName, fileName));
|
|
}
|
|
delete info.additional_files;
|
|
|
|
zip.addBuffer(Buffer.from(JSON.stringify(info, null, "\t")), path.posix.join(modName, "info.json"));
|
|
}
|
|
|
|
async function buildMod(args, info, modName) {
|
|
if (args.build) {
|
|
await fs.mkdir(args.outputDir, { recursive: true });
|
|
modName = args.modName ?? `${info.name}_${info.version}`;
|
|
|
|
if (args.pack) {
|
|
let modPath = path.join(args.outputDir, `${modName}.zip`);
|
|
console.log(`Writing ${modPath}`);
|
|
let zip = new yazl.ZipFile();
|
|
// yazl reports errors on the zip object, not the output stream.
|
|
zip.on("error", err => zip.outputStream.destroy(err));
|
|
// addFile() can fail before the traversal is done, so start writing first.
|
|
const writing = pipeline(zip.outputStream, (await fs.open(modPath, "w")).createWriteStream());
|
|
writing.catch(() => {});
|
|
try {
|
|
await addModFiles(args, info, modName, zip);
|
|
zip.end();
|
|
await writing;
|
|
} catch (err) {
|
|
zip.outputStream.destroy();
|
|
await writing.catch(() => {});
|
|
await fs.rm(modPath, { force: true });
|
|
throw err;
|
|
}
|
|
|
|
} else {
|
|
let modDir = path.join(args.outputDir, modName);
|
|
console.log(`Removing potentially existing build ${modDir}`);
|
|
await fs.rm(modDir, { force: true, recursive: true, maxRetries: 10 });
|
|
console.log(`Building ${modDir}`);
|
|
await fs.cp(args.sourceDir, modDir, {
|
|
errorOnExists: true,
|
|
mode: fs.constants.COPYFILE_FICLONE,
|
|
recursive: true,
|
|
});
|
|
for (let [fileName, pathParts] of Object.entries(info.additional_files) || []) {
|
|
let filePath = path.join(...pathParts);
|
|
await fs.copyFile(
|
|
filePath,
|
|
path.join(modDir, fileName),
|
|
fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE,
|
|
);
|
|
}
|
|
delete info.additional_files;
|
|
|
|
await fs.writeFile(path.join(modDir, "info.json"), JSON.stringify(info, null, "\t"));
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function build(args) {
|
|
let info = JSON.parse(await fs.readFile(path.join(args.sourceDir, "info.json")));
|
|
|
|
if (args.factorioVersion) {
|
|
info.factorio_version = args.factorioVersion;
|
|
}
|
|
|
|
if (args.dependencies) {
|
|
info.dependencies = args.dependencies;
|
|
}
|
|
|
|
if (args.clean) {
|
|
let splitter = /^(.*)_(\d+\.\d+\.\d+)(\.zip)?$/;
|
|
for (let entry of await fs.readdir(args.outputDir)) {
|
|
let match = splitter.exec(entry);
|
|
if (match) {
|
|
let name = match[1];
|
|
if (name === info.name) {
|
|
let modPath = path.join(args.outputDir, entry);
|
|
console.log(`Removing ${modPath}`);
|
|
await fs.rm(modPath, { force: true, recursive: true, maxRetries: 10 });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (info.variants) {
|
|
for (let variantOverrides of info.variants) {
|
|
let variantInfo = {
|
|
...info,
|
|
...variantOverrides,
|
|
};
|
|
delete variantInfo.variants;
|
|
await buildMod(args, variantInfo);
|
|
}
|
|
} else {
|
|
await buildMod(args, info);
|
|
}
|
|
}
|
|
|
|
|
|
async function main() {
|
|
const args = yargs(process.argv.slice(2))
|
|
.scriptName("build")
|
|
.options({
|
|
"clean": { describe: "Remove previous builds", type: "boolean", default: false },
|
|
"build": { describe: "Build mod(s)", type: "boolean", default: true },
|
|
"pack": { describe: "Pack into zip file", type: "boolean", default: true },
|
|
"factorio-version": { describe: "Override factorio_version", type: "string" },
|
|
"source-dir": {
|
|
describe: "Path to mod source files",
|
|
type: "string",
|
|
nargs: 1,
|
|
normalize: true,
|
|
demandOption: true,
|
|
},
|
|
"output-dir": {
|
|
describe: "Path to output built mod(s)",
|
|
type: "string",
|
|
nargs: 1,
|
|
normalize: true,
|
|
default: "dist/factorio",
|
|
},
|
|
})
|
|
.strict()
|
|
.parse()
|
|
;
|
|
|
|
await build(args);
|
|
}
|
|
|
|
if (import.meta.main) {
|
|
main().catch(err => { console.log(err); });
|
|
}
|