@topgunbuild/client 0.2.0 → 0.3.0
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/dist/index.d.mts +840 -10
- package/dist/index.d.ts +840 -10
- package/dist/index.js +2258 -187
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2257 -180
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/SyncEngine.ts","../src/utils/logger.ts","../src/SyncState.ts","../src/SyncStateMachine.ts","../src/errors/BackpressureError.ts","../src/BackpressureConfig.ts","../src/TopGunClient.ts","../src/QueryHandle.ts","../src/DistributedLock.ts","../src/TopicHandle.ts","../src/adapters/IDBAdapter.ts","../src/TopGun.ts","../src/crypto/EncryptionManager.ts","../src/adapters/EncryptedStorageAdapter.ts","../src/index.ts"],"sourcesContent":["import { HLC, LWWMap, ORMap, serialize, deserialize, evaluatePredicate } from '@topgunbuild/core';\nimport type { LWWRecord, ORMapRecord, Timestamp } from '@topgunbuild/core';\nimport type { IStorageAdapter } from './IStorageAdapter';\nimport { QueryHandle } from './QueryHandle';\nimport type { QueryFilter } from './QueryHandle';\nimport { TopicHandle } from './TopicHandle';\nimport { logger } from './utils/logger';\nimport { SyncStateMachine, StateChangeEvent } from './SyncStateMachine';\nimport { SyncState } from './SyncState';\nimport { BackpressureError } from './errors/BackpressureError';\nimport type {\n BackpressureConfig,\n BackpressureStatus,\n BackpressureStrategy,\n BackpressureThresholdEvent,\n OperationDroppedEvent,\n} from './BackpressureConfig';\nimport { DEFAULT_BACKPRESSURE_CONFIG } from './BackpressureConfig';\n\nexport interface OpLogEntry {\n id: string; // Unique ID for the operation\n mapName: string;\n opType: 'PUT' | 'REMOVE' | 'OR_ADD' | 'OR_REMOVE';\n key: string;\n record?: LWWRecord<any>; // LWW Put/Remove (Remove has null value)\n orRecord?: ORMapRecord<any>; // ORMap Add\n orTag?: string; // ORMap Remove (Tombstone tag)\n timestamp: Timestamp; // HLC timestamp of the operation\n synced: boolean; // True if this operation has been successfully pushed to the server\n}\n\nexport interface HeartbeatConfig {\n intervalMs: number; // Default: 5000 (5 seconds)\n timeoutMs: number; // Default: 15000 (15 seconds)\n enabled: boolean; // Default: true\n}\n\nexport interface BackoffConfig {\n /** Initial delay in milliseconds (default: 1000) */\n initialDelayMs: number;\n /** Maximum delay in milliseconds (default: 30000) */\n maxDelayMs: number;\n /** Multiplier for exponential backoff (default: 2) */\n multiplier: number;\n /** Whether to add random jitter to delay (default: true) */\n jitter: boolean;\n /** Maximum number of retry attempts before entering ERROR state (default: 10) */\n maxRetries: number;\n}\n\nexport interface SyncEngineConfig {\n nodeId: string;\n serverUrl: string;\n storageAdapter: IStorageAdapter;\n reconnectInterval?: number;\n heartbeat?: Partial<HeartbeatConfig>;\n backoff?: Partial<BackoffConfig>;\n backpressure?: Partial<BackpressureConfig>;\n}\n\nconst DEFAULT_BACKOFF_CONFIG: BackoffConfig = {\n initialDelayMs: 1000,\n maxDelayMs: 30000,\n multiplier: 2,\n jitter: true,\n maxRetries: 10,\n};\n\nexport class SyncEngine {\n private readonly nodeId: string;\n private readonly serverUrl: string;\n private readonly storageAdapter: IStorageAdapter;\n private readonly hlc: HLC;\n private readonly stateMachine: SyncStateMachine;\n private readonly backoffConfig: BackoffConfig;\n\n private websocket: WebSocket | null = null;\n private opLog: OpLogEntry[] = [];\n private maps: Map<string, LWWMap<any, any> | ORMap<any, any>> = new Map();\n private queries: Map<string, QueryHandle<any>> = new Map();\n private topics: Map<string, TopicHandle> = new Map();\n private pendingLockRequests: Map<string, { resolve: (res: any) => void, reject: (err: any) => void, timer: any }> = new Map();\n private lastSyncTimestamp: number = 0;\n private reconnectTimer: any = null; // NodeJS.Timeout\n private authToken: string | null = null;\n private tokenProvider: (() => Promise<string | null>) | null = null;\n private backoffAttempt: number = 0;\n\n // Heartbeat state\n private readonly heartbeatConfig: HeartbeatConfig;\n private heartbeatInterval: ReturnType<typeof setInterval> | null = null;\n private lastPongReceived: number = Date.now();\n private lastRoundTripTime: number | null = null;\n\n // Backpressure state\n private readonly backpressureConfig: BackpressureConfig;\n private backpressurePaused: boolean = false;\n private waitingForCapacity: Array<() => void> = [];\n private highWaterMarkEmitted: boolean = false;\n private backpressureListeners: Map<string, Set<(...args: any[]) => void>> = new Map();\n\n constructor(config: SyncEngineConfig) {\n this.nodeId = config.nodeId;\n this.serverUrl = config.serverUrl;\n this.storageAdapter = config.storageAdapter;\n this.hlc = new HLC(this.nodeId);\n\n // Initialize state machine\n this.stateMachine = new SyncStateMachine();\n\n // Initialize heartbeat config with defaults\n this.heartbeatConfig = {\n intervalMs: config.heartbeat?.intervalMs ?? 5000,\n timeoutMs: config.heartbeat?.timeoutMs ?? 15000,\n enabled: config.heartbeat?.enabled ?? true,\n };\n\n // Merge backoff config with defaults\n this.backoffConfig = {\n ...DEFAULT_BACKOFF_CONFIG,\n ...config.backoff,\n };\n\n // Merge backpressure config with defaults\n this.backpressureConfig = {\n ...DEFAULT_BACKPRESSURE_CONFIG,\n ...config.backpressure,\n };\n\n this.initConnection();\n this.loadOpLog();\n }\n\n // ============================================\n // State Machine Public API\n // ============================================\n\n /**\n * Get the current connection state\n */\n getConnectionState(): SyncState {\n return this.stateMachine.getState();\n }\n\n /**\n * Subscribe to connection state changes\n * @returns Unsubscribe function\n */\n onConnectionStateChange(listener: (event: StateChangeEvent) => void): () => void {\n return this.stateMachine.onStateChange(listener);\n }\n\n /**\n * Get state machine history for debugging\n */\n getStateHistory(limit?: number): StateChangeEvent[] {\n return this.stateMachine.getHistory(limit);\n }\n\n // ============================================\n // Internal State Helpers (replace boolean flags)\n // ============================================\n\n /**\n * Check if WebSocket is connected (but may not be authenticated yet)\n */\n private isOnline(): boolean {\n const state = this.stateMachine.getState();\n return (\n state === SyncState.CONNECTING ||\n state === SyncState.AUTHENTICATING ||\n state === SyncState.SYNCING ||\n state === SyncState.CONNECTED\n );\n }\n\n /**\n * Check if fully authenticated and ready for operations\n */\n private isAuthenticated(): boolean {\n const state = this.stateMachine.getState();\n return state === SyncState.SYNCING || state === SyncState.CONNECTED;\n }\n\n /**\n * Check if fully connected and synced\n */\n private isConnected(): boolean {\n return this.stateMachine.getState() === SyncState.CONNECTED;\n }\n\n // ============================================\n // Connection Management\n // ============================================\n\n private initConnection(): void {\n // Transition to CONNECTING state\n this.stateMachine.transition(SyncState.CONNECTING);\n\n this.websocket = new WebSocket(this.serverUrl);\n this.websocket.binaryType = 'arraybuffer';\n\n this.websocket.onopen = () => {\n // WebSocket is open, now we need to authenticate\n // [CHANGE] Don't send auth immediately if we don't have a token\n // This prevents the \"AUTH_REQUIRED -> Close -> Retry loop\" for anonymous initial connects\n if (this.authToken || this.tokenProvider) {\n logger.info('WebSocket connected. Sending auth...');\n this.stateMachine.transition(SyncState.AUTHENTICATING);\n this.sendAuth();\n } else {\n logger.info('WebSocket connected. Waiting for auth token...');\n // Stay in CONNECTING state until we have a token\n // We're online but not authenticated\n this.stateMachine.transition(SyncState.AUTHENTICATING);\n }\n };\n\n this.websocket.onmessage = (event) => {\n let message: any;\n if (event.data instanceof ArrayBuffer) {\n message = deserialize(new Uint8Array(event.data));\n } else {\n try {\n message = JSON.parse(event.data);\n } catch (e) {\n logger.error({ err: e }, 'Failed to parse message');\n return;\n }\n }\n this.handleServerMessage(message);\n };\n\n this.websocket.onclose = () => {\n logger.info('WebSocket disconnected.');\n this.stopHeartbeat();\n this.stateMachine.transition(SyncState.DISCONNECTED);\n this.scheduleReconnect();\n };\n\n this.websocket.onerror = (error) => {\n logger.error({ err: error }, 'WebSocket error');\n // Error will typically be followed by close, so we don't transition here\n };\n }\n\n private scheduleReconnect(): void {\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = null;\n }\n\n // Check if we've exceeded max retries\n if (this.backoffAttempt >= this.backoffConfig.maxRetries) {\n logger.error(\n { attempts: this.backoffAttempt },\n 'Max reconnection attempts reached. Entering ERROR state.'\n );\n this.stateMachine.transition(SyncState.ERROR);\n return;\n }\n\n // Transition to BACKOFF state\n this.stateMachine.transition(SyncState.BACKOFF);\n\n const delay = this.calculateBackoffDelay();\n logger.info({ delay, attempt: this.backoffAttempt }, `Backing off for ${delay}ms`);\n\n this.reconnectTimer = setTimeout(() => {\n this.reconnectTimer = null;\n this.backoffAttempt++;\n this.initConnection();\n }, delay);\n }\n\n private calculateBackoffDelay(): number {\n const { initialDelayMs, maxDelayMs, multiplier, jitter } = this.backoffConfig;\n let delay = initialDelayMs * Math.pow(multiplier, this.backoffAttempt);\n delay = Math.min(delay, maxDelayMs);\n\n if (jitter) {\n // Add jitter: 0.5x to 1.5x of calculated delay\n delay = delay * (0.5 + Math.random());\n }\n\n return Math.floor(delay);\n }\n\n /**\n * Reset backoff counter (called on successful connection)\n */\n private resetBackoff(): void {\n this.backoffAttempt = 0;\n }\n\n private async loadOpLog(): Promise<void> {\n const storedTimestamp = await this.storageAdapter.getMeta('lastSyncTimestamp');\n if (storedTimestamp) {\n this.lastSyncTimestamp = storedTimestamp;\n }\n\n const pendingOps = await this.storageAdapter.getPendingOps();\n this.opLog = pendingOps.map(op => ({\n ...op,\n id: String(op.id),\n synced: false\n })) as unknown as OpLogEntry[];\n\n if (this.opLog.length > 0) {\n logger.info({ count: this.opLog.length }, 'Loaded pending operations from local storage');\n }\n }\n\n private async saveOpLog(): Promise<void> {\n await this.storageAdapter.setMeta('lastSyncTimestamp', this.lastSyncTimestamp);\n }\n\n public registerMap(mapName: string, map: LWWMap<any, any> | ORMap<any, any>): void {\n this.maps.set(mapName, map);\n }\n\n public async recordOperation(\n mapName: string,\n opType: 'PUT' | 'REMOVE' | 'OR_ADD' | 'OR_REMOVE',\n key: string,\n data: { record?: LWWRecord<any>; orRecord?: ORMapRecord<any>; orTag?: string; timestamp: Timestamp }\n ): Promise<string> {\n // Check backpressure before adding new operation\n await this.checkBackpressure();\n\n const opLogEntry: Omit<OpLogEntry, 'id'> & { id?: string } = {\n mapName,\n opType,\n key,\n record: data.record,\n orRecord: data.orRecord,\n orTag: data.orTag,\n timestamp: data.timestamp,\n synced: false,\n };\n\n const id = await this.storageAdapter.appendOpLog(opLogEntry as any);\n opLogEntry.id = String(id);\n\n this.opLog.push(opLogEntry as OpLogEntry);\n\n // Check high water mark after adding operation\n this.checkHighWaterMark();\n\n if (this.isAuthenticated()) {\n this.syncPendingOperations();\n }\n\n return opLogEntry.id;\n }\n\n private syncPendingOperations(): void {\n const pending = this.opLog.filter(op => !op.synced);\n if (pending.length === 0) return;\n\n logger.info({ count: pending.length }, 'Syncing pending operations');\n\n if (this.websocket?.readyState === WebSocket.OPEN) {\n this.websocket.send(serialize({\n type: 'OP_BATCH',\n payload: {\n ops: pending\n }\n }));\n }\n }\n\n private startMerkleSync(): void {\n for (const [mapName, map] of this.maps) {\n if (map instanceof LWWMap) {\n logger.info({ mapName }, 'Starting Merkle sync for LWWMap');\n this.websocket?.send(serialize({\n type: 'SYNC_INIT',\n mapName,\n lastSyncTimestamp: this.lastSyncTimestamp\n }));\n } else if (map instanceof ORMap) {\n logger.info({ mapName }, 'Starting Merkle sync for ORMap');\n const tree = map.getMerkleTree();\n const rootHash = tree.getRootHash();\n\n // Build bucket hashes for all non-empty buckets at depth 0\n const bucketHashes: Record<string, number> = tree.getBuckets('');\n\n this.websocket?.send(serialize({\n type: 'ORMAP_SYNC_INIT',\n mapName,\n rootHash,\n bucketHashes,\n lastSyncTimestamp: this.lastSyncTimestamp\n }));\n }\n }\n }\n\n public setAuthToken(token: string): void {\n this.authToken = token;\n this.tokenProvider = null;\n\n const state = this.stateMachine.getState();\n if (state === SyncState.AUTHENTICATING || state === SyncState.CONNECTING) {\n // If we are already connected (e.g. waiting for token), send it now\n this.sendAuth();\n } else if (state === SyncState.BACKOFF || state === SyncState.DISCONNECTED) {\n // [CHANGE] Force immediate reconnect if we were waiting for retry timer\n logger.info('Auth token set during backoff/disconnect. Reconnecting immediately.');\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = null;\n }\n // Reset backoff since user provided new credentials\n this.resetBackoff();\n this.initConnection();\n }\n }\n\n public setTokenProvider(provider: () => Promise<string | null>): void {\n this.tokenProvider = provider;\n const state = this.stateMachine.getState();\n if (state === SyncState.AUTHENTICATING) {\n this.sendAuth();\n }\n }\n\n private async sendAuth(): Promise<void> {\n if (this.tokenProvider) {\n try {\n const token = await this.tokenProvider();\n if (token) {\n this.authToken = token;\n }\n } catch (err) {\n logger.error({ err }, 'Failed to get token from provider');\n return;\n }\n }\n\n const token = this.authToken;\n if (!token) return; // Don't send anonymous auth anymore\n\n this.websocket?.send(serialize({\n type: 'AUTH',\n token\n }));\n }\n\n public subscribeToQuery(query: QueryHandle<any>) {\n this.queries.set(query.id, query);\n if (this.isAuthenticated()) {\n this.sendQuerySubscription(query);\n }\n }\n\n public subscribeToTopic(topic: string, handle: TopicHandle) {\n this.topics.set(topic, handle);\n if (this.isAuthenticated()) {\n this.sendTopicSubscription(topic);\n }\n }\n\n public unsubscribeFromTopic(topic: string) {\n this.topics.delete(topic);\n if (this.isAuthenticated()) {\n this.websocket?.send(serialize({\n type: 'TOPIC_UNSUB',\n payload: { topic }\n }));\n }\n }\n\n public publishTopic(topic: string, data: any) {\n if (this.isAuthenticated()) {\n this.websocket?.send(serialize({\n type: 'TOPIC_PUB',\n payload: { topic, data }\n }));\n } else {\n // TODO: Queue topic messages or drop?\n // Spec says Fire-and-Forget, so dropping is acceptable if offline,\n // but queueing is better UX.\n // For now, log warning.\n logger.warn({ topic }, 'Dropped topic publish (offline)');\n }\n }\n\n private sendTopicSubscription(topic: string) {\n this.websocket?.send(serialize({\n type: 'TOPIC_SUB',\n payload: { topic }\n }));\n }\n\n /**\n * Executes a query against local storage immediately\n */\n public async runLocalQuery(mapName: string, filter: QueryFilter): Promise<{ key: string, value: any }[]> {\n // Retrieve all keys for the map\n const keys = await this.storageAdapter.getAllKeys();\n const mapKeys = keys.filter(k => k.startsWith(mapName + ':'));\n\n const results = [];\n for (const fullKey of mapKeys) {\n const record = await this.storageAdapter.get(fullKey);\n if (record && record.value) {\n // Extract actual key from \"mapName:key\"\n const actualKey = fullKey.slice(mapName.length + 1);\n\n let matches = true;\n\n // Apply 'where' (equality)\n if (filter.where) {\n for (const [k, v] of Object.entries(filter.where)) {\n if (record.value[k] !== v) {\n matches = false;\n break;\n }\n }\n }\n\n // Apply 'predicate'\n if (matches && filter.predicate) {\n if (!evaluatePredicate(filter.predicate, record.value)) {\n matches = false;\n }\n }\n\n if (matches) {\n results.push({ key: actualKey, value: record.value });\n }\n }\n }\n return results;\n }\n\n public unsubscribeFromQuery(queryId: string) {\n this.queries.delete(queryId);\n if (this.isAuthenticated()) {\n this.websocket?.send(serialize({\n type: 'QUERY_UNSUB',\n payload: { queryId }\n }));\n }\n }\n\n private sendQuerySubscription(query: QueryHandle<any>) {\n this.websocket?.send(serialize({\n type: 'QUERY_SUB',\n payload: {\n queryId: query.id,\n mapName: query.getMapName(),\n query: query.getFilter()\n }\n }));\n }\n\n public requestLock(name: string, requestId: string, ttl: number): Promise<{ fencingToken: number }> {\n if (!this.isAuthenticated()) {\n return Promise.reject(new Error('Not connected or authenticated'));\n }\n\n return new Promise((resolve, reject) => {\n // Timeout if no response (server might be down or message lost)\n // We set a client-side timeout slightly larger than TTL if TTL is short,\n // but usually we want a separate \"Wait Timeout\".\n // For now, use a fixed 30s timeout for the *response*.\n const timer = setTimeout(() => {\n if (this.pendingLockRequests.has(requestId)) {\n this.pendingLockRequests.delete(requestId);\n reject(new Error('Lock request timed out waiting for server response'));\n }\n }, 30000);\n\n this.pendingLockRequests.set(requestId, { resolve, reject, timer });\n\n try {\n this.websocket?.send(serialize({\n type: 'LOCK_REQUEST',\n payload: { requestId, name, ttl }\n }));\n } catch (e) {\n clearTimeout(timer);\n this.pendingLockRequests.delete(requestId);\n reject(e);\n }\n });\n }\n\n public releaseLock(name: string, requestId: string, fencingToken: number): Promise<boolean> {\n if (!this.isOnline()) return Promise.resolve(false);\n\n return new Promise((resolve, reject) => {\n const timer = setTimeout(() => {\n if (this.pendingLockRequests.has(requestId)) {\n this.pendingLockRequests.delete(requestId);\n // Resolve false on timeout? Or reject?\n // Release is usually fire-and-forget but we wanted ACK.\n resolve(false);\n }\n }, 5000);\n\n this.pendingLockRequests.set(requestId, { resolve, reject, timer });\n\n try {\n this.websocket?.send(serialize({\n type: 'LOCK_RELEASE',\n payload: { requestId, name, fencingToken }\n }));\n } catch (e) {\n clearTimeout(timer);\n this.pendingLockRequests.delete(requestId);\n resolve(false);\n }\n });\n }\n\n private async handleServerMessage(message: any): Promise<void> {\n switch (message.type) {\n case 'AUTH_REQUIRED':\n this.sendAuth();\n break;\n\n case 'AUTH_ACK': {\n logger.info('Authenticated successfully');\n const wasAuthenticated = this.isAuthenticated();\n\n // Transition to SYNCING state\n this.stateMachine.transition(SyncState.SYNCING);\n\n // Reset backoff on successful auth\n this.resetBackoff();\n\n this.syncPendingOperations();\n\n // Only re-subscribe on first authentication to prevent UI flickering\n if (!wasAuthenticated) {\n this.startHeartbeat();\n this.startMerkleSync();\n for (const query of this.queries.values()) {\n this.sendQuerySubscription(query);\n }\n for (const topic of this.topics.keys()) {\n this.sendTopicSubscription(topic);\n }\n }\n\n // After initial sync setup, transition to CONNECTED\n // In a real implementation, you might wait for SYNC_COMPLETE message\n this.stateMachine.transition(SyncState.CONNECTED);\n break;\n }\n\n case 'PONG': {\n this.handlePong(message);\n break;\n }\n\n case 'AUTH_FAIL':\n logger.error({ error: message.error }, 'Authentication failed');\n this.authToken = null; // Clear invalid token\n // Stay in AUTHENTICATING or go to ERROR depending on severity\n // For now, let the connection close naturally or retry with new token\n break;\n\n case 'OP_ACK': {\n const { lastId } = message.payload;\n logger.info({ lastId }, 'Received ACK for ops');\n let maxSyncedId = -1;\n let ackedCount = 0;\n this.opLog.forEach(op => {\n if (op.id && op.id <= lastId) {\n if (!op.synced) {\n ackedCount++;\n }\n op.synced = true;\n const idNum = parseInt(op.id, 10);\n if (!isNaN(idNum) && idNum > maxSyncedId) {\n maxSyncedId = idNum;\n }\n }\n });\n if (maxSyncedId !== -1) {\n this.storageAdapter.markOpsSynced(maxSyncedId).catch(err => logger.error({ err }, 'Failed to mark ops synced'));\n }\n // Check low water mark after ACKs reduce pending count\n if (ackedCount > 0) {\n this.checkLowWaterMark();\n }\n break;\n }\n\n case 'LOCK_GRANTED': {\n const { requestId, fencingToken } = message.payload;\n const req = this.pendingLockRequests.get(requestId);\n if (req) {\n clearTimeout(req.timer);\n this.pendingLockRequests.delete(requestId);\n req.resolve({ fencingToken });\n }\n break;\n }\n\n case 'LOCK_RELEASED': {\n const { requestId, success } = message.payload;\n const req = this.pendingLockRequests.get(requestId);\n if (req) {\n clearTimeout(req.timer);\n this.pendingLockRequests.delete(requestId);\n req.resolve(success);\n }\n break;\n }\n\n case 'QUERY_RESP': {\n const { queryId, results } = message.payload;\n const query = this.queries.get(queryId);\n if (query) {\n query.onResult(results, 'server');\n }\n break;\n }\n\n case 'QUERY_UPDATE': {\n const { queryId, key, value, type } = message.payload;\n const query = this.queries.get(queryId);\n if (query) {\n query.onUpdate(key, type === 'REMOVE' ? null : value);\n }\n break;\n }\n\n case 'SERVER_EVENT': {\n // Modified to support ORMap\n const { mapName, eventType, key, record, orRecord, orTag } = message.payload;\n const localMap = this.maps.get(mapName);\n if (localMap) {\n if (localMap instanceof LWWMap && record) {\n localMap.merge(key, record);\n await this.storageAdapter.put(`${mapName}:${key}`, record);\n } else if (localMap instanceof ORMap) {\n if (eventType === 'OR_ADD' && orRecord) {\n localMap.apply(key, orRecord);\n // We need to store ORMap records differently in storageAdapter or use a convention\n // For now, skipping persistent storage update for ORMap in this example\n } else if (eventType === 'OR_REMOVE' && orTag) {\n localMap.applyTombstone(orTag);\n }\n }\n }\n break;\n }\n\n case 'TOPIC_MESSAGE': {\n const { topic, data, publisherId, timestamp } = message.payload;\n const handle = this.topics.get(topic);\n if (handle) {\n handle.onMessage(data, { publisherId, timestamp });\n }\n break;\n }\n\n case 'GC_PRUNE': {\n const { olderThan } = message.payload;\n logger.info({ olderThan: olderThan.millis }, 'Received GC_PRUNE request');\n\n for (const [name, map] of this.maps) {\n if (map instanceof LWWMap) {\n const removedKeys = map.prune(olderThan);\n for (const key of removedKeys) {\n await this.storageAdapter.remove(`${name}:${key}`);\n }\n if (removedKeys.length > 0) {\n logger.info({ mapName: name, count: removedKeys.length }, 'Pruned tombstones from LWWMap');\n }\n } else if (map instanceof ORMap) {\n const removedTags = map.prune(olderThan);\n if (removedTags.length > 0) {\n logger.info({ mapName: name, count: removedTags.length }, 'Pruned tombstones from ORMap');\n }\n }\n }\n break;\n }\n\n case 'SYNC_RESET_REQUIRED': {\n const { mapName } = message.payload;\n logger.warn({ mapName }, 'Sync Reset Required due to GC Age');\n await this.resetMap(mapName);\n // Trigger re-sync as fresh\n this.websocket?.send(serialize({\n type: 'SYNC_INIT',\n mapName,\n lastSyncTimestamp: 0\n }));\n break;\n }\n\n case 'SYNC_RESP_ROOT': {\n const { mapName, rootHash, timestamp } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof LWWMap) {\n const localRootHash = map.getMerkleTree().getRootHash();\n if (localRootHash !== rootHash) {\n logger.info({ mapName, localRootHash, remoteRootHash: rootHash }, 'Root hash mismatch, requesting buckets');\n this.websocket?.send(serialize({\n type: 'MERKLE_REQ_BUCKET',\n payload: { mapName, path: '' }\n }));\n } else {\n logger.info({ mapName }, 'Map is in sync');\n }\n }\n // Update HLC with server timestamp\n if (timestamp) {\n this.hlc.update(timestamp);\n this.lastSyncTimestamp = timestamp.millis;\n await this.saveOpLog();\n }\n break;\n }\n\n case 'SYNC_RESP_BUCKETS': {\n const { mapName, path, buckets } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof LWWMap) {\n const tree = map.getMerkleTree();\n const localBuckets = tree.getBuckets(path);\n\n for (const [bucketKey, remoteHash] of Object.entries(buckets)) {\n const localHash = localBuckets[bucketKey] || 0;\n if (localHash !== remoteHash) {\n const newPath = path + bucketKey;\n this.websocket?.send(serialize({\n type: 'MERKLE_REQ_BUCKET',\n payload: { mapName, path: newPath }\n }));\n }\n }\n }\n break;\n }\n\n case 'SYNC_RESP_LEAF': {\n const { mapName, records } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof LWWMap) {\n let updateCount = 0;\n for (const { key, record } of records) {\n // Merge into local map\n const updated = map.merge(key, record);\n if (updated) {\n updateCount++;\n // Persist to storage\n await this.storageAdapter.put(`${mapName}:${key}`, record);\n }\n }\n if (updateCount > 0) {\n logger.info({ mapName, count: updateCount }, 'Synced records from server');\n }\n }\n break;\n }\n\n // ============ ORMap Sync Message Handlers ============\n\n case 'ORMAP_SYNC_RESP_ROOT': {\n const { mapName, rootHash, timestamp } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof ORMap) {\n const localTree = map.getMerkleTree();\n const localRootHash = localTree.getRootHash();\n\n if (localRootHash !== rootHash) {\n logger.info({ mapName, localRootHash, remoteRootHash: rootHash }, 'ORMap root hash mismatch, requesting buckets');\n this.websocket?.send(serialize({\n type: 'ORMAP_MERKLE_REQ_BUCKET',\n payload: { mapName, path: '' }\n }));\n } else {\n logger.info({ mapName }, 'ORMap is in sync');\n }\n }\n // Update HLC with server timestamp\n if (timestamp) {\n this.hlc.update(timestamp);\n this.lastSyncTimestamp = timestamp.millis;\n await this.saveOpLog();\n }\n break;\n }\n\n case 'ORMAP_SYNC_RESP_BUCKETS': {\n const { mapName, path, buckets } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof ORMap) {\n const tree = map.getMerkleTree();\n const localBuckets = tree.getBuckets(path);\n\n for (const [bucketKey, remoteHash] of Object.entries(buckets)) {\n const localHash = localBuckets[bucketKey] || 0;\n if (localHash !== remoteHash) {\n const newPath = path + bucketKey;\n this.websocket?.send(serialize({\n type: 'ORMAP_MERKLE_REQ_BUCKET',\n payload: { mapName, path: newPath }\n }));\n }\n }\n\n // Also check for buckets that exist locally but not on remote\n for (const [bucketKey, localHash] of Object.entries(localBuckets)) {\n if (!(bucketKey in buckets) && localHash !== 0) {\n // Local has data that remote doesn't - need to push\n const newPath = path + bucketKey;\n const keys = tree.getKeysInBucket(newPath);\n if (keys.length > 0) {\n this.pushORMapDiff(mapName, keys, map);\n }\n }\n }\n }\n break;\n }\n\n case 'ORMAP_SYNC_RESP_LEAF': {\n const { mapName, entries } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof ORMap) {\n let totalAdded = 0;\n let totalUpdated = 0;\n\n for (const entry of entries) {\n const { key, records, tombstones } = entry;\n const result = map.mergeKey(key, records, tombstones);\n totalAdded += result.added;\n totalUpdated += result.updated;\n }\n\n if (totalAdded > 0 || totalUpdated > 0) {\n logger.info({ mapName, added: totalAdded, updated: totalUpdated }, 'Synced ORMap records from server');\n }\n\n // Now push any local records that server might not have\n const keysToCheck = entries.map((e: { key: string }) => e.key);\n await this.pushORMapDiff(mapName, keysToCheck, map);\n }\n break;\n }\n\n case 'ORMAP_DIFF_RESPONSE': {\n const { mapName, entries } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof ORMap) {\n let totalAdded = 0;\n let totalUpdated = 0;\n\n for (const entry of entries) {\n const { key, records, tombstones } = entry;\n const result = map.mergeKey(key, records, tombstones);\n totalAdded += result.added;\n totalUpdated += result.updated;\n }\n\n if (totalAdded > 0 || totalUpdated > 0) {\n logger.info({ mapName, added: totalAdded, updated: totalUpdated }, 'Merged ORMap diff from server');\n }\n }\n break;\n }\n }\n\n if (message.timestamp) {\n this.hlc.update(message.timestamp);\n this.lastSyncTimestamp = message.timestamp.millis;\n await this.saveOpLog();\n }\n }\n\n public getHLC(): HLC {\n return this.hlc;\n }\n\n /**\n * Closes the WebSocket connection and cleans up resources.\n */\n public close(): void {\n this.stopHeartbeat();\n\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = null;\n }\n\n if (this.websocket) {\n this.websocket.onclose = null; // Prevent reconnect on intentional close\n this.websocket.close();\n this.websocket = null;\n }\n\n this.stateMachine.transition(SyncState.DISCONNECTED);\n logger.info('SyncEngine closed');\n }\n\n /**\n * Reset the state machine and connection.\n * Use after fatal errors to start fresh.\n */\n public resetConnection(): void {\n this.close();\n this.stateMachine.reset();\n this.resetBackoff();\n this.initConnection();\n }\n\n private async resetMap(mapName: string): Promise<void> {\n const map = this.maps.get(mapName);\n if (map) {\n // Clear memory\n if (map instanceof LWWMap) {\n map.clear();\n } else if (map instanceof ORMap) {\n map.clear();\n }\n }\n\n // Clear storage\n const allKeys = await this.storageAdapter.getAllKeys();\n const mapKeys = allKeys.filter(k => k.startsWith(mapName + ':'));\n for (const key of mapKeys) {\n await this.storageAdapter.remove(key);\n }\n logger.info({ mapName, removedStorageCount: mapKeys.length }, 'Reset map: Cleared memory and storage');\n }\n\n // ============ Heartbeat Methods ============\n\n /**\n * Starts the heartbeat mechanism after successful connection.\n */\n private startHeartbeat(): void {\n if (!this.heartbeatConfig.enabled) {\n return;\n }\n\n this.stopHeartbeat(); // Clear any existing interval\n this.lastPongReceived = Date.now();\n\n this.heartbeatInterval = setInterval(() => {\n this.sendPing();\n this.checkHeartbeatTimeout();\n }, this.heartbeatConfig.intervalMs);\n\n logger.info({ intervalMs: this.heartbeatConfig.intervalMs }, 'Heartbeat started');\n }\n\n /**\n * Stops the heartbeat mechanism.\n */\n private stopHeartbeat(): void {\n if (this.heartbeatInterval) {\n clearInterval(this.heartbeatInterval);\n this.heartbeatInterval = null;\n logger.info('Heartbeat stopped');\n }\n }\n\n /**\n * Sends a PING message to the server.\n */\n private sendPing(): void {\n if (this.websocket?.readyState === WebSocket.OPEN) {\n const pingMessage = {\n type: 'PING',\n timestamp: Date.now(),\n };\n this.websocket.send(serialize(pingMessage));\n }\n }\n\n /**\n * Handles incoming PONG message from server.\n */\n private handlePong(msg: { timestamp: number; serverTime: number }): void {\n const now = Date.now();\n this.lastPongReceived = now;\n this.lastRoundTripTime = now - msg.timestamp;\n\n logger.debug({\n rtt: this.lastRoundTripTime,\n serverTime: msg.serverTime,\n clockSkew: msg.serverTime - (msg.timestamp + this.lastRoundTripTime / 2),\n }, 'Received PONG');\n }\n\n /**\n * Checks if heartbeat has timed out and triggers reconnection if needed.\n */\n private checkHeartbeatTimeout(): void {\n const now = Date.now();\n const timeSinceLastPong = now - this.lastPongReceived;\n\n if (timeSinceLastPong > this.heartbeatConfig.timeoutMs) {\n logger.warn({\n timeSinceLastPong,\n timeoutMs: this.heartbeatConfig.timeoutMs,\n }, 'Heartbeat timeout - triggering reconnection');\n\n this.stopHeartbeat();\n\n // Force close and reconnect\n if (this.websocket) {\n this.websocket.close();\n }\n }\n }\n\n /**\n * Returns the last measured round-trip time in milliseconds.\n * Returns null if no PONG has been received yet.\n */\n public getLastRoundTripTime(): number | null {\n return this.lastRoundTripTime;\n }\n\n /**\n * Returns true if the connection is considered healthy based on heartbeat.\n * A connection is healthy if it's online, authenticated, and has received\n * a PONG within the timeout window.\n */\n public isConnectionHealthy(): boolean {\n if (!this.isOnline() || !this.isAuthenticated()) {\n return false;\n }\n\n if (!this.heartbeatConfig.enabled) {\n return true; // If heartbeat disabled, consider healthy if online\n }\n\n const timeSinceLastPong = Date.now() - this.lastPongReceived;\n return timeSinceLastPong < this.heartbeatConfig.timeoutMs;\n }\n\n // ============ ORMap Sync Methods ============\n\n /**\n * Push local ORMap diff to server for the given keys.\n * Sends local records and tombstones that the server might not have.\n */\n private async pushORMapDiff(\n mapName: string,\n keys: string[],\n map: ORMap<any, any>\n ): Promise<void> {\n const entries: Array<{\n key: string;\n records: ORMapRecord<any>[];\n tombstones: string[];\n }> = [];\n\n const snapshot = map.getSnapshot();\n\n for (const key of keys) {\n const recordsMap = map.getRecordsMap(key);\n if (recordsMap && recordsMap.size > 0) {\n // Get records as array\n const records = Array.from(recordsMap.values());\n\n // Get tombstones relevant to this key's records\n // (tombstones that match tags that were in this key)\n const tombstones: string[] = [];\n for (const tag of snapshot.tombstones) {\n // Include all tombstones - server will filter\n tombstones.push(tag);\n }\n\n entries.push({\n key,\n records,\n tombstones\n });\n }\n }\n\n if (entries.length > 0) {\n this.websocket?.send(serialize({\n type: 'ORMAP_PUSH_DIFF',\n payload: {\n mapName,\n entries\n }\n }));\n logger.debug({ mapName, keyCount: entries.length }, 'Pushed ORMap diff to server');\n }\n }\n\n // ============ Backpressure Methods ============\n\n /**\n * Get the current number of pending (unsynced) operations.\n */\n public getPendingOpsCount(): number {\n return this.opLog.filter(op => !op.synced).length;\n }\n\n /**\n * Get the current backpressure status.\n */\n public getBackpressureStatus(): BackpressureStatus {\n const pending = this.getPendingOpsCount();\n const max = this.backpressureConfig.maxPendingOps;\n return {\n pending,\n max,\n percentage: max > 0 ? pending / max : 0,\n isPaused: this.backpressurePaused,\n strategy: this.backpressureConfig.strategy,\n };\n }\n\n /**\n * Returns true if writes are currently paused due to backpressure.\n */\n public isBackpressurePaused(): boolean {\n return this.backpressurePaused;\n }\n\n /**\n * Subscribe to backpressure events.\n * @param event Event name: 'backpressure:high', 'backpressure:low', 'backpressure:paused', 'backpressure:resumed', 'operation:dropped'\n * @param listener Callback function\n * @returns Unsubscribe function\n */\n public onBackpressure(\n event: 'backpressure:high' | 'backpressure:low' | 'backpressure:paused' | 'backpressure:resumed' | 'operation:dropped',\n listener: (data?: BackpressureThresholdEvent | OperationDroppedEvent) => void\n ): () => void {\n if (!this.backpressureListeners.has(event)) {\n this.backpressureListeners.set(event, new Set());\n }\n this.backpressureListeners.get(event)!.add(listener);\n\n return () => {\n this.backpressureListeners.get(event)?.delete(listener);\n };\n }\n\n /**\n * Emit a backpressure event to all listeners.\n */\n private emitBackpressureEvent(\n event: 'backpressure:high' | 'backpressure:low' | 'backpressure:paused' | 'backpressure:resumed' | 'operation:dropped',\n data?: BackpressureThresholdEvent | OperationDroppedEvent\n ): void {\n const listeners = this.backpressureListeners.get(event);\n if (listeners) {\n for (const listener of listeners) {\n try {\n listener(data);\n } catch (err) {\n logger.error({ err, event }, 'Error in backpressure event listener');\n }\n }\n }\n }\n\n /**\n * Check backpressure before adding a new operation.\n * May pause, throw, or drop depending on strategy.\n */\n private async checkBackpressure(): Promise<void> {\n const pendingCount = this.getPendingOpsCount();\n\n if (pendingCount < this.backpressureConfig.maxPendingOps) {\n return; // Capacity available\n }\n\n switch (this.backpressureConfig.strategy) {\n case 'pause':\n await this.waitForCapacity();\n break;\n case 'throw':\n throw new BackpressureError(\n pendingCount,\n this.backpressureConfig.maxPendingOps\n );\n case 'drop-oldest':\n this.dropOldestOp();\n break;\n }\n }\n\n /**\n * Check high water mark and emit event if threshold reached.\n */\n private checkHighWaterMark(): void {\n const pendingCount = this.getPendingOpsCount();\n const threshold = Math.floor(\n this.backpressureConfig.maxPendingOps * this.backpressureConfig.highWaterMark\n );\n\n if (pendingCount >= threshold && !this.highWaterMarkEmitted) {\n this.highWaterMarkEmitted = true;\n logger.warn(\n { pending: pendingCount, max: this.backpressureConfig.maxPendingOps },\n 'Backpressure high water mark reached'\n );\n this.emitBackpressureEvent('backpressure:high', {\n pending: pendingCount,\n max: this.backpressureConfig.maxPendingOps,\n });\n }\n }\n\n /**\n * Check low water mark and resume paused writes if threshold reached.\n */\n private checkLowWaterMark(): void {\n const pendingCount = this.getPendingOpsCount();\n const lowThreshold = Math.floor(\n this.backpressureConfig.maxPendingOps * this.backpressureConfig.lowWaterMark\n );\n const highThreshold = Math.floor(\n this.backpressureConfig.maxPendingOps * this.backpressureConfig.highWaterMark\n );\n\n // Reset high water mark flag when below high threshold\n if (pendingCount < highThreshold && this.highWaterMarkEmitted) {\n this.highWaterMarkEmitted = false;\n }\n\n // Emit low water mark event when crossing below threshold\n if (pendingCount <= lowThreshold) {\n if (this.backpressurePaused) {\n this.backpressurePaused = false;\n logger.info(\n { pending: pendingCount, max: this.backpressureConfig.maxPendingOps },\n 'Backpressure low water mark reached, resuming writes'\n );\n this.emitBackpressureEvent('backpressure:low', {\n pending: pendingCount,\n max: this.backpressureConfig.maxPendingOps,\n });\n this.emitBackpressureEvent('backpressure:resumed');\n\n // Resume all waiting writes\n const waiting = this.waitingForCapacity;\n this.waitingForCapacity = [];\n for (const resolve of waiting) {\n resolve();\n }\n }\n }\n }\n\n /**\n * Wait for capacity to become available (used by 'pause' strategy).\n */\n private async waitForCapacity(): Promise<void> {\n if (!this.backpressurePaused) {\n this.backpressurePaused = true;\n logger.warn('Backpressure paused - waiting for capacity');\n this.emitBackpressureEvent('backpressure:paused');\n }\n\n return new Promise<void>((resolve) => {\n this.waitingForCapacity.push(resolve);\n });\n }\n\n /**\n * Drop the oldest pending operation (used by 'drop-oldest' strategy).\n */\n private dropOldestOp(): void {\n // Find oldest unsynced operation by array order (oldest first)\n const oldestIndex = this.opLog.findIndex(op => !op.synced);\n\n if (oldestIndex !== -1) {\n const dropped = this.opLog[oldestIndex];\n this.opLog.splice(oldestIndex, 1);\n\n logger.warn(\n { opId: dropped.id, mapName: dropped.mapName, key: dropped.key },\n 'Dropped oldest pending operation due to backpressure'\n );\n\n this.emitBackpressureEvent('operation:dropped', {\n opId: dropped.id,\n mapName: dropped.mapName,\n opType: dropped.opType,\n key: dropped.key,\n });\n }\n }\n}\n","import pino from 'pino';\n\n// Simple check for browser environment\nconst isBrowser = typeof window !== 'undefined';\n\n// In browser, we might not have process.env, so we default to 'info'\n// Users can configure this via window.LOG_LEVEL or similar if needed,\n// but for now we stick to a safe default.\nconst logLevel = (typeof process !== 'undefined' && process.env && process.env.LOG_LEVEL) || 'info';\n\nexport const logger = pino({\n level: logLevel,\n transport: !isBrowser && (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production') ? {\n target: 'pino-pretty',\n options: {\n colorize: true,\n translateTime: 'SYS:standard',\n ignore: 'pid,hostname'\n }\n } : undefined,\n browser: {\n asObject: true\n }\n});\n\nexport type Logger = typeof logger;\n\n","/**\n * Defines the possible states for the SyncEngine connection state machine.\n */\nexport enum SyncState {\n /** Initial state before any connection attempt */\n INITIAL = 'INITIAL',\n /** WebSocket connection is being established */\n CONNECTING = 'CONNECTING',\n /** Connected, waiting for authentication response */\n AUTHENTICATING = 'AUTHENTICATING',\n /** Authenticated, performing initial data sync */\n SYNCING = 'SYNCING',\n /** Fully connected and synchronized */\n CONNECTED = 'CONNECTED',\n /** Intentionally or unexpectedly disconnected */\n DISCONNECTED = 'DISCONNECTED',\n /** Waiting before retry (exponential backoff) */\n BACKOFF = 'BACKOFF',\n /** Fatal error requiring manual intervention or reset */\n ERROR = 'ERROR',\n}\n\n/**\n * Defines valid state transitions for the SyncEngine FSM.\n * Each key is a current state, and the value is an array of valid target states.\n */\nexport const VALID_TRANSITIONS: Record<SyncState, SyncState[]> = {\n [SyncState.INITIAL]: [SyncState.CONNECTING],\n [SyncState.CONNECTING]: [SyncState.AUTHENTICATING, SyncState.BACKOFF, SyncState.ERROR, SyncState.DISCONNECTED],\n [SyncState.AUTHENTICATING]: [SyncState.SYNCING, SyncState.BACKOFF, SyncState.ERROR, SyncState.DISCONNECTED],\n [SyncState.SYNCING]: [SyncState.CONNECTED, SyncState.BACKOFF, SyncState.ERROR, SyncState.DISCONNECTED],\n [SyncState.CONNECTED]: [SyncState.SYNCING, SyncState.DISCONNECTED, SyncState.BACKOFF],\n [SyncState.DISCONNECTED]: [SyncState.CONNECTING, SyncState.BACKOFF, SyncState.INITIAL],\n [SyncState.BACKOFF]: [SyncState.CONNECTING, SyncState.DISCONNECTED, SyncState.INITIAL],\n [SyncState.ERROR]: [SyncState.INITIAL],\n};\n\n/**\n * Helper function to check if a transition is valid\n */\nexport function isValidTransition(from: SyncState, to: SyncState): boolean {\n return VALID_TRANSITIONS[from]?.includes(to) ?? false;\n}\n","import { SyncState, isValidTransition } from './SyncState';\nimport { logger } from './utils/logger';\n\n/**\n * Event emitted when the state machine transitions between states.\n */\nexport interface StateChangeEvent {\n /** The state before the transition */\n from: SyncState;\n /** The state after the transition */\n to: SyncState;\n /** Unix timestamp (ms) when the transition occurred */\n timestamp: number;\n}\n\n/**\n * Listener callback for state change events.\n */\nexport type StateChangeListener = (event: StateChangeEvent) => void;\n\n/**\n * Configuration options for the state machine.\n */\nexport interface SyncStateMachineConfig {\n /** Maximum number of state transitions to keep in history (default: 50) */\n maxHistorySize?: number;\n}\n\nconst DEFAULT_MAX_HISTORY_SIZE = 50;\n\n/**\n * A finite state machine for managing SyncEngine connection states.\n *\n * Features:\n * - Validates all state transitions against allowed paths\n * - Emits events on state changes for observability\n * - Maintains a history of transitions for debugging\n * - Logs invalid transition attempts (graceful degradation)\n */\nexport class SyncStateMachine {\n private state: SyncState = SyncState.INITIAL;\n private readonly listeners: Set<StateChangeListener> = new Set();\n private history: StateChangeEvent[] = [];\n private readonly maxHistorySize: number;\n\n constructor(config: SyncStateMachineConfig = {}) {\n this.maxHistorySize = config.maxHistorySize ?? DEFAULT_MAX_HISTORY_SIZE;\n }\n\n /**\n * Attempt to transition to a new state.\n * @param to The target state\n * @returns true if the transition was valid and executed, false otherwise\n */\n transition(to: SyncState): boolean {\n const from = this.state;\n\n if (from === to) {\n // No-op: already in target state\n return true;\n }\n\n if (!isValidTransition(from, to)) {\n logger.warn(\n { from, to, currentHistory: this.getHistory(5) },\n `Invalid state transition attempted: ${from} → ${to}`\n );\n return false;\n }\n\n // Execute the transition\n this.state = to;\n\n const event: StateChangeEvent = {\n from,\n to,\n timestamp: Date.now(),\n };\n\n // Add to history\n this.history.push(event);\n if (this.history.length > this.maxHistorySize) {\n this.history.shift();\n }\n\n // Notify listeners\n for (const listener of this.listeners) {\n try {\n listener(event);\n } catch (err) {\n logger.error({ err, event }, 'State change listener threw an error');\n }\n }\n\n logger.debug({ from, to }, `State transition: ${from} → ${to}`);\n\n return true;\n }\n\n /**\n * Get the current state.\n */\n getState(): SyncState {\n return this.state;\n }\n\n /**\n * Check if a transition from the current state to the target state is valid.\n * @param to The target state to check\n */\n canTransition(to: SyncState): boolean {\n return this.state === to || isValidTransition(this.state, to);\n }\n\n /**\n * Subscribe to state change events.\n * @param listener Callback function to be called on each state change\n * @returns An unsubscribe function\n */\n onStateChange(listener: StateChangeListener): () => void {\n this.listeners.add(listener);\n return () => {\n this.listeners.delete(listener);\n };\n }\n\n /**\n * Get the state transition history.\n * @param limit Maximum number of entries to return (default: all)\n * @returns Array of state change events, oldest first\n */\n getHistory(limit?: number): StateChangeEvent[] {\n if (limit === undefined || limit >= this.history.length) {\n return [...this.history];\n }\n return this.history.slice(-limit);\n }\n\n /**\n * Reset the state machine to INITIAL state.\n * This is a forced reset that bypasses normal transition validation.\n * Use for testing or hard resets after fatal errors.\n * @param clearHistory If true, also clears the transition history (default: true)\n */\n reset(clearHistory = true): void {\n const from = this.state;\n this.state = SyncState.INITIAL;\n\n if (clearHistory) {\n this.history = [];\n } else {\n // Record the reset as a transition\n const event: StateChangeEvent = {\n from,\n to: SyncState.INITIAL,\n timestamp: Date.now(),\n };\n this.history.push(event);\n if (this.history.length > this.maxHistorySize) {\n this.history.shift();\n }\n\n // Notify listeners\n for (const listener of this.listeners) {\n try {\n listener(event);\n } catch (err) {\n logger.error({ err, event }, 'State change listener threw an error during reset');\n }\n }\n }\n\n logger.info({ from }, 'State machine reset to INITIAL');\n }\n\n /**\n * Check if the state machine is in a \"connected\" state\n * (either SYNCING or CONNECTED)\n */\n isConnected(): boolean {\n return this.state === SyncState.CONNECTED || this.state === SyncState.SYNCING;\n }\n\n /**\n * Check if the state machine is in a state where operations can be sent\n * (authenticated and connected)\n */\n isReady(): boolean {\n return this.state === SyncState.CONNECTED;\n }\n\n /**\n * Check if the state machine is currently attempting to connect\n */\n isConnecting(): boolean {\n return (\n this.state === SyncState.CONNECTING ||\n this.state === SyncState.AUTHENTICATING ||\n this.state === SyncState.SYNCING\n );\n }\n}\n","/**\n * Error thrown when backpressure limit is reached and strategy is 'throw'.\n */\nexport class BackpressureError extends Error {\n public readonly name = 'BackpressureError';\n\n constructor(\n public readonly pendingCount: number,\n public readonly maxPending: number\n ) {\n super(\n `Backpressure limit reached: ${pendingCount}/${maxPending} pending operations. ` +\n `Wait for acknowledgments or increase maxPendingOps.`\n );\n\n // Maintains proper stack trace for where error was thrown (only available on V8)\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, BackpressureError);\n }\n }\n}\n","/**\n * Backpressure strategy when maxPendingOps is reached.\n */\nexport type BackpressureStrategy = 'pause' | 'throw' | 'drop-oldest';\n\n/**\n * Configuration for backpressure control on SyncEngine.\n */\nexport interface BackpressureConfig {\n /**\n * Maximum number of operations waiting for server acknowledgment.\n * When this limit is reached, the configured strategy will be applied.\n * @default 1000\n */\n maxPendingOps: number;\n\n /**\n * Strategy when maxPendingOps is reached:\n * - 'pause': Wait for capacity (returns Promise that resolves when space available)\n * - 'throw': Throw BackpressureError immediately\n * - 'drop-oldest': Remove oldest pending op to make room (data loss!)\n * @default 'pause'\n */\n strategy: BackpressureStrategy;\n\n /**\n * High water mark (percentage of maxPendingOps).\n * Emit 'backpressure:high' event when reached.\n * Value should be between 0 and 1.\n * @default 0.8 (80%)\n */\n highWaterMark: number;\n\n /**\n * Low water mark (percentage of maxPendingOps).\n * Resume paused writes and emit 'backpressure:low' when pending ops drop below this.\n * Value should be between 0 and 1.\n * @default 0.5 (50%)\n */\n lowWaterMark: number;\n}\n\n/**\n * Default backpressure configuration.\n */\nexport const DEFAULT_BACKPRESSURE_CONFIG: BackpressureConfig = {\n maxPendingOps: 1000,\n strategy: 'pause',\n highWaterMark: 0.8,\n lowWaterMark: 0.5,\n};\n\n/**\n * Status of backpressure mechanism.\n */\nexport interface BackpressureStatus {\n /** Current number of pending (unacknowledged) operations */\n pending: number;\n /** Maximum allowed pending operations */\n max: number;\n /** Percentage of capacity used (0-1) */\n percentage: number;\n /** Whether writes are currently paused due to backpressure */\n isPaused: boolean;\n /** Current backpressure strategy */\n strategy: BackpressureStrategy;\n}\n\n/**\n * Event data for backpressure:high and backpressure:low events.\n */\nexport interface BackpressureThresholdEvent {\n pending: number;\n max: number;\n}\n\n/**\n * Event data for operation:dropped event.\n */\nexport interface OperationDroppedEvent {\n opId: string;\n mapName: string;\n opType: string;\n key: string;\n}\n","import { LWWMap, ORMap } from '@topgunbuild/core';\nimport type { ORMapRecord, LWWRecord } from '@topgunbuild/core';\nimport type { IStorageAdapter } from './IStorageAdapter';\nimport { SyncEngine } from './SyncEngine';\nimport type { BackoffConfig } from './SyncEngine';\nimport { QueryHandle } from './QueryHandle';\nimport type { QueryFilter } from './QueryHandle';\nimport { DistributedLock } from './DistributedLock';\nimport { TopicHandle } from './TopicHandle';\nimport { logger } from './utils/logger';\nimport { SyncState } from './SyncState';\nimport type { StateChangeEvent } from './SyncStateMachine';\nimport type {\n BackpressureConfig,\n BackpressureStatus,\n BackpressureThresholdEvent,\n OperationDroppedEvent,\n} from './BackpressureConfig';\n\nexport class TopGunClient {\n private readonly nodeId: string;\n private readonly syncEngine: SyncEngine;\n private readonly maps: Map<string, LWWMap<any, any> | ORMap<any, any>> = new Map();\n private readonly storageAdapter: IStorageAdapter;\n private readonly topicHandles: Map<string, TopicHandle> = new Map();\n\n constructor(config: {\n nodeId?: string;\n serverUrl: string;\n storage: IStorageAdapter;\n backoff?: Partial<BackoffConfig>;\n backpressure?: Partial<BackpressureConfig>;\n }) {\n this.nodeId = config.nodeId || crypto.randomUUID();\n this.storageAdapter = config.storage;\n\n const syncEngineConfig = {\n nodeId: this.nodeId,\n serverUrl: config.serverUrl,\n storageAdapter: this.storageAdapter,\n backoff: config.backoff,\n backpressure: config.backpressure,\n };\n this.syncEngine = new SyncEngine(syncEngineConfig);\n }\n\n public async start(): Promise<void> {\n await this.storageAdapter.initialize('topgun_offline_db');\n // this.syncEngine.start();\n }\n\n public setAuthToken(token: string): void {\n this.syncEngine.setAuthToken(token);\n }\n\n public setAuthTokenProvider(provider: () => Promise<string | null>): void {\n this.syncEngine.setTokenProvider(provider);\n }\n\n /**\n * Creates a live query subscription for a map.\n */\n public query<T>(mapName: string, filter: QueryFilter): QueryHandle<T> {\n return new QueryHandle<T>(this.syncEngine, mapName, filter);\n }\n\n /**\n * Retrieves a distributed lock instance.\n * @param name The name of the lock.\n */\n public getLock(name: string): DistributedLock {\n return new DistributedLock(this.syncEngine, name);\n }\n\n /**\n * Retrieves a topic handle for Pub/Sub messaging.\n * @param name The name of the topic.\n */\n public topic(name: string): TopicHandle {\n if (!this.topicHandles.has(name)) {\n this.topicHandles.set(name, new TopicHandle(this.syncEngine, name));\n }\n return this.topicHandles.get(name)!;\n }\n\n /**\n * Retrieves an LWWMap instance. If the map doesn't exist locally, it's created.\n * @param name The name of the map.\n * @returns An LWWMap instance.\n */\n public getMap<K, V>(name: string): LWWMap<K, V> {\n if (this.maps.has(name)) {\n const map = this.maps.get(name);\n if (map instanceof LWWMap) {\n return map as LWWMap<K, V>;\n }\n throw new Error(`Map ${name} exists but is not an LWWMap`);\n }\n\n const lwwMap = new LWWMap<K, V>(this.syncEngine.getHLC());\n this.maps.set(name, lwwMap);\n this.syncEngine.registerMap(name, lwwMap);\n\n // Restore state from storage asynchronously\n this.storageAdapter.getAllKeys().then(async (keys) => {\n const mapPrefix = `${name}:`;\n for (const fullKey of keys) {\n if (fullKey.startsWith(mapPrefix)) {\n const record = await this.storageAdapter.get(fullKey);\n if (record && (record as LWWRecord<V>).timestamp && !(record as any).tag) {\n // Strip prefix to get actual key\n const key = fullKey.substring(mapPrefix.length) as unknown as K;\n // Merge into in-memory map without triggering new ops\n lwwMap.merge(key, record as LWWRecord<V>);\n }\n }\n }\n }).catch(err => logger.error({ err }, 'Failed to restore keys from storage'));\n\n // Wrap LWWMap with IMap interface logic\n const originalSet = lwwMap.set.bind(lwwMap);\n lwwMap.set = (key: K, value: V, ttlMs?: number) => {\n const record = originalSet(key, value, ttlMs);\n this.storageAdapter.put(`${name}:${key}`, record).catch(err => logger.error({ err }, 'Failed to put record to storage'));\n this.syncEngine.recordOperation(name, 'PUT', String(key), { record, timestamp: record.timestamp }).catch(err => logger.error({ err }, 'Failed to record PUT op'));\n return record;\n };\n\n const originalRemove = lwwMap.remove.bind(lwwMap);\n lwwMap.remove = (key: K) => {\n const tombstone = originalRemove(key);\n this.storageAdapter.put(`${name}:${key}`, tombstone).catch(err => logger.error({ err }, 'Failed to put tombstone to storage'));\n this.syncEngine.recordOperation(name, 'REMOVE', String(key), { record: tombstone, timestamp: tombstone.timestamp }).catch(err => logger.error({ err }, 'Failed to record REMOVE op'));\n return tombstone;\n };\n\n return lwwMap;\n }\n\n /**\n * Retrieves an ORMap instance. If the map doesn't exist locally, it's created.\n * @param name The name of the map.\n * @returns An ORMap instance.\n */\n public getORMap<K, V>(name: string): ORMap<K, V> {\n if (this.maps.has(name)) {\n const map = this.maps.get(name);\n if (map instanceof ORMap) {\n return map as ORMap<K, V>;\n }\n throw new Error(`Map ${name} exists but is not an ORMap`);\n }\n\n const orMap = new ORMap<K, V>(this.syncEngine.getHLC());\n this.maps.set(name, orMap);\n this.syncEngine.registerMap(name, orMap);\n\n // Restore state from storage\n this.restoreORMap(name, orMap);\n\n // Wrap ORMap methods to record operations\n const originalAdd = orMap.add.bind(orMap);\n orMap.add = (key: K, value: V, ttlMs?: number) => {\n const record = originalAdd(key, value, ttlMs);\n \n // Persist records\n this.persistORMapKey(name, orMap, key);\n\n this.syncEngine.recordOperation(name, 'OR_ADD', String(key), { orRecord: record, timestamp: record.timestamp }).catch(err => logger.error({ err }, 'Failed to record OR_ADD op'));\n return record;\n };\n\n const originalRemove = orMap.remove.bind(orMap);\n orMap.remove = (key: K, value: V) => {\n const tombstones = originalRemove(key, value);\n const timestamp = this.syncEngine.getHLC().now(); \n \n // Update storage for the key (items removed)\n this.persistORMapKey(name, orMap, key);\n // Update storage for tombstones\n this.persistORMapTombstones(name, orMap);\n\n for (const tag of tombstones) {\n this.syncEngine.recordOperation(name, 'OR_REMOVE', String(key), { orTag: tag, timestamp }).catch(err => logger.error({ err }, 'Failed to record OR_REMOVE op'));\n }\n return tombstones;\n };\n\n return orMap;\n }\n\n private async restoreORMap<K, V>(name: string, orMap: ORMap<K, V>) {\n try {\n // 1. Restore Tombstones\n const tombstoneKey = `__sys__:${name}:tombstones`;\n const tombstones = await this.storageAdapter.getMeta(tombstoneKey);\n if (Array.isArray(tombstones)) {\n for (const tag of tombstones) {\n orMap.applyTombstone(tag);\n }\n }\n\n // 2. Restore Items\n const keys = await this.storageAdapter.getAllKeys();\n const mapPrefix = `${name}:`;\n for (const fullKey of keys) {\n if (fullKey.startsWith(mapPrefix)) {\n const keyPart = fullKey.substring(mapPrefix.length);\n \n const data = await this.storageAdapter.get(fullKey);\n if (Array.isArray(data)) {\n // It's likely an ORMap value list (Array of ORMapRecord)\n const records = data as ORMapRecord<V>[];\n const key = keyPart as unknown as K;\n \n for (const record of records) {\n orMap.apply(key, record);\n }\n }\n }\n }\n } catch (e) {\n logger.error({ mapName: name, err: e }, 'Failed to restore ORMap');\n }\n }\n\n private async persistORMapKey<K, V>(mapName: string, orMap: ORMap<K, V>, key: K) {\n const records = orMap.getRecords(key);\n if (records.length > 0) {\n await this.storageAdapter.put(`${mapName}:${key}`, records);\n } else {\n await this.storageAdapter.remove(`${mapName}:${key}`);\n }\n }\n \n private async persistORMapTombstones<K, V>(mapName: string, orMap: ORMap<K, V>) {\n const tombstoneKey = `__sys__:${mapName}:tombstones`;\n const tombstones = orMap.getTombstones();\n await this.storageAdapter.setMeta(tombstoneKey, tombstones);\n }\n\n /**\n * Closes the client, disconnecting from the server and cleaning up resources.\n */\n public close(): void {\n this.syncEngine.close();\n }\n\n // ============================================\n // Connection State API\n // ============================================\n\n /**\n * Get the current connection state\n */\n public getConnectionState(): SyncState {\n return this.syncEngine.getConnectionState();\n }\n\n /**\n * Subscribe to connection state changes\n * @param listener Callback function called on each state change\n * @returns Unsubscribe function\n */\n public onConnectionStateChange(listener: (event: StateChangeEvent) => void): () => void {\n return this.syncEngine.onConnectionStateChange(listener);\n }\n\n /**\n * Get state machine history for debugging\n * @param limit Maximum number of entries to return\n */\n public getStateHistory(limit?: number): StateChangeEvent[] {\n return this.syncEngine.getStateHistory(limit);\n }\n\n /**\n * Reset the connection and state machine.\n * Use after fatal errors to start fresh.\n */\n public resetConnection(): void {\n this.syncEngine.resetConnection();\n }\n\n // ============================================\n // Backpressure API\n // ============================================\n\n /**\n * Get the current number of pending (unacknowledged) operations.\n */\n public getPendingOpsCount(): number {\n return this.syncEngine.getPendingOpsCount();\n }\n\n /**\n * Get the current backpressure status.\n */\n public getBackpressureStatus(): BackpressureStatus {\n return this.syncEngine.getBackpressureStatus();\n }\n\n /**\n * Returns true if writes are currently paused due to backpressure.\n */\n public isBackpressurePaused(): boolean {\n return this.syncEngine.isBackpressurePaused();\n }\n\n /**\n * Subscribe to backpressure events.\n *\n * Available events:\n * - 'backpressure:high': Emitted when pending ops reach high water mark\n * - 'backpressure:low': Emitted when pending ops drop below low water mark\n * - 'backpressure:paused': Emitted when writes are paused (pause strategy)\n * - 'backpressure:resumed': Emitted when writes resume after being paused\n * - 'operation:dropped': Emitted when an operation is dropped (drop-oldest strategy)\n *\n * @param event Event name\n * @param listener Callback function\n * @returns Unsubscribe function\n *\n * @example\n * ```typescript\n * client.onBackpressure('backpressure:high', ({ pending, max }) => {\n * console.warn(`Warning: ${pending}/${max} pending ops`);\n * });\n *\n * client.onBackpressure('backpressure:paused', () => {\n * showLoadingSpinner();\n * });\n *\n * client.onBackpressure('backpressure:resumed', () => {\n * hideLoadingSpinner();\n * });\n * ```\n */\n public onBackpressure(\n event: 'backpressure:high' | 'backpressure:low' | 'backpressure:paused' | 'backpressure:resumed' | 'operation:dropped',\n listener: (data?: BackpressureThresholdEvent | OperationDroppedEvent) => void\n ): () => void {\n return this.syncEngine.onBackpressure(event, listener);\n }\n}\n","import { SyncEngine } from './SyncEngine';\nimport type { PredicateNode } from '@topgunbuild/core';\n\nexport interface QueryFilter {\n where?: Record<string, any>;\n predicate?: PredicateNode;\n sort?: Record<string, 'asc' | 'desc'>;\n limit?: number;\n offset?: number;\n}\n\n/** Source of query results for proper handling of race conditions */\nexport type QueryResultSource = 'local' | 'server';\n\n/** Result item with _key field for client-side lookups */\nexport type QueryResultItem<T> = T & { _key: string };\n\nexport class QueryHandle<T> {\n public readonly id: string;\n private syncEngine: SyncEngine;\n private mapName: string;\n private filter: QueryFilter;\n private listeners: Set<(results: QueryResultItem<T>[]) => void> = new Set();\n private currentResults: Map<string, T> = new Map();\n\n constructor(syncEngine: SyncEngine, mapName: string, filter: QueryFilter = {}) {\n this.id = crypto.randomUUID();\n this.syncEngine = syncEngine;\n this.mapName = mapName;\n this.filter = filter;\n }\n\n public subscribe(callback: (results: QueryResultItem<T>[]) => void): () => void {\n this.listeners.add(callback);\n \n // If this is the first listener, activate subscription\n if (this.listeners.size === 1) {\n this.syncEngine.subscribeToQuery(this);\n } else {\n // Immediately invoke with cached results\n callback(this.getSortedResults());\n }\n \n // [FIX]: Attempt to load local results immediately if available\n // This ensures that if data is already in storage but sync hasn't happened,\n // we still show something.\n this.loadInitialLocalData().then(data => {\n // If we haven't received server results yet (currentResults empty),\n // and we have local data OR it's just the initial load, we should notify.\n // Even if data is empty, we might want to tell the subscriber \"nothing here yet\".\n if (this.currentResults.size === 0) {\n this.onResult(data, 'local');\n }\n });\n\n return () => {\n this.listeners.delete(callback);\n if (this.listeners.size === 0) {\n this.syncEngine.unsubscribeFromQuery(this.id);\n }\n };\n }\n\n private async loadInitialLocalData() {\n // This requires SyncEngine to expose a method to query local storage\n // For now, we can't easily reach storageAdapter directly from here without leaking abstraction.\n // A better approach is for SyncEngine.subscribeToQuery to trigger a local load.\n return this.syncEngine.runLocalQuery(this.mapName, this.filter);\n }\n\n // Track if we've received authoritative server response\n private hasReceivedServerData: boolean = false;\n\n /**\n * Called by SyncEngine when server sends initial results or by local storage load.\n * Uses merge strategy instead of clear to prevent UI flickering.\n *\n * @param items - Array of key-value pairs\n * @param source - 'local' for IndexedDB data, 'server' for QUERY_RESP from server\n *\n * Race condition protection:\n * - Empty server responses are ignored until we receive non-empty server data\n * - This prevents clearing local data when server hasn't loaded from storage yet\n * - Works with any async storage adapter (PostgreSQL, SQLite, Redis, etc.)\n */\n public onResult(items: { key: string, value: T }[], source: QueryResultSource = 'server') {\n console.log(`[QueryHandle:${this.mapName}] onResult called with ${items.length} items`, {\n source,\n currentResultsCount: this.currentResults.size,\n newItemKeys: items.map(i => i.key),\n hasReceivedServerData: this.hasReceivedServerData\n });\n\n // [FIX] Race condition protection for any async storage adapter:\n // If server sends empty QUERY_RESP before loading data from storage,\n // we ignore it to prevent clearing valid local data.\n // This is safe because:\n // 1. If server truly has no data, next non-empty response will clear local-only items\n // 2. If server is still loading, we preserve local data until real data arrives\n if (source === 'server' && items.length === 0 && !this.hasReceivedServerData) {\n console.log(`[QueryHandle:${this.mapName}] Ignoring empty server response - waiting for authoritative data`);\n return;\n }\n\n // Mark that we've received authoritative server data (non-empty from server)\n if (source === 'server' && items.length > 0) {\n this.hasReceivedServerData = true;\n }\n\n const newKeys = new Set(items.map(i => i.key));\n\n // Remove only keys that are not in the new results\n const removedKeys: string[] = [];\n for (const key of this.currentResults.keys()) {\n if (!newKeys.has(key)) {\n removedKeys.push(key);\n this.currentResults.delete(key);\n }\n }\n if (removedKeys.length > 0) {\n console.log(`[QueryHandle:${this.mapName}] Removed ${removedKeys.length} keys:`, removedKeys);\n }\n\n // Add/update new results\n for (const item of items) {\n this.currentResults.set(item.key, item.value);\n }\n console.log(`[QueryHandle:${this.mapName}] After merge: ${this.currentResults.size} results`);\n this.notify();\n }\n\n /**\n * Called by SyncEngine when server sends a live update\n */\n public onUpdate(key: string, value: T | null) {\n if (value === null) {\n this.currentResults.delete(key);\n } else {\n this.currentResults.set(key, value);\n }\n this.notify();\n }\n\n private notify() {\n const results = this.getSortedResults();\n for (const listener of this.listeners) {\n listener(results);\n }\n }\n\n private getSortedResults(): (T & { _key: string })[] {\n // Include _key in each result for client-side matching/lookup\n const results = Array.from(this.currentResults.entries()).map(\n ([key, value]) => ({ ...(value as object), _key: key } as T & { _key: string })\n );\n\n if (this.filter.sort) {\n results.sort((a: any, b: any) => {\n for (const [field, direction] of Object.entries(this.filter.sort!)) {\n const valA = a[field];\n const valB = b[field];\n\n if (valA < valB) return direction === 'asc' ? -1 : 1;\n if (valA > valB) return direction === 'asc' ? 1 : -1;\n }\n return 0;\n });\n }\n\n return results;\n }\n\n public getFilter(): QueryFilter {\n return this.filter;\n }\n\n public getMapName(): string {\n return this.mapName;\n }\n}\n","import { SyncEngine } from './SyncEngine';\n\nexport interface ILock {\n lock(ttl?: number): Promise<boolean>;\n unlock(): Promise<void>;\n isLocked(): boolean;\n}\n\nexport class DistributedLock implements ILock {\n private syncEngine: SyncEngine;\n private name: string;\n private fencingToken: number | null = null;\n private _isLocked: boolean = false;\n\n constructor(syncEngine: SyncEngine, name: string) {\n this.syncEngine = syncEngine;\n this.name = name;\n }\n\n public async lock(ttl: number = 10000): Promise<boolean> {\n const requestId = crypto.randomUUID();\n try {\n const result = await this.syncEngine.requestLock(this.name, requestId, ttl);\n this.fencingToken = result.fencingToken;\n this._isLocked = true;\n return true;\n } catch (e) {\n return false;\n }\n }\n\n public async unlock(): Promise<void> {\n if (!this._isLocked || this.fencingToken === null) return;\n \n const requestId = crypto.randomUUID();\n try {\n await this.syncEngine.releaseLock(this.name, requestId, this.fencingToken);\n } finally {\n this._isLocked = false;\n this.fencingToken = null;\n }\n }\n\n public isLocked(): boolean {\n return this._isLocked;\n }\n}\n\n","import { SyncEngine } from './SyncEngine';\n\nexport type TopicCallback = (data: any, context: { timestamp: number; publisherId?: string }) => void;\n\nexport class TopicHandle {\n private engine: SyncEngine;\n private topic: string;\n private listeners: Set<TopicCallback> = new Set();\n\n constructor(engine: SyncEngine, topic: string) {\n this.engine = engine;\n this.topic = topic;\n }\n\n public get id(): string {\n return this.topic;\n }\n\n /**\n * Publish a message to the topic\n */\n public publish(data: any) {\n this.engine.publishTopic(this.topic, data);\n }\n\n /**\n * Subscribe to the topic\n */\n public subscribe(callback: TopicCallback) {\n if (this.listeners.size === 0) {\n this.engine.subscribeToTopic(this.topic, this);\n }\n this.listeners.add(callback);\n return () => this.unsubscribe(callback);\n }\n\n private unsubscribe(callback: TopicCallback) {\n this.listeners.delete(callback);\n if (this.listeners.size === 0) {\n this.engine.unsubscribeFromTopic(this.topic);\n }\n }\n\n /**\n * Called by SyncEngine when a message is received\n */\n public onMessage(data: any, context: { timestamp: number; publisherId?: string }) {\n this.listeners.forEach(cb => {\n try {\n cb(data, context);\n } catch (e) {\n console.error('Error in topic listener', e);\n }\n });\n }\n}\n\n","import type { LWWRecord, ORMapRecord } from '@topgunbuild/core';\nimport type { IStorageAdapter, OpLogEntry } from '../IStorageAdapter';\nimport { openDB } from 'idb';\nimport type { IDBPDatabase } from 'idb';\n\n/**\n * Represents an operation queued before IndexedDB is ready.\n */\ninterface QueuedOperation {\n type: 'put' | 'remove' | 'setMeta' | 'appendOpLog' | 'markOpsSynced' | 'batchPut';\n args: any[];\n resolve: (value: any) => void;\n reject: (error: any) => void;\n}\n\n/**\n * Non-blocking IndexedDB adapter that allows immediate use before initialization completes.\n *\n * Operations are queued in memory and replayed once IndexedDB is ready.\n * This enables true \"memory-first\" behavior where the UI can render immediately\n * without waiting for IndexedDB to initialize (which can take 50-500ms).\n */\nexport class IDBAdapter implements IStorageAdapter {\n private dbPromise?: Promise<IDBPDatabase>;\n private db?: IDBPDatabase;\n private isReady = false;\n private operationQueue: QueuedOperation[] = [];\n private initPromise?: Promise<void>;\n\n /**\n * Initializes IndexedDB in the background.\n * Returns immediately - does NOT block on IndexedDB being ready.\n * Use waitForReady() if you need to ensure initialization is complete.\n */\n async initialize(dbName: string): Promise<void> {\n // Start initialization but don't await it\n this.initPromise = this.initializeInternal(dbName);\n // Return immediately - non-blocking!\n }\n\n /**\n * Internal initialization that actually opens IndexedDB.\n */\n private async initializeInternal(dbName: string): Promise<void> {\n try {\n this.dbPromise = openDB(dbName, 2, {\n upgrade(db) {\n if (!db.objectStoreNames.contains('kv_store')) {\n db.createObjectStore('kv_store', { keyPath: 'key' });\n }\n if (!db.objectStoreNames.contains('op_log')) {\n db.createObjectStore('op_log', { keyPath: 'id', autoIncrement: true });\n }\n if (!db.objectStoreNames.contains('meta_store')) {\n db.createObjectStore('meta_store', { keyPath: 'key' });\n }\n },\n });\n\n this.db = await this.dbPromise;\n this.isReady = true;\n\n // Replay queued operations\n await this.flushQueue();\n } catch (error) {\n // Re-throw to allow error handling\n throw error;\n }\n }\n\n /**\n * Waits for IndexedDB to be fully initialized.\n * Call this if you need guaranteed persistence before proceeding.\n */\n async waitForReady(): Promise<void> {\n if (this.isReady) return;\n if (this.initPromise) {\n await this.initPromise;\n }\n }\n\n /**\n * Flushes all queued operations once IndexedDB is ready.\n */\n private async flushQueue(): Promise<void> {\n const queue = this.operationQueue;\n this.operationQueue = [];\n\n for (const op of queue) {\n try {\n let result: any;\n switch (op.type) {\n case 'put':\n result = await this.putInternal(op.args[0], op.args[1]);\n break;\n case 'remove':\n result = await this.removeInternal(op.args[0]);\n break;\n case 'setMeta':\n result = await this.setMetaInternal(op.args[0], op.args[1]);\n break;\n case 'appendOpLog':\n result = await this.appendOpLogInternal(op.args[0]);\n break;\n case 'markOpsSynced':\n result = await this.markOpsSyncedInternal(op.args[0]);\n break;\n case 'batchPut':\n result = await this.batchPutInternal(op.args[0]);\n break;\n }\n op.resolve(result);\n } catch (error) {\n op.reject(error);\n }\n }\n }\n\n /**\n * Queues an operation if not ready, or executes immediately if ready.\n */\n private queueOrExecute<T>(\n type: QueuedOperation['type'],\n args: any[],\n executor: () => Promise<T>\n ): Promise<T> {\n if (this.isReady) {\n return executor();\n }\n\n return new Promise<T>((resolve, reject) => {\n this.operationQueue.push({ type, args, resolve, reject });\n });\n }\n\n async close(): Promise<void> {\n if (this.db) {\n this.db.close();\n }\n }\n\n // ============================================\n // Read Operations - Wait for ready\n // ============================================\n\n async get<V>(key: string): Promise<LWWRecord<V> | ORMapRecord<V>[] | any | undefined> {\n // Read operations must wait for DB to be ready\n await this.waitForReady();\n const result = await this.db?.get('kv_store', key);\n return result?.value;\n }\n\n async getMeta(key: string): Promise<any> {\n await this.waitForReady();\n const result = await this.db?.get('meta_store', key);\n return result?.value;\n }\n\n async getPendingOps(): Promise<OpLogEntry[]> {\n await this.waitForReady();\n const all = await this.db?.getAll('op_log');\n return all?.filter((op: any) => op.synced === 0) || [];\n }\n\n async getAllKeys(): Promise<string[]> {\n await this.waitForReady();\n return (await this.db?.getAllKeys('kv_store')) as string[] || [];\n }\n\n // ============================================\n // Write Operations - Queue if not ready\n // ============================================\n\n async put(key: string, value: any): Promise<void> {\n return this.queueOrExecute('put', [key, value], () => this.putInternal(key, value));\n }\n\n private async putInternal(key: string, value: any): Promise<void> {\n await this.db?.put('kv_store', { key, value });\n }\n\n async remove(key: string): Promise<void> {\n return this.queueOrExecute('remove', [key], () => this.removeInternal(key));\n }\n\n private async removeInternal(key: string): Promise<void> {\n await this.db?.delete('kv_store', key);\n }\n\n async setMeta(key: string, value: any): Promise<void> {\n return this.queueOrExecute('setMeta', [key, value], () => this.setMetaInternal(key, value));\n }\n\n private async setMetaInternal(key: string, value: any): Promise<void> {\n await this.db?.put('meta_store', { key, value });\n }\n\n async batchPut(entries: Map<string, any>): Promise<void> {\n return this.queueOrExecute('batchPut', [entries], () => this.batchPutInternal(entries));\n }\n\n private async batchPutInternal(entries: Map<string, any>): Promise<void> {\n const tx = this.db?.transaction('kv_store', 'readwrite');\n if (!tx) return;\n\n await Promise.all(\n Array.from(entries.entries()).map(([key, value]) =>\n tx.store.put({ key, value })\n )\n );\n await tx.done;\n }\n\n async appendOpLog(entry: any): Promise<number> {\n return this.queueOrExecute('appendOpLog', [entry], () => this.appendOpLogInternal(entry));\n }\n\n private async appendOpLogInternal(entry: any): Promise<number> {\n const entryToSave = { ...entry, synced: 0 };\n return await this.db?.add('op_log', entryToSave) as number;\n }\n\n async markOpsSynced(lastId: number): Promise<void> {\n return this.queueOrExecute('markOpsSynced', [lastId], () => this.markOpsSyncedInternal(lastId));\n }\n\n private async markOpsSyncedInternal(lastId: number): Promise<void> {\n const tx = this.db?.transaction('op_log', 'readwrite');\n if (!tx) return;\n\n let cursor = await tx.store.openCursor();\n while (cursor) {\n if (cursor.value.id <= lastId) {\n const update = { ...cursor.value, synced: 1 };\n await cursor.update(update);\n }\n cursor = await cursor.continue();\n }\n await tx.done;\n }\n}\n\n","import { TopGunClient } from './TopGunClient';\nimport { IDBAdapter } from './adapters/IDBAdapter';\nimport type { IStorageAdapter } from './IStorageAdapter';\nimport { LWWMap } from '@topgunbuild/core';\nimport type { LWWRecord } from '@topgunbuild/core';\n\nexport interface TopGunConfig {\n sync: string;\n persist: 'indexeddb' | IStorageAdapter;\n nodeId?: string;\n}\n\n// Generic schema type\nexport type TopGunSchema = Record<string, any>;\n\nconst handler: ProxyHandler<TopGun<any>> = {\n get(target, prop, receiver) {\n if (prop in target || typeof prop === 'symbol') {\n return Reflect.get(target, prop, receiver);\n }\n if (typeof prop === 'string') {\n return target.collection(prop);\n }\n return undefined;\n }\n};\n\nexport class TopGun<T extends TopGunSchema = any> {\n private client: TopGunClient;\n private initPromise: Promise<void>;\n \n // Allow property access for collections based on Schema T\n [key: string]: any;\n\n constructor(config: TopGunConfig) {\n let storage: IStorageAdapter;\n\n if (config.persist === 'indexeddb') {\n storage = new IDBAdapter();\n } else if (typeof config.persist === 'object') {\n storage = config.persist;\n } else {\n throw new Error(`Unsupported persist option: ${config.persist}`);\n }\n\n this.client = new TopGunClient({\n serverUrl: config.sync,\n storage,\n nodeId: config.nodeId\n });\n\n // Start client initialization (non-blocking)\n // The IDBAdapter now initializes in the background and queues operations\n this.initPromise = this.client.start().catch(err => {\n console.error('Failed to start TopGun client:', err);\n throw err;\n });\n\n return new Proxy(this, handler);\n }\n\n /**\n * Waits for the storage adapter to be fully initialized.\n * This is optional - you can start using the database immediately.\n * Operations are queued in memory and persisted once IndexedDB is ready.\n */\n public async waitForReady(): Promise<void> {\n await this.initPromise;\n }\n\n public collection<K extends keyof T & string>(name: K): CollectionWrapper<T[K]> {\n // Explicitly type the map\n const map = this.client.getMap<string, T[K]>(name);\n return new CollectionWrapper<T[K]>(map);\n }\n}\n\nexport class CollectionWrapper<ItemType = any> {\n private map: LWWMap<string, ItemType>;\n\n constructor(map: LWWMap<string, ItemType>) {\n this.map = map;\n }\n\n /**\n * Sets an item in the collection. \n * The item MUST have an 'id' or '_id' field.\n */\n async set(value: ItemType): Promise<ItemType> {\n const v = value as any;\n const key = v.id || v._id;\n if (!key) {\n throw new Error('Object must have an \"id\" or \"_id\" property to be saved in a collection.');\n }\n \n // LWWMap.set is synchronous in updating memory and queueing ops,\n // but we return a Promise to match typical async DB APIs.\n this.map.set(key, value);\n return Promise.resolve(value); \n }\n \n /**\n * Retrieves an item by ID.\n * Returns the value directly (unwrapped from CRDT record).\n */\n get(key: string): ItemType | undefined {\n return this.map.get(key);\n }\n\n /**\n * Get the raw LWWRecord (including metadata like timestamp).\n */\n getRecord(key: string): LWWRecord<ItemType> | undefined {\n return this.map.getRecord(key);\n }\n\n // Expose raw map if needed for advanced usage\n get raw() {\n return this.map;\n }\n}\n","import { serialize, deserialize } from '@topgunbuild/core';\n\nexport class EncryptionManager {\n private static ALGORITHM = 'AES-GCM';\n private static IV_LENGTH = 12;\n\n /**\n * Encrypts data using AES-GCM.\n * Serializes data to MessagePack before encryption.\n */\n static async encrypt(key: CryptoKey, data: any): Promise<{ iv: Uint8Array; data: Uint8Array }> {\n const encoded = serialize(data);\n\n // Generate IV\n const iv = window.crypto.getRandomValues(new Uint8Array(EncryptionManager.IV_LENGTH));\n\n // Encrypt\n const ciphertext = await window.crypto.subtle.encrypt(\n {\n name: EncryptionManager.ALGORITHM,\n iv: iv,\n },\n key,\n encoded as any\n );\n\n return {\n iv,\n data: new Uint8Array(ciphertext),\n };\n }\n\n /**\n * Decrypts AES-GCM encrypted data.\n * Deserializes from MessagePack after decryption.\n */\n static async decrypt(key: CryptoKey, record: { iv: Uint8Array; data: Uint8Array }): Promise<any> {\n try {\n const plaintextBuffer = await window.crypto.subtle.decrypt(\n {\n name: EncryptionManager.ALGORITHM,\n iv: record.iv as any,\n },\n key,\n record.data as any\n );\n\n return deserialize(new Uint8Array(plaintextBuffer));\n } catch (err) {\n console.error('Decryption failed', err);\n throw new Error('Failed to decrypt data: ' + err);\n }\n }\n}\n","import { IStorageAdapter, OpLogEntry } from '../IStorageAdapter';\nimport { EncryptionManager } from '../crypto/EncryptionManager';\n\n/**\n * Wraps an underlying storage adapter and encrypts data at rest using AES-GCM.\n */\nexport class EncryptedStorageAdapter implements IStorageAdapter {\n constructor(\n private wrapped: IStorageAdapter,\n private key: CryptoKey\n ) { }\n\n async initialize(dbName: string): Promise<void> {\n return this.wrapped.initialize(dbName);\n }\n\n async close(): Promise<void> {\n return this.wrapped.close();\n }\n\n // --- KV Operations ---\n\n async get<V>(key: string): Promise<V | any | undefined> {\n const raw = await this.wrapped.get<any>(key);\n\n if (!raw) {\n return undefined;\n }\n\n // Check if it looks like an encrypted record\n // We expect { iv: Uint8Array, data: Uint8Array }\n // Note: In a real app we might want a stricter check or a version tag.\n if (this.isEncryptedRecord(raw)) {\n try {\n return await EncryptionManager.decrypt(this.key, raw);\n } catch (e) {\n // Fallback for migration or corruption?\n // For now, fail loud as per spec.\n throw e;\n }\n }\n\n // Return raw if not encrypted (backwards compatibility during dev, or unencrypted data)\n return raw;\n }\n\n async put(key: string, value: any): Promise<void> {\n const encrypted = await EncryptionManager.encrypt(this.key, value);\n // Store as plain object to be compatible with structured clone algorithm of IndexedDB\n const storedValue = {\n iv: encrypted.iv,\n data: encrypted.data\n };\n return this.wrapped.put(key, storedValue);\n }\n\n async remove(key: string): Promise<void> {\n return this.wrapped.remove(key);\n }\n\n // --- Metadata ---\n\n async getMeta(key: string): Promise<any> {\n const raw = await this.wrapped.getMeta(key);\n if (!raw) return undefined;\n\n if (this.isEncryptedRecord(raw)) {\n return EncryptionManager.decrypt(this.key, raw);\n }\n return raw;\n }\n\n async setMeta(key: string, value: any): Promise<void> {\n const encrypted = await EncryptionManager.encrypt(this.key, value);\n return this.wrapped.setMeta(key, {\n iv: encrypted.iv,\n data: encrypted.data\n });\n }\n\n // --- Batch ---\n\n async batchPut(entries: Map<string, any>): Promise<void> {\n const encryptedEntries = new Map<string, any>();\n\n for (const [key, value] of entries.entries()) {\n const encrypted = await EncryptionManager.encrypt(this.key, value);\n encryptedEntries.set(key, {\n iv: encrypted.iv,\n data: encrypted.data\n });\n }\n\n return this.wrapped.batchPut(encryptedEntries);\n }\n\n // --- OpLog ---\n\n async appendOpLog(entry: Omit<OpLogEntry, 'id'>): Promise<number> {\n // Encrypt sensitive fields: value, record, orRecord\n const encryptedEntry = { ...entry };\n\n if (entry.value !== undefined) {\n const enc = await EncryptionManager.encrypt(this.key, entry.value);\n encryptedEntry.value = { iv: enc.iv, data: enc.data };\n }\n\n if (entry.record !== undefined) {\n const enc = await EncryptionManager.encrypt(this.key, entry.record);\n encryptedEntry.record = { iv: enc.iv, data: enc.data } as any;\n }\n\n if (entry.orRecord !== undefined) {\n const enc = await EncryptionManager.encrypt(this.key, entry.orRecord);\n encryptedEntry.orRecord = { iv: enc.iv, data: enc.data } as any;\n }\n\n // Note: 'key', 'op', 'mapName', 'orTag', 'hlc', 'synced' remain plaintext for indexing\n\n return this.wrapped.appendOpLog(encryptedEntry);\n }\n\n async getPendingOps(): Promise<OpLogEntry[]> {\n const ops = await this.wrapped.getPendingOps();\n\n // Decrypt in place\n // We map concurrently for performance\n return Promise.all(ops.map(async op => {\n const decryptedOp = { ...op };\n\n if (this.isEncryptedRecord(op.value)) {\n decryptedOp.value = await EncryptionManager.decrypt(this.key, op.value);\n }\n\n if (this.isEncryptedRecord(op.record)) {\n decryptedOp.record = await EncryptionManager.decrypt(this.key, op.record as any);\n }\n\n if (this.isEncryptedRecord(op.orRecord)) {\n decryptedOp.orRecord = await EncryptionManager.decrypt(this.key, op.orRecord as any);\n }\n\n return decryptedOp;\n }));\n }\n\n async markOpsSynced(lastId: number): Promise<void> {\n return this.wrapped.markOpsSynced(lastId);\n }\n\n // --- Iteration ---\n\n async getAllKeys(): Promise<string[]> {\n return this.wrapped.getAllKeys();\n }\n\n // --- Helpers ---\n\n private isEncryptedRecord(data: any): data is { iv: Uint8Array, data: Uint8Array } {\n return data &&\n typeof data === 'object' &&\n data.iv instanceof Uint8Array &&\n data.data instanceof Uint8Array;\n }\n}\n","import { SyncEngine } from './SyncEngine';\nimport { TopGunClient } from './TopGunClient';\nimport { TopGun } from './TopGun';\nexport * from './adapters/IDBAdapter';\nexport * from './adapters/EncryptedStorageAdapter';\nimport { QueryHandle } from './QueryHandle';\nimport { LWWMap, Predicates } from '@topgunbuild/core';\nimport { TopicHandle } from './TopicHandle';\nimport { SyncState, VALID_TRANSITIONS, isValidTransition } from './SyncState';\nimport { SyncStateMachine } from './SyncStateMachine';\nimport { BackpressureError } from './errors/BackpressureError';\nimport { DEFAULT_BACKPRESSURE_CONFIG } from './BackpressureConfig';\n\n// Type imports\nimport type { IStorageAdapter, OpLogEntry } from './IStorageAdapter';\nimport type { LWWRecord, PredicateNode } from '@topgunbuild/core';\nimport type { QueryFilter, QueryResultItem, QueryResultSource } from './QueryHandle';\nimport type { TopicCallback } from './TopicHandle';\nimport type { BackoffConfig, HeartbeatConfig, SyncEngineConfig } from './SyncEngine';\nimport type { StateChangeEvent, StateChangeListener, SyncStateMachineConfig } from './SyncStateMachine';\nimport type {\n BackpressureConfig,\n BackpressureStrategy,\n BackpressureStatus,\n BackpressureThresholdEvent,\n OperationDroppedEvent,\n} from './BackpressureConfig';\n\n// Value exports\nexport { SyncEngine, TopGunClient, TopGun, QueryHandle, LWWMap, Predicates, TopicHandle };\nexport { SyncState, VALID_TRANSITIONS, isValidTransition, SyncStateMachine };\nexport { BackpressureError, DEFAULT_BACKPRESSURE_CONFIG };\nexport { logger } from './utils/logger';\n\n// Type exports\nexport type {\n IStorageAdapter,\n OpLogEntry,\n LWWRecord,\n PredicateNode,\n QueryFilter,\n QueryResultItem,\n QueryResultSource,\n TopicCallback,\n BackoffConfig,\n HeartbeatConfig,\n SyncEngineConfig,\n StateChangeEvent,\n StateChangeListener,\n SyncStateMachineConfig,\n BackpressureConfig,\n BackpressureStr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AkB,QAAQ,KAAK,KAAK,MAAM,QAAQ;AACpE,qBAAe,WAAW,EAAE,IAAI,IAAI,IAAI,MAAM,IAAI,KAAK;AAAA,IAC3D;AAIA,WAAO,KAAK,QAAQ,YAAY,cAAc;AAAA,EAClD;AAAA,EAEA,MAAM,gBAAuC;AACzC,UAAM,MAAM,MAAM,KAAK,QAAQ,cAAc;AAI7C,WAAO,QAAQ,IAAI,IAAI,IAAI,OAAM,OAAM;AACnC,YAAM,cAAc,EAAE,GAAG,GAAG;AAE5B,UAAI,KAAK,kBAAkB,GAAG,KAAK,GAAG;AAClC,oBAAY,QAAQ,MAAM,kBAAkB,QAAQ,KAAK,KAAK,GAAG,KAAK;AAAA,MAC1E;AAEA,UAAI,KAAK,kBAAkB,GAAG,MAAM,GAAG;AACnC,oBAAY,SAAS,MAAM,kBAAkB,QAAQ,KAAK,KAAK,GAAG,MAAa;AAAA,MACnF;AAEA,UAAI,KAAK,kBAAkB,GAAG,QAAQ,GAAG;AACrC,oBAAY,WAAW,MAAM,kBAAkB,QAAQ,KAAK,KAAK,GAAG,QAAe;AAAA,MACvF;AAEA,aAAO;AAAA,IACX,CAAC,CAAC;AAAA,EACN;AAAA,EAEA,MAAM,cAAc,QAA+B;AAC/C,WAAO,KAAK,QAAQ,cAAc,MAAM;AAAA,EAC5C;AAAA;AAAA,EAIA,MAAM,aAAgC;AAClC,WAAO,KAAK,QAAQ,WAAW;AAAA,EACnC;AAAA;AAAA,EAIQ,kBAAkB,MAAyD;AAC/E,WAAO,QACH,OAAO,SAAS,YAChB,KAAK,cAAc,cACnB,KAAK,gBAAgB;AAAA,EAC7B;AACJ;;;AC9JA,SAAS,UAAAC,SAAQ,kBAAkB;","names":["SyncState","token","LWWMap","ORMap","LWWMap","ORMap","serialize","deserialize","LWWMap"]}
|
|
1
|
+
{"version":3,"sources":["../src/SyncEngine.ts","../src/utils/logger.ts","../src/SyncState.ts","../src/SyncStateMachine.ts","../src/errors/BackpressureError.ts","../src/BackpressureConfig.ts","../src/connection/SingleServerProvider.ts","../src/TopGunClient.ts","../src/QueryHandle.ts","../src/DistributedLock.ts","../src/TopicHandle.ts","../src/cluster/ClusterClient.ts","../src/cluster/ConnectionPool.ts","../src/cluster/PartitionRouter.ts","../src/adapters/IDBAdapter.ts","../src/TopGun.ts","../src/crypto/EncryptionManager.ts","../src/adapters/EncryptedStorageAdapter.ts","../src/index.ts"],"sourcesContent":["import { HLC, LWWMap, ORMap, serialize, deserialize, evaluatePredicate } from '@topgunbuild/core';\nimport type { LWWRecord, ORMapRecord, Timestamp } from '@topgunbuild/core';\nimport type { IStorageAdapter } from './IStorageAdapter';\nimport { QueryHandle } from './QueryHandle';\nimport type { QueryFilter } from './QueryHandle';\nimport { TopicHandle } from './TopicHandle';\nimport { logger } from './utils/logger';\nimport { SyncStateMachine, StateChangeEvent } from './SyncStateMachine';\nimport { SyncState } from './SyncState';\nimport { BackpressureError } from './errors/BackpressureError';\nimport type {\n BackpressureConfig,\n BackpressureStatus,\n BackpressureStrategy,\n BackpressureThresholdEvent,\n OperationDroppedEvent,\n} from './BackpressureConfig';\nimport { DEFAULT_BACKPRESSURE_CONFIG } from './BackpressureConfig';\nimport type { IConnectionProvider } from './types';\nimport { SingleServerProvider } from './connection/SingleServerProvider';\n\nexport interface OpLogEntry {\n id: string; // Unique ID for the operation\n mapName: string;\n opType: 'PUT' | 'REMOVE' | 'OR_ADD' | 'OR_REMOVE';\n key: string;\n record?: LWWRecord<any>; // LWW Put/Remove (Remove has null value)\n orRecord?: ORMapRecord<any>; // ORMap Add\n orTag?: string; // ORMap Remove (Tombstone tag)\n timestamp: Timestamp; // HLC timestamp of the operation\n synced: boolean; // True if this operation has been successfully pushed to the server\n}\n\nexport interface HeartbeatConfig {\n intervalMs: number; // Default: 5000 (5 seconds)\n timeoutMs: number; // Default: 15000 (15 seconds)\n enabled: boolean; // Default: true\n}\n\nexport interface BackoffConfig {\n /** Initial delay in milliseconds (default: 1000) */\n initialDelayMs: number;\n /** Maximum delay in milliseconds (default: 30000) */\n maxDelayMs: number;\n /** Multiplier for exponential backoff (default: 2) */\n multiplier: number;\n /** Whether to add random jitter to delay (default: true) */\n jitter: boolean;\n /** Maximum number of retry attempts before entering ERROR state (default: 10) */\n maxRetries: number;\n}\n\nexport interface SyncEngineConfig {\n nodeId: string;\n /** @deprecated Use connectionProvider instead */\n serverUrl?: string;\n /** Connection provider (preferred over serverUrl) */\n connectionProvider?: IConnectionProvider;\n storageAdapter: IStorageAdapter;\n reconnectInterval?: number;\n heartbeat?: Partial<HeartbeatConfig>;\n backoff?: Partial<BackoffConfig>;\n backpressure?: Partial<BackpressureConfig>;\n}\n\nconst DEFAULT_BACKOFF_CONFIG: BackoffConfig = {\n initialDelayMs: 1000,\n maxDelayMs: 30000,\n multiplier: 2,\n jitter: true,\n maxRetries: 10,\n};\n\nexport class SyncEngine {\n private readonly nodeId: string;\n private readonly serverUrl: string;\n private readonly storageAdapter: IStorageAdapter;\n private readonly hlc: HLC;\n private readonly stateMachine: SyncStateMachine;\n private readonly backoffConfig: BackoffConfig;\n private readonly connectionProvider: IConnectionProvider;\n private readonly useConnectionProvider: boolean;\n\n private websocket: WebSocket | null = null;\n private opLog: OpLogEntry[] = [];\n private maps: Map<string, LWWMap<any, any> | ORMap<any, any>> = new Map();\n private queries: Map<string, QueryHandle<any>> = new Map();\n private topics: Map<string, TopicHandle> = new Map();\n private pendingLockRequests: Map<string, { resolve: (res: any) => void, reject: (err: any) => void, timer: any }> = new Map();\n private lastSyncTimestamp: number = 0;\n private reconnectTimer: any = null; // NodeJS.Timeout\n private authToken: string | null = null;\n private tokenProvider: (() => Promise<string | null>) | null = null;\n private backoffAttempt: number = 0;\n\n // Heartbeat state\n private readonly heartbeatConfig: HeartbeatConfig;\n private heartbeatInterval: ReturnType<typeof setInterval> | null = null;\n private lastPongReceived: number = Date.now();\n private lastRoundTripTime: number | null = null;\n\n // Backpressure state\n private readonly backpressureConfig: BackpressureConfig;\n private backpressurePaused: boolean = false;\n private waitingForCapacity: Array<() => void> = [];\n private highWaterMarkEmitted: boolean = false;\n private backpressureListeners: Map<string, Set<(...args: any[]) => void>> = new Map();\n\n // Write Concern state (Phase 5.01)\n private pendingWriteConcernPromises: Map<string, {\n resolve: (result: any) => void;\n reject: (error: Error) => void;\n timeoutHandle?: ReturnType<typeof setTimeout>;\n }> = new Map();\n\n constructor(config: SyncEngineConfig) {\n // Validate config: either serverUrl or connectionProvider required\n if (!config.serverUrl && !config.connectionProvider) {\n throw new Error('SyncEngine requires either serverUrl or connectionProvider');\n }\n\n this.nodeId = config.nodeId;\n this.serverUrl = config.serverUrl || '';\n this.storageAdapter = config.storageAdapter;\n this.hlc = new HLC(this.nodeId);\n\n // Initialize state machine\n this.stateMachine = new SyncStateMachine();\n\n // Initialize heartbeat config with defaults\n this.heartbeatConfig = {\n intervalMs: config.heartbeat?.intervalMs ?? 5000,\n timeoutMs: config.heartbeat?.timeoutMs ?? 15000,\n enabled: config.heartbeat?.enabled ?? true,\n };\n\n // Merge backoff config with defaults\n this.backoffConfig = {\n ...DEFAULT_BACKOFF_CONFIG,\n ...config.backoff,\n };\n\n // Merge backpressure config with defaults\n this.backpressureConfig = {\n ...DEFAULT_BACKPRESSURE_CONFIG,\n ...config.backpressure,\n };\n\n // Initialize connection provider\n if (config.connectionProvider) {\n this.connectionProvider = config.connectionProvider;\n this.useConnectionProvider = true;\n this.initConnectionProvider();\n } else {\n // Legacy mode: create SingleServerProvider internally\n this.connectionProvider = new SingleServerProvider({ url: config.serverUrl! });\n this.useConnectionProvider = false;\n this.initConnection();\n }\n\n this.loadOpLog();\n }\n\n // ============================================\n // State Machine Public API\n // ============================================\n\n /**\n * Get the current connection state\n */\n getConnectionState(): SyncState {\n return this.stateMachine.getState();\n }\n\n /**\n * Subscribe to connection state changes\n * @returns Unsubscribe function\n */\n onConnectionStateChange(listener: (event: StateChangeEvent) => void): () => void {\n return this.stateMachine.onStateChange(listener);\n }\n\n /**\n * Get state machine history for debugging\n */\n getStateHistory(limit?: number): StateChangeEvent[] {\n return this.stateMachine.getHistory(limit);\n }\n\n // ============================================\n // Internal State Helpers (replace boolean flags)\n // ============================================\n\n /**\n * Check if WebSocket is connected (but may not be authenticated yet)\n */\n private isOnline(): boolean {\n const state = this.stateMachine.getState();\n return (\n state === SyncState.CONNECTING ||\n state === SyncState.AUTHENTICATING ||\n state === SyncState.SYNCING ||\n state === SyncState.CONNECTED\n );\n }\n\n /**\n * Check if fully authenticated and ready for operations\n */\n private isAuthenticated(): boolean {\n const state = this.stateMachine.getState();\n return state === SyncState.SYNCING || state === SyncState.CONNECTED;\n }\n\n /**\n * Check if fully connected and synced\n */\n private isConnected(): boolean {\n return this.stateMachine.getState() === SyncState.CONNECTED;\n }\n\n // ============================================\n // Connection Management\n // ============================================\n\n /**\n * Initialize connection using IConnectionProvider (Phase 4.5 cluster mode).\n * Sets up event handlers for the connection provider.\n */\n private initConnectionProvider(): void {\n // Transition to CONNECTING state\n this.stateMachine.transition(SyncState.CONNECTING);\n\n // Set up event handlers\n this.connectionProvider.on('connected', (_nodeId: string) => {\n if (this.authToken || this.tokenProvider) {\n logger.info('ConnectionProvider connected. Sending auth...');\n this.stateMachine.transition(SyncState.AUTHENTICATING);\n this.sendAuth();\n } else {\n logger.info('ConnectionProvider connected. Waiting for auth token...');\n this.stateMachine.transition(SyncState.AUTHENTICATING);\n }\n });\n\n this.connectionProvider.on('disconnected', (_nodeId: string) => {\n logger.info('ConnectionProvider disconnected.');\n this.stopHeartbeat();\n this.stateMachine.transition(SyncState.DISCONNECTED);\n // Don't schedule reconnect - provider handles it\n });\n\n this.connectionProvider.on('reconnected', (_nodeId: string) => {\n logger.info('ConnectionProvider reconnected.');\n this.stateMachine.transition(SyncState.CONNECTING);\n if (this.authToken || this.tokenProvider) {\n this.stateMachine.transition(SyncState.AUTHENTICATING);\n this.sendAuth();\n }\n });\n\n this.connectionProvider.on('message', (_nodeId: string, data: any) => {\n let message: any;\n if (data instanceof ArrayBuffer) {\n message = deserialize(new Uint8Array(data));\n } else if (data instanceof Uint8Array) {\n message = deserialize(data);\n } else {\n try {\n message = typeof data === 'string' ? JSON.parse(data) : data;\n } catch (e) {\n logger.error({ err: e }, 'Failed to parse message from ConnectionProvider');\n return;\n }\n }\n this.handleServerMessage(message);\n });\n\n this.connectionProvider.on('partitionMapUpdated', () => {\n logger.debug('Partition map updated');\n // Could trigger re-subscriptions if needed\n });\n\n this.connectionProvider.on('error', (error: Error) => {\n logger.error({ err: error }, 'ConnectionProvider error');\n });\n\n // Start connection\n this.connectionProvider.connect().catch((err) => {\n logger.error({ err }, 'Failed to connect via ConnectionProvider');\n this.stateMachine.transition(SyncState.DISCONNECTED);\n });\n }\n\n /**\n * Initialize connection using direct WebSocket (legacy single-server mode).\n */\n private initConnection(): void {\n // Transition to CONNECTING state\n this.stateMachine.transition(SyncState.CONNECTING);\n\n this.websocket = new WebSocket(this.serverUrl);\n this.websocket.binaryType = 'arraybuffer';\n\n this.websocket.onopen = () => {\n // WebSocket is open, now we need to authenticate\n // [CHANGE] Don't send auth immediately if we don't have a token\n // This prevents the \"AUTH_REQUIRED -> Close -> Retry loop\" for anonymous initial connects\n if (this.authToken || this.tokenProvider) {\n logger.info('WebSocket connected. Sending auth...');\n this.stateMachine.transition(SyncState.AUTHENTICATING);\n this.sendAuth();\n } else {\n logger.info('WebSocket connected. Waiting for auth token...');\n // Stay in CONNECTING state until we have a token\n // We're online but not authenticated\n this.stateMachine.transition(SyncState.AUTHENTICATING);\n }\n };\n\n this.websocket.onmessage = (event) => {\n let message: any;\n if (event.data instanceof ArrayBuffer) {\n message = deserialize(new Uint8Array(event.data));\n } else {\n try {\n message = JSON.parse(event.data);\n } catch (e) {\n logger.error({ err: e }, 'Failed to parse message');\n return;\n }\n }\n this.handleServerMessage(message);\n };\n\n this.websocket.onclose = () => {\n logger.info('WebSocket disconnected.');\n this.stopHeartbeat();\n this.stateMachine.transition(SyncState.DISCONNECTED);\n this.scheduleReconnect();\n };\n\n this.websocket.onerror = (error) => {\n logger.error({ err: error }, 'WebSocket error');\n // Error will typically be followed by close, so we don't transition here\n };\n }\n\n private scheduleReconnect(): void {\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = null;\n }\n\n // Check if we've exceeded max retries\n if (this.backoffAttempt >= this.backoffConfig.maxRetries) {\n logger.error(\n { attempts: this.backoffAttempt },\n 'Max reconnection attempts reached. Entering ERROR state.'\n );\n this.stateMachine.transition(SyncState.ERROR);\n return;\n }\n\n // Transition to BACKOFF state\n this.stateMachine.transition(SyncState.BACKOFF);\n\n const delay = this.calculateBackoffDelay();\n logger.info({ delay, attempt: this.backoffAttempt }, `Backing off for ${delay}ms`);\n\n this.reconnectTimer = setTimeout(() => {\n this.reconnectTimer = null;\n this.backoffAttempt++;\n this.initConnection();\n }, delay);\n }\n\n private calculateBackoffDelay(): number {\n const { initialDelayMs, maxDelayMs, multiplier, jitter } = this.backoffConfig;\n let delay = initialDelayMs * Math.pow(multiplier, this.backoffAttempt);\n delay = Math.min(delay, maxDelayMs);\n\n if (jitter) {\n // Add jitter: 0.5x to 1.5x of calculated delay\n delay = delay * (0.5 + Math.random());\n }\n\n return Math.floor(delay);\n }\n\n /**\n * Reset backoff counter (called on successful connection)\n */\n private resetBackoff(): void {\n this.backoffAttempt = 0;\n }\n\n /**\n * Send a message through the current connection.\n * Uses connectionProvider if in cluster mode, otherwise uses direct websocket.\n * @param message Message object to serialize and send\n * @param key Optional key for routing (cluster mode only)\n * @returns true if message was sent, false otherwise\n */\n private sendMessage(message: any, key?: string): boolean {\n const data = serialize(message);\n\n if (this.useConnectionProvider) {\n try {\n this.connectionProvider.send(data, key);\n return true;\n } catch (err) {\n logger.warn({ err }, 'Failed to send via ConnectionProvider');\n return false;\n }\n } else {\n if (this.websocket?.readyState === WebSocket.OPEN) {\n this.websocket.send(data);\n return true;\n }\n return false;\n }\n }\n\n /**\n * Check if we can send messages (connection is ready).\n */\n private canSend(): boolean {\n if (this.useConnectionProvider) {\n return this.connectionProvider.isConnected();\n }\n return this.websocket?.readyState === WebSocket.OPEN;\n }\n\n private async loadOpLog(): Promise<void> {\n const storedTimestamp = await this.storageAdapter.getMeta('lastSyncTimestamp');\n if (storedTimestamp) {\n this.lastSyncTimestamp = storedTimestamp;\n }\n\n const pendingOps = await this.storageAdapter.getPendingOps();\n this.opLog = pendingOps.map(op => ({\n ...op,\n id: String(op.id),\n synced: false\n })) as unknown as OpLogEntry[];\n\n if (this.opLog.length > 0) {\n logger.info({ count: this.opLog.length }, 'Loaded pending operations from local storage');\n }\n }\n\n private async saveOpLog(): Promise<void> {\n await this.storageAdapter.setMeta('lastSyncTimestamp', this.lastSyncTimestamp);\n }\n\n public registerMap(mapName: string, map: LWWMap<any, any> | ORMap<any, any>): void {\n this.maps.set(mapName, map);\n }\n\n public async recordOperation(\n mapName: string,\n opType: 'PUT' | 'REMOVE' | 'OR_ADD' | 'OR_REMOVE',\n key: string,\n data: { record?: LWWRecord<any>; orRecord?: ORMapRecord<any>; orTag?: string; timestamp: Timestamp }\n ): Promise<string> {\n // Check backpressure before adding new operation\n await this.checkBackpressure();\n\n const opLogEntry: Omit<OpLogEntry, 'id'> & { id?: string } = {\n mapName,\n opType,\n key,\n record: data.record,\n orRecord: data.orRecord,\n orTag: data.orTag,\n timestamp: data.timestamp,\n synced: false,\n };\n\n const id = await this.storageAdapter.appendOpLog(opLogEntry as any);\n opLogEntry.id = String(id);\n\n this.opLog.push(opLogEntry as OpLogEntry);\n\n // Check high water mark after adding operation\n this.checkHighWaterMark();\n\n if (this.isAuthenticated()) {\n this.syncPendingOperations();\n }\n\n return opLogEntry.id;\n }\n\n private syncPendingOperations(): void {\n const pending = this.opLog.filter(op => !op.synced);\n if (pending.length === 0) return;\n\n logger.info({ count: pending.length }, 'Syncing pending operations');\n\n this.sendMessage({\n type: 'OP_BATCH',\n payload: {\n ops: pending\n }\n });\n }\n\n private startMerkleSync(): void {\n for (const [mapName, map] of this.maps) {\n if (map instanceof LWWMap) {\n logger.info({ mapName }, 'Starting Merkle sync for LWWMap');\n this.sendMessage({\n type: 'SYNC_INIT',\n mapName,\n lastSyncTimestamp: this.lastSyncTimestamp\n });\n } else if (map instanceof ORMap) {\n logger.info({ mapName }, 'Starting Merkle sync for ORMap');\n const tree = map.getMerkleTree();\n const rootHash = tree.getRootHash();\n\n // Build bucket hashes for all non-empty buckets at depth 0\n const bucketHashes: Record<string, number> = tree.getBuckets('');\n\n this.sendMessage({\n type: 'ORMAP_SYNC_INIT',\n mapName,\n rootHash,\n bucketHashes,\n lastSyncTimestamp: this.lastSyncTimestamp\n });\n }\n }\n }\n\n public setAuthToken(token: string): void {\n this.authToken = token;\n this.tokenProvider = null;\n\n const state = this.stateMachine.getState();\n if (state === SyncState.AUTHENTICATING || state === SyncState.CONNECTING) {\n // If we are already connected (e.g. waiting for token), send it now\n this.sendAuth();\n } else if (state === SyncState.BACKOFF || state === SyncState.DISCONNECTED) {\n // [CHANGE] Force immediate reconnect if we were waiting for retry timer\n logger.info('Auth token set during backoff/disconnect. Reconnecting immediately.');\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = null;\n }\n // Reset backoff since user provided new credentials\n this.resetBackoff();\n this.initConnection();\n }\n }\n\n public setTokenProvider(provider: () => Promise<string | null>): void {\n this.tokenProvider = provider;\n const state = this.stateMachine.getState();\n if (state === SyncState.AUTHENTICATING) {\n this.sendAuth();\n }\n }\n\n private async sendAuth(): Promise<void> {\n if (this.tokenProvider) {\n try {\n const token = await this.tokenProvider();\n if (token) {\n this.authToken = token;\n }\n } catch (err) {\n logger.error({ err }, 'Failed to get token from provider');\n return;\n }\n }\n\n const token = this.authToken;\n if (!token) return; // Don't send anonymous auth anymore\n\n this.sendMessage({\n type: 'AUTH',\n token\n });\n }\n\n public subscribeToQuery(query: QueryHandle<any>) {\n this.queries.set(query.id, query);\n if (this.isAuthenticated()) {\n this.sendQuerySubscription(query);\n }\n }\n\n public subscribeToTopic(topic: string, handle: TopicHandle) {\n this.topics.set(topic, handle);\n if (this.isAuthenticated()) {\n this.sendTopicSubscription(topic);\n }\n }\n\n public unsubscribeFromTopic(topic: string) {\n this.topics.delete(topic);\n if (this.isAuthenticated()) {\n this.sendMessage({\n type: 'TOPIC_UNSUB',\n payload: { topic }\n });\n }\n }\n\n public publishTopic(topic: string, data: any) {\n if (this.isAuthenticated()) {\n this.sendMessage({\n type: 'TOPIC_PUB',\n payload: { topic, data }\n });\n } else {\n // TODO: Queue topic messages or drop?\n // Spec says Fire-and-Forget, so dropping is acceptable if offline,\n // but queueing is better UX.\n // For now, log warning.\n logger.warn({ topic }, 'Dropped topic publish (offline)');\n }\n }\n\n private sendTopicSubscription(topic: string) {\n this.sendMessage({\n type: 'TOPIC_SUB',\n payload: { topic }\n });\n }\n\n /**\n * Executes a query against local storage immediately\n */\n public async runLocalQuery(mapName: string, filter: QueryFilter): Promise<{ key: string, value: any }[]> {\n // Retrieve all keys for the map\n const keys = await this.storageAdapter.getAllKeys();\n const mapKeys = keys.filter(k => k.startsWith(mapName + ':'));\n\n const results = [];\n for (const fullKey of mapKeys) {\n const record = await this.storageAdapter.get(fullKey);\n if (record && record.value) {\n // Extract actual key from \"mapName:key\"\n const actualKey = fullKey.slice(mapName.length + 1);\n\n let matches = true;\n\n // Apply 'where' (equality)\n if (filter.where) {\n for (const [k, v] of Object.entries(filter.where)) {\n if (record.value[k] !== v) {\n matches = false;\n break;\n }\n }\n }\n\n // Apply 'predicate'\n if (matches && filter.predicate) {\n if (!evaluatePredicate(filter.predicate, record.value)) {\n matches = false;\n }\n }\n\n if (matches) {\n results.push({ key: actualKey, value: record.value });\n }\n }\n }\n return results;\n }\n\n public unsubscribeFromQuery(queryId: string) {\n this.queries.delete(queryId);\n if (this.isAuthenticated()) {\n this.sendMessage({\n type: 'QUERY_UNSUB',\n payload: { queryId }\n });\n }\n }\n\n private sendQuerySubscription(query: QueryHandle<any>) {\n this.sendMessage({\n type: 'QUERY_SUB',\n payload: {\n queryId: query.id,\n mapName: query.getMapName(),\n query: query.getFilter()\n }\n });\n }\n\n public requestLock(name: string, requestId: string, ttl: number): Promise<{ fencingToken: number }> {\n if (!this.isAuthenticated()) {\n return Promise.reject(new Error('Not connected or authenticated'));\n }\n\n return new Promise((resolve, reject) => {\n // Timeout if no response (server might be down or message lost)\n // We set a client-side timeout slightly larger than TTL if TTL is short,\n // but usually we want a separate \"Wait Timeout\".\n // For now, use a fixed 30s timeout for the *response*.\n const timer = setTimeout(() => {\n if (this.pendingLockRequests.has(requestId)) {\n this.pendingLockRequests.delete(requestId);\n reject(new Error('Lock request timed out waiting for server response'));\n }\n }, 30000);\n\n this.pendingLockRequests.set(requestId, { resolve, reject, timer });\n\n try {\n const sent = this.sendMessage({\n type: 'LOCK_REQUEST',\n payload: { requestId, name, ttl }\n });\n if (!sent) {\n clearTimeout(timer);\n this.pendingLockRequests.delete(requestId);\n reject(new Error('Failed to send lock request'));\n }\n } catch (e) {\n clearTimeout(timer);\n this.pendingLockRequests.delete(requestId);\n reject(e);\n }\n });\n }\n\n public releaseLock(name: string, requestId: string, fencingToken: number): Promise<boolean> {\n if (!this.isOnline()) return Promise.resolve(false);\n\n return new Promise((resolve, reject) => {\n const timer = setTimeout(() => {\n if (this.pendingLockRequests.has(requestId)) {\n this.pendingLockRequests.delete(requestId);\n // Resolve false on timeout? Or reject?\n // Release is usually fire-and-forget but we wanted ACK.\n resolve(false);\n }\n }, 5000);\n\n this.pendingLockRequests.set(requestId, { resolve, reject, timer });\n\n try {\n const sent = this.sendMessage({\n type: 'LOCK_RELEASE',\n payload: { requestId, name, fencingToken }\n });\n if (!sent) {\n clearTimeout(timer);\n this.pendingLockRequests.delete(requestId);\n resolve(false);\n }\n } catch (e) {\n clearTimeout(timer);\n this.pendingLockRequests.delete(requestId);\n resolve(false);\n }\n });\n }\n\n private async handleServerMessage(message: any): Promise<void> {\n switch (message.type) {\n case 'BATCH': {\n // Unbatch and process each message\n // Format: [4 bytes: count][4 bytes: len1][msg1][4 bytes: len2][msg2]...\n const batchData = message.data as Uint8Array;\n const view = new DataView(batchData.buffer, batchData.byteOffset, batchData.byteLength);\n let offset = 0;\n\n const count = view.getUint32(offset, true);\n offset += 4;\n\n for (let i = 0; i < count; i++) {\n const msgLen = view.getUint32(offset, true);\n offset += 4;\n\n const msgData = batchData.slice(offset, offset + msgLen);\n offset += msgLen;\n\n const innerMsg = deserialize(msgData);\n await this.handleServerMessage(innerMsg);\n }\n break;\n }\n\n case 'AUTH_REQUIRED':\n this.sendAuth();\n break;\n\n case 'AUTH_ACK': {\n logger.info('Authenticated successfully');\n const wasAuthenticated = this.isAuthenticated();\n\n // Transition to SYNCING state\n this.stateMachine.transition(SyncState.SYNCING);\n\n // Reset backoff on successful auth\n this.resetBackoff();\n\n this.syncPendingOperations();\n\n // Only re-subscribe on first authentication to prevent UI flickering\n if (!wasAuthenticated) {\n this.startHeartbeat();\n this.startMerkleSync();\n for (const query of this.queries.values()) {\n this.sendQuerySubscription(query);\n }\n for (const topic of this.topics.keys()) {\n this.sendTopicSubscription(topic);\n }\n }\n\n // After initial sync setup, transition to CONNECTED\n // In a real implementation, you might wait for SYNC_COMPLETE message\n this.stateMachine.transition(SyncState.CONNECTED);\n break;\n }\n\n case 'PONG': {\n this.handlePong(message);\n break;\n }\n\n case 'AUTH_FAIL':\n logger.error({ error: message.error }, 'Authentication failed');\n this.authToken = null; // Clear invalid token\n // Stay in AUTHENTICATING or go to ERROR depending on severity\n // For now, let the connection close naturally or retry with new token\n break;\n\n case 'OP_ACK': {\n const { lastId, achievedLevel, results } = message.payload;\n logger.info({ lastId, achievedLevel, hasResults: !!results }, 'Received ACK for ops');\n\n // Handle per-operation results if available (Write Concern Phase 5.01)\n if (results && Array.isArray(results)) {\n for (const result of results) {\n const op = this.opLog.find(o => o.id === result.opId);\n if (op && !op.synced) {\n op.synced = true;\n logger.debug({ opId: result.opId, achievedLevel: result.achievedLevel, success: result.success }, 'Op ACK with Write Concern');\n }\n // Resolve pending Write Concern promise if exists\n this.resolveWriteConcernPromise(result.opId, result);\n }\n }\n\n // Backwards compatible: mark all ops up to lastId as synced\n let maxSyncedId = -1;\n let ackedCount = 0;\n this.opLog.forEach(op => {\n if (op.id && op.id <= lastId) {\n if (!op.synced) {\n ackedCount++;\n }\n op.synced = true;\n const idNum = parseInt(op.id, 10);\n if (!isNaN(idNum) && idNum > maxSyncedId) {\n maxSyncedId = idNum;\n }\n }\n });\n if (maxSyncedId !== -1) {\n this.storageAdapter.markOpsSynced(maxSyncedId).catch(err => logger.error({ err }, 'Failed to mark ops synced'));\n }\n // Check low water mark after ACKs reduce pending count\n if (ackedCount > 0) {\n this.checkLowWaterMark();\n }\n break;\n }\n\n case 'LOCK_GRANTED': {\n const { requestId, fencingToken } = message.payload;\n const req = this.pendingLockRequests.get(requestId);\n if (req) {\n clearTimeout(req.timer);\n this.pendingLockRequests.delete(requestId);\n req.resolve({ fencingToken });\n }\n break;\n }\n\n case 'LOCK_RELEASED': {\n const { requestId, success } = message.payload;\n const req = this.pendingLockRequests.get(requestId);\n if (req) {\n clearTimeout(req.timer);\n this.pendingLockRequests.delete(requestId);\n req.resolve(success);\n }\n break;\n }\n\n case 'QUERY_RESP': {\n const { queryId, results } = message.payload;\n const query = this.queries.get(queryId);\n if (query) {\n query.onResult(results, 'server');\n }\n break;\n }\n\n case 'QUERY_UPDATE': {\n const { queryId, key, value, type } = message.payload;\n const query = this.queries.get(queryId);\n if (query) {\n query.onUpdate(key, type === 'REMOVE' ? null : value);\n }\n break;\n }\n\n case 'SERVER_EVENT': {\n // Modified to support ORMap\n const { mapName, eventType, key, record, orRecord, orTag } = message.payload;\n await this.applyServerEvent(mapName, eventType, key, record, orRecord, orTag);\n break;\n }\n\n case 'SERVER_BATCH_EVENT': {\n // === OPTIMIZATION: Batch event processing ===\n // Server sends multiple events in a single message for efficiency\n const { events } = message.payload;\n for (const event of events) {\n await this.applyServerEvent(\n event.mapName,\n event.eventType,\n event.key,\n event.record,\n event.orRecord,\n event.orTag\n );\n }\n break;\n }\n\n case 'TOPIC_MESSAGE': {\n const { topic, data, publisherId, timestamp } = message.payload;\n const handle = this.topics.get(topic);\n if (handle) {\n handle.onMessage(data, { publisherId, timestamp });\n }\n break;\n }\n\n case 'GC_PRUNE': {\n const { olderThan } = message.payload;\n logger.info({ olderThan: olderThan.millis }, 'Received GC_PRUNE request');\n\n for (const [name, map] of this.maps) {\n if (map instanceof LWWMap) {\n const removedKeys = map.prune(olderThan);\n for (const key of removedKeys) {\n await this.storageAdapter.remove(`${name}:${key}`);\n }\n if (removedKeys.length > 0) {\n logger.info({ mapName: name, count: removedKeys.length }, 'Pruned tombstones from LWWMap');\n }\n } else if (map instanceof ORMap) {\n const removedTags = map.prune(olderThan);\n if (removedTags.length > 0) {\n logger.info({ mapName: name, count: removedTags.length }, 'Pruned tombstones from ORMap');\n }\n }\n }\n break;\n }\n\n case 'SYNC_RESET_REQUIRED': {\n const { mapName } = message.payload;\n logger.warn({ mapName }, 'Sync Reset Required due to GC Age');\n await this.resetMap(mapName);\n // Trigger re-sync as fresh\n this.sendMessage({\n type: 'SYNC_INIT',\n mapName,\n lastSyncTimestamp: 0\n });\n break;\n }\n\n case 'SYNC_RESP_ROOT': {\n const { mapName, rootHash, timestamp } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof LWWMap) {\n const localRootHash = map.getMerkleTree().getRootHash();\n if (localRootHash !== rootHash) {\n logger.info({ mapName, localRootHash, remoteRootHash: rootHash }, 'Root hash mismatch, requesting buckets');\n this.sendMessage({\n type: 'MERKLE_REQ_BUCKET',\n payload: { mapName, path: '' }\n });\n } else {\n logger.info({ mapName }, 'Map is in sync');\n }\n }\n // Update HLC with server timestamp\n if (timestamp) {\n this.hlc.update(timestamp);\n this.lastSyncTimestamp = timestamp.millis;\n await this.saveOpLog();\n }\n break;\n }\n\n case 'SYNC_RESP_BUCKETS': {\n const { mapName, path, buckets } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof LWWMap) {\n const tree = map.getMerkleTree();\n const localBuckets = tree.getBuckets(path);\n\n for (const [bucketKey, remoteHash] of Object.entries(buckets)) {\n const localHash = localBuckets[bucketKey] || 0;\n if (localHash !== remoteHash) {\n const newPath = path + bucketKey;\n this.sendMessage({\n type: 'MERKLE_REQ_BUCKET',\n payload: { mapName, path: newPath }\n });\n }\n }\n }\n break;\n }\n\n case 'SYNC_RESP_LEAF': {\n const { mapName, records } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof LWWMap) {\n let updateCount = 0;\n for (const { key, record } of records) {\n // Merge into local map\n const updated = map.merge(key, record);\n if (updated) {\n updateCount++;\n // Persist to storage\n await this.storageAdapter.put(`${mapName}:${key}`, record);\n }\n }\n if (updateCount > 0) {\n logger.info({ mapName, count: updateCount }, 'Synced records from server');\n }\n }\n break;\n }\n\n // ============ ORMap Sync Message Handlers ============\n\n case 'ORMAP_SYNC_RESP_ROOT': {\n const { mapName, rootHash, timestamp } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof ORMap) {\n const localTree = map.getMerkleTree();\n const localRootHash = localTree.getRootHash();\n\n if (localRootHash !== rootHash) {\n logger.info({ mapName, localRootHash, remoteRootHash: rootHash }, 'ORMap root hash mismatch, requesting buckets');\n this.sendMessage({\n type: 'ORMAP_MERKLE_REQ_BUCKET',\n payload: { mapName, path: '' }\n });\n } else {\n logger.info({ mapName }, 'ORMap is in sync');\n }\n }\n // Update HLC with server timestamp\n if (timestamp) {\n this.hlc.update(timestamp);\n this.lastSyncTimestamp = timestamp.millis;\n await this.saveOpLog();\n }\n break;\n }\n\n case 'ORMAP_SYNC_RESP_BUCKETS': {\n const { mapName, path, buckets } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof ORMap) {\n const tree = map.getMerkleTree();\n const localBuckets = tree.getBuckets(path);\n\n for (const [bucketKey, remoteHash] of Object.entries(buckets)) {\n const localHash = localBuckets[bucketKey] || 0;\n if (localHash !== remoteHash) {\n const newPath = path + bucketKey;\n this.sendMessage({\n type: 'ORMAP_MERKLE_REQ_BUCKET',\n payload: { mapName, path: newPath }\n });\n }\n }\n\n // Also check for buckets that exist locally but not on remote\n for (const [bucketKey, localHash] of Object.entries(localBuckets)) {\n if (!(bucketKey in buckets) && localHash !== 0) {\n // Local has data that remote doesn't - need to push\n const newPath = path + bucketKey;\n const keys = tree.getKeysInBucket(newPath);\n if (keys.length > 0) {\n this.pushORMapDiff(mapName, keys, map);\n }\n }\n }\n }\n break;\n }\n\n case 'ORMAP_SYNC_RESP_LEAF': {\n const { mapName, entries } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof ORMap) {\n let totalAdded = 0;\n let totalUpdated = 0;\n\n for (const entry of entries) {\n const { key, records, tombstones } = entry;\n const result = map.mergeKey(key, records, tombstones);\n totalAdded += result.added;\n totalUpdated += result.updated;\n }\n\n if (totalAdded > 0 || totalUpdated > 0) {\n logger.info({ mapName, added: totalAdded, updated: totalUpdated }, 'Synced ORMap records from server');\n }\n\n // Now push any local records that server might not have\n const keysToCheck = entries.map((e: { key: string }) => e.key);\n await this.pushORMapDiff(mapName, keysToCheck, map);\n }\n break;\n }\n\n case 'ORMAP_DIFF_RESPONSE': {\n const { mapName, entries } = message.payload;\n const map = this.maps.get(mapName);\n if (map instanceof ORMap) {\n let totalAdded = 0;\n let totalUpdated = 0;\n\n for (const entry of entries) {\n const { key, records, tombstones } = entry;\n const result = map.mergeKey(key, records, tombstones);\n totalAdded += result.added;\n totalUpdated += result.updated;\n }\n\n if (totalAdded > 0 || totalUpdated > 0) {\n logger.info({ mapName, added: totalAdded, updated: totalUpdated }, 'Merged ORMap diff from server');\n }\n }\n break;\n }\n }\n\n if (message.timestamp) {\n this.hlc.update(message.timestamp);\n this.lastSyncTimestamp = message.timestamp.millis;\n await this.saveOpLog();\n }\n }\n\n public getHLC(): HLC {\n return this.hlc;\n }\n\n /**\n * Helper method to apply a single server event to the local map.\n * Used by both SERVER_EVENT and SERVER_BATCH_EVENT handlers.\n */\n private async applyServerEvent(\n mapName: string,\n eventType: string,\n key: string,\n record?: any,\n orRecord?: any,\n orTag?: string\n ): Promise<void> {\n const localMap = this.maps.get(mapName);\n if (localMap) {\n if (localMap instanceof LWWMap && record) {\n localMap.merge(key, record);\n await this.storageAdapter.put(`${mapName}:${key}`, record);\n } else if (localMap instanceof ORMap) {\n if (eventType === 'OR_ADD' && orRecord) {\n localMap.apply(key, orRecord);\n // We need to store ORMap records differently in storageAdapter or use a convention\n // For now, skipping persistent storage update for ORMap in this example\n } else if (eventType === 'OR_REMOVE' && orTag) {\n localMap.applyTombstone(orTag);\n }\n }\n }\n }\n\n /**\n * Closes the WebSocket connection and cleans up resources.\n */\n public close(): void {\n this.stopHeartbeat();\n\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = null;\n }\n\n if (this.useConnectionProvider) {\n // Close via connection provider\n this.connectionProvider.close().catch((err) => {\n logger.error({ err }, 'Error closing ConnectionProvider');\n });\n } else if (this.websocket) {\n // Legacy: close direct websocket\n this.websocket.onclose = null; // Prevent reconnect on intentional close\n this.websocket.close();\n this.websocket = null;\n }\n\n // Cancel pending Write Concern promises (Phase 5.01)\n this.cancelAllWriteConcernPromises(new Error('SyncEngine closed'));\n\n this.stateMachine.transition(SyncState.DISCONNECTED);\n logger.info('SyncEngine closed');\n }\n\n /**\n * Reset the state machine and connection.\n * Use after fatal errors to start fresh.\n */\n public resetConnection(): void {\n this.close();\n this.stateMachine.reset();\n this.resetBackoff();\n if (this.useConnectionProvider) {\n this.initConnectionProvider();\n } else {\n this.initConnection();\n }\n }\n\n // ============================================\n // Failover Support Methods (Phase 4.5 Task 05)\n // ============================================\n\n /**\n * Wait for a partition map update from the connection provider.\n * Used when an operation fails with NOT_OWNER error and needs\n * to wait for an updated partition map before retrying.\n *\n * @param timeoutMs - Maximum time to wait (default: 5000ms)\n * @returns Promise that resolves when partition map is updated or times out\n */\n public waitForPartitionMapUpdate(timeoutMs: number = 5000): Promise<void> {\n return new Promise((resolve) => {\n const timeout = setTimeout(resolve, timeoutMs);\n\n const handler = () => {\n clearTimeout(timeout);\n this.connectionProvider.off('partitionMapUpdated', handler);\n resolve();\n };\n\n this.connectionProvider.on('partitionMapUpdated', handler);\n });\n }\n\n /**\n * Wait for the connection to be available.\n * Used when an operation fails due to connection issues and needs\n * to wait for reconnection before retrying.\n *\n * @param timeoutMs - Maximum time to wait (default: 10000ms)\n * @returns Promise that resolves when connected or rejects on timeout\n */\n public waitForConnection(timeoutMs: number = 10000): Promise<void> {\n return new Promise((resolve, reject) => {\n // If already connected, resolve immediately\n if (this.connectionProvider.isConnected()) {\n resolve();\n return;\n }\n\n const timeout = setTimeout(() => {\n this.connectionProvider.off('connected', handler);\n reject(new Error('Connection timeout waiting for reconnection'));\n }, timeoutMs);\n\n const handler = () => {\n clearTimeout(timeout);\n this.connectionProvider.off('connected', handler);\n resolve();\n };\n\n this.connectionProvider.on('connected', handler);\n });\n }\n\n /**\n * Wait for a specific sync state.\n * Useful for waiting until fully connected and synced.\n *\n * @param targetState - The state to wait for\n * @param timeoutMs - Maximum time to wait (default: 30000ms)\n * @returns Promise that resolves when state is reached or rejects on timeout\n */\n public waitForState(targetState: SyncState, timeoutMs: number = 30000): Promise<void> {\n return new Promise((resolve, reject) => {\n // If already in target state, resolve immediately\n if (this.stateMachine.getState() === targetState) {\n resolve();\n return;\n }\n\n const timeout = setTimeout(() => {\n unsubscribe();\n reject(new Error(`Timeout waiting for state ${targetState}`));\n }, timeoutMs);\n\n const unsubscribe = this.stateMachine.onStateChange((event) => {\n if (event.to === targetState) {\n clearTimeout(timeout);\n unsubscribe();\n resolve();\n }\n });\n });\n }\n\n /**\n * Check if the connection provider is connected.\n * Convenience method for failover logic.\n */\n public isProviderConnected(): boolean {\n return this.connectionProvider.isConnected();\n }\n\n /**\n * Get the connection provider for direct access.\n * Use with caution - prefer using SyncEngine methods.\n */\n public getConnectionProvider(): IConnectionProvider {\n return this.connectionProvider;\n }\n\n private async resetMap(mapName: string): Promise<void> {\n const map = this.maps.get(mapName);\n if (map) {\n // Clear memory\n if (map instanceof LWWMap) {\n map.clear();\n } else if (map instanceof ORMap) {\n map.clear();\n }\n }\n\n // Clear storage\n const allKeys = await this.storageAdapter.getAllKeys();\n const mapKeys = allKeys.filter(k => k.startsWith(mapName + ':'));\n for (const key of mapKeys) {\n await this.storageAdapter.remove(key);\n }\n logger.info({ mapName, removedStorageCount: mapKeys.length }, 'Reset map: Cleared memory and storage');\n }\n\n // ============ Heartbeat Methods ============\n\n /**\n * Starts the heartbeat mechanism after successful connection.\n */\n private startHeartbeat(): void {\n if (!this.heartbeatConfig.enabled) {\n return;\n }\n\n this.stopHeartbeat(); // Clear any existing interval\n this.lastPongReceived = Date.now();\n\n this.heartbeatInterval = setInterval(() => {\n this.sendPing();\n this.checkHeartbeatTimeout();\n }, this.heartbeatConfig.intervalMs);\n\n logger.info({ intervalMs: this.heartbeatConfig.intervalMs }, 'Heartbeat started');\n }\n\n /**\n * Stops the heartbeat mechanism.\n */\n private stopHeartbeat(): void {\n if (this.heartbeatInterval) {\n clearInterval(this.heartbeatInterval);\n this.heartbeatInterval = null;\n logger.info('Heartbeat stopped');\n }\n }\n\n /**\n * Sends a PING message to the server.\n */\n private sendPing(): void {\n if (this.canSend()) {\n const pingMessage = {\n type: 'PING',\n timestamp: Date.now(),\n };\n this.sendMessage(pingMessage);\n }\n }\n\n /**\n * Handles incoming PONG message from server.\n */\n private handlePong(msg: { timestamp: number; serverTime: number }): void {\n const now = Date.now();\n this.lastPongReceived = now;\n this.lastRoundTripTime = now - msg.timestamp;\n\n logger.debug({\n rtt: this.lastRoundTripTime,\n serverTime: msg.serverTime,\n clockSkew: msg.serverTime - (msg.timestamp + this.lastRoundTripTime / 2),\n }, 'Received PONG');\n }\n\n /**\n * Checks if heartbeat has timed out and triggers reconnection if needed.\n */\n private checkHeartbeatTimeout(): void {\n const now = Date.now();\n const timeSinceLastPong = now - this.lastPongReceived;\n\n if (timeSinceLastPong > this.heartbeatConfig.timeoutMs) {\n logger.warn({\n timeSinceLastPong,\n timeoutMs: this.heartbeatConfig.timeoutMs,\n }, 'Heartbeat timeout - triggering reconnection');\n\n this.stopHeartbeat();\n\n // Force close and reconnect\n if (this.websocket) {\n this.websocket.close();\n }\n }\n }\n\n /**\n * Returns the last measured round-trip time in milliseconds.\n * Returns null if no PONG has been received yet.\n */\n public getLastRoundTripTime(): number | null {\n return this.lastRoundTripTime;\n }\n\n /**\n * Returns true if the connection is considered healthy based on heartbeat.\n * A connection is healthy if it's online, authenticated, and has received\n * a PONG within the timeout window.\n */\n public isConnectionHealthy(): boolean {\n if (!this.isOnline() || !this.isAuthenticated()) {\n return false;\n }\n\n if (!this.heartbeatConfig.enabled) {\n return true; // If heartbeat disabled, consider healthy if online\n }\n\n const timeSinceLastPong = Date.now() - this.lastPongReceived;\n return timeSinceLastPong < this.heartbeatConfig.timeoutMs;\n }\n\n // ============ ORMap Sync Methods ============\n\n /**\n * Push local ORMap diff to server for the given keys.\n * Sends local records and tombstones that the server might not have.\n */\n private async pushORMapDiff(\n mapName: string,\n keys: string[],\n map: ORMap<any, any>\n ): Promise<void> {\n const entries: Array<{\n key: string;\n records: ORMapRecord<any>[];\n tombstones: string[];\n }> = [];\n\n const snapshot = map.getSnapshot();\n\n for (const key of keys) {\n const recordsMap = map.getRecordsMap(key);\n if (recordsMap && recordsMap.size > 0) {\n // Get records as array\n const records = Array.from(recordsMap.values());\n\n // Get tombstones relevant to this key's records\n // (tombstones that match tags that were in this key)\n const tombstones: string[] = [];\n for (const tag of snapshot.tombstones) {\n // Include all tombstones - server will filter\n tombstones.push(tag);\n }\n\n entries.push({\n key,\n records,\n tombstones\n });\n }\n }\n\n if (entries.length > 0) {\n this.sendMessage({\n type: 'ORMAP_PUSH_DIFF',\n payload: {\n mapName,\n entries\n }\n });\n logger.debug({ mapName, keyCount: entries.length }, 'Pushed ORMap diff to server');\n }\n }\n\n // ============ Backpressure Methods ============\n\n /**\n * Get the current number of pending (unsynced) operations.\n */\n public getPendingOpsCount(): number {\n return this.opLog.filter(op => !op.synced).length;\n }\n\n /**\n * Get the current backpressure status.\n */\n public getBackpressureStatus(): BackpressureStatus {\n const pending = this.getPendingOpsCount();\n const max = this.backpressureConfig.maxPendingOps;\n return {\n pending,\n max,\n percentage: max > 0 ? pending / max : 0,\n isPaused: this.backpressurePaused,\n strategy: this.backpressureConfig.strategy,\n };\n }\n\n /**\n * Returns true if writes are currently paused due to backpressure.\n */\n public isBackpressurePaused(): boolean {\n return this.backpressurePaused;\n }\n\n /**\n * Subscribe to backpressure events.\n * @param event Event name: 'backpressure:high', 'backpressure:low', 'backpressure:paused', 'backpressure:resumed', 'operation:dropped'\n * @param listener Callback function\n * @returns Unsubscribe function\n */\n public onBackpressure(\n event: 'backpressure:high' | 'backpressure:low' | 'backpressure:paused' | 'backpressure:resumed' | 'operation:dropped',\n listener: (data?: BackpressureThresholdEvent | OperationDroppedEvent) => void\n ): () => void {\n if (!this.backpressureListeners.has(event)) {\n this.backpressureListeners.set(event, new Set());\n }\n this.backpressureListeners.get(event)!.add(listener);\n\n return () => {\n this.backpressureListeners.get(event)?.delete(listener);\n };\n }\n\n /**\n * Emit a backpressure event to all listeners.\n */\n private emitBackpressureEvent(\n event: 'backpressure:high' | 'backpressure:low' | 'backpressure:paused' | 'backpressure:resumed' | 'operation:dropped',\n data?: BackpressureThresholdEvent | OperationDroppedEvent\n ): void {\n const listeners = this.backpressureListeners.get(event);\n if (listeners) {\n for (const listener of listeners) {\n try {\n listener(data);\n } catch (err) {\n logger.error({ err, event }, 'Error in backpressure event listener');\n }\n }\n }\n }\n\n /**\n * Check backpressure before adding a new operation.\n * May pause, throw, or drop depending on strategy.\n */\n private async checkBackpressure(): Promise<void> {\n const pendingCount = this.getPendingOpsCount();\n\n if (pendingCount < this.backpressureConfig.maxPendingOps) {\n return; // Capacity available\n }\n\n switch (this.backpressureConfig.strategy) {\n case 'pause':\n await this.waitForCapacity();\n break;\n case 'throw':\n throw new BackpressureError(\n pendingCount,\n this.backpressureConfig.maxPendingOps\n );\n case 'drop-oldest':\n this.dropOldestOp();\n break;\n }\n }\n\n /**\n * Check high water mark and emit event if threshold reached.\n */\n private checkHighWaterMark(): void {\n const pendingCount = this.getPendingOpsCount();\n const threshold = Math.floor(\n this.backpressureConfig.maxPendingOps * this.backpressureConfig.highWaterMark\n );\n\n if (pendingCount >= threshold && !this.highWaterMarkEmitted) {\n this.highWaterMarkEmitted = true;\n logger.warn(\n { pending: pendingCount, max: this.backpressureConfig.maxPendingOps },\n 'Backpressure high water mark reached'\n );\n this.emitBackpressureEvent('backpressure:high', {\n pending: pendingCount,\n max: this.backpressureConfig.maxPendingOps,\n });\n }\n }\n\n /**\n * Check low water mark and resume paused writes if threshold reached.\n */\n private checkLowWaterMark(): void {\n const pendingCount = this.getPendingOpsCount();\n const lowThreshold = Math.floor(\n this.backpressureConfig.maxPendingOps * this.backpressureConfig.lowWaterMark\n );\n const highThreshold = Math.floor(\n this.backpressureConfig.maxPendingOps * this.backpressureConfig.highWaterMark\n );\n\n // Reset high water mark flag when below high threshold\n if (pendingCount < highThreshold && this.highWaterMarkEmitted) {\n this.highWaterMarkEmitted = false;\n }\n\n // Emit low water mark event when crossing below threshold\n if (pendingCount <= lowThreshold) {\n if (this.backpressurePaused) {\n this.backpressurePaused = false;\n logger.info(\n { pending: pendingCount, max: this.backpressureConfig.maxPendingOps },\n 'Backpressure low water mark reached, resuming writes'\n );\n this.emitBackpressureEvent('backpressure:low', {\n pending: pendingCount,\n max: this.backpressureConfig.maxPendingOps,\n });\n this.emitBackpressureEvent('backpressure:resumed');\n\n // Resume all waiting writes\n const waiting = this.waitingForCapacity;\n this.waitingForCapacity = [];\n for (const resolve of waiting) {\n resolve();\n }\n }\n }\n }\n\n /**\n * Wait for capacity to become available (used by 'pause' strategy).\n */\n private async waitForCapacity(): Promise<void> {\n if (!this.backpressurePaused) {\n this.backpressurePaused = true;\n logger.warn('Backpressure paused - waiting for capacity');\n this.emitBackpressureEvent('backpressure:paused');\n }\n\n return new Promise<void>((resolve) => {\n this.waitingForCapacity.push(resolve);\n });\n }\n\n /**\n * Drop the oldest pending operation (used by 'drop-oldest' strategy).\n */\n private dropOldestOp(): void {\n // Find oldest unsynced operation by array order (oldest first)\n const oldestIndex = this.opLog.findIndex(op => !op.synced);\n\n if (oldestIndex !== -1) {\n const dropped = this.opLog[oldestIndex];\n this.opLog.splice(oldestIndex, 1);\n\n logger.warn(\n { opId: dropped.id, mapName: dropped.mapName, key: dropped.key },\n 'Dropped oldest pending operation due to backpressure'\n );\n\n this.emitBackpressureEvent('operation:dropped', {\n opId: dropped.id,\n mapName: dropped.mapName,\n opType: dropped.opType,\n key: dropped.key,\n });\n }\n }\n\n // ============================================\n // Write Concern Methods (Phase 5.01)\n // ============================================\n\n /**\n * Register a pending Write Concern promise for an operation.\n * The promise will be resolved when the server sends an ACK with the operation result.\n *\n * @param opId - Operation ID\n * @param timeout - Timeout in ms (default: 5000)\n * @returns Promise that resolves with the Write Concern result\n */\n public registerWriteConcernPromise(opId: string, timeout: number = 5000): Promise<any> {\n return new Promise((resolve, reject) => {\n const timeoutHandle = setTimeout(() => {\n this.pendingWriteConcernPromises.delete(opId);\n reject(new Error(`Write Concern timeout for operation ${opId}`));\n }, timeout);\n\n this.pendingWriteConcernPromises.set(opId, {\n resolve,\n reject,\n timeoutHandle,\n });\n });\n }\n\n /**\n * Resolve a pending Write Concern promise with the server result.\n *\n * @param opId - Operation ID\n * @param result - Result from server ACK\n */\n private resolveWriteConcernPromise(opId: string, result: any): void {\n const pending = this.pendingWriteConcernPromises.get(opId);\n if (pending) {\n if (pending.timeoutHandle) {\n clearTimeout(pending.timeoutHandle);\n }\n pending.resolve(result);\n this.pendingWriteConcernPromises.delete(opId);\n }\n }\n\n /**\n * Cancel all pending Write Concern promises (e.g., on disconnect).\n */\n private cancelAllWriteConcernPromises(error: Error): void {\n for (const [opId, pending] of this.pendingWriteConcernPromises.entries()) {\n if (pending.timeoutHandle) {\n clearTimeout(pending.timeoutHandle);\n }\n pending.reject(error);\n }\n this.pendingWriteConcernPromises.clear();\n }\n}\n","import pino from 'pino';\n\n// Simple check for browser environment\nconst isBrowser = typeof window !== 'undefined';\n\n// In browser, we might not have process.env, so we default to 'info'\n// Users can configure this via window.LOG_LEVEL or similar if needed,\n// but for now we stick to a safe default.\nconst logLevel = (typeof process !== 'undefined' && process.env && process.env.LOG_LEVEL) || 'info';\n\nexport const logger = pino({\n level: logLevel,\n transport: !isBrowser && (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production') ? {\n target: 'pino-pretty',\n options: {\n colorize: true,\n translateTime: 'SYS:standard',\n ignore: 'pid,hostname'\n }\n } : undefined,\n browser: {\n asObject: true\n }\n});\n\nexport type Logger = typeof logger;\n\n","/**\n * Defines the possible states for the SyncEngine connection state machine.\n */\nexport enum SyncState {\n /** Initial state before any connection attempt */\n INITIAL = 'INITIAL',\n /** WebSocket connection is being established */\n CONNECTING = 'CONNECTING',\n /** Connected, waiting for authentication response */\n AUTHENTICATING = 'AUTHENTICATING',\n /** Authenticated, performing initial data sync */\n SYNCING = 'SYNCING',\n /** Fully connected and synchronized */\n CONNECTED = 'CONNECTED',\n /** Intentionally or unexpectedly disconnected */\n DISCONNECTED = 'DISCONNECTED',\n /** Waiting before retry (exponential backoff) */\n BACKOFF = 'BACKOFF',\n /** Fatal error requiring manual intervention or reset */\n ERROR = 'ERROR',\n}\n\n/**\n * Defines valid state transitions for the SyncEngine FSM.\n * Each key is a current state, and the value is an array of valid target states.\n */\nexport const VALID_TRANSITIONS: Record<SyncState, SyncState[]> = {\n [SyncState.INITIAL]: [SyncState.CONNECTING],\n [SyncState.CONNECTING]: [SyncState.AUTHENTICATING, SyncState.BACKOFF, SyncState.ERROR, SyncState.DISCONNECTED],\n [SyncState.AUTHENTICATING]: [SyncState.SYNCING, SyncState.BACKOFF, SyncState.ERROR, SyncState.DISCONNECTED],\n [SyncState.SYNCING]: [SyncState.CONNECTED, SyncState.BACKOFF, SyncState.ERROR, SyncState.DISCONNECTED],\n [SyncState.CONNECTED]: [SyncState.SYNCING, SyncState.DISCONNECTED, SyncState.BACKOFF],\n [SyncState.DISCONNECTED]: [SyncState.CONNECTING, SyncState.BACKOFF, SyncState.INITIAL],\n [SyncState.BACKOFF]: [SyncState.CONNECTING, SyncState.DISCONNECTED, SyncState.INITIAL],\n [SyncState.ERROR]: [SyncState.INITIAL],\n};\n\n/**\n * Helper function to check if a transition is valid\n */\nexport function isValidTransition(from: SyncState, to: SyncState): boolean {\n return VALID_TRANSITIONS[from]?.includes(to) ?? false;\n}\n","import { SyncState, isValidTransition } from './SyncState';\nimport { logger } from './utils/logger';\n\n/**\n * Event emitted when the state machine transitions between states.\n */\nexport interface StateChangeEvent {\n /** The state before the transition */\n from: SyncState;\n /** The state after the transition */\n to: SyncState;\n /** Unix timestamp (ms) when the transition occurred */\n timestamp: number;\n}\n\n/**\n * Listener callback for state change events.\n */\nexport type StateChangeListener = (event: StateChangeEvent) => void;\n\n/**\n * Configuration options for the state machine.\n */\nexport interface SyncStateMachineConfig {\n /** Maximum number of state transitions to keep in history (default: 50) */\n maxHistorySize?: number;\n}\n\nconst DEFAULT_MAX_HISTORY_SIZE = 50;\n\n/**\n * A finite state machine for managing SyncEngine connection states.\n *\n * Features:\n * - Validates all state transitions against allowed paths\n * - Emits events on state changes for observability\n * - Maintains a history of transitions for debugging\n * - Logs invalid transition attempts (graceful degradation)\n */\nexport class SyncStateMachine {\n private state: SyncState = SyncState.INITIAL;\n private readonly listeners: Set<StateChangeListener> = new Set();\n private history: StateChangeEvent[] = [];\n private readonly maxHistorySize: number;\n\n constructor(config: SyncStateMachineConfig = {}) {\n this.maxHistorySize = config.maxHistorySize ?? DEFAULT_MAX_HISTORY_SIZE;\n }\n\n /**\n * Attempt to transition to a new state.\n * @param to The target state\n * @returns true if the transition was valid and executed, false otherwise\n */\n transition(to: SyncState): boolean {\n const from = this.state;\n\n if (from === to) {\n // No-op: already in target state\n return true;\n }\n\n if (!isValidTransition(from, to)) {\n logger.warn(\n { from, to, currentHistory: this.getHistory(5) },\n `Invalid state transition attempted: ${from} → ${to}`\n );\n return false;\n }\n\n // Execute the transition\n this.state = to;\n\n const event: StateChangeEvent = {\n from,\n to,\n timestamp: Date.now(),\n };\n\n // Add to history\n this.history.push(event);\n if (this.history.length > this.maxHistorySize) {\n this.history.shift();\n }\n\n // Notify listeners\n for (const listener of this.listeners) {\n try {\n listener(event);\n } catch (err) {\n logger.error({ err, event }, 'State change listener threw an error');\n }\n }\n\n logger.debug({ from, to }, `State transition: ${from} → ${to}`);\n\n return true;\n }\n\n /**\n * Get the current state.\n */\n getState(): SyncState {\n return this.state;\n }\n\n /**\n * Check if a transition from the current state to the target state is valid.\n * @param to The target state to check\n */\n canTransition(to: SyncState): boolean {\n return this.state === to || isValidTransition(this.state, to);\n }\n\n /**\n * Subscribe to state change events.\n * @param listener Callback function to be called on each state change\n * @returns An unsubscribe function\n */\n onStateChange(listener: StateChangeListener): () => void {\n this.listeners.add(listener);\n return () => {\n this.listeners.delete(listener);\n };\n }\n\n /**\n * Get the state transition history.\n * @param limit Maximum number of entries to return (default: all)\n * @returns Array of state change events, oldest first\n */\n getHistory(limit?: number): StateChangeEvent[] {\n if (limit === undefined || limit >= this.history.length) {\n return [...this.history];\n }\n return this.history.slice(-limit);\n }\n\n /**\n * Reset the state machine to INITIAL state.\n * This is a forced reset that bypasses normal transition validation.\n * Use for testing or hard resets after fatal errors.\n * @param clearHistory If true, also clears the transition history (default: true)\n */\n reset(clearHistory = true): void {\n const from = this.state;\n this.state = SyncState.INITIAL;\n\n if (clearHistory) {\n this.history = [];\n } else {\n // Record the reset as a transition\n const event: StateChangeEvent = {\n from,\n to: SyncState.INITIAL,\n timestamp: Date.now(),\n };\n this.history.push(event);\n if (this.history.length > this.maxHistorySize) {\n this.history.shift();\n }\n\n // Notify listeners\n for (const listener of this.listeners) {\n try {\n listener(event);\n } catch (err) {\n logger.error({ err, event }, 'State change listener threw an error during reset');\n }\n }\n }\n\n logger.info({ from }, 'State machine reset to INITIAL');\n }\n\n /**\n * Check if the state machine is in a \"connected\" state\n * (either SYNCING or CONNECTED)\n */\n isConnected(): boolean {\n return this.state === SyncState.CONNECTED || this.state === SyncState.SYNCING;\n }\n\n /**\n * Check if the state machine is in a state where operations can be sent\n * (authenticated and connected)\n */\n isReady(): boolean {\n return this.state === SyncState.CONNECTED;\n }\n\n /**\n * Check if the state machine is currently attempting to connect\n */\n isConnecting(): boolean {\n return (\n this.state === SyncState.CONNECTING ||\n this.state === SyncState.AUTHENTICATING ||\n this.state === SyncState.SYNCING\n );\n }\n}\n","/**\n * Error thrown when backpressure limit is reached and strategy is 'throw'.\n */\nexport class BackpressureError extends Error {\n public readonly name = 'BackpressureError';\n\n constructor(\n public readonly pendingCount: number,\n public readonly maxPending: number\n ) {\n super(\n `Backpressure limit reached: ${pendingCount}/${maxPending} pending operations. ` +\n `Wait for acknowledgments or increase maxPendingOps.`\n );\n\n // Maintains proper stack trace for where error was thrown (only available on V8)\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, BackpressureError);\n }\n }\n}\n","/**\n * Backpressure strategy when maxPendingOps is reached.\n */\nexport type BackpressureStrategy = 'pause' | 'throw' | 'drop-oldest';\n\n/**\n * Configuration for backpressure control on SyncEngine.\n */\nexport interface BackpressureConfig {\n /**\n * Maximum number of operations waiting for server acknowledgment.\n * When this limit is reached, the configured strategy will be applied.\n * @default 1000\n */\n maxPendingOps: number;\n\n /**\n * Strategy when maxPendingOps is reached:\n * - 'pause': Wait for capacity (returns Promise that resolves when space available)\n * - 'throw': Throw BackpressureError immediately\n * - 'drop-oldest': Remove oldest pending op to make room (data loss!)\n * @default 'pause'\n */\n strategy: BackpressureStrategy;\n\n /**\n * High water mark (percentage of maxPendingOps).\n * Emit 'backpressure:high' event when reached.\n * Value should be between 0 and 1.\n * @default 0.8 (80%)\n */\n highWaterMark: number;\n\n /**\n * Low water mark (percentage of maxPendingOps).\n * Resume paused writes and emit 'backpressure:low' when pending ops drop below this.\n * Value should be between 0 and 1.\n * @default 0.5 (50%)\n */\n lowWaterMark: number;\n}\n\n/**\n * Default backpressure configuration.\n */\nexport const DEFAULT_BACKPRESSURE_CONFIG: BackpressureConfig = {\n maxPendingOps: 1000,\n strategy: 'pause',\n highWaterMark: 0.8,\n lowWaterMark: 0.5,\n};\n\n/**\n * Status of backpressure mechanism.\n */\nexport interface BackpressureStatus {\n /** Current number of pending (unacknowledged) operations */\n pending: number;\n /** Maximum allowed pending operations */\n max: number;\n /** Percentage of capacity used (0-1) */\n percentage: number;\n /** Whether writes are currently paused due to backpressure */\n isPaused: boolean;\n /** Current backpressure strategy */\n strategy: BackpressureStrategy;\n}\n\n/**\n * Event data for backpressure:high and backpressure:low events.\n */\nexport interface BackpressureThresholdEvent {\n pending: number;\n max: number;\n}\n\n/**\n * Event data for operation:dropped event.\n */\nexport interface OperationDroppedEvent {\n opId: string;\n mapName: string;\n opType: string;\n key: string;\n}\n","import type {\n IConnectionProvider,\n ConnectionProviderEvent,\n ConnectionEventHandler,\n SingleServerProviderConfig,\n} from '../types';\nimport { logger } from '../utils/logger';\n\n/**\n * Default configuration values for SingleServerProvider.\n */\nconst DEFAULT_CONFIG = {\n maxReconnectAttempts: 10,\n reconnectDelayMs: 1000,\n backoffMultiplier: 2,\n maxReconnectDelayMs: 30000,\n};\n\n/**\n * SingleServerProvider implements IConnectionProvider for single-server mode.\n *\n * This is an adapter that wraps direct WebSocket connection handling,\n * providing the same interface used by ClusterClient for multi-node mode.\n */\nexport class SingleServerProvider implements IConnectionProvider {\n private readonly url: string;\n private readonly config: Required<SingleServerProviderConfig>;\n private ws: WebSocket | null = null;\n private reconnectAttempts: number = 0;\n private reconnectTimer: ReturnType<typeof setTimeout> | null = null;\n private isClosing: boolean = false;\n private listeners: Map<ConnectionProviderEvent, Set<ConnectionEventHandler>> = new Map();\n\n constructor(config: SingleServerProviderConfig) {\n this.url = config.url;\n this.config = {\n url: config.url,\n maxReconnectAttempts: config.maxReconnectAttempts ?? DEFAULT_CONFIG.maxReconnectAttempts,\n reconnectDelayMs: config.reconnectDelayMs ?? DEFAULT_CONFIG.reconnectDelayMs,\n backoffMultiplier: config.backoffMultiplier ?? DEFAULT_CONFIG.backoffMultiplier,\n maxReconnectDelayMs: config.maxReconnectDelayMs ?? DEFAULT_CONFIG.maxReconnectDelayMs,\n };\n }\n\n /**\n * Connect to the WebSocket server.\n */\n async connect(): Promise<void> {\n if (this.ws && this.ws.readyState === WebSocket.OPEN) {\n return; // Already connected\n }\n\n this.isClosing = false;\n\n return new Promise((resolve, reject) => {\n try {\n this.ws = new WebSocket(this.url);\n this.ws.binaryType = 'arraybuffer';\n\n this.ws.onopen = () => {\n this.reconnectAttempts = 0;\n logger.info({ url: this.url }, 'SingleServerProvider connected');\n this.emit('connected', 'default');\n resolve();\n };\n\n this.ws.onerror = (error) => {\n logger.error({ err: error, url: this.url }, 'SingleServerProvider WebSocket error');\n this.emit('error', error);\n // Don't reject here - wait for onclose\n };\n\n this.ws.onclose = (event) => {\n logger.info({ url: this.url, code: event.code }, 'SingleServerProvider disconnected');\n this.emit('disconnected', 'default');\n\n if (!this.isClosing) {\n this.scheduleReconnect();\n }\n };\n\n this.ws.onmessage = (event) => {\n this.emit('message', 'default', event.data);\n };\n\n // Set up initial connection timeout\n const timeoutId = setTimeout(() => {\n if (this.ws && this.ws.readyState === WebSocket.CONNECTING) {\n this.ws.close();\n reject(new Error(`Connection timeout to ${this.url}`));\n }\n }, this.config.reconnectDelayMs * 5); // 5x initial delay as connection timeout\n\n // Clear timeout on successful connection\n const originalOnOpen = this.ws.onopen;\n const wsRef = this.ws;\n this.ws.onopen = (ev) => {\n clearTimeout(timeoutId);\n if (originalOnOpen) {\n originalOnOpen.call(wsRef, ev);\n }\n };\n } catch (error) {\n reject(error);\n }\n });\n }\n\n /**\n * Get connection for a specific key.\n * In single-server mode, key is ignored.\n */\n getConnection(_key: string): WebSocket {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error('Not connected');\n }\n return this.ws;\n }\n\n /**\n * Get any available connection.\n */\n getAnyConnection(): WebSocket {\n return this.getConnection('');\n }\n\n /**\n * Check if connected.\n */\n isConnected(): boolean {\n return this.ws?.readyState === WebSocket.OPEN;\n }\n\n /**\n * Get connected node IDs.\n * Single-server mode returns ['default'] when connected.\n */\n getConnectedNodes(): string[] {\n return this.isConnected() ? ['default'] : [];\n }\n\n /**\n * Subscribe to connection events.\n */\n on(event: ConnectionProviderEvent, handler: ConnectionEventHandler): void {\n if (!this.listeners.has(event)) {\n this.listeners.set(event, new Set());\n }\n this.listeners.get(event)!.add(handler);\n }\n\n /**\n * Unsubscribe from connection events.\n */\n off(event: ConnectionProviderEvent, handler: ConnectionEventHandler): void {\n this.listeners.get(event)?.delete(handler);\n }\n\n /**\n * Send data via the WebSocket connection.\n * In single-server mode, key parameter is ignored.\n */\n send(data: ArrayBuffer | Uint8Array, _key?: string): void {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error('Not connected');\n }\n this.ws.send(data);\n }\n\n /**\n * Close the WebSocket connection.\n */\n async close(): Promise<void> {\n this.isClosing = true;\n\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = null;\n }\n\n if (this.ws) {\n // Remove onclose handler to prevent reconnect\n this.ws.onclose = null;\n this.ws.onerror = null;\n this.ws.onmessage = null;\n\n if (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING) {\n this.ws.close();\n }\n this.ws = null;\n }\n\n logger.info({ url: this.url }, 'SingleServerProvider closed');\n }\n\n /**\n * Emit an event to all listeners.\n */\n private emit(event: ConnectionProviderEvent, ...args: any[]): void {\n const handlers = this.listeners.get(event);\n if (handlers) {\n for (const handler of handlers) {\n try {\n handler(...args);\n } catch (err) {\n logger.error({ err, event }, 'Error in SingleServerProvider event handler');\n }\n }\n }\n }\n\n /**\n * Schedule a reconnection attempt with exponential backoff.\n */\n private scheduleReconnect(): void {\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = null;\n }\n\n if (this.reconnectAttempts >= this.config.maxReconnectAttempts) {\n logger.error(\n { attempts: this.reconnectAttempts, url: this.url },\n 'SingleServerProvider max reconnect attempts reached'\n );\n this.emit('error', new Error('Max reconnection attempts reached'));\n return;\n }\n\n const delay = this.calculateBackoffDelay();\n logger.info(\n { delay, attempt: this.reconnectAttempts, url: this.url },\n `SingleServerProvider scheduling reconnect in ${delay}ms`\n );\n\n this.reconnectTimer = setTimeout(async () => {\n this.reconnectTimer = null;\n this.reconnectAttempts++;\n\n try {\n await this.connect();\n this.emit('reconnected', 'default');\n } catch (error) {\n logger.error({ err: error }, 'SingleServerProvider reconnection failed');\n this.scheduleReconnect();\n }\n }, delay);\n }\n\n /**\n * Calculate backoff delay with exponential increase.\n */\n private calculateBackoffDelay(): number {\n const { reconnectDelayMs, backoffMultiplier, maxReconnectDelayMs } = this.config;\n let delay = reconnectDelayMs * Math.pow(backoffMultiplier, this.reconnectAttempts);\n delay = Math.min(delay, maxReconnectDelayMs);\n\n // Add jitter (0.5x to 1.5x)\n delay = delay * (0.5 + Math.random());\n\n return Math.floor(delay);\n }\n\n /**\n * Get the WebSocket URL this provider connects to.\n */\n getUrl(): string {\n return this.url;\n }\n\n /**\n * Get current reconnection attempt count.\n */\n getReconnectAttempts(): number {\n return this.reconnectAttempts;\n }\n\n /**\n * Reset reconnection counter.\n * Called externally after successful authentication.\n */\n resetReconnectAttempts(): void {\n this.reconnectAttempts = 0;\n }\n}\n","import { LWWMap, ORMap } from '@topgunbuild/core';\nimport type { ORMapRecord, LWWRecord } from '@topgunbuild/core';\nimport type { IStorageAdapter } from './IStorageAdapter';\nimport { SyncEngine } from './SyncEngine';\nimport type { BackoffConfig } from './SyncEngine';\nimport { QueryHandle } from './QueryHandle';\nimport type { QueryFilter } from './QueryHandle';\nimport { DistributedLock } from './DistributedLock';\nimport { TopicHandle } from './TopicHandle';\nimport { logger } from './utils/logger';\nimport { SyncState } from './SyncState';\nimport type { StateChangeEvent } from './SyncStateMachine';\nimport type {\n BackpressureConfig,\n BackpressureStatus,\n BackpressureThresholdEvent,\n OperationDroppedEvent,\n} from './BackpressureConfig';\nimport { ClusterClient } from './cluster/ClusterClient';\nimport type { NodeHealth } from '@topgunbuild/core';\n\n// ============================================\n// Cluster Configuration Types\n// ============================================\n\n/**\n * Cluster mode configuration for TopGunClient.\n * When provided, the client connects to multiple nodes with partition-aware routing.\n */\nexport interface TopGunClusterConfig {\n /** Initial seed nodes (at least one required) */\n seeds: string[];\n\n /** Connection pool size per node (default: 1) */\n connectionsPerNode?: number;\n\n /** Enable smart routing to partition owner (default: true) */\n smartRouting?: boolean;\n\n /** Partition map refresh interval in ms (default: 30000) */\n partitionMapRefreshMs?: number;\n\n /** Connection timeout per node in ms (default: 5000) */\n connectionTimeoutMs?: number;\n\n /** Retry attempts for failed operations (default: 3) */\n retryAttempts?: number;\n}\n\n/**\n * Default values for cluster configuration\n */\nexport const DEFAULT_CLUSTER_CONFIG: Required<Omit<TopGunClusterConfig, 'seeds'>> = {\n connectionsPerNode: 1,\n smartRouting: true,\n partitionMapRefreshMs: 30000,\n connectionTimeoutMs: 5000,\n retryAttempts: 3,\n};\n\n/**\n * TopGunClient configuration options\n */\nexport interface TopGunClientConfig {\n /** Unique node identifier (auto-generated if not provided) */\n nodeId?: string;\n\n /** Single-server mode: WebSocket URL to connect to */\n serverUrl?: string;\n\n /** Cluster mode: Configuration for multi-node routing */\n cluster?: TopGunClusterConfig;\n\n /** Storage adapter for local persistence */\n storage: IStorageAdapter;\n\n /** Backoff configuration for reconnection */\n backoff?: Partial<BackoffConfig>;\n\n /** Backpressure configuration */\n backpressure?: Partial<BackpressureConfig>;\n}\n\nexport class TopGunClient {\n private readonly nodeId: string;\n private readonly syncEngine: SyncEngine;\n private readonly maps: Map<string, LWWMap<any, any> | ORMap<any, any>> = new Map();\n private readonly storageAdapter: IStorageAdapter;\n private readonly topicHandles: Map<string, TopicHandle> = new Map();\n private readonly clusterClient?: ClusterClient;\n private readonly isClusterMode: boolean;\n private readonly clusterConfig?: Required<Omit<TopGunClusterConfig, 'seeds'>> & { seeds: string[] };\n\n constructor(config: TopGunClientConfig) {\n // Validate: either serverUrl or cluster, not both\n if (config.serverUrl && config.cluster) {\n throw new Error('Cannot specify both serverUrl and cluster config');\n }\n if (!config.serverUrl && !config.cluster) {\n throw new Error('Must specify either serverUrl or cluster config');\n }\n\n this.nodeId = config.nodeId || crypto.randomUUID();\n this.storageAdapter = config.storage;\n this.isClusterMode = !!config.cluster;\n\n if (config.cluster) {\n // Validate cluster seeds\n if (!config.cluster.seeds || config.cluster.seeds.length === 0) {\n throw new Error('Cluster config requires at least one seed node');\n }\n\n // Merge with defaults\n this.clusterConfig = {\n seeds: config.cluster.seeds,\n connectionsPerNode: config.cluster.connectionsPerNode ?? DEFAULT_CLUSTER_CONFIG.connectionsPerNode,\n smartRouting: config.cluster.smartRouting ?? DEFAULT_CLUSTER_CONFIG.smartRouting,\n partitionMapRefreshMs: config.cluster.partitionMapRefreshMs ?? DEFAULT_CLUSTER_CONFIG.partitionMapRefreshMs,\n connectionTimeoutMs: config.cluster.connectionTimeoutMs ?? DEFAULT_CLUSTER_CONFIG.connectionTimeoutMs,\n retryAttempts: config.cluster.retryAttempts ?? DEFAULT_CLUSTER_CONFIG.retryAttempts,\n };\n\n // Initialize cluster mode\n this.clusterClient = new ClusterClient({\n enabled: true,\n seedNodes: this.clusterConfig.seeds,\n routingMode: this.clusterConfig.smartRouting ? 'direct' : 'forward',\n connectionPool: {\n maxConnectionsPerNode: this.clusterConfig.connectionsPerNode,\n connectionTimeoutMs: this.clusterConfig.connectionTimeoutMs,\n },\n routing: {\n mapRefreshIntervalMs: this.clusterConfig.partitionMapRefreshMs,\n },\n });\n\n // SyncEngine uses ClusterClient as connectionProvider for partition-aware routing\n this.syncEngine = new SyncEngine({\n nodeId: this.nodeId,\n connectionProvider: this.clusterClient,\n storageAdapter: this.storageAdapter,\n backoff: config.backoff,\n backpressure: config.backpressure,\n });\n\n logger.info({ seeds: this.clusterConfig.seeds }, 'TopGunClient initialized in cluster mode');\n } else {\n // Single-server mode (existing behavior)\n this.syncEngine = new SyncEngine({\n nodeId: this.nodeId,\n serverUrl: config.serverUrl!,\n storageAdapter: this.storageAdapter,\n backoff: config.backoff,\n backpressure: config.backpressure,\n });\n\n logger.info({ serverUrl: config.serverUrl }, 'TopGunClient initialized in single-server mode');\n }\n }\n\n public async start(): Promise<void> {\n await this.storageAdapter.initialize('topgun_offline_db');\n // this.syncEngine.start();\n }\n\n public setAuthToken(token: string): void {\n this.syncEngine.setAuthToken(token);\n }\n\n public setAuthTokenProvider(provider: () => Promise<string | null>): void {\n this.syncEngine.setTokenProvider(provider);\n }\n\n /**\n * Creates a live query subscription for a map.\n */\n public query<T>(mapName: string, filter: QueryFilter): QueryHandle<T> {\n return new QueryHandle<T>(this.syncEngine, mapName, filter);\n }\n\n /**\n * Retrieves a distributed lock instance.\n * @param name The name of the lock.\n */\n public getLock(name: string): DistributedLock {\n return new DistributedLock(this.syncEngine, name);\n }\n\n /**\n * Retrieves a topic handle for Pub/Sub messaging.\n * @param name The name of the topic.\n */\n public topic(name: string): TopicHandle {\n if (!this.topicHandles.has(name)) {\n this.topicHandles.set(name, new TopicHandle(this.syncEngine, name));\n }\n return this.topicHandles.get(name)!;\n }\n\n /**\n * Retrieves an LWWMap instance. If the map doesn't exist locally, it's created.\n * @param name The name of the map.\n * @returns An LWWMap instance.\n */\n public getMap<K, V>(name: string): LWWMap<K, V> {\n if (this.maps.has(name)) {\n const map = this.maps.get(name);\n if (map instanceof LWWMap) {\n return map as LWWMap<K, V>;\n }\n throw new Error(`Map ${name} exists but is not an LWWMap`);\n }\n\n const lwwMap = new LWWMap<K, V>(this.syncEngine.getHLC());\n this.maps.set(name, lwwMap);\n this.syncEngine.registerMap(name, lwwMap);\n\n // Restore state from storage asynchronously\n this.storageAdapter.getAllKeys().then(async (keys) => {\n const mapPrefix = `${name}:`;\n for (const fullKey of keys) {\n if (fullKey.startsWith(mapPrefix)) {\n const record = await this.storageAdapter.get(fullKey);\n if (record && (record as LWWRecord<V>).timestamp && !(record as any).tag) {\n // Strip prefix to get actual key\n const key = fullKey.substring(mapPrefix.length) as unknown as K;\n // Merge into in-memory map without triggering new ops\n lwwMap.merge(key, record as LWWRecord<V>);\n }\n }\n }\n }).catch(err => logger.error({ err }, 'Failed to restore keys from storage'));\n\n // Wrap LWWMap with IMap interface logic\n const originalSet = lwwMap.set.bind(lwwMap);\n lwwMap.set = (key: K, value: V, ttlMs?: number) => {\n const record = originalSet(key, value, ttlMs);\n this.storageAdapter.put(`${name}:${key}`, record).catch(err => logger.error({ err }, 'Failed to put record to storage'));\n this.syncEngine.recordOperation(name, 'PUT', String(key), { record, timestamp: record.timestamp }).catch(err => logger.error({ err }, 'Failed to record PUT op'));\n return record;\n };\n\n const originalRemove = lwwMap.remove.bind(lwwMap);\n lwwMap.remove = (key: K) => {\n const tombstone = originalRemove(key);\n this.storageAdapter.put(`${name}:${key}`, tombstone).catch(err => logger.error({ err }, 'Failed to put tombstone to storage'));\n this.syncEngine.recordOperation(name, 'REMOVE', String(key), { record: tombstone, timestamp: tombstone.timestamp }).catch(err => logger.error({ err }, 'Failed to record REMOVE op'));\n return tombstone;\n };\n\n return lwwMap;\n }\n\n /**\n * Retrieves an ORMap instance. If the map doesn't exist locally, it's created.\n * @param name The name of the map.\n * @returns An ORMap instance.\n */\n public getORMap<K, V>(name: string): ORMap<K, V> {\n if (this.maps.has(name)) {\n const map = this.maps.get(name);\n if (map instanceof ORMap) {\n return map as ORMap<K, V>;\n }\n throw new Error(`Map ${name} exists but is not an ORMap`);\n }\n\n const orMap = new ORMap<K, V>(this.syncEngine.getHLC());\n this.maps.set(name, orMap);\n this.syncEngine.registerMap(name, orMap);\n\n // Restore state from storage\n this.restoreORMap(name, orMap);\n\n // Wrap ORMap methods to record operations\n const originalAdd = orMap.add.bind(orMap);\n orMap.add = (key: K, value: V, ttlMs?: number) => {\n const record = originalAdd(key, value, ttlMs);\n \n // Persist records\n this.persistORMapKey(name, orMap, key);\n\n this.syncEngine.recordOperation(name, 'OR_ADD', String(key), { orRecord: record, timestamp: record.timestamp }).catch(err => logger.error({ err }, 'Failed to record OR_ADD op'));\n return record;\n };\n\n const originalRemove = orMap.remove.bind(orMap);\n orMap.remove = (key: K, value: V) => {\n const tombstones = originalRemove(key, value);\n const timestamp = this.syncEngine.getHLC().now(); \n \n // Update storage for the key (items removed)\n this.persistORMapKey(name, orMap, key);\n // Update storage for tombstones\n this.persistORMapTombstones(name, orMap);\n\n for (const tag of tombstones) {\n this.syncEngine.recordOperation(name, 'OR_REMOVE', String(key), { orTag: tag, timestamp }).catch(err => logger.error({ err }, 'Failed to record OR_REMOVE op'));\n }\n return tombstones;\n };\n\n return orMap;\n }\n\n private async restoreORMap<K, V>(name: string, orMap: ORMap<K, V>) {\n try {\n // 1. Restore Tombstones\n const tombstoneKey = `__sys__:${name}:tombstones`;\n const tombstones = await this.storageAdapter.getMeta(tombstoneKey);\n if (Array.isArray(tombstones)) {\n for (const tag of tombstones) {\n orMap.applyTombstone(tag);\n }\n }\n\n // 2. Restore Items\n const keys = await this.storageAdapter.getAllKeys();\n const mapPrefix = `${name}:`;\n for (const fullKey of keys) {\n if (fullKey.startsWith(mapPrefix)) {\n const keyPart = fullKey.substring(mapPrefix.length);\n \n const data = await this.storageAdapter.get(fullKey);\n if (Array.isArray(data)) {\n // It's likely an ORMap value list (Array of ORMapRecord)\n const records = data as ORMapRecord<V>[];\n const key = keyPart as unknown as K;\n \n for (const record of records) {\n orMap.apply(key, record);\n }\n }\n }\n }\n } catch (e) {\n logger.error({ mapName: name, err: e }, 'Failed to restore ORMap');\n }\n }\n\n private async persistORMapKey<K, V>(mapName: string, orMap: ORMap<K, V>, key: K) {\n const records = orMap.getRecords(key);\n if (records.length > 0) {\n await this.storageAdapter.put(`${mapName}:${key}`, records);\n } else {\n await this.storageAdapter.remove(`${mapName}:${key}`);\n }\n }\n \n private async persistORMapTombstones<K, V>(mapName: string, orMap: ORMap<K, V>) {\n const tombstoneKey = `__sys__:${mapName}:tombstones`;\n const tombstones = orMap.getTombstones();\n await this.storageAdapter.setMeta(tombstoneKey, tombstones);\n }\n\n /**\n * Closes the client, disconnecting from the server and cleaning up resources.\n */\n public close(): void {\n if (this.clusterClient) {\n this.clusterClient.close();\n }\n this.syncEngine.close();\n }\n\n // ============================================\n // Cluster Mode API\n // ============================================\n\n /**\n * Check if running in cluster mode\n */\n public isCluster(): boolean {\n return this.isClusterMode;\n }\n\n /**\n * Get list of connected cluster nodes (cluster mode only)\n * @returns Array of connected node IDs, or empty array in single-server mode\n */\n public getConnectedNodes(): string[] {\n if (!this.clusterClient) return [];\n return this.clusterClient.getConnectedNodes();\n }\n\n /**\n * Get the current partition map version (cluster mode only)\n * @returns Partition map version, or 0 in single-server mode\n */\n public getPartitionMapVersion(): number {\n if (!this.clusterClient) return 0;\n return this.clusterClient.getRouterStats().mapVersion;\n }\n\n /**\n * Check if direct routing is active (cluster mode only)\n * Direct routing sends operations directly to partition owners.\n * @returns true if routing is active, false otherwise\n */\n public isRoutingActive(): boolean {\n if (!this.clusterClient) return false;\n return this.clusterClient.isRoutingActive();\n }\n\n /**\n * Get health status for all cluster nodes (cluster mode only)\n * @returns Map of node IDs to their health status\n */\n public getClusterHealth(): Map<string, NodeHealth> {\n if (!this.clusterClient) return new Map();\n return this.clusterClient.getHealthStatus();\n }\n\n /**\n * Force refresh of partition map (cluster mode only)\n * Use this after detecting routing errors.\n */\n public async refreshPartitionMap(): Promise<void> {\n if (!this.clusterClient) return;\n await this.clusterClient.refreshPartitionMap();\n }\n\n /**\n * Get cluster router statistics (cluster mode only)\n */\n public getClusterStats(): {\n mapVersion: number;\n partitionCount: number;\n nodeCount: number;\n lastRefresh: number;\n isStale: boolean;\n } | null {\n if (!this.clusterClient) return null;\n return this.clusterClient.getRouterStats();\n }\n\n // ============================================\n // Connection State API\n // ============================================\n\n /**\n * Get the current connection state\n */\n public getConnectionState(): SyncState {\n return this.syncEngine.getConnectionState();\n }\n\n /**\n * Subscribe to connection state changes\n * @param listener Callback function called on each state change\n * @returns Unsubscribe function\n */\n public onConnectionStateChange(listener: (event: StateChangeEvent) => void): () => void {\n return this.syncEngine.onConnectionStateChange(listener);\n }\n\n /**\n * Get state machine history for debugging\n * @param limit Maximum number of entries to return\n */\n public getStateHistory(limit?: number): StateChangeEvent[] {\n return this.syncEngine.getStateHistory(limit);\n }\n\n /**\n * Reset the connection and state machine.\n * Use after fatal errors to start fresh.\n */\n public resetConnection(): void {\n this.syncEngine.resetConnection();\n }\n\n // ============================================\n // Backpressure API\n // ============================================\n\n /**\n * Get the current number of pending (unacknowledged) operations.\n */\n public getPendingOpsCount(): number {\n return this.syncEngine.getPendingOpsCount();\n }\n\n /**\n * Get the current backpressure status.\n */\n public getBackpressureStatus(): BackpressureStatus {\n return this.syncEngine.getBackpressureStatus();\n }\n\n /**\n * Returns true if writes are currently paused due to backpressure.\n */\n public isBackpressurePaused(): boolean {\n return this.syncEngine.isBackpressurePaused();\n }\n\n /**\n * Subscribe to backpressure events.\n *\n * Available events:\n * - 'backpressure:high': Emitted when pending ops reach high water mark\n * - 'backpressure:low': Emitted when pending ops drop below low water mark\n * - 'backpressure:paused': Emitted when writes are paused (pause strategy)\n * - 'backpressure:resumed': Emitted when writes resume after being paused\n * - 'operation:dropped': Emitted when an operation is dropped (drop-oldest strategy)\n *\n * @param event Event name\n * @param listener Callback function\n * @returns Unsubscribe function\n *\n * @example\n * ```typescript\n * client.onBackpressure('backpressure:high', ({ pending, max }) => {\n * console.warn(`Warning: ${pending}/${max} pending ops`);\n * });\n *\n * client.onBackpressure('backpressure:paused', () => {\n * showLoadingSpinner();\n * });\n *\n * client.onBackpressure('backpressure:resumed', () => {\n * hideLoadingSpinner();\n * });\n * ```\n */\n public onBackpressure(\n event: 'backpressure:high' | 'backpressure:low' | 'backpressure:paused' | 'backpressure:resumed' | 'operation:dropped',\n listener: (data?: BackpressureThresholdEvent | OperationDroppedEvent) => void\n ): () => void {\n return this.syncEngine.onBackpressure(event, listener);\n }\n}\n","import { SyncEngine } from './SyncEngine';\nimport type { PredicateNode } from '@topgunbuild/core';\n\nexport interface QueryFilter {\n where?: Record<string, any>;\n predicate?: PredicateNode;\n sort?: Record<string, 'asc' | 'desc'>;\n limit?: number;\n offset?: number;\n}\n\n/** Source of query results for proper handling of race conditions */\nexport type QueryResultSource = 'local' | 'server';\n\n/** Result item with _key field for client-side lookups */\nexport type QueryResultItem<T> = T & { _key: string };\n\nexport class QueryHandle<T> {\n public readonly id: string;\n private syncEngine: SyncEngine;\n private mapName: string;\n private filter: QueryFilter;\n private listeners: Set<(results: QueryResultItem<T>[]) => void> = new Set();\n private currentResults: Map<string, T> = new Map();\n\n constructor(syncEngine: SyncEngine, mapName: string, filter: QueryFilter = {}) {\n this.id = crypto.randomUUID();\n this.syncEngine = syncEngine;\n this.mapName = mapName;\n this.filter = filter;\n }\n\n public subscribe(callback: (results: QueryResultItem<T>[]) => void): () => void {\n this.listeners.add(callback);\n \n // If this is the first listener, activate subscription\n if (this.listeners.size === 1) {\n this.syncEngine.subscribeToQuery(this);\n } else {\n // Immediately invoke with cached results\n callback(this.getSortedResults());\n }\n \n // [FIX]: Attempt to load local results immediately if available\n // This ensures that if data is already in storage but sync hasn't happened,\n // we still show something.\n this.loadInitialLocalData().then(data => {\n // If we haven't received server results yet (currentResults empty),\n // and we have local data OR it's just the initial load, we should notify.\n // Even if data is empty, we might want to tell the subscriber \"nothing here yet\".\n if (this.currentResults.size === 0) {\n this.onResult(data, 'local');\n }\n });\n\n return () => {\n this.listeners.delete(callback);\n if (this.listeners.size === 0) {\n this.syncEngine.unsubscribeFromQuery(this.id);\n }\n };\n }\n\n private async loadInitialLocalData() {\n // This requires SyncEngine to expose a method to query local storage\n // For now, we can't easily reach storageAdapter directly from here without leaking abstraction.\n // A better approach is for SyncEngine.subscribeToQuery to trigger a local load.\n return this.syncEngine.runLocalQuery(this.mapName, this.filter);\n }\n\n // Track if we've received authoritative server response\n private hasReceivedServerData: boolean = false;\n\n /**\n * Called by SyncEngine when server sends initial results or by local storage load.\n * Uses merge strategy instead of clear to prevent UI flickering.\n *\n * @param items - Array of key-value pairs\n * @param source - 'local' for IndexedDB data, 'server' for QUERY_RESP from server\n *\n * Race condition protection:\n * - Empty server responses are ignored until we receive non-empty server data\n * - This prevents clearing local data when server hasn't loaded from storage yet\n * - Works with any async storage adapter (PostgreSQL, SQLite, Redis, etc.)\n */\n public onResult(items: { key: string, value: T }[], source: QueryResultSource = 'server') {\n console.log(`[QueryHandle:${this.mapName}] onResult called with ${items.length} items`, {\n source,\n currentResultsCount: this.currentResults.size,\n newItemKeys: items.map(i => i.key),\n hasReceivedServerData: this.hasReceivedServerData\n });\n\n // [FIX] Race condition protection for any async storage adapter:\n // If server sends empty QUERY_RESP before loading data from storage,\n // we ignore it to prevent clearing valid local data.\n // This is safe because:\n // 1. If server truly has no data, next non-empty response will clear local-only items\n // 2. If server is still loading, we preserve local data until real data arrives\n if (source === 'server' && items.length === 0 && !this.hasReceivedServerData) {\n console.log(`[QueryHandle:${this.mapName}] Ignoring empty server response - waiting for authoritative data`);\n return;\n }\n\n // Mark that we've received authoritative server data (non-empty from server)\n if (source === 'server' && items.length > 0) {\n this.hasReceivedServerData = true;\n }\n\n const newKeys = new Set(items.map(i => i.key));\n\n // Remove only keys that are not in the new results\n const removedKeys: string[] = [];\n for (const key of this.currentResults.keys()) {\n if (!newKeys.has(key)) {\n removedKeys.push(key);\n this.currentResults.delete(key);\n }\n }\n if (removedKeys.length > 0) {\n console.log(`[QueryHandle:${this.mapName}] Removed ${removedKeys.length} keys:`, removedKeys);\n }\n\n // Add/update new results\n for (const item of items) {\n this.currentResults.set(item.key, item.value);\n }\n console.log(`[QueryHandle:${this.mapName}] After merge: ${this.currentResults.size} results`);\n this.notify();\n }\n\n /**\n * Called by SyncEngine when server sends a live update\n */\n public onUpdate(key: string, value: T | null) {\n if (value === null) {\n this.currentResults.delete(key);\n } else {\n this.currentResults.set(key, value);\n }\n this.notify();\n }\n\n private notify() {\n const results = this.getSortedResults();\n for (const listener of this.listeners) {\n listener(results);\n }\n }\n\n private getSortedResults(): (T & { _key: string })[] {\n // Include _key in each result for client-side matching/lookup\n const results = Array.from(this.currentResults.entries()).map(\n ([key, value]) => ({ ...(value as object), _key: key } as T & { _key: string })\n );\n\n if (this.filter.sort) {\n results.sort((a: any, b: any) => {\n for (const [field, direction] of Object.entries(this.filter.sort!)) {\n const valA = a[field];\n const valB = b[field];\n\n if (valA < valB) return direction === 'asc' ? -1 : 1;\n if (valA > valB) return direction === 'asc' ? 1 : -1;\n }\n return 0;\n });\n }\n\n return results;\n }\n\n public getFilter(): QueryFilter {\n return this.filter;\n }\n\n public getMapName(): string {\n return this.mapName;\n }\n}\n","import { SyncEngine } from './SyncEngine';\n\nexport interface ILock {\n lock(ttl?: number): Promise<boolean>;\n unlock(): Promise<void>;\n isLocked(): boolean;\n}\n\nexport class DistributedLock implements ILock {\n private syncEngine: SyncEngine;\n private name: string;\n private fencingToken: number | null = null;\n private _isLocked: boolean = false;\n\n constructor(syncEngine: SyncEngine, name: string) {\n this.syncEngine = syncEngine;\n this.name = name;\n }\n\n public async lock(ttl: number = 10000): Promise<boolean> {\n const requestId = crypto.randomUUID();\n try {\n const result = await this.syncEngine.requestLock(this.name, requestId, ttl);\n this.fencingToken = result.fencingToken;\n this._isLocked = true;\n return true;\n } catch (e) {\n return false;\n }\n }\n\n public async unlock(): Promise<void> {\n if (!this._isLocked || this.fencingToken === null) return;\n \n const requestId = crypto.randomUUID();\n try {\n await this.syncEngine.releaseLock(this.name, requestId, this.fencingToken);\n } finally {\n this._isLocked = false;\n this.fencingToken = null;\n }\n }\n\n public isLocked(): boolean {\n return this._isLocked;\n }\n}\n\n","import { SyncEngine } from './SyncEngine';\n\nexport type TopicCallback = (data: any, context: { timestamp: number; publisherId?: string }) => void;\n\nexport class TopicHandle {\n private engine: SyncEngine;\n private topic: string;\n private listeners: Set<TopicCallback> = new Set();\n\n constructor(engine: SyncEngine, topic: string) {\n this.engine = engine;\n this.topic = topic;\n }\n\n public get id(): string {\n return this.topic;\n }\n\n /**\n * Publish a message to the topic\n */\n public publish(data: any) {\n this.engine.publishTopic(this.topic, data);\n }\n\n /**\n * Subscribe to the topic\n */\n public subscribe(callback: TopicCallback) {\n if (this.listeners.size === 0) {\n this.engine.subscribeToTopic(this.topic, this);\n }\n this.listeners.add(callback);\n return () => this.unsubscribe(callback);\n }\n\n private unsubscribe(callback: TopicCallback) {\n this.listeners.delete(callback);\n if (this.listeners.size === 0) {\n this.engine.unsubscribeFromTopic(this.topic);\n }\n }\n\n /**\n * Called by SyncEngine when a message is received\n */\n public onMessage(data: any, context: { timestamp: number; publisherId?: string }) {\n this.listeners.forEach(cb => {\n try {\n cb(data, context);\n } catch (e) {\n console.error('Error in topic listener', e);\n }\n });\n }\n}\n\n","/**\n * ClusterClient - Cluster-aware client wrapper\n *\n * Phase 4: Partition-Aware Client Routing\n * Phase 4.5: Implements IConnectionProvider for SyncEngine abstraction\n *\n * Wraps the standard TopGunClient with cluster-aware routing capabilities.\n * Coordinates between ConnectionPool and PartitionRouter for optimal\n * request routing in a clustered environment.\n */\n\nimport {\n ClusterClientConfig,\n ConnectionPoolConfig,\n PartitionRouterConfig,\n CircuitBreakerConfig,\n DEFAULT_CONNECTION_POOL_CONFIG,\n DEFAULT_PARTITION_ROUTER_CONFIG,\n DEFAULT_CIRCUIT_BREAKER_CONFIG,\n NodeHealth,\n serialize,\n} from '@topgunbuild/core';\nimport { ConnectionPool } from './ConnectionPool';\nimport { PartitionRouter } from './PartitionRouter';\nimport { logger } from '../utils/logger';\nimport type { IConnectionProvider, ConnectionProviderEvent, ConnectionEventHandler } from '../types';\n\nexport interface ClusterClientEvents {\n 'connected': () => void;\n 'disconnected': (reason: string) => void;\n 'partitionMap:ready': (version: number) => void;\n 'routing:active': () => void;\n 'error': (error: Error) => void;\n 'circuit:open': (nodeId: string) => void;\n 'circuit:closed': (nodeId: string) => void;\n 'circuit:half-open': (nodeId: string) => void;\n}\n\n/**\n * Circuit breaker state for a node.\n */\nexport interface CircuitState {\n /** Number of consecutive failures */\n failures: number;\n /** Timestamp of last failure */\n lastFailure: number;\n /** Current circuit state */\n state: 'closed' | 'open' | 'half-open';\n}\n\n/**\n * Routing metrics for monitoring smart routing effectiveness.\n */\nexport interface RoutingMetrics {\n /** Operations routed directly to partition owner */\n directRoutes: number;\n /** Operations falling back to any node (owner unavailable) */\n fallbackRoutes: number;\n /** Operations when partition map is missing/stale */\n partitionMisses: number;\n /** Total routing decisions made */\n totalRoutes: number;\n}\n\nexport type ClusterRoutingMode = 'direct' | 'forward';\n\n/**\n * ClusterClient implements IConnectionProvider for multi-node cluster mode.\n * It provides partition-aware routing and connection management.\n */\nexport class ClusterClient implements IConnectionProvider {\n private readonly listeners: Map<string, Set<(...args: any[]) => void>> = new Map();\n private readonly connectionPool: ConnectionPool;\n private readonly partitionRouter: PartitionRouter;\n private readonly config: ClusterClientConfig;\n private initialized: boolean = false;\n private routingActive: boolean = false;\n private readonly routingMetrics: RoutingMetrics = {\n directRoutes: 0,\n fallbackRoutes: 0,\n partitionMisses: 0,\n totalRoutes: 0,\n };\n\n // Circuit breaker state per node\n private readonly circuits: Map<string, CircuitState> = new Map();\n private readonly circuitBreakerConfig: CircuitBreakerConfig;\n\n constructor(config: ClusterClientConfig) {\n this.config = config;\n\n // Initialize circuit breaker config\n this.circuitBreakerConfig = {\n ...DEFAULT_CIRCUIT_BREAKER_CONFIG,\n ...config.circuitBreaker,\n };\n\n // Initialize connection pool\n const poolConfig: ConnectionPoolConfig = {\n ...DEFAULT_CONNECTION_POOL_CONFIG,\n ...config.connectionPool,\n };\n this.connectionPool = new ConnectionPool(poolConfig);\n\n // Initialize partition router\n const routerConfig: PartitionRouterConfig = {\n ...DEFAULT_PARTITION_ROUTER_CONFIG,\n fallbackMode: config.routingMode === 'direct' ? 'error' : 'forward',\n ...config.routing,\n };\n this.partitionRouter = new PartitionRouter(this.connectionPool, routerConfig);\n\n this.setupEventHandlers();\n }\n\n // ============================================\n // Event Emitter Methods (browser-compatible)\n // ============================================\n\n public on(event: string, listener: (...args: any[]) => void): this {\n if (!this.listeners.has(event)) {\n this.listeners.set(event, new Set());\n }\n this.listeners.get(event)!.add(listener);\n return this;\n }\n\n public off(event: string, listener: (...args: any[]) => void): this {\n this.listeners.get(event)?.delete(listener);\n return this;\n }\n\n public emit(event: string, ...args: any[]): boolean {\n const eventListeners = this.listeners.get(event);\n if (!eventListeners || eventListeners.size === 0) {\n return false;\n }\n for (const listener of eventListeners) {\n try {\n listener(...args);\n } catch (err) {\n logger.error({ event, err }, 'Error in event listener');\n }\n }\n return true;\n }\n\n public removeAllListeners(event?: string): this {\n if (event) {\n this.listeners.delete(event);\n } else {\n this.listeners.clear();\n }\n return this;\n }\n\n // ============================================\n // IConnectionProvider Implementation\n // ============================================\n\n /**\n * Connect to cluster nodes (IConnectionProvider interface).\n * Alias for start() method.\n */\n public async connect(): Promise<void> {\n return this.start();\n }\n\n /**\n * Get connection for a specific key (IConnectionProvider interface).\n * Routes to partition owner based on key hash when smart routing is enabled.\n * @throws Error if not connected\n */\n public getConnection(key: string): WebSocket {\n if (!this.isConnected()) {\n throw new Error('ClusterClient not connected');\n }\n\n this.routingMetrics.totalRoutes++;\n\n // If not in direct routing mode or routing not active, use fallback\n if (this.config.routingMode !== 'direct' || !this.routingActive) {\n this.routingMetrics.fallbackRoutes++;\n return this.getFallbackConnection();\n }\n\n // Try to route to partition owner\n const routing = this.partitionRouter.route(key);\n\n // No partition map available\n if (!routing) {\n this.routingMetrics.partitionMisses++;\n logger.debug({ key }, 'No partition map available, using fallback');\n return this.getFallbackConnection();\n }\n\n const owner = routing.nodeId;\n\n // Check if owner is connected\n if (!this.connectionPool.isNodeConnected(owner)) {\n this.routingMetrics.fallbackRoutes++;\n logger.debug({ key, owner }, 'Partition owner not connected, using fallback');\n // Request partition map refresh since owner might have changed\n this.requestPartitionMapRefresh();\n return this.getFallbackConnection();\n }\n\n // Get connection to owner\n const socket = this.connectionPool.getConnection(owner);\n if (!socket) {\n this.routingMetrics.fallbackRoutes++;\n logger.debug({ key, owner }, 'Could not get connection to owner, using fallback');\n return this.getFallbackConnection();\n }\n\n this.routingMetrics.directRoutes++;\n return socket;\n }\n\n /**\n * Get fallback connection when owner is unavailable.\n * @throws Error if no connection available\n */\n private getFallbackConnection(): WebSocket {\n const conn = this.connectionPool.getAnyHealthyConnection();\n if (!conn?.socket) {\n throw new Error('No healthy connection available');\n }\n return conn.socket;\n }\n\n /**\n * Request a partition map refresh in the background.\n * Called when routing to an unknown/disconnected owner.\n */\n private requestPartitionMapRefresh(): void {\n this.partitionRouter.refreshPartitionMap().catch(err => {\n logger.error({ err }, 'Failed to refresh partition map');\n });\n }\n\n /**\n * Request partition map from a specific node.\n * Called on first node connection.\n */\n private requestPartitionMapFromNode(nodeId: string): void {\n const socket = this.connectionPool.getConnection(nodeId);\n if (socket) {\n logger.debug({ nodeId }, 'Requesting partition map from node');\n socket.send(serialize({\n type: 'PARTITION_MAP_REQUEST',\n payload: {\n currentVersion: this.partitionRouter.getMapVersion(),\n },\n }));\n }\n }\n\n /**\n * Check if at least one connection is active (IConnectionProvider interface).\n */\n public isConnected(): boolean {\n return this.connectionPool.getConnectedNodes().length > 0;\n }\n\n /**\n * Send data via the appropriate connection (IConnectionProvider interface).\n * Routes based on key if provided.\n */\n public send(data: ArrayBuffer | Uint8Array, key?: string): void {\n if (!this.isConnected()) {\n throw new Error('ClusterClient not connected');\n }\n\n const socket = key ? this.getConnection(key) : this.getAnyConnection();\n socket.send(data);\n }\n\n /**\n * Send data with automatic retry and rerouting on failure.\n * @param data - Data to send\n * @param key - Optional key for routing\n * @param options - Retry options\n * @throws Error after max retries exceeded\n */\n public async sendWithRetry(\n data: ArrayBuffer | Uint8Array,\n key?: string,\n options: {\n maxRetries?: number;\n retryDelayMs?: number;\n retryOnNotOwner?: boolean;\n } = {}\n ): Promise<void> {\n const {\n maxRetries = 3,\n retryDelayMs = 100,\n retryOnNotOwner = true,\n } = options;\n\n let lastError: Error | null = null;\n let nodeId: string | null = null;\n\n for (let attempt = 0; attempt < maxRetries; attempt++) {\n try {\n // Get the target node for circuit breaker tracking\n if (key && this.routingActive) {\n const routing = this.partitionRouter.route(key);\n nodeId = routing?.nodeId ?? null;\n }\n\n // Check circuit breaker\n if (nodeId && !this.canUseNode(nodeId)) {\n logger.debug({ nodeId, attempt }, 'Circuit open, using fallback');\n nodeId = null; // Force fallback\n }\n\n // Get connection and send\n const socket = key && nodeId\n ? this.connectionPool.getConnection(nodeId)\n : this.getAnyConnection();\n\n if (!socket) {\n throw new Error('No connection available');\n }\n\n socket.send(data);\n\n // Record success if using a specific node\n if (nodeId) {\n this.recordSuccess(nodeId);\n }\n\n return; // Success\n } catch (error) {\n lastError = error as Error;\n\n // Record failure if using a specific node\n if (nodeId) {\n this.recordFailure(nodeId);\n }\n\n const errorCode = (error as any)?.code;\n\n // Check if error is retryable\n if (this.isRetryableError(error)) {\n logger.debug(\n { attempt, maxRetries, errorCode, nodeId },\n 'Retryable error, will retry'\n );\n\n // Handle specific error types\n if (errorCode === 'NOT_OWNER' && retryOnNotOwner) {\n // Wait for partition map update\n await this.waitForPartitionMapUpdateInternal(2000);\n } else if (errorCode === 'CONNECTION_CLOSED' || !this.isConnected()) {\n // Wait for reconnection\n await this.waitForConnectionInternal(5000);\n }\n\n // Small delay before retry\n await this.delay(retryDelayMs * (attempt + 1)); // Exponential backoff\n continue;\n }\n\n // Non-retryable error, fail immediately\n throw error;\n }\n }\n\n throw new Error(\n `Operation failed after ${maxRetries} retries: ${lastError?.message}`\n );\n }\n\n /**\n * Check if an error is retryable.\n */\n private isRetryableError(error: any): boolean {\n const code = error?.code;\n const message = error?.message || '';\n\n return (\n code === 'NOT_OWNER' ||\n code === 'CONNECTION_CLOSED' ||\n code === 'TIMEOUT' ||\n code === 'ECONNRESET' ||\n message.includes('No active connections') ||\n message.includes('No connection available') ||\n message.includes('No healthy connection')\n );\n }\n\n /**\n * Wait for partition map update.\n */\n private waitForPartitionMapUpdateInternal(timeoutMs: number): Promise<void> {\n return new Promise((resolve) => {\n const timeout = setTimeout(resolve, timeoutMs);\n\n const handler = () => {\n clearTimeout(timeout);\n this.off('partitionMapUpdated', handler);\n resolve();\n };\n\n this.on('partitionMapUpdated', handler);\n });\n }\n\n /**\n * Wait for at least one connection to be available.\n */\n private waitForConnectionInternal(timeoutMs: number): Promise<void> {\n return new Promise((resolve, reject) => {\n if (this.isConnected()) {\n resolve();\n return;\n }\n\n const timeout = setTimeout(() => {\n this.off('connected', handler);\n reject(new Error('Connection timeout'));\n }, timeoutMs);\n\n const handler = () => {\n clearTimeout(timeout);\n this.off('connected', handler);\n resolve();\n };\n\n this.on('connected', handler);\n });\n }\n\n /**\n * Helper delay function.\n */\n private delay(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n\n // ============================================\n // Cluster-Specific Methods\n // ============================================\n\n /**\n * Initialize cluster connections\n */\n public async start(): Promise<void> {\n if (this.initialized) return;\n\n logger.info({ seedNodes: this.config.seedNodes }, 'Starting cluster client');\n\n // Connect to seed nodes\n for (let i = 0; i < this.config.seedNodes.length; i++) {\n const endpoint = this.config.seedNodes[i];\n const nodeId = `seed-${i}`; // Temporary ID until we get real ID from handshake\n await this.connectionPool.addNode(nodeId, endpoint);\n }\n\n // Start health monitoring\n this.connectionPool.startHealthCheck();\n\n // Start periodic partition map refresh\n this.partitionRouter.startPeriodicRefresh();\n\n this.initialized = true;\n\n // Wait for first partition map\n await this.waitForPartitionMap();\n }\n\n /**\n * Set authentication token\n */\n public setAuthToken(token: string): void {\n this.connectionPool.setAuthToken(token);\n }\n\n /**\n * Send operation with automatic routing (legacy API for cluster operations).\n * @deprecated Use send(data, key) for IConnectionProvider interface\n */\n public sendMessage(key: string, message: any): boolean {\n if (this.config.routingMode === 'direct' && this.routingActive) {\n return this.sendDirect(key, message);\n }\n return this.sendForward(message);\n }\n\n /**\n * Send directly to partition owner\n */\n public sendDirect(key: string, message: any): boolean {\n const connection = this.partitionRouter.routeToConnection(key);\n if (!connection) {\n logger.warn({ key }, 'No route available for key');\n return false;\n }\n\n // Add routing metadata\n const routedMessage = {\n ...message,\n _routing: {\n partitionId: this.partitionRouter.getPartitionId(key),\n mapVersion: this.partitionRouter.getMapVersion(),\n },\n };\n\n connection.socket.send(serialize(routedMessage));\n return true;\n }\n\n /**\n * Send to primary node for server-side forwarding\n */\n public sendForward(message: any): boolean {\n return this.connectionPool.sendToPrimary(message);\n }\n\n /**\n * Send batch of operations with routing\n */\n public sendBatch(operations: Array<{ key: string; message: any }>): Map<string, boolean> {\n const results = new Map<string, boolean>();\n\n if (this.config.routingMode === 'direct' && this.routingActive) {\n // Group by target node\n const nodeMessages = new Map<string, any[]>();\n\n for (const { key, message } of operations) {\n const routing = this.partitionRouter.route(key);\n const nodeId = routing?.nodeId ?? 'primary';\n\n if (!nodeMessages.has(nodeId)) {\n nodeMessages.set(nodeId, []);\n }\n nodeMessages.get(nodeId)!.push({ key, message });\n }\n\n // Send to each node\n for (const [nodeId, messages] of nodeMessages) {\n let success: boolean;\n if (nodeId === 'primary') {\n success = this.connectionPool.sendToPrimary({\n type: 'OP_BATCH',\n payload: { ops: messages.map(m => m.message) },\n });\n } else {\n success = this.connectionPool.send(nodeId, {\n type: 'OP_BATCH',\n payload: { ops: messages.map(m => m.message) },\n });\n }\n\n for (const { key } of messages) {\n results.set(key, success);\n }\n }\n } else {\n // Forward all to primary\n const success = this.connectionPool.sendToPrimary({\n type: 'OP_BATCH',\n payload: { ops: operations.map(o => o.message) },\n });\n\n for (const { key } of operations) {\n results.set(key, success);\n }\n }\n\n return results;\n }\n\n /**\n * Get connection pool health status\n */\n public getHealthStatus(): Map<string, NodeHealth> {\n return this.connectionPool.getHealthStatus();\n }\n\n /**\n * Get partition router stats\n */\n public getRouterStats(): ReturnType<PartitionRouter['getStats']> {\n return this.partitionRouter.getStats();\n }\n\n /**\n * Get routing metrics for monitoring smart routing effectiveness.\n */\n public getRoutingMetrics(): RoutingMetrics {\n return { ...this.routingMetrics };\n }\n\n /**\n * Reset routing metrics counters.\n * Useful for monitoring intervals.\n */\n public resetRoutingMetrics(): void {\n this.routingMetrics.directRoutes = 0;\n this.routingMetrics.fallbackRoutes = 0;\n this.routingMetrics.partitionMisses = 0;\n this.routingMetrics.totalRoutes = 0;\n }\n\n /**\n * Check if cluster routing is active\n */\n public isRoutingActive(): boolean {\n return this.routingActive;\n }\n\n /**\n * Get list of connected nodes\n */\n public getConnectedNodes(): string[] {\n return this.connectionPool.getConnectedNodes();\n }\n\n /**\n * Check if cluster client is initialized\n */\n public isInitialized(): boolean {\n return this.initialized;\n }\n\n /**\n * Force refresh of partition map\n */\n public async refreshPartitionMap(): Promise<void> {\n await this.partitionRouter.refreshPartitionMap();\n }\n\n /**\n * Shutdown cluster client (IConnectionProvider interface).\n */\n public async close(): Promise<void> {\n this.partitionRouter.close();\n this.connectionPool.close();\n this.initialized = false;\n this.routingActive = false;\n logger.info('Cluster client closed');\n }\n\n // ============================================\n // Internal Access for TopGunClient\n // ============================================\n\n /**\n * Get the connection pool (for internal use)\n */\n public getConnectionPool(): ConnectionPool {\n return this.connectionPool;\n }\n\n /**\n * Get the partition router (for internal use)\n */\n public getPartitionRouter(): PartitionRouter {\n return this.partitionRouter;\n }\n\n /**\n * Get any healthy WebSocket connection (IConnectionProvider interface).\n * @throws Error if not connected\n */\n public getAnyConnection(): WebSocket {\n const conn = this.connectionPool.getAnyHealthyConnection();\n if (!conn?.socket) {\n throw new Error('No healthy connection available');\n }\n return conn.socket;\n }\n\n /**\n * Get any healthy WebSocket connection, or null if none available.\n * Use this for optional connection checks.\n */\n public getAnyConnectionOrNull(): WebSocket | null {\n const conn = this.connectionPool.getAnyHealthyConnection();\n return conn?.socket ?? null;\n }\n\n // ============================================\n // Circuit Breaker Methods\n // ============================================\n\n /**\n * Get circuit breaker state for a node.\n */\n public getCircuit(nodeId: string): CircuitState {\n let circuit = this.circuits.get(nodeId);\n if (!circuit) {\n circuit = { failures: 0, lastFailure: 0, state: 'closed' };\n this.circuits.set(nodeId, circuit);\n }\n return circuit;\n }\n\n /**\n * Check if a node can be used (circuit not open).\n */\n public canUseNode(nodeId: string): boolean {\n const circuit = this.getCircuit(nodeId);\n\n if (circuit.state === 'closed') {\n return true;\n }\n\n if (circuit.state === 'open') {\n // Check if reset timeout elapsed\n if (Date.now() - circuit.lastFailure > this.circuitBreakerConfig.resetTimeoutMs) {\n circuit.state = 'half-open';\n logger.debug({ nodeId }, 'Circuit breaker half-open, allowing test request');\n this.emit('circuit:half-open', nodeId);\n return true; // Allow one test request\n }\n return false;\n }\n\n // half-open: allow requests\n return true;\n }\n\n /**\n * Record a successful operation to a node.\n * Resets circuit breaker on success.\n */\n public recordSuccess(nodeId: string): void {\n const circuit = this.getCircuit(nodeId);\n const wasOpen = circuit.state !== 'closed';\n\n circuit.failures = 0;\n circuit.state = 'closed';\n\n if (wasOpen) {\n logger.info({ nodeId }, 'Circuit breaker closed after success');\n this.emit('circuit:closed', nodeId);\n }\n }\n\n /**\n * Record a failed operation to a node.\n * Opens circuit breaker after threshold failures.\n */\n public recordFailure(nodeId: string): void {\n const circuit = this.getCircuit(nodeId);\n circuit.failures++;\n circuit.lastFailure = Date.now();\n\n if (circuit.failures >= this.circuitBreakerConfig.failureThreshold) {\n if (circuit.state !== 'open') {\n circuit.state = 'open';\n logger.warn({ nodeId, failures: circuit.failures }, 'Circuit breaker opened');\n this.emit('circuit:open', nodeId);\n }\n }\n }\n\n /**\n * Get all circuit breaker states.\n */\n public getCircuitStates(): Map<string, CircuitState> {\n return new Map(this.circuits);\n }\n\n /**\n * Reset circuit breaker for a specific node.\n */\n public resetCircuit(nodeId: string): void {\n this.circuits.delete(nodeId);\n logger.debug({ nodeId }, 'Circuit breaker reset');\n }\n\n /**\n * Reset all circuit breakers.\n */\n public resetAllCircuits(): void {\n this.circuits.clear();\n logger.debug('All circuit breakers reset');\n }\n\n // ============================================\n // Private Methods\n // ============================================\n\n private setupEventHandlers(): void {\n // Connection pool events\n this.connectionPool.on('node:connected', (nodeId: string) => {\n logger.debug({ nodeId }, 'Node connected');\n\n // Request partition map on first connection if not already received\n if (this.partitionRouter.getMapVersion() === 0) {\n this.requestPartitionMapFromNode(nodeId);\n }\n\n if (this.connectionPool.getConnectedNodes().length === 1) {\n this.emit('connected');\n }\n });\n\n this.connectionPool.on('node:disconnected', (nodeId: string, reason: string) => {\n logger.debug({ nodeId, reason }, 'Node disconnected');\n if (this.connectionPool.getConnectedNodes().length === 0) {\n this.routingActive = false;\n this.emit('disconnected', reason);\n }\n });\n\n this.connectionPool.on('node:unhealthy', (nodeId: string, reason: string) => {\n logger.warn({ nodeId, reason }, 'Node unhealthy');\n });\n\n this.connectionPool.on('error', (nodeId: string, error: Error) => {\n this.emit('error', error);\n });\n\n // Forward messages from connection pool\n this.connectionPool.on('message', (nodeId: string, data: any) => {\n this.emit('message', nodeId, data);\n });\n\n // Partition router events\n this.partitionRouter.on('partitionMap:updated', (version: number, changesCount: number) => {\n if (!this.routingActive && this.partitionRouter.hasPartitionMap()) {\n this.routingActive = true;\n logger.info({ version }, 'Direct routing activated');\n this.emit('routing:active');\n }\n this.emit('partitionMap:ready', version);\n // Emit IConnectionProvider compatible event\n this.emit('partitionMapUpdated');\n });\n\n this.partitionRouter.on('routing:miss', (key: string, expected: string, actual: string) => {\n logger.debug({ key, expected, actual }, 'Routing miss detected');\n });\n }\n\n private async waitForPartitionMap(timeoutMs: number = 10000): Promise<void> {\n if (this.partitionRouter.hasPartitionMap()) {\n this.routingActive = true;\n return;\n }\n\n return new Promise((resolve) => {\n const timeout = setTimeout(() => {\n this.partitionRouter.off('partitionMap:updated', onUpdate);\n // Don't reject - fallback mode will be used\n logger.warn('Partition map not received, using fallback routing');\n resolve();\n }, timeoutMs);\n\n const onUpdate = () => {\n clearTimeout(timeout);\n this.partitionRouter.off('partitionMap:updated', onUpdate);\n this.routingActive = true;\n resolve();\n };\n\n this.partitionRouter.once('partitionMap:updated', onUpdate);\n });\n }\n}\n","/**\n * ConnectionPool - Manages WebSocket connections to multiple cluster nodes\n *\n * Phase 4: Partition-Aware Client Routing\n *\n * Features:\n * - Maintains connections to all known cluster nodes\n * - Automatic reconnection with exponential backoff\n * - Health monitoring and status tracking\n * - Connection lifecycle management\n */\n\nimport {\n ConnectionPoolConfig,\n DEFAULT_CONNECTION_POOL_CONFIG,\n ConnectionState,\n NodeHealth,\n} from '@topgunbuild/core';\nimport { serialize, deserialize } from '@topgunbuild/core';\nimport { logger } from '../utils/logger';\n\nexport type ConnectionPoolEventType =\n | 'node:connected'\n | 'node:disconnected'\n | 'node:healthy'\n | 'node:unhealthy'\n | 'message'\n | 'error';\n\nexport interface ConnectionPoolEvents {\n 'node:connected': (nodeId: string) => void;\n 'node:disconnected': (nodeId: string, reason: string) => void;\n 'node:healthy': (nodeId: string) => void;\n 'node:unhealthy': (nodeId: string, reason: string) => void;\n 'message': (nodeId: string, message: any) => void;\n 'error': (nodeId: string, error: Error) => void;\n}\n\ninterface NodeConnection {\n nodeId: string;\n endpoint: string;\n socket: WebSocket | null;\n state: ConnectionState;\n lastSeen: number;\n latencyMs: number;\n reconnectAttempts: number;\n reconnectTimer: ReturnType<typeof setTimeout> | null;\n pendingMessages: Uint8Array[];\n}\n\nexport class ConnectionPool {\n private readonly listeners: Map<string, Set<(...args: any[]) => void>> = new Map();\n private readonly config: ConnectionPoolConfig;\n private readonly connections: Map<string, NodeConnection> = new Map();\n private primaryNodeId: string | null = null;\n private healthCheckTimer: ReturnType<typeof setInterval> | null = null;\n private authToken: string | null = null;\n\n constructor(config: Partial<ConnectionPoolConfig> = {}) {\n this.config = {\n ...DEFAULT_CONNECTION_POOL_CONFIG,\n ...config,\n };\n }\n\n // ============================================\n // Event Emitter Methods (browser-compatible)\n // ============================================\n\n public on(event: string, listener: (...args: any[]) => void): this {\n if (!this.listeners.has(event)) {\n this.listeners.set(event, new Set());\n }\n this.listeners.get(event)!.add(listener);\n return this;\n }\n\n public off(event: string, listener: (...args: any[]) => void): this {\n this.listeners.get(event)?.delete(listener);\n return this;\n }\n\n public emit(event: string, ...args: any[]): boolean {\n const eventListeners = this.listeners.get(event);\n if (!eventListeners || eventListeners.size === 0) {\n return false;\n }\n for (const listener of eventListeners) {\n try {\n listener(...args);\n } catch (err) {\n logger.error({ event, err }, 'Error in event listener');\n }\n }\n return true;\n }\n\n public removeAllListeners(event?: string): this {\n if (event) {\n this.listeners.delete(event);\n } else {\n this.listeners.clear();\n }\n return this;\n }\n\n /**\n * Set authentication token for all connections\n */\n public setAuthToken(token: string): void {\n this.authToken = token;\n // Re-authenticate existing connections\n for (const conn of this.connections.values()) {\n if (conn.state === 'CONNECTED') {\n this.sendAuth(conn);\n }\n }\n }\n\n /**\n * Add a node to the connection pool\n */\n public async addNode(nodeId: string, endpoint: string): Promise<void> {\n if (this.connections.has(nodeId)) {\n const existing = this.connections.get(nodeId)!;\n if (existing.endpoint !== endpoint) {\n // Endpoint changed, reconnect\n await this.removeNode(nodeId);\n } else {\n return; // Already connected\n }\n }\n\n const connection: NodeConnection = {\n nodeId,\n endpoint,\n socket: null,\n state: 'DISCONNECTED',\n lastSeen: 0,\n latencyMs: 0,\n reconnectAttempts: 0,\n reconnectTimer: null,\n pendingMessages: [],\n };\n\n this.connections.set(nodeId, connection);\n\n // Set first node as primary\n if (!this.primaryNodeId) {\n this.primaryNodeId = nodeId;\n }\n\n await this.connect(nodeId);\n }\n\n /**\n * Remove a node from the connection pool\n */\n public async removeNode(nodeId: string): Promise<void> {\n const connection = this.connections.get(nodeId);\n if (!connection) return;\n\n // Clear reconnect timer\n if (connection.reconnectTimer) {\n clearTimeout(connection.reconnectTimer);\n connection.reconnectTimer = null;\n }\n\n // Close socket\n if (connection.socket) {\n connection.socket.onclose = null; // Prevent reconnect\n connection.socket.close();\n connection.socket = null;\n }\n\n this.connections.delete(nodeId);\n\n // Update primary if needed\n if (this.primaryNodeId === nodeId) {\n this.primaryNodeId = this.connections.size > 0\n ? this.connections.keys().next().value ?? null\n : null;\n }\n\n logger.info({ nodeId }, 'Node removed from connection pool');\n }\n\n /**\n * Get connection for a specific node\n */\n public getConnection(nodeId: string): WebSocket | null {\n const connection = this.connections.get(nodeId);\n if (!connection || connection.state !== 'AUTHENTICATED') {\n return null;\n }\n return connection.socket;\n }\n\n /**\n * Get primary connection (first/seed node)\n */\n public getPrimaryConnection(): WebSocket | null {\n if (!this.primaryNodeId) return null;\n return this.getConnection(this.primaryNodeId);\n }\n\n /**\n * Get any healthy connection\n */\n public getAnyHealthyConnection(): { nodeId: string; socket: WebSocket } | null {\n for (const [nodeId, conn] of this.connections) {\n if (conn.state === 'AUTHENTICATED' && conn.socket) {\n return { nodeId, socket: conn.socket };\n }\n }\n return null;\n }\n\n /**\n * Send message to a specific node\n */\n public send(nodeId: string, message: any): boolean {\n const connection = this.connections.get(nodeId);\n if (!connection) {\n logger.warn({ nodeId }, 'Cannot send: node not in pool');\n return false;\n }\n\n const data = serialize(message);\n\n if (connection.state === 'AUTHENTICATED' && connection.socket?.readyState === WebSocket.OPEN) {\n connection.socket.send(data);\n return true;\n }\n\n // Queue message for later\n if (connection.pendingMessages.length < 1000) {\n connection.pendingMessages.push(data);\n return true;\n }\n\n logger.warn({ nodeId }, 'Message queue full, dropping message');\n return false;\n }\n\n /**\n * Send message to primary node\n */\n public sendToPrimary(message: any): boolean {\n if (!this.primaryNodeId) {\n logger.warn('No primary node available');\n return false;\n }\n return this.send(this.primaryNodeId, message);\n }\n\n /**\n * Get health status for all nodes\n */\n public getHealthStatus(): Map<string, NodeHealth> {\n const status = new Map<string, NodeHealth>();\n for (const [nodeId, conn] of this.connections) {\n status.set(nodeId, {\n nodeId,\n state: conn.state,\n lastSeen: conn.lastSeen,\n latencyMs: conn.latencyMs,\n reconnectAttempts: conn.reconnectAttempts,\n });\n }\n return status;\n }\n\n /**\n * Get list of connected node IDs\n */\n public getConnectedNodes(): string[] {\n return Array.from(this.connections.entries())\n .filter(([_, conn]) => conn.state === 'AUTHENTICATED')\n .map(([nodeId]) => nodeId);\n }\n\n /**\n * Get all node IDs\n */\n public getAllNodes(): string[] {\n return Array.from(this.connections.keys());\n }\n\n /**\n * Check if node is connected and authenticated\n */\n public isNodeConnected(nodeId: string): boolean {\n const conn = this.connections.get(nodeId);\n return conn?.state === 'AUTHENTICATED';\n }\n\n /**\n * Check if connected to a specific node.\n * Alias for isNodeConnected() for IConnectionProvider compatibility.\n */\n public isConnected(nodeId: string): boolean {\n return this.isNodeConnected(nodeId);\n }\n\n /**\n * Start health monitoring\n */\n public startHealthCheck(): void {\n if (this.healthCheckTimer) return;\n\n this.healthCheckTimer = setInterval(() => {\n this.performHealthCheck();\n }, this.config.healthCheckIntervalMs);\n }\n\n /**\n * Stop health monitoring\n */\n public stopHealthCheck(): void {\n if (this.healthCheckTimer) {\n clearInterval(this.healthCheckTimer);\n this.healthCheckTimer = null;\n }\n }\n\n /**\n * Close all connections and cleanup\n */\n public close(): void {\n this.stopHealthCheck();\n\n for (const nodeId of this.connections.keys()) {\n this.removeNode(nodeId);\n }\n\n this.connections.clear();\n this.primaryNodeId = null;\n }\n\n // ============================================\n // Private Methods\n // ============================================\n\n private async connect(nodeId: string): Promise<void> {\n const connection = this.connections.get(nodeId);\n if (!connection) return;\n\n if (connection.state === 'CONNECTING' || connection.state === 'CONNECTED') {\n return;\n }\n\n connection.state = 'CONNECTING';\n logger.info({ nodeId, endpoint: connection.endpoint }, 'Connecting to node');\n\n try {\n const socket = new WebSocket(connection.endpoint);\n socket.binaryType = 'arraybuffer';\n connection.socket = socket;\n\n socket.onopen = () => {\n connection.state = 'CONNECTED';\n connection.reconnectAttempts = 0;\n connection.lastSeen = Date.now();\n logger.info({ nodeId }, 'Connected to node');\n this.emit('node:connected', nodeId);\n\n // Send auth if we have token\n if (this.authToken) {\n this.sendAuth(connection);\n }\n\n // Flush pending messages after auth\n // Note: Messages will be sent after AUTH_ACK\n };\n\n socket.onmessage = (event) => {\n connection.lastSeen = Date.now();\n this.handleMessage(nodeId, event);\n };\n\n socket.onerror = (error) => {\n logger.error({ nodeId, error }, 'WebSocket error');\n this.emit('error', nodeId, error instanceof Error ? error : new Error('WebSocket error'));\n };\n\n socket.onclose = () => {\n const wasConnected = connection.state === 'AUTHENTICATED';\n connection.state = 'DISCONNECTED';\n connection.socket = null;\n\n if (wasConnected) {\n this.emit('node:disconnected', nodeId, 'Connection closed');\n }\n\n // Schedule reconnect\n this.scheduleReconnect(nodeId);\n };\n\n } catch (error) {\n connection.state = 'FAILED';\n logger.error({ nodeId, error }, 'Failed to connect');\n this.scheduleReconnect(nodeId);\n }\n }\n\n private sendAuth(connection: NodeConnection): void {\n if (!this.authToken || !connection.socket) return;\n\n connection.socket.send(serialize({\n type: 'AUTH',\n token: this.authToken,\n }));\n }\n\n private handleMessage(nodeId: string, event: MessageEvent): void {\n const connection = this.connections.get(nodeId);\n if (!connection) return;\n\n let message: any;\n try {\n if (event.data instanceof ArrayBuffer) {\n message = deserialize(new Uint8Array(event.data));\n } else {\n message = JSON.parse(event.data);\n }\n } catch (e) {\n logger.error({ nodeId, error: e }, 'Failed to parse message');\n return;\n }\n\n // Handle auth response\n if (message.type === 'AUTH_ACK') {\n connection.state = 'AUTHENTICATED';\n logger.info({ nodeId }, 'Authenticated with node');\n this.emit('node:healthy', nodeId);\n\n // Flush pending messages\n this.flushPendingMessages(connection);\n return;\n }\n\n if (message.type === 'AUTH_REQUIRED') {\n if (this.authToken) {\n this.sendAuth(connection);\n }\n return;\n }\n\n if (message.type === 'AUTH_FAIL') {\n logger.error({ nodeId, error: message.error }, 'Authentication failed');\n connection.state = 'FAILED';\n return;\n }\n\n if (message.type === 'PONG') {\n // Update latency\n if (message.timestamp) {\n connection.latencyMs = Date.now() - message.timestamp;\n }\n return;\n }\n\n // Handle partition map updates\n if (message.type === 'PARTITION_MAP' || message.type === 'PARTITION_MAP_DELTA') {\n this.emit('message', nodeId, message);\n return;\n }\n\n // Emit other messages\n this.emit('message', nodeId, message);\n }\n\n private flushPendingMessages(connection: NodeConnection): void {\n if (!connection.socket || connection.state !== 'AUTHENTICATED') return;\n\n const pending = connection.pendingMessages;\n connection.pendingMessages = [];\n\n for (const data of pending) {\n if (connection.socket.readyState === WebSocket.OPEN) {\n connection.socket.send(data);\n }\n }\n\n if (pending.length > 0) {\n logger.debug({ nodeId: connection.nodeId, count: pending.length }, 'Flushed pending messages');\n }\n }\n\n private scheduleReconnect(nodeId: string): void {\n const connection = this.connections.get(nodeId);\n if (!connection) return;\n\n // Clear existing timer\n if (connection.reconnectTimer) {\n clearTimeout(connection.reconnectTimer);\n connection.reconnectTimer = null;\n }\n\n // Check max attempts\n if (connection.reconnectAttempts >= this.config.maxReconnectAttempts) {\n connection.state = 'FAILED';\n logger.error({ nodeId, attempts: connection.reconnectAttempts }, 'Max reconnect attempts reached');\n this.emit('node:unhealthy', nodeId, 'Max reconnect attempts reached');\n return;\n }\n\n // Calculate backoff delay\n const delay = Math.min(\n this.config.reconnectDelayMs * Math.pow(2, connection.reconnectAttempts),\n this.config.maxReconnectDelayMs\n );\n\n connection.state = 'RECONNECTING';\n connection.reconnectAttempts++;\n\n logger.info({ nodeId, delay, attempt: connection.reconnectAttempts }, 'Scheduling reconnect');\n\n connection.reconnectTimer = setTimeout(() => {\n connection.reconnectTimer = null;\n this.connect(nodeId);\n }, delay);\n }\n\n private performHealthCheck(): void {\n const now = Date.now();\n\n for (const [nodeId, connection] of this.connections) {\n // Skip nodes that are not authenticated\n if (connection.state !== 'AUTHENTICATED') continue;\n\n // Check staleness\n const timeSinceLastSeen = now - connection.lastSeen;\n if (timeSinceLastSeen > this.config.healthCheckIntervalMs * 3) {\n logger.warn({ nodeId, timeSinceLastSeen }, 'Node appears stale, sending ping');\n }\n\n // Send ping\n if (connection.socket?.readyState === WebSocket.OPEN) {\n connection.socket.send(serialize({\n type: 'PING',\n timestamp: now,\n }));\n }\n }\n }\n}\n","/**\n * PartitionRouter - Routes operations to the correct cluster node\n *\n * Phase 4: Partition-Aware Client Routing\n *\n * Features:\n * - Maintains local copy of partition map\n * - Routes keys to owner nodes using consistent hashing\n * - Handles stale routing with automatic refresh\n * - Supports fallback to server-side forwarding\n */\n\nimport {\n PartitionMap,\n PartitionRouterConfig,\n DEFAULT_PARTITION_ROUTER_CONFIG,\n PartitionMapMessage,\n PartitionMapDeltaMessage,\n PartitionChange,\n PARTITION_COUNT,\n hashString,\n} from '@topgunbuild/core';\nimport { ConnectionPool } from './ConnectionPool';\nimport { logger } from '../utils/logger';\n\nexport interface RoutingResult {\n nodeId: string;\n partitionId: number;\n isOwner: boolean;\n isBackup: boolean;\n}\n\nexport interface PartitionRouterEvents {\n 'partitionMap:updated': (version: number, changesCount: number) => void;\n 'partitionMap:stale': (currentVersion: number, lastRefresh: number) => void;\n 'routing:miss': (key: string, expectedOwner: string, actualOwner: string) => void;\n}\n\nexport class PartitionRouter {\n private readonly listeners: Map<string, Set<(...args: any[]) => void>> = new Map();\n private readonly config: PartitionRouterConfig;\n private readonly connectionPool: ConnectionPool;\n private partitionMap: PartitionMap | null = null;\n private lastRefreshTime: number = 0;\n private refreshTimer: ReturnType<typeof setInterval> | null = null;\n private pendingRefresh: Promise<void> | null = null;\n\n constructor(\n connectionPool: ConnectionPool,\n config: Partial<PartitionRouterConfig> = {}\n ) {\n this.connectionPool = connectionPool;\n this.config = {\n ...DEFAULT_PARTITION_ROUTER_CONFIG,\n ...config,\n };\n\n // Listen for partition map updates from any connection\n this.connectionPool.on('message', (nodeId: string, message: any) => {\n if (message.type === 'PARTITION_MAP') {\n this.handlePartitionMap(message as PartitionMapMessage);\n } else if (message.type === 'PARTITION_MAP_DELTA') {\n this.handlePartitionMapDelta(message as PartitionMapDeltaMessage);\n }\n });\n }\n\n // ============================================\n // Event Emitter Methods (browser-compatible)\n // ============================================\n\n public on(event: string, listener: (...args: any[]) => void): this {\n if (!this.listeners.has(event)) {\n this.listeners.set(event, new Set());\n }\n this.listeners.get(event)!.add(listener);\n return this;\n }\n\n public off(event: string, listener: (...args: any[]) => void): this {\n this.listeners.get(event)?.delete(listener);\n return this;\n }\n\n public once(event: string, listener: (...args: any[]) => void): this {\n const wrapper = (...args: any[]) => {\n this.off(event, wrapper);\n listener(...args);\n };\n return this.on(event, wrapper);\n }\n\n public emit(event: string, ...args: any[]): boolean {\n const eventListeners = this.listeners.get(event);\n if (!eventListeners || eventListeners.size === 0) {\n return false;\n }\n for (const listener of eventListeners) {\n try {\n listener(...args);\n } catch (err) {\n logger.error({ event, err }, 'Error in event listener');\n }\n }\n return true;\n }\n\n public removeListener(event: string, listener: (...args: any[]) => void): this {\n return this.off(event, listener);\n }\n\n public removeAllListeners(event?: string): this {\n if (event) {\n this.listeners.delete(event);\n } else {\n this.listeners.clear();\n }\n return this;\n }\n\n /**\n * Get the partition ID for a given key\n */\n public getPartitionId(key: string): number {\n return Math.abs(hashString(key)) % PARTITION_COUNT;\n }\n\n /**\n * Route a key to the owner node\n */\n public route(key: string): RoutingResult | null {\n if (!this.partitionMap) {\n return null;\n }\n\n const partitionId = this.getPartitionId(key);\n const partition = this.partitionMap.partitions.find(p => p.partitionId === partitionId);\n\n if (!partition) {\n logger.warn({ key, partitionId }, 'Partition not found in map');\n return null;\n }\n\n return {\n nodeId: partition.ownerNodeId,\n partitionId,\n isOwner: true,\n isBackup: false,\n };\n }\n\n /**\n * Route a key and get the WebSocket connection to use\n */\n public routeToConnection(key: string): { nodeId: string; socket: WebSocket } | null {\n const routing = this.route(key);\n\n if (!routing) {\n // No partition map, use fallback\n if (this.config.fallbackMode === 'forward') {\n const primary = this.connectionPool.getAnyHealthyConnection();\n if (primary) {\n return primary;\n }\n }\n return null;\n }\n\n // Try to get connection to owner\n const socket = this.connectionPool.getConnection(routing.nodeId);\n if (socket) {\n return { nodeId: routing.nodeId, socket };\n }\n\n // Owner not available, try backup\n const partition = this.partitionMap!.partitions.find(p => p.partitionId === routing.partitionId);\n if (partition) {\n for (const backupId of partition.backupNodeIds) {\n const backupSocket = this.connectionPool.getConnection(backupId);\n if (backupSocket) {\n logger.debug({ key, owner: routing.nodeId, backup: backupId }, 'Using backup node');\n return { nodeId: backupId, socket: backupSocket };\n }\n }\n }\n\n // Fallback to any connection\n if (this.config.fallbackMode === 'forward') {\n return this.connectionPool.getAnyHealthyConnection();\n }\n\n return null;\n }\n\n /**\n * Get routing info for multiple keys (batch routing)\n */\n public routeBatch(keys: string[]): Map<string, RoutingResult[]> {\n const result = new Map<string, RoutingResult[]>();\n\n for (const key of keys) {\n const routing = this.route(key);\n if (routing) {\n const nodeId = routing.nodeId;\n if (!result.has(nodeId)) {\n result.set(nodeId, []);\n }\n result.get(nodeId)!.push({ ...routing, key } as any);\n }\n }\n\n return result;\n }\n\n /**\n * Get all partitions owned by a specific node\n */\n public getPartitionsForNode(nodeId: string): number[] {\n if (!this.partitionMap) return [];\n\n return this.partitionMap.partitions\n .filter(p => p.ownerNodeId === nodeId)\n .map(p => p.partitionId);\n }\n\n /**\n * Get current partition map version\n */\n public getMapVersion(): number {\n return this.partitionMap?.version ?? 0;\n }\n\n /**\n * Check if partition map is available\n */\n public hasPartitionMap(): boolean {\n return this.partitionMap !== null;\n }\n\n /**\n * Get owner node for a key.\n * Returns null if partition map is not available.\n */\n public getOwner(key: string): string | null {\n if (!this.partitionMap) return null;\n\n const partitionId = this.getPartitionId(key);\n const partition = this.partitionMap.partitions.find(p => p.partitionId === partitionId);\n\n return partition?.ownerNodeId ?? null;\n }\n\n /**\n * Get backup nodes for a key.\n * Returns empty array if partition map is not available.\n */\n public getBackups(key: string): string[] {\n if (!this.partitionMap) return [];\n\n const partitionId = this.getPartitionId(key);\n const partition = this.partitionMap.partitions.find(p => p.partitionId === partitionId);\n\n return partition?.backupNodeIds ?? [];\n }\n\n /**\n * Get the full partition map.\n * Returns null if not available.\n */\n public getMap(): PartitionMap | null {\n return this.partitionMap;\n }\n\n /**\n * Update entire partition map.\n * Only accepts newer versions.\n */\n public updateMap(map: PartitionMap): boolean {\n if (this.partitionMap && map.version <= this.partitionMap.version) {\n return false;\n }\n\n this.partitionMap = map;\n this.lastRefreshTime = Date.now();\n\n // Update connection pool with node endpoints\n this.updateConnectionPool(map);\n\n const changesCount = map.partitions.length;\n logger.info({\n version: map.version,\n partitions: map.partitionCount,\n nodes: map.nodes.length\n }, 'Partition map updated via updateMap');\n\n this.emit('partitionMap:updated', map.version, changesCount);\n return true;\n }\n\n /**\n * Update a single partition (for delta updates).\n */\n public updatePartition(partitionId: number, owner: string, backups: string[]): void {\n if (!this.partitionMap) return;\n\n const partition = this.partitionMap.partitions.find(p => p.partitionId === partitionId);\n if (partition) {\n partition.ownerNodeId = owner;\n partition.backupNodeIds = backups;\n }\n }\n\n /**\n * Check if partition map is stale\n */\n public isMapStale(): boolean {\n if (!this.partitionMap) return true;\n\n const now = Date.now();\n return (now - this.lastRefreshTime) > this.config.maxMapStalenessMs;\n }\n\n /**\n * Request fresh partition map from server\n */\n public async refreshPartitionMap(): Promise<void> {\n if (this.pendingRefresh) {\n return this.pendingRefresh;\n }\n\n this.pendingRefresh = this.doRefreshPartitionMap();\n\n try {\n await this.pendingRefresh;\n } finally {\n this.pendingRefresh = null;\n }\n }\n\n /**\n * Start periodic partition map refresh\n */\n public startPeriodicRefresh(): void {\n if (this.refreshTimer) return;\n\n this.refreshTimer = setInterval(() => {\n if (this.isMapStale()) {\n this.emit('partitionMap:stale', this.getMapVersion(), this.lastRefreshTime);\n this.refreshPartitionMap().catch(err => {\n logger.error({ error: err }, 'Failed to refresh partition map');\n });\n }\n }, this.config.mapRefreshIntervalMs);\n }\n\n /**\n * Stop periodic refresh\n */\n public stopPeriodicRefresh(): void {\n if (this.refreshTimer) {\n clearInterval(this.refreshTimer);\n this.refreshTimer = null;\n }\n }\n\n /**\n * Handle NOT_OWNER error from server\n */\n public handleNotOwnerError(key: string, actualOwner: string, newMapVersion: number): void {\n const routing = this.route(key);\n const expectedOwner = routing?.nodeId ?? 'unknown';\n\n this.emit('routing:miss', key, expectedOwner, actualOwner);\n\n // If server has newer map, request it\n if (newMapVersion > this.getMapVersion()) {\n this.refreshPartitionMap().catch(err => {\n logger.error({ error: err }, 'Failed to refresh partition map after NOT_OWNER');\n });\n }\n }\n\n /**\n * Get statistics about routing\n */\n public getStats(): {\n mapVersion: number;\n partitionCount: number;\n nodeCount: number;\n lastRefresh: number;\n isStale: boolean;\n } {\n return {\n mapVersion: this.getMapVersion(),\n partitionCount: this.partitionMap?.partitionCount ?? 0,\n nodeCount: this.partitionMap?.nodes.length ?? 0,\n lastRefresh: this.lastRefreshTime,\n isStale: this.isMapStale(),\n };\n }\n\n /**\n * Cleanup resources\n */\n public close(): void {\n this.stopPeriodicRefresh();\n this.partitionMap = null;\n }\n\n // ============================================\n // Private Methods\n // ============================================\n\n private handlePartitionMap(message: PartitionMapMessage): void {\n const newMap = message.payload;\n\n // Only accept newer versions\n if (this.partitionMap && newMap.version <= this.partitionMap.version) {\n logger.debug({\n current: this.partitionMap.version,\n received: newMap.version\n }, 'Ignoring older partition map');\n return;\n }\n\n this.partitionMap = newMap;\n this.lastRefreshTime = Date.now();\n\n // Update connection pool with node endpoints\n this.updateConnectionPool(newMap);\n\n const changesCount = newMap.partitions.length;\n logger.info({\n version: newMap.version,\n partitions: newMap.partitionCount,\n nodes: newMap.nodes.length\n }, 'Partition map updated');\n\n this.emit('partitionMap:updated', newMap.version, changesCount);\n }\n\n private handlePartitionMapDelta(message: PartitionMapDeltaMessage): void {\n const delta = message.payload;\n\n // Must have base map and correct previous version\n if (!this.partitionMap) {\n logger.warn('Received delta but no base map, requesting full map');\n this.refreshPartitionMap();\n return;\n }\n\n if (delta.previousVersion !== this.partitionMap.version) {\n logger.warn({\n expected: this.partitionMap.version,\n received: delta.previousVersion\n }, 'Delta version mismatch, requesting full map');\n this.refreshPartitionMap();\n return;\n }\n\n // Apply changes\n for (const change of delta.changes) {\n this.applyPartitionChange(change);\n }\n\n this.partitionMap.version = delta.version;\n this.lastRefreshTime = Date.now();\n\n logger.info({\n version: delta.version,\n changes: delta.changes.length\n }, 'Applied partition map delta');\n\n this.emit('partitionMap:updated', delta.version, delta.changes.length);\n }\n\n private applyPartitionChange(change: PartitionChange): void {\n if (!this.partitionMap) return;\n\n const partition = this.partitionMap.partitions.find(p => p.partitionId === change.partitionId);\n if (partition) {\n partition.ownerNodeId = change.newOwner;\n // Backups would also be updated but simplified here\n }\n }\n\n private updateConnectionPool(map: PartitionMap): void {\n // Add new nodes\n for (const node of map.nodes) {\n if (node.status === 'ACTIVE' || node.status === 'JOINING') {\n this.connectionPool.addNode(node.nodeId, node.endpoints.websocket);\n }\n }\n\n // Remove nodes that are no longer in the map\n const currentNodeIds = new Set(map.nodes.map(n => n.nodeId));\n for (const nodeId of this.connectionPool.getAllNodes()) {\n if (!currentNodeIds.has(nodeId)) {\n this.connectionPool.removeNode(nodeId);\n }\n }\n }\n\n private async doRefreshPartitionMap(): Promise<void> {\n logger.debug('Requesting partition map refresh');\n\n // Send request to any connected node\n const sent = this.connectionPool.sendToPrimary({\n type: 'PARTITION_MAP_REQUEST',\n payload: {\n currentVersion: this.getMapVersion(),\n },\n });\n\n if (!sent) {\n throw new Error('No connection available to request partition map');\n }\n\n // Wait for response (handled via message event)\n return new Promise((resolve, reject) => {\n const timeout = setTimeout(() => {\n this.removeListener('partitionMap:updated', onUpdate);\n reject(new Error('Partition map refresh timeout'));\n }, 5000);\n\n const onUpdate = () => {\n clearTimeout(timeout);\n this.removeListener('partitionMap:updated', onUpdate);\n resolve();\n };\n\n this.once('partitionMap:updated', onUpdate);\n });\n }\n}\n","import type { LWWRecord, ORMapRecord } from '@topgunbuild/core';\nimport type { IStorageAdapter, OpLogEntry } from '../IStorageAdapter';\nimport { openDB } from 'idb';\nimport type { IDBPDatabase } from 'idb';\n\n/**\n * Represents an operation queued before IndexedDB is ready.\n */\ninterface QueuedOperation {\n type: 'put' | 'remove' | 'setMeta' | 'appendOpLog' | 'markOpsSynced' | 'batchPut';\n args: any[];\n resolve: (value: any) => void;\n reject: (error: any) => void;\n}\n\n/**\n * Non-blocking IndexedDB adapter that allows immediate use before initialization completes.\n *\n * Operations are queued in memory and replayed once IndexedDB is ready.\n * This enables true \"memory-first\" behavior where the UI can render immediately\n * without waiting for IndexedDB to initialize (which can take 50-500ms).\n */\nexport class IDBAdapter implements IStorageAdapter {\n private dbPromise?: Promise<IDBPDatabase>;\n private db?: IDBPDatabase;\n private isReady = false;\n private operationQueue: QueuedOperation[] = [];\n private initPromise?: Promise<void>;\n\n /**\n * Initializes IndexedDB in the background.\n * Returns immediately - does NOT block on IndexedDB being ready.\n * Use waitForReady() if you need to ensure initialization is complete.\n */\n async initialize(dbName: string): Promise<void> {\n // Start initialization but don't await it\n this.initPromise = this.initializeInternal(dbName);\n // Return immediately - non-blocking!\n }\n\n /**\n * Internal initialization that actually opens IndexedDB.\n */\n private async initializeInternal(dbName: string): Promise<void> {\n try {\n this.dbPromise = openDB(dbName, 2, {\n upgrade(db) {\n if (!db.objectStoreNames.contains('kv_store')) {\n db.createObjectStore('kv_store', { keyPath: 'key' });\n }\n if (!db.objectStoreNames.contains('op_log')) {\n db.createObjectStore('op_log', { keyPath: 'id', autoIncrement: true });\n }\n if (!db.objectStoreNames.contains('meta_store')) {\n db.createObjectStore('meta_store', { keyPath: 'key' });\n }\n },\n });\n\n this.db = await this.dbPromise;\n this.isReady = true;\n\n // Replay queued operations\n await this.flushQueue();\n } catch (error) {\n // Re-throw to allow error handling\n throw error;\n }\n }\n\n /**\n * Waits for IndexedDB to be fully initialized.\n * Call this if you need guaranteed persistence before proceeding.\n */\n async waitForReady(): Promise<void> {\n if (this.isReady) return;\n if (this.initPromise) {\n await this.initPromise;\n }\n }\n\n /**\n * Flushes all queued operations once IndexedDB is ready.\n */\n private async flushQueue(): Promise<void> {\n const queue = this.operationQueue;\n this.operationQueue = [];\n\n for (const op of queue) {\n try {\n let result: any;\n switch (op.type) {\n case 'put':\n result = await this.putInternal(op.args[0], op.args[1]);\n break;\n case 'remove':\n result = await this.removeInternal(op.args[0]);\n break;\n case 'setMeta':\n result = await this.setMetaInternal(op.args[0], op.args[1]);\n break;\n case 'appendOpLog':\n result = await this.appendOpLogInternal(op.args[0]);\n break;\n case 'markOpsSynced':\n result = await this.markOpsSyncedInternal(op.args[0]);\n break;\n case 'batchPut':\n result = await this.batchPutInternal(op.args[0]);\n break;\n }\n op.resolve(result);\n } catch (error) {\n op.reject(error);\n }\n }\n }\n\n /**\n * Queues an operation if not ready, or executes immediately if ready.\n */\n private queueOrExecute<T>(\n type: QueuedOperation['type'],\n args: any[],\n executor: () => Promise<T>\n ): Promise<T> {\n if (this.isReady) {\n return executor();\n }\n\n return new Promise<T>((resolve, reject) => {\n this.operationQueue.push({ type, args, resolve, reject });\n });\n }\n\n async close(): Promise<void> {\n if (this.db) {\n this.db.close();\n }\n }\n\n // ============================================\n // Read Operations - Wait for ready\n // ============================================\n\n async get<V>(key: string): Promise<LWWRecord<V> | ORMapRecord<V>[] | any | undefined> {\n // Read operations must wait for DB to be ready\n await this.waitForReady();\n const result = await this.db?.get('kv_store', key);\n return result?.value;\n }\n\n async getMeta(key: string): Promise<any> {\n await this.waitForReady();\n const result = await this.db?.get('meta_store', key);\n return result?.value;\n }\n\n async getPendingOps(): Promise<OpLogEntry[]> {\n await this.waitForReady();\n const all = await this.db?.getAll('op_log');\n return all?.filter((op: any) => op.synced === 0) || [];\n }\n\n async getAllKeys(): Promise<string[]> {\n await this.waitForReady();\n return (await this.db?.getAllKeys('kv_store')) as string[] || [];\n }\n\n // ============================================\n // Write Operations - Queue if not ready\n // ============================================\n\n async put(key: string, value: any): Promise<void> {\n return this.queueOrExecute('put', [key, value], () => this.putInternal(key, value));\n }\n\n private async putInternal(key: string, value: any): Promise<void> {\n await this.db?.put('kv_store', { key, value });\n }\n\n async remove(key: string): Promise<void> {\n return this.queueOrExecute('remove', [key], () => this.removeInternal(key));\n }\n\n private async removeInternal(key: string): Promise<void> {\n await this.db?.delete('kv_store', key);\n }\n\n async setMeta(key: string, value: any): Promise<void> {\n return this.queueOrExecute('setMeta', [key, value], () => this.setMetaInternal(key, value));\n }\n\n private async setMetaInternal(key: string, value: any): Promise<void> {\n await this.db?.put('meta_store', { key, value });\n }\n\n async batchPut(entries: Map<string, any>): Promise<void> {\n return this.queueOrExecute('batchPut', [entries], () => this.batchPutInternal(entries));\n }\n\n private async batchPutInternal(entries: Map<string, any>): Promise<void> {\n const tx = this.db?.transaction('kv_store', 'readwrite');\n if (!tx) return;\n\n await Promise.all(\n Array.from(entries.entries()).map(([key, value]) =>\n tx.store.put({ key, value })\n )\n );\n await tx.done;\n }\n\n async appendOpLog(entry: any): Promise<number> {\n return this.queueOrExecute('appendOpLog', [entry], () => this.appendOpLogInternal(entry));\n }\n\n private async appendOpLogInternal(entry: any): Promise<number> {\n const entryToSave = { ...entry, synced: 0 };\n return await this.db?.add('op_log', entryToSave) as number;\n }\n\n async markOpsSynced(lastId: number): Promise<void> {\n return this.queueOrExecute('markOpsSynced', [lastId], () => this.markOpsSyncedInternal(lastId));\n }\n\n private async markOpsSyncedInternal(lastId: number): Promise<void> {\n const tx = this.db?.transaction('op_log', 'readwrite');\n if (!tx) return;\n\n let cursor = await tx.store.openCursor();\n while (cursor) {\n if (cursor.value.id <= lastId) {\n const update = { ...cursor.value, synced: 1 };\n await cursor.update(update);\n }\n cursor = await cursor.continue();\n }\n await tx.done;\n }\n}\n\n","import { TopGunClient } from './TopGunClient';\nimport { IDBAdapter } from './adapters/IDBAdapter';\nimport type { IStorageAdapter } from './IStorageAdapter';\nimport { LWWMap } from '@topgunbuild/core';\nimport type { LWWRecord } from '@topgunbuild/core';\n\nexport interface TopGunConfig {\n sync: string;\n persist: 'indexeddb' | IStorageAdapter;\n nodeId?: string;\n}\n\n// Generic schema type\nexport type TopGunSchema = Record<string, any>;\n\nconst handler: ProxyHandler<TopGun<any>> = {\n get(target, prop, receiver) {\n if (prop in target || typeof prop === 'symbol') {\n return Reflect.get(target, prop, receiver);\n }\n if (typeof prop === 'string') {\n return target.collection(prop);\n }\n return undefined;\n }\n};\n\nexport class TopGun<T extends TopGunSchema = any> {\n private client: TopGunClient;\n private initPromise: Promise<void>;\n \n // Allow property access for collections based on Schema T\n [key: string]: any;\n\n constructor(config: TopGunConfig) {\n let storage: IStorageAdapter;\n\n if (config.persist === 'indexeddb') {\n storage = new IDBAdapter();\n } else if (typeof config.persist === 'object') {\n storage = config.persist;\n } else {\n throw new Error(`Unsupported persist option: ${config.persist}`);\n }\n\n this.client = new TopGunClient({\n serverUrl: config.sync,\n storage,\n nodeId: config.nodeId\n });\n\n // Start client initialization (non-blocking)\n // The IDBAdapter now initializes in the background and queues operations\n this.initPromise = this.client.start().catch(err => {\n console.error('Failed to start TopGun client:', err);\n throw err;\n });\n\n return new Proxy(this, handler);\n }\n\n /**\n * Waits for the storage adapter to be fully initialized.\n * This is optional - you can start using the database immediately.\n * Operations are queued in memory and persisted once IndexedDB is ready.\n */\n public async waitForReady(): Promise<void> {\n await this.initPromise;\n }\n\n public collection<K extends keyof T & string>(name: K): CollectionWrapper<T[K]> {\n // Explicitly type the map\n const map = this.client.getMap<string, T[K]>(name);\n return new CollectionWrapper<T[K]>(map);\n }\n}\n\nexport class CollectionWrapper<ItemType = any> {\n private map: LWWMap<string, ItemType>;\n\n constructor(map: LWWMap<string, ItemType>) {\n this.map = map;\n }\n\n /**\n * Sets an item in the collection. \n * The item MUST have an 'id' or '_id' field.\n */\n async set(value: ItemType): Promise<ItemType> {\n const v = value as any;\n const key = v.id || v._id;\n if (!key) {\n throw new Error('Object must have an \"id\" or \"_id\" property to be saved in a collection.');\n }\n \n // LWWMap.set is synchronous in updating memory and queueing ops,\n // but we return a Promise to match typical async DB APIs.\n this.map.set(key, value);\n return Promise.resolve(value); \n }\n \n /**\n * Retrieves an item by ID.\n * Returns the value directly (unwrapped from CRDT record).\n */\n get(key: string): ItemType | undefined {\n return this.map.get(key);\n }\n\n /**\n * Get the raw LWWRecord (including metadata like timestamp).\n */\n getRecord(key: string): LWWRecord<ItemType> | undefined {\n return this.map.getRecord(key);\n }\n\n // Expose raw map if needed for advanced usage\n get raw() {\n return this.map;\n }\n}\n","import { serialize, deserialize } from '@topgunbuild/core';\n\nexport class EncryptionManager {\n private static ALGORITHM = 'AES-GCM';\n private static IV_LENGTH = 12;\n\n /**\n * Encrypts data using AES-GCM.\n * Serializes data to MessagePack before encryption.\n */\n static async encrypt(key: CryptoKey, data: any): Promise<{ iv: Uint8Array; data: Uint8Array }> {\n const encoded = serialize(data);\n\n // Generate IV\n const iv = window.crypto.getRandomValues(new Uint8Array(EncryptionManager.IV_LENGTH));\n\n // Encrypt\n const ciphertext = await window.crypto.subtle.encrypt(\n {\n name: EncryptionManager.ALGORITHM,\n iv: iv,\n },\n key,\n encoded as any\n );\n\n return {\n iv,\n data: new Uint8Array(ciphertext),\n };\n }\n\n /**\n * Decrypts AES-GCM encrypted data.\n * Deserializes from MessagePack after decryption.\n */\n static async decrypt(key: CryptoKey, record: { iv: Uint8Array; data: Uint8Array }): Promise<any> {\n try {\n const plaintextBuffer = await window.crypto.subtle.decrypt(\n {\n name: EncryptionManager.ALGORITHM,\n iv: record.iv as any,\n },\n key,\n record.data as any\n );\n\n return deserialize(new Uint8Array(plaintextBuffer));\n } catch (err) {\n console.error('Decryption failed', err);\n throw new Error('Failed to decrypt data: ' + err);\n }\n }\n}\n","import { IStorageAdapter, OpLogEntry } from '../IStorageAdapter';\nimport { EncryptionManager } from '../crypto/EncryptionManager';\n\n/**\n * Wraps an underlying storage adapter and encrypts data at rest using AES-GCM.\n */\nexport class EncryptedStorageAdapter implements IStorageAdapter {\n constructor(\n private wrapped: IStorageAdapter,\n private key: CryptoKey\n ) { }\n\n async initialize(dbName: string): Promise<void> {\n return this.wrapped.initialize(dbName);\n }\n\n async close(): Promise<void> {\n return this.wrapped.close();\n }\n\n // --- KV Operations ---\n\n async get<V>(key: string): Promise<V | any | undefined> {\n const raw = await this.wrapped.get<any>(key);\n\n if (!raw) {\n return undefined;\n }\n\n // Check if it looks like an encrypted record\n // We expect { iv: Uint8Array, data: Uint8Array }\n // Note: In a real app we might want a stricter check or a version tag.\n if (this.isEncryptedRecord(raw)) {\n try {\n return await EncryptionManager.decrypt(this.key, raw);\n } catch (e) {\n // Fallback for migration or corruption?\n // For now, fail loud as per spec.\n throw e;\n }\n }\n\n // Return raw if not encrypted (backwards compatibility during dev, or unencrypted data)\n return raw;\n }\n\n async put(key: string, value: any): Promise<void> {\n const encrypted = await EncryptionManager.encrypt(this.key, value);\n // Store as plain object to be compatible with structured clone algorithm of IndexedDB\n const storedValue = {\n iv: encrypted.iv,\n data: encrypted.data\n };\n return this.wrapped.put(key, storedValue);\n }\n\n async remove(key: string): Promise<void> {\n return this.wrapped.remove(key);\n }\n\n // --- Metadata ---\n\n async getMeta(key: string): Promise<any> {\n const raw = await this.wrapped.getMeta(key);\n if (!raw) return undefined;\n\n if (this.isEncryptedRecord(raw)) {\n return EncryptionManager.decrypt(this.key, raw);\n }\n return raw;\n }\n\n async setMeta(key: string, value: any): Promise<void> {\n const encrypted = await EncryptionManager.encrypt(this.key, value);\n return this.wrapped.setMeta(key, {\n iv: encrypted.iv,\n data: encrypted.data\n });\n }\n\n // --- Batch ---\n\n async batchPut(entries: Map<string, any>): Promise<void> {\n const encryptedEntries = new Map<string, any>();\n\n for (const [key, value] of entries.entries()) {\n const encrypted = await EncryptionManager.encrypt(this.key, value);\n encryptedEntries.set(key, {\n iv: encrypted.iv,\n data: encrypted.data\n });\n }\n\n return this.wrapped.batchPut(encryptedEntries);\n }\n\n // --- OpLog ---\n\n async appendOpLog(entry: Omit<OpLogEntry, 'id'>): Promise<number> {\n // Encrypt sensitive fields: value, record, orRecord\n const encryptedEntry = { ...entry };\n\n if (entry.value !== undefined) {\n const enc = await EncryptionManager.encrypt(this.key, entry.value);\n encryptedEntry.value = { iv: enc.iv, data: enc.data };\n }\n\n if (entry.record !== undefined) {\n const enc = await EncryptionManager.encrypt(this.key, entry.record);\n encryptedEntry.record = { iv: enc.iv, data: enc.data } as any;\n }\n\n if (entry.orRecord !== undefined) {\n const enc = await EncryptionManager.encrypt(this.key, entry.orRecord);\n encryptedEntry.orRecord = { iv: enc.iv, data: enc.data } as any;\n }\n\n // Note: 'key', 'op', 'mapName', 'orTag', 'hlc', 'synced' remain plaintext for indexing\n\n return this.wrapped.appendOpLog(encryptedEntry);\n }\n\n async getPendingOps(): Promise<OpLogEntry[]> {\n const ops = await this.wrapped.getPendingOps();\n\n // Decrypt in place\n // We map concurrently for performance\n return Promise.all(ops.map(async op => {\n const decryptedOp = { ...op };\n\n if (this.isEncryptedRecord(op.value)) {\n decryptedOp.value = await EncryptionManager.decrypt(this.key, op.value);\n }\n\n if (this.isEncryptedRecord(op.record)) {\n decryptedOp.record = await EncryptionManager.decrypt(this.key, op.record as any);\n }\n\n if (this.isEncryptedRecord(op.orRecord)) {\n decryptedOp.orRecord = await EncryptionManager.decrypt(this.key, op.orRecord as any);\n }\n\n return decryptedOp;\n }));\n }\n\n async markOpsSynced(lastId: number): Promise<void> {\n return this.wrapped.markOpsSynced(lastId);\n }\n\n // --- Iteration ---\n\n async getAllKeys(): Promise<string[]> {\n return this.wrapped.getAllKeys();\n }\n\n // --- Helpers ---\n\n private isEncryptedRecord(data: any): data is { iv: Uint8Array, data: Uint8Array } {\n return data &&\n typeof data === 'object' &&\n data.iv instanceof Uint8Array &&\n data.data instanceof Uint8Array;\n }\n}\n","import { SyncEngine } from './SyncEngine';\nimport { TopGunClient, DEFAULT_CLUSTER_CONFIG } from './TopGunClient';\nimport { TopGun } from './TopGun';\nexport * from './adapters/IDBAdapter';\nexport * from './adapters/EncryptedStorageAdapter';\nimport { QueryHandle } from './QueryHandle';\nimport { LWWMap, Predicates } from '@topgunbuild/core';\nimport { TopicHandle } from './TopicHandle';\nimport { SyncState, VALID_TRANSITIONS, isValidTransition } from './SyncState';\nimport { SyncStateMachine } from './SyncStateMachine';\nimport { BackpressureError } from './errors/BackpressureError';\nimport { DEFAULT_BACKPRESSURE_CONFIG } from './BackpressureConfig';\n\n// Cluster imports (Phase 4)\nimport { ConnectionPool, PartitionRouter, ClusterClient } from './cluster';\n\n// Connection provider imports (Phase 4.5)\nimport { SingleServerProvider } from './connection';\n\n// Type imports\nimport type { IStorageAdapter, OpLogEntry } from './IStorageAdapter';\nimport type { LWWRecord, PredicateNode } from '@topgunbuild/core';\nimport type { QueryFilter, QueryResultItem, QueryResultSource } from './QueryHandle';\nimport type { TopicCallback } from './TopicHandle';\nimport type { BackoffConfig, HeartbeatConfig, SyncEngineConfig } from './SyncEngine';\nimport type { StateChangeEvent, StateChangeListener, SyncStateMachineConfig } from './SyncStateMachine';\nimport type {\n BackpressureConfig,\n BackpressureStrategy,\n BackpressureStatus,\n BackpressureThresholdEvent,\n OperationDroppedEvent,\n} from './BackpressureConfig';\n\n// Value exports\nexport { SyncEngine, TopGunClient, TopGun, QueryHandle, LWWMap, Predicates, TopicHandle };\nexport { SyncState, VALID_TRANSITIONS, isValidTransition, SyncStateMachine };\nexport { BackpressureError, DEFAULT_BACKPRESSURE_CONFIG, DEFAULT_CLUSTER_CONFIG };\nexport { logger } from './utils/logger';\n\n// Cluster exports (Phase 4)\nexport { ConnectionPool, PartitionRouter, ClusterClient };\nexport type {\n ConnectionPoolEvents,\n RoutingResult,\n PartitionRouterEvents,\n ClusterClientEvents,\n ClusterRoutingMode,\n RoutingMetrics,\n CircuitState,\n} from './cluster';\n\n// Connection provider exports (Phase 4.5)\nexport { SingleServerProvider };\nexport type {\n IConnectionProvider,\n ConnectionProviderEvent,\n ConnectionEventHandler,\n SingleServerProviderConfig,\n} from './types';\n\n// TopGunClient cluster config types (Phase 4.5)\nexport type { TopGunClusterConfig, TopGunClientConfig } from './TopGunClient';\n\n// Type exports\nexport type {\n IStorageAdapter,\n OpLogEntry,\n LWWRecord,\n PredicateNode,\n QueryFilter,\n QueryResultItem,\n QueryResultSource,\n TopicCallback,\n BackoffConfig,\n HeartbeatConfig,\n SyncEngineConfig,\n StateChangeEvent,\n StateChangeListener,\n SyncStateMachineConfig,\n BackpressureConfig,\n BackpressureStrategy,\n BackpressureStatus,\n BackpressureThresholdEvent,\n OperationDroppedEvent,\n};\n"],"mappings":";AAAA,SAAS,KAAK,QAAQ,OAAO,WAAW,aAAa,yBAAyB;;;ACA9E,OAAO,UAAU;AAGjB,IAAM,YAAY,OAAO,WAAW;AAKpC,IAAM,WAAY,OAAO,YAAY,eAAe,QAAQ,OAAO,QAAQ,IAAI,aAAc;AAEtF,IAAM,SAAS,KAAK;AAAA,EACzB,OAAO;AAAA,EACP,WAAW,CAAC,cAAc,OAAO,YAAY,eAAe,QAAQ,IAAI,aAAa,gBAAgB;AAAA,IACnG,QAAQ;AAAA,IACR,SAAS;AAAA,MACP,UAAU;AAAA,MACV,eAAe;AAAA,MACf,QAAQ;AAAA,IACV;AAAA,EACF,IAAI;AAAA,EACJ,SAAS;AAAA,IACP,UAAU;AAAA,EACZ;AACF,CAAC;;;ACpBM,IAAK,YAAL,kBAAKA,eAAL;AAEL,EAAAA,WAAA,aAAU;AAEV,EAAAA,WAAA,gBAAa;AAEb,EAAAA,WAAA,oBAAiB;AAEjB,EAAAA,WAAA,aAAU;AAEV,EAAAA,WAAA,eAAY;AAEZ,EAAAA,WAAA,kBAAe;AAEf,EAAAA,WAAA,aAAU;AAEV,EAAAA,WAAA,WAAQ;AAhBE,SAAAA;AAAA,GAAA;AAuBL,IAAM,oBAAoD;AAAA,EAC/D,CAAC,uBAAiB,GAAG,CAAC,6BAAoB;AAAA,EAC1C,CAAC,6BAAoB,GAAG,CAAC,uCAA0B,yBAAmB,qBAAiB,iCAAsB;AAAA,EAC7G,CAAC,qCAAwB,GAAG,CAAC,yBAAmB,yBAAmB,qBAAiB,iCAAsB;AAAA,EAC1G,CAAC,uBAAiB,GAAG,CAAC,6BAAqB,yBAAmB,qBAAiB,iCAAsB;AAAA,EACrG,CAAC,2BAAmB,GAAG,CAA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cAAc,QAA+B;AAC/C,WAAO,KAAK,QAAQ,cAAc,MAAM;AAAA,EAC5C;AAAA;AAAA,EAIA,MAAM,aAAgC;AAClC,WAAO,KAAK,QAAQ,WAAW;AAAA,EACnC;AAAA;AAAA,EAIQ,kBAAkB,MAAyD;AAC/E,WAAO,QACH,OAAO,SAAS,YAChB,KAAK,cAAc,cACnB,KAAK,gBAAgB;AAAA,EAC7B;AACJ;;;AC9JA,SAAS,UAAAC,SAAQ,kBAAkB;","names":["SyncState","handler","token","handler","LWWMap","ORMap","DEFAULT_CONNECTION_POOL_CONFIG","DEFAULT_PARTITION_ROUTER_CONFIG","serialize","serialize","deserialize","serialize","deserialize","DEFAULT_CONNECTION_POOL_CONFIG","DEFAULT_PARTITION_ROUTER_CONFIG","serialize","handler","LWWMap","ORMap","serialize","deserialize","LWWMap"]}
|