Merge branch 'master' into feature/download-file-cleanup

This commit is contained in:
Cooldude2606 authored and GitHub committed 2026-07-29 23:13:29 +01:00
commit 3a689b379a
21 files changed
+632 -46

No files matched your search

+85 -1
View File
@@ -12,7 +12,7 @@ import ModInfo, { ModDependencyUnsatisfiedReason } from "./ModInfo";
import {
PartialVersion, PartialVersionSchema, integerPartialVersion,
SourceVersion, SourceVersionSchema, normaliseFullVersion,
SourceVersion, SourceVersionSchema, normaliseFullVersion, integerSourceVersion,
} from "./version";
@@ -32,6 +32,10 @@ export interface ModSetting {
value: boolean | number | string | ModSettingColor;
}
export type ModRecordAdvisory =
| { type: "recommended_dependency", sourceModName: string }
| { type: "update_available", version: SourceVersion };
const ModSettingJsonSchema = Type.Object({
"value": Type.Union([Type.Boolean(), Type.Number(), Type.String(), ModSettingColor]),
});
@@ -56,6 +60,86 @@ export interface ModRecord {
warning?: ModDependencyUnsatisfiedReason | "wrong_factorio_version",
/** Used inside packages\web_ui\src\components\ModPackViewPage.tsx when there is no error. */
info?: ModInfo,
/** Non-blocking recommendations displayed by the Web UI. */
advisories?: ModRecordAdvisory[],
}
type AnnotatedModRecord = ModRecord & { advisories: ModRecordAdvisory[] };
/**
* Finds newer versions already installed on the controller for mods in a pack.
*/
export function getInstalledModUpdates(mods: ModRecord[], installedMods: Iterable<ModInfo>) {
const updates = new Map<string, SourceVersion>();
for (const mod of mods) {
if (!mod.info) {
continue;
}
for (const installedMod of installedMods) {
if (
installedMod.name !== mod.name
|| installedMod.factorioVersion !== mod.info.factorioVersion
|| installedMod.integerVersion <= integerSourceVersion(mod.version)
) {
continue;
}
const existingUpdate = updates.get(mod.name);
if (!existingUpdate || installedMod.integerVersion > integerSourceVersion(existingUpdate)) {
updates.set(mod.name, installedMod.version);
}
}
}
return updates;
}
export function applyModRecordAdvisories(
mods: ModRecord[],
availableUpdates: ReadonlyMap<string, SourceVersion> = new Map(),
) {
const annotatedMods: AnnotatedModRecord[] = mods.map(mod => ({ ...mod, advisories: [] }));
const modsByName = new Map(annotatedMods.map(mod => [mod.name, mod]));
for (const sourceMod of annotatedMods) {
if (!sourceMod.enabled || !sourceMod.info) {
continue;
}
for (const dependency of sourceMod.info.dependencies) {
if (!dependency.recommended) {
continue;
}
const targetMod = modsByName.get(dependency.name);
if (
!targetMod
|| targetMod.enabled
|| dependency.version && !dependency.version.testVersion(targetMod.version)
) {
continue;
}
targetMod.advisories.push({
type: "recommended_dependency",
sourceModName: sourceMod.name,
});
}
}
for (const mod of annotatedMods) {
const availableVersion = availableUpdates.get(mod.name);
if (availableVersion && integerSourceVersion(availableVersion) > integerSourceVersion(mod.version)) {
mod.advisories.push({ type: "update_available", version: availableVersion });
}
if (mod.advisories.length === 0) {
delete (mod as ModRecord).advisories;
}
}
return annotatedMods;
}
const ModRecordJsonSchema = Type.Object({
+2 -2
View File
@@ -5,8 +5,8 @@
*/
export { default as ExportManifest } from "./ExportManifest";
export { default as ModInfo, ModDependency } from "./ModInfo";
export { ModSettingColor } from "./ModPack";
export type { ModSetting, ModRecord } from "./ModPack";
export { ModSettingColor, getInstalledModUpdates, applyModRecordAdvisories } from "./ModPack";
export type { ModSetting, ModRecord, ModRecordAdvisory } from "./ModPack";
export { default as ModPack } from "./ModPack";
export { default as ModuleInfo } from "./ModuleInfo";
export { default as Permission } from "./Permission";
+18
View File
@@ -114,6 +114,24 @@ export class Address {
return `${this.type}:${this.id}` as const;
}
/**
* Returns this address with any request id removed
*
* A request id identifies one request in flight and is set on the
* source address of a request so that its response can be matched back
* up with it. It is meaningless on anything else, so an address taken
* from a request has to be stripped of it before it is used as the
* destination of an event.
*
* @returns this address without a request id.
*/
withoutRequestId() {
if (this.requestId === undefined) {
return this;
}
return new Address(this.type, this.id);
}
/**
* Returns true if this address targets the given destination
* @param dst - Destination to check
+4 -3
View File
@@ -7,6 +7,8 @@ import fs from "node:fs/promises";
import path from "path";
import stream from "stream";
import { redactUrl } from "./helpers";
/**
* Returns the newest file in a directory
@@ -332,12 +334,11 @@ export async function downloadFile(
}
// Fetch stream bytes. Anything that goes wrong from here until the
// body has been written leaves a temporary file that is of no use to
// anyone, including the request never reaching the server at all.
// body has been written leaves a temporary file that needs to be removed
try {
const response = await fetch(url);
if (!response.ok || !response.body) {
throw new Error(`Failed to download ${url}: ${response.status} ${response.statusText}`);
throw new Error(`Failed to download ${redactUrl(url)}: ${response.status} ${response.statusText}`);
}
const writer = stream.Writable.toWeb(writeStream);
await response.body.pipeTo(writer);
+45
View File
@@ -268,6 +268,51 @@ export function formatBytes(bytes: number, prefixes: "si" | "binary" = "si") {
return significant.toFixed(fractionDigits) + units[exponent];
}
/**
* Query parameters whose values are credentials and must never be logged.
*
* Matched case insensitively against the full parameter name.
*/
const sensitiveSearchParams = new Set([
"access_token",
"api_key",
"apikey",
"password",
"secret",
"token",
]);
/**
* Replace credentials in a URL with a placeholder
*
* Returns the URL as a string with the values of any query parameter
* carrying a credential replaced by `REDACTED`, so that the URL can
* safely be included in log output and error messages. The parameter
* names are kept intact to preserve the diagnostic value of the URL.
*
* Returns the input unchanged if it is a string which does not parse as
* a URL, as there are then no query parameters to redact.
*
* @param url - URL to redact credentials from.
* @returns url with credentials replaced by `REDACTED`.
*/
export function redactUrl(url: URL | string) {
let parsed;
try {
parsed = new URL(url);
} catch {
return String(url);
}
const searchParams = new URLSearchParams();
for (const [name, value] of parsed.searchParams) {
searchParams.append(name, sensitiveSearchParams.has(name.toLowerCase()) ? "REDACTED" : value);
}
parsed.search = searchParams.toString();
return parsed.toString();
}
function skipWhitespace(pos: number, input: string) {
// whitespace = 1*" "
while (pos < input.length && input.charAt(pos) === " ") {
+10 -9
View File
@@ -238,7 +238,7 @@ export class SubscriptionRequest {
type Subscriber = {
link: Link,
dst: Address,
address: Address,
filters: SubscriptionFilters,
}
@@ -296,7 +296,7 @@ export class SubscriptionController {
continue;
}
if (subscriber.filters.intersects(broadcastFilters)) {
subscriber.link.sendTo(subscriber.dst, event);
subscriber.link.sendTo(subscriber.address, event);
}
}
}
@@ -343,8 +343,9 @@ export class SubscriptionController {
if (!eventData) {
throw new Error(`Event ${request.eventName} is not a registered as subscribable`);
}
const addressIndex = src.addressIndex();
const subscriber = eventData.subscriptions.get(addressIndex);
const index = src.addressIndex();
const address = src.withoutRequestId();
const subscriber = eventData.subscriptions.get(index);
switch (request.action) {
case "unsubscribe":
if (!subscriber) {
@@ -352,13 +353,13 @@ export class SubscriptionController {
}
subscriber.filters.subtract(request.filters);
if (subscriber.filters.isEmpty()) {
eventData.subscriptions.delete(addressIndex);
eventData.subscriptions.delete(index);
}
break;
case "subscribe":
if (!subscriber) {
eventData.subscriptions.set(addressIndex, { link: link, dst: src, filters: request.filters });
eventData.subscriptions.set(index, { link, address, filters: request.filters });
} else {
subscriber.filters.union(request.filters);
}
@@ -369,9 +370,9 @@ export class SubscriptionController {
if (!subscriber) {
return false;
}
eventData.subscriptions.delete(addressIndex);
eventData.subscriptions.delete(index);
} else if (!subscriber) {
eventData.subscriptions.set(addressIndex, { link: link, dst: src, filters: request.filters });
eventData.subscriptions.set(index, { link, address, filters: request.filters });
} else {
subscriber.filters = request.filters;
}
@@ -384,7 +385,7 @@ export class SubscriptionController {
if (eventData.subscriptionUpdate) {
const eventReplay = await eventData.subscriptionUpdate(request, src, dst);
if (eventReplay) {
link.sendTo(src, eventReplay);
link.sendTo(address, eventReplay);
return true;
}
}