|
| 1 | +/** |
| 2 | + * ObjectQL |
| 3 | + * Copyright (c) 2026-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { BasePlugin } from '@objectql/types'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Error thrown when plugin operations fail |
| 13 | + */ |
| 14 | +export class PluginError extends Error { |
| 15 | + constructor( |
| 16 | + public code: 'DUPLICATE_PLUGIN' | 'MISSING_DEPENDENCY' | 'CIRCULAR_DEPENDENCY' | 'SETUP_FAILED', |
| 17 | + message: string, |
| 18 | + public pluginName?: string |
| 19 | + ) { |
| 20 | + super(message); |
| 21 | + this.name = 'PluginError'; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * PluginManager handles plugin registration, dependency resolution, and lifecycle |
| 27 | + */ |
| 28 | +export class PluginManager { |
| 29 | + private plugins: Map<string, BasePlugin> = new Map(); |
| 30 | + private setupOrder: string[] = []; |
| 31 | + private isBooted = false; |
| 32 | + |
| 33 | + /** |
| 34 | + * Register a plugin |
| 35 | + * @param plugin Plugin to register |
| 36 | + * @throws {PluginError} If plugin name is duplicated |
| 37 | + */ |
| 38 | + register(plugin: BasePlugin): void { |
| 39 | + const name = plugin.metadata.name; |
| 40 | + |
| 41 | + if (this.plugins.has(name)) { |
| 42 | + throw new PluginError( |
| 43 | + 'DUPLICATE_PLUGIN', |
| 44 | + `Plugin "${name}" is already registered`, |
| 45 | + name |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + this.plugins.set(name, plugin); |
| 50 | + this.setupOrder = []; // Invalidate cached order |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Resolve plugin dependencies using topological sort |
| 55 | + * @returns Ordered list of plugin names (dependencies first) |
| 56 | + * @throws {PluginError} If there are missing dependencies or circular dependencies |
| 57 | + */ |
| 58 | + resolveDependencies(): string[] { |
| 59 | + if (this.setupOrder.length > 0) { |
| 60 | + return this.setupOrder; |
| 61 | + } |
| 62 | + |
| 63 | + const visited = new Set<string>(); |
| 64 | + const visiting = new Set<string>(); |
| 65 | + const order: string[] = []; |
| 66 | + |
| 67 | + const visit = (pluginName: string, path: string[] = []): void => { |
| 68 | + // Check if already processed |
| 69 | + if (visited.has(pluginName)) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // Check for circular dependency |
| 74 | + if (visiting.has(pluginName)) { |
| 75 | + const cycle = [...path, pluginName].join(' -> '); |
| 76 | + throw new PluginError( |
| 77 | + 'CIRCULAR_DEPENDENCY', |
| 78 | + `Circular dependency detected: ${cycle}`, |
| 79 | + pluginName |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + const plugin = this.plugins.get(pluginName); |
| 84 | + if (!plugin) { |
| 85 | + throw new PluginError( |
| 86 | + 'MISSING_DEPENDENCY', |
| 87 | + `Plugin "${pluginName}" is required but not registered`, |
| 88 | + pluginName |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + // Mark as being visited |
| 93 | + visiting.add(pluginName); |
| 94 | + |
| 95 | + // Visit dependencies first |
| 96 | + const dependencies = plugin.metadata.dependencies || []; |
| 97 | + for (const dep of dependencies) { |
| 98 | + visit(dep, [...path, pluginName]); |
| 99 | + } |
| 100 | + |
| 101 | + // Mark as visited and add to order |
| 102 | + visiting.delete(pluginName); |
| 103 | + visited.add(pluginName); |
| 104 | + order.push(pluginName); |
| 105 | + }; |
| 106 | + |
| 107 | + // Visit all plugins |
| 108 | + for (const pluginName of this.plugins.keys()) { |
| 109 | + visit(pluginName); |
| 110 | + } |
| 111 | + |
| 112 | + this.setupOrder = order; |
| 113 | + return order; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Boot all plugins in dependency order |
| 118 | + * @param runtime Runtime instance to pass to setup hooks |
| 119 | + * @throws {PluginError} If setup fails for any plugin |
| 120 | + */ |
| 121 | + async boot(runtime: any): Promise<void> { |
| 122 | + if (this.isBooted) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + const order = this.resolveDependencies(); |
| 127 | + |
| 128 | + for (const pluginName of order) { |
| 129 | + const plugin = this.plugins.get(pluginName); |
| 130 | + if (!plugin) { |
| 131 | + continue; // Should never happen after resolveDependencies |
| 132 | + } |
| 133 | + |
| 134 | + if (plugin.setup) { |
| 135 | + try { |
| 136 | + await plugin.setup(runtime); |
| 137 | + } catch (error) { |
| 138 | + throw new PluginError( |
| 139 | + 'SETUP_FAILED', |
| 140 | + `Failed to setup plugin "${pluginName}": ${error instanceof Error ? error.message : String(error)}`, |
| 141 | + pluginName |
| 142 | + ); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + this.isBooted = true; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Shutdown all plugins in reverse dependency order |
| 152 | + */ |
| 153 | + async shutdown(): Promise<void> { |
| 154 | + if (!this.isBooted) { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + const order = [...this.setupOrder].reverse(); |
| 159 | + |
| 160 | + for (const pluginName of order) { |
| 161 | + const plugin = this.plugins.get(pluginName); |
| 162 | + if (plugin?.teardown) { |
| 163 | + try { |
| 164 | + await plugin.teardown(); |
| 165 | + } catch (error) { |
| 166 | + // Log but don't throw during shutdown |
| 167 | + console.error(`Failed to teardown plugin "${pluginName}":`, error); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + this.isBooted = false; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Get a registered plugin by name |
| 177 | + */ |
| 178 | + get(name: string): BasePlugin | undefined { |
| 179 | + return this.plugins.get(name); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Get all registered plugins |
| 184 | + */ |
| 185 | + getAll(): BasePlugin[] { |
| 186 | + return Array.from(this.plugins.values()); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Get plugins by type |
| 191 | + */ |
| 192 | + getByType(type: string): BasePlugin[] { |
| 193 | + return this.getAll().filter(p => p.metadata.type === type); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Check if booted |
| 198 | + */ |
| 199 | + isInitialized(): boolean { |
| 200 | + return this.isBooted; |
| 201 | + } |
| 202 | +} |
0 commit comments