mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Reverse the direction of the API for sending messages from
messageDefinition.send(link, { content });
to
link.send(new MessageClass(content));
and convert all message definitions to use the JSON serialisable class
pattern and explicit registration of message handlers, along with a
end-to-end based routing of messages instead of link to link.
The experience I gained from the existing way messages were defined and
sent over links was that it was difficult to understand for people
unfamiliar with the system. The automatic registration of message
handlers ment that code was being called as if by magic from nowhere
with no easy way to discover it. The manual forwarding and rules for
what can be sent where and how obscured its working.
The use of pure JSON as both input and output from message handlers made
it tedious to work with. Data had to be manually converted between the
program representation and the over the wire format, which made handler
code unplesant to write. Particularly because the over the wire format
used snake_case while the program code uses camelCase for variables.
The idea behind the decision to do it this way was to make it more clear
which data came from the wire and which from the program, and while this
worked it didn't make message handlers any nicer to write.
The direction of the API and magic registration of message handlers also
meant that a future migration to TypeScript would be troublesome. While
it may be possible to implement, the degree of TypeScript magic required
would go starkly against the principle of keep it simple stupid.
By refactoring to using the JSON serialisable class pattern most of
these issues go away. There is no longer a need to distinguish the on
wire format of messages with the program representation because the only
place that deals with the wire format of messages are the toJSON and
fromJSON methods on the classes themselves. Message handlers will work
with proper instances of classes instead of raw data, something that
makes them easier to write, and trivial to type with TypeScript.
End-to-end routing also simplifies sending messages that need to do
mulitple hops to reach the destination. And removes the need for having
all messages be specified in plugin's info module, as they can be routed
without having to be parsed completely.
The explicit registration of message handlers is something that I
thought would be tedious boilerplate code that could be automated away.
But experience now shows it to be the opposite. Allthough dull
repetition, the purpose it serves is to link together the code in a way
that humans can easily find. With explicit registration searching the
name of the message handler in the editor will show the registration that
ties it together with the type of message it handles and the kind of
registration. With implicit registration searching the name of the
message handler turns up only the handler with no clue as to what calls
it.
475 lines
9.0 KiB
JavaScript
475 lines
9.0 KiB
JavaScript
"use strict";
|
|
const ModPack = require("./ModPack");
|
|
const ModInfo = require("./ModInfo");
|
|
const { jsonArray, JsonString } = require("./composites");
|
|
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModPackGetRequest {
|
|
static type = "request";
|
|
static src = ["instance", "control"];
|
|
static dst = "controller";
|
|
static permission = "core.mod_pack.get";
|
|
|
|
/** @type {number} */
|
|
id;
|
|
|
|
constructor(id) {
|
|
this.id = id;
|
|
}
|
|
|
|
static jsonSchema = {
|
|
type: "object",
|
|
required: ["id"],
|
|
properties: {
|
|
id: { type: "integer" },
|
|
},
|
|
};
|
|
|
|
static fromJSON(json) {
|
|
return new this(json.id);
|
|
}
|
|
|
|
static Response = ModPack;
|
|
}
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModPackGetDefaultRequest {
|
|
static type = "request";
|
|
static src = ["instance", "control"];
|
|
static dst = "controller";
|
|
static permission = "core.mod_pack.get";
|
|
static Response = ModPack;
|
|
}
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModPackListRequest {
|
|
static type = "request";
|
|
static src = "control";
|
|
static dst = "controller";
|
|
static permission = "core.mod_pack.list";
|
|
static Response = jsonArray(ModPack);
|
|
}
|
|
|
|
/** @memberof module:lib/data */
|
|
class ModPackCreateRequest {
|
|
static type = "request";
|
|
static src = "control";
|
|
static dst = "controller";
|
|
static permission = "core.mod_pack.create";
|
|
|
|
/** @type {module:lib/data.ModPack} */
|
|
modPack;
|
|
|
|
constructor(modPack) {
|
|
this.modPack = modPack;
|
|
}
|
|
|
|
static jsonSchema = {
|
|
type: "object",
|
|
required: ["modPack"],
|
|
properties: {
|
|
"modPack": ModPack.jsonSchema,
|
|
},
|
|
};
|
|
|
|
static fromJSON(json) {
|
|
return new this(ModPack.fromJSON(json.modPack));
|
|
}
|
|
}
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModPackUpdateRequest {
|
|
static type = "request";
|
|
static src = "control";
|
|
static dst = "controller";
|
|
static permission = "core.mod_pack.update";
|
|
|
|
/** @type {module:lib/data.ModPack} */
|
|
modPack;
|
|
|
|
constructor(modPack) {
|
|
this.modPack = modPack;
|
|
}
|
|
|
|
static jsonSchema = {
|
|
type: "object",
|
|
required: ["modPack"],
|
|
properties: {
|
|
"modPack": ModPack.jsonSchema,
|
|
},
|
|
};
|
|
|
|
static fromJSON(json) {
|
|
return new this(ModPack.fromJSON(json.modPack));
|
|
}
|
|
}
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModPackDeleteRequest {
|
|
static type = "request";
|
|
static src = "control";
|
|
static dst = "controller";
|
|
static permission = "core.mod_pack.delete";
|
|
|
|
/** @type {number} */
|
|
id;
|
|
|
|
constructor(id) {
|
|
this.id = id;
|
|
}
|
|
|
|
static jsonSchema = {
|
|
type: "object",
|
|
required: ["id"],
|
|
properties: {
|
|
id: { type: "integer" },
|
|
},
|
|
};
|
|
|
|
static fromJSON(json) {
|
|
return new this(json.id);
|
|
}
|
|
}
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModPackSetSubscriptionsRequest {
|
|
static type = "request";
|
|
static src = "control";
|
|
static dst = "controller";
|
|
static permission = "core.mod_pack.subscribe";
|
|
|
|
/** @type {boolean} */
|
|
all;
|
|
/** @type {Array<number>} */
|
|
modPackIds;
|
|
|
|
constructor(all, modPackIds) {
|
|
this.all = all;
|
|
this.modPackIds = modPackIds;
|
|
}
|
|
|
|
static jsonSchema = {
|
|
type: "object",
|
|
required: ["all", "modPackIds"],
|
|
properties: {
|
|
all: { type: "boolean" },
|
|
modPackIds: {
|
|
type: "array",
|
|
items: { type: "integer" },
|
|
},
|
|
},
|
|
};
|
|
|
|
static fromJSON(json) {
|
|
return new this(json.all, json.modPackIds);
|
|
}
|
|
}
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModGetRequest {
|
|
static type = "request";
|
|
static src = "control";
|
|
static dst = "controller";
|
|
static permission = "core.mod.get";
|
|
|
|
/** @type {string} */
|
|
name;
|
|
/** @type {string} */
|
|
version;
|
|
|
|
constructor(name, version) {
|
|
this.name = name;
|
|
this.version = version;
|
|
}
|
|
|
|
static jsonSchema = {
|
|
type: "object",
|
|
required: ["name", "version"],
|
|
properties: {
|
|
name: { type: "string" },
|
|
version: { type: "string" },
|
|
},
|
|
};
|
|
|
|
static fromJSON(json) {
|
|
return new this(json.name, json.version);
|
|
}
|
|
|
|
static Response = ModInfo;
|
|
}
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModListRequest {
|
|
static type = "request";
|
|
static src = "control";
|
|
static dst = "controller";
|
|
static permission = "core.mod.list";
|
|
static Response = jsonArray(ModInfo);
|
|
}
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModSearchRequest {
|
|
static type = "request";
|
|
static src = "control";
|
|
static dst = "controller";
|
|
static permission = "core.mod.search";
|
|
|
|
/** @type {string} */
|
|
query;
|
|
/** @type {string} */
|
|
factorioVersion;
|
|
/** @type {number} */
|
|
page;
|
|
/** @type {number|undefined} */
|
|
pageSize;
|
|
/** @type {string|undefined} */
|
|
sort;
|
|
/** @type {string|undefined} */
|
|
sortOrder;
|
|
|
|
constructor(query, factorioVersion, page, pageSize, sort, sortOrder) {
|
|
this.query = query;
|
|
this.factorioVersion = factorioVersion;
|
|
this.page = page;
|
|
if (pageSize) { this.pageSize = pageSize; }
|
|
if (sort) { this.sort = sort; }
|
|
if (sortOrder) { this.sortOrder = sortOrder; }
|
|
}
|
|
|
|
static jsonSchema = {
|
|
type: "object",
|
|
required: ["query", "factorioVersion", "page"],
|
|
properties: {
|
|
query: { type: "string" },
|
|
factorioVersion: { type: "string" },
|
|
pageSize: { type: "integer" },
|
|
page: { type: "integer" },
|
|
sort: { type: "string" },
|
|
sortOrder: { type: "string" },
|
|
},
|
|
};
|
|
|
|
static fromJSON(json) {
|
|
return new this(json.query, json.factorioVersion, json.page, json.pageSize, json.sort, json.sortOrder);
|
|
}
|
|
}
|
|
ModSearchRequest.Response = class Response {
|
|
/** @type {Array<string>} */
|
|
queryIssues;
|
|
/** @type {number} */
|
|
pageCount;
|
|
/** @type {number} */
|
|
resultCount;
|
|
/** @type {Array<object>} */
|
|
results;
|
|
|
|
constructor(queryIssues, pageCount, resultCount, results) {
|
|
this.queryIssues = queryIssues;
|
|
this.pageCount = pageCount;
|
|
this.resultCount = resultCount;
|
|
this.results = results;
|
|
}
|
|
|
|
static jsonSchema = {
|
|
type: "object",
|
|
required: ["queryIssues", "pageCount", "resultCount", "results"],
|
|
properties: {
|
|
queryIssues: {
|
|
type: "array",
|
|
items: { type: "string" },
|
|
},
|
|
pageCount: { type: "integer" },
|
|
resultCount: { type: "integer" },
|
|
results: {
|
|
type: "array",
|
|
items: {
|
|
type: "object",
|
|
properties: {
|
|
name: { type: "string" },
|
|
versions: {
|
|
type: "array",
|
|
items: ModInfo.jsonSchema,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
static fromJSON(json) {
|
|
let results = json.results.map(
|
|
({ name, versions }) => ({ name, versions: versions.map(mod => ModInfo.fromJSON(mod)) })
|
|
);
|
|
return new this(json.queryIssues, json.pageCount, json.resultCount, results);
|
|
}
|
|
};
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModSetSubscriptionsRequest {
|
|
static type = "request";
|
|
static src = "control";
|
|
static dst = "controller";
|
|
static permission = "core.mod.subscribe";
|
|
|
|
/** @type {boolean} */
|
|
all;
|
|
/** @type {Array<string>} */
|
|
modNames;
|
|
|
|
constructor(all, modNames) {
|
|
this.all = all;
|
|
this.modNames = modNames;
|
|
}
|
|
|
|
static jsonSchema = {
|
|
type: "object",
|
|
required: ["all", "modNames"],
|
|
properties: {
|
|
all: { type: "boolean" },
|
|
modNames: {
|
|
type: "array",
|
|
items: { type: "string" },
|
|
},
|
|
},
|
|
};
|
|
|
|
static fromJSON(json) {
|
|
return new this(json.all, json.modNames);
|
|
}
|
|
}
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModDownloadRequest {
|
|
static type = "request";
|
|
static src = "control";
|
|
static dst = "controller";
|
|
static permission = "core.mod.download";
|
|
|
|
/** @type {string} */
|
|
name;
|
|
/** @type {string} */
|
|
version;
|
|
|
|
constructor(name, version) {
|
|
this.name = name;
|
|
this.version = version;
|
|
}
|
|
|
|
static jsonSchema = {
|
|
type: "object",
|
|
required: ["name", "version"],
|
|
properties: {
|
|
name: { type: "string" },
|
|
version: { type: "string" },
|
|
},
|
|
};
|
|
|
|
static fromJSON(json) {
|
|
return new this(json.name, json.version);
|
|
}
|
|
|
|
static Response = JsonString;
|
|
}
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModDeleteRequest {
|
|
static type = "request";
|
|
static src = "control";
|
|
static dst = "controller";
|
|
static permission = "core.mod.delete";
|
|
|
|
/** @type {string} */
|
|
name;
|
|
/** @type {string} */
|
|
version;
|
|
|
|
constructor(name, version) {
|
|
this.name = name;
|
|
this.version = version;
|
|
}
|
|
|
|
static jsonSchema = {
|
|
type: "object",
|
|
required: ["name", "version"],
|
|
properties: {
|
|
name: { type: "string" },
|
|
version: { type: "string" },
|
|
},
|
|
};
|
|
|
|
static fromJSON(json) {
|
|
return new this(json.name, json.version);
|
|
}
|
|
}
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModPackUpdateEvent {
|
|
static type = "event";
|
|
static src = "controller";
|
|
static dst = "control";
|
|
|
|
/** @type {module:lib/data.ModPack} */
|
|
modPack;
|
|
|
|
constructor(modPack) {
|
|
this.modPack = modPack;
|
|
}
|
|
|
|
static jsonSchema = {
|
|
type: "object",
|
|
required: ["modPack"],
|
|
properties: {
|
|
modPack: ModPack.jsonSchema,
|
|
},
|
|
};
|
|
|
|
static fromJSON(json) {
|
|
return new this(ModPack.fromJSON(json.modPack));
|
|
}
|
|
}
|
|
|
|
/* @memberof module:lib/data */
|
|
class ModUpdateEvent {
|
|
static type = "event";
|
|
static src = "controller";
|
|
static dst = "control";
|
|
|
|
/** @type {module:lib/data.ModInfo} */
|
|
mod;
|
|
|
|
constructor(mod) {
|
|
this.mod = mod;
|
|
}
|
|
|
|
static jsonSchema = {
|
|
type: "object",
|
|
required: ["mod"],
|
|
properties: {
|
|
mod: ModInfo.jsonSchema,
|
|
},
|
|
};
|
|
|
|
static fromJSON(json) {
|
|
return new this(ModInfo.fromJSON(json.mod));
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
ModPackGetRequest,
|
|
ModPackGetDefaultRequest,
|
|
ModPackListRequest,
|
|
ModPackCreateRequest,
|
|
ModPackUpdateRequest,
|
|
ModPackDeleteRequest,
|
|
ModPackSetSubscriptionsRequest,
|
|
ModGetRequest,
|
|
ModListRequest,
|
|
ModSearchRequest,
|
|
ModSetSubscriptionsRequest,
|
|
ModDownloadRequest,
|
|
ModDeleteRequest,
|
|
ModPackUpdateEvent,
|
|
ModUpdateEvent,
|
|
};
|