@voidhash/mimic 0.0.1-alpha.7 → 0.0.1-alpha.8
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/.turbo/turbo-build.log +43 -15
- package/dist/Document-ChuFrTk1.cjs +571 -0
- package/dist/Document-CwiAFTIq.mjs +438 -0
- package/dist/Document-CwiAFTIq.mjs.map +1 -0
- package/dist/Presence-DKKP4v5X.d.cts +91 -0
- package/dist/Presence-DKKP4v5X.d.cts.map +1 -0
- package/dist/Presence-DdMVKcOv.mjs +110 -0
- package/dist/Presence-DdMVKcOv.mjs.map +1 -0
- package/dist/Presence-N8u7Eppr.d.mts +91 -0
- package/dist/Presence-N8u7Eppr.d.mts.map +1 -0
- package/dist/Presence-gWrmGBeu.cjs +126 -0
- package/dist/Primitive-BK7kfHJZ.d.cts +1165 -0
- package/dist/Primitive-BK7kfHJZ.d.cts.map +1 -0
- package/dist/Primitive-D1kdB6za.d.mts +1165 -0
- package/dist/Primitive-D1kdB6za.d.mts.map +1 -0
- package/dist/client/index.cjs +1456 -0
- package/dist/client/index.d.cts +692 -0
- package/dist/client/index.d.cts.map +1 -0
- package/dist/client/index.d.mts +692 -0
- package/dist/client/index.d.mts.map +1 -0
- package/dist/client/index.mjs +1413 -0
- package/dist/client/index.mjs.map +1 -0
- package/dist/index.cjs +224 -765
- package/dist/index.d.cts +5 -1152
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +5 -1152
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +69 -569
- package/dist/index.mjs.map +1 -1
- package/dist/server/index.cjs +191 -0
- package/dist/server/index.d.cts +148 -0
- package/dist/server/index.d.cts.map +1 -0
- package/dist/server/index.d.mts +148 -0
- package/dist/server/index.d.mts.map +1 -0
- package/dist/server/index.mjs +182 -0
- package/dist/server/index.mjs.map +1 -0
- package/package.json +16 -4
- package/src/primitives/Array.ts +25 -14
- package/src/primitives/Boolean.ts +29 -17
- package/src/primitives/Either.ts +30 -17
- package/src/primitives/Lazy.ts +16 -2
- package/src/primitives/Literal.ts +29 -18
- package/src/primitives/Number.ts +35 -24
- package/src/primitives/String.ts +36 -23
- package/src/primitives/Struct.ts +74 -26
- package/src/primitives/Tree.ts +30 -14
- package/src/primitives/Union.ts +21 -21
- package/src/primitives/shared.ts +27 -34
- package/tests/primitives/Array.test.ts +108 -0
- package/tests/primitives/Struct.test.ts +2 -2
- package/tests/primitives/Tree.test.ts +128 -0
- package/tsdown.config.ts +1 -1
- /package/dist/{chunk-C6wwvPpM.mjs → chunk-CLMFDpHK.mjs} +0 -0
|
@@ -0,0 +1,692 @@
|
|
|
1
|
+
import { a as InferState, f as Operation, h as __export, l as EncodedTransaction, n as AnyPrimitive, o as TransformResult$1, r as InferProxy, u as Transaction } from "../Primitive-BK7kfHJZ.cjs";
|
|
2
|
+
import { i as Presence_d_exports, n as Infer, r as PresenceEntry$1, t as AnyPresence } from "../Presence-DKKP4v5X.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/client/Transport.d.ts
|
|
5
|
+
declare namespace Transport_d_exports {
|
|
6
|
+
export { AuthMessage, AuthResultMessage, ClientMessage, EncodedClientMessage, EncodedServerMessage, EncodedSubmitTransactionMessage, EncodedTransactionMessage, ErrorMessage, PingMessage, PongMessage, PresenceClearMessage, PresenceEntry, PresenceRemoveMessage, PresenceSetMessage, PresenceSnapshotMessage, PresenceUpdateMessage, RequestSnapshotMessage, ServerMessage, SnapshotMessage, SubmitTransactionMessage, TransactionMessage, Transport, TransportEvent, TransportEventHandler, TransportOptions };
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Message received when the server broadcasts a committed transaction.
|
|
10
|
+
*/
|
|
11
|
+
interface TransactionMessage {
|
|
12
|
+
readonly type: "transaction";
|
|
13
|
+
readonly transaction: Transaction;
|
|
14
|
+
/** Server-assigned version number for ordering */
|
|
15
|
+
readonly version: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Message received when requesting or receiving a full state snapshot.
|
|
19
|
+
*/
|
|
20
|
+
interface SnapshotMessage {
|
|
21
|
+
readonly type: "snapshot";
|
|
22
|
+
readonly state: unknown;
|
|
23
|
+
readonly version: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Message received when the server rejects a transaction.
|
|
27
|
+
*/
|
|
28
|
+
interface ErrorMessage {
|
|
29
|
+
readonly type: "error";
|
|
30
|
+
readonly transactionId: string;
|
|
31
|
+
readonly reason: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Message received in response to a ping (heartbeat).
|
|
35
|
+
*/
|
|
36
|
+
interface PongMessage {
|
|
37
|
+
readonly type: "pong";
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Message received after authentication attempt.
|
|
41
|
+
*/
|
|
42
|
+
interface AuthResultMessage {
|
|
43
|
+
readonly type: "auth_result";
|
|
44
|
+
readonly success: boolean;
|
|
45
|
+
readonly error?: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A presence entry as transmitted.
|
|
49
|
+
*/
|
|
50
|
+
interface PresenceEntry {
|
|
51
|
+
/** The presence data */
|
|
52
|
+
readonly data: unknown;
|
|
53
|
+
/** Optional user ID from authentication */
|
|
54
|
+
readonly userId?: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Message received with initial presence state after authentication.
|
|
58
|
+
*/
|
|
59
|
+
interface PresenceSnapshotMessage {
|
|
60
|
+
readonly type: "presence_snapshot";
|
|
61
|
+
/** This connection's ID (used to identify self) */
|
|
62
|
+
readonly selfId: string;
|
|
63
|
+
/** Map of connectionId to presence entry */
|
|
64
|
+
readonly presences: Record<string, PresenceEntry>;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Message received when another user updates their presence.
|
|
68
|
+
*/
|
|
69
|
+
interface PresenceUpdateMessage {
|
|
70
|
+
readonly type: "presence_update";
|
|
71
|
+
/** The connection ID of the user who updated */
|
|
72
|
+
readonly id: string;
|
|
73
|
+
/** The presence data */
|
|
74
|
+
readonly data: unknown;
|
|
75
|
+
/** Optional user ID from authentication */
|
|
76
|
+
readonly userId?: string;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Message received when another user's presence is removed (disconnect).
|
|
80
|
+
*/
|
|
81
|
+
interface PresenceRemoveMessage {
|
|
82
|
+
readonly type: "presence_remove";
|
|
83
|
+
/** The connection ID of the user who disconnected */
|
|
84
|
+
readonly id: string;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Union of all possible server messages.
|
|
88
|
+
*/
|
|
89
|
+
type ServerMessage = TransactionMessage | SnapshotMessage | ErrorMessage | PongMessage | AuthResultMessage | PresenceSnapshotMessage | PresenceUpdateMessage | PresenceRemoveMessage;
|
|
90
|
+
/**
|
|
91
|
+
* Message sent to submit a transaction to the server.
|
|
92
|
+
*/
|
|
93
|
+
interface SubmitTransactionMessage {
|
|
94
|
+
readonly type: "submit";
|
|
95
|
+
readonly transaction: Transaction;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Message sent to request a full state snapshot.
|
|
99
|
+
*/
|
|
100
|
+
interface RequestSnapshotMessage {
|
|
101
|
+
readonly type: "request_snapshot";
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Message sent as heartbeat ping.
|
|
105
|
+
*/
|
|
106
|
+
interface PingMessage {
|
|
107
|
+
readonly type: "ping";
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Message sent to authenticate with the server.
|
|
111
|
+
*/
|
|
112
|
+
interface AuthMessage {
|
|
113
|
+
readonly type: "auth";
|
|
114
|
+
readonly token: string;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Message sent to set/update this client's presence.
|
|
118
|
+
*/
|
|
119
|
+
interface PresenceSetMessage {
|
|
120
|
+
readonly type: "presence_set";
|
|
121
|
+
/** The presence data (validated against schema on client before sending) */
|
|
122
|
+
readonly data: unknown;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Message sent to clear this client's presence.
|
|
126
|
+
*/
|
|
127
|
+
interface PresenceClearMessage {
|
|
128
|
+
readonly type: "presence_clear";
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Union of all possible client messages.
|
|
132
|
+
*/
|
|
133
|
+
type ClientMessage = SubmitTransactionMessage | RequestSnapshotMessage | PingMessage | AuthMessage | PresenceSetMessage | PresenceClearMessage;
|
|
134
|
+
/**
|
|
135
|
+
* Encoded transaction message for network transport.
|
|
136
|
+
*/
|
|
137
|
+
interface EncodedTransactionMessage {
|
|
138
|
+
readonly type: "transaction";
|
|
139
|
+
readonly transaction: EncodedTransaction;
|
|
140
|
+
readonly version: number;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Encoded submit message for network transport.
|
|
144
|
+
*/
|
|
145
|
+
interface EncodedSubmitTransactionMessage {
|
|
146
|
+
readonly type: "submit";
|
|
147
|
+
readonly transaction: EncodedTransaction;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Union of all possible encoded server messages (for network transport).
|
|
151
|
+
*/
|
|
152
|
+
type EncodedServerMessage = EncodedTransactionMessage | SnapshotMessage | ErrorMessage | PongMessage | AuthResultMessage | PresenceSnapshotMessage | PresenceUpdateMessage | PresenceRemoveMessage;
|
|
153
|
+
/**
|
|
154
|
+
* Union of all possible encoded client messages (for network transport).
|
|
155
|
+
*/
|
|
156
|
+
type EncodedClientMessage = EncodedSubmitTransactionMessage | RequestSnapshotMessage | PingMessage | AuthMessage | PresenceSetMessage | PresenceClearMessage;
|
|
157
|
+
/**
|
|
158
|
+
* Abstract transport interface for server communication.
|
|
159
|
+
* Implementations can use WebSocket, HTTP, or any other protocol.
|
|
160
|
+
*/
|
|
161
|
+
interface Transport {
|
|
162
|
+
/**
|
|
163
|
+
* Sends a transaction to the server for processing.
|
|
164
|
+
* @param transaction - The transaction to submit
|
|
165
|
+
*/
|
|
166
|
+
readonly send: (transaction: Transaction) => void;
|
|
167
|
+
/**
|
|
168
|
+
* Requests a full state snapshot from the server.
|
|
169
|
+
* Used for initial sync or recovery from drift.
|
|
170
|
+
*/
|
|
171
|
+
readonly requestSnapshot: () => void;
|
|
172
|
+
/**
|
|
173
|
+
* Subscribes to messages from the server.
|
|
174
|
+
* @param handler - Callback invoked for each server message
|
|
175
|
+
* @returns Unsubscribe function
|
|
176
|
+
*/
|
|
177
|
+
readonly subscribe: (handler: (message: ServerMessage) => void) => () => void;
|
|
178
|
+
/**
|
|
179
|
+
* Establishes connection to the server.
|
|
180
|
+
* @returns Promise that resolves when connected
|
|
181
|
+
*/
|
|
182
|
+
readonly connect: () => Promise<void>;
|
|
183
|
+
/**
|
|
184
|
+
* Disconnects from the server.
|
|
185
|
+
*/
|
|
186
|
+
readonly disconnect: () => void;
|
|
187
|
+
/**
|
|
188
|
+
* Returns whether the transport is currently connected.
|
|
189
|
+
*/
|
|
190
|
+
readonly isConnected: () => boolean;
|
|
191
|
+
/**
|
|
192
|
+
* Sends presence data to the server.
|
|
193
|
+
* The data should be validated against the presence schema before calling.
|
|
194
|
+
* @param data - The presence data to set
|
|
195
|
+
*/
|
|
196
|
+
readonly sendPresenceSet: (data: unknown) => void;
|
|
197
|
+
/**
|
|
198
|
+
* Clears this client's presence on the server.
|
|
199
|
+
*/
|
|
200
|
+
readonly sendPresenceClear: () => void;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Events emitted by the transport for connection status.
|
|
204
|
+
*/
|
|
205
|
+
type TransportEvent = {
|
|
206
|
+
type: "connected";
|
|
207
|
+
} | {
|
|
208
|
+
type: "disconnected";
|
|
209
|
+
reason?: string;
|
|
210
|
+
} | {
|
|
211
|
+
type: "reconnecting";
|
|
212
|
+
attempt: number;
|
|
213
|
+
} | {
|
|
214
|
+
type: "error";
|
|
215
|
+
error: Error;
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* Handler for transport events.
|
|
219
|
+
*/
|
|
220
|
+
type TransportEventHandler = (event: TransportEvent) => void;
|
|
221
|
+
/**
|
|
222
|
+
* Options for creating a transport.
|
|
223
|
+
*/
|
|
224
|
+
interface TransportOptions {
|
|
225
|
+
/** Handler for transport lifecycle events */
|
|
226
|
+
readonly onEvent?: TransportEventHandler;
|
|
227
|
+
/** Timeout in milliseconds for connection attempts */
|
|
228
|
+
readonly connectionTimeout?: number;
|
|
229
|
+
/** Whether to automatically reconnect on disconnect */
|
|
230
|
+
readonly autoReconnect?: boolean;
|
|
231
|
+
/** Maximum number of reconnection attempts */
|
|
232
|
+
readonly maxReconnectAttempts?: number;
|
|
233
|
+
/** Base delay between reconnection attempts (ms) */
|
|
234
|
+
readonly reconnectDelay?: number;
|
|
235
|
+
}
|
|
236
|
+
declare namespace ClientDocument_d_exports {
|
|
237
|
+
export { ClientDocument, ClientDocumentListener, ClientDocumentOptions, ClientPresence, PresenceListener, make$2 as make };
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Listener for presence changes.
|
|
241
|
+
*/
|
|
242
|
+
interface PresenceListener<_TData> {
|
|
243
|
+
/** Called when any presence changes (self or others) */
|
|
244
|
+
readonly onPresenceChange?: () => void;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Presence API exposed on the ClientDocument.
|
|
248
|
+
*/
|
|
249
|
+
interface ClientPresence<TData> {
|
|
250
|
+
/**
|
|
251
|
+
* Returns this client's connection ID (set after receiving presence_snapshot).
|
|
252
|
+
* Returns undefined before the snapshot is received.
|
|
253
|
+
*/
|
|
254
|
+
readonly selfId: () => string | undefined;
|
|
255
|
+
/**
|
|
256
|
+
* Returns this client's current presence data.
|
|
257
|
+
* Returns undefined if not set.
|
|
258
|
+
*/
|
|
259
|
+
readonly self: () => TData | undefined;
|
|
260
|
+
/**
|
|
261
|
+
* Returns a map of other clients' presence data.
|
|
262
|
+
* Keys are connection IDs.
|
|
263
|
+
*/
|
|
264
|
+
readonly others: () => ReadonlyMap<string, PresenceEntry$1<TData>>;
|
|
265
|
+
/**
|
|
266
|
+
* Returns all presence entries including self.
|
|
267
|
+
*/
|
|
268
|
+
readonly all: () => ReadonlyMap<string, PresenceEntry$1<TData>>;
|
|
269
|
+
/**
|
|
270
|
+
* Sets this client's presence data.
|
|
271
|
+
* Validates against the presence schema before sending.
|
|
272
|
+
* @throws ParseError if validation fails
|
|
273
|
+
*/
|
|
274
|
+
readonly set: (data: TData) => void;
|
|
275
|
+
/**
|
|
276
|
+
* Clears this client's presence data.
|
|
277
|
+
*/
|
|
278
|
+
readonly clear: () => void;
|
|
279
|
+
/**
|
|
280
|
+
* Subscribes to presence changes.
|
|
281
|
+
* @returns Unsubscribe function
|
|
282
|
+
*/
|
|
283
|
+
readonly subscribe: (listener: PresenceListener<TData>) => () => void;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Options for creating a ClientDocument.
|
|
287
|
+
*/
|
|
288
|
+
interface ClientDocumentOptions<TSchema extends AnyPrimitive, TPresence extends AnyPresence | undefined = undefined> {
|
|
289
|
+
/** The schema defining the document structure */
|
|
290
|
+
readonly schema: TSchema;
|
|
291
|
+
/** Transport for server communication */
|
|
292
|
+
readonly transport: Transport;
|
|
293
|
+
/** Initial state (optional, will sync from server if not provided) */
|
|
294
|
+
readonly initialState?: InferState<TSchema>;
|
|
295
|
+
/** Initial server version (optional) */
|
|
296
|
+
readonly initialVersion?: number;
|
|
297
|
+
/** Called when server rejects a transaction */
|
|
298
|
+
readonly onRejection?: (transaction: Transaction, reason: string) => void;
|
|
299
|
+
/** Called when optimistic state changes */
|
|
300
|
+
readonly onStateChange?: (state: InferState<TSchema> | undefined) => void;
|
|
301
|
+
/** Called when connection status changes */
|
|
302
|
+
readonly onConnectionChange?: (connected: boolean) => void;
|
|
303
|
+
/** Called when client is fully initialized and ready */
|
|
304
|
+
readonly onReady?: () => void;
|
|
305
|
+
/** Timeout in ms for pending transactions (default: 30000) */
|
|
306
|
+
readonly transactionTimeout?: number;
|
|
307
|
+
/** Timeout in ms for initialization (default: 10000) */
|
|
308
|
+
readonly initTimeout?: number;
|
|
309
|
+
/** Enable debug logging for all activity (default: false) */
|
|
310
|
+
readonly debug?: boolean;
|
|
311
|
+
/**
|
|
312
|
+
* Optional presence schema for ephemeral per-user data.
|
|
313
|
+
* When provided, enables the presence API on the ClientDocument.
|
|
314
|
+
*/
|
|
315
|
+
readonly presence?: TPresence;
|
|
316
|
+
/** Initial presence data, that will be set on the ClientDocument when it is created */
|
|
317
|
+
readonly initialPresence?: TPresence extends AnyPresence ? Infer<TPresence> : undefined;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Listener callbacks for subscribing to ClientDocument events.
|
|
321
|
+
*/
|
|
322
|
+
interface ClientDocumentListener<TSchema extends AnyPrimitive> {
|
|
323
|
+
/** Called when optimistic state changes */
|
|
324
|
+
readonly onStateChange?: (state: InferState<TSchema> | undefined) => void;
|
|
325
|
+
/** Called when connection status changes */
|
|
326
|
+
readonly onConnectionChange?: (connected: boolean) => void;
|
|
327
|
+
/** Called when client is fully initialized and ready */
|
|
328
|
+
readonly onReady?: () => void;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* A ClientDocument provides optimistic updates with server synchronization.
|
|
332
|
+
*/
|
|
333
|
+
interface ClientDocument<TSchema extends AnyPrimitive, TPresence extends AnyPresence | undefined = undefined> {
|
|
334
|
+
/** The schema defining this document's structure */
|
|
335
|
+
readonly schema: TSchema;
|
|
336
|
+
/** Root proxy for accessing and modifying document data (optimistic) */
|
|
337
|
+
readonly root: InferProxy<TSchema>;
|
|
338
|
+
/** Returns the current optimistic state (server + pending) */
|
|
339
|
+
get(): InferState<TSchema> | undefined;
|
|
340
|
+
/** Returns the confirmed server state */
|
|
341
|
+
getServerState(): InferState<TSchema> | undefined;
|
|
342
|
+
/** Returns the current server version */
|
|
343
|
+
getServerVersion(): number;
|
|
344
|
+
/** Returns pending transactions count */
|
|
345
|
+
getPendingCount(): number;
|
|
346
|
+
/** Returns whether there are pending transactions */
|
|
347
|
+
hasPendingChanges(): boolean;
|
|
348
|
+
/**
|
|
349
|
+
* Runs a function within a transaction.
|
|
350
|
+
* Changes are applied optimistically and sent to the server.
|
|
351
|
+
*/
|
|
352
|
+
transaction<R>(fn: (root: InferProxy<TSchema>) => R): R;
|
|
353
|
+
/**
|
|
354
|
+
* Connects to the server and starts syncing.
|
|
355
|
+
*/
|
|
356
|
+
connect(): Promise<void>;
|
|
357
|
+
/**
|
|
358
|
+
* Disconnects from the server.
|
|
359
|
+
*/
|
|
360
|
+
disconnect(): void;
|
|
361
|
+
/**
|
|
362
|
+
* Returns whether currently connected to the server.
|
|
363
|
+
*/
|
|
364
|
+
isConnected(): boolean;
|
|
365
|
+
/**
|
|
366
|
+
* Forces a full resync from the server.
|
|
367
|
+
*/
|
|
368
|
+
resync(): void;
|
|
369
|
+
/**
|
|
370
|
+
* Returns whether the client is fully initialized and ready.
|
|
371
|
+
*/
|
|
372
|
+
isReady(): boolean;
|
|
373
|
+
/**
|
|
374
|
+
* Subscribes to document events (state changes, connection changes, ready).
|
|
375
|
+
* @returns Unsubscribe function
|
|
376
|
+
*/
|
|
377
|
+
subscribe(listener: ClientDocumentListener<TSchema>): () => void;
|
|
378
|
+
/**
|
|
379
|
+
* Presence API for ephemeral per-user data.
|
|
380
|
+
* Only available when presence schema is provided in options.
|
|
381
|
+
*/
|
|
382
|
+
readonly presence: TPresence extends AnyPresence ? ClientPresence<Infer<TPresence>> : undefined;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Creates a new ClientDocument for the given schema.
|
|
386
|
+
*/
|
|
387
|
+
declare const make$2: <TSchema extends AnyPrimitive, TPresence extends AnyPresence | undefined = undefined>(options: ClientDocumentOptions<TSchema, TPresence>) => ClientDocument<TSchema, TPresence>;
|
|
388
|
+
declare namespace WebSocketTransport_d_exports {
|
|
389
|
+
export { WebSocketTransportOptions, make$1 as make };
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Options for creating a WebSocket transport.
|
|
393
|
+
*/
|
|
394
|
+
interface WebSocketTransportOptions extends TransportOptions {
|
|
395
|
+
/** WebSocket URL (ws:// or wss://) - base URL without document path */
|
|
396
|
+
readonly url: string;
|
|
397
|
+
/** Document ID to connect to. Will be appended to URL as /doc/{documentId} */
|
|
398
|
+
readonly documentId?: string;
|
|
399
|
+
/** WebSocket subprotocols */
|
|
400
|
+
readonly protocols?: string[];
|
|
401
|
+
/** Authentication token or function that returns a token */
|
|
402
|
+
readonly authToken?: string | (() => string | Promise<string>);
|
|
403
|
+
/** Interval between heartbeat pings (ms). Default: 30000 */
|
|
404
|
+
readonly heartbeatInterval?: number;
|
|
405
|
+
/** Timeout to wait for pong response (ms). Default: 10000 */
|
|
406
|
+
readonly heartbeatTimeout?: number;
|
|
407
|
+
/** Maximum delay between reconnection attempts (ms). Default: 30000 */
|
|
408
|
+
readonly maxReconnectDelay?: number;
|
|
409
|
+
}
|
|
410
|
+
declare const make$1: (options: WebSocketTransportOptions) => Transport;
|
|
411
|
+
declare namespace Rebase_d_exports {
|
|
412
|
+
export { TransformResult, rebaseAfterRejection, rebaseAfterRejectionWithPrimitive, rebasePendingTransactions, rebasePendingTransactionsWithPrimitive, transformOperation, transformOperationWithPrimitive, transformTransaction, transformTransactionWithPrimitive };
|
|
413
|
+
}
|
|
414
|
+
type TransformResult = TransformResult$1;
|
|
415
|
+
/**
|
|
416
|
+
* Transforms a client operation against a server operation using a primitive.
|
|
417
|
+
*
|
|
418
|
+
* This delegates to the primitive's transformOperation method, which handles
|
|
419
|
+
* type-specific conflict resolution.
|
|
420
|
+
*
|
|
421
|
+
* @param clientOp - The client's operation to transform
|
|
422
|
+
* @param serverOp - The server's operation that has already been applied
|
|
423
|
+
* @param primitive - The root primitive to use for transformation
|
|
424
|
+
* @returns TransformResult indicating how the client operation should be handled
|
|
425
|
+
*/
|
|
426
|
+
declare const transformOperationWithPrimitive: (clientOp: Operation<any, any, any>, serverOp: Operation<any, any, any>, primitive: AnyPrimitive) => TransformResult;
|
|
427
|
+
/**
|
|
428
|
+
* Transforms a client operation against a server operation.
|
|
429
|
+
*
|
|
430
|
+
* This is a standalone implementation for cases where the primitive is not available.
|
|
431
|
+
* For schema-aware transformation, use transformOperationWithPrimitive instead.
|
|
432
|
+
*
|
|
433
|
+
* The key principle: client ops "shadow" server ops for the same path,
|
|
434
|
+
* meaning if both touch the same field, the client's intention wins
|
|
435
|
+
* (since it was made with knowledge of the server state at that time).
|
|
436
|
+
*/
|
|
437
|
+
declare const transformOperation: (clientOp: Operation<any, any, any>, serverOp: Operation<any, any, any>) => TransformResult;
|
|
438
|
+
/**
|
|
439
|
+
* Transforms all operations in a client transaction against a server transaction.
|
|
440
|
+
* Uses the primitive's transformOperation for schema-aware transformation.
|
|
441
|
+
*/
|
|
442
|
+
declare const transformTransactionWithPrimitive: (clientTx: Transaction, serverTx: Transaction, primitive: AnyPrimitive) => Transaction;
|
|
443
|
+
/**
|
|
444
|
+
* Transforms all operations in a client transaction against a server transaction.
|
|
445
|
+
* This is a standalone version that doesn't require a primitive.
|
|
446
|
+
*/
|
|
447
|
+
declare const transformTransaction: (clientTx: Transaction, serverTx: Transaction) => Transaction;
|
|
448
|
+
/**
|
|
449
|
+
* Rebases a list of pending transactions against a server transaction using a primitive.
|
|
450
|
+
*
|
|
451
|
+
* This is called when a server transaction arrives that is NOT one of our pending
|
|
452
|
+
* transactions. We need to transform all pending transactions to work correctly
|
|
453
|
+
* on top of the new server state.
|
|
454
|
+
*/
|
|
455
|
+
declare const rebasePendingTransactionsWithPrimitive: (pendingTxs: ReadonlyArray<Transaction>, serverTx: Transaction, primitive: AnyPrimitive) => Transaction[];
|
|
456
|
+
/**
|
|
457
|
+
* Rebases a list of pending transactions against a server transaction.
|
|
458
|
+
*
|
|
459
|
+
* This is called when a server transaction arrives that is NOT one of our pending
|
|
460
|
+
* transactions. We need to transform all pending transactions to work correctly
|
|
461
|
+
* on top of the new server state.
|
|
462
|
+
*/
|
|
463
|
+
declare const rebasePendingTransactions: (pendingTxs: ReadonlyArray<Transaction>, serverTx: Transaction) => Transaction[];
|
|
464
|
+
/**
|
|
465
|
+
* Rebases pending transactions after a rejection using a primitive.
|
|
466
|
+
*
|
|
467
|
+
* When a transaction is rejected, we need to re-transform remaining pending
|
|
468
|
+
* transactions as if the rejected transaction never happened. This is done by
|
|
469
|
+
* rebuilding from the original operations against the current server state.
|
|
470
|
+
*
|
|
471
|
+
* @param originalPendingTxs - The original pending transactions before any rebasing
|
|
472
|
+
* @param rejectedTxId - ID of the rejected transaction
|
|
473
|
+
* @param serverTxsSinceOriginal - Server transactions that have arrived since original
|
|
474
|
+
* @param primitive - The root primitive to use for transformation
|
|
475
|
+
*/
|
|
476
|
+
declare const rebaseAfterRejectionWithPrimitive: (originalPendingTxs: ReadonlyArray<Transaction>, rejectedTxId: string, serverTxsSinceOriginal: ReadonlyArray<Transaction>, primitive: AnyPrimitive) => Transaction[];
|
|
477
|
+
/**
|
|
478
|
+
* Rebases pending transactions after a rejection.
|
|
479
|
+
*
|
|
480
|
+
* When a transaction is rejected, we need to re-transform remaining pending
|
|
481
|
+
* transactions as if the rejected transaction never happened. This is done by
|
|
482
|
+
* rebuilding from the original operations against the current server state.
|
|
483
|
+
*
|
|
484
|
+
* @param originalPendingTxs - The original pending transactions before any rebasing
|
|
485
|
+
* @param rejectedTxId - ID of the rejected transaction
|
|
486
|
+
* @param serverTxsSinceOriginal - Server transactions that have arrived since original
|
|
487
|
+
*/
|
|
488
|
+
declare const rebaseAfterRejection: (originalPendingTxs: ReadonlyArray<Transaction>, rejectedTxId: string, serverTxsSinceOriginal: ReadonlyArray<Transaction>) => Transaction[];
|
|
489
|
+
declare namespace StateMonitor_d_exports {
|
|
490
|
+
export { PendingInfo, RecoveryAction, StateMonitor, StateMonitorEvent, StateMonitorEventHandler, StateMonitorOptions, StateMonitorStatus, determineRecoveryAction, make };
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Events emitted by the state monitor.
|
|
494
|
+
*/
|
|
495
|
+
type StateMonitorEvent = {
|
|
496
|
+
type: "drift_detected";
|
|
497
|
+
expectedVersion: number;
|
|
498
|
+
receivedVersion: number;
|
|
499
|
+
} | {
|
|
500
|
+
type: "recovery_started";
|
|
501
|
+
} | {
|
|
502
|
+
type: "recovery_completed";
|
|
503
|
+
version: number;
|
|
504
|
+
} | {
|
|
505
|
+
type: "recovery_failed";
|
|
506
|
+
error: Error;
|
|
507
|
+
} | {
|
|
508
|
+
type: "pending_timeout";
|
|
509
|
+
transactionId: string;
|
|
510
|
+
elapsedMs: number;
|
|
511
|
+
} | {
|
|
512
|
+
type: "health_check";
|
|
513
|
+
pendingCount: number;
|
|
514
|
+
oldestPendingMs: number | null;
|
|
515
|
+
};
|
|
516
|
+
/**
|
|
517
|
+
* Handler for state monitor events.
|
|
518
|
+
*/
|
|
519
|
+
type StateMonitorEventHandler = (event: StateMonitorEvent) => void;
|
|
520
|
+
/**
|
|
521
|
+
* Options for creating a StateMonitor.
|
|
522
|
+
*/
|
|
523
|
+
interface StateMonitorOptions {
|
|
524
|
+
/** Handler for monitor events */
|
|
525
|
+
readonly onEvent?: StateMonitorEventHandler;
|
|
526
|
+
/** Interval for health checks in ms (default: 5000) */
|
|
527
|
+
readonly healthCheckInterval?: number;
|
|
528
|
+
/** Threshold for considering a pending transaction "stale" in ms (default: 10000) */
|
|
529
|
+
readonly stalePendingThreshold?: number;
|
|
530
|
+
/** Maximum allowed version gap before triggering recovery (default: 10) */
|
|
531
|
+
readonly maxVersionGap?: number;
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Pending transaction info for monitoring.
|
|
535
|
+
*/
|
|
536
|
+
interface PendingInfo {
|
|
537
|
+
readonly id: string;
|
|
538
|
+
readonly sentAt: number;
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* A StateMonitor watches for state drift and triggers recovery.
|
|
542
|
+
*/
|
|
543
|
+
interface StateMonitor {
|
|
544
|
+
/**
|
|
545
|
+
* Called when a server transaction is received.
|
|
546
|
+
* Returns true if the version is valid, false if drift is detected.
|
|
547
|
+
*/
|
|
548
|
+
readonly onServerVersion: (version: number) => boolean;
|
|
549
|
+
/**
|
|
550
|
+
* Called when a pending transaction is added.
|
|
551
|
+
*/
|
|
552
|
+
readonly trackPending: (info: PendingInfo) => void;
|
|
553
|
+
/**
|
|
554
|
+
* Called when a pending transaction is confirmed or rejected.
|
|
555
|
+
*/
|
|
556
|
+
readonly untrackPending: (id: string) => void;
|
|
557
|
+
/**
|
|
558
|
+
* Returns pending transactions that have exceeded the stale threshold.
|
|
559
|
+
*/
|
|
560
|
+
readonly getStalePending: () => PendingInfo[];
|
|
561
|
+
/**
|
|
562
|
+
* Returns current monitoring status.
|
|
563
|
+
*/
|
|
564
|
+
readonly getStatus: () => StateMonitorStatus;
|
|
565
|
+
/**
|
|
566
|
+
* Starts the health check loop.
|
|
567
|
+
*/
|
|
568
|
+
readonly start: () => void;
|
|
569
|
+
/**
|
|
570
|
+
* Stops the health check loop.
|
|
571
|
+
*/
|
|
572
|
+
readonly stop: () => void;
|
|
573
|
+
/**
|
|
574
|
+
* Resets the monitor state (called after recovery).
|
|
575
|
+
*/
|
|
576
|
+
readonly reset: (newVersion: number) => void;
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Current monitoring status.
|
|
580
|
+
*/
|
|
581
|
+
interface StateMonitorStatus {
|
|
582
|
+
readonly expectedVersion: number;
|
|
583
|
+
readonly pendingCount: number;
|
|
584
|
+
readonly oldestPendingMs: number | null;
|
|
585
|
+
readonly isHealthy: boolean;
|
|
586
|
+
readonly isRecovering: boolean;
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Creates a new StateMonitor.
|
|
590
|
+
*/
|
|
591
|
+
declare const make: (options?: StateMonitorOptions) => StateMonitor;
|
|
592
|
+
/**
|
|
593
|
+
* Recovery actions that can be taken.
|
|
594
|
+
*/
|
|
595
|
+
type RecoveryAction = {
|
|
596
|
+
type: "request_snapshot";
|
|
597
|
+
} | {
|
|
598
|
+
type: "retry_pending";
|
|
599
|
+
transactionIds: string[];
|
|
600
|
+
} | {
|
|
601
|
+
type: "drop_pending";
|
|
602
|
+
transactionIds: string[];
|
|
603
|
+
};
|
|
604
|
+
/**
|
|
605
|
+
* Determines the appropriate recovery action based on current state.
|
|
606
|
+
*/
|
|
607
|
+
declare const determineRecoveryAction: (status: StateMonitorStatus, stalePending: PendingInfo[]) => RecoveryAction;
|
|
608
|
+
//#endregion
|
|
609
|
+
//#region src/client/errors.d.ts
|
|
610
|
+
/**
|
|
611
|
+
* Base error class for all mimic-client errors.
|
|
612
|
+
*/
|
|
613
|
+
declare class MimicClientError extends Error {
|
|
614
|
+
readonly _tag: string;
|
|
615
|
+
constructor(message: string);
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Error thrown when a transaction is rejected by the server.
|
|
619
|
+
*/
|
|
620
|
+
declare class TransactionRejectedError extends MimicClientError {
|
|
621
|
+
readonly _tag = "TransactionRejectedError";
|
|
622
|
+
readonly transaction: Transaction;
|
|
623
|
+
readonly reason: string;
|
|
624
|
+
constructor(transaction: Transaction, reason: string);
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Error thrown when the transport is not connected.
|
|
628
|
+
*/
|
|
629
|
+
declare class NotConnectedError extends MimicClientError {
|
|
630
|
+
readonly _tag = "NotConnectedError";
|
|
631
|
+
constructor();
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Error thrown when connection to the server fails.
|
|
635
|
+
*/
|
|
636
|
+
declare class ConnectionError extends MimicClientError {
|
|
637
|
+
readonly _tag = "ConnectionError";
|
|
638
|
+
readonly cause?: Error;
|
|
639
|
+
constructor(message: string, cause?: Error);
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Error thrown when state drift is detected and cannot be recovered.
|
|
643
|
+
*/
|
|
644
|
+
declare class StateDriftError extends MimicClientError {
|
|
645
|
+
readonly _tag = "StateDriftError";
|
|
646
|
+
readonly expectedVersion: number;
|
|
647
|
+
readonly receivedVersion: number;
|
|
648
|
+
constructor(expectedVersion: number, receivedVersion: number);
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Error thrown when a pending transaction times out waiting for confirmation.
|
|
652
|
+
*/
|
|
653
|
+
declare class TransactionTimeoutError extends MimicClientError {
|
|
654
|
+
readonly _tag = "TransactionTimeoutError";
|
|
655
|
+
readonly transaction: Transaction;
|
|
656
|
+
readonly timeoutMs: number;
|
|
657
|
+
constructor(transaction: Transaction, timeoutMs: number);
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Error thrown when rebasing operations fails.
|
|
661
|
+
*/
|
|
662
|
+
declare class RebaseError extends MimicClientError {
|
|
663
|
+
readonly _tag = "RebaseError";
|
|
664
|
+
readonly transactionId: string;
|
|
665
|
+
constructor(transactionId: string, message: string);
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* Error thrown when the client document is in an invalid state.
|
|
669
|
+
*/
|
|
670
|
+
declare class InvalidStateError extends MimicClientError {
|
|
671
|
+
readonly _tag = "InvalidStateError";
|
|
672
|
+
constructor(message: string);
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Error thrown when WebSocket connection or communication fails.
|
|
676
|
+
*/
|
|
677
|
+
declare class WebSocketError extends MimicClientError {
|
|
678
|
+
readonly _tag = "WebSocketError";
|
|
679
|
+
readonly code?: number;
|
|
680
|
+
readonly reason?: string;
|
|
681
|
+
constructor(message: string, code?: number, reason?: string);
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* Error thrown when authentication fails.
|
|
685
|
+
*/
|
|
686
|
+
declare class AuthenticationError extends MimicClientError {
|
|
687
|
+
readonly _tag = "AuthenticationError";
|
|
688
|
+
constructor(message: string);
|
|
689
|
+
}
|
|
690
|
+
//#endregion
|
|
691
|
+
export { AuthenticationError, ClientDocument_d_exports as ClientDocument, ConnectionError, InvalidStateError, MimicClientError, NotConnectedError, Presence_d_exports as Presence, Rebase_d_exports as Rebase, RebaseError, StateDriftError, StateMonitor_d_exports as StateMonitor, TransactionRejectedError, TransactionTimeoutError, Transport_d_exports as Transport, WebSocketError, WebSocketTransport_d_exports as WebSocketTransport };
|
|
692
|
+
//# sourceMappingURL=index.d.cts.map
|