mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 15: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>
93 lines
2.6 KiB
JavaScript
93 lines
2.6 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import yazl from "yazl";
|
|
import { compile, zipOutputStream } from "@clusterio/lib";
|
|
|
|
/**
|
|
* Generate a flat array of tests from a matrix of inputs.
|
|
*
|
|
* @template {any[][]} T
|
|
* @param {T} arrays - A list of arrays representing the different values for each argument
|
|
* @returns {Array<{ [K in keyof T]: T[K][number] }>}
|
|
* An array of tuples, each containing one value from each input array.
|
|
*/
|
|
export function testMatrix(...arrays) {
|
|
return arrays.reduce((acc, curr) => acc.flatMap(a => curr.map(b => [...a, b])), [[]]);
|
|
}
|
|
|
|
/**
|
|
* Test that a class is round trip json serialisable across multiple test cases.
|
|
*
|
|
* @template {any[]} T - Constructor arguments
|
|
* @param {{new(...args: T): object}} Class - The class which has toJSON and fromJSON methods.
|
|
* @param {T[]} tests - The tests inputs to pass to the class constructor.
|
|
*/
|
|
export function testRoundTripJsonSerialisable(Class, tests) {
|
|
const validate = compile(Class.jsonSchema);
|
|
for (const test of tests) {
|
|
const original = new Class(...test);
|
|
const serialised = JSON.stringify(original);
|
|
const jsonObject = JSON.parse(serialised);
|
|
const reconstructed = Class.fromJSON(jsonObject);
|
|
assert.deepEqual(reconstructed, original, JSON.stringify(test));
|
|
if (!validate(jsonObject)) {
|
|
throw validate.errors;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Mark that this test takes a lot of time, or depends on a test that takes a
|
|
* lot of time.
|
|
*/
|
|
export function slowTest(test) {
|
|
if (process.env.FAST_TEST) {
|
|
test.skip();
|
|
}
|
|
test.timeout(30000);
|
|
}
|
|
|
|
/**
|
|
* Mark that this test depends on an external API and may be slow or flaky.
|
|
*/
|
|
export function externalTest(test) {
|
|
if (process.env.NO_EXTERNAL_TEST) {
|
|
test.skip();
|
|
}
|
|
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);
|
|
}
|