diff --git a/packages/controller/src/routes.ts b/packages/controller/src/routes.ts index 9c5121d5..48348726 100644 --- a/packages/controller/src/routes.ts +++ b/packages/controller/src/routes.ts @@ -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); diff --git a/packages/lib/src/ModStore.ts b/packages/lib/src/ModStore.ts index 3efb7a5b..f9ba4c84 100644 --- a/packages/lib/src/ModStore.ts +++ b/packages/lib/src/ModStore.ts @@ -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 { 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; + 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}.`); } diff --git a/packages/lib/src/data/ModInfo.ts b/packages/lib/src/data/ModInfo.ts index c44c8a6e..26420ba1 100644 --- a/packages/lib/src/data/ModInfo.ts +++ b/packages/lib/src/data/ModInfo.ts @@ -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; - 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, - }); - } } diff --git a/test/lib/ModStore.test.js b/test/lib/ModStore.test.js index 309f3117..a2e02bfd 100644 --- a/test/lib/ModStore.test.js +++ b/test/lib/ModStore.test.js @@ -6,7 +6,8 @@ import stream from "node:stream"; // Capture native fetch before any potential mocks are applied 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 "../setup_logging.js"; @@ -706,6 +707,38 @@ describe("lib/ModStore", function () { }); // === 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 () { let modStore; let testMod1Name = "test-mod-1"; diff --git a/test/lib/data/ModInfo.js b/test/lib/data/ModInfo.js index 68926e4a..190b5ac8 100644 --- a/test/lib/data/ModInfo.js +++ b/test/lib/data/ModInfo.js @@ -1,9 +1,6 @@ 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 libBuildMod from "@clusterio/lib/build_mod.js"; import { ModInfo, ModDependency } from "@clusterio/lib"; 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)) ); }); - 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() { it("should return undefined when satisfied", function() { const mod = ModInfo.fromJSON({ dependencies: ["! a", "b = 1.0.0", "? c = 2.0.0"] });