mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 23:33: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
@@ -8,7 +8,8 @@ import fs from "node:fs/promises";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import * as libErrors from "./errors.js";
|
||||
import * as libPlugin from "./plugin.js";
|
||||
import { logger } from "./logging.js";
|
||||
import { type Logger, logger } from "./logging.js";
|
||||
import { loadPluginEntrypoint, loadPluginClass } from "./loadPlugin.js";
|
||||
|
||||
|
||||
/**
|
||||
@@ -88,22 +89,41 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
|
||||
return plugins;
|
||||
}
|
||||
|
||||
export async function loadPluginClass<Class extends { new (...args: any): any }>(
|
||||
pluginName: string,
|
||||
requirePath: string,
|
||||
className: string,
|
||||
pluginClass: Class,
|
||||
): Promise<Class> {
|
||||
let entrypoint = await import(pathToFileURL(requirePath).href);
|
||||
if (!entrypoint[className]) {
|
||||
throw new libErrors.PluginError(pluginName,
|
||||
new Error(`Expected ${requirePath} to export a class named ${className}`)
|
||||
);
|
||||
export async function loadPlugin<
|
||||
Context extends { logger: Logger },
|
||||
Class extends libPlugin.PluginClass<Context, libPlugin.PluginNodeEnvInfo>,
|
||||
> (
|
||||
pluginInfo: libPlugin.PluginNodeEnvInfo,
|
||||
pluginType: libPlugin.PluginType,
|
||||
context: Context,
|
||||
exportName: `${string}Plugin`,
|
||||
baseClass: Class,
|
||||
) {
|
||||
const entrypoint = `${pluginType}Entrypoint` as const;
|
||||
const requirePath = pluginInfo[entrypoint];
|
||||
|
||||
if (!requirePath) {
|
||||
return;
|
||||
}
|
||||
if (!(entrypoint[className].prototype instanceof pluginClass)) {
|
||||
throw new libErrors.PluginError(pluginName,
|
||||
new Error(`Expected ${className} exported from ${requirePath} to be a subclass of ${pluginClass.name}`)
|
||||
);
|
||||
|
||||
const module = await import(pathToFileURL(path.posix.join(pluginInfo.requirePath, requirePath)).href);
|
||||
const pluginContext: libPlugin.PluginLoadContext<Context> = {
|
||||
...context,
|
||||
plugin: pluginInfo,
|
||||
logger: context.logger.child({ plugin: pluginInfo.name }),
|
||||
};
|
||||
|
||||
if (typeof module.default === "function") {
|
||||
await loadPluginEntrypoint(pluginInfo, pluginType, pluginContext, module);
|
||||
return;
|
||||
}
|
||||
return entrypoint[className];
|
||||
|
||||
// migrate: accept plugins which export classes
|
||||
if (module[exportName]) {
|
||||
pluginContext.logger.warn(`Plugin ${pluginInfo.name} is using deprecated class export`);
|
||||
await loadPluginClass(pluginInfo, pluginType, pluginContext, module, exportName, baseClass);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error(`Plugin ${pluginInfo.name} must export either a default function or ${exportName} class`);
|
||||
}
|
||||
Reference in new issue
Block a user