mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 15:04:52 +09:00
The lib library was written with the assumption that there's only one copy of it in the whole application and everything imports that one copy. Unfortunately there are quite a few circumstances were package managers end up installing multiple copies of lib and then different parts use different versions of the library. This causes Clusterio to break with strange and hard to track down errors. Most notably permissions and link messages for a plugin may get registered to one copy of lib while the controller uses a different copy and thus does not see or register that what the plugin added exists. Detect this condition when importing @clusterio/lib and throw an error when it has been imported twice in order to prevent the broken installation from causing future hard to debug issues.
99 lines
3.0 KiB
TypeScript
99 lines
3.0 KiB
TypeScript
|
|
/**
|
|
* Helpers for loading plugins in Node.js
|
|
* @module lib/plugin_loader
|
|
*/
|
|
import path from "path";
|
|
import fs from "fs/promises";
|
|
import * as libErrors from "./errors";
|
|
import * as libPlugin from "./plugin";
|
|
import { logger } from "./logging";
|
|
|
|
|
|
/**
|
|
* Load plugin information
|
|
*
|
|
* Loads plugin info modules for the paths to the given plugins. Once
|
|
* loaded the info modules will not be reloaded should this function be
|
|
* called again.
|
|
*
|
|
* @param pluginList -
|
|
* Mapping of plugin name to require path for the plugins to load.
|
|
* @returns Array of plugin info modules.
|
|
*/
|
|
export async function loadPluginInfos(pluginList: Map<string, string>) {
|
|
let plugins: libPlugin.PluginNodeEnvInfo[] = [];
|
|
for (let [pluginName, pluginPath] of pluginList) {
|
|
let pluginInfo: libPlugin.PluginNodeEnvInfo;
|
|
let pluginPackage: { name?: string, version: string, main?: string, private?: boolean };
|
|
|
|
// Check if plugin path exists, otherwise remove it
|
|
try {
|
|
require.resolve(pluginPath);
|
|
} catch {
|
|
let errMsg = `Plugin path ${pluginPath} does not exist`;
|
|
try {
|
|
await fs.access(pluginPath, fs.constants.F_OK);
|
|
errMsg = `Plugin path ${pluginPath} missing index or main file`;
|
|
} catch {}
|
|
logger.error(`${errMsg}, not loading ${pluginName}`);
|
|
pluginList.delete(pluginName);
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
pluginInfo = require(pluginPath).plugin;
|
|
pluginPackage = require(path.posix.join(pluginPath, "package.json"));
|
|
} catch (err: any) {
|
|
if (err.code === "InstallationError") {
|
|
throw err;
|
|
}
|
|
throw new libErrors.PluginError(pluginName, err);
|
|
}
|
|
|
|
if (typeof pluginInfo !== "object") {
|
|
throw new libErrors.EnvironmentError(
|
|
`Expected plugin at ${pluginPath} to export an object named 'plugin' but got ${typeof pluginInfo}`,
|
|
);
|
|
}
|
|
|
|
if (pluginInfo.name !== pluginName) {
|
|
throw new libErrors.EnvironmentError(
|
|
`Expected plugin at ${pluginPath} to be named ${pluginName} but got ${pluginInfo.name}`
|
|
);
|
|
}
|
|
|
|
// migrate: ignore incompatible old plugins
|
|
if (pluginInfo.messages && !(pluginInfo.messages instanceof Array)) {
|
|
logger.warn(`Ignoring incompatible pre alpha.14 plugin ${pluginName}`);
|
|
continue;
|
|
}
|
|
|
|
pluginInfo.requirePath = pluginPath;
|
|
pluginInfo.version = pluginPackage.version;
|
|
pluginInfo.npmPackage = !pluginPackage.private && pluginPath === pluginPackage.name ? pluginPath : undefined;
|
|
plugins.push(pluginInfo);
|
|
}
|
|
return plugins;
|
|
}
|
|
|
|
export async function loadPluginClass<Class extends { new (...args: any): any }>(
|
|
pluginName: string,
|
|
requirePath: string,
|
|
className: string,
|
|
pluginClass: Class,
|
|
): Promise<Class> {
|
|
let entrypoint = require(requirePath);
|
|
if (!entrypoint[className]) {
|
|
throw new libErrors.PluginError(pluginName,
|
|
new Error(`Expected ${requirePath} to export a class named ${className}`)
|
|
);
|
|
}
|
|
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}`)
|
|
);
|
|
}
|
|
return entrypoint[className];
|
|
}
|