From a0504443e6f44e893ebec51f4ce95c66e32f445e Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 19 Apr 2023 00:01:14 -0400 Subject: [PATCH] *refactor to handle invalid plugins --- src/modules/plugin.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/modules/plugin.ts b/src/modules/plugin.ts index 8ed955c..849ca88 100644 --- a/src/modules/plugin.ts +++ b/src/modules/plugin.ts @@ -147,22 +147,33 @@ export class PluginAPIManager { } let plugin: Plugin; + let pluginPath = paths.shift(); try { - plugin = require(paths.shift() as string) as Plugin; + plugin = require(pluginPath as string) as Plugin; } catch (e) { throw e; } log.debug("Loaded plugin %s", moduleName); - return this.loadPluginInstance(plugin); + const instance = await this.loadPluginInstance(plugin); + + if (!instance) { + throw new Error(`Corrupt plugin found at ${pluginPath}`); + } + + return instance as Plugin; } - public async loadPluginInstance(plugin: Plugin): Promise { + public async loadPluginInstance(plugin: Plugin): Promise { if ("default" in plugin) { plugin = plugin?.default as Plugin; } + if (!("name" in plugin)) { + return false; + } + plugin.name = sanitizeName(plugin.name); this.registeredPlugins.set(plugin.name, plugin);