mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-25 00:23:12 +09:00
Merge pull request #1236 from bbassie/chore/web-bundle-zip-zlib
Keep yauzl, browserify-zlib and assert out of the web interface
This commit is contained in:
16 files changed
+146
-74
No files matched your search
@@ -828,7 +828,7 @@ async function uploadMod(req: Request, res: Response) {
|
|||||||
// Pipe does not pass on an upload that was cut short.
|
// Pipe does not pass on an upload that was cut short.
|
||||||
await Promise.all([finished(file.stream), finished(writeStream)]);
|
await Promise.all([finished(file.stream), finished(writeStream)]);
|
||||||
|
|
||||||
const modInfo = await lib.ModInfo.fromModFile(tempFilePath);
|
const modInfo = await lib.ModStore.readModInfo(tempFilePath);
|
||||||
checkModName(modInfo.filename);
|
checkModName(modInfo.filename);
|
||||||
await fs.rename(tempFilePath, path.join(modsDirectory, modInfo.filename));
|
await fs.rename(tempFilePath, path.join(modsDirectory, modInfo.filename));
|
||||||
req.app.locals.controller.modStore.addMod(modInfo);
|
req.app.locals.controller.modStore.addMod(modInfo);
|
||||||
|
|||||||
@@ -31,6 +31,13 @@
|
|||||||
},
|
},
|
||||||
"import": "./src/link/WebSocket.browser.ts"
|
"import": "./src/link/WebSocket.browser.ts"
|
||||||
},
|
},
|
||||||
|
"#zlib_sync": {
|
||||||
|
"node": {
|
||||||
|
"types": "./src/zlib_sync.node.ts",
|
||||||
|
"default": "./dist/node/src/zlib_sync.node.js"
|
||||||
|
},
|
||||||
|
"import": "./src/zlib_sync.browser.ts"
|
||||||
|
},
|
||||||
"#is_deep_strict_equal": {
|
"#is_deep_strict_equal": {
|
||||||
"node": {
|
"node": {
|
||||||
"types": "./src/is_deep_strict_equal.node.ts",
|
"types": "./src/is_deep_strict_equal.node.ts",
|
||||||
@@ -53,6 +60,7 @@
|
|||||||
"@sinclair/typebox": "catalog:",
|
"@sinclair/typebox": "catalog:",
|
||||||
"ajv": "^8.20.0",
|
"ajv": "^8.20.0",
|
||||||
"fast-deep-equal": "^3.1.3",
|
"fast-deep-equal": "^3.1.3",
|
||||||
|
"fflate": "catalog:",
|
||||||
"set-blocking": "catalog:",
|
"set-blocking": "catalog:",
|
||||||
"triple-beam": "^1.4.1",
|
"triple-beam": "^1.4.1",
|
||||||
"winston": "catalog:",
|
"winston": "catalog:",
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import events from "events";
|
|||||||
import { Static } from "@sinclair/typebox";
|
import { Static } from "@sinclair/typebox";
|
||||||
import { logger } from "./logging.js";
|
import { logger } from "./logging.js";
|
||||||
import { downloadFile, atomicOverwriteFile } from "./file_ops.js";
|
import { downloadFile, atomicOverwriteFile } from "./file_ops.js";
|
||||||
|
import { hashFile } from "./hash.js";
|
||||||
|
import { ZipArchive, findRoot } from "./zip_ops.js";
|
||||||
import { ModPortalReleaseSchema, ModPortalDetailsSchema, ModNameVersionPair } from "./data/messages_mod.js";
|
import { ModPortalReleaseSchema, ModPortalDetailsSchema, ModNameVersionPair } from "./data/messages_mod.js";
|
||||||
import { ModInfo, ModPack, MajorMinorVersion, ModVersionEquality, SourceVersion } from "./data/index.js";
|
import { ModInfo, ModPack, MajorMinorVersion, ModVersionEquality, SourceVersion } from "./data/index.js";
|
||||||
|
|
||||||
@@ -54,8 +56,44 @@ export default class ModStore extends events.EventEmitter<ModStoreEvents> {
|
|||||||
|
|
||||||
mods() { return this.files.values(); }
|
mods() { return this.files.values(); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read the info.json of a mod zip file along with its size, mtime and hash
|
||||||
|
*
|
||||||
|
* @param modPath - Path to the mod zip file.
|
||||||
|
* @returns Mod info for the file.
|
||||||
|
*/
|
||||||
|
static async readModInfo(modPath: string) {
|
||||||
|
let modInfo: Static<typeof ModInfo.jsonSchema>;
|
||||||
|
let zip = await ZipArchive.fromFile(modPath);
|
||||||
|
try {
|
||||||
|
let content = await zip.readFile(path.posix.join(findRoot(zip), "info.json"));
|
||||||
|
if (content === null) {
|
||||||
|
throw new Error("Mod contains no info.json file");
|
||||||
|
}
|
||||||
|
modInfo = JSON.parse(content.toString("utf8"));
|
||||||
|
} finally {
|
||||||
|
zip.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
let valid = ModInfo.validateInfo(modInfo);
|
||||||
|
if (!valid) {
|
||||||
|
throw new Error("Mod's info.json is not valid");
|
||||||
|
}
|
||||||
|
|
||||||
|
const stat = await fs.stat(modPath);
|
||||||
|
return ModInfo.fromJSON({
|
||||||
|
...modInfo,
|
||||||
|
|
||||||
|
size: stat.size,
|
||||||
|
sha1: await hashFile(modPath),
|
||||||
|
mtime_ms: stat.mtimeMs,
|
||||||
|
updated_at_ms: stat.mtimeMs,
|
||||||
|
is_deleted: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
static async loadFile(modsDirectory: string, file: string) {
|
static async loadFile(modsDirectory: string, file: string) {
|
||||||
let modInfo = await ModInfo.fromModFile(path.join(modsDirectory, file));
|
let modInfo = await ModStore.readModInfo(path.join(modsDirectory, file));
|
||||||
if (modInfo.filename !== file) {
|
if (modInfo.filename !== file) {
|
||||||
throw new Error(`filename does not match the expected name ${modInfo.filename}.`);
|
throw new Error(`filename does not match the expected name ${modInfo.filename}.`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
import fs from "node:fs/promises";
|
|
||||||
import path from "path";
|
|
||||||
import { Type, Static } from "@sinclair/typebox";
|
import { Type, Static } from "@sinclair/typebox";
|
||||||
|
|
||||||
import * as libHash from "../hash.js";
|
|
||||||
import * as libSchema from "../schema.js";
|
import * as libSchema from "../schema.js";
|
||||||
import { ZipArchive, findRoot } from "../zip_ops.js";
|
|
||||||
import { ModRecord } from "./ModPack.js";
|
import { ModRecord } from "./ModPack.js";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -349,34 +345,4 @@ export default class ModInfo {
|
|||||||
if (this.isDeleted) { json.is_deleted = this.isDeleted; }
|
if (this.isDeleted) { json.is_deleted = this.isDeleted; }
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async fromModFile(modPath: string) {
|
|
||||||
let modInfo: Static<typeof ModInfo.jsonSchema>;
|
|
||||||
let zip = await ZipArchive.fromFile(modPath);
|
|
||||||
try {
|
|
||||||
let content = await zip.readFile(path.posix.join(findRoot(zip), "info.json"));
|
|
||||||
if (content === null) {
|
|
||||||
throw new Error("Mod contains no info.json file");
|
|
||||||
}
|
|
||||||
modInfo = JSON.parse(content.toString("utf8"));
|
|
||||||
} finally {
|
|
||||||
zip.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
let valid = this.validateInfo(modInfo);
|
|
||||||
if (!valid) {
|
|
||||||
throw new Error("Mod's info.json is not valid");
|
|
||||||
}
|
|
||||||
|
|
||||||
const stat = await fs.stat(modPath);
|
|
||||||
return this.fromJSON({
|
|
||||||
...modInfo,
|
|
||||||
|
|
||||||
size: stat.size,
|
|
||||||
sha1: await libHash.hashFile(modPath),
|
|
||||||
mtime_ms: stat.mtimeMs,
|
|
||||||
updated_at_ms: stat.mtimeMs,
|
|
||||||
is_deleted: false,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import zlib from "zlib";
|
import * as zlib from "#zlib_sync";
|
||||||
import { Type, Static } from "@sinclair/typebox";
|
import { Type, Static } from "@sinclair/typebox";
|
||||||
|
|
||||||
import * as libSchema from "../schema.js";
|
import * as libSchema from "../schema.js";
|
||||||
@@ -306,7 +306,7 @@ export default class ModPack {
|
|||||||
|
|
||||||
// eslint-disable-next-line node/no-sync
|
// eslint-disable-next-line node/no-sync
|
||||||
let buf = zlib.deflateSync(JSON.stringify(json));
|
let buf = zlib.deflateSync(JSON.stringify(json));
|
||||||
return buf.toString("base64");
|
return Buffer.from(buf).toString("base64");
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromModPackString(modPackString: string) {
|
static fromModPackString(modPackString: string) {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import zlib from "zlib";
|
import * as zlib from "#zlib_sync";
|
||||||
|
|
||||||
class MapReaderState {
|
class MapReaderState {
|
||||||
pos = 0;
|
pos = 0;
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
/** Throws if condition is falsy, node:assert is not available in the browser. */
|
||||||
|
export function assert(condition: unknown): asserts condition {
|
||||||
|
if (!condition) {
|
||||||
|
throw new Error("Assertion failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
// Connection adapters for Links
|
// Connection adapters for Links
|
||||||
import { strict as assert } from "assert";
|
|
||||||
import events from "events";
|
import events from "events";
|
||||||
|
|
||||||
import { WebSocket } from "#WebSocket";
|
import { WebSocket } from "#WebSocket";
|
||||||
@@ -7,6 +6,7 @@ import * as libData from "../data/index.js";
|
|||||||
import * as libErrors from "../errors.js";
|
import * as libErrors from "../errors.js";
|
||||||
import { logger } from "../logging.js";
|
import { logger } from "../logging.js";
|
||||||
import ExponentialBackoff from "../ExponentialBackoff.js";
|
import ExponentialBackoff from "../ExponentialBackoff.js";
|
||||||
|
import { assert } from "./assert.js";
|
||||||
import type { Request, Event } from "./link.js";
|
import type { Request, Event } from "./link.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { logger } from "../logging.js";
|
|||||||
import * as libSchema from "../schema.js";
|
import * as libSchema from "../schema.js";
|
||||||
import { dataClasses, errorClasses } from "./messages.js";
|
import { dataClasses, errorClasses } from "./messages.js";
|
||||||
import { BaseConnector, WebSocketBaseConnector } from "./connectors.js";
|
import { BaseConnector, WebSocketBaseConnector } from "./connectors.js";
|
||||||
import { strict as assert } from "assert";
|
import { assert } from "./assert.js";
|
||||||
import type { PluginNodeEnvInfo, PluginWebEnvInfo } from "../plugin.js";
|
import type { PluginNodeEnvInfo, PluginWebEnvInfo } from "../plugin.js";
|
||||||
import type {
|
import type {
|
||||||
AddressType, JSONDeserialisable, MessageRoutable, MessageRequest, MessageEvent, IUser,
|
AddressType, JSONDeserialisable, MessageRoutable, MessageRequest, MessageEvent, IUser,
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
/* eslint-disable node/no-sync */
|
||||||
|
import { strToU8, unzlibSync, zlibSync } from "fflate";
|
||||||
|
|
||||||
|
export function inflateSync(data: Uint8Array): Uint8Array {
|
||||||
|
try {
|
||||||
|
return unzlibSync(data);
|
||||||
|
} catch (err: any) {
|
||||||
|
// Match the error code node:zlib gives for invalid data.
|
||||||
|
throw Object.assign(new Error(err.message), { code: "Z_DATA_ERROR" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function deflateSync(data: Uint8Array | string): Uint8Array {
|
||||||
|
return zlibSync(typeof data === "string" ? strToU8(data) : data);
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import zlib from "zlib";
|
||||||
|
|
||||||
|
export function inflateSync(data: Uint8Array): Uint8Array {
|
||||||
|
// eslint-disable-next-line node/no-sync
|
||||||
|
return zlib.inflateSync(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function deflateSync(data: Uint8Array | string): Uint8Array {
|
||||||
|
// eslint-disable-next-line node/no-sync
|
||||||
|
return zlib.deflateSync(data);
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@
|
|||||||
"./src/hash.ts",
|
"./src/hash.ts",
|
||||||
"./src/ini.ts",
|
"./src/ini.ts",
|
||||||
"./src/is_deep_strict_equal.browser.ts",
|
"./src/is_deep_strict_equal.browser.ts",
|
||||||
|
"./src/zlib_sync.browser.ts",
|
||||||
"./src/link/*.ts",
|
"./src/link/*.ts",
|
||||||
"./src/lua_tools.ts",
|
"./src/lua_tools.ts",
|
||||||
"./src/logging.ts",
|
"./src/logging.ts",
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ catalog:
|
|||||||
'@vitejs/plugin-react': ^6.1.2
|
'@vitejs/plugin-react': ^6.1.2
|
||||||
antd: ^5.29.3
|
antd: ^5.29.3
|
||||||
express: ^5.3.0
|
express: ^5.3.0
|
||||||
|
fflate: ^0.8.3
|
||||||
jsonwebtoken: ^9.0.3
|
jsonwebtoken: ^9.0.3
|
||||||
react: ^18.3.1
|
react: ^18.3.1
|
||||||
react-dom: ^18.3.1
|
react-dom: ^18.3.1
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ import stream from "node:stream";
|
|||||||
// Capture native fetch before any potential mocks are applied
|
// Capture native fetch before any potential mocks are applied
|
||||||
const nativeFetch = global.fetch;
|
const nativeFetch = global.fetch;
|
||||||
|
|
||||||
import { ModStore, ModInfo, ModVersionEquality, zipOutputStream } from "@clusterio/lib";
|
import { ModStore, ModInfo, ModVersionEquality, zipOutputStream, hashFile } from "@clusterio/lib";
|
||||||
|
import * as libBuildMod from "@clusterio/lib/build_mod.js";
|
||||||
import { externalTest, createZip, createZipBuffer } from "../common.js";
|
import { externalTest, createZip, createZipBuffer } from "../common.js";
|
||||||
import "../setup_logging.js";
|
import "../setup_logging.js";
|
||||||
|
|
||||||
@@ -706,6 +707,38 @@ describe("lib/ModStore", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// === Instance Methods ===
|
// === Instance Methods ===
|
||||||
|
describe("readModInfo (static)", function () {
|
||||||
|
it("should load from a mod zip file", async function () {
|
||||||
|
await libBuildMod.build({
|
||||||
|
build: true,
|
||||||
|
pack: true,
|
||||||
|
sourceDir: path.join("test", "file", "empty_mod"),
|
||||||
|
outputDir: path.join("temp", "test"),
|
||||||
|
});
|
||||||
|
const filePath = path.join("temp", "test", "empty_mod_1.0.0.zip");
|
||||||
|
const hash = await hashFile(filePath);
|
||||||
|
const stat = await fs.stat(filePath);
|
||||||
|
const mod = await ModStore.readModInfo(filePath);
|
||||||
|
assert.deepEqual(
|
||||||
|
mod,
|
||||||
|
ModInfo.fromJSON({
|
||||||
|
name: "empty_mod",
|
||||||
|
version: "1.0.0",
|
||||||
|
title: "An Empty Mod",
|
||||||
|
author: "Me",
|
||||||
|
description: "An empty mod for testing",
|
||||||
|
factorio_version: "1.1",
|
||||||
|
dependencies: [],
|
||||||
|
filename: "empty_mod_1.0.0.zip",
|
||||||
|
size: stat.size,
|
||||||
|
mtime_ms: stat.mtimeMs,
|
||||||
|
updated_at_ms: stat.mtimeMs,
|
||||||
|
sha1: hash,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("Instance Methods", function () {
|
describe("Instance Methods", function () {
|
||||||
let modStore;
|
let modStore;
|
||||||
let testMod1Name = "test-mod-1";
|
let testMod1Name = "test-mod-1";
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
import assert from "node:assert/strict";
|
import assert from "node:assert/strict";
|
||||||
import fs from "node:fs/promises";
|
|
||||||
import path from "node:path";
|
|
||||||
|
|
||||||
import * as lib from "@clusterio/lib";
|
import * as lib from "@clusterio/lib";
|
||||||
import * as libBuildMod from "@clusterio/lib/build_mod.js";
|
|
||||||
import { ModInfo, ModDependency } from "@clusterio/lib";
|
import { ModInfo, ModDependency } from "@clusterio/lib";
|
||||||
|
|
||||||
import { testMatrix, testRoundTripJsonSerialisable } from "../../common.js";
|
import { testMatrix, testRoundTripJsonSerialisable } from "../../common.js";
|
||||||
@@ -413,35 +410,6 @@ describe("lib/data/ModInfo", function() {
|
|||||||
["UltraMod", "SuperLib >= 1.00", "! bad-mod"].map(dep => new ModDependency(dep))
|
["UltraMod", "SuperLib >= 1.00", "! bad-mod"].map(dep => new ModDependency(dep))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
it("should load from a mod zip file", async function() {
|
|
||||||
await libBuildMod.build({
|
|
||||||
build: true,
|
|
||||||
pack: true,
|
|
||||||
sourceDir: path.join("test", "file", "empty_mod"),
|
|
||||||
outputDir: path.join("temp", "test"),
|
|
||||||
});
|
|
||||||
const filePath = path.join("temp", "test", "empty_mod_1.0.0.zip");
|
|
||||||
const hash = await lib.hashFile(filePath);
|
|
||||||
const stat = await fs.stat(filePath);
|
|
||||||
const mod = await ModInfo.fromModFile(filePath);
|
|
||||||
assert.deepEqual(
|
|
||||||
mod,
|
|
||||||
ModInfo.fromJSON({
|
|
||||||
name: "empty_mod",
|
|
||||||
version: "1.0.0",
|
|
||||||
title: "An Empty Mod",
|
|
||||||
author: "Me",
|
|
||||||
description: "An empty mod for testing",
|
|
||||||
factorio_version: "1.1",
|
|
||||||
dependencies: [],
|
|
||||||
filename: "empty_mod_1.0.0.zip",
|
|
||||||
size: stat.size,
|
|
||||||
mtime_ms: stat.mtimeMs,
|
|
||||||
updated_at_ms: stat.mtimeMs,
|
|
||||||
sha1: hash,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
|
||||||
describe("checkDependencySatisfaction()", function() {
|
describe("checkDependencySatisfaction()", function() {
|
||||||
it("should return undefined when satisfied", function() {
|
it("should return undefined when satisfied", function() {
|
||||||
const mod = ModInfo.fromJSON({ dependencies: ["! a", "b = 1.0.0", "? c = 2.0.0"] });
|
const mod = ModInfo.fromJSON({ dependencies: ["! a", "b = 1.0.0", "? c = 2.0.0"] });
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/* eslint-disable node/no-sync */
|
||||||
|
import assert from "node:assert/strict";
|
||||||
|
import zlib from "node:zlib";
|
||||||
|
|
||||||
|
import * as browserZlib from "../../packages/lib/src/zlib_sync.browser.ts";
|
||||||
|
|
||||||
|
describe("lib/zlib_sync.browser", function() {
|
||||||
|
const text = JSON.stringify({ name: "pack", mods: [{ name: "base", version: "2.0.0" }] });
|
||||||
|
|
||||||
|
it("should inflate data deflated by node:zlib", function() {
|
||||||
|
const inflated = browserZlib.inflateSync(zlib.deflateSync(text));
|
||||||
|
assert.equal(Buffer.from(inflated).toString(), text);
|
||||||
|
});
|
||||||
|
it("should deflate data node:zlib can inflate", function() {
|
||||||
|
assert.equal(zlib.inflateSync(browserZlib.deflateSync(text)).toString(), text);
|
||||||
|
const bytes = Buffer.from(text);
|
||||||
|
assert.equal(zlib.inflateSync(browserZlib.deflateSync(bytes)).toString(), text);
|
||||||
|
});
|
||||||
|
it("should throw with a Z_ code on invalid data", function() {
|
||||||
|
assert.throws(
|
||||||
|
() => browserZlib.inflateSync(Buffer.from("not zlib data")),
|
||||||
|
err => err.code === "Z_DATA_ERROR"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user