@objectstack/core 1.0.2 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +58 -0
- package/CHANGELOG.md +25 -0
- package/dist/index.cjs +4294 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1777 -0
- package/dist/index.d.ts +1776 -21
- package/dist/index.js +4246 -23
- package/dist/index.js.map +1 -0
- package/package.json +4 -4
- package/tsconfig.json +1 -3
- package/dist/api-registry-plugin.d.ts +0 -54
- package/dist/api-registry-plugin.d.ts.map +0 -1
- package/dist/api-registry-plugin.js +0 -53
- package/dist/api-registry-plugin.test.d.ts +0 -2
- package/dist/api-registry-plugin.test.d.ts.map +0 -1
- package/dist/api-registry-plugin.test.js +0 -334
- package/dist/api-registry.d.ts +0 -259
- package/dist/api-registry.d.ts.map +0 -1
- package/dist/api-registry.js +0 -600
- package/dist/api-registry.test.d.ts +0 -2
- package/dist/api-registry.test.d.ts.map +0 -1
- package/dist/api-registry.test.js +0 -957
- package/dist/contracts/data-engine.d.ts +0 -62
- package/dist/contracts/data-engine.d.ts.map +0 -1
- package/dist/contracts/data-engine.js +0 -1
- package/dist/contracts/http-server.d.ts +0 -119
- package/dist/contracts/http-server.d.ts.map +0 -1
- package/dist/contracts/http-server.js +0 -11
- package/dist/contracts/logger.d.ts +0 -63
- package/dist/contracts/logger.d.ts.map +0 -1
- package/dist/contracts/logger.js +0 -1
- package/dist/dependency-resolver.d.ts +0 -62
- package/dist/dependency-resolver.d.ts.map +0 -1
- package/dist/dependency-resolver.js +0 -317
- package/dist/dependency-resolver.test.d.ts +0 -2
- package/dist/dependency-resolver.test.d.ts.map +0 -1
- package/dist/dependency-resolver.test.js +0 -241
- package/dist/health-monitor.d.ts +0 -65
- package/dist/health-monitor.d.ts.map +0 -1
- package/dist/health-monitor.js +0 -269
- package/dist/health-monitor.test.d.ts +0 -2
- package/dist/health-monitor.test.d.ts.map +0 -1
- package/dist/health-monitor.test.js +0 -68
- package/dist/hot-reload.d.ts +0 -79
- package/dist/hot-reload.d.ts.map +0 -1
- package/dist/hot-reload.js +0 -313
- package/dist/index.d.ts.map +0 -1
- package/dist/kernel-base.d.ts +0 -84
- package/dist/kernel-base.d.ts.map +0 -1
- package/dist/kernel-base.js +0 -219
- package/dist/kernel.d.ts +0 -113
- package/dist/kernel.d.ts.map +0 -1
- package/dist/kernel.js +0 -472
- package/dist/kernel.test.d.ts +0 -2
- package/dist/kernel.test.d.ts.map +0 -1
- package/dist/kernel.test.js +0 -414
- package/dist/lite-kernel.d.ts +0 -55
- package/dist/lite-kernel.d.ts.map +0 -1
- package/dist/lite-kernel.js +0 -112
- package/dist/lite-kernel.test.d.ts +0 -2
- package/dist/lite-kernel.test.d.ts.map +0 -1
- package/dist/lite-kernel.test.js +0 -161
- package/dist/logger.d.ts +0 -71
- package/dist/logger.d.ts.map +0 -1
- package/dist/logger.js +0 -312
- package/dist/logger.test.d.ts +0 -2
- package/dist/logger.test.d.ts.map +0 -1
- package/dist/logger.test.js +0 -92
- package/dist/plugin-loader.d.ts +0 -164
- package/dist/plugin-loader.d.ts.map +0 -1
- package/dist/plugin-loader.js +0 -319
- package/dist/plugin-loader.test.d.ts +0 -2
- package/dist/plugin-loader.test.d.ts.map +0 -1
- package/dist/plugin-loader.test.js +0 -348
- package/dist/qa/adapter.d.ts +0 -14
- package/dist/qa/adapter.d.ts.map +0 -1
- package/dist/qa/adapter.js +0 -1
- package/dist/qa/http-adapter.d.ts +0 -16
- package/dist/qa/http-adapter.d.ts.map +0 -1
- package/dist/qa/http-adapter.js +0 -107
- package/dist/qa/index.d.ts +0 -4
- package/dist/qa/index.d.ts.map +0 -1
- package/dist/qa/index.js +0 -3
- package/dist/qa/runner.d.ts +0 -27
- package/dist/qa/runner.d.ts.map +0 -1
- package/dist/qa/runner.js +0 -157
- package/dist/security/index.d.ts +0 -17
- package/dist/security/index.d.ts.map +0 -1
- package/dist/security/index.js +0 -17
- package/dist/security/permission-manager.d.ts +0 -96
- package/dist/security/permission-manager.d.ts.map +0 -1
- package/dist/security/permission-manager.js +0 -235
- package/dist/security/permission-manager.test.d.ts +0 -2
- package/dist/security/permission-manager.test.d.ts.map +0 -1
- package/dist/security/permission-manager.test.js +0 -220
- package/dist/security/plugin-config-validator.d.ts +0 -79
- package/dist/security/plugin-config-validator.d.ts.map +0 -1
- package/dist/security/plugin-config-validator.js +0 -166
- package/dist/security/plugin-config-validator.test.d.ts +0 -2
- package/dist/security/plugin-config-validator.test.d.ts.map +0 -1
- package/dist/security/plugin-config-validator.test.js +0 -223
- package/dist/security/plugin-permission-enforcer.d.ts +0 -154
- package/dist/security/plugin-permission-enforcer.d.ts.map +0 -1
- package/dist/security/plugin-permission-enforcer.js +0 -323
- package/dist/security/plugin-permission-enforcer.test.d.ts +0 -2
- package/dist/security/plugin-permission-enforcer.test.d.ts.map +0 -1
- package/dist/security/plugin-permission-enforcer.test.js +0 -205
- package/dist/security/plugin-signature-verifier.d.ts +0 -96
- package/dist/security/plugin-signature-verifier.d.ts.map +0 -1
- package/dist/security/plugin-signature-verifier.js +0 -250
- package/dist/security/sandbox-runtime.d.ts +0 -115
- package/dist/security/sandbox-runtime.d.ts.map +0 -1
- package/dist/security/sandbox-runtime.js +0 -311
- package/dist/security/security-scanner.d.ts +0 -92
- package/dist/security/security-scanner.d.ts.map +0 -1
- package/dist/security/security-scanner.js +0 -273
- package/dist/types.d.ts +0 -89
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -1
- package/dist/utils/env.d.ts +0 -20
- package/dist/utils/env.d.ts.map +0 -1
- package/dist/utils/env.js +0 -46
- package/dist/utils/env.test.d.ts +0 -2
- package/dist/utils/env.test.d.ts.map +0 -1
- package/dist/utils/env.test.js +0 -52
package/dist/kernel.js
DELETED
|
@@ -1,472 +0,0 @@
|
|
|
1
|
-
import { createLogger } from './logger.js';
|
|
2
|
-
import { ServiceRequirementDef } from '@objectstack/spec/system';
|
|
3
|
-
import { PluginLoader, ServiceLifecycle } from './plugin-loader.js';
|
|
4
|
-
import { isNode, safeExit } from './utils/env.js';
|
|
5
|
-
/**
|
|
6
|
-
* Enhanced ObjectKernel with Advanced Plugin Management
|
|
7
|
-
*
|
|
8
|
-
* Extends the basic ObjectKernel with:
|
|
9
|
-
* - Async plugin loading with validation
|
|
10
|
-
* - Version compatibility checking
|
|
11
|
-
* - Plugin signature verification
|
|
12
|
-
* - Configuration validation (Zod)
|
|
13
|
-
* - Factory-based dependency injection
|
|
14
|
-
* - Service lifecycle management (singleton/transient/scoped)
|
|
15
|
-
* - Circular dependency detection
|
|
16
|
-
* - Lazy loading services
|
|
17
|
-
* - Graceful shutdown
|
|
18
|
-
* - Plugin startup timeout control
|
|
19
|
-
* - Startup failure rollback
|
|
20
|
-
* - Plugin health checks
|
|
21
|
-
*/
|
|
22
|
-
export class ObjectKernel {
|
|
23
|
-
constructor(config = {}) {
|
|
24
|
-
this.plugins = new Map();
|
|
25
|
-
this.services = new Map();
|
|
26
|
-
this.hooks = new Map();
|
|
27
|
-
this.state = 'idle';
|
|
28
|
-
this.startedPlugins = new Set();
|
|
29
|
-
this.pluginStartTimes = new Map();
|
|
30
|
-
this.shutdownHandlers = [];
|
|
31
|
-
this.config = {
|
|
32
|
-
defaultStartupTimeout: 30000, // 30 seconds
|
|
33
|
-
gracefulShutdown: true,
|
|
34
|
-
shutdownTimeout: 60000, // 60 seconds
|
|
35
|
-
rollbackOnFailure: true,
|
|
36
|
-
...config,
|
|
37
|
-
};
|
|
38
|
-
this.logger = createLogger(config.logger);
|
|
39
|
-
this.pluginLoader = new PluginLoader(this.logger);
|
|
40
|
-
// Initialize context
|
|
41
|
-
this.context = {
|
|
42
|
-
registerService: (name, service) => {
|
|
43
|
-
this.registerService(name, service);
|
|
44
|
-
},
|
|
45
|
-
getService: (name) => {
|
|
46
|
-
// 1. Try direct service map first (synchronous cache)
|
|
47
|
-
const service = this.services.get(name);
|
|
48
|
-
if (service) {
|
|
49
|
-
return service;
|
|
50
|
-
}
|
|
51
|
-
// 2. Try to get from plugin loader cache (Sync access to factories)
|
|
52
|
-
const loaderService = this.pluginLoader.getServiceInstance(name);
|
|
53
|
-
if (loaderService) {
|
|
54
|
-
// Cache it locally for faster next access
|
|
55
|
-
this.services.set(name, loaderService);
|
|
56
|
-
return loaderService;
|
|
57
|
-
}
|
|
58
|
-
// 3. Try to get from plugin loader (support async factories)
|
|
59
|
-
try {
|
|
60
|
-
const service = this.pluginLoader.getService(name);
|
|
61
|
-
if (service instanceof Promise) {
|
|
62
|
-
// If we found it in the loader but not in the sync map, it's likely a factory-based service or still loading
|
|
63
|
-
throw new Error(`Service '${name}' is async - use await`);
|
|
64
|
-
}
|
|
65
|
-
return service;
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
if (error.message?.includes('is async')) {
|
|
69
|
-
throw error;
|
|
70
|
-
}
|
|
71
|
-
// Re-throw critical factory errors instead of masking them as "not found"
|
|
72
|
-
// If the error came from the factory execution (e.g. database connection failed), we must see it.
|
|
73
|
-
// "Service '${name}' not found" comes from PluginLoader.getService fallback.
|
|
74
|
-
const isNotFoundError = error.message === `Service '${name}' not found`;
|
|
75
|
-
if (!isNotFoundError) {
|
|
76
|
-
throw error;
|
|
77
|
-
}
|
|
78
|
-
throw new Error(`[Kernel] Service '${name}' not found`);
|
|
79
|
-
}
|
|
80
|
-
},
|
|
81
|
-
hook: (name, handler) => {
|
|
82
|
-
if (!this.hooks.has(name)) {
|
|
83
|
-
this.hooks.set(name, []);
|
|
84
|
-
}
|
|
85
|
-
this.hooks.get(name).push(handler);
|
|
86
|
-
},
|
|
87
|
-
trigger: async (name, ...args) => {
|
|
88
|
-
const handlers = this.hooks.get(name) || [];
|
|
89
|
-
for (const handler of handlers) {
|
|
90
|
-
await handler(...args);
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
getServices: () => {
|
|
94
|
-
return new Map(this.services);
|
|
95
|
-
},
|
|
96
|
-
logger: this.logger,
|
|
97
|
-
getKernel: () => this, // Type compatibility
|
|
98
|
-
};
|
|
99
|
-
this.pluginLoader.setContext(this.context);
|
|
100
|
-
// Register shutdown handler
|
|
101
|
-
if (this.config.gracefulShutdown) {
|
|
102
|
-
this.registerShutdownSignals();
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Register a plugin with enhanced validation
|
|
107
|
-
*/
|
|
108
|
-
async use(plugin) {
|
|
109
|
-
if (this.state !== 'idle') {
|
|
110
|
-
throw new Error('[Kernel] Cannot register plugins after bootstrap has started');
|
|
111
|
-
}
|
|
112
|
-
// Load plugin through enhanced loader
|
|
113
|
-
const result = await this.pluginLoader.loadPlugin(plugin);
|
|
114
|
-
if (!result.success || !result.plugin) {
|
|
115
|
-
throw new Error(`Failed to load plugin: ${plugin.name} - ${result.error?.message}`);
|
|
116
|
-
}
|
|
117
|
-
const pluginMeta = result.plugin;
|
|
118
|
-
this.plugins.set(pluginMeta.name, pluginMeta);
|
|
119
|
-
this.logger.info(`Plugin registered: ${pluginMeta.name}@${pluginMeta.version}`, {
|
|
120
|
-
plugin: pluginMeta.name,
|
|
121
|
-
version: pluginMeta.version,
|
|
122
|
-
});
|
|
123
|
-
return this;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Register a service instance directly
|
|
127
|
-
*/
|
|
128
|
-
registerService(name, service) {
|
|
129
|
-
if (this.services.has(name)) {
|
|
130
|
-
throw new Error(`[Kernel] Service '${name}' already registered`);
|
|
131
|
-
}
|
|
132
|
-
this.services.set(name, service);
|
|
133
|
-
this.pluginLoader.registerService(name, service);
|
|
134
|
-
this.logger.info(`Service '${name}' registered`, { service: name });
|
|
135
|
-
return this;
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Register a service factory with lifecycle management
|
|
139
|
-
*/
|
|
140
|
-
registerServiceFactory(name, factory, lifecycle = ServiceLifecycle.SINGLETON, dependencies) {
|
|
141
|
-
this.pluginLoader.registerServiceFactory({
|
|
142
|
-
name,
|
|
143
|
-
factory,
|
|
144
|
-
lifecycle,
|
|
145
|
-
dependencies,
|
|
146
|
-
});
|
|
147
|
-
return this;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Validate Critical System Requirements
|
|
151
|
-
*/
|
|
152
|
-
validateSystemRequirements() {
|
|
153
|
-
if (this.config.skipSystemValidation) {
|
|
154
|
-
this.logger.debug('System requirement validation skipped');
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
this.logger.debug('Validating system service requirements...');
|
|
158
|
-
const missingServices = [];
|
|
159
|
-
const missingCoreServices = [];
|
|
160
|
-
// Iterate through all defined requirements
|
|
161
|
-
for (const [serviceName, criticality] of Object.entries(ServiceRequirementDef)) {
|
|
162
|
-
const hasService = this.services.has(serviceName) || this.pluginLoader.hasService(serviceName);
|
|
163
|
-
if (!hasService) {
|
|
164
|
-
if (criticality === 'required') {
|
|
165
|
-
this.logger.error(`CRITICAL: Required service missing: ${serviceName}`);
|
|
166
|
-
missingServices.push(serviceName);
|
|
167
|
-
}
|
|
168
|
-
else if (criticality === 'core') {
|
|
169
|
-
this.logger.warn(`CORE: Core service missing, functionality may be degraded: ${serviceName}`);
|
|
170
|
-
missingCoreServices.push(serviceName);
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
this.logger.info(`Info: Optional service not present: ${serviceName}`);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
if (missingServices.length > 0) {
|
|
178
|
-
const errorMsg = `System failed to start. Missing critical services: ${missingServices.join(', ')}`;
|
|
179
|
-
this.logger.error(errorMsg);
|
|
180
|
-
throw new Error(errorMsg);
|
|
181
|
-
}
|
|
182
|
-
if (missingCoreServices.length > 0) {
|
|
183
|
-
this.logger.warn(`System started with degraded capabilities. Missing core services: ${missingCoreServices.join(', ')}`);
|
|
184
|
-
}
|
|
185
|
-
this.logger.info('System requirement check passed');
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Bootstrap the kernel with enhanced features
|
|
189
|
-
*/
|
|
190
|
-
async bootstrap() {
|
|
191
|
-
if (this.state !== 'idle') {
|
|
192
|
-
throw new Error('[Kernel] Kernel already bootstrapped');
|
|
193
|
-
}
|
|
194
|
-
this.state = 'initializing';
|
|
195
|
-
this.logger.info('Bootstrap started');
|
|
196
|
-
try {
|
|
197
|
-
// Check for circular dependencies
|
|
198
|
-
const cycles = this.pluginLoader.detectCircularDependencies();
|
|
199
|
-
if (cycles.length > 0) {
|
|
200
|
-
this.logger.warn('Circular service dependencies detected:', { cycles });
|
|
201
|
-
}
|
|
202
|
-
// Resolve plugin dependencies
|
|
203
|
-
const orderedPlugins = this.resolveDependencies();
|
|
204
|
-
// Phase 1: Init - Plugins register services
|
|
205
|
-
this.logger.info('Phase 1: Init plugins');
|
|
206
|
-
for (const plugin of orderedPlugins) {
|
|
207
|
-
await this.initPluginWithTimeout(plugin);
|
|
208
|
-
}
|
|
209
|
-
// Phase 2: Start - Plugins execute business logic
|
|
210
|
-
this.logger.info('Phase 2: Start plugins');
|
|
211
|
-
this.state = 'running';
|
|
212
|
-
for (const plugin of orderedPlugins) {
|
|
213
|
-
const result = await this.startPluginWithTimeout(plugin);
|
|
214
|
-
if (!result.success) {
|
|
215
|
-
this.logger.error(`Plugin startup failed: ${plugin.name}`, result.error);
|
|
216
|
-
if (this.config.rollbackOnFailure) {
|
|
217
|
-
this.logger.warn('Rolling back started plugins...');
|
|
218
|
-
await this.rollbackStartedPlugins();
|
|
219
|
-
throw new Error(`Plugin ${plugin.name} failed to start - rollback complete`);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
// Phase 3: Trigger kernel:ready hook
|
|
224
|
-
this.validateSystemRequirements(); // Final check before ready
|
|
225
|
-
this.logger.debug('Triggering kernel:ready hook');
|
|
226
|
-
await this.context.trigger('kernel:ready');
|
|
227
|
-
this.logger.info('✅ Bootstrap complete');
|
|
228
|
-
}
|
|
229
|
-
catch (error) {
|
|
230
|
-
this.state = 'stopped';
|
|
231
|
-
throw error;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Graceful shutdown with timeout
|
|
236
|
-
*/
|
|
237
|
-
async shutdown() {
|
|
238
|
-
if (this.state === 'stopped' || this.state === 'stopping') {
|
|
239
|
-
this.logger.warn('Kernel already stopped or stopping');
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
if (this.state !== 'running') {
|
|
243
|
-
throw new Error('[Kernel] Kernel not running');
|
|
244
|
-
}
|
|
245
|
-
this.state = 'stopping';
|
|
246
|
-
this.logger.info('Graceful shutdown started');
|
|
247
|
-
try {
|
|
248
|
-
// Create shutdown promise with timeout
|
|
249
|
-
const shutdownPromise = this.performShutdown();
|
|
250
|
-
const timeoutPromise = new Promise((_, reject) => {
|
|
251
|
-
setTimeout(() => {
|
|
252
|
-
reject(new Error('Shutdown timeout exceeded'));
|
|
253
|
-
}, this.config.shutdownTimeout);
|
|
254
|
-
});
|
|
255
|
-
// Race between shutdown and timeout
|
|
256
|
-
await Promise.race([shutdownPromise, timeoutPromise]);
|
|
257
|
-
this.state = 'stopped';
|
|
258
|
-
this.logger.info('✅ Graceful shutdown complete');
|
|
259
|
-
}
|
|
260
|
-
catch (error) {
|
|
261
|
-
this.logger.error('Shutdown error - forcing stop', error);
|
|
262
|
-
this.state = 'stopped';
|
|
263
|
-
throw error;
|
|
264
|
-
}
|
|
265
|
-
finally {
|
|
266
|
-
// Cleanup logger resources
|
|
267
|
-
await this.logger.destroy();
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Check health of a specific plugin
|
|
272
|
-
*/
|
|
273
|
-
async checkPluginHealth(pluginName) {
|
|
274
|
-
return await this.pluginLoader.checkPluginHealth(pluginName);
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Check health of all plugins
|
|
278
|
-
*/
|
|
279
|
-
async checkAllPluginsHealth() {
|
|
280
|
-
const results = new Map();
|
|
281
|
-
for (const pluginName of this.plugins.keys()) {
|
|
282
|
-
const health = await this.checkPluginHealth(pluginName);
|
|
283
|
-
results.set(pluginName, health);
|
|
284
|
-
}
|
|
285
|
-
return results;
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Get plugin startup metrics
|
|
289
|
-
*/
|
|
290
|
-
getPluginMetrics() {
|
|
291
|
-
return new Map(this.pluginStartTimes);
|
|
292
|
-
}
|
|
293
|
-
/**
|
|
294
|
-
* Get a service (sync helper)
|
|
295
|
-
*/
|
|
296
|
-
getService(name) {
|
|
297
|
-
return this.context.getService(name);
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Get a service asynchronously (supports factories)
|
|
301
|
-
*/
|
|
302
|
-
async getServiceAsync(name, scopeId) {
|
|
303
|
-
return await this.pluginLoader.getService(name, scopeId);
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Check if kernel is running
|
|
307
|
-
*/
|
|
308
|
-
isRunning() {
|
|
309
|
-
return this.state === 'running';
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* Get kernel state
|
|
313
|
-
*/
|
|
314
|
-
getState() {
|
|
315
|
-
return this.state;
|
|
316
|
-
}
|
|
317
|
-
// Private methods
|
|
318
|
-
async initPluginWithTimeout(plugin) {
|
|
319
|
-
const timeout = plugin.startupTimeout || this.config.defaultStartupTimeout;
|
|
320
|
-
this.logger.debug(`Init: ${plugin.name}`, { plugin: plugin.name });
|
|
321
|
-
const initPromise = plugin.init(this.context);
|
|
322
|
-
const timeoutPromise = new Promise((_, reject) => {
|
|
323
|
-
setTimeout(() => {
|
|
324
|
-
reject(new Error(`Plugin ${plugin.name} init timeout after ${timeout}ms`));
|
|
325
|
-
}, timeout);
|
|
326
|
-
});
|
|
327
|
-
await Promise.race([initPromise, timeoutPromise]);
|
|
328
|
-
}
|
|
329
|
-
async startPluginWithTimeout(plugin) {
|
|
330
|
-
if (!plugin.start) {
|
|
331
|
-
return { success: true, pluginName: plugin.name };
|
|
332
|
-
}
|
|
333
|
-
const timeout = plugin.startupTimeout || this.config.defaultStartupTimeout;
|
|
334
|
-
const startTime = Date.now();
|
|
335
|
-
this.logger.debug(`Start: ${plugin.name}`, { plugin: plugin.name });
|
|
336
|
-
try {
|
|
337
|
-
const startPromise = plugin.start(this.context);
|
|
338
|
-
const timeoutPromise = new Promise((_, reject) => {
|
|
339
|
-
setTimeout(() => {
|
|
340
|
-
reject(new Error(`Plugin ${plugin.name} start timeout after ${timeout}ms`));
|
|
341
|
-
}, timeout);
|
|
342
|
-
});
|
|
343
|
-
await Promise.race([startPromise, timeoutPromise]);
|
|
344
|
-
const duration = Date.now() - startTime;
|
|
345
|
-
this.startedPlugins.add(plugin.name);
|
|
346
|
-
this.pluginStartTimes.set(plugin.name, duration);
|
|
347
|
-
this.logger.debug(`Plugin started: ${plugin.name} (${duration}ms)`);
|
|
348
|
-
return {
|
|
349
|
-
success: true,
|
|
350
|
-
pluginName: plugin.name,
|
|
351
|
-
startTime: duration,
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
catch (error) {
|
|
355
|
-
const duration = Date.now() - startTime;
|
|
356
|
-
const isTimeout = error.message.includes('timeout');
|
|
357
|
-
return {
|
|
358
|
-
success: false,
|
|
359
|
-
pluginName: plugin.name,
|
|
360
|
-
error: error,
|
|
361
|
-
startTime: duration,
|
|
362
|
-
timedOut: isTimeout,
|
|
363
|
-
};
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
async rollbackStartedPlugins() {
|
|
367
|
-
const pluginsToRollback = Array.from(this.startedPlugins).reverse();
|
|
368
|
-
for (const pluginName of pluginsToRollback) {
|
|
369
|
-
const plugin = this.plugins.get(pluginName);
|
|
370
|
-
if (plugin?.destroy) {
|
|
371
|
-
try {
|
|
372
|
-
this.logger.debug(`Rollback: ${pluginName}`);
|
|
373
|
-
await plugin.destroy();
|
|
374
|
-
}
|
|
375
|
-
catch (error) {
|
|
376
|
-
this.logger.error(`Rollback failed for ${pluginName}`, error);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
this.startedPlugins.clear();
|
|
381
|
-
}
|
|
382
|
-
async performShutdown() {
|
|
383
|
-
// Trigger shutdown hook
|
|
384
|
-
await this.context.trigger('kernel:shutdown');
|
|
385
|
-
// Destroy plugins in reverse order
|
|
386
|
-
const orderedPlugins = Array.from(this.plugins.values()).reverse();
|
|
387
|
-
for (const plugin of orderedPlugins) {
|
|
388
|
-
if (plugin.destroy) {
|
|
389
|
-
this.logger.debug(`Destroy: ${plugin.name}`, { plugin: plugin.name });
|
|
390
|
-
try {
|
|
391
|
-
await plugin.destroy();
|
|
392
|
-
}
|
|
393
|
-
catch (error) {
|
|
394
|
-
this.logger.error(`Error destroying plugin ${plugin.name}`, error);
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
// Execute custom shutdown handlers
|
|
399
|
-
for (const handler of this.shutdownHandlers) {
|
|
400
|
-
try {
|
|
401
|
-
await handler();
|
|
402
|
-
}
|
|
403
|
-
catch (error) {
|
|
404
|
-
this.logger.error('Shutdown handler error', error);
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
resolveDependencies() {
|
|
409
|
-
const resolved = [];
|
|
410
|
-
const visited = new Set();
|
|
411
|
-
const visiting = new Set();
|
|
412
|
-
const visit = (pluginName) => {
|
|
413
|
-
if (visited.has(pluginName))
|
|
414
|
-
return;
|
|
415
|
-
if (visiting.has(pluginName)) {
|
|
416
|
-
throw new Error(`[Kernel] Circular dependency detected: ${pluginName}`);
|
|
417
|
-
}
|
|
418
|
-
const plugin = this.plugins.get(pluginName);
|
|
419
|
-
if (!plugin) {
|
|
420
|
-
throw new Error(`[Kernel] Plugin '${pluginName}' not found`);
|
|
421
|
-
}
|
|
422
|
-
visiting.add(pluginName);
|
|
423
|
-
// Visit dependencies first
|
|
424
|
-
const deps = plugin.dependencies || [];
|
|
425
|
-
for (const dep of deps) {
|
|
426
|
-
if (!this.plugins.has(dep)) {
|
|
427
|
-
throw new Error(`[Kernel] Dependency '${dep}' not found for plugin '${pluginName}'`);
|
|
428
|
-
}
|
|
429
|
-
visit(dep);
|
|
430
|
-
}
|
|
431
|
-
visiting.delete(pluginName);
|
|
432
|
-
visited.add(pluginName);
|
|
433
|
-
resolved.push(plugin);
|
|
434
|
-
};
|
|
435
|
-
// Visit all plugins
|
|
436
|
-
for (const pluginName of this.plugins.keys()) {
|
|
437
|
-
visit(pluginName);
|
|
438
|
-
}
|
|
439
|
-
return resolved;
|
|
440
|
-
}
|
|
441
|
-
registerShutdownSignals() {
|
|
442
|
-
const signals = ['SIGINT', 'SIGTERM', 'SIGQUIT'];
|
|
443
|
-
let shutdownInProgress = false;
|
|
444
|
-
const handleShutdown = async (signal) => {
|
|
445
|
-
if (shutdownInProgress) {
|
|
446
|
-
this.logger.warn(`Shutdown already in progress, ignoring ${signal}`);
|
|
447
|
-
return;
|
|
448
|
-
}
|
|
449
|
-
shutdownInProgress = true;
|
|
450
|
-
this.logger.info(`Received ${signal} - initiating graceful shutdown`);
|
|
451
|
-
try {
|
|
452
|
-
await this.shutdown();
|
|
453
|
-
safeExit(0);
|
|
454
|
-
}
|
|
455
|
-
catch (error) {
|
|
456
|
-
this.logger.error('Shutdown failed', error);
|
|
457
|
-
safeExit(1);
|
|
458
|
-
}
|
|
459
|
-
};
|
|
460
|
-
if (isNode) {
|
|
461
|
-
for (const signal of signals) {
|
|
462
|
-
process.on(signal, () => handleShutdown(signal));
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
/**
|
|
467
|
-
* Register a custom shutdown handler
|
|
468
|
-
*/
|
|
469
|
-
onShutdown(handler) {
|
|
470
|
-
this.shutdownHandlers.push(handler);
|
|
471
|
-
}
|
|
472
|
-
}
|
package/dist/kernel.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"kernel.test.d.ts","sourceRoot":"","sources":["../src/kernel.test.ts"],"names":[],"mappings":""}
|