Refactor message registration and validation

Split up the action of registring an event or request data class to be
decoded by the Link class and the action of registring a handler for
those requests and events.  This moves the validation work from Link
instantiation time to startup initialisation which makes logic errors
fail on startup instead of failing instantiation of the link when a
connection attempt is made.

Additionally implement a partial validation logic for messages received
using the newly refactored message registration.  This unfortunately
ended up merged with the registration refactor.
This commit is contained in:
Hornwitser committed 2023-06-03 16:47:47 +02:00
1 parent bcd94709d2
commit 7f8a700716
39 files changed
+648 -332

No files matched your search

+5
View File
@@ -1,4 +1,5 @@
"use strict";
const messages = require("./messages");
module.exports = {
name: "global_chat",
@@ -6,4 +7,8 @@ module.exports = {
description: "Forwards chat between instances.",
instanceEntrypoint: "instance",
controlEntrypoint: "control",
messages: [
messages.ChatEvent,
],
};
+1 -1
View File
@@ -20,7 +20,7 @@ function removeTags(content) {
class InstancePlugin extends libPlugin.BaseInstancePlugin {
async init() {
this.messageQueue = [];
this.instance.register(ChatEvent, this.handleChatEvent.bind(this));
this.instance.handle(ChatEvent, this.handleChatEvent.bind(this));
}
onControllerConnectionEvent(event) {
+2
View File
@@ -1,5 +1,6 @@
"use strict";
const assert = require("assert").strict;
const libLink = require("@clusterio/lib/link");
const mock = require("../../../test/mock");
const lines = require("../../../test/lib/factorio/lines");
@@ -25,6 +26,7 @@ describe("global_chat plugin", function() {
let instancePlugin;
before(async function() {
libLink.Link.register(ChatEvent);
instancePlugin = new instance.InstancePlugin(info, new mock.MockInstance(), new mock.MockHost());
await instancePlugin.init();
});
+5 -5
View File
@@ -48,11 +48,11 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
});
}, this.controller.config.get("inventory_sync.autosave_interval") * 1000);
this.controller.register(AcquireRequest, this.handleAcquireRequest.bind(this));
this.controller.register(ReleaseRequest, this.handleReleaseRequest.bind(this));
this.controller.register(UploadRequest, this.handleUploadRequest.bind(this));
this.controller.register(DownloadRequest, this.handleDownloadRequest.bind(this));
this.controller.register(DatabaseStatsRequest, this.handleDatabaseStatsRequest.bind(this));
this.controller.handle(AcquireRequest, this.handleAcquireRequest.bind(this));
this.controller.handle(ReleaseRequest, this.handleReleaseRequest.bind(this));
this.controller.handle(UploadRequest, this.handleUploadRequest.bind(this));
this.controller.handle(DownloadRequest, this.handleDownloadRequest.bind(this));
this.controller.handle(DatabaseStatsRequest, this.handleDatabaseStatsRequest.bind(this));
}
async onInstanceStatusChanged(instance) {
+8
View File
@@ -1,5 +1,6 @@
"use strict";
const { libConfig, libUsers } = require("@clusterio/lib");
let messages = require("./messages");
class ControllerConfigGroup extends libConfig.PluginConfigGroup { }
ControllerConfigGroup.defaultAccess = ["controller", "host", "control"];
@@ -51,6 +52,13 @@ module.exports = {
controllerEntrypoint: "controller",
ControllerConfigGroup,
messages: [
messages.AcquireRequest,
messages.ReleaseRequest,
messages.UploadRequest,
messages.DownloadRequest,
messages.DatabaseStatsRequest,
],
webEntrypoint: "./web",
routes: ["/inventory"],
};
+2 -2
View File
@@ -63,8 +63,8 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
this.handleVerify(req, res).catch(next);
});
this.controller.register(FetchPlayerCodeRequest, this.handleFetchPlayerCodeRequest.bind(this));
this.controller.register(SetVerifyCodeRequest, this.handleSetVerifyCodeRequest.bind(this));
this.controller.handle(FetchPlayerCodeRequest, this.handleFetchPlayerCodeRequest.bind(this));
this.controller.handle(SetVerifyCodeRequest, this.handleSetVerifyCodeRequest.bind(this));
}
async handlePlayerCode(req, res) {
+6
View File
@@ -1,5 +1,6 @@
"use strict";
let { libConfig } = require("@clusterio/lib");
let messages = require("./messages");
class ControllerConfigGroup extends libConfig.PluginConfigGroup {}
@@ -29,4 +30,9 @@ module.exports = {
instanceEntrypoint: "instance",
webEntrypoint: "./web",
ControllerConfigGroup,
messages: [
messages.FetchPlayerCodeRequest,
messages.SetVerifyCodeRequest,
],
};
+3 -3
View File
@@ -49,9 +49,9 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
this.progressBroadcastId = null;
this.progressToBroadcast = new Set();
this.controller.register(ContributionEvent, this.handleContributionEvent.bind(this));
this.controller.register(FinishedEvent, this.handleFinishedEvent.bind(this));
this.controller.register(SyncTechnologiesRequest, this.handleSyncTechnologiesRequest.bind(this));
this.controller.handle(ContributionEvent, this.handleContributionEvent.bind(this));
this.controller.handle(FinishedEvent, this.handleFinishedEvent.bind(this));
this.controller.handle(SyncTechnologiesRequest, this.handleSyncTechnologiesRequest.bind(this));
}
async onShutdown() {
+8
View File
@@ -1,4 +1,5 @@
"use strict";
const messages = require("./messages");
module.exports = {
name: "research_sync",
@@ -6,4 +7,11 @@ module.exports = {
description: "Synchronises technology research progress between instances.",
instanceEntrypoint: "instance",
controllerEntrypoint: "controller",
messages: [
messages.ContributionEvent,
messages.ProgressEvent,
messages.FinishedEvent,
messages.SyncTechnologiesRequest,
],
};
+2 -2
View File
@@ -28,8 +28,8 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
});
this.syncStarted = false;
this.instance.register(ProgressEvent, this.handleProgressEvent.bind(this));
this.instance.register(FinishedEvent, this.handleFinishedEvent.bind(this));
this.instance.handle(ProgressEvent, this.handleProgressEvent.bind(this));
this.instance.handle(FinishedEvent, this.handleFinishedEvent.bind(this));
}
async researchContribution(tech) {
+5 -5
View File
@@ -5,7 +5,7 @@ class ContributionEvent {
static type = "event";
static src = "instance";
static dst = "controller";
static json = "research_sync";
static plugin = "research_sync";
/** @type {string} */
name;
@@ -39,7 +39,7 @@ class ProgressEvent {
static type = "event";
static src = "controller";
static dst = "instance";
static json = "research_sync";
static plugin = "research_sync";
/** @type {Array<>} */
technologies;
@@ -74,9 +74,9 @@ class ProgressEvent {
class FinishedEvent {
static type = "event";
static src = "instance";
static src = ["instance", "controller"];
static dst = "instance";
static json = "research_sync";
static plugin = "research_sync";
/** @type {string} */
name;
@@ -144,7 +144,7 @@ class SyncTechnologiesRequest {
static type = "request";
static src = "instance";
static dst = "controller";
static json = "research_sync";
static plugin = "research_sync";
/** @type {Array<Technology>} */
technologies;
+4 -4
View File
@@ -97,10 +97,10 @@ class ControllerPlugin extends libPlugin.BaseControllerPlugin {
routes.addApiRoutes(this.controller.app, this.items);
this.controller.register(GetStorageRequest, this.handleGetStorageRequest.bind(this));
this.controller.register(PlaceEvent, this.handlePlaceEvent.bind(this));
this.controller.register(RemoveRequest, this.handleRemoveRequest.bind(this));
this.controller.register(SetStorageSubscriptionRequest, this.handleSetStorageSubscriptionRequest.bind(this));
this.controller.handle(GetStorageRequest, this.handleGetStorageRequest.bind(this));
this.controller.handle(PlaceEvent, this.handlePlaceEvent.bind(this));
this.controller.handle(RemoveRequest, this.handleRemoveRequest.bind(this));
this.controller.handle(SetStorageSubscriptionRequest, this.handleSetStorageSubscriptionRequest.bind(this));
}
updateStorage() {
+8
View File
@@ -1,5 +1,6 @@
"use strict";
const { libConfig, libUsers } = require("@clusterio/lib");
const messages = require("./messages");
class ControllerConfigGroup extends libConfig.PluginConfigGroup {}
ControllerConfigGroup.defaultAccess = ["controller", "host", "control"];
@@ -57,6 +58,13 @@ module.exports = {
controllerEntrypoint: "controller",
ControllerConfigGroup,
messages: [
messages.PlaceEvent,
messages.RemoveRequest,
messages.GetStorageRequest,
messages.UpdateStorageEvent,
messages.SetStorageSubscriptionRequest,
],
webEntrypoint: "./web",
routes: ["/storage"],
};
+1 -1
View File
@@ -31,7 +31,7 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
task.finally(() => { this.pendingTasks.delete(task); });
});
this.instance.register(UpdateStorageEvent, this.handleUpdateStorageEvent.bind(this));
this.instance.handle(UpdateStorageEvent, this.handleUpdateStorageEvent.bind(this));
}
async onStart() {