SillyTavern/src/plugin-loader.js

221 lines
7.0 KiB
JavaScript
Raw Normal View History

2024-10-10 21:37:22 +02:00
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as url from 'node:url';
import express from 'express';
import { getConfigValue } from './util.js';
2023-12-16 21:21:40 +01:00
const enableServerPlugins = getConfigValue('enableServerPlugins', false);
2023-12-23 23:00:20 +01:00
/**
* Map of loaded plugins.
* @type {Map<string, any>}
*/
const loadedPlugins = new Map();
2023-12-16 21:21:40 +01:00
/**
* Determine if a file is a CommonJS module.
* @param {string} file Path to file
* @returns {boolean} True if file is a CommonJS module
*/
const isCommonJS = (file) => path.extname(file) === '.js';
/**
* Determine if a file is an ECMAScript module.
* @param {string} file Path to file
* @returns {boolean} True if file is an ECMAScript module
*/
const isESModule = (file) => path.extname(file) === '.mjs';
/**
* Load and initialize server plugins from a directory if they are enabled.
* @param {import('express').Express} app Express app
* @param {string} pluginsPath Path to plugins directory
* @returns {Promise<Function>} Promise that resolves when all plugins are loaded. Resolves to a "cleanup" function to
* be called before the server shuts down.
2023-12-16 21:21:40 +01:00
*/
2024-10-10 21:37:22 +02:00
export async function loadPlugins(app, pluginsPath) {
const exitHooks = [];
2023-12-23 18:03:13 +01:00
const emptyFn = () => {};
2023-12-16 21:21:40 +01:00
// Server plugins are disabled.
if (!enableServerPlugins) {
2023-12-23 18:03:13 +01:00
return emptyFn;
2023-12-16 21:21:40 +01:00
}
// Plugins directory does not exist.
if (!fs.existsSync(pluginsPath)) {
2023-12-23 18:03:13 +01:00
return emptyFn;
2023-12-16 21:21:40 +01:00
}
const files = fs.readdirSync(pluginsPath);
// No plugins to load.
if (files.length === 0) {
2023-12-23 18:03:13 +01:00
return emptyFn;
2023-12-16 21:21:40 +01:00
}
for (const file of files) {
const pluginFilePath = path.join(pluginsPath, file);
if (fs.statSync(pluginFilePath).isDirectory()) {
await loadFromDirectory(app, pluginFilePath, exitHooks);
2023-12-17 00:23:28 +01:00
continue;
2023-12-16 21:21:40 +01:00
}
// Not a JavaScript file.
if (!isCommonJS(file) && !isESModule(file)) {
continue;
}
await loadFromFile(app, pluginFilePath, exitHooks);
2023-12-16 21:21:40 +01:00
}
// Call all plugin "exit" functions at once and wait for them to finish
return () => Promise.all(exitHooks.map(exitFn => exitFn()));
2023-12-16 21:21:40 +01:00
}
async function loadFromDirectory(app, pluginDirectoryPath, exitHooks) {
2023-12-16 21:21:40 +01:00
const files = fs.readdirSync(pluginDirectoryPath);
// No plugins to load.
if (files.length === 0) {
return;
}
// Plugin is an npm package.
const packageJsonFilePath = path.join(pluginDirectoryPath, 'package.json');
if (fs.existsSync(packageJsonFilePath)) {
if (await loadFromPackage(app, packageJsonFilePath, exitHooks)) {
2023-12-16 21:21:40 +01:00
return;
}
}
// Plugin is a CommonJS module.
const cjsFilePath = path.join(pluginDirectoryPath, 'index.js');
if (fs.existsSync(cjsFilePath)) {
if (await loadFromFile(app, cjsFilePath, exitHooks)) {
2023-12-16 21:21:40 +01:00
return;
}
}
// Plugin is an ECMAScript module.
const esmFilePath = path.join(pluginDirectoryPath, 'index.mjs');
if (fs.existsSync(esmFilePath)) {
if (await loadFromFile(app, esmFilePath, exitHooks)) {
2023-12-16 21:21:40 +01:00
return;
}
}
}
/**
* Loads and initializes a plugin from an npm package.
* @param {import('express').Express} app Express app
* @param {string} packageJsonPath Path to package.json file
* @param {Array<Function>} exitHooks Array of functions to be run on plugin exit. Will be pushed to if the plugin has
* an "exit" function.
2023-12-16 21:21:40 +01:00
* @returns {Promise<boolean>} Promise that resolves to true if plugin was loaded successfully
*/
async function loadFromPackage(app, packageJsonPath, exitHooks) {
2023-12-16 21:21:40 +01:00
try {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
if (packageJson.main) {
const pluginFilePath = path.join(path.dirname(packageJsonPath), packageJson.main);
return await loadFromFile(app, pluginFilePath, exitHooks);
2023-12-16 21:21:40 +01:00
}
} catch (error) {
console.error(`Failed to load plugin from ${packageJsonPath}: ${error}`);
}
return false;
}
/**
* Loads and initializes a plugin from a file.
* @param {import('express').Express} app Express app
* @param {string} pluginFilePath Path to plugin directory
* @param {Array.<Function>} exitHooks Array of functions to be run on plugin exit. Will be pushed to if the plugin has
* an "exit" function.
2023-12-16 21:21:40 +01:00
* @returns {Promise<boolean>} Promise that resolves to true if plugin was loaded successfully
*/
async function loadFromFile(app, pluginFilePath, exitHooks) {
2023-12-16 21:21:40 +01:00
try {
2023-12-23 18:46:32 +01:00
const fileUrl = url.pathToFileURL(pluginFilePath).toString();
const plugin = await import(fileUrl);
2023-12-16 21:21:40 +01:00
console.log(`Initializing plugin from ${pluginFilePath}`);
return await initPlugin(app, plugin, exitHooks);
2023-12-16 21:21:40 +01:00
} catch (error) {
console.error(`Failed to load plugin from ${pluginFilePath}: ${error}`);
return false;
}
}
/**
* Check whether a plugin ID is valid (only lowercase alphanumeric, hyphens, and underscores).
* @param {string} id The plugin ID to check
* @returns {boolean} True if the plugin ID is valid.
*/
function isValidPluginID(id) {
2023-12-23 18:03:13 +01:00
return /^[a-z0-9_-]+$/.test(id);
}
2023-12-16 21:21:40 +01:00
/**
* Initializes a plugin module.
* @param {import('express').Express} app Express app
* @param {any} plugin Plugin module
* @param {Array.<Function>} exitHooks Array of functions to be run on plugin exit. Will be pushed to if the plugin has
* an "exit" function.
2023-12-16 21:21:40 +01:00
* @returns {Promise<boolean>} Promise that resolves to true if plugin was initialized successfully
*/
async function initPlugin(app, plugin, exitHooks) {
2023-12-23 18:03:13 +01:00
const info = plugin.info || plugin.default?.info;
if (typeof info !== 'object') {
console.error('Failed to load plugin module; plugin info not found');
return false;
2023-12-16 21:21:40 +01:00
}
// We don't currently use "name" or "description" but it would be nice to have a UI for listing server plugins, so
// require them now just to be safe
for (const field of ['id', 'name', 'description']) {
2023-12-23 18:03:13 +01:00
if (typeof info[field] !== 'string') {
console.error(`Failed to load plugin module; plugin info missing field '${field}'`);
return false;
}
}
const init = plugin.init || plugin.default?.init;
if (typeof init !== 'function') {
console.error('Failed to load plugin module; no init function');
return false;
}
2023-12-23 18:03:13 +01:00
const { id } = info;
if (!isValidPluginID(id)) {
console.error(`Failed to load plugin module; invalid plugin ID '${id}'`);
2023-12-23 18:03:13 +01:00
return false;
}
2023-12-23 23:00:20 +01:00
if (loadedPlugins.has(id)) {
console.error(`Failed to load plugin module; plugin ID '${id}' is already in use`);
return false;
}
2023-12-23 18:03:13 +01:00
// Allow the plugin to register API routes under /api/plugins/[plugin ID] via a router
const router = express.Router();
await init(router);
2023-12-23 23:00:20 +01:00
loadedPlugins.set(id, plugin);
// Add API routes to the app if the plugin registered any
if (router.stack.length > 0) {
2023-12-23 18:03:13 +01:00
app.use(`/api/plugins/${id}`, router);
}
const exit = plugin.exit || plugin.default?.exit;
if (typeof exit === 'function') {
exitHooks.push(exit);
}
return true;
2023-12-16 21:21:40 +01:00
}