mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +09:00
Port packages/web_ui to TypeScript (#508)
* .jsx to .tsx * controller: tsc --build before webpack-cli * Type Annotation * disabled move into the <Input/> and <Checkbox/> * cleanup debug left over * documentation * default ModPack * modPackUpdateHandlers no undefined * PluginInfo changes proposition * PluginInfo changes proposition miss * ModRecord & ModInfo feedback loop at const mods = [ ? * ["connect", "drop", "resume", "close"] as const * .tsx to .ts when possible
This commit is contained in:
1 parent
a4893751e9
commit
838170a9ff
92 files changed
+1104
-707
No files matched your search
@@ -1,4 +1,5 @@
|
||||
// Configuration classes
|
||||
import { Type, Static } from "@sinclair/typebox";
|
||||
import events from "events";
|
||||
|
||||
import isDeepStrictEqual from "../is_deep_strict_equal";
|
||||
@@ -317,8 +318,34 @@ export class Config extends events.EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: remove after config refactor
|
||||
export class RawConfig {
|
||||
constructor(
|
||||
public serializedConfig: SerializedConfig
|
||||
) { }
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
"serializedConfig": Type.Object({
|
||||
"groups": Type.Array(
|
||||
Type.Object({
|
||||
"name": Type.String(),
|
||||
"fields": Type.Record(
|
||||
Type.String(),
|
||||
Type.Unknown(),
|
||||
),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
});
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(json.serializedConfig);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
type FieldType = "boolean" | "string" | "number" | "object";
|
||||
interface FieldDefinition {
|
||||
export interface FieldDefinition {
|
||||
type: FieldType;
|
||||
name: string;
|
||||
fullName: string;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Core definitions for the configuration system
|
||||
import * as classes from "./classes";
|
||||
import type { PluginInfo } from "../plugin";
|
||||
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin";
|
||||
|
||||
|
||||
/**
|
||||
@@ -503,14 +503,14 @@ export class ControlConfig extends classes.Config { }
|
||||
ControlConfig.registerGroup(ControlGroup);
|
||||
|
||||
|
||||
function validateGroup(pluginInfo: PluginInfo, groupName: string) {
|
||||
if (!(pluginInfo[groupName].prototype instanceof classes.PluginConfigGroup)) {
|
||||
function validateGroup(pluginInfo: PluginNodeEnvInfo|PluginWebpackEnvInfo, groupName: "ControllerConfigGroup"|"InstanceConfigGroup") {
|
||||
if (!(pluginInfo[groupName]?.prototype instanceof classes.PluginConfigGroup)) {
|
||||
throw new Error(
|
||||
`Expected ${groupName} for ${pluginInfo.name} to be a subclass of PluginConfigGroup`
|
||||
);
|
||||
}
|
||||
|
||||
if (pluginInfo[groupName].groupName !== pluginInfo.name) {
|
||||
if (pluginInfo[groupName]?.groupName !== pluginInfo.name) {
|
||||
throw new Error(
|
||||
`Expected ${groupName} for ${pluginInfo.name} to be named after the plugin`
|
||||
);
|
||||
@@ -522,7 +522,7 @@ function validateGroup(pluginInfo: PluginInfo, groupName: string) {
|
||||
*
|
||||
* @param {Array<Object>} pluginInfos - Array of plugin info objects.
|
||||
*/
|
||||
export function registerPluginConfigGroups(pluginInfos: PluginInfo[]) {
|
||||
export function registerPluginConfigGroups(pluginInfos: PluginNodeEnvInfo[]|PluginWebpackEnvInfo[]) {
|
||||
for (let pluginInfo of pluginInfos) {
|
||||
if (pluginInfo.ControllerConfigGroup) {
|
||||
validateGroup(pluginInfo, "ControllerConfigGroup");
|
||||
|
||||
@@ -12,7 +12,7 @@ import type { Logger } from "../logging";
|
||||
/**
|
||||
* A setting for a mod.
|
||||
*/
|
||||
interface ModSetting {
|
||||
export interface ModSetting {
|
||||
/** Value of the given mod setting. */
|
||||
value: boolean | number | string;
|
||||
}
|
||||
@@ -26,7 +26,7 @@ const ModSettingsJsonSchema = Type.Record(Type.String(), ModSettingJsonSchema);
|
||||
/**
|
||||
* A mod in a mod pack
|
||||
*/
|
||||
interface ModRecord {
|
||||
export interface ModRecord {
|
||||
/** name of the mod. */
|
||||
name: string,
|
||||
/** if mod is to be loaded. */
|
||||
@@ -35,6 +35,8 @@ interface ModRecord {
|
||||
version: string,
|
||||
/** SHA1 hash of the zip file. */
|
||||
sha1?: string,
|
||||
/** Used inside packages\web_ui\src\components\ModPackViewPage.tsx to define an error type. */
|
||||
error?: "missing"|"bad_checksum",
|
||||
}
|
||||
|
||||
const ModRecordJsonSchema = Type.Object({
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
export { default as ExportManifest } from "./ExportManifest";
|
||||
export { default as ModInfo } from "./ModInfo";
|
||||
export { default as ModPack } from "./ModPack";
|
||||
export { default as ModPack, ModSetting, ModRecord } from "./ModPack";
|
||||
export { default as ModuleInfo } from "./ModuleInfo";
|
||||
export * from "./composites";
|
||||
export * from "./messages_core";
|
||||
|
||||
@@ -1,21 +1,7 @@
|
||||
import { Type, Static } from "@sinclair/typebox";
|
||||
import { JsonString, StringEnum } from "./composites";
|
||||
import { levels } from "../logging";
|
||||
|
||||
// TODO: remove after config refactor
|
||||
export class RawConfig {
|
||||
constructor(
|
||||
public serializedConfig: object
|
||||
) { }
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
"serializedConfig": Type.Object({}),
|
||||
});
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(json.serializedConfig);
|
||||
}
|
||||
}
|
||||
import { RawConfig } from "../config";
|
||||
|
||||
export class ControllerConfigGetRequest {
|
||||
declare ["constructor"]: typeof ControllerConfigGetRequest;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { TSchema, Type, Static } from "@sinclair/typebox";
|
||||
import messageValidate from "./message_validate";
|
||||
import { StringEnum } from "./composites";
|
||||
import { AccountRole } from "../plugin";
|
||||
|
||||
export type AddressType = "controller" | "host" | "instance" | "control" | "broadcast";
|
||||
export type AddressShorthand =
|
||||
@@ -288,7 +289,7 @@ export class MessageRegisterControl extends Message {
|
||||
export class AccountDetails {
|
||||
constructor(
|
||||
public name: string,
|
||||
public roles: { name: string, id: number, permissions: string[] }[],
|
||||
public roles: AccountRole[],
|
||||
) { }
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { Type, Static } from "@sinclair/typebox";
|
||||
import PlayerStats from "../PlayerStats";
|
||||
import { JsonString, StringEnum, jsonArray } from "./composites";
|
||||
import { RawConfig } from "../config";
|
||||
import type { User } from "../users";
|
||||
import type { MessageRequest } from "./messages_core";
|
||||
import type { SerializedConfig } from "../config";
|
||||
|
||||
|
||||
export type InstanceStatus =
|
||||
"unknown" | "unassigned" | "stopped" | "starting" | "running"
|
||||
@@ -134,6 +137,8 @@ export class InstanceConfigGetRequest {
|
||||
static src = "control" as const;
|
||||
static dst = "controller" as const;
|
||||
static permission = "core.instance.get_config" as const;
|
||||
// TODO replace with InstanceConfig after config refactor
|
||||
static Response = RawConfig;
|
||||
|
||||
constructor(
|
||||
public instanceId: number,
|
||||
@@ -146,21 +151,6 @@ export class InstanceConfigGetRequest {
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(json.instanceId);
|
||||
}
|
||||
|
||||
// TODO replace with InstanceConfig after config refactor
|
||||
static Response = class Response {
|
||||
constructor(
|
||||
public config: object,
|
||||
) { }
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
"config": Type.Object({}),
|
||||
});
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(json.config);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export class InstanceConfigSetFieldRequest {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Type, Static } from "@sinclair/typebox";
|
||||
import PlayerStats from "../PlayerStats";
|
||||
import { JsonNumber, jsonArray } from "./composites";
|
||||
import { AccountRole } from "../plugin";
|
||||
|
||||
export class RawPermission { // TODO refactor into lib/user.Permission
|
||||
constructor(
|
||||
@@ -436,7 +437,7 @@ export class AccountUpdateEvent {
|
||||
static dst = "control" as const;
|
||||
|
||||
constructor(
|
||||
public roles?: { name: string, id: number, permissions: string[] }[],
|
||||
public roles?: AccountRole[],
|
||||
) { }
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
|
||||
@@ -82,6 +82,10 @@ export abstract class BaseConnector extends events.EventEmitter {
|
||||
|
||||
type ConnectorState = "closed" | "connecting" | "connected" | "resuming";
|
||||
|
||||
export type WebSocketClusterio = WebSocket.WebSocket & {
|
||||
clusterio_ignore_dump?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Base connector for links
|
||||
*
|
||||
@@ -91,7 +95,7 @@ export abstract class WebSocketBaseConnector extends BaseConnector {
|
||||
// One of closed, connecting (client only), connected and resuming.
|
||||
_state: ConnectorState = "closed";
|
||||
_closing = false;
|
||||
_socket: WebSocket | null = null;
|
||||
_socket: WebSocketClusterio | null = null;
|
||||
_lastHeartbeat: number | null = null;
|
||||
_heartbeatId: ReturnType<typeof setInterval> | null = null;
|
||||
_heartbeatInterval: number | null = null;
|
||||
@@ -451,12 +455,12 @@ export abstract class WebSocketClientConnector extends WebSocketBaseConnector {
|
||||
|
||||
// eslint-disable-next-line node/no-process-env
|
||||
if (process.env.APP_ENV === "browser") {
|
||||
this._socket = new WebSocket(url);
|
||||
this._socket = new WebSocket(url) as WebSocketClusterio;
|
||||
|
||||
} else {
|
||||
let options: { ca?: string } = {};
|
||||
if (this._tlsCa) { options.ca = this._tlsCa; }
|
||||
this._socket = new WebSocket(url, options);
|
||||
this._socket = new WebSocket(url, options) as WebSocketClusterio;
|
||||
}
|
||||
|
||||
this._attachSocketHandlers();
|
||||
|
||||
@@ -6,7 +6,7 @@ import * as libSchema from "../schema";
|
||||
import { dataClasses } from "./messages";
|
||||
import { BaseConnector, WebSocketBaseConnector } from "./connectors";
|
||||
import { strict as assert } from "assert";
|
||||
import type { PluginInfo } from "../plugin";
|
||||
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin";
|
||||
import type { User } from "../users";
|
||||
import type { AddressType, JSONDeserialisable, MessageRoutable, MessageRequest, MessageEvent } from "../data";
|
||||
|
||||
@@ -778,7 +778,7 @@ for (let Class of dataClasses) {
|
||||
Link.register(Class as RequestClass<unknown, unknown>);
|
||||
}
|
||||
|
||||
export function registerPluginMessages(pluginInfos: PluginInfo[]) {
|
||||
export function registerPluginMessages(pluginInfos: PluginNodeEnvInfo[]|PluginWebpackEnvInfo[]) {
|
||||
for (let pluginInfo of pluginInfos) {
|
||||
for (let Class of pluginInfo.messages || []) {
|
||||
if (Class.plugin !== pluginInfo.name) {
|
||||
|
||||
@@ -12,7 +12,7 @@ import type { Link } from "./link";
|
||||
import type { CommandTree } from "./command";
|
||||
import type { User } from "./users";
|
||||
import type { MessageRequest, MessageEvent } from "./data";
|
||||
|
||||
import type { PluginConfigGroup } from "./config";
|
||||
|
||||
/**
|
||||
* Conceptual base for controller and instance plugins.
|
||||
@@ -21,11 +21,41 @@ export type BasePlugin =
|
||||
| BaseControllerPlugin
|
||||
| BaseInstancePlugin
|
||||
| BaseControlPlugin
|
||||
| BaseWebPlugin
|
||||
;
|
||||
|
||||
// TODO Add proper typing for plugins
|
||||
export type PluginInfo = any;
|
||||
/* Used to define the export of info.ts from plugins */
|
||||
export type PluginDeclaration = {
|
||||
name: string;
|
||||
title: string;
|
||||
description?: string;
|
||||
|
||||
instanceEntrypoint?: string;
|
||||
InstanceConfigGroup?: typeof PluginConfigGroup;
|
||||
|
||||
controllerEntrypoint?: string;
|
||||
ControllerConfigGroup?: typeof PluginConfigGroup;
|
||||
|
||||
controlEntrypoint?: string;
|
||||
ControlConfigGroup?: typeof PluginConfigGroup;
|
||||
|
||||
messages?: any[];
|
||||
|
||||
webEntrypoint?: string;
|
||||
routes?: string[];
|
||||
}
|
||||
export type PluginNodeEnvInfo = PluginDeclaration & {
|
||||
requirePath: string;
|
||||
version: string;
|
||||
manifest: any;
|
||||
};
|
||||
|
||||
export type PluginWebpackEnvInfo = PluginDeclaration & {
|
||||
container: any;
|
||||
package: any;
|
||||
enabled?: boolean;
|
||||
};
|
||||
|
||||
type Controller = any;
|
||||
type Instance = any;
|
||||
type Host = any;
|
||||
@@ -96,7 +126,7 @@ export class BaseInstancePlugin {
|
||||
/**
|
||||
* The plugin's own info module
|
||||
*/
|
||||
public info: PluginInfo,
|
||||
public info: PluginNodeEnvInfo,
|
||||
/**
|
||||
* Instance the plugin started for
|
||||
*/
|
||||
@@ -332,7 +362,7 @@ export class BaseControllerPlugin {
|
||||
logger: Logger;
|
||||
|
||||
constructor(
|
||||
public info: PluginInfo,
|
||||
public info: PluginNodeEnvInfo,
|
||||
public controller: Controller,
|
||||
public metrics: any[],
|
||||
logger: Logger
|
||||
@@ -600,7 +630,7 @@ export class BaseControlPlugin {
|
||||
/**
|
||||
* The plugin's own info module
|
||||
*/
|
||||
public info: PluginInfo,
|
||||
public info: PluginNodeEnvInfo,
|
||||
logger: Logger,
|
||||
) {
|
||||
this.logger = logger.child({ plugin: this.info.name }) as unknown as Logger;
|
||||
@@ -647,6 +677,26 @@ export interface PluginLoginForm {
|
||||
Component: React["Component"];
|
||||
};
|
||||
|
||||
export type AccountRole = {
|
||||
name: string;
|
||||
id: number;
|
||||
permissions: string[];
|
||||
};
|
||||
|
||||
export type UserAccount = {
|
||||
/** Name of the currently logged in account. */
|
||||
name: string;
|
||||
/** Roles of the corrently logged in account. */
|
||||
roles: AccountRole[];
|
||||
/** Check if the currently logged in account has the given permission. */
|
||||
hasPermission: (permission: string) => boolean | null;
|
||||
/** Check if the currently logged in account has any of the given permissions. */
|
||||
hasAnyPermission: (...permissions: string[]) => boolean | null;
|
||||
/** Check if the currently logged in account has all of given permissions. */
|
||||
hasAllPermission: (...permissions: string[]) => boolean | null;
|
||||
/** Logs out of the web interface. */
|
||||
logOut: () => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* Plugin supplied pages
|
||||
@@ -677,7 +727,7 @@ export interface PluginPage {
|
||||
/**
|
||||
* Permission to access page. function are expected to throw an error if access is deny.
|
||||
*/
|
||||
permission?: string | ((user: User, message: MessageRequest|MessageEvent) => void);
|
||||
permission?: string | ((account: UserAccount) => (boolean|null));
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -762,7 +812,7 @@ export class BaseWebPlugin {
|
||||
/**
|
||||
* The plugin's own info module
|
||||
*/
|
||||
public info: PluginInfo,
|
||||
public info: PluginWebpackEnvInfo,
|
||||
logger: Logger,
|
||||
) {
|
||||
this.package = packageData;
|
||||
|
||||
@@ -21,9 +21,9 @@ import { logger } from "./logging";
|
||||
* @returns Array of plugin info modules.
|
||||
*/
|
||||
export async function loadPluginInfos(pluginList: Map<string, string>) {
|
||||
let plugins: libPlugin.PluginInfo[] = [];
|
||||
let plugins: libPlugin.PluginNodeEnvInfo[] = [];
|
||||
for (let [pluginName, pluginPath] of pluginList) {
|
||||
let pluginInfo: libPlugin.PluginInfo;
|
||||
let pluginInfo: libPlugin.PluginNodeEnvInfo;
|
||||
let pluginPackage: { version: string, main?: string };
|
||||
|
||||
try {
|
||||
@@ -53,12 +53,12 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
|
||||
}
|
||||
|
||||
function loadPluginClass(
|
||||
entrypointName: string,
|
||||
entrypointName: "controllerEntrypoint"|"instanceEntrypoint"|"controlEntrypoint",
|
||||
className: string,
|
||||
pluginClass: any,
|
||||
pluginInfo: libPlugin.PluginInfo
|
||||
pluginInfo: libPlugin.PluginNodeEnvInfo
|
||||
) {
|
||||
let resolvedPath = path.posix.join(pluginInfo.requirePath, pluginInfo[entrypointName]);
|
||||
let resolvedPath = path.posix.join(pluginInfo.requirePath, pluginInfo[entrypointName]!);
|
||||
let entrypoint = require(resolvedPath);
|
||||
if (!entrypoint[className]) {
|
||||
throw new libErrors.PluginError(pluginInfo.name,
|
||||
@@ -82,7 +82,7 @@ function loadPluginClass(
|
||||
* @returns plugin class
|
||||
*/
|
||||
export async function loadControllerPluginClass(
|
||||
pluginInfo: libPlugin.PluginInfo
|
||||
pluginInfo: libPlugin.PluginNodeEnvInfo
|
||||
): Promise<typeof libPlugin.BaseControllerPlugin> {
|
||||
return loadPluginClass("controllerEntrypoint", "ControllerPlugin", libPlugin.BaseControllerPlugin, pluginInfo);
|
||||
}
|
||||
@@ -96,7 +96,7 @@ export async function loadControllerPluginClass(
|
||||
* @returns plugin class
|
||||
*/
|
||||
export async function loadInstancePluginClass(
|
||||
pluginInfo: libPlugin.PluginInfo
|
||||
pluginInfo: libPlugin.PluginNodeEnvInfo
|
||||
): Promise<typeof libPlugin.BaseInstancePlugin> {
|
||||
return loadPluginClass("instanceEntrypoint", "InstancePlugin", libPlugin.BaseInstancePlugin, pluginInfo);
|
||||
}
|
||||
@@ -110,7 +110,7 @@ export async function loadInstancePluginClass(
|
||||
* @returns plugin class
|
||||
*/
|
||||
export async function loadControlPluginClass(
|
||||
pluginInfo: libPlugin.PluginInfo
|
||||
pluginInfo: libPlugin.PluginNodeEnvInfo
|
||||
): Promise<typeof libPlugin.BaseControlPlugin> {
|
||||
return loadPluginClass("controlEntrypoint", "ControlPlugin", libPlugin.BaseControlPlugin, pluginInfo);
|
||||
}
|
||||
Reference in new issue
Block a user