|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Server-side Kernel Singleton for Vercel Serverless Functions |
| 5 | + * |
| 6 | + * Boots an ObjectKernel with ObjectQL + InMemoryDriver, seeds data from |
| 7 | + * the Studio configuration, and exposes a Hono app via createHonoApp. |
| 8 | + * |
| 9 | + * The kernel instance survives across warm invocations of the same |
| 10 | + * serverless function container, so boot cost is paid only on cold start. |
| 11 | + * |
| 12 | + * @module |
| 13 | + */ |
| 14 | + |
| 15 | +import { ObjectKernel, DriverPlugin, AppPlugin } from '@objectstack/runtime'; |
| 16 | +import { ObjectQLPlugin } from '@objectstack/objectql'; |
| 17 | +import { InMemoryDriver } from '@objectstack/driver-memory'; |
| 18 | +import { createHonoApp } from '@objectstack/hono'; |
| 19 | +import { Hono } from 'hono'; |
| 20 | +import { createBrokerShim } from '../src/lib/create-broker-shim'; |
| 21 | +import studioConfig from '../objectstack.config'; |
| 22 | + |
| 23 | +// --- Singleton state (persists across warm invocations) --- |
| 24 | +let _kernel: ObjectKernel | null = null; |
| 25 | +let _app: Hono | null = null; |
| 26 | + |
| 27 | +/** |
| 28 | + * Boot the ObjectStack kernel (one-time cold-start cost). |
| 29 | + */ |
| 30 | +async function bootKernel(): Promise<ObjectKernel> { |
| 31 | + if (_kernel) return _kernel; |
| 32 | + |
| 33 | + console.log('[Vercel] Booting ObjectStack Kernel (server mode)...'); |
| 34 | + |
| 35 | + const kernel = new ObjectKernel(); |
| 36 | + |
| 37 | + await kernel.use(new ObjectQLPlugin()); |
| 38 | + await kernel.use(new DriverPlugin(new InMemoryDriver(), 'memory')); |
| 39 | + await kernel.use(new AppPlugin(studioConfig)); |
| 40 | + |
| 41 | + // Broker shim — bridges HttpDispatcher → ObjectQL engine |
| 42 | + (kernel as any).broker = createBrokerShim(kernel); |
| 43 | + |
| 44 | + await kernel.bootstrap(); |
| 45 | + |
| 46 | + // Seed data from config |
| 47 | + await seedData(kernel, [studioConfig]); |
| 48 | + |
| 49 | + _kernel = kernel; |
| 50 | + console.log('[Vercel] Kernel ready.'); |
| 51 | + return kernel; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Seed records defined in app configs into the ObjectQL engine. |
| 56 | + */ |
| 57 | +async function seedData(kernel: ObjectKernel, configs: any[]) { |
| 58 | + const ql = (kernel as any).context?.getService('objectql'); |
| 59 | + if (!ql) return; |
| 60 | + |
| 61 | + // Reserved namespaces ('base', 'system') bypass FQN transformation — |
| 62 | + // objects in these namespaces keep their short name as-is. |
| 63 | + const RESERVED_NS = new Set(['base', 'system']); |
| 64 | + const toFQN = (name: string, namespace?: string) => { |
| 65 | + if (name.includes('__') || !namespace || RESERVED_NS.has(namespace)) return name; |
| 66 | + return `${namespace}__${name}`; |
| 67 | + }; |
| 68 | + |
| 69 | + for (const appConfig of configs) { |
| 70 | + const namespace = (appConfig.manifest || appConfig)?.namespace as string | undefined; |
| 71 | + |
| 72 | + const seedDatasets: any[] = []; |
| 73 | + if (Array.isArray(appConfig.data)) { |
| 74 | + seedDatasets.push(...appConfig.data); |
| 75 | + } |
| 76 | + if (appConfig.manifest && Array.isArray(appConfig.manifest.data)) { |
| 77 | + seedDatasets.push(...appConfig.manifest.data); |
| 78 | + } |
| 79 | + |
| 80 | + for (const dataset of seedDatasets) { |
| 81 | + if (!dataset.records || !dataset.object) continue; |
| 82 | + |
| 83 | + const objectFQN = toFQN(dataset.object, namespace); |
| 84 | + |
| 85 | + // Handle PaginatedResult wrapper — InMemoryDriver may return { value: [...] } |
| 86 | + let existing = await ql.find(objectFQN); |
| 87 | + if (existing && (existing as any).value) existing = (existing as any).value; |
| 88 | + |
| 89 | + if (!existing || existing.length === 0) { |
| 90 | + console.log(`[Vercel] Seeding ${dataset.records.length} records for ${objectFQN}`); |
| 91 | + for (const record of dataset.records) { |
| 92 | + await ql.insert(objectFQN, record); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Get (or create) the Hono application backed by the ObjectStack kernel. |
| 101 | + * The prefix `/api/v1` matches the client SDK's default API path. |
| 102 | + */ |
| 103 | +export async function getApp(): Promise<Hono> { |
| 104 | + if (_app) return _app; |
| 105 | + |
| 106 | + const kernel = await bootKernel(); |
| 107 | + _app = createHonoApp({ kernel, prefix: '/api/v1' }); |
| 108 | + return _app; |
| 109 | +} |
0 commit comments