@mesh-kit/server 2.0.5 → 2.0.7

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 CHANGED
@@ -3,9 +3,9 @@ export { ServerOptions } from 'ws';
3
3
  import { LogLevel, Status, Command } from '@mesh-kit/shared';
4
4
  import { EventEmitter } from 'node:events';
5
5
  import { IncomingMessage } from 'node:http';
6
- import Redis$1, { RedisOptions, Redis } from 'ioredis';
7
- import { EventEmitter as EventEmitter$1 } from 'events';
6
+ import Redis, { RedisOptions, Redis as Redis$1 } from 'ioredis';
8
7
  import { Operation } from 'fast-json-patch';
8
+ import { EventEmitter as EventEmitter$1 } from 'events';
9
9
 
10
10
  declare class Latency {
11
11
  start: number;
@@ -71,21 +71,19 @@ interface ChannelPersistenceOptions {
71
71
  */
72
72
  maxBufferSize?: number;
73
73
  }
74
- interface RecordPersistenceOptions {
75
- /**
76
- * Optional adapter override for this pattern
77
- */
74
+ interface RecordPersistenceAdapterConfig {
78
75
  adapter?: PersistenceAdapter;
79
- /**
80
- * How often (in ms) to flush buffered records to the database
81
- * @default 500
82
- */
76
+ restorePattern: string;
77
+ }
78
+ interface RecordPersistenceHooksConfig {
79
+ persist: (records: CustomPersistedRecord[]) => Promise<void>;
80
+ restore: () => Promise<CustomPersistedRecord[]>;
81
+ }
82
+ interface RecordPersistenceConfig {
83
+ pattern: string | RegExp;
84
+ adapter?: RecordPersistenceAdapterConfig;
85
+ hooks?: RecordPersistenceHooksConfig;
83
86
  flushInterval?: number;
84
- /**
85
- * Maximum number of records to hold in memory before flushing
86
- * If this limit is reached, the buffer is flushed immediately
87
- * @default 100
88
- */
89
87
  maxBufferSize?: number;
90
88
  }
91
89
  interface PersistedRecord {
@@ -94,6 +92,11 @@ interface PersistedRecord {
94
92
  value: string;
95
93
  timestamp: number;
96
94
  }
95
+ interface CustomPersistedRecord {
96
+ recordId: string;
97
+ value: unknown;
98
+ version: number;
99
+ }
97
100
  interface PersistenceAdapterOptions {
98
101
  /**
99
102
  * Database file path for file-based adapters
@@ -112,6 +115,11 @@ interface PostgreSQLAdapterOptions extends PersistenceAdapterOptions {
112
115
  max?: number;
113
116
  }
114
117
 
118
+ interface AuthenticationError {
119
+ code?: number;
120
+ message?: string;
121
+ }
122
+ type AuthenticateConnectionFn = (req: IncomingMessage) => Promise<any> | any;
115
123
  type SocketMiddleware = (context: MeshContext<any>) => any | Promise<any>;
116
124
  interface MeshServerOptions extends ServerOptions {
117
125
  /**
@@ -172,6 +180,31 @@ interface MeshServerOptions extends ServerOptions {
172
180
  * @default "sqlite"
173
181
  */
174
182
  persistenceAdapter?: "sqlite" | "postgres";
183
+ /**
184
+ * Called during WebSocket upgrade to authenticate the connection.
185
+ * Receives the raw HTTP request with headers and cookies.
186
+ *
187
+ * Return any truthy value to accept the connection - the returned data
188
+ * will be automatically stored as the connection's initial metadata.
189
+ *
190
+ * Throw an error or return null/undefined to reject with 401 Unauthorized.
191
+ * Throw an object with { code, message } for custom HTTP status codes.
192
+ *
193
+ * @example
194
+ * ```ts
195
+ * authenticateConnection: async (req) => {
196
+ * const cookies = parseCookie(req.headers.cookie || "");
197
+ * const token = cookies["auth_token"];
198
+ *
199
+ * const user = await validateToken(token);
200
+ * if (!user) throw { code: 401, message: "Invalid token" };
201
+ *
202
+ * // returned data becomes connection metadata
203
+ * return { userId: user.id, email: user.email };
204
+ * }
205
+ * ```
206
+ */
207
+ authenticateConnection?: AuthenticateConnectionFn;
175
208
  }
176
209
  type ChannelPattern$1 = string | RegExp;
177
210
 
@@ -195,230 +228,10 @@ declare class Connection extends EventEmitter {
195
228
  close(): Promise<boolean>;
196
229
  }
197
230
 
198
- declare class RecordManager {
199
- private redis;
200
- private recordUpdateCallbacks;
201
- private recordRemovedCallbacks;
202
- private server;
203
- constructor(options: {
204
- redis: Redis;
205
- server: MeshServer;
206
- });
207
- /**
208
- * Gets the server instance associated with this record manager
209
- */
210
- getServer(): MeshServer;
211
- /**
212
- * Gets the Redis instance used by this record manager
213
- * This is used by the persistence manager to restore records
214
- */
215
- getRedis(): Redis;
216
- recordKey(recordId: string): string;
217
- recordVersionKey(recordId: string): string;
218
- /**
219
- * Retrieves a record from Redis by its unique identifier. Attempts to parse
220
- * the stored data as JSON before returning. If the record does not exist,
221
- * returns null.
222
- *
223
- * @param {string} recordId - The unique identifier of the record to retrieve.
224
- * @returns {Promise<any | null>} A promise that resolves to the parsed record object,
225
- * or null if the record does not exist.
226
- * @throws {SyntaxError} If the stored data is not valid JSON and cannot be parsed.
227
- * @throws {Error} If an error occurs during the Redis operation.
228
- */
229
- getRecord(recordId: string): Promise<any | null>;
230
- /**
231
- * Retrieves the version number associated with the specified record ID from Redis.
232
- * If no version is found, returns 0.
233
- *
234
- * @param {string} recordId - The unique identifier for the record whose version is to be retrieved.
235
- * @returns {Promise<number>} A promise that resolves to the version number of the record. Returns 0 if not found.
236
- * @throws {Error} If there is an issue communicating with Redis or parsing the version.
237
- */
238
- getVersion(recordId: string): Promise<number>;
239
- /**
240
- * Retrieves a record and its associated version from Redis.
241
- * Fetches both the record data and its version by their respective keys.
242
- *
243
- * @param {string} recordId - The unique identifier for the record to retrieve.
244
- * @returns {Promise<{ record: any | null; version: number }>}
245
- * A promise that resolves to an object containing the parsed record (or null if not found)
246
- * and its version number (0 if version data is not found or invalid).
247
- * @throws {Error} If there is a Redis error or if JSON parsing fails for the record data.
248
- */
249
- getRecordAndVersion(recordId: string): Promise<{
250
- record: any | null;
251
- version: number;
252
- }>;
253
- /**
254
- * Publishes an update to a record by computing and applying a JSON Patch,
255
- * incrementing the version, and persisting the updated value and version in Redis.
256
- * If there are no changes between the old and new value, returns null.
257
- *
258
- * @param {string} recordId - The unique identifier of the record to update.
259
- * @param {any} newValue - The new value to set for the record, or partial value when using merge strategy.
260
- * @param {"replace" | "merge" | "deepMerge"} [strategy="replace"] - Update strategy: "replace" (default) replaces the entire record, "merge" merges with existing object properties, "deepMerge" recursively merges nested objects.
261
- * @returns {Promise<{ patch: Operation[]; version: number; finalValue: any } | null>}
262
- * A promise resolving to an object containing the JSON Patch operations, new version number, and final merged value,
263
- * or null if there were no changes to publish.
264
- * @throws {Error} If there is a failure reading or writing to Redis, or during patch computation, the promise will be rejected with the error.
265
- */
266
- publishUpdate(recordId: string, newValue: any, strategy?: "replace" | "merge" | "deepMerge"): Promise<{
267
- patch: Operation[];
268
- version: number;
269
- finalValue: any;
270
- } | null>;
271
- /**
272
- * Deletes a record and its associated version from Redis storage.
273
- *
274
- * @param {string} recordId - The unique identifier of the record to be deleted.
275
- * @returns {Promise<{ version: number }|null>} A promise that resolves to the final version of the deleted record, or null if the record didn't exist.
276
- * @throws {Error} If an error occurs during the Redis pipeline execution, the promise will be rejected with the error.
277
- */
278
- deleteRecord(recordId: string): Promise<{
279
- version: number;
280
- } | null>;
281
- /**
282
- * Registers a callback function to be called when a record is updated.
283
- *
284
- * @param {(data: { recordId: string; value: any }) => Promise<void> | void} callback - The callback function to execute when a record is updated.
285
- * @returns {() => void} A function that, when called, will unregister the callback.
286
- */
287
- onRecordUpdate(callback: (data: {
288
- recordId: string;
289
- value: any;
290
- }) => Promise<void> | void): () => void;
291
- /**
292
- * Registers a callback function to be called when a record is removed.
293
- *
294
- * @param {(data: { recordId: string; value: any }) => Promise<void> | void} callback - The callback function to execute when a record is removed.
295
- * @returns {() => void} A function that, when called, will unregister the callback.
296
- */
297
- onRecordRemoved(callback: (data: {
298
- recordId: string;
299
- value: any;
300
- }) => Promise<void> | void): () => void;
301
- }
302
-
303
- type RecordPersistencePattern = string | RegExp | {
304
- writePattern: string | RegExp;
305
- restorePattern: string | RegExp;
306
- };
307
- declare class PersistenceManager extends EventEmitter$1 {
308
- private defaultAdapter;
309
- private channelPatterns;
310
- private recordPatterns;
311
- private messageBuffer;
312
- private recordBuffer;
313
- private flushTimers;
314
- private recordFlushTimer;
315
- private isShuttingDown;
316
- private initialized;
317
- private recordManager;
318
- private pendingRecordUpdates;
319
- private messageStream;
320
- constructor(options: {
321
- defaultAdapterOptions?: any;
322
- adapterType?: "sqlite" | "postgres";
323
- });
324
- /**
325
- * Sets the record manager reference for record restoration
326
- * @param recordManager The record manager instance
327
- */
328
- setRecordManager(recordManager: RecordManager): void;
329
- /**
330
- * Waits until the persistence manager is fully ready and initialized.
331
- *
332
- * @returns {Promise<void>} A promise that resolves when persistence is ready.
333
- */
334
- ready(): Promise<void>;
335
- /**
336
- * Processes any record updates that were buffered during initialization
337
- */
338
- private processPendingRecordUpdates;
339
- initialize(): Promise<void>;
340
- /**
341
- * Restores persisted records from storage into Redis on startup
342
- */
343
- restorePersistedRecords(): Promise<void>;
344
- /**
345
- * Handle a message received from the internal message stream.
346
- */
347
- private handleStreamMessage;
348
- /**
349
- * Enable persistence for channels matching the given pattern.
350
- * @param pattern string or regexp pattern to match channel names
351
- * @param options persistence options
352
- */
353
- enableChannelPersistence(pattern: string | RegExp, options?: ChannelPersistenceOptions): void;
354
- /**
355
- * Enable persistence for records matching the given pattern.
356
- * @param pattern string, regexp, or object with writePattern/restorePattern
357
- * @param options persistence options
358
- */
359
- enableRecordPersistence(pattern: RecordPersistencePattern, options?: RecordPersistenceOptions): void;
360
- /**
361
- * Check if a channel has persistence enabled and return its options.
362
- * @param channel channel name to check
363
- * @returns the persistence options if enabled, undefined otherwise
364
- */
365
- getChannelPersistenceOptions(channel: string): Required<ChannelPersistenceOptions> | undefined;
366
- /**
367
- * Check if a record has persistence enabled and return its options.
368
- * @param recordId record ID to check
369
- * @returns the persistence options if enabled, undefined otherwise
370
- */
371
- getRecordPersistenceOptions(recordId: string): Required<RecordPersistenceOptions> | undefined;
372
- /**
373
- * Handle an incoming message for potential persistence.
374
- * @param channel channel the message was published to
375
- * @param message the message content
376
- * @param instanceId id of the server instance
377
- */
378
- handleChannelMessage(channel: string, message: string, instanceId: string, timestamp?: number): void;
379
- /**
380
- * Flush buffered messages for a specific channel to its adapter.
381
- * @param channel channel to flush
382
- */
383
- private flushChannel;
384
- /**
385
- * Flush all buffered messages across all channels.
386
- */
387
- flushAll(): Promise<void>;
388
- /**
389
- * Get persisted messages for a channel.
390
- * @param channel channel to get messages for
391
- * @param since optional cursor (timestamp or message id) to retrieve messages after
392
- * @param limit maximum number of messages to retrieve
393
- */
394
- getMessages(channel: string, since?: string | number, limit?: number): Promise<PersistedMessage[]>;
395
- /**
396
- * Handles a record update for potential persistence
397
- * @param recordId ID of the record
398
- * @param value record value (will be stringified)
399
- * @param version record version
400
- */
401
- handleRecordUpdate(recordId: string, value: any, version: number): void;
402
- /**
403
- * Flush all buffered records to storage
404
- */
405
- flushRecords(): Promise<void>;
406
- /**
407
- * Retrieve persisted records matching a pattern
408
- * @param pattern pattern to match record IDs
409
- * @returns array of persisted records
410
- */
411
- getPersistedRecords(pattern: string): Promise<PersistedRecord[]>;
412
- /**
413
- * Shutdown the persistence manager, flushing pending messages and closing adapters.
414
- */
415
- shutdown(): Promise<void>;
416
- }
417
-
418
231
  declare class RoomManager {
419
232
  private redis;
420
233
  constructor(options: {
421
- redis: Redis$1;
234
+ redis: Redis;
422
235
  });
423
236
  private roomKey;
424
237
  private connectionsRoomKey;
@@ -554,7 +367,7 @@ declare class ConnectionManager {
554
367
  private localConnections;
555
368
  private roomManager;
556
369
  constructor(options: {
557
- redis: Redis$1;
370
+ redis: Redis;
558
371
  instanceId: string;
559
372
  roomManager: RoomManager;
560
373
  });
@@ -638,21 +451,21 @@ declare class RedisManager {
638
451
  * @returns The Redis client
639
452
  * @throws Error if Redis is not initialized
640
453
  */
641
- get redis(): Redis;
454
+ get redis(): Redis$1;
642
455
  /**
643
456
  * Gets the Redis client for publishing
644
457
  *
645
458
  * @returns The publishing Redis client
646
459
  * @throws Error if Redis is not initialized
647
460
  */
648
- get pubClient(): Redis;
461
+ get pubClient(): Redis$1;
649
462
  /**
650
463
  * Gets the Redis client for subscribing
651
464
  *
652
465
  * @returns The subscribing Redis client
653
466
  * @throws Error if Redis is not initialized
654
467
  */
655
- get subClient(): Redis;
468
+ get subClient(): Redis$1;
656
469
  /**
657
470
  * Disconnects all Redis clients
658
471
  */
@@ -695,7 +508,7 @@ declare class PresenceManager {
695
508
  private roomTTLs;
696
509
  private defaultTTL;
697
510
  constructor(options: {
698
- redis: Redis;
511
+ redis: Redis$1;
699
512
  roomManager: RoomManager;
700
513
  redisManager: RedisManager;
701
514
  enableExpirationEvents?: boolean;
@@ -768,6 +581,111 @@ declare class PresenceManager {
768
581
  cleanup(): Promise<void>;
769
582
  }
770
583
 
584
+ declare class RecordManager {
585
+ private redis;
586
+ private recordUpdateCallbacks;
587
+ private recordRemovedCallbacks;
588
+ private server;
589
+ constructor(options: {
590
+ redis: Redis$1;
591
+ server: MeshServer;
592
+ });
593
+ /**
594
+ * Gets the server instance associated with this record manager
595
+ */
596
+ getServer(): MeshServer;
597
+ /**
598
+ * Gets the Redis instance used by this record manager
599
+ * This is used by the persistence manager to restore records
600
+ */
601
+ getRedis(): Redis$1;
602
+ recordKey(recordId: string): string;
603
+ recordVersionKey(recordId: string): string;
604
+ /**
605
+ * Retrieves a record from Redis by its unique identifier. Attempts to parse
606
+ * the stored data as JSON before returning. If the record does not exist,
607
+ * returns null.
608
+ *
609
+ * @param {string} recordId - The unique identifier of the record to retrieve.
610
+ * @returns {Promise<any | null>} A promise that resolves to the parsed record object,
611
+ * or null if the record does not exist.
612
+ * @throws {SyntaxError} If the stored data is not valid JSON and cannot be parsed.
613
+ * @throws {Error} If an error occurs during the Redis operation.
614
+ */
615
+ getRecord(recordId: string): Promise<any | null>;
616
+ /**
617
+ * Retrieves the version number associated with the specified record ID from Redis.
618
+ * If no version is found, returns 0.
619
+ *
620
+ * @param {string} recordId - The unique identifier for the record whose version is to be retrieved.
621
+ * @returns {Promise<number>} A promise that resolves to the version number of the record. Returns 0 if not found.
622
+ * @throws {Error} If there is an issue communicating with Redis or parsing the version.
623
+ */
624
+ getVersion(recordId: string): Promise<number>;
625
+ /**
626
+ * Retrieves a record and its associated version from Redis.
627
+ * Fetches both the record data and its version by their respective keys.
628
+ *
629
+ * @param {string} recordId - The unique identifier for the record to retrieve.
630
+ * @returns {Promise<{ record: any | null; version: number }>}
631
+ * A promise that resolves to an object containing the parsed record (or null if not found)
632
+ * and its version number (0 if version data is not found or invalid).
633
+ * @throws {Error} If there is a Redis error or if JSON parsing fails for the record data.
634
+ */
635
+ getRecordAndVersion(recordId: string): Promise<{
636
+ record: any | null;
637
+ version: number;
638
+ }>;
639
+ /**
640
+ * Publishes an update to a record by computing and applying a JSON Patch,
641
+ * incrementing the version, and persisting the updated value and version in Redis.
642
+ * If there are no changes between the old and new value, returns null.
643
+ *
644
+ * @param {string} recordId - The unique identifier of the record to update.
645
+ * @param {any} newValue - The new value to set for the record, or partial value when using merge strategy.
646
+ * @param {"replace" | "merge" | "deepMerge"} [strategy="replace"] - Update strategy: "replace" (default) replaces the entire record, "merge" merges with existing object properties, "deepMerge" recursively merges nested objects.
647
+ * @returns {Promise<{ patch: Operation[]; version: number; finalValue: any } | null>}
648
+ * A promise resolving to an object containing the JSON Patch operations, new version number, and final merged value,
649
+ * or null if there were no changes to publish.
650
+ * @throws {Error} If there is a failure reading or writing to Redis, or during patch computation, the promise will be rejected with the error.
651
+ */
652
+ publishUpdate(recordId: string, newValue: any, strategy?: "replace" | "merge" | "deepMerge"): Promise<{
653
+ patch: Operation[];
654
+ version: number;
655
+ finalValue: any;
656
+ } | null>;
657
+ /**
658
+ * Deletes a record and its associated version from Redis storage.
659
+ *
660
+ * @param {string} recordId - The unique identifier of the record to be deleted.
661
+ * @returns {Promise<{ version: number }|null>} A promise that resolves to the final version of the deleted record, or null if the record didn't exist.
662
+ * @throws {Error} If an error occurs during the Redis pipeline execution, the promise will be rejected with the error.
663
+ */
664
+ deleteRecord(recordId: string): Promise<{
665
+ version: number;
666
+ } | null>;
667
+ /**
668
+ * Registers a callback function to be called when a record is updated.
669
+ *
670
+ * @param {(data: { recordId: string; value: any }) => Promise<void> | void} callback - The callback function to execute when a record is updated.
671
+ * @returns {() => void} A function that, when called, will unregister the callback.
672
+ */
673
+ onRecordUpdate(callback: (data: {
674
+ recordId: string;
675
+ value: any;
676
+ }) => Promise<void> | void): () => void;
677
+ /**
678
+ * Registers a callback function to be called when a record is removed.
679
+ *
680
+ * @param {(data: { recordId: string; value: any }) => Promise<void> | void} callback - The callback function to execute when a record is removed.
681
+ * @returns {() => void} A function that, when called, will unregister the callback.
682
+ */
683
+ onRecordRemoved(callback: (data: {
684
+ recordId: string;
685
+ value: any;
686
+ }) => Promise<void> | void): () => void;
687
+ }
688
+
771
689
  declare class MeshServer extends WebSocketServer {
772
690
  readonly instanceId: string;
773
691
  private redisManager;
@@ -779,6 +697,7 @@ declare class MeshServer extends WebSocketServer {
779
697
  private collectionManager;
780
698
  private broadcastManager;
781
699
  private persistenceManager;
700
+ private authenticateConnection?;
782
701
  roomManager: RoomManager;
783
702
  recordManager: RecordManager;
784
703
  connectionManager: ConnectionManager;
@@ -860,12 +779,9 @@ declare class MeshServer extends WebSocketServer {
860
779
  enableChannelPersistence(pattern: ChannelPattern$1, options?: ChannelPersistenceOptions): void;
861
780
  /**
862
781
  * Enables persistence for records matching the specified pattern.
863
- *
864
- * @param {RecordPersistencePattern} pattern - The record ID pattern to enable persistence for.
865
- * @param {RecordPersistenceOptions} [options] - Options for persistence.
866
- * @throws {Error} If persistence is not enabled for this server instance.
782
+ * Use either adapter (for mesh's internal JSON blob storage) or hooks (for custom DB persistence).
867
783
  */
868
- enableRecordPersistence(pattern: RecordPersistencePattern, options?: RecordPersistenceOptions): void;
784
+ enableRecordPersistence(config: RecordPersistenceConfig): void;
869
785
  /**
870
786
  * Exposes a record or pattern for client subscriptions, optionally adding a guard function.
871
787
  *
@@ -1130,4 +1046,127 @@ declare class MessageStream extends EventEmitter$1 {
1130
1046
  }) => void): void;
1131
1047
  }
1132
1048
 
1133
- export { type ChannelPattern$1 as ChannelPattern, Connection, ConnectionManager, MeshContext, MeshServer, type MeshServerOptions, MessageStream, type PersistenceAdapter, type PersistenceAdapterOptions, PersistenceManager, type PostgreSQLAdapterOptions, PostgreSQLPersistenceAdapter, PresenceManager, RecordManager, type RecordPersistencePattern, RoomManager, SQLitePersistenceAdapter, type SocketMiddleware };
1049
+ interface RecordPatternConfig {
1050
+ pattern: string | RegExp;
1051
+ adapter?: {
1052
+ adapter: PersistenceAdapter;
1053
+ restorePattern: string;
1054
+ };
1055
+ hooks?: {
1056
+ persist: (records: CustomPersistedRecord[]) => Promise<void>;
1057
+ restore: () => Promise<CustomPersistedRecord[]>;
1058
+ };
1059
+ flushInterval: number;
1060
+ maxBufferSize: number;
1061
+ }
1062
+ declare class PersistenceManager extends EventEmitter$1 {
1063
+ private defaultAdapter;
1064
+ private channelPatterns;
1065
+ private recordPatterns;
1066
+ private messageBuffer;
1067
+ private recordBuffer;
1068
+ private flushTimers;
1069
+ private recordFlushTimer;
1070
+ private isShuttingDown;
1071
+ private initialized;
1072
+ private recordManager;
1073
+ private pendingRecordUpdates;
1074
+ private messageStream;
1075
+ constructor(options: {
1076
+ defaultAdapterOptions?: any;
1077
+ adapterType?: "sqlite" | "postgres";
1078
+ });
1079
+ /**
1080
+ * Sets the record manager reference for record restoration
1081
+ * @param recordManager The record manager instance
1082
+ */
1083
+ setRecordManager(recordManager: RecordManager): void;
1084
+ /**
1085
+ * Waits until the persistence manager is fully ready and initialized.
1086
+ *
1087
+ * @returns {Promise<void>} A promise that resolves when persistence is ready.
1088
+ */
1089
+ ready(): Promise<void>;
1090
+ /**
1091
+ * Processes any record updates that were buffered during initialization
1092
+ */
1093
+ private processPendingRecordUpdates;
1094
+ initialize(): Promise<void>;
1095
+ /**
1096
+ * Restores persisted records from storage into Redis on startup
1097
+ */
1098
+ restorePersistedRecords(): Promise<void>;
1099
+ /**
1100
+ * Handle a message received from the internal message stream.
1101
+ */
1102
+ private handleStreamMessage;
1103
+ /**
1104
+ * Enable persistence for channels matching the given pattern.
1105
+ * @param pattern string or regexp pattern to match channel names
1106
+ * @param options persistence options
1107
+ */
1108
+ enableChannelPersistence(pattern: string | RegExp, options?: ChannelPersistenceOptions): void;
1109
+ /**
1110
+ * Enable persistence for records matching the given pattern.
1111
+ * Use either adapter (for mesh's internal JSON blob storage) or hooks (for custom DB persistence).
1112
+ */
1113
+ enableRecordPersistence(config: RecordPersistenceConfig): void;
1114
+ /**
1115
+ * Check if a channel has persistence enabled and return its options.
1116
+ * @param channel channel name to check
1117
+ * @returns the persistence options if enabled, undefined otherwise
1118
+ */
1119
+ getChannelPersistenceOptions(channel: string): Required<ChannelPersistenceOptions> | undefined;
1120
+ /**
1121
+ * Check if a record has persistence enabled and return its config.
1122
+ * @param recordId record ID to check
1123
+ * @returns the persistence config if enabled, undefined otherwise
1124
+ */
1125
+ getRecordPersistenceConfig(recordId: string): RecordPatternConfig | undefined;
1126
+ /**
1127
+ * Handle an incoming message for potential persistence.
1128
+ * @param channel channel the message was published to
1129
+ * @param message the message content
1130
+ * @param instanceId id of the server instance
1131
+ */
1132
+ handleChannelMessage(channel: string, message: string, instanceId: string, timestamp?: number): void;
1133
+ /**
1134
+ * Flush buffered messages for a specific channel to its adapter.
1135
+ * @param channel channel to flush
1136
+ */
1137
+ private flushChannel;
1138
+ /**
1139
+ * Flush all buffered messages across all channels.
1140
+ */
1141
+ flushAll(): Promise<void>;
1142
+ /**
1143
+ * Get persisted messages for a channel.
1144
+ * @param channel channel to get messages for
1145
+ * @param since optional cursor (timestamp or message id) to retrieve messages after
1146
+ * @param limit maximum number of messages to retrieve
1147
+ */
1148
+ getMessages(channel: string, since?: string | number, limit?: number): Promise<PersistedMessage[]>;
1149
+ /**
1150
+ * Handles a record update for potential persistence
1151
+ * @param recordId ID of the record
1152
+ * @param value record value (will be stringified)
1153
+ * @param version record version
1154
+ */
1155
+ handleRecordUpdate(recordId: string, value: any, version: number): void;
1156
+ /**
1157
+ * Flush all buffered records to storage
1158
+ */
1159
+ flushRecords(): Promise<void>;
1160
+ /**
1161
+ * Retrieve persisted records matching a pattern
1162
+ * @param pattern pattern to match record IDs
1163
+ * @returns array of persisted records
1164
+ */
1165
+ getPersistedRecords(pattern: string): Promise<PersistedRecord[]>;
1166
+ /**
1167
+ * Shutdown the persistence manager, flushing pending messages and closing adapters.
1168
+ */
1169
+ shutdown(): Promise<void>;
1170
+ }
1171
+
1172
+ export { type AuthenticateConnectionFn, type AuthenticationError, type ChannelPattern$1 as ChannelPattern, Connection, ConnectionManager, type CustomPersistedRecord, MeshContext, MeshServer, type MeshServerOptions, MessageStream, type PersistenceAdapter, type PersistenceAdapterOptions, PersistenceManager, type PostgreSQLAdapterOptions, PostgreSQLPersistenceAdapter, PresenceManager, RecordManager, type RecordPersistenceConfig, RoomManager, SQLitePersistenceAdapter, type SocketMiddleware };