@objectstack/runtime 0.9.2 → 1.0.1

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.
@@ -0,0 +1,65 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { Runtime } from './runtime';
3
+ import { IHttpServer, PluginContext } from '@objectstack/core';
4
+
5
+ // Mock ObjectKernel to isolate Runtime logic
6
+ vi.mock('@objectstack/core', async () => {
7
+ const actual = await vi.importActual<any>('@objectstack/core');
8
+ return {
9
+ ...actual,
10
+ ObjectKernel: class {
11
+ use = vi.fn();
12
+ registerService = vi.fn();
13
+ bootstrap = vi.fn().mockResolvedValue(undefined);
14
+ getServices = vi.fn().mockReturnValue(new Map());
15
+ }
16
+ };
17
+ });
18
+
19
+ describe('Runtime', () => {
20
+ it('should initialize successfully', () => {
21
+ const runtime = new Runtime();
22
+ expect(runtime).toBeDefined();
23
+ // Should create a kernel
24
+ expect(runtime.getKernel()).toBeDefined();
25
+ });
26
+
27
+ it('should register api registry plugin by default', () => {
28
+ const runtime = new Runtime();
29
+ const kernel = runtime.getKernel();
30
+ // Check if use was called (at least once for api registry)
31
+ expect(kernel.use).toHaveBeenCalled();
32
+ });
33
+
34
+ it('should register external http server if provided', () => {
35
+ const mockServer: IHttpServer = {
36
+ listen: vi.fn(),
37
+ close: vi.fn(),
38
+ get: vi.fn(),
39
+ post: vi.fn(),
40
+ put: vi.fn(),
41
+ delete: vi.fn(),
42
+ patch: vi.fn(),
43
+ use: vi.fn(),
44
+ };
45
+
46
+ const runtime = new Runtime({ server: mockServer });
47
+ const kernel = runtime.getKernel();
48
+
49
+ expect(kernel.registerService).toHaveBeenCalledWith('http.server', mockServer);
50
+ });
51
+
52
+ it('should delegate use() to kernel', () => {
53
+ const runtime = new Runtime();
54
+ const mockPlugin = { name: 'test', init: vi.fn() };
55
+
56
+ runtime.use(mockPlugin);
57
+ expect(runtime.getKernel().use).toHaveBeenCalledWith(mockPlugin);
58
+ });
59
+
60
+ it('should delegate start() to kernel.bootstrap()', async () => {
61
+ const runtime = new Runtime();
62
+ await runtime.start();
63
+ expect(runtime.getKernel().bootstrap).toHaveBeenCalled();
64
+ });
65
+ });
package/src/runtime.ts ADDED
@@ -0,0 +1,78 @@
1
+ import { ObjectKernel, Plugin, IHttpServer, ObjectKernelConfig } from '@objectstack/core';
2
+ import { HttpServer } from './http-server.js';
3
+ import { createApiRegistryPlugin, ApiRegistryConfig } from './api-registry-plugin.js';
4
+
5
+ export interface RuntimeConfig {
6
+ /**
7
+ * Optional existing server instance (e.g. Hono, Express app)
8
+ * If provided, Runtime will use it as the 'http.server' service.
9
+ * If not provided, Runtime expects a server plugin (like HonoServerPlugin) to be registered manually.
10
+ */
11
+ server?: IHttpServer;
12
+
13
+ /**
14
+ * API Registry Configuration
15
+ */
16
+ api?: ApiRegistryConfig;
17
+
18
+ /**
19
+ * Kernel Configuration
20
+ */
21
+ kernel?: ObjectKernelConfig;
22
+ }
23
+
24
+ /**
25
+ * ObjectStack Runtime
26
+ *
27
+ * High-level entry point for bootstrapping an ObjectStack application.
28
+ * Wraps ObjectKernel and provides standard orchestration for:
29
+ * - HTTP Server binding
30
+ * - API Registry (REST Routes)
31
+ * - Plugin Management
32
+ */
33
+ export class Runtime {
34
+ readonly kernel: ObjectKernel;
35
+
36
+ constructor(config: RuntimeConfig = {}) {
37
+ this.kernel = new ObjectKernel(config.kernel);
38
+
39
+ // If external server provided, register it immediately
40
+ if (config.server) {
41
+ // If the provided server is not already an HttpServer wrapper, wrap it?
42
+ // Since IHttpServer is the interface, we assume it complies.
43
+ // But HttpServer class in runtime is an adapter.
44
+ // If user passes raw Hono, it won't work unless they wrapped it.
45
+ // We'll assume they pass a compliant IHttpServer.
46
+ this.kernel.registerService('http.server', config.server);
47
+ }
48
+
49
+ // Register API Registry by default
50
+ // This plugin is passive (wait for services) so it's safe to add early.
51
+ this.kernel.use(createApiRegistryPlugin(config.api));
52
+ }
53
+
54
+ /**
55
+ * Register a plugin
56
+ */
57
+ use(plugin: Plugin) {
58
+ this.kernel.use(plugin);
59
+ return this;
60
+ }
61
+
62
+ /**
63
+ * Start the runtime
64
+ * 1. Initializes all plugins (init phase)
65
+ * 2. Starts all plugins (start phase)
66
+ */
67
+ async start() {
68
+ await this.kernel.bootstrap();
69
+ return this;
70
+ }
71
+
72
+ /**
73
+ * Get the kernel instance
74
+ */
75
+ getKernel() {
76
+ return this.kernel;
77
+ }
78
+ }