mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 23:04:52 +09:00
* 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>
117 lines
3.0 KiB
TypeScript
117 lines
3.0 KiB
TypeScript
import { Type, Static } from "@sinclair/typebox";
|
|
import { HostDetails } from "@clusterio/lib";
|
|
|
|
/** Underlying data class for hosts on the controller */
|
|
export default class HostRecord {
|
|
constructor(
|
|
/** Id of this host */
|
|
public id: number,
|
|
/** Name of this host */
|
|
public name: string,
|
|
/** Version this host last connected with */
|
|
public version: string,
|
|
/** Plugins this host last connected with */
|
|
public plugins: Map<string, string>,
|
|
/** True if this host is currently connected to controller */
|
|
public connected: boolean = false,
|
|
/** IP this host last connected from */
|
|
public remoteAddress: string = "",
|
|
/** Value of host.public_address configured for this host */
|
|
public publicAddress: string = "",
|
|
/** Unix timestamp in seconds host token must be issued after to be valid */
|
|
public tokenValidAfter: number = 0,
|
|
/** Millisecond Unix timestamp this entry was last updated at */
|
|
public updatedAtMs: number = 0,
|
|
/** True if this host has been deleted */
|
|
public isDeleted: boolean = false,
|
|
) { }
|
|
|
|
static jsonSchema = Type.Object({
|
|
"id": Type.Number(),
|
|
"name": Type.String(),
|
|
"version": Type.String(),
|
|
"plugins": Type.Record(Type.String(), Type.String()),
|
|
"connected": Type.Optional(Type.Boolean()),
|
|
"remote_address": Type.Optional(Type.String()),
|
|
"public_address": Type.Optional(Type.String()),
|
|
"token_valid_after": Type.Optional(Type.Number()),
|
|
"updated_at_ms": Type.Optional(Type.Number()),
|
|
"is_deleted": Type.Optional(Type.Boolean()),
|
|
});
|
|
|
|
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
|
return new this(
|
|
json.id,
|
|
json.name,
|
|
json.version,
|
|
new Map(Object.entries(json.plugins)),
|
|
json.connected,
|
|
json.remote_address,
|
|
json.public_address,
|
|
json.token_valid_after,
|
|
json.updated_at_ms,
|
|
json.is_deleted,
|
|
);
|
|
}
|
|
|
|
toJSON() {
|
|
const json = {
|
|
id: this.id,
|
|
name: this.name,
|
|
version: this.version,
|
|
plugins: Object.fromEntries(this.plugins),
|
|
} as Static<typeof HostRecord.jsonSchema>;
|
|
|
|
if (this.connected !== false) {
|
|
json.connected = this.connected;
|
|
}
|
|
if (this.remoteAddress !== "") {
|
|
json.remote_address = this.remoteAddress;
|
|
}
|
|
if (this.publicAddress !== "") {
|
|
json.public_address = this.publicAddress;
|
|
}
|
|
if (this.tokenValidAfter !== 0) {
|
|
json.token_valid_after = this.tokenValidAfter;
|
|
}
|
|
if (this.updatedAtMs !== 0) {
|
|
json.updated_at_ms = this.updatedAtMs;
|
|
}
|
|
if (this.isDeleted !== false) {
|
|
json.is_deleted = this.isDeleted;
|
|
}
|
|
|
|
return json;
|
|
}
|
|
|
|
|
|
static fromHostDetails(details: HostDetails, plugins: Map<string, string>) {
|
|
return new HostRecord(
|
|
details.id,
|
|
details.name,
|
|
details.version,
|
|
plugins,
|
|
details.connected,
|
|
details.remoteAddress,
|
|
details.publicAddress,
|
|
details.tokenValidAfter,
|
|
details.updatedAtMs,
|
|
details.isDeleted,
|
|
);
|
|
}
|
|
|
|
toHostDetails() {
|
|
return new HostDetails(
|
|
this.version,
|
|
this.name,
|
|
this.id,
|
|
this.connected,
|
|
this.remoteAddress,
|
|
this.publicAddress,
|
|
this.tokenValidAfter,
|
|
this.updatedAtMs,
|
|
this.isDeleted,
|
|
);
|
|
}
|
|
}
|