/** * CoordinatorService - Main service layer for multi-tenant sync coordination. * * Manages multiple database stores with lazy loading and provides * sync operations with validation hooks and client session management. * * This is a generic implementation. Applications provide custom database ID * handling, cascade delete, and other app-specific behavior via hooks. */ import type { WebSocket } from 'ws'; import { type HLC, type SiteId, type ChangeSet, type ApplyResult, type SnapshotChunk, type SnapshotCheckpoint } from '@quereus/sync'; import type { CoordinatorConfig } from '../config/types.js'; import { type CoordinatorMetrics } from '../metrics/index.js'; import type { ClientIdentity, ClientSession, AuthContext, SyncOperation, CoordinatorHooks } from './types.js'; import { StoreManager, type StoreManagerHooks } from './store-manager.js'; import { type S3StorageConfig } from './s3-config.js'; import { type SnapshotScheduleConfig } from './s3-snapshot-store.js'; /** * Options for creating a CoordinatorService. */ export interface CoordinatorServiceOptions { /** Full configuration */ config: CoordinatorConfig; /** Custom hooks for validation/auth */ hooks?: CoordinatorHooks; /** Custom metrics (uses global registry if not provided) */ metrics?: CoordinatorMetrics; /** Hooks for customizing store behavior (database ID handling, path resolution) */ storeHooks?: StoreManagerHooks; /** S3 configuration for durable batch storage (optional) */ s3Config?: S3StorageConfig; /** Snapshot schedule configuration (optional) */ snapshotConfig?: Partial; } /** * Multi-tenant coordinator service that manages sync operations with hooks. */ export declare class CoordinatorService { private readonly config; private readonly hooks; private readonly metrics; private readonly _storeManager; private readonly s3BatchStore?; private readonly s3SnapshotStore?; private readonly maintenanceLoop; /** Active WebSocket sessions by connection ID */ private readonly sessions; /** Connection IDs by database ID for broadcasting */ private readonly databaseToConnections; private initialized; constructor(options: CoordinatorServiceOptions); /** * Get the underlying StoreManager for app-layer extensions. * Apps can use this to register listeners, cascade delete services, etc. */ get storeManager(): StoreManager; /** * Initialize the service. */ initialize(): Promise; /** * Shutdown the service. */ shutdown(): Promise; /** * Build a StoreContext from auth information. */ private buildStoreContext; /** * Get a store entry for a database, acquiring if needed. */ private getStore; /** * Release a store reference. */ private releaseStore; /** * Authenticate a request/connection. */ authenticate(context: AuthContext): Promise; /** * Authorize an operation for a client. */ authorize(client: ClientIdentity, operation: SyncOperation): Promise; /** * Get the coordinator's site ID for a specific database. */ getSiteId(databaseId: string, client?: ClientIdentity): Promise; /** * Get current HLC for a specific database. */ getCurrentHLC(databaseId: string, client?: ClientIdentity): Promise; /** * Get changes since a given HLC for a client. */ getChangesSince(databaseId: string, client: ClientIdentity, sinceHLC?: HLC): Promise; /** * Apply changes from a client. */ applyChanges(databaseId: string, client: ClientIdentity, changes: ChangeSet[]): Promise; /** * Stream a full snapshot. */ getSnapshotStream(databaseId: string, client: ClientIdentity, chunkSize?: number): AsyncIterable; /** * Resume a snapshot stream from a checkpoint. */ resumeSnapshotStream(databaseId: string, client: ClientIdentity, checkpoint: SnapshotCheckpoint): AsyncIterable; /** * Check if delta sync is possible. */ canDeltaSync(databaseId: string, client: ClientIdentity, sinceHLC: HLC): Promise; /** * Register a new WebSocket client session. * @param databaseId The database to connect to * @param socket The WebSocket connection * @param identity The authenticated client identity * @param authContext Optional auth context with raw token for store hooks */ registerSession(databaseId: string, socket: WebSocket, identity: ClientIdentity, authContext?: AuthContext): Promise; /** * Unregister a WebSocket client session. */ unregisterSession(connectionId: string): void; /** * Get a session by connection ID. */ getSession(connectionId: string): ClientSession | undefined; /** * Update the last sync HLC for a session. */ updateSessionSyncState(connectionId: string, hlc: HLC): void; /** * Broadcast changes to all connected clients on the same database except the sender. */ private broadcastChanges; /** * Check if a database ID is valid. */ isValidDatabaseId(databaseId: string): boolean; /** * Get server status and stats. */ getStatus(): { openStores: number; connectedClients: number; uptime: number; }; /** * Get the metrics registry for this service. */ getMetrics(): CoordinatorMetrics; /** * Restore a database from S3 snapshot + batches. * Called by StoreManager's onStoreCreated when a new store is opened with no local data. */ private restoreFromS3; /** * Manually trigger a snapshot for a database. */ createSnapshot(databaseId: string, client?: ClientIdentity): Promise<{ snapshotId: string; } | null>; /** * Get databases that need snapshots according to schedule. */ getDatabasesNeedingSnapshot(): string[]; } //# sourceMappingURL=coordinator-service.d.ts.map