mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 07:43:12 +09:00
Add typed hook system for plugins
Replace invokeHook and the per plugin on* methods with AsyncHook collections on the controller, host, instance, clusterioctl and web control objects. Plugin entrypoints export a default function that receives a load context and attaches handlers to the hooks it needs. Class exports still load through fromContext with a deprecation warning. The create templates and docs use the entrypoint function. This is clusterio/clusterio#921 recreated on top of master after the ESM migration and the PluginDeclaration permissions change. Closes #730 Co-Authored-By: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
8c03ac15ab
commit
7122cbccf8
64 files changed
+1878
-1184
No files matched your search
+20
-26
@@ -1,6 +1,5 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "path";
|
||||
import events from "events";
|
||||
import pidusage from "pidusage";
|
||||
import semver from "semver";
|
||||
import setBlocking from "set-blocking";
|
||||
@@ -15,9 +14,7 @@ import { logger } from "@clusterio/lib";
|
||||
import type { HostConnector } from "../host.js";
|
||||
import Instance from "./Instance.js";
|
||||
import InstanceConnection from "./InstanceConnection.js";
|
||||
import BaseHostPlugin from "./BaseHostPlugin.js";
|
||||
|
||||
const finished = util.promisify(stream.finished);
|
||||
import { BaseHostPlugin, HostHooks } from "./BaseHostPlugin.js";
|
||||
|
||||
|
||||
function checkRequestSaveName(name: string) {
|
||||
@@ -273,8 +270,10 @@ export default class Host extends lib.Link {
|
||||
pluginInfos: lib.PluginNodeEnvInfo[];
|
||||
config: lib.HostConfig;
|
||||
|
||||
/** Mapping of plugin name to loaded plugin */
|
||||
plugins: Map<string, BaseHostPlugin> = new Map();
|
||||
/** Hooks which plugins can attach to */
|
||||
hooks = new HostHooks(logger);
|
||||
/** Plugins which are currently loaded */
|
||||
loadedPlugins: Set<lib.PluginNodeEnvInfo> = new Set();
|
||||
|
||||
/** A map from instance id to instance connection. Only present when instance is running. */
|
||||
instanceConnections = new Map<number, InstanceConnection>();
|
||||
@@ -333,7 +332,7 @@ export default class Host extends lib.Link {
|
||||
if (name === "host.name" || name === "host.public_address") {
|
||||
this.sendHostUpdate();
|
||||
}
|
||||
lib.invokeHook(this.plugins, "onHostConfigFieldChanged", name, curr, prev);
|
||||
this.hooks.hostConfigFieldChanged.invoke(name, curr, prev);
|
||||
});
|
||||
|
||||
this.connector.on("hello", data => {
|
||||
@@ -385,9 +384,7 @@ export default class Host extends lib.Link {
|
||||
for (let instanceConnection of this.instanceConnections.values()) {
|
||||
instanceConnection.send(message);
|
||||
}
|
||||
for (let plugin of this.plugins.values()) {
|
||||
plugin.onControllerConnectionEvent(event);
|
||||
}
|
||||
this.hooks.controllerConnectionEvent.invoke(event);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -424,6 +421,7 @@ export default class Host extends lib.Link {
|
||||
}
|
||||
|
||||
async loadPlugins() {
|
||||
const context = { logger, host: this };
|
||||
for (let pluginInfo of this.pluginInfos) {
|
||||
if (
|
||||
!pluginInfo.hostEntrypoint && !pluginInfo.instanceEntrypoint
|
||||
@@ -437,20 +435,16 @@ export default class Host extends lib.Link {
|
||||
continue;
|
||||
}
|
||||
|
||||
let HostPluginClass = BaseHostPlugin;
|
||||
try {
|
||||
if (pluginInfo.hostEntrypoint) {
|
||||
HostPluginClass = await lib.loadPluginClass(
|
||||
pluginInfo.name,
|
||||
path.posix.join(pluginInfo.requirePath, pluginInfo.hostEntrypoint),
|
||||
"HostPlugin",
|
||||
BaseHostPlugin,
|
||||
);
|
||||
}
|
||||
await lib.loadPlugin(
|
||||
pluginInfo,
|
||||
"host",
|
||||
context,
|
||||
"HostPlugin",
|
||||
BaseHostPlugin,
|
||||
);
|
||||
|
||||
let hostPlugin = new HostPluginClass(pluginInfo, this, logger);
|
||||
await hostPlugin.init();
|
||||
this.plugins.set(pluginInfo.name, hostPlugin);
|
||||
this.loadedPlugins.add(pluginInfo);
|
||||
|
||||
} catch (err: any) {
|
||||
if (err.code === "InstallationError") {
|
||||
@@ -922,7 +916,7 @@ export default class Host extends lib.Link {
|
||||
}
|
||||
|
||||
let results = [];
|
||||
let pluginResults = await lib.invokeHook(this.plugins, "onMetrics");
|
||||
let pluginResults = await this.hooks.metrics.collect();
|
||||
for (let metricIterator of pluginResults) {
|
||||
for await (let metric of metricIterator) {
|
||||
results.push(metric);
|
||||
@@ -1154,7 +1148,7 @@ export default class Host extends lib.Link {
|
||||
async handlePluginListRequest(request: lib.PluginListRequest) {
|
||||
return this.pluginInfos.map(pluginInfo => lib.PluginDetails.fromNodeEnvInfo(
|
||||
pluginInfo,
|
||||
this.plugins.has(pluginInfo.name),
|
||||
this.loadedPlugins.has(pluginInfo),
|
||||
this.config.get(`${pluginInfo.name}.load_plugin`),
|
||||
));
|
||||
}
|
||||
@@ -1215,7 +1209,7 @@ export default class Host extends lib.Link {
|
||||
}
|
||||
|
||||
async prepareDisconnect() {
|
||||
await lib.invokeHook(this.plugins, "onPrepareControllerDisconnect", this);
|
||||
await this.hooks.prepareControllerDisconnect.invoke(this);
|
||||
for (let instanceConnection of this.instanceConnections.values()) {
|
||||
await instanceConnection.send(new lib.PrepareControllerDisconnectRequest());
|
||||
}
|
||||
@@ -1232,7 +1226,7 @@ export default class Host extends lib.Link {
|
||||
}
|
||||
this._shuttingDown = true;
|
||||
|
||||
await lib.invokeHook(this.plugins, "onShutdown");
|
||||
await this.hooks.shutdown.invoke();
|
||||
|
||||
for (let instanceConnection of this.instanceConnections.values()) {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user