Add check for server plugin id clash

This commit is contained in:
Cohee 2023-12-24 00:00:20 +02:00
parent 73548faa33
commit 88993bd3e8
1 changed files with 13 additions and 0 deletions

View File

@ -5,6 +5,12 @@ const express = require('express');
const { getConfigValue } = require('./util');
const enableServerPlugins = getConfigValue('enableServerPlugins', false);
/**
* Map of loaded plugins.
* @type {Map<string, any>}
*/
const loadedPlugins = new Map();
/**
* Determine if a file is a CommonJS module.
* @param {string} file Path to file
@ -186,11 +192,18 @@ async function initPlugin(app, plugin, exitHooks) {
return false;
}
if (loadedPlugins.has(id)) {
console.error(`Failed to load plugin module; plugin ID '${id}' is already in use`);
return false;
}
// Allow the plugin to register API routes under /api/plugins/[plugin ID] via a router
const router = express.Router();
await plugin.init(router);
loadedPlugins.set(id, plugin);
// Add API routes to the app if the plugin registered any
if (router.stack.length > 0) {
app.use(`/api/plugins/${id}`, router);