Merge branch 'master' into feature/plugin-install

This commit is contained in:
Cooldude2606 authored and GitHub committed 2026-09-12 22:10:52 +01:00
commit 512f62c22b
277 files changed
+2557 -2079

No files matched your search

+4 -4
View File
@@ -2,10 +2,10 @@ import fs from "node:fs/promises";
import path from "path";
import events from "events";
import { Static } from "@sinclair/typebox";
import { logger } from "./logging";
import { downloadFile, safeOutputFile } from "./file_ops";
import { ModPortalReleaseSchema, ModPortalDetailsSchema, ModNameVersionPair } from "./data/messages_mod";
import { ModInfo, ModPack, MajorMinorVersion, ModVersionEquality, SourceVersion } from "./data";
import { logger } from "./logging.js";
import { downloadFile, safeOutputFile } from "./file_ops.js";
import { ModPortalReleaseSchema, ModPortalDetailsSchema, ModNameVersionPair } from "./data/messages_mod.js";
import { ModInfo, ModPack, MajorMinorVersion, ModVersionEquality, SourceVersion } from "./data/index.js";
export interface ModStoreEvents {
/** A stored mod was created, updated or deleted */
+2 -1
View File
@@ -4,7 +4,8 @@ export type PluginWebApi = {
enabled: boolean;
loaded: boolean;
web: {
main: string;
/** Path to the web bundle, not present if the plugin has no web build. */
main?: string;
error?: string;
};
/**
+1 -1
View File
@@ -1,4 +1,4 @@
import { InstallationError } from "./errors";
import { InstallationError } from "./errors.js";
declare global {
// eslint-disable-next-line vars-on-top
var _clusterioLibImportedFrom: string;
+34 -72
View File
@@ -6,9 +6,9 @@
* @author Hornwitser
* @module lib/command
*/
import * as libData from "./data";
import * as libErrors from "./errors";
import type { Link } from "./link";
import * as libData from "./data/index.js";
import * as libErrors from "./errors.js";
import type { Link } from "./link/index.js";
import type { Argv } from "yargs";
export type CommandHandler = (args: any, control: any) => Promise<void>;
@@ -146,6 +146,22 @@ export class CommandTree {
}
/**
* Resolve a string with either an id or a name to the id of an entry
*
* An integer like string is treated as an id if an entry with that id
* exists, otherwise it is looked up as a name.
*/
function resolveId(entries: { id: number, name: string }[], idOrName: string) {
if (/^-?\d+$/.test(idOrName)) {
const id = parseInt(idOrName, 10);
if (entries.some(entry => entry.id === id)) {
return id;
}
}
return entries.find(entry => entry.name === idOrName)?.id;
}
/**
* Resolve a string into a host ID
*
@@ -157,23 +173,11 @@ export class CommandTree {
* @returns host ID.
*/
export async function resolveHost(client: Link, hostName: string) {
let hostId: number | undefined;
if (/^-?\d+$/.test(hostName)) {
hostId = parseInt(hostName, 10);
} else {
let hosts = await client.sendTo("controller", new libData.HostListRequest());
for (let host of hosts) {
if (host.name === hostName) {
hostId = host.id;
break;
}
}
if (hostId === undefined) {
throw new libErrors.CommandError(`No host named ${hostName}`);
}
const hosts = await client.sendTo("controller", new libData.HostListRequest());
const hostId = resolveId(hosts, hostName);
if (hostId === undefined) {
throw new libErrors.CommandError(`No host named ${hostName}`);
}
return hostId;
}
@@ -188,23 +192,11 @@ export async function resolveHost(client: Link, hostName: string) {
* @returns instance ID.
*/
export async function resolveInstance(client: Link, instanceName: string) {
let instanceId: number | undefined;
if (/^-?\d+$/.test(instanceName)) {
instanceId = parseInt(instanceName, 10);
} else {
let instances = await client.sendTo("controller", new libData.InstanceDetailsListRequest());
for (let instance of instances) {
if (instance.name === instanceName) {
instanceId = instance.id;
break;
}
}
if (instanceId === undefined) {
throw new libErrors.CommandError(`No instance named ${instanceName}`);
}
const instances = await client.sendTo("controller", new libData.InstanceDetailsListRequest());
const instanceId = resolveId(instances, instanceName);
if (instanceId === undefined) {
throw new libErrors.CommandError(`No instance named ${instanceName}`);
}
return instanceId;
}
@@ -220,23 +212,11 @@ export async function resolveInstance(client: Link, instanceName: string) {
* @returns mod pack ID.
*/
export async function resolveModPack(client: Link, modPackName: string) {
let modPackId: number | undefined;
if (/^-?\d+/.test(modPackName)) {
modPackId = parseInt(modPackName, 10);
} else {
let modPacks = await client.sendTo("controller", new libData.ModPackListRequest());
for (let modPack of modPacks) {
if (modPack.name === modPackName) {
modPackId = modPack.id;
break;
}
}
if (modPackId === undefined) {
throw new libErrors.CommandError(`No mod pack named ${modPackName}`);
}
const modPacks = await client.sendTo("controller", new libData.ModPackListRequest());
const modPackId = resolveId(modPacks, modPackName);
if (modPackId === undefined) {
throw new libErrors.CommandError(`No mod pack named ${modPackName}`);
}
return modPackId;
}
@@ -251,27 +231,9 @@ export async function resolveModPack(client: Link, modPackName: string) {
* @returns Role info.
*/
export async function retrieveRole(client: Link, roleName: string) {
let roles = await client.sendTo("controller", new libData.RoleListRequest());
let resolvedRole: libData.Role | undefined;
if (/^-?\d+$/.test(roleName)) {
let roleId = parseInt(roleName, 10);
for (let role of roles) {
if (role.id === roleId) {
resolvedRole = role;
break;
}
}
} else {
for (let role of roles) {
if (role.name === roleName) {
resolvedRole = role;
break;
}
}
}
const roles = await client.sendTo("controller", new libData.RoleListRequest());
const roleId = resolveId(roles, roleName);
const resolvedRole = roles.find(role => role.id === roleId);
if (!resolvedRole) {
throw new libErrors.CommandError(`No role named ${roleName}`);
}
+8 -8
View File
@@ -3,13 +3,13 @@ import { Type, Static } from "@sinclair/typebox";
import * as fs from "node:fs/promises";
import events from "events";
import isDeepStrictEqual from "../is_deep_strict_equal";
import { basicType } from "../helpers";
import * as libSchema from "../schema";
import { StringEnum } from "../data/composites";
import { safeOutputFile } from "../file_ops";
import * as validators from "./validators";
import { RequestError } from "../errors";
import isDeepStrictEqual from "#is_deep_strict_equal";
import { basicType } from "../helpers.js";
import * as libSchema from "../schema.js";
import { StringEnum } from "../data/composites.js";
import { safeOutputFile } from "../file_ops.js";
import * as validators from "./validators.js";
import { RequestError } from "../errors.js";
const ConfigLocation = StringEnum(["controller", "host", "control"]);
export type ConfigLocation = Static<typeof ConfigLocation>;
@@ -468,7 +468,7 @@ export class Config<
if (!this.filepath) {
throw new Error("Cannot save config which has no filepath");
}
await safeOutputFile(this.filepath, JSON.stringify(this, null, "\t"));
await safeOutputFile(this.filepath, JSON.stringify(this, null, "\t"), { mode: 0o600 });
this.dirty = false;
}
+4 -4
View File
@@ -1,9 +1,9 @@
// Core definitions for the configuration system
import * as classes from "./classes";
import * as validators from "./validators";
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin";
import * as classes from "./classes.js";
import * as validators from "./validators.js";
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin.js";
import { Static } from "@sinclair/typebox";
import { isTargetVersion } from "../data/version";
import { isTargetVersion } from "../data/version.js";
type configFromJSON<T> = (...args: Parameters<typeof classes.Config.fromJSON>) => T;
type configFromFile<T> = (...args: Parameters<typeof classes.Config.fromFile>) => Promise<T>;
+2 -2
View File
@@ -3,5 +3,5 @@
* @author Hornwitser
* @module lib/config
*/
export * from "./classes";
export * from "./definitions";
export * from "./classes.js";
export * from "./definitions.js";
+2 -2
View File
@@ -1,6 +1,6 @@
import { Static, Type } from "@sinclair/typebox";
import { StringEnum } from "./composites";
import { TargetVersion, TargetVersionSchema } from "./version";
import { StringEnum } from "./composites.js";
import { TargetVersion, TargetVersionSchema } from "./version.js";
export const InstanceStatus = StringEnum([
"unknown",
+5 -5
View File
@@ -2,16 +2,16 @@ import fs from "node:fs/promises";
import JSZip from "jszip";
import { Type, Static } from "@sinclair/typebox";
import * as libHash from "../hash";
import * as libSchema from "../schema";
import { findRoot } from "../zip_ops";
import { ModRecord } from "./ModPack";
import * as libHash from "../hash.js";
import * as libSchema from "../schema.js";
import { findRoot } from "../zip_ops.js";
import { ModRecord } from "./ModPack.js";
import {
MajorMinorVersion, MajorMinorVersionSchema, normaliseMajorMinorVersion, integerMajorMinorVersion,
SourceVersion, SourceVersionSchema, isSourceVersion, normaliseSourceVersion, integerSourceVersion,
ModVersionEquality,
} from "./version";
} from "./version.js";
type ModDependencyType = "incompatible" | "optional" | "hidden" | "unordered" | "required" | "recommended";
+7 -7
View File
@@ -1,19 +1,19 @@
import zlib from "zlib";
import { Type, Static } from "@sinclair/typebox";
import * as libSchema from "../schema";
import * as libLuaTools from "../lua_tools";
import * as libSchema from "../schema.js";
import * as libLuaTools from "../lua_tools.js";
import ExportManifest from "./ExportManifest";
import type { ExportSettings } from "../export";
import type { Logger } from "../logging";
import ExportManifest from "./ExportManifest.js";
import type { ExportSettings } from "../export.js";
import type { Logger } from "../logging.js";
import ModInfo, { ModDependencyUnsatisfiedReason } from "./ModInfo";
import ModInfo, { ModDependencyUnsatisfiedReason } from "./ModInfo.js";
import {
PartialVersion, PartialVersionSchema, integerPartialVersion,
SourceVersion, SourceVersionSchema, normaliseFullVersion, integerSourceVersion,
} from "./version";
} from "./version.js";
export const ModSettingColor = Type.Object({
+1 -1
View File
@@ -1,5 +1,5 @@
import { Type, Static } from "@sinclair/typebox";
import { compile } from "../schema";
import { compile } from "../schema.js";
/**
* Information about a module stored in its module.json file
+1 -1
View File
@@ -1,5 +1,5 @@
import { Static, Type } from "@sinclair/typebox";
import { permissions as globalPermissions } from "../permissions";
import { permissions as globalPermissions } from "../permissions.js";
interface RoleCollection {
get(id: number): Role;
+3 -3
View File
@@ -1,6 +1,6 @@
import { Static, Type } from "@sinclair/typebox";
import PlayerStats from "./PlayerStats";
import type { PermissionName } from "../permissions.js";
import PlayerStats from "./PlayerStats.js";
/** Represents a user in the cluster */
export default class UserDetails {
@@ -164,5 +164,5 @@ export interface IUser extends UserDetails {
* @throws {Error} If the given permission does not exist.
* @throws {libErrors.PermissionError} if the user does noh have the given permission.
*/
checkPermission(permission: string): void
checkPermission(permission: PermissionName): void
}
+23 -23
View File
@@ -3,26 +3,26 @@
* @module lib/data
* @author Hornwitser
*/
export { default as ExportManifest } from "./ExportManifest";
export { default as ModInfo, ModDependency } from "./ModInfo";
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";
export { default as PlayerStats } from "./PlayerStats";
export { default as Role } from "./Role";
export { default as InstanceDetails, InstanceStatus } from "./InstanceDetails";
export { default as HostDetails } from "./HostDetails";
export { default as UserDetails } from "./UserDetails";
export type { IUser } from "./UserDetails";
export * from "./composites";
export * from "./messages_core";
export * from "./messages_controller";
export * from "./messages_host";
export * from "./messages_instance";
export * from "./messages_mod";
export * from "./messages_user";
export * from "./messages_role";
export * from "./messages_plugin";
export * from "./version";
export { default as ExportManifest } from "./ExportManifest.js";
export { default as ModInfo, ModDependency } from "./ModInfo.js";
export { ModSettingColor, getInstalledModUpdates, applyModRecordAdvisories } from "./ModPack.js";
export type { ModSetting, ModRecord, ModRecordAdvisory } from "./ModPack.js";
export { default as ModPack } from "./ModPack.js";
export { default as ModuleInfo } from "./ModuleInfo.js";
export { default as Permission } from "./Permission.js";
export { default as PlayerStats } from "./PlayerStats.js";
export { default as Role } from "./Role.js";
export { default as InstanceDetails, InstanceStatus } from "./InstanceDetails.js";
export { default as HostDetails } from "./HostDetails.js";
export { default as UserDetails } from "./UserDetails.js";
export type { IUser } from "./UserDetails.js";
export * from "./composites.js";
export * from "./messages_core.js";
export * from "./messages_controller.js";
export * from "./messages_host.js";
export * from "./messages_instance.js";
export * from "./messages_mod.js";
export * from "./messages_user.js";
export * from "./messages_role.js";
export * from "./messages_plugin.js";
export * from "./version.js";
+1 -1
View File
@@ -1,4 +1,4 @@
import type { ValidateFunction } from "ajv";
import type { Message } from "./messages_core";
import type { Message } from "./messages_core.js";
const message_validate: ValidateFunction<Message>;
export default message_validate;
+4 -4
View File
@@ -1,8 +1,8 @@
import { Type, Static } from "@sinclair/typebox";
import { JsonString, StringEnum, StringKey, plainJson } from "./composites";
import { levels } from "../logging";
import { ControllerConfig, HostConfig } from "../config";
import { ExternalFactorioVersionSchema, LatestReleasesSchema } from "../external";
import { JsonString, StringEnum, StringKey, plainJson } from "./composites.js";
import { levels } from "../logging.js";
import { ControllerConfig, HostConfig } from "../config/index.js";
import { ExternalFactorioVersionSchema, LatestReleasesSchema } from "../external/index.js";
export class ControllerStopRequest {
declare ["constructor"]: typeof ControllerStopRequest;
+2 -2
View File
@@ -1,8 +1,8 @@
// Note: Run compile_validator script after making changes to this file.
import { TSchema, Type, Static } from "@sinclair/typebox";
import messageValidate from "./message_validate"; // generated file
import { StringEnum } from "./composites";
import messageValidate from "./message_validate.js"; // generated file
import { StringEnum } from "./composites.js";
export type AddressType = "controller" | "host" | "instance" | "control" | "broadcast";
export type AddressShorthand =
+4 -4
View File
@@ -1,8 +1,8 @@
import { Type, Static } from "@sinclair/typebox";
import { jsonArray, plainJson, StringEnum } from "./composites";
import { CollectorResultSerialized } from "../prometheus";
import { HostConfig } from "../config/definitions";
import HostDetails from "./HostDetails";
import { jsonArray, plainJson, StringEnum } from "./composites.js";
import { CollectorResultSerialized } from "../prometheus.js";
import { HostConfig } from "../config/definitions.js";
import HostDetails from "./HostDetails.js";
export class HostStopRequest {
declare ["constructor"]: typeof HostStopRequest;
+8 -8
View File
@@ -1,12 +1,12 @@
import { Type, Static } from "@sinclair/typebox";
import PlayerStats from "./PlayerStats";
import { JsonString, StringEnum, jsonArray, plainJson } from "./composites";
import { InstanceConfig } from "../config";
import type { IUser } from "./UserDetails";
import type { MessageRequest } from "./messages_core";
import { CollectorResultSerialized } from "../prometheus";
import { TargetVersion, TargetVersionSchema, PartialVersion, PartialVersionSchema } from "./version";
import InstanceDetails, { InstanceStatus } from "./InstanceDetails";
import PlayerStats from "./PlayerStats.js";
import { JsonString, StringEnum, jsonArray, plainJson } from "./composites.js";
import { InstanceConfig } from "../config/index.js";
import type { IUser } from "./UserDetails.js";
import type { MessageRequest } from "./messages_core.js";
import { CollectorResultSerialized } from "../prometheus.js";
import { TargetVersion, TargetVersionSchema, PartialVersion, PartialVersionSchema } from "./version.js";
import InstanceDetails, { InstanceStatus } from "./InstanceDetails.js";
export class InstanceDetailsGetRequest {
declare ["constructor"]: typeof InstanceDetailsGetRequest;
+4 -4
View File
@@ -1,13 +1,13 @@
import { Type, Static } from "@sinclair/typebox";
import ModInfo, { ModDependency } from "./ModInfo";
import ModPack from "./ModPack";
import { JsonString, jsonArray } from "./composites";
import ModInfo, { ModDependency } from "./ModInfo.js";
import ModPack from "./ModPack.js";
import { JsonString, jsonArray } from "./composites.js";
import {
MajorMinorVersion, MajorMinorVersionSchema, normaliseMajorMinorVersion,
SourceVersion, SourceVersionSchema,
ModVersionEquality,
} from "./version";
} from "./version.js";
export class ModPackGetRequest {
+15 -2
View File
@@ -1,6 +1,8 @@
import { Type, Static } from "@sinclair/typebox";
import { jsonArray } from "./composites";
import { PluginNodeEnvInfo } from "../plugin";
import { jsonArray } from "./composites.js";
import { PluginNodeEnvInfo } from "../plugin.js";
import { Address, MessageRequest } from "./messages_core.js";
import type { IUser } from "./UserDetails.js";
/* This is similar to other plugin definitions but with lots removed */
export class PluginDetails {
@@ -91,3 +93,14 @@ export class PluginInstallRequest {
return this.pluginPackage;
}
}
export class UpdateAllRequest {
declare ["constructor"]: typeof UpdateAllRequest;
static type = "request" as const;
static src = "control" as const;
static dst = ["controller", "host"] as const;
static permission(user: IUser, message: MessageRequest) {
user.checkPermission(message.dst.type === Address.host ? "core.host.update" : "core.controller.update");
user.checkPermission("core.plugin.update");
}
}
+3 -3
View File
@@ -1,7 +1,7 @@
import { Type, Static } from "@sinclair/typebox";
import { JsonNumber, jsonArray } from "./composites";
import Permission from "./Permission";
import Role from "./Role";
import { JsonNumber, jsonArray } from "./composites.js";
import Permission from "./Permission.js";
import Role from "./Role.js";
export class PermissionListRequest {
declare ["constructor"]: typeof PermissionListRequest;
+3 -3
View File
@@ -1,7 +1,7 @@
import { Type, Static } from "@sinclair/typebox";
import UserDetails, { IUser } from "./UserDetails";
import { StringEnum, jsonArray, plainJson } from "./composites";
import { MessageRequest } from "./messages_core";
import UserDetails, { IUser } from "./UserDetails.js";
import { StringEnum, jsonArray, plainJson } from "./composites.js";
import { MessageRequest } from "./messages_core.js";
export class UserGetRequest {
declare ["constructor"]: typeof UserGetRequest;
+2 -2
View File
@@ -6,8 +6,8 @@
*/
import fs from "node:fs/promises";
import * as libFileOps from "./file_ops";
import { basicType } from "./helpers";
import * as libFileOps from "./file_ops.js";
import { basicType } from "./helpers.js";
export type ItemCountWithQuality = { [quality: string]: number };
+4 -4
View File
@@ -2,10 +2,10 @@ import path from "path";
import fs from "node:fs/promises";
import { EventEmitter } from "events";
import { safeOutputFile } from "./file_ops";
import { SubscribableValue } from "./subscriptions";
import { logger } from "./logging";
import { ControllerConfig } from "./config";
import { safeOutputFile } from "./file_ops.js";
import { SubscribableValue } from "./subscriptions.js";
import { logger } from "./logging.js";
import { ControllerConfig } from "./config/index.js";
type DatastoreKey = string | number;
type DatastoreValue = string | number | boolean | object;
+1 -1
View File
@@ -1,4 +1,4 @@
import { FactorioColor } from "./lua_tools";
import { FactorioColor } from "./lua_tools.js";
/**
* A localised string.
+1 -1
View File
@@ -1,5 +1,5 @@
import { Static, Type } from "@sinclair/typebox";
import { FullVersionSchema, integerFullVersion, isFullVersion } from "../data/version";
import { FullVersionSchema, integerFullVersion, isFullVersion } from "../data/version.js";
const ARCHIVE_URL = "https://factorio.com/download/archive/";
+1 -1
View File
@@ -1,5 +1,5 @@
import { Static, Type } from "@sinclair/typebox";
import { FullVersion, FullVersionSchema } from "../data/version";
import { FullVersion, FullVersionSchema } from "../data/version.js";
const LATEST_RELEASES_URL = "https://factorio.com/api/latest-releases";
+2 -2
View File
@@ -2,5 +2,5 @@
* Library for interfacing with external apis
* @module lib/external
*/
export * from "./FactorioVersions";
export * from "./LatestReleases";
export * from "./FactorioVersions.js";
export * from "./LatestReleases.js";
+1 -1
View File
@@ -3,4 +3,4 @@
* @module lib/factorio
* @author Hornwitser
*/
export * from "./exchange_string";
export * from "./exchange_string.js";
+5 -1
View File
@@ -7,7 +7,7 @@ import fs from "node:fs/promises";
import path from "path";
import stream from "stream";
import { redactUrl } from "./helpers";
import { redactUrl } from "./helpers.js";
/**
@@ -168,6 +168,10 @@ export async function safeOutputFile(
throw err;
}
}
if (options?.mode !== undefined) {
// writeFile only applies mode when creating, a leftover temporary keeps its old mode.
await fs.chmod(temporary, options.mode);
}
await fs.rename(temporary, file);
}
+1 -1
View File
@@ -3,7 +3,7 @@
* @module lib/helpers
*/
import type { Logger } from "./logging";
import type { Logger } from "./logging.js";
import setBlocking from "set-blocking";
/**
@@ -0,0 +1 @@
export { default } from "fast-deep-equal/es6/index.js";
-1
View File
@@ -1 +0,0 @@
export default function isDeepStrictEqual(a: any, b: any): boolean;
-9
View File
@@ -1,9 +0,0 @@
"use strict";
if (process.env.APP_ENV === "browser") {
module.exports = require("fast-deep-equal/es6");
} else {
module.exports = require("util").isDeepStrictEqual;
}
@@ -0,0 +1 @@
export { isDeepStrictEqual as default } from "util";
@@ -0,0 +1,2 @@
const NativeWebSocket = WebSocket;
export { NativeWebSocket as WebSocket };
-2
View File
@@ -1,2 +0,0 @@
import ws = require("ws");
export = ws;
-3
View File
@@ -1,3 +0,0 @@
"use strict";
module.exports = process.env.APP_ENV === "browser" ? window.WebSocket : require("ws");
+1
View File
@@ -0,0 +1 @@
export { WebSocket } from "ws";
+16 -11
View File
@@ -2,12 +2,12 @@
import { strict as assert } from "assert";
import events from "events";
import WebSocket from "./WebSocket";
import * as libData from "../data";
import * as libErrors from "../errors";
import { logger } from "../logging";
import ExponentialBackoff from "../ExponentialBackoff";
import type { Request, Event } from "./link";
import { WebSocket } from "#WebSocket";
import * as libData from "../data/index.js";
import * as libErrors from "../errors.js";
import { logger } from "../logging.js";
import ExponentialBackoff from "../ExponentialBackoff.js";
import type { Request, Event } from "./link.js";
/**
* Numbered codes describing why a connection was closed
@@ -151,7 +151,7 @@ type WebSocketBaseConnectorEvents = {
"drop": [],
};
export type WebSocketClusterio = WebSocket.WebSocket & {
export type WebSocketClusterio = WebSocket & {
clusterio_ignore_dump?: boolean;
};
@@ -275,7 +275,9 @@ export abstract class WebSocketBaseConnector<
if (process.env.APP_ENV === "browser") {
this._socket!.close(ConnectionClosed.Timeout, "Heartbeat timeout");
} else {
this._socket!.terminate();
// This is only available in node, but the typechecking doesn't
// detect that this code is unreachable in the browser build.
(this._socket! as any).terminate();
}
} else {
@@ -635,7 +637,7 @@ export abstract class WebSocketClientConnector extends WebSocketBaseConnector<We
}
};
this._socket!.onerror = (event) => {
this._socket!.onerror = ((event: { error?: any, message?: string, }) => {
// It's assumed that close is always called by ws
let code = !event.error ? "" : `, code: ${event.error.code}`;
let message = `Connector | Socket error: ${event.message || "unknown error"}${code}`;
@@ -657,7 +659,8 @@ export abstract class WebSocketClientConnector extends WebSocketBaseConnector<We
this._closing = true;
}
}
};
// The event paramater have conflicting types in the node and browser versions, ignore it.
}) as any;
this._socket!.onopen = () => {
logger.verbose("Connector | Open");
@@ -710,12 +713,14 @@ export abstract class WebSocketClientConnector extends WebSocketBaseConnector<We
this.emit("connect", data);
} else if (type === "continue") {
logger.info("Connector | resuming existing session");
this._state = "connected";
this._heartbeatInterval = data.heartbeatInterval;
this._sessionTimeout = data.sessionTimeout;
this.startHeartbeat();
this._dropSendBufferSeq(data.lastSeq);
logger.info(
`Connector | resuming existing session, resending ${this._sendBuffer.length} buffered messages`
);
for (let bufferedMessage of this._sendBuffer) {
this._sendInternal(bufferedMessage);
}
+2 -2
View File
@@ -3,5 +3,5 @@
* @module lib/link
* @author Hornwitser
*/
export * from "./link";
export * from "./connectors";
export * from "./link.js";
export * from "./connectors.js";
+10 -10
View File
@@ -1,15 +1,15 @@
// Implementation of Link class
import * as libData from "../data";
import * as libErrors from "../errors";
import { logger } from "../logging";
import * as libSchema from "../schema";
import { dataClasses } from "./messages";
import { BaseConnector, WebSocketBaseConnector } from "./connectors";
import * as libData from "../data/index.js";
import * as libErrors from "../errors.js";
import { logger } from "../logging.js";
import * as libSchema from "../schema.js";
import { dataClasses } from "./messages.js";
import { BaseConnector, WebSocketBaseConnector } from "./connectors.js";
import { strict as assert } from "assert";
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin";
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin.js";
import type {
AddressType, JSONDeserialisable, MessageRoutable, MessageRequest, MessageEvent, IUser,
} from "../data";
} from "../data/index.js";
export interface Request<Req, Res> {
constructor: Partial<JSONDeserialisable<Req & Request<Req, Res>>> & {
@@ -444,7 +444,7 @@ export class Link {
let pending = this._pendingRequests.get(message.dst.requestId!);
if (!pending) {
throw new libErrors.InvalidMessage(
`Received response ${message.dst.requestId} without a pending request`
`Received response ${message.dst.requestId} from ${message.src} without a pending request`
);
}
@@ -465,7 +465,7 @@ export class Link {
let pending = this._pendingRequests.get(message.dst.requestId!);
if (!pending) {
throw new libErrors.InvalidMessage(
`Received error response ${message.dst.requestId} without a pending request`
`Received error response ${message.dst.requestId} from ${message.src} without a pending request`
);
}
+11 -10
View File
@@ -1,13 +1,13 @@
import * as core from "../data/messages_core";
import * as controller from "../data/messages_controller";
import * as host from "../data/messages_host";
import * as instance from "../data/messages_instance";
import * as mod from "../data/messages_mod";
import * as user from "../data/messages_user";
import * as role from "../data/messages_role";
import * as plugin from "../data/messages_plugin";
import * as subscriptions from "../subscriptions";
import type { RequestClass, EventClass } from "./link";
import * as core from "../data/messages_core.js";
import * as controller from "../data/messages_controller.js";
import * as host from "../data/messages_host.js";
import * as instance from "../data/messages_instance.js";
import * as mod from "../data/messages_mod.js";
import * as user from "../data/messages_user.js";
import * as role from "../data/messages_role.js";
import * as plugin from "../data/messages_plugin.js";
import * as subscriptions from "../subscriptions.js";
import type { RequestClass, EventClass } from "./link.js";
/**
* Event and Request payloads registered with the Link class. Used to decode
@@ -140,4 +140,5 @@ export const dataClasses: (RequestClass<unknown, unknown> | EventClass<unknown>)
plugin.PluginListRequest,
plugin.PluginUpdateRequest,
plugin.PluginInstallRequest,
plugin.UpdateAllRequest,
];
+3 -2
View File
@@ -1,8 +1,8 @@
import fs from "node:fs/promises";
import { createRequire } from "node:module";
import path from "path";
import { logger } from "./logging";
import * as libFileOps from "./file_ops";
import { logger } from "./logging.js";
import * as libFileOps from "./file_ops.js";
import vm from "vm";
/**
@@ -67,6 +67,7 @@ async function findLocalPlugins(pluginList: Map<string, string>): Promise<boolea
return hasChanged > 0;
}
const require = createRequire(import.meta.url);
function getPluginName(requireSpec: string) {
const context = vm.createContext({ require: require, pluginInfo: null });
const code = `pluginInfo = require(${JSON.stringify(requireSpec)}).plugin;`;
+6 -6
View File
@@ -14,12 +14,12 @@ import Transport from "winston-transport";
import { LEVEL, MESSAGE } from "triple-beam";
import chalk from "chalk";
import * as libData from "./data";
import * as libErrors from "./errors";
import * as libFileOps from "./file_ops";
import { type LogFilter, levels, logFilter, logger } from "./logging";
import * as libStream from "./stream";
import type { Link } from "./link";
import * as libData from "./data/index.js";
import * as libErrors from "./errors.js";
import * as libFileOps from "./file_ops.js";
import { type LogFilter, levels, logFilter, logger } from "./logging.js";
import * as libStream from "./stream.js";
import type { Link } from "./link/index.js";
const finished = util.promisify(stream.finished);
File diff suppressed because it is too large. Load diff
+27 -5
View File
@@ -2,11 +2,11 @@
* Plugin interfaces and utilities.
* @module lib/plugin
*/
import * as libHelpers from "./helpers";
import type { Logger } from "./logging";
import type { FieldDefinition } from "./config";
import type { PlayerStats } from "./data";
import * as libHelpers from "./helpers.js";
import type { Logger } from "./logging.js";
import type { FieldDefinition } from "./config/index.js";
import type { PermissionDefinition } from "./permissions.js";
import type { PlayerStats } from "./data/index.js";
export const PluginFeatureFlags = [
/** The plugin requires module code to be patched into the save */
@@ -37,15 +37,37 @@ export type PluginDeclaration = {
features?: (typeof PluginFeatureFlags)[number][];
messages?: any[];
permissions?: PermissionDefinition[];
routes?: string[];
}
/**
* Check if a plugin is expected to ship a web build.
*
* Mirrors the rule used by the create tool: a web build is generated when the
* plugin has a web or controller entrypoint or defines config fields.
*/
export function pluginNeedsWebBuild(info: PluginDeclaration) {
return Boolean(
info.webEntrypoint
|| info.controllerEntrypoint
|| info.controllerConfigFields
|| info.hostConfigFields
|| info.instanceConfigFields
|| info.controlConfigFields
);
}
export type PluginNodeEnvInfo = PluginDeclaration & {
/**
* Path to the folder with the static files that should be hosted on the web
* server in order for the web interface to be able to load the plugin.
*/
webStaticPath: string;
/**
* Absolute path to the package.json file for the plugin.
*/
packagePath: string;
requirePath: string;
version: string;
manifest: any;
+20 -15
View File
@@ -5,9 +5,10 @@
*/
import path from "path";
import fs from "node:fs/promises";
import * as libErrors from "./errors";
import * as libPlugin from "./plugin";
import { logger } from "./logging";
import { fileURLToPath, pathToFileURL } from "node:url";
import * as libErrors from "./errors.js";
import * as libPlugin from "./plugin.js";
import { logger } from "./logging.js";
/**
@@ -26,11 +27,17 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
for (let [pluginName, pluginPath] of pluginList) {
let pluginInfo: libPlugin.PluginNodeEnvInfo;
let pluginPackage: { name?: string, version: string, main?: string, private?: boolean };
let packagePath;
// Check if plugin path exists, otherwise remove it
// Check if plugin has a package.json file, otherwise remove it
try {
require.resolve(pluginPath);
} catch {
const absolute = path.isAbsolute(pluginPath);
const packageImport = path.posix.join(pluginPath, "package.json");
packagePath = fileURLToPath(import.meta.resolve(
absolute ? pathToFileURL(packageImport).href : packageImport
));
await fs.access(packagePath, fs.constants.F_OK);
} catch (err) {
let errMsg = `Plugin path ${pluginPath} does not exist`;
try {
await fs.access(pluginPath, fs.constants.F_OK);
@@ -42,8 +49,10 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
}
try {
pluginInfo = require(pluginPath).plugin;
pluginPackage = require(path.posix.join(pluginPath, "package.json"));
pluginPackage = (await import(pathToFileURL(packagePath).href, { with: { type: "json" }})).default;
pluginInfo = (await import(
pathToFileURL(path.posix.join(pluginPath, pluginPackage.main ?? "index.js")).href
)).plugin;
} catch (err: any) {
if (err.code === "InstallationError") {
throw err;
@@ -69,12 +78,8 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
continue;
}
pluginInfo.webStaticPath = path.join(
path.dirname(
require.resolve(path.posix.join(pluginPath, "package.json"))
),
"dist", "web", "static",
);
pluginInfo.webStaticPath = path.join(path.dirname(packagePath), "dist", "web", "static");
pluginInfo.packagePath = packagePath;
pluginInfo.requirePath = pluginPath;
pluginInfo.version = pluginPackage.version;
pluginInfo.npmPackage = !pluginPackage.private && pluginPath === pluginPackage.name ? pluginPath : undefined;
@@ -89,7 +94,7 @@ export async function loadPluginClass<Class extends { new (...args: any): any }>
className: string,
pluginClass: Class,
): Promise<Class> {
let entrypoint = require(requirePath);
let entrypoint = await import(pathToFileURL(requirePath).href);
if (!entrypoint[className]) {
throw new libErrors.PluginError(pluginName,
new Error(`Expected ${requirePath} to export a class named ${className}`)
+6 -4
View File
@@ -12,7 +12,7 @@
* the listed interfaces for more information.
*
* @example
* const { Counter, exposition } = require("@clusterio/lib");
* import { Counter, exposition } from "@clusterio/lib";
*
* // Collectors are by default registered to the default collector regitry.
* const totalRequests = new Counter(
@@ -27,7 +27,8 @@
*
* // Code handling the /metrics HTTP request, here shown for express
* // but any http framework may be used
* const app = require("express")();
* import express from "express";
* const app = express();
* async function getMetrics(req, res) {
* // By default exposition uses the default collector registry
* let text = await exposition();
@@ -39,7 +40,7 @@
* @module lib/prometheus
*/
import { Type, Static } from "@sinclair/typebox";
import { StringEnum } from "./data/composites";
import { StringEnum } from "./data/composites.js";
/**
* Result from collecting a {@link Collector}
@@ -1669,7 +1670,8 @@ async function* expositionLines(
* express.js.
*
* @example
* const app = require("express")();
* import express from "express";
* const app = express();
* async function getMetrics(req, res) {
* // By default exposition uses the default collector registry
* let text = await exposition();
+13 -6
View File
@@ -1,15 +1,15 @@
import util from "util";
import path from "path";
import { exec } from "child_process";
import { logger } from "./logging";
import { RequestError } from "./errors";
import { PluginNodeEnvInfo } from "./plugin";
import { logger } from "./logging.js";
import { RequestError } from "./errors.js";
import { PluginNodeEnvInfo } from "./plugin.js";
const execAsync = util.promisify(exec);
function isDev() {
// dev: <devRoot>/packages/lib/dist/src/rce_ops.js
// prod: <prodRoot>/node_modules/@clusterio/lib/dist/src/rce_ops.js
return __dirname.split(path.sep).at(-5) === "packages"; // opposed to "@clusterio"
return import.meta.dirname.split(path.sep).at(-5) === "packages"; // opposed to "@clusterio"
}
async function logExec(cmd: string) {
@@ -19,8 +19,8 @@ async function logExec(cmd: string) {
}
}
export async function updatePackage(name: string) {
return logExec(`npm update --save ${name}`);
export async function updatePackage(...names: string[]) {
return logExec(`npm update --save ${names.join(" ")}`);
}
export async function installPackage(name: string) {
@@ -35,6 +35,13 @@ export async function handlePluginUpdate(pluginName: string, pluginInfos: Plugin
return await updatePackage(pluginName);
}
export async function handleUpdateAll(corePackage: string, pluginInfos: PluginNodeEnvInfo[]) {
const pluginPackages = pluginInfos
.map(plugin => plugin.npmPackage)
.filter((name): name is string => name !== undefined);
return await updatePackage(corePackage, ...pluginPackages);
}
export async function handlePluginInstall(pluginName: string) {
if (pluginName.length > 214 || /[^a-zA-Z0-9\-_.+@\/]/.test(pluginName)) {
// https://docs.npmjs.com/cli/v11/configuring-npm/package-json#name
+1 -1
View File
@@ -2,7 +2,7 @@
* JSON schemas used for validating link messages
* @module lib/schema
*/
import Ajv, { JSONSchemaType, ValidateFunction } from "ajv";
import { Ajv, JSONSchemaType, ValidateFunction } from "ajv";
const ajv = new Ajv({
// These are duplicated in scripts/compile_validator
allowUnionTypes: true,
+14 -8
View File
@@ -6,12 +6,13 @@ import fs from "fs/promises";
import path from "path";
import { spawn } from "child_process";
import * as libConfig from "./config";
import * as libFileOps from "./file_ops";
import { logger } from "./logging";
import * as libHelpers from "./helpers";
import { LockFile } from "./LockFile";
import { loadPluginList } from "./load_plugin_list";
import { loadPluginList } from "./load_plugin_list.js";
import * as libConfig from "./config/index.js";
import * as libFileOps from "./file_ops.js";
import { logger } from "./logging.js";
import * as libHelpers from "./helpers.js";
import { LockFile } from "./LockFile.js";
import { pathToFileURL } from "url";
function print(...content: any[]) {
@@ -171,8 +172,13 @@ export async function handlePluginCommand(
let pluginInfo: { name: string };
try {
pluginInfo = require(pluginPath).plugin;
const pluginPackage = (await import(
pathToFileURL(path.posix.join(pluginPath, "package.json")).href,
{ with: { type: "json" }},
)).default;
pluginInfo = (await import(
pathToFileURL(path.posix.join(pluginPath, pluginPackage.main ?? "index.js")).href
)).plugin;
} catch (err: any) {
logger.error(`Unable to import plugin info from ${args.path}:\n${err.stack}`);
process.exitCode = 1;
+7 -6
View File
@@ -1,9 +1,10 @@
import { Type, Static } from "@sinclair/typebox";
import { Link, Event, EventClass, RequestHandler, WebSocketBaseConnector } from "./link";
import { Address, MessageRequest, IUser, JsonBoolean, StringEnum, AccountDetails } from "./data";
import isDeepStrictEqual from "./is_deep_strict_equal";
import { RequestError } from "./errors";
import { logger } from "./logging";
import { Link, Event, EventClass, RequestHandler, WebSocketBaseConnector } from "./link/index.js";
import { Address, MessageRequest, IUser, JsonBoolean, StringEnum } from "./data/index.js";
import isDeepStrictEqual from "#is_deep_strict_equal";
import type { PermissionName } from "./permissions.js";
import { RequestError } from "./errors.js";
import { logger } from "./logging.js";
export type SubscriptionRequestHandler<T> = RequestHandler<SubscriptionRequest, Event<T> | null>;
export type EventSubscriberCallback<T> = (event: T | null, synced: boolean) => void
@@ -155,7 +156,7 @@ export class SubscriptionRequest {
const entry = Link._eventsByName.get(data[0]);
if (entry && entry.Event.permission) {
if (typeof entry.Event.permission === "string") {
user.checkPermission(entry.Event.permission);
user.checkPermission(entry.Event.permission as PermissionName);
} else {
entry.Event.permission(user, message);
}
+2 -2
View File
@@ -1,8 +1,8 @@
import fs from "node:fs/promises";
import os from "os";
import util from "util";
import { Gauge } from "./prometheus";
import { SystemInfo } from "./data";
import { Gauge } from "./prometheus.js";
import { SystemInfo } from "./data/index.js";
function filterCpuModel() {
const model = os.cpus()[0].model;