import type * as Mysql2 from 'mysql2'; import type * as Pg from 'pg'; import type { Dialect } from '@enbox/dwn-sql-store'; import type { DidResolver } from '@enbox/dids'; import type { DwnServerConfig } from './config.js'; import type { EventBus } from './event-bus.js'; import type PgCursor from 'pg-cursor'; import type { DataStore, DwnConfig, EventLog, MessageStore, ResumableTaskStore, TenantGate, WakePublisher, } from '@enbox/dwn-sdk-js'; import * as fs from 'node:fs'; import { createRequire } from 'node:module'; import { fileURLToPath } from 'node:url'; import { Kysely } from 'kysely'; import { PluginLoader } from './plugin-loader.js'; import { runServerMigrations } from './server-migration-runner.js'; import { DurableEventLog, } from '@enbox/dwn-sdk-js'; import { createBunSqliteDatabase, DataStoreSql, MessageStoreSql, MysqlDialect, PostgresDialect, ResumableTaskStoreSql, runDwnStoreMigrations, SqliteDialect, } from '@enbox/dwn-sql-store'; import { DataStoreLevel, MessageStoreLevel, ResumableTaskStoreLevel } from '@enbox/dwn-sdk-js/stores/level'; const require = createRequire(import.meta.url); export enum StoreType { DataStore, MessageStore, ResumableTaskStore, } export enum BackendTypes { LEVEL = 'level', SQLITE = 'sqlite', MYSQL = 'mysql', POSTGRES = 'postgres', } export type DwnStore = DataStore | MessageStore | ResumableTaskStore; type GetDwnConfigOptions = { didResolver? : DidResolver; tenantGate? : TenantGate; eventLog? : EventLog; eventBus? : EventBus; enableEventLog? : boolean; }; type WakePublisherAwareStore = { setWakePublisher(wakePublisher: WakePublisher | undefined): void; }; const requiredMessageStorePluginMethods = [ 'completeData', 'logRead', 'logBounds', 'fingerprint', 'epoch', ] as const satisfies readonly (keyof MessageStore)[]; type Mysql2Module = { createPool : typeof Mysql2.createPool; }; type MysqlPool = ReturnType; type PgCursorModule = typeof PgCursor; type PgModule = { Pool : typeof Pg.Pool; }; type PgPool = Pg.Pool; type EndablePool = { end(): Promise }; type PostgresDependencies = { Cursor : PgCursorModule; pg : PgModule; }; function loadOptionalDependency(specifier: string, installCommand: string): TModule { try { return require(specifier) as TModule; } catch { throw new Error(`${specifier} is required for this backend. Install it with: ${installCommand}`); } } function loadMysqlCreatePool(): typeof Mysql2.createPool { return loadOptionalDependency( 'mysql2', 'bun add mysql2' ).createPool; } function loadPostgresDependencies(): PostgresDependencies { return { Cursor : loadOptionalDependency('pg-cursor', 'bun add pg pg-cursor'), pg : loadOptionalDependency('pg', 'bun add pg pg-cursor'), }; } /** * Returns a (potentially cached) dialect for the given connection URL. For * Postgres, creates a pool with configurable sizing from the server config. * For other backends, delegates to `getDialectFromUrl()` which handles its * own caching (critical for in-memory SQLite). * * All Postgres dialects are cached in a separate map keyed by URL so that * multiple DWN stores sharing the same Postgres URL reuse a single * `pg.Pool`, reducing connection count from 4 × pool_max to 1 × pool_max. */ const postgresDialectCache: Map = new Map(); function getOrCreateDialect(connectionUrl: URL, config: DwnServerConfig): Dialect { const protocol = connectionUrl.protocol.slice(0, -1); if (protocol !== BackendTypes.POSTGRES) { // getDialectFromUrl handles its own caching for SQLite/MySQL. return getDialectFromUrl(connectionUrl); } const key = connectionUrl.toString(); const cached = postgresDialectCache.get(key); if (cached !== undefined) { return cached; } const { Cursor, pg } = loadPostgresDependencies(); // Create a single pg.Pool instance with configurable sizing. const pool = new pg.Pool({ connectionString : connectionUrl.toString(), min : config.pgPoolMin, max : config.pgPoolMax, idleTimeoutMillis : config.pgPoolIdleTimeout, }); makePostgresPoolEndIdempotent(pool, (): void => { postgresDialectCache.delete(key); }); const dialect = new PostgresDialect({ pool : async (): Promise => pool, cursor : Cursor, }); postgresDialectCache.set(key, dialect); return dialect; } /** * Memoize a shared Postgres pool's shutdown without wrapping the pool itself. * Several Kysely drivers can own the cached dialect and each destroys it, but * `pg` rejects a second `end()`. The first call evicts the ended dialect and * every Kysely driver observes the same underlying completion. */ export function makePostgresPoolEndIdempotent(pool: EndablePool, onFirstEnd?: () => void): void { const end = pool.end.bind(pool); let completion: Promise | undefined; pool.end = (): Promise => { if (completion === undefined) { onFirstEnd?.(); completion = end(); } return completion; }; } export async function getDwnConfig( config : DwnServerConfig, options : GetDwnConfigOptions ): Promise { const { tenantGate, didResolver } = options; // Run SQL schema migrations before creating stores. Uses the data store // connection to determine the dialect — all SQL stores typically share the // same database. Non-SQL backends (level://) are skipped. await runSqlMigrationsIfNeeded(config); const dataStore: DataStore = await getStore(config, config.dataStore, StoreType.DataStore); const messageStore: MessageStore = await getStore(config, config.messageStore, StoreType.MessageStore, options.eventBus); const resumableTaskStore: ResumableTaskStore = await getStore(config, config.resumableTaskStore, StoreType.ResumableTaskStore); const eventLog = options.eventLog ?? createDurableEventLog(messageStore, options); return { didResolver, eventLog, dataStore, messageStore, resumableTaskStore, tenantGate }; } function createDurableEventLog(messageStore: MessageStore, options: GetDwnConfigOptions): EventLog | undefined { if (options.enableEventLog !== true) { return undefined; } if (options.eventBus === undefined) { throw new Error('DWN server misconfiguration: an EventBus is required when event log support is enabled.'); } return new DurableEventLog(messageStore, options.eventBus); } /** * Runs DWN SQL schema migrations if the data store is configured with a SQL * backend. Creates a temporary Kysely instance, runs all pending migrations, * then destroys it. The subsequent store `open()` calls will reuse the shared * dialect/pool and find the schema already in place. */ async function runSqlMigrationsIfNeeded(config: DwnServerConfig): Promise { // Skip if the data store config is a file path (plugin) or non-SQL backend if (isFilePath(config.dataStore)) { return; } let storeUrl: URL; try { storeUrl = new URL(config.dataStore); } catch { return; // Not a valid URL — skip } const protocol = storeUrl.protocol.slice(0, -1); const sqlBackends: string[] = [BackendTypes.SQLITE, BackendTypes.MYSQL, BackendTypes.POSTGRES]; if (!sqlBackends.includes(protocol)) { return; } const dialect = getOrCreateDialect(storeUrl, config); const db = new Kysely>({ dialect }); try { const applied = await runDwnStoreMigrations(db, dialect); if (applied.length > 0) { console.log(`DWN migrations applied: ${applied.join(', ')}`); } } finally { // Do NOT destroy the Kysely instance — the dialect is cached and will be // reused by stores. For in-memory SQLite, destroying would close the // database and lose all migrated schema. For Postgres, the pool is shared. } } /** * Runs DWN server schema migrations (admin stores, registration, TTL cache) * if the given URL points to a SQL backend. Uses the `registrationStoreUrl` * (or the TTL cache URL) as the target database. * * Server migrations use a separate tracking table (`dwn_server_migration`) * so they do not conflict with the DWN store migrations. * * Call this once during server startup, before creating admin stores. * * @returns The dialect used for the target database (so the caller can reuse * it for the TTL cache and admin stores), or `undefined` if no SQL * backend was configured or needed. */ export async function runServerMigrationsIfNeeded(config: DwnServerConfig): Promise { const sqlBackends = new Set([BackendTypes.SQLITE, BackendTypes.MYSQL, BackendTypes.POSTGRES]); // Determine the target URL for server migrations. Prefer registrationStoreUrl // since admin stores and the TTL cache share that database. Fall back to // ttlCacheUrl when no registration store is configured (the cacheEntries // table still needs a schema). const targetUrl = config.registrationStoreUrl ?? config.ttlCacheUrl; if (!targetUrl) { return undefined; } if (isFilePath(targetUrl)) { return undefined; } let parsedUrl: URL; try { parsedUrl = new URL(targetUrl); } catch { return undefined; } const protocol = parsedUrl.protocol.slice(0, -1); if (!sqlBackends.has(protocol)) { return undefined; } // When both registrationStoreUrl and ttlCacheUrl are set and differ, // validate they point at the same database — the cacheEntries table is // included in the server migration so it must live alongside the other // server tables. if (config.registrationStoreUrl && config.ttlCacheUrl && config.ttlCacheUrl !== config.registrationStoreUrl) { let ttlUrl: URL | undefined; try { ttlUrl = new URL(config.ttlCacheUrl); } catch { /* not a URL */ } if (ttlUrl) { const ttlProtocol = ttlUrl.protocol.slice(0, -1); if (sqlBackends.has(ttlProtocol)) { throw new Error( 'DWN server misconfiguration: DWN_TTL_CACHE_URL must point to the same database as ' + 'DWN_REGISTRATION_STORE_URL (or DWN_STORAGE) because the cacheEntries table is managed ' + 'by the server migration system. ' + `Got registrationStoreUrl="${config.registrationStoreUrl}", ttlCacheUrl="${config.ttlCacheUrl}".` ); } } } const dialect = getOrCreateDialect(parsedUrl, config); const db = new Kysely>({ dialect }); try { const applied = await runServerMigrations(db, dialect); if (applied.length > 0) { console.log(`Server migrations applied: ${applied.join(', ')}`); } } finally { // For Postgres, don't destroy — the pool is cached in sharedDialectCache. // For SQLite/MySQL, we also keep the Kysely instance alive so the caller // can reuse the same dialect (critical for in-memory SQLite). if (protocol === BackendTypes.POSTGRES) { // Pool stays alive via sharedDialectCache. } // NOTE: We intentionally do NOT destroy the Kysely instance for any // backend. The dialect is returned to the caller for reuse (e.g. by the // TTL cache and admin stores). For in-memory SQLite, destroying would // lose the database. } return dialect; } function getLevelStore( storeURI: URL, storeType: StoreType, wakePublisher?: WakePublisher, ): DwnStore { const location = storeURI.host === '' ? fileURLToPath(new URL(`file://${storeURI.pathname}`)) : storeURI.host + storeURI.pathname; switch (storeType) { case StoreType.DataStore: return new DataStoreLevel({ blockstoreLocation: location + '/DATASTORE', }); case StoreType.MessageStore: return new MessageStoreLevel({ location: location + '/MESSAGESTORE', wakePublisher, }); case StoreType.ResumableTaskStore: return new ResumableTaskStoreLevel({ location: location + '/RESUMABLE-TASK-STORE', }); default: throw new Error('Unexpected level store type'); } } function getSqlStore( config: DwnServerConfig, connectionUrl: URL, storeType: StoreType, wakePublisher?: WakePublisher, ): DwnStore { const dialect = getOrCreateDialect(connectionUrl, config); switch (storeType) { case StoreType.DataStore: return new DataStoreSql(dialect); case StoreType.MessageStore: return new MessageStoreSql(dialect, wakePublisher); case StoreType.ResumableTaskStore: return new ResumableTaskStoreSql(dialect); default: throw new Error(`Unsupported store type ${storeType} for SQL store.`); } } /** * Check if the given string is a file path. */ function isFilePath(configString: string): boolean { const filePathPrefixes = ['/', './', '../']; return filePathPrefixes.some(prefix => configString.startsWith(prefix)); } async function getStore(config: DwnServerConfig, storeString: string, storeType: StoreType.DataStore): Promise; async function getStore( config: DwnServerConfig, storeString: string, storeType: StoreType.MessageStore, wakePublisher?: WakePublisher, ): Promise; async function getStore(config: DwnServerConfig, storeString: string, storeType: StoreType.ResumableTaskStore): Promise; async function getStore( config: DwnServerConfig, storeConfigString: string, storeType: StoreType, wakePublisher?: WakePublisher, ): Promise { if (isFilePath(storeConfigString)) { return await loadStoreFromFilePath(storeConfigString, storeType, wakePublisher); } // else treat the `storeConfigString` as a connection string const storeURI = new URL(storeConfigString); switch (storeURI.protocol.slice(0, -1)) { case BackendTypes.LEVEL: return getLevelStore(storeURI, storeType, wakePublisher); case BackendTypes.SQLITE: case BackendTypes.MYSQL: case BackendTypes.POSTGRES: return getSqlStore(config, storeURI, storeType, wakePublisher); default: throw invalidStorageSchemeMessage(storeURI.protocol); } } /** * Loads a DWN store plugin of the given type from the given file path. */ async function loadStoreFromFilePath( filePath: string, storeType: StoreType, wakePublisher?: WakePublisher, ): Promise { let store: DwnStore; switch (storeType) { case StoreType.DataStore: store = await PluginLoader.loadPlugin(filePath); break; case StoreType.MessageStore: { const messageStorePlugin = await PluginLoader.loadPlugin(filePath); validateRequiredMessageStorePluginMethods(messageStorePlugin, filePath); setWakePublisherIfSupported(messageStorePlugin, wakePublisher); store = messageStorePlugin; break; } case StoreType.ResumableTaskStore: store = await PluginLoader.loadPlugin(filePath); break; default: throw new Error(`Loading store for unsupported store type ${storeType} from path ${filePath}`); } return store; } /** Checks only methods newly required from plugins; the pre-existing MessageStore surface remains trusted. */ function validateRequiredMessageStorePluginMethods(store: MessageStore, filePath: string): void { const missing = requiredMessageStorePluginMethods.filter(method => typeof store[method] !== 'function'); if (missing.length > 0) { throw new Error(`MessageStore plugin at ${filePath} is missing required methods: ${missing.join(', ')}`); } } function setWakePublisherIfSupported(store: DwnStore, wakePublisher: WakePublisher | undefined): void { const maybeStore = store as Partial; if (typeof maybeStore.setWakePublisher === 'function') { maybeStore.setWakePublisher(wakePublisher); } } /** * Cache for the in-memory SQLite dialect. Since every call to * `createBunSqliteDatabase(':memory:')` creates a separate, empty database, * we must ensure that `getDialectFromUrl(new URL('sqlite://'))` always * returns the same dialect (and thus the same underlying database) within a * process. This is critical for the DWN server startup flow where migrations, * the registration store, and the TTL cache all need to share the same * in-memory database. * * File-based SQLite and other backends are NOT cached here — file-based SQLite * connections naturally share state through the filesystem, and caching would * break test isolation when multiple test files run in the same process. */ let inMemorySqliteDialect: Dialect | undefined; export function getDialectFromUrl(connectionUrl: URL): Dialect { switch (connectionUrl.protocol.slice(0, -1)) { case BackendTypes.SQLITE: { const path = connectionUrl.host + connectionUrl.pathname; console.log('SQL-lite relative path:', path ? path : undefined); // NOTE, using ? for lose equality comparison if (connectionUrl.host && !fs.existsSync(connectionUrl.host)) { console.log('SQL-lite directory does not exist, creating:', connectionUrl.host); fs.mkdirSync(connectionUrl.host, { recursive: true }); } // Use in-memory database if no path is provided (for tests). const dbPath = path || ':memory:'; // For in-memory SQLite, return a cached dialect so that all callers // (migrations, registration store, TTL cache) share the same database. // The wrapper makes close() a no-op so that individual consumers (e.g. // DwnServer.stop() → Dwn.close() → store.close()) cannot destroy the // shared database out from under other consumers. if (dbPath === ':memory:') { if (inMemorySqliteDialect === undefined) { const sharedDb = createBunSqliteDatabase(':memory:'); const nonCloseableDb = { close(): void { // no-op — shared instance must survive the process }, prepare(sql: string): ReturnType { return sharedDb.prepare(sql); }, }; inMemorySqliteDialect = new SqliteDialect({ database: async (): Promise => nonCloseableDb, }); } return inMemorySqliteDialect; } return new SqliteDialect({ database: async () => createBunSqliteDatabase(dbPath), }); } case BackendTypes.MYSQL: { const createPool = loadMysqlCreatePool(); return new MysqlDialect({ pool: async (): Promise => createPool(connectionUrl.toString()), }); } case BackendTypes.POSTGRES: { const { Cursor, pg } = loadPostgresDependencies(); return new PostgresDialect({ pool : async (): Promise => new pg.Pool({ connectionString: connectionUrl.toString() }), cursor : Cursor, }); } default: throw new Error(`Unsupported database protocol: ${connectionUrl.protocol}`); } } function invalidStorageSchemeMessage(protocol: string): string { const schemes = []; for (const [_, value] of Object.entries(BackendTypes)) { schemes.push(value); } return ( 'Unknown storage protocol ' + protocol.slice(0, -1) + '! Please use one of: ' + schemes.join(', ') + '. For details, see README' ); }