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:
Cooldude2606 authored and GitHub committed 2026-11-20 20:55:12 +00:00
commit e9a11013a4
16 files changed
+146 -74

No files matched your search

+1 -1
View File
@@ -828,7 +828,7 @@ async function uploadMod(req: Request, res: Response) {
// Pipe does not pass on an upload that was cut short.
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);
await fs.rename(tempFilePath, path.join(modsDirectory, modInfo.filename));
req.app.locals.controller.modStore.addMod(modInfo);
+8
View File
@@ -31,6 +31,13 @@
},
"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": {
"node": {
"types": "./src/is_deep_strict_equal.node.ts",
@@ -53,6 +60,7 @@
"@sinclair/typebox": "catalog:",
"ajv": "^8.20.0",
"fast-deep-equal": "^3.1.3",
"fflate": "catalog:",
"set-blocking": "catalog:",
"triple-beam": "^1.4.1",
"winston": "catalog:",
+39 -1
View File
@@ -4,6 +4,8 @@ import events from "events";
import { Static } from "@sinclair/typebox";
import { logger } from "./logging.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 { 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(); }
/**
* 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) {
let modInfo = await ModInfo.fromModFile(path.join(modsDirectory, file));
let modInfo = await ModStore.readModInfo(path.join(modsDirectory, file));
if (modInfo.filename !== file) {
throw new Error(`filename does not match the expected name ${modInfo.filename}.`);
}
-34
View File
@@ -1,10 +1,6 @@
import fs from "node:fs/promises";
import path from "path";
import { Type, Static } from "@sinclair/typebox";
import * as libHash from "../hash.js";
import * as libSchema from "../schema.js";
import { ZipArchive, findRoot } from "../zip_ops.js";
import { ModRecord } from "./ModPack.js";
import {
@@ -349,34 +345,4 @@ export default class ModInfo {
if (this.isDeleted) { json.is_deleted = this.isDeleted; }
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,
});
}
}
+2 -2
View File
@@ -1,4 +1,4 @@
import zlib from "zlib";
import * as zlib from "#zlib_sync";
import { Type, Static } from "@sinclair/typebox";
import * as libSchema from "../schema.js";
@@ -306,7 +306,7 @@ export default class ModPack {
// eslint-disable-next-line node/no-sync
let buf = zlib.deflateSync(JSON.stringify(json));
return buf.toString("base64");
return Buffer.from(buf).toString("base64");
}
static fromModPackString(modPackString: string) {
+1 -1
View File
@@ -1,4 +1,4 @@
import zlib from "zlib";
import * as zlib from "#zlib_sync";
class MapReaderState {
pos = 0;
+6
View File
@@ -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 -1
View File
@@ -1,5 +1,4 @@
// Connection adapters for Links
import { strict as assert } from "assert";
import events from "events";
import { WebSocket } from "#WebSocket";
@@ -7,6 +6,7 @@ import * as libData from "../data/index.js";
import * as libErrors from "../errors.js";
import { logger } from "../logging.js";
import ExponentialBackoff from "../ExponentialBackoff.js";
import { assert } from "./assert.js";
import type { Request, Event } from "./link.js";
/**
+1 -1
View File
@@ -5,7 +5,7 @@ import { logger } from "../logging.js";
import * as libSchema from "../schema.js";
import { dataClasses, errorClasses } from "./messages.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 {
AddressType, JSONDeserialisable, MessageRoutable, MessageRequest, MessageEvent, IUser,
+15
View File
@@ -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);
}
+11
View File
@@ -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);
}
+1
View File
@@ -19,6 +19,7 @@
"./src/hash.ts",
"./src/ini.ts",
"./src/is_deep_strict_equal.browser.ts",
"./src/zlib_sync.browser.ts",
"./src/link/*.ts",
"./src/lua_tools.ts",
"./src/logging.ts",