adjusted function input for getting latest mod version and adding chunking to not hit request limits

This commit is contained in:
O2theC authored and Danielv123 committed 2025-03-15 15:29:59 +01:00
1 parent 71c1cffcac
commit a2a3bcd4f8
1 file changed
+64 -89
+64 -89
View File
@@ -2,7 +2,7 @@ import fs from "fs-extra";
import path from "path"; import path from "path";
import { Static, StringContentEncodingOption, Type } from "@sinclair/typebox"; import { Static, StringContentEncodingOption, Type } from "@sinclair/typebox";
import { logger } from "./logging"; import { logger } from "./logging";
import { ModInfo } from "./data"; import { integerModVersion, ModInfo } from "./data";
import * as libSchema from "./schema"; import * as libSchema from "./schema";
import TypedEventEmitter from "./TypedEventEmitter"; import TypedEventEmitter from "./TypedEventEmitter";
import { safeOutputFile } from "./file_ops"; import { safeOutputFile } from "./file_ops";
@@ -12,56 +12,36 @@ export interface ModStoreEvents {
change: (mod: ModInfo) => void; change: (mod: ModInfo) => void;
} }
interface ModDownloadRequest {
name: string;
version?: string;
sha1?: string;
}
interface ModRelease { interface ModRelease {
download_url: string, download_url: string,
// /download/maraxsis/670de301df21001f6147fd1c -- append to mods.factorio.com to download file, auth required
file_name: string, file_name: string,
// maraxsis_1.8.0.zip -- the file name of the downloaded file ^^ info_json: { factorio_version: string; },
info_json: { releasted_at: string, // ISO 8601
dependencies?: Array<string>, version: string,
factorio_version: string; sha1: string;
};
released_at: string,
// 2024-10-15T03:35:29.892000Z -- (ISO 8601)
sha1: string,
// e227ee560d93485c7f215c9451eaa9cc38d81c98
version: string; // 1.8.0 -- just a normal version string
} }
interface ModImage {
id: string,
thumbnail: string,
url: string;
}
interface ModLicense {
description: string,
id: string,
name: string,
title: string,
url: string;
}
interface ModDetails { interface ModDetails {
category: string, // content -- what mod catogory is this apart of, a mod can only be apart of 1 category name: string,
download_count: number, // 3653 -- how many downloads does the mod have as a number title: string,
name: string, // maraxsis -- the name of the mod owner: string,
owner: string, // notnotmelon -- the name of the author of the mod summary: string,
releases: Array<ModRelease>, // an array of all releases and version of the mod downloads_count: number,
score: number, // 1054.8333333333333 -- it's prob used for sorting purposes category: string,
summary: string, // the mod description score: number,
thumbnail: string, // append path to assets-mods.factorio.com to get full url to thumbnail file releases: Array<ModRelease>,
title: string, // the full title of the mod, shown on the mod page }
changelog?: string,
created_at?: string, interface ModsInfoResponse {
description?: string, pagination: any,
homepage?: string, results: Array<ModDetails>,
images?: Array<ModImage>,
license?: ModLicense,
source_url?: string,
tags?: Array<string>,
updated_at?: string,
deprecated?: boolean;
} }
export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, ModStoreEvents> { export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, ModStoreEvents> {
@@ -188,54 +168,49 @@ export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, Mo
return new this(modsDirectory, files); return new this(modsDirectory, files);
} }
// gets the latest version of the mod provided as a string static getLatestVersionFromReleases(releases: Array<ModRelease>): string | undefined {
static async fetchLatestVersionString(modName: string): Promise<string> { let latestVersion = "0.0.0";
const modDetails: ModDetails = await ( releases.forEach((release) => {
await fetch(`https://mods.factorio.com/api/mods/${modName}`) if (integerModVersion(latestVersion) < integerModVersion(release.version)) {
).json() as ModDetails; latestVersion = release.version;
return modDetails.releases[modDetails.releases.length].version; }
});
if (latestVersion === "0.0.0") {
return undefined;
}
return latestVersion;
} }
// gets the latest version of the mod provided as a string static async getLatestVersionsChunk(modNames: Array<string>): Promise<{ [key: string]: string | undefined; }> {
static async fetchLatestVersionModInfo(modName: string): Promise<ModInfo> { const url = new URL("https://mods.factorio.com/api/mods");
const modDetails: ModDetails = await ( url.searchParams.set("page_size", "max");
await fetch(`https://mods.factorio.com/api/mods/${modName}/full`) const response = await fetch(url, {
).json() as ModDetails; method: "POST",
const latestRelease: ModRelease = modDetails.releases[modDetails.releases.length]; body: new URLSearchParams({ namelist: modNames }),
let modInfo = new ModInfo(); });
const info_json = latestRelease.info_json; if (response.status !== 200) {
throw Error(`Fetch: ${url} returned ${response.status} ${response.statusText}`);
// info.json fields }
if (modDetails.name) { modInfo.name = modDetails.name; } const mods = (await response.json() as ModsInfoResponse).results;
if (latestRelease.version) { modInfo.version = latestRelease.version; } const versions: { [key: string]: string | undefined; } = {};
if (modDetails.title) { modInfo.title = modDetails.title; } mods.forEach((mod) => {
if (modDetails.owner) { modInfo.author = modDetails.owner; } const modName = mod.name;
if (modDetails.homepage) { modInfo.homepage = modDetails.homepage; } let latestVersion = ModStore.getLatestVersionFromReleases(mod.releases);
if (modDetails.description) { modInfo.description = modDetails.description; } versions[modName] = latestVersion;
if (info_json.factorio_version) { modInfo.factorioVersion = info_json.factorio_version; }
if (info_json.dependencies) { modInfo.dependencies = info_json.dependencies; }
const infoJsonSchema = Type.Object({
"name": Type.String(),
"version": Type.String(),
"title": Type.String(),
"author": Type.String(),
"contact": Type.Optional(Type.String()),
"homepage": Type.Optional(Type.String()),
"description": Type.Optional(Type.String()),
"factorio_version": Type.Optional(Type.String()),
"dependencies": Type.Optional(Type.Array(Type.String())),
}); });
const validateInfo = libSchema.compile<Static<typeof infoJsonSchema>>(infoJsonSchema as any); return versions;
const valid = validateInfo(modInfo);
if (valid) {
return modInfo;
}
return modInfo;
} }
static async getLatestVersions(modNames: Array<string>): Promise<{ [key: string]: string | undefined; }> {
const chunkSize = 500;
const chunks: Array<{ [key: string]: string | undefined; }> = [];
for (let i = 0; i < modNames.length; i += chunkSize) {
chunks.push(await ModStore.getLatestVersionsChunk(modNames.slice(i, i + chunkSize)));
}
const versions = chunks.reduce((acc, curr) => ({ ...acc, ...curr }), {});
return versions;
}
} }