Files
factorio-clusterio/packages/lib/src/data/messages_host.ts
T
Cooldude2606andHornwitser 2235294c59 Refactor user and instance manager (#837)
* Refactor UserManager to use Datastore

* Rename lib.User to UserDetails also adds to/fromUserDetails

* Fix existing tests

* Add user class tests

* Add tests for finaliseUsers

* Move HostDetails to own file with tests

* Move InstanceDetails to its own file with tests

* Rename HostInfo and add tests

* Rename InstanceInfo and add tests

- Also changes the names of the host properties related to instances
- Also removes the need to specify the location of InstanceInfo

* Make json round trip test validate schema

* Add InstanceManager

* Fix lint errors

* Extra tests

* Add final test coverage

* Add one last test to InstanceManager

* Response to comments

* Use generator rather than return anonumous generator

Co-authored-by: Hornwitser <github@hornwitser.no>

* Fix lint issue with generator

---------

Co-authored-by: Hornwitser <github@hornwitser.no>
2026-03-01 23:04:37 +00:00

251 lines
6.5 KiB
TypeScript

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";
export class HostStopRequest {
declare ["constructor"]: typeof HostStopRequest;
static type = "request" as const;
static src = ["control", "controller"] as const;
static dst = "host" as const;
static permission = "core.host.stop" as const;
}
export class HostRestartRequest {
declare ["constructor"]: typeof HostRestartRequest;
static type = "request" as const;
static src = ["control", "controller"] as const;
static dst = "host" as const;
static permission = "core.host.restart" as const;
}
export class HostUpdateRequest {
declare ["constructor"]: typeof HostUpdateRequest;
static type = "request" as const;
static src = ["control", "controller"] as const;
static dst = "host" as const;
static permission = "core.host.update" as const;
}
export class HostConfigGetRequest {
declare ["constructor"]: typeof HostConfigGetRequest;
static type = "request" as const;
static src = ["control", "controller"] as const;
static dst = "host" as const;
static permission = "core.host.get_config" as const;
static Response = plainJson(HostConfig.jsonSchema);
}
export class HostConfigSetFieldRequest {
declare ["constructor"]: typeof HostConfigSetFieldRequest;
static type = "request" as const;
static src = ["control", "controller"] as const;
static dst = "host" as const;
static permission = "core.host.update_config" as const;
constructor(
public field: string,
public value: string,
) { }
static jsonSchema = Type.Object({
"field": Type.String(),
"value": Type.String(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.field, json.value);
}
}
export class HostConfigSetPropRequest {
declare ["constructor"]: typeof HostConfigSetPropRequest;
static type = "request" as const;
static src = ["control", "controller"] as const;
static dst = "host" as const;
static permission = "core.host.update_config" as const;
constructor(
public field: string,
public prop: string,
public value?: unknown,
) { }
static jsonSchema = Type.Object({
"field": Type.String(),
"prop": Type.String(),
"value": Type.Optional(Type.Unknown()),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.field, json.prop, json.value);
}
}
export class HostListRequest {
declare ["constructor"]: typeof HostListRequest;
static type = "request" as const;
static src = "control" as const;
static dst = "controller" as const;
static permission = "core.host.list" as const;
static Response = jsonArray(HostDetails);
}
export class HostInfoUpdate {
constructor(
public name: string,
public publicAddress: string,
) { }
static jsonSchema = Type.Object({
"name": Type.String(),
"publicAddress": Type.String(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(
json.name,
json.publicAddress,
);
}
}
export class HostInfoUpdateEvent {
declare ["constructor"]: typeof HostInfoUpdateEvent;
static type = "event" as const;
static src = "host" as const;
static dst = "controller" as const;
constructor(
public update: HostInfoUpdate,
) { }
static jsonSchema = Type.Object({
"update": HostInfoUpdate.jsonSchema,
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(HostInfoUpdate.fromJSON(json.update));
}
}
export class HostUpdatesEvent {
declare ["constructor"]: typeof HostUpdatesEvent;
static type = "event" as const;
static src = "controller" as const;
static dst = "control" as const;
static permission = "core.host.subscribe" as const;
constructor(
public updates: HostDetails[],
) { }
static jsonSchema = Type.Object({
"updates": Type.Array(HostDetails.jsonSchema),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.updates.map(update => HostDetails.fromJSON(update)));
}
}
export class HostMetricsRequest {
declare ["constructor"]: typeof HostMetricsRequest;
static type = "request" as const;
static src = "controller" as const;
static dst = "host" as const;
static Response = class Response { // TODO: Use JSON class pattern in Prometheus
constructor(
public results: CollectorResultSerialized[],
) { }
static jsonSchema = Type.Object({
"results": Type.Array(CollectorResultSerialized),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.results);
}
};
}
export class ControllerConnectionEvent {
declare ["constructor"]: typeof ControllerConnectionEvent;
static type = "event" as const;
static src = "host" as const;
static dst = "instance" as const;
constructor(
public event: "connect" | "drop" | "resume" | "close",
) { }
static jsonSchema = Type.Object({
"event": StringEnum(["connect", "drop", "resume", "close"]),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.event);
}
}
export class PrepareControllerDisconnectRequest {
declare ["constructor"]: typeof PrepareControllerDisconnectRequest;
static type = "request" as const;
static src = "host" as const;
static dst = "instance" as const;
}
export class SyncUserListsEvent {
declare ["constructor"]: typeof SyncUserListsEvent;
static type = "event" as const;
static src = "controller" as const;
static dst = "host" as const;
constructor(
public adminlist: Set<string>,
public banlist: Map<string, string>,
public whitelist: Set<string>,
) { }
static jsonSchema = Type.Object({
"adminlist": Type.Array(Type.String()),
"banlist": Type.Array(
Type.Tuple([Type.String(), Type.String()])
),
"whitelist": Type.Array(Type.String()),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(new Set(json.adminlist), new Map(json.banlist), new Set(json.whitelist));
}
toJSON() {
return {
adminlist: [...this.adminlist],
banlist: [...this.banlist],
whitelist: [...this.whitelist],
};
}
}
export class HostRevokeTokensRequest {
declare["constructor"]: typeof HostRevokeTokensRequest;
static type = "request" as const;
static src = "control" as const;
static dst = "controller" as const;
static permission = "core.host.revoke_token" as const;
constructor(
public hostId: number,
) { }
static jsonSchema = Type.Object({
"hostId": Type.Number(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.hostId);
}
}