mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 00:13:13 +09:00
Load all plugin types through one short lived PluginLoader
PluginLoader.load takes the plugin type, context, export name and base class as options and is used by the controller, host, instance, ctl, web interface and migrations. Module imports go through #plugin_module so the node variant stays out of the web bundle. loadPlugin.ts and the loading half of plugin_loader.ts are merged into it. The loader lives for one load. Plugins reach it through context.plugins, with waitFor(name, timeoutMs) and checkInstalled(name), replacing the waitForPlugin methods. Waiting on a plugin that is not in the load list or failed to load settles right away, as loading can't finish while the waiting entrypoint is still running. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7f688d2ed0
commit
c1ea4d470b
20 files changed
+500
-522
No files matched your search
+186
-59
@@ -1,72 +1,199 @@
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
import * as mock from "../mock.js";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { BaseControllerPlugin } from "@clusterio/controller";
|
||||
|
||||
describe("lib/PluginLoader", function() {
|
||||
let dir;
|
||||
let logger;
|
||||
let loader;
|
||||
let controller;
|
||||
before(async function() {
|
||||
// Inside the repo so the class entrypoints can import @clusterio/controller
|
||||
dir = path.resolve("temp", "test", "plugin_loader");
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
const entrypoints = {
|
||||
"function.js": `export default async function(context) {
|
||||
context.controller.loadedWith = context;
|
||||
return { api: true };
|
||||
}`,
|
||||
"class.js": `import { BaseControllerPlugin } from "@clusterio/controller";
|
||||
export class ControllerPlugin extends BaseControllerPlugin {
|
||||
async init() { this.controller.loadedWith = this; }
|
||||
async onSaveData() { }
|
||||
}`,
|
||||
"throwing_class.js": `import { BaseControllerPlugin } from "@clusterio/controller";
|
||||
export class ControllerPlugin extends BaseControllerPlugin {
|
||||
async init() { throw new Error("init failed"); }
|
||||
async onSaveData() { }
|
||||
}`,
|
||||
"empty.js": "",
|
||||
"wrong_parent.js": "export class ControllerPlugin {};\n",
|
||||
"throwing.js": `export default async function({ plugin }) {
|
||||
await new Promise(resolve => setTimeout(resolve, plugin.delay ?? 0));
|
||||
throw new Error(\`\${plugin.name} failed\`);
|
||||
}`,
|
||||
// Waits for the plugin named in plugin.waitFor and returns what it got
|
||||
"waiting.js": `export default async function({ plugin, plugins }) {
|
||||
const got = plugin.waitFor ? await plugins.waitFor(plugin.waitFor, plugin.timeoutMs) : undefined;
|
||||
return { name: plugin.name, got };
|
||||
}`,
|
||||
};
|
||||
for (const [file, content] of Object.entries(entrypoints)) {
|
||||
await fs.writeFile(path.join(dir, file), content);
|
||||
}
|
||||
});
|
||||
after(async function() {
|
||||
await fs.rm(dir, { recursive: true, force: true });
|
||||
});
|
||||
beforeEach(function() {
|
||||
logger = { warnings: [], warn(msg) { this.warnings.push(msg); } };
|
||||
loader = new lib.PluginLoader(logger);
|
||||
controller = new mock.MockController();
|
||||
logger = new mock.MockLogger();
|
||||
logger.warnings = [];
|
||||
logger.warn = msg => logger.warnings.push(msg);
|
||||
});
|
||||
const infos = (...names) => names.map(name => ({ name }));
|
||||
|
||||
it("should let a plugin await a plugin later in the list while loading", async function() {
|
||||
const apis = { b: { b: true } };
|
||||
await loader.load(infos("a", "b"), async ({ name }) => {
|
||||
if (name === "a") {
|
||||
apis.a = { gotB: await loader.waitFor("a", "b") };
|
||||
}
|
||||
return apis[name];
|
||||
function info(name, entrypoint, extra = {}) {
|
||||
return { name, packagePath: path.join(dir, "package.json"), controllerEntrypoint: entrypoint, ...extra };
|
||||
}
|
||||
function load(pluginInfos, options = {}) {
|
||||
return lib.PluginLoader.load({
|
||||
type: "controller",
|
||||
context: { controller },
|
||||
logger,
|
||||
installed: pluginInfos,
|
||||
load: pluginInfos,
|
||||
exportName: "ControllerPlugin",
|
||||
baseClass: BaseControllerPlugin,
|
||||
...options,
|
||||
});
|
||||
assert.deepEqual(apis.a, { gotB: { b: true } });
|
||||
assert.equal(await loader.waitFor("c", "a"), apis.a);
|
||||
assert.deepEqual(logger.warnings, []);
|
||||
});
|
||||
it("should resolve undefined with a warning once loading finishes without the plugin", async function() {
|
||||
const result = loader.waitFor("a", "b");
|
||||
await loader.load(infos("c"), async () => ({}));
|
||||
assert.equal(await result, undefined);
|
||||
assert.equal(await loader.waitFor("a", "d"), undefined);
|
||||
assert.deepEqual(logger.warnings, [
|
||||
"Plugin a waited for plugin b which is not loaded",
|
||||
"Plugin a waited for plugin d which is not loaded",
|
||||
]);
|
||||
});
|
||||
it("should resolve undefined with a warning on timeout", async function() {
|
||||
const result = loader.waitFor("a", "b", 10);
|
||||
const other = loader.waitFor("c", "b");
|
||||
assert.equal(await result, undefined);
|
||||
assert.deepEqual(logger.warnings, ["Plugin a timed out after 10ms waiting for plugin b"]);
|
||||
await loader.load(infos("b"), async () => "api");
|
||||
assert.equal(await other, "api");
|
||||
});
|
||||
it("should break a wait cycle with the timeout", async function() {
|
||||
const got = {};
|
||||
await loader.load(infos("a", "b"), async ({ name }) => {
|
||||
got[name] = await loader.waitFor(name, name === "a" ? "b" : "a", name === "a" ? 10 : 1000);
|
||||
return name;
|
||||
}
|
||||
async function loadOne(pluginInfo) {
|
||||
const { loaded } = await load([pluginInfo]);
|
||||
return loaded.get(pluginInfo);
|
||||
}
|
||||
|
||||
describe("loading", function() {
|
||||
it("should load plugins without the entrypoint as undefined", async function() {
|
||||
const pluginInfo = { name: "test" };
|
||||
const { loaded } = await load([pluginInfo]);
|
||||
assert.deepEqual([...loaded], [[pluginInfo, undefined]]);
|
||||
assert.equal(controller.loadedWith, undefined);
|
||||
});
|
||||
it("should call the default export with the load context", async function() {
|
||||
const pluginInfo = info("test", "function.js");
|
||||
assert.deepEqual(await loadOne(pluginInfo), { api: true });
|
||||
assert.equal(controller.loadedWith.controller, controller);
|
||||
assert.equal(controller.loadedWith.plugin, pluginInfo);
|
||||
assert.equal(controller.loadedWith.logger, logger);
|
||||
assert.equal(typeof controller.loadedWith.plugins.waitFor, "function");
|
||||
assert.deepEqual(logger.warnings, []);
|
||||
});
|
||||
it("should build the context per plugin when given a function", async function() {
|
||||
const pluginInfo = info("test", "function.js");
|
||||
await load([pluginInfo], { context: p => ({ controller, name: p.name }) });
|
||||
assert.equal(controller.loadedWith.name, "test");
|
||||
});
|
||||
it("should load a deprecated class export", async function() {
|
||||
const pluginInfo = info("test", "class.js");
|
||||
assert.equal(await loadOne(pluginInfo), controller.loadedWith);
|
||||
assert(controller.loadedWith instanceof BaseControllerPlugin);
|
||||
assert.equal(controller.loadedWith.info, pluginInfo);
|
||||
assert.deepEqual([...controller.hooks.save.attached], ["test"]);
|
||||
assert.deepEqual(logger.warnings, ["Plugin test is using deprecated class export"]);
|
||||
});
|
||||
it("should not accept class exports without exportName", async function() {
|
||||
await assert.rejects(
|
||||
load([info("test", "class.js")], { exportName: undefined }),
|
||||
{ message: "PluginError: Expected controller plugin test to export a default function" },
|
||||
);
|
||||
});
|
||||
it("should detach hooks if init throws", async function() {
|
||||
await assert.rejects(
|
||||
loadOne(info("test", "throwing_class.js")),
|
||||
err => err instanceof lib.PluginError && err.original.message === "init failed",
|
||||
);
|
||||
assert.equal(controller.hooks.size, 0);
|
||||
});
|
||||
it("should throw if neither a function nor a class is exported", async function() {
|
||||
await assert.rejects(
|
||||
loadOne(info("test", "empty.js")),
|
||||
{ message: "PluginError: Plugin test must export either a default function or ControllerPlugin class" },
|
||||
);
|
||||
});
|
||||
it("should throw if the class is not a subclass of the base class", async function() {
|
||||
await assert.rejects(
|
||||
loadOne(info("test", "wrong_parent.js")),
|
||||
{ message: "PluginError: Expected ControllerPlugin exported from test to extend BaseControllerPlugin" },
|
||||
);
|
||||
});
|
||||
it("should finish loading the rest before throwing the first error in list order", async function() {
|
||||
const ok = info("ok", "function.js");
|
||||
await assert.rejects(
|
||||
load([info("a", "throwing.js", { delay: 10 }), info("b", "throwing.js"), ok]),
|
||||
err => err instanceof lib.PluginError && err.pluginName === "a",
|
||||
);
|
||||
assert.equal(controller.loadedWith.plugin, ok);
|
||||
});
|
||||
it("should return failures with continueOnError", async function() {
|
||||
const a = info("a", "throwing.js");
|
||||
const ok = info("ok", "function.js");
|
||||
const { loaded, failed } = await load([a, ok], { continueOnError: true });
|
||||
assert.deepEqual([...loaded.keys()], [ok]);
|
||||
assert.deepEqual([...failed.keys()], [a]);
|
||||
assert(failed.get(a) instanceof lib.PluginError);
|
||||
});
|
||||
assert.deepEqual(got, { a: undefined, b: "a" });
|
||||
assert.deepEqual(logger.warnings, ["Plugin a timed out after 10ms waiting for plugin b"]);
|
||||
});
|
||||
it("should finish loading the rest before throwing the first error in list order", async function() {
|
||||
const loaded = [];
|
||||
await assert.rejects(
|
||||
loader.load(infos("a", "b", "c"), async ({ name }) => {
|
||||
if (name === "a") {
|
||||
await new Promise(resolve => setTimeout(resolve, 10));
|
||||
throw new Error("a failed");
|
||||
}
|
||||
if (name === "b") {
|
||||
throw new Error("b failed");
|
||||
}
|
||||
loaded.push(name);
|
||||
return name;
|
||||
}),
|
||||
{ message: "a failed" },
|
||||
);
|
||||
assert.deepEqual(loaded, ["c"]);
|
||||
assert.equal(await loader.waitFor("x", "a"), undefined);
|
||||
assert.deepEqual(logger.warnings, ["Plugin x waited for plugin a which is not loaded"]);
|
||||
|
||||
describe("plugins.waitFor()", function() {
|
||||
it("should resolve to what a plugin later in the list returned", async function() {
|
||||
const { loaded } = await load([info("a", "waiting.js", { waitFor: "b" }), info("b", "waiting.js")]);
|
||||
const b = { name: "b", got: undefined };
|
||||
assert.deepEqual([...loaded.values()], [{ name: "a", got: b }, b]);
|
||||
assert.deepEqual(logger.warnings, []);
|
||||
});
|
||||
it("should resolve after loading finished", async function() {
|
||||
const pluginInfo = info("test", "function.js");
|
||||
await load([pluginInfo, info("b", "waiting.js")]);
|
||||
const { plugins } = controller.loadedWith;
|
||||
assert.deepEqual(await plugins.waitFor("b"), { name: "b", got: undefined });
|
||||
assert.equal(await plugins.waitFor("missing"), undefined);
|
||||
assert.deepEqual(logger.warnings, ["Plugin test waited for plugin missing which is not loaded"]);
|
||||
});
|
||||
it("should resolve undefined with a warning once loading finishes without the plugin", async function() {
|
||||
const installed = [info("off", "function.js")];
|
||||
const waiting = info("a", "waiting.js", { waitFor: "off", timeoutMs: 60_000 });
|
||||
const { loaded } = await load([waiting], { installed });
|
||||
assert.deepEqual(loaded.get(waiting), { name: "a", got: undefined });
|
||||
assert.deepEqual(logger.warnings, ["Plugin a waited for plugin off which is not loaded"]);
|
||||
});
|
||||
it("should resolve undefined with a warning if the plugin failed", async function() {
|
||||
const waiting = info("a", "waiting.js", { waitFor: "b", timeoutMs: 60_000 });
|
||||
const { loaded } = await load([waiting, info("b", "throwing.js")], { continueOnError: true });
|
||||
assert.deepEqual(loaded.get(waiting), { name: "a", got: undefined });
|
||||
assert.deepEqual(logger.warnings, ["Plugin a waited for plugin b which is not loaded"]);
|
||||
});
|
||||
it("should break a wait cycle with the timeout", async function() {
|
||||
const { loaded } = await load([
|
||||
info("a", "waiting.js", { waitFor: "b", timeoutMs: 10 }),
|
||||
info("b", "waiting.js", { waitFor: "a", timeoutMs: 60_000 }),
|
||||
]);
|
||||
const a = { name: "a", got: undefined };
|
||||
assert.deepEqual([...loaded.values()], [a, { name: "b", got: a }]);
|
||||
assert.deepEqual(logger.warnings, ["Plugin a timed out after 10ms waiting for plugin b"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("plugins.checkInstalled()", function() {
|
||||
it("should check the installed list", async function() {
|
||||
const pluginInfo = info("test", "function.js");
|
||||
await load([pluginInfo], { installed: [pluginInfo, { name: "off" }] });
|
||||
const { plugins } = controller.loadedWith;
|
||||
assert.equal(plugins.checkInstalled("test"), true);
|
||||
assert.equal(plugins.checkInstalled("off"), true);
|
||||
assert.equal(plugins.checkInstalled("missing"), false);
|
||||
});
|
||||
});
|
||||
});
|
||||
+7
-118
@@ -5,7 +5,6 @@ import { pathToFileURL } from "node:url";
|
||||
|
||||
import * as mock from "../mock.js";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { BaseControllerPlugin } from "@clusterio/controller";
|
||||
import { computePluginStatuses, planUpdate } from "../../packages/web_ui/src/util/plugin_status.ts";
|
||||
|
||||
|
||||
@@ -105,10 +104,13 @@ describe("lib/plugin_loader", function() {
|
||||
assert.equal(pluginInfo.packagePath, path.join(packageDir, "package.json"));
|
||||
|
||||
const controller = new mock.MockController();
|
||||
await lib.loadPlugin(
|
||||
pluginInfo, "controller", { controller, metrics: {}, logger: new mock.MockLogger() },
|
||||
"ControllerPlugin", BaseControllerPlugin,
|
||||
);
|
||||
await lib.PluginLoader.load({
|
||||
type: "controller",
|
||||
context: { controller },
|
||||
logger: new mock.MockLogger(),
|
||||
installed: [pluginInfo],
|
||||
load: [pluginInfo],
|
||||
});
|
||||
assert.equal(controller.loadedNpmPlugin, true);
|
||||
});
|
||||
});
|
||||
@@ -165,119 +167,6 @@ describe("lib/plugin_loader", function() {
|
||||
);
|
||||
});
|
||||
});
|
||||
describe("loadPlugin()", function() {
|
||||
const baseDir = path.join("temp", "test", "plugin");
|
||||
const functionPlugin = path.resolve(baseDir, "function_plugin");
|
||||
const classPlugin = path.resolve(baseDir, "class_plugin");
|
||||
const throwingClassPlugin = path.resolve(baseDir, "throwing_class_plugin");
|
||||
const missingClassPlugin = path.resolve(baseDir, "missing_class_plugin");
|
||||
const wrongParentClassPlugin = path.resolve(baseDir, "wrong_parent_class_plugin");
|
||||
let controller;
|
||||
let logger;
|
||||
let context;
|
||||
|
||||
before(async function() {
|
||||
async function writeEntrypoint(pluginPath, content) {
|
||||
await fs.mkdir(pluginPath, { recursive: true });
|
||||
await fs.writeFile(path.join(pluginPath, "controller.js"), content);
|
||||
}
|
||||
|
||||
await writeEntrypoint(functionPlugin, `
|
||||
export default async function(context) {
|
||||
context.controller.loadedWith = context;
|
||||
return { api: true };
|
||||
}
|
||||
`);
|
||||
await writeEntrypoint(classPlugin, `
|
||||
import { BaseControllerPlugin } from "@clusterio/controller";
|
||||
export class ControllerPlugin extends BaseControllerPlugin {
|
||||
async init() { this.controller.loadedWith = this; }
|
||||
async onSaveData() { }
|
||||
}
|
||||
`);
|
||||
await writeEntrypoint(throwingClassPlugin, `
|
||||
import { BaseControllerPlugin } from "@clusterio/controller";
|
||||
export class ControllerPlugin extends BaseControllerPlugin {
|
||||
async init() { throw new Error("init failed"); }
|
||||
async onSaveData() { }
|
||||
}
|
||||
`);
|
||||
await writeEntrypoint(missingClassPlugin, "");
|
||||
await writeEntrypoint(wrongParentClassPlugin, "export class ControllerPlugin {};\n");
|
||||
});
|
||||
|
||||
beforeEach(function() {
|
||||
controller = new mock.MockController();
|
||||
logger = new mock.MockLogger();
|
||||
logger.warnings = [];
|
||||
logger.warn = msg => logger.warnings.push(msg);
|
||||
context = { controller, metrics: {}, logger };
|
||||
});
|
||||
|
||||
function info(requirePath, entrypoint = "controller.js") {
|
||||
return {
|
||||
name: "test",
|
||||
requirePath,
|
||||
packagePath: path.join(requirePath, "package.json"),
|
||||
controllerEntrypoint: entrypoint,
|
||||
};
|
||||
}
|
||||
|
||||
it("should do nothing when the entrypoint is not set", async function() {
|
||||
await lib.loadPlugin(
|
||||
{ name: "test", requirePath: missingClassPlugin }, "controller", context,
|
||||
"ControllerPlugin", BaseControllerPlugin,
|
||||
);
|
||||
assert.equal(controller.loadedWith, undefined);
|
||||
});
|
||||
it("should call the default export with the load context", async function() {
|
||||
const pluginInfo = info(functionPlugin);
|
||||
const result = await lib.loadPlugin(
|
||||
pluginInfo, "controller", context, "ControllerPlugin", BaseControllerPlugin,
|
||||
);
|
||||
assert.deepEqual(result, { api: true });
|
||||
assert.equal(controller.loadedWith.controller, controller);
|
||||
assert.equal(controller.loadedWith.plugin, pluginInfo);
|
||||
assert.equal(controller.loadedWith.logger, logger);
|
||||
assert.deepEqual(logger.warnings, []);
|
||||
});
|
||||
it("should load a deprecated class export", async function() {
|
||||
const pluginInfo = info(classPlugin);
|
||||
const result = await lib.loadPlugin(
|
||||
pluginInfo, "controller", context, "ControllerPlugin", BaseControllerPlugin,
|
||||
);
|
||||
assert.equal(result, controller.loadedWith);
|
||||
assert(controller.loadedWith instanceof BaseControllerPlugin);
|
||||
assert.equal(controller.loadedWith.info, pluginInfo);
|
||||
assert.deepEqual([...controller.hooks.save.attached], ["test"]);
|
||||
assert.deepEqual(logger.warnings, ["Plugin test is using deprecated class export"]);
|
||||
});
|
||||
it("should detach hooks if init throws", async function() {
|
||||
await assert.rejects(
|
||||
lib.loadPlugin(
|
||||
info(throwingClassPlugin), "controller", context, "ControllerPlugin", BaseControllerPlugin
|
||||
),
|
||||
new Error("init failed")
|
||||
);
|
||||
assert.equal(controller.hooks.size, 0);
|
||||
});
|
||||
it("should throw if neither a function nor a class is exported", async function() {
|
||||
await assert.rejects(
|
||||
lib.loadPlugin(
|
||||
info(missingClassPlugin), "controller", context, "ControllerPlugin", BaseControllerPlugin
|
||||
),
|
||||
new Error("Plugin test must export either a default function or ControllerPlugin class")
|
||||
);
|
||||
});
|
||||
it("should throw if the class is not a subclass of the base class", async function() {
|
||||
await assert.rejects(
|
||||
lib.loadPlugin(
|
||||
info(wrongParentClassPlugin), "controller", context, "ControllerPlugin", BaseControllerPlugin
|
||||
),
|
||||
new Error("Expected ControllerPlugin exported from test to extend BaseControllerPlugin")
|
||||
);
|
||||
});
|
||||
});
|
||||
describe("loadPluginList()", async function() {
|
||||
const old_cwd = process.cwd();
|
||||
const baseDir = path.join("temp", "test", "plugin_list");
|
||||
|
||||
Reference in new issue
Block a user