mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-25 16:33:12 +09:00
Move ModInfo.fromModFile to ModStore.readModInfo
Reading a mod zip needs zip_ops, which pulled yauzl into the web interface bundle through the data classes. ModStore is node only, so the zip reading now lives there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
464baabf96
commit
909f6ab08b
5 files changed
+74
-69
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);
|
||||||
|
|||||||
@@ -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,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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"] });
|
||||||
|
|||||||
Reference in new issue
Block a user