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:
BastiaanandClaude Opus 5.5 authored and GitHub committed 2026-10-17 21:44:19 +03:00
1 parent e810020db5
commit c28e617b22
23 files changed
+976 -298

No files matched your search

+36 -1
View File
@@ -1,5 +1,6 @@
import assert from "node:assert/strict";
import { compile } from "@clusterio/lib";
import yazl from "yazl";
import { compile, zipOutputStream } from "@clusterio/lib";
/**
* Generate a flat array of tests from a matrix of inputs.
@@ -55,3 +56,37 @@ export function externalTest(test) {
}
test.timeout(60000);
}
/**
* Create a zip file in memory
*
* @param {Iterable<[string, string | Buffer]>} files -
* Paths and content of the files to add, paths ending with / are directories.
* @returns {yazl.ZipFile} zip file with the output stream ready to be read.
*/
export function createZip(files) {
const zip = new yazl.ZipFile();
for (const [filePath, content] of files) {
if (filePath.endsWith("/")) {
zip.addEmptyDirectory(filePath);
} else {
zip.addBuffer(Buffer.from(content), filePath);
}
}
zip.end();
return zip;
}
/**
* Create a zip file in memory and return its content
*
* @param {Iterable<[string, string | Buffer]>} files - Paths and content of the files to add.
* @returns {Promise<Buffer>} content of the zip file.
*/
export async function createZipBuffer(files) {
const chunks = [];
for await (const chunk of zipOutputStream(createZip(files))) {
chunks.push(chunk);
}
return Buffer.concat(chunks);
}
+31 -8
View File
@@ -1,6 +1,5 @@
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import JSZip from "jszip";
import path from "node:path";
import * as lib from "@clusterio/lib";
@@ -8,6 +7,7 @@ import {
createScenario, createScenarioCommand, handleCreateScenarioCommand,
} from "@clusterio/host/dist/node/src/create_scenario.js";
import { SaveModule, PatchInfo } from "@clusterio/host/dist/node/src/patch.js";
import { createZipBuffer } from "../common.js";
describe("host/create_scenario", function() {
@@ -48,14 +48,15 @@ describe("host/create_scenario", function() {
before(async function() {
await fs.rm(tempDir, { recursive: true, force: true });
await fs.mkdir(path.join(tempDir, "scenario"), { recursive: true });
const zip = new JSZip();
for (const [name, content] of scenarioFiles) {
const fsPath = path.join(tempDir, "scenario", name);
await fs.mkdir(path.dirname(fsPath), { recursive: true });
await fs.writeFile(fsPath, content);
zip.file(`scenario/${name}`, content);
}
await fs.writeFile(path.join(tempDir, "scenario.zip"), await zip.generateAsync({ type: "nodebuffer" }));
await fs.writeFile(
path.join(tempDir, "scenario.zip"),
await createZipBuffer([...scenarioFiles].map(([name, content]) => [`scenario/${name}`, content])),
);
});
it("should patch a scenario directory", async function() {
@@ -73,12 +74,16 @@ describe("host/create_scenario", function() {
it("should write a zip file when asked to", async function() {
const output = path.join(tempDir, "zipped.zip");
await createScenario(path.join(tempDir, "scenario"), output, [module], true);
const zip = await JSZip.loadAsync(await fs.readFile(output));
const zip = await lib.ZipArchive.fromFile(output);
const files = new Map();
for (const file of Object.values(zip.files)) {
if (!file.dir) {
files.set(file.name.replace(/^scenario\//, ""), await file.async("string"));
try {
for (const entry of zip.entries.values()) {
if (!entry.fileName.endsWith("/")) {
files.set(entry.fileName.replace(/^scenario\//, ""), (await zip.readEntry(entry)).toString());
}
}
} finally {
zip.close();
}
checkOutput(files);
});
@@ -105,6 +110,24 @@ describe("host/create_scenario", function() {
);
});
it("should throw if a scenario file is unreadable", async function() {
if (process.platform === "win32" || process.getuid?.() === 0) {
this.skip();
}
const scenario = path.join(tempDir, "unreadable");
await fs.cp(path.join(tempDir, "scenario"), scenario, { recursive: true });
await fs.writeFile(path.join(scenario, "unreadable.lua"), "");
await fs.chmod(path.join(scenario, "unreadable.lua"), 0o000);
for (let i = 0; i < 20; i++) {
await fs.mkdir(path.join(scenario, `dir${i}`));
await fs.writeFile(path.join(scenario, `dir${i}`, "file.lua"), "");
}
await assert.rejects(
createScenario(scenario, path.join(tempDir, "from_unreadable"), [module]),
{ code: "EACCES" },
);
});
describe("createScenarioCommand()", function() {
it("should define the output positional and options", function() {
const positionals = [];
+5 -4
View File
@@ -15,14 +15,14 @@ describe("host/src/export", function() {
describe("exportLocale()", function() {
it("returns a flattened mapping with locale definitions", async function() {
let locale = await _exportLocale(testServer, new Map(), ["base"], "en");
let locale = await _exportLocale(testServer, new Map(), new Map(), ["base"], "en");
assert.deepEqual(locale, new Map([
["test.core-a", "1"], ["test.core-b", "2"],
["test.base-a", "1"], ["test.base-b", "2"],
]));
});
it("returns a filtered locale definitions based on mod order", async function() {
let locale = await _exportLocale(testServer, new Map(), [], "en");
let locale = await _exportLocale(testServer, new Map(), new Map(), [], "en");
assert.deepEqual(locale, new Map([
["test.core-a", "1"], ["test.core-b", "2"],
]));
@@ -34,7 +34,7 @@ describe("host/src/export", function() {
describe("loadSimpleIcon()", function() {
it("defaults icon_size to 64", async function() {
const icon = await _loadSimpleIcon(
testServer, new Map(), { icon: "__base__/graphics/icons/test-icon.png" }, 32, new Map()
testServer, new Map(), new Map(), { icon: "__base__/graphics/icons/test-icon.png" }, 32, new Map()
);
assert.equal(icon.bitmap.width, 32);
assert.equal(icon.bitmap.height, 32);
@@ -44,7 +44,8 @@ describe("host/src/export", function() {
describe("loadLayeredIcon()", function() {
it("defaults icon_size to 64", async function() {
const icon = await _loadLayeredIcon(
testServer, new Map(), { icons: [{ icon: "__base__/graphics/icons/test-icon.png" }] }, 32, new Map()
testServer, new Map(), new Map(),
{ icons: [{ icon: "__base__/graphics/icons/test-icon.png" }] }, 32, new Map(),
);
assert.equal(icon.bitmap.width, 32);
assert.equal(icon.bitmap.height, 32);
+2 -4
View File
@@ -1,10 +1,10 @@
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import JSZip from "jszip";
import path from "node:path";
import * as lib from "@clusterio/lib";
import * as patch from "@clusterio/host/dist/node/src/patch.js";
import { createZipBuffer } from "../common.js";
describe("host/patch", function() {
@@ -262,10 +262,8 @@ describe("host/patch", function() {
describe("patch()", function() {
it("should throw on unknown scenario", async function() {
let zip = new JSZip();
zip.file("world/control.lua", "-- unknown\n");
let zipPath = path.join("temp", "test", "patch.zip");
await fs.writeFile(zipPath, await zip.generateAsync({ type: "nodebuffer" }));
await fs.writeFile(zipPath, await createZipBuffer([["world/control.lua", "-- unknown\n"]]));
await assert.rejects(
patch.patch(zipPath, []),
+1 -1
View File
@@ -15,7 +15,7 @@ describe("Integration of lib/factorio/export", function() {
let testServer = new FactorioServer(factorioDir, writePath, {});
await testServer.init();
let locale = await _exportLocale(testServer, new Map(), ["base"], "en");
let locale = await _exportLocale(testServer, new Map(), new Map(), ["base"], "en");
assert(locale instanceof Map, "locale is not a map");
assert.equal(locale.get("entity-name.fish"), "Fish");
+21 -15
View File
@@ -1,6 +1,4 @@
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import JSZip from "jszip";
import path from "node:path";
import { patch, SaveModule } from "@clusterio/host/dist/node/src/patch.js";
@@ -23,23 +21,31 @@ describe("Integration of lib/factorio/patch", function() {
await subdirModule.loadFiles("test/file/modules/subdir");
await patch(savePath, [testModule, subdirModule]);
let zip = await JSZip.loadAsync(await fs.readFile(savePath));
assert.equal(await zip.file("test/modules/test/test.lua").async("string"), "-- test\n");
assert.equal(await zip.file("test/modules/subdir/dir/test.lua").async("string"), "-- test\n");
assert.equal(
await zip.file("test/locale/en/test.cfg").async("string"),
"module-test=A Test\n"
);
assert.equal(
await zip.file("test/locale/en/test-locale.cfg").async("string"),
"module-test-locale=Test Locale\n"
);
let zip = await lib.ZipArchive.fromFile(savePath);
try {
assert.equal((await zip.readFile("test/modules/test/test.lua")).toString(), "-- test\n");
assert.equal((await zip.readFile("test/modules/subdir/dir/test.lua")).toString(), "-- test\n");
assert.equal(
(await zip.readFile("test/locale/en/test.cfg")).toString(),
"module-test=A Test\n"
);
assert.equal(
(await zip.readFile("test/locale/en/test-locale.cfg")).toString(),
"module-test-locale=Test Locale\n"
);
} finally {
zip.close();
}
});
it("should remove old modules in a save", async function() {
slowTest(this);
await patch(savePath, []);
let zip = await JSZip.loadAsync(await fs.readFile(savePath));
assert.equal(zip.file("test/modules/test/test.lua"), null);
let zip = await lib.ZipArchive.fromFile(savePath);
try {
assert.equal(zip.file("test/modules/test/test.lua"), null);
} finally {
zip.close();
}
});
});
});
+10 -13
View File
@@ -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}` };
+42
View File
@@ -0,0 +1,42 @@
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import path from "node:path";
import * as libBuildMod from "@clusterio/lib/build_mod.js";
describe("lib/build_mod", function() {
describe("build()", function() {
const tempDir = path.join("temp", "test", "build_mod");
const sourceDir = path.join(tempDir, "unreadable_mod");
const outputDir = path.join(tempDir, "output");
before(async function() {
if (process.platform === "win32" || process.getuid?.() === 0) {
this.skip();
}
await fs.rm(tempDir, { recursive: true, force: true });
await fs.mkdir(sourceDir, { recursive: true });
await fs.writeFile(
path.join(sourceDir, "info.json"),
JSON.stringify({ name: "unreadable_mod", version: "1.0.0" }),
);
await fs.writeFile(path.join(sourceDir, "unreadable.lua"), "");
await fs.chmod(path.join(sourceDir, "unreadable.lua"), 0o000);
for (let i = 0; i < 20; i++) {
await fs.mkdir(path.join(sourceDir, `dir${i}`));
await fs.writeFile(path.join(sourceDir, `dir${i}`, "file.lua"), "");
}
});
after(async function() {
await fs.rm(tempDir, { recursive: true, force: true });
});
it("should reject when a source file is unreadable", async function() {
await assert.rejects(
libBuildMod.build({ build: true, pack: true, sourceDir, outputDir }),
{ code: "EACCES" },
);
assert.deepEqual(await fs.readdir(outputDir), []);
});
});
});
+112 -21
View File
@@ -1,41 +1,132 @@
import assert from "node:assert/strict";
import JSZip from "jszip";
import zlib from "node:zlib";
import { findRoot } from "@clusterio/lib";
import { ZipArchive, ZipWriter, findRoot } from "@clusterio/lib";
import { createZipBuffer } from "../common.js";
describe("lib/factorio", function() {
async function readAll(stream) {
const chunks = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
return Buffer.concat(chunks);
}
describe("lib/zip_ops", function() {
describe("class ZipArchive", function() {
it("should read files from a zip", async function() {
const zip = await ZipArchive.fromBuffer(await createZipBuffer([
["root/foo.txt", "foo"],
["root/bar.txt", "bar"],
]));
assert.deepEqual([...zip.entries.keys()], ["root/foo.txt", "root/bar.txt"]);
assert.equal((await zip.readFile("root/foo.txt")).toString(), "foo");
assert.equal(await zip.readFile("root/missing.txt"), null);
assert.equal(zip.file("root/missing.txt"), null);
assert.equal(zip.file("root/bar.txt").fileName, "root/bar.txt");
});
it("should reject invalid zips", async function() {
await assert.rejects(ZipArchive.fromBuffer(Buffer.from("not a zip file")));
});
});
describe("class ZipWriter", function() {
const sourceFiles = [
["root/", ""],
["root/foo.txt", "foo"],
["root/big.txt", "lorem ipsum ".repeat(10000)],
["root/ünïcode.txt", "unicode"],
];
async function roundTrip(forceZip64) {
const source = await ZipArchive.fromBuffer(await createZipBuffer(sourceFiles));
const writer = new ZipWriter({ forceZip64 });
const output = readAll(writer.outputStream);
for (const entry of source.entries.values()) {
await writer.addEntry(source, entry);
}
await writer.addBuffer(Buffer.from("added"), "root/added.txt", { mtime: new Date(2020, 0, 2, 3, 4, 6) });
await writer.end();
return { source, copy: await ZipArchive.fromBuffer(await output) };
}
async function assertRoundTrip(forceZip64) {
const { source, copy } = await roundTrip(forceZip64);
assert.deepEqual([...copy.entries.keys()], [...source.entries.keys(), "root/added.txt"]);
for (const [name, sourceEntry] of source.entries) {
const entry = copy.entries.get(name);
assert.equal(entry.compressionMethod, sourceEntry.compressionMethod, name);
assert.equal(entry.compressedSize, sourceEntry.compressedSize, name);
assert.equal(entry.uncompressedSize, sourceEntry.uncompressedSize, name);
assert.equal(entry.crc32, sourceEntry.crc32, name);
assert.deepEqual(entry.getLastModDate(), sourceEntry.getLastModDate({ forceDosFormat: true }), name);
if (!name.endsWith("/")) {
const content = await copy.readEntry(entry);
assert.deepEqual(content, await source.readEntry(sourceEntry), name);
assert.equal(zlib.crc32(content), entry.crc32, name);
}
}
const added = copy.entries.get("root/added.txt");
assert.equal((await copy.readEntry(added)).toString(), "added");
assert.equal(zlib.crc32("added"), added.crc32);
assert.deepEqual(added.getLastModDate(), new Date(2020, 0, 2, 3, 4, 6));
assert.equal(added.versionNeededToExtract, forceZip64 ? 45 : 20);
}
it("should copy entries and add files", async function() {
await assertRoundTrip(false);
});
it("should write ZIP64 structures when forced", async function() {
await assertRoundTrip(true);
});
it("should reject writes after the output stream is destroyed", async function() {
const writer = new ZipWriter();
writer.outputStream.on("error", () => {});
writer.outputStream.destroy(new Error("Test error"));
await assert.rejects(writer.addBuffer(Buffer.from(""), "foo.txt"), new Error("Test error"));
await assert.rejects(writer.end(), new Error("Test error"));
});
it("should fail when the output stream is destroyed while waiting for drain", async function() {
const writer = new ZipWriter();
writer.outputStream.pause();
const adding = writer.addBuffer(Buffer.alloc(1024 * 1024), "foo.txt");
writer.outputStream.destroy();
await assert.rejects(adding, new Error("Zip output stream was closed"));
});
});
describe("findRoot()", function() {
it("should find the root of a zip", function() {
let zip = new JSZip();
zip.file("root/foo.txt", "");
zip.file("root/bar.txt", "");
it("should find the root of a zip", async function() {
const zip = await ZipArchive.fromBuffer(await createZipBuffer([
["root/foo.txt", ""],
["root/bar.txt", ""],
]));
assert.equal(findRoot(zip), "root");
});
it("should throw if the first file is at the root of the zip", function() {
let zip = new JSZip();
zip.file("bar.txt", "");
zip.file("root/foo.txt", "");
it("should throw if the first file is at the root of the zip", async function() {
const zip = await ZipArchive.fromBuffer(await createZipBuffer([
["bar.txt", ""],
["root/foo.txt", ""],
]));
assert.throws(
() => findRoot(zip),
new Error("Zip contains file 'bar.txt' in root dir")
);
});
it("should return directory of first file if there are multiple root dirs", function() {
let zip = new JSZip();
zip.file("root-1/foo.txt", "");
zip.file("root-2/bar.txt", "");
it("should return directory of first file if there are multiple root dirs", async function() {
const zip = await ZipArchive.fromBuffer(await createZipBuffer([
["root-1/foo.txt", ""],
["root-2/bar.txt", ""],
]));
assert.equal(findRoot(zip), "root-1");
});
it("should throw if given an empty zip file", function() {
let zip = new JSZip();
it("should throw if given an empty zip file", async function() {
const zip = await ZipArchive.fromBuffer(await createZipBuffer([]));
assert.throws(
() => findRoot(zip),
new Error("Empty zip file")