mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 15:33:12 +09:00
Replace jszip with yauzl and yazl (#1011)
* 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>
This commit is contained in:
1 parent
e810020db5
commit
c28e617b22
23 files changed
+976
-298
No files matched your search
+10
-13
@@ -2,20 +2,18 @@ import assert from "node:assert/strict";
|
||||
import path from "node:path";
|
||||
import fs from "node:fs/promises";
|
||||
import stream from "node:stream";
|
||||
import JSZip from "jszip"; // Added for creating mock zips
|
||||
|
||||
// Capture native fetch before any potential mocks are applied
|
||||
const nativeFetch = global.fetch;
|
||||
|
||||
import { ModStore, ModInfo, ModVersionEquality } from "@clusterio/lib"; // Adjust path based on compiled output
|
||||
import { externalTest } from "../common.js";
|
||||
import { ModStore, ModInfo, ModVersionEquality, zipOutputStream } from "@clusterio/lib";
|
||||
import { externalTest, createZip, createZipBuffer } from "../common.js";
|
||||
import "../setup_logging.js";
|
||||
|
||||
const MODS_DIR = path.join("temp", "test", "mod_store", "mods");
|
||||
const CACHE_FILE = path.join(MODS_DIR, "mod-info-cache.json");
|
||||
|
||||
function createMockModJSZip(name, version, factorioVersion = "1.1", dependencies = ["base >= 1.1"]) {
|
||||
const zip = new JSZip();
|
||||
function mockModFiles(name, version, factorioVersion = "1.1", dependencies = ["base >= 1.1"]) {
|
||||
const infoJson = {
|
||||
name: name,
|
||||
version: version,
|
||||
@@ -25,16 +23,15 @@ function createMockModJSZip(name, version, factorioVersion = "1.1", dependencies
|
||||
dependencies: dependencies,
|
||||
description: `Description for ${name}`,
|
||||
};
|
||||
zip.file(`${name}/info.json`, JSON.stringify(infoJson, null, 4));
|
||||
zip.file(`${name}/dummy.lua`, "-- Mock Lua file");
|
||||
|
||||
return zip;
|
||||
return [
|
||||
[`${name}/info.json`, JSON.stringify(infoJson, null, 4)],
|
||||
[`${name}/dummy.lua`, "-- Mock Lua file"],
|
||||
];
|
||||
}
|
||||
|
||||
// Helper function to create a mock mod zip file
|
||||
async function createMockModZip(filePath, name, version, factorioVersion = "1.1", dependencies = ["base >= 1.1"]) {
|
||||
const zip = createMockModJSZip(name, version, factorioVersion, dependencies);
|
||||
const buffer = await zip.generateAsync({ type: "nodebuffer", compression: "DEFLATE" });
|
||||
const buffer = await createZipBuffer(mockModFiles(name, version, factorioVersion, dependencies));
|
||||
await fs.writeFile(filePath, buffer);
|
||||
}
|
||||
|
||||
@@ -254,8 +251,8 @@ describe("lib/ModStore", function () {
|
||||
if (name === "bad-download-mod") {
|
||||
return { ok: false, status: 503, statusText: "Test Mock download error" };
|
||||
}
|
||||
const zip = createMockModJSZip(name, version);
|
||||
return { ok: true, status: 200, body: stream.Readable.toWeb(zip.generateNodeStream()) };
|
||||
const zip = createZip(mockModFiles(name, version));
|
||||
return { ok: true, status: 200, body: stream.Readable.toWeb(zipOutputStream(zip)) };
|
||||
}
|
||||
// Fallback for unhandled fetches in this test
|
||||
return { ok: false, status: 404, statusText: `Test Mock 404: ${urlString}` };
|
||||
|
||||
Reference in new issue
Block a user