@objectstack/core 2.0.5 → 2.0.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@objectstack/core",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Microkernel Core for ObjectStack",
6
6
  "type": "module",
@@ -22,7 +22,7 @@
22
22
  "pino": "^10.3.0",
23
23
  "pino-pretty": "^13.1.3",
24
24
  "zod": "^4.3.6",
25
- "@objectstack/spec": "2.0.5"
25
+ "@objectstack/spec": "2.0.6"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "pino": "^8.0.0"
@@ -85,6 +85,21 @@ export abstract class ObjectKernelBase {
85
85
  return this.services.get<T>(name);
86
86
  }
87
87
  },
88
+ replaceService: <T>(name: string, implementation: T): void => {
89
+ if (this.services instanceof Map) {
90
+ if (!this.services.has(name)) {
91
+ throw new Error(`[Kernel] Service '${name}' not found. Use registerService() to add new services.`);
92
+ }
93
+ this.services.set(name, implementation);
94
+ } else {
95
+ // IServiceRegistry implementation
96
+ if (!this.services.has(name)) {
97
+ throw new Error(`[Kernel] Service '${name}' not found. Use registerService() to add new services.`);
98
+ }
99
+ this.services.register(name, implementation);
100
+ }
101
+ this.logger.info(`Service '${name}' replaced`, { service: name });
102
+ },
88
103
  hook: (name, handler) => {
89
104
  if (!this.hooks.has(name)) {
90
105
  this.hooks.set(name, []);
@@ -534,4 +534,91 @@ describe('ObjectKernel', () => {
534
534
  }).rejects.toThrow('not running');
535
535
  });
536
536
  });
537
+
538
+ describe('Service Replacement', () => {
539
+ it('should replace an existing service via replaceService', async () => {
540
+ const originalService = { value: 'original' };
541
+ const replacementService = { value: 'replaced' };
542
+
543
+ const plugin: Plugin = {
544
+ name: 'register-plugin',
545
+ version: '1.0.0',
546
+ init: async (ctx) => {
547
+ ctx.registerService('metadata', originalService);
548
+ },
549
+ };
550
+
551
+ const optimizationPlugin: Plugin = {
552
+ name: 'optimization-plugin',
553
+ version: '1.0.0',
554
+ dependencies: ['register-plugin'],
555
+ init: async (ctx) => {
556
+ const existing = ctx.getService('metadata');
557
+ expect(existing).toBe(originalService);
558
+ ctx.replaceService('metadata', replacementService);
559
+ },
560
+ };
561
+
562
+ await kernel.use(plugin);
563
+ await kernel.use(optimizationPlugin);
564
+ await kernel.bootstrap();
565
+
566
+ const result = kernel.getService('metadata');
567
+ expect(result).toBe(replacementService);
568
+
569
+ await kernel.shutdown();
570
+ });
571
+
572
+ it('should throw when replacing a non-existent service', async () => {
573
+ const plugin: Plugin = {
574
+ name: 'bad-replace-plugin',
575
+ version: '1.0.0',
576
+ init: async (ctx) => {
577
+ expect(() => {
578
+ ctx.replaceService('nonexistent', { value: 'test' });
579
+ }).toThrow("Service 'nonexistent' not found");
580
+ },
581
+ };
582
+
583
+ await kernel.use(plugin);
584
+ await kernel.bootstrap();
585
+ await kernel.shutdown();
586
+ });
587
+
588
+ it('should allow decorator pattern via replaceService', async () => {
589
+ const original = {
590
+ getData: () => 'raw-data',
591
+ };
592
+
593
+ const plugin: Plugin = {
594
+ name: 'data-plugin',
595
+ version: '1.0.0',
596
+ init: async (ctx) => {
597
+ ctx.registerService('data', original);
598
+ },
599
+ };
600
+
601
+ const wrapperPlugin: Plugin = {
602
+ name: 'wrapper-plugin',
603
+ version: '1.0.0',
604
+ dependencies: ['data-plugin'],
605
+ init: async (ctx) => {
606
+ const existing = ctx.getService<typeof original>('data');
607
+ const decorated = {
608
+ getData: () => `cached(${existing.getData()})`,
609
+ };
610
+ ctx.replaceService('data', decorated);
611
+ },
612
+ };
613
+
614
+ await kernel.use(plugin);
615
+ await kernel.use(wrapperPlugin);
616
+ await kernel.bootstrap();
617
+
618
+ const result = kernel.getService<typeof original>('data');
619
+ expect(result.getData()).toBe('cached(raw-data)');
620
+
621
+ await kernel.shutdown();
622
+ });
623
+ });
537
624
  });
package/src/kernel.ts CHANGED
@@ -119,6 +119,15 @@ export class ObjectKernel {
119
119
  throw new Error(`[Kernel] Service '${name}' not found`);
120
120
  }
121
121
  },
122
+ replaceService: <T>(name: string, implementation: T): void => {
123
+ const hasService = this.services.has(name) || this.pluginLoader.hasService(name);
124
+ if (!hasService) {
125
+ throw new Error(`[Kernel] Service '${name}' not found. Use registerService() to add new services.`);
126
+ }
127
+ this.services.set(name, implementation);
128
+ this.pluginLoader.replaceService(name, implementation);
129
+ this.logger.info(`Service '${name}' replaced`, { service: name });
130
+ },
122
131
  hook: (name, handler) => {
123
132
  if (!this.hooks.has(name)) {
124
133
  this.hooks.set(name, []);
@@ -197,4 +197,52 @@ describe('LiteKernel with Configurable Logger', () => {
197
197
  await browserKernel.shutdown();
198
198
  });
199
199
  });
200
+
201
+ describe('Service Replacement', () => {
202
+ it('should replace an existing service via replaceService', async () => {
203
+ const originalService = { value: 'original' };
204
+ const replacementService = { value: 'replaced' };
205
+
206
+ const plugin: Plugin = {
207
+ name: 'register-plugin',
208
+ init: async (ctx) => {
209
+ ctx.registerService('metadata', originalService);
210
+ },
211
+ };
212
+
213
+ const optimizationPlugin: Plugin = {
214
+ name: 'optimization-plugin',
215
+ dependencies: ['register-plugin'],
216
+ init: async (ctx) => {
217
+ const existing = ctx.getService('metadata');
218
+ expect(existing).toBe(originalService);
219
+ ctx.replaceService('metadata', replacementService);
220
+ },
221
+ };
222
+
223
+ kernel.use(plugin);
224
+ kernel.use(optimizationPlugin);
225
+ await kernel.bootstrap();
226
+
227
+ const result = kernel.getService('metadata');
228
+ expect(result).toBe(replacementService);
229
+
230
+ await kernel.shutdown();
231
+ });
232
+
233
+ it('should throw when replacing a non-existent service', async () => {
234
+ const plugin: Plugin = {
235
+ name: 'bad-replace-plugin',
236
+ init: async (ctx) => {
237
+ expect(() => {
238
+ ctx.replaceService('nonexistent', { value: 'test' });
239
+ }).toThrow("Service 'nonexistent' not found");
240
+ },
241
+ };
242
+
243
+ kernel.use(plugin);
244
+ await kernel.bootstrap();
245
+ await kernel.shutdown();
246
+ });
247
+ });
200
248
  });
@@ -248,6 +248,18 @@ export class PluginLoader {
248
248
  this.serviceInstances.set(name, service);
249
249
  }
250
250
 
251
+ /**
252
+ * Replace an existing service instance.
253
+ * Used by optimization plugins to swap kernel internals.
254
+ * @throws Error if service does not exist
255
+ */
256
+ replaceService(name: string, service: any): void {
257
+ if (!this.hasService(name)) {
258
+ throw new Error(`Service '${name}' not found`);
259
+ }
260
+ this.serviceInstances.set(name, service);
261
+ }
262
+
251
263
  /**
252
264
  * Check if a service is registered (either as instance or factory)
253
265
  */
@@ -394,6 +394,12 @@ export class SecurePluginContext implements PluginContext {
394
394
  return this.baseContext.getService<T>(name);
395
395
  }
396
396
 
397
+ replaceService<T>(name: string, implementation: T): void {
398
+ // Check permission before replacing service
399
+ this.permissionEnforcer.enforceServiceAccess(this.pluginName, name);
400
+ this.baseContext.replaceService(name, implementation);
401
+ }
402
+
397
403
  getServices(): Map<string, any> {
398
404
  // Return all services (no permission check for listing)
399
405
  return this.baseContext.getServices();
package/src/types.ts CHANGED
@@ -28,6 +28,17 @@ export interface PluginContext {
28
28
  */
29
29
  getService<T>(name: string): T;
30
30
 
31
+ /**
32
+ * Replace an existing service with a new implementation.
33
+ * Useful for optimization plugins that wrap or swap kernel internals
34
+ * (e.g., metadata registry, connection pooling).
35
+ *
36
+ * @param name - Service name to replace
37
+ * @param implementation - New service implementation
38
+ * @throws Error if the service does not exist
39
+ */
40
+ replaceService<T>(name: string, implementation: T): void;
41
+
31
42
  /**
32
43
  * Get all registered services
33
44
  */