@voidhash/mimic 0.0.1-alpha.1
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/README.md +17 -0
- package/package.json +33 -0
- package/src/Document.ts +256 -0
- package/src/FractionalIndex.ts +1249 -0
- package/src/Operation.ts +59 -0
- package/src/OperationDefinition.ts +23 -0
- package/src/OperationPath.ts +197 -0
- package/src/Presence.ts +142 -0
- package/src/Primitive.ts +32 -0
- package/src/Proxy.ts +8 -0
- package/src/ProxyEnvironment.ts +52 -0
- package/src/Transaction.ts +72 -0
- package/src/Transform.ts +13 -0
- package/src/client/ClientDocument.ts +1163 -0
- package/src/client/Rebase.ts +309 -0
- package/src/client/StateMonitor.ts +307 -0
- package/src/client/Transport.ts +318 -0
- package/src/client/WebSocketTransport.ts +572 -0
- package/src/client/errors.ts +145 -0
- package/src/client/index.ts +61 -0
- package/src/index.ts +12 -0
- package/src/primitives/Array.ts +457 -0
- package/src/primitives/Boolean.ts +128 -0
- package/src/primitives/Lazy.ts +89 -0
- package/src/primitives/Literal.ts +128 -0
- package/src/primitives/Number.ts +169 -0
- package/src/primitives/String.ts +189 -0
- package/src/primitives/Struct.ts +348 -0
- package/src/primitives/Tree.ts +1120 -0
- package/src/primitives/TreeNode.ts +113 -0
- package/src/primitives/Union.ts +329 -0
- package/src/primitives/shared.ts +122 -0
- package/src/server/ServerDocument.ts +267 -0
- package/src/server/errors.ts +90 -0
- package/src/server/index.ts +40 -0
- package/tests/Document.test.ts +556 -0
- package/tests/FractionalIndex.test.ts +377 -0
- package/tests/OperationPath.test.ts +151 -0
- package/tests/Presence.test.ts +321 -0
- package/tests/Primitive.test.ts +381 -0
- package/tests/client/ClientDocument.test.ts +1398 -0
- package/tests/client/WebSocketTransport.test.ts +992 -0
- package/tests/primitives/Array.test.ts +418 -0
- package/tests/primitives/Boolean.test.ts +126 -0
- package/tests/primitives/Lazy.test.ts +143 -0
- package/tests/primitives/Literal.test.ts +122 -0
- package/tests/primitives/Number.test.ts +133 -0
- package/tests/primitives/String.test.ts +128 -0
- package/tests/primitives/Struct.test.ts +311 -0
- package/tests/primitives/Tree.test.ts +467 -0
- package/tests/primitives/TreeNode.test.ts +50 -0
- package/tests/primitives/Union.test.ts +210 -0
- package/tests/server/ServerDocument.test.ts +528 -0
- package/tsconfig.build.json +24 -0
- package/tsconfig.json +8 -0
- package/tsdown.config.ts +18 -0
- package/vitest.mts +11 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import type * as Transaction from "../Transaction";
|
|
2
|
+
|
|
3
|
+
// =============================================================================
|
|
4
|
+
// Server Messages
|
|
5
|
+
// =============================================================================
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Message received when the server broadcasts a committed transaction.
|
|
9
|
+
*/
|
|
10
|
+
export interface TransactionMessage {
|
|
11
|
+
readonly type: "transaction";
|
|
12
|
+
readonly transaction: Transaction.Transaction;
|
|
13
|
+
/** Server-assigned version number for ordering */
|
|
14
|
+
readonly version: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Message received when requesting or receiving a full state snapshot.
|
|
19
|
+
*/
|
|
20
|
+
export interface SnapshotMessage {
|
|
21
|
+
readonly type: "snapshot";
|
|
22
|
+
readonly state: unknown;
|
|
23
|
+
readonly version: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Message received when the server rejects a transaction.
|
|
28
|
+
*/
|
|
29
|
+
export interface ErrorMessage {
|
|
30
|
+
readonly type: "error";
|
|
31
|
+
readonly transactionId: string;
|
|
32
|
+
readonly reason: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Message received in response to a ping (heartbeat).
|
|
37
|
+
*/
|
|
38
|
+
export interface PongMessage {
|
|
39
|
+
readonly type: "pong";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Message received after authentication attempt.
|
|
44
|
+
*/
|
|
45
|
+
export interface AuthResultMessage {
|
|
46
|
+
readonly type: "auth_result";
|
|
47
|
+
readonly success: boolean;
|
|
48
|
+
readonly error?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// =============================================================================
|
|
52
|
+
// Presence Server Messages
|
|
53
|
+
// =============================================================================
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A presence entry as transmitted.
|
|
57
|
+
*/
|
|
58
|
+
export interface PresenceEntry {
|
|
59
|
+
/** The presence data */
|
|
60
|
+
readonly data: unknown;
|
|
61
|
+
/** Optional user ID from authentication */
|
|
62
|
+
readonly userId?: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Message received with initial presence state after authentication.
|
|
67
|
+
*/
|
|
68
|
+
export interface PresenceSnapshotMessage {
|
|
69
|
+
readonly type: "presence_snapshot";
|
|
70
|
+
/** This connection's ID (used to identify self) */
|
|
71
|
+
readonly selfId: string;
|
|
72
|
+
/** Map of connectionId to presence entry */
|
|
73
|
+
readonly presences: Record<string, PresenceEntry>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Message received when another user updates their presence.
|
|
78
|
+
*/
|
|
79
|
+
export interface PresenceUpdateMessage {
|
|
80
|
+
readonly type: "presence_update";
|
|
81
|
+
/** The connection ID of the user who updated */
|
|
82
|
+
readonly id: string;
|
|
83
|
+
/** The presence data */
|
|
84
|
+
readonly data: unknown;
|
|
85
|
+
/** Optional user ID from authentication */
|
|
86
|
+
readonly userId?: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Message received when another user's presence is removed (disconnect).
|
|
91
|
+
*/
|
|
92
|
+
export interface PresenceRemoveMessage {
|
|
93
|
+
readonly type: "presence_remove";
|
|
94
|
+
/** The connection ID of the user who disconnected */
|
|
95
|
+
readonly id: string;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Union of all possible server messages.
|
|
100
|
+
*/
|
|
101
|
+
export type ServerMessage =
|
|
102
|
+
| TransactionMessage
|
|
103
|
+
| SnapshotMessage
|
|
104
|
+
| ErrorMessage
|
|
105
|
+
| PongMessage
|
|
106
|
+
| AuthResultMessage
|
|
107
|
+
| PresenceSnapshotMessage
|
|
108
|
+
| PresenceUpdateMessage
|
|
109
|
+
| PresenceRemoveMessage;
|
|
110
|
+
|
|
111
|
+
// =============================================================================
|
|
112
|
+
// Client Messages
|
|
113
|
+
// =============================================================================
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Message sent to submit a transaction to the server.
|
|
117
|
+
*/
|
|
118
|
+
export interface SubmitTransactionMessage {
|
|
119
|
+
readonly type: "submit";
|
|
120
|
+
readonly transaction: Transaction.Transaction;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Message sent to request a full state snapshot.
|
|
125
|
+
*/
|
|
126
|
+
export interface RequestSnapshotMessage {
|
|
127
|
+
readonly type: "request_snapshot";
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Message sent as heartbeat ping.
|
|
132
|
+
*/
|
|
133
|
+
export interface PingMessage {
|
|
134
|
+
readonly type: "ping";
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Message sent to authenticate with the server.
|
|
139
|
+
*/
|
|
140
|
+
export interface AuthMessage {
|
|
141
|
+
readonly type: "auth";
|
|
142
|
+
readonly token: string;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// =============================================================================
|
|
146
|
+
// Presence Client Messages
|
|
147
|
+
// =============================================================================
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Message sent to set/update this client's presence.
|
|
151
|
+
*/
|
|
152
|
+
export interface PresenceSetMessage {
|
|
153
|
+
readonly type: "presence_set";
|
|
154
|
+
/** The presence data (validated against schema on client before sending) */
|
|
155
|
+
readonly data: unknown;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Message sent to clear this client's presence.
|
|
160
|
+
*/
|
|
161
|
+
export interface PresenceClearMessage {
|
|
162
|
+
readonly type: "presence_clear";
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Union of all possible client messages.
|
|
167
|
+
*/
|
|
168
|
+
export type ClientMessage =
|
|
169
|
+
| SubmitTransactionMessage
|
|
170
|
+
| RequestSnapshotMessage
|
|
171
|
+
| PingMessage
|
|
172
|
+
| AuthMessage
|
|
173
|
+
| PresenceSetMessage
|
|
174
|
+
| PresenceClearMessage;
|
|
175
|
+
|
|
176
|
+
// =============================================================================
|
|
177
|
+
// Encoded Message Types (for network transport)
|
|
178
|
+
// =============================================================================
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Encoded transaction message for network transport.
|
|
182
|
+
*/
|
|
183
|
+
export interface EncodedTransactionMessage {
|
|
184
|
+
readonly type: "transaction";
|
|
185
|
+
readonly transaction: Transaction.EncodedTransaction;
|
|
186
|
+
readonly version: number;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Encoded submit message for network transport.
|
|
191
|
+
*/
|
|
192
|
+
export interface EncodedSubmitTransactionMessage {
|
|
193
|
+
readonly type: "submit";
|
|
194
|
+
readonly transaction: Transaction.EncodedTransaction;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Union of all possible encoded server messages (for network transport).
|
|
199
|
+
*/
|
|
200
|
+
export type EncodedServerMessage =
|
|
201
|
+
| EncodedTransactionMessage
|
|
202
|
+
| SnapshotMessage
|
|
203
|
+
| ErrorMessage
|
|
204
|
+
| PongMessage
|
|
205
|
+
| AuthResultMessage
|
|
206
|
+
| PresenceSnapshotMessage
|
|
207
|
+
| PresenceUpdateMessage
|
|
208
|
+
| PresenceRemoveMessage;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Union of all possible encoded client messages (for network transport).
|
|
212
|
+
*/
|
|
213
|
+
export type EncodedClientMessage =
|
|
214
|
+
| EncodedSubmitTransactionMessage
|
|
215
|
+
| RequestSnapshotMessage
|
|
216
|
+
| PingMessage
|
|
217
|
+
| AuthMessage
|
|
218
|
+
| PresenceSetMessage
|
|
219
|
+
| PresenceClearMessage;
|
|
220
|
+
|
|
221
|
+
// =============================================================================
|
|
222
|
+
// Transport Interface
|
|
223
|
+
// =============================================================================
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Abstract transport interface for server communication.
|
|
227
|
+
* Implementations can use WebSocket, HTTP, or any other protocol.
|
|
228
|
+
*/
|
|
229
|
+
export interface Transport {
|
|
230
|
+
/**
|
|
231
|
+
* Sends a transaction to the server for processing.
|
|
232
|
+
* @param transaction - The transaction to submit
|
|
233
|
+
*/
|
|
234
|
+
readonly send: (transaction: Transaction.Transaction) => void;
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Requests a full state snapshot from the server.
|
|
238
|
+
* Used for initial sync or recovery from drift.
|
|
239
|
+
*/
|
|
240
|
+
readonly requestSnapshot: () => void;
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Subscribes to messages from the server.
|
|
244
|
+
* @param handler - Callback invoked for each server message
|
|
245
|
+
* @returns Unsubscribe function
|
|
246
|
+
*/
|
|
247
|
+
readonly subscribe: (handler: (message: ServerMessage) => void) => () => void;
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Establishes connection to the server.
|
|
251
|
+
* @returns Promise that resolves when connected
|
|
252
|
+
*/
|
|
253
|
+
readonly connect: () => Promise<void>;
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Disconnects from the server.
|
|
257
|
+
*/
|
|
258
|
+
readonly disconnect: () => void;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Returns whether the transport is currently connected.
|
|
262
|
+
*/
|
|
263
|
+
readonly isConnected: () => boolean;
|
|
264
|
+
|
|
265
|
+
// ===========================================================================
|
|
266
|
+
// Presence Methods
|
|
267
|
+
// ===========================================================================
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Sends presence data to the server.
|
|
271
|
+
* The data should be validated against the presence schema before calling.
|
|
272
|
+
* @param data - The presence data to set
|
|
273
|
+
*/
|
|
274
|
+
readonly sendPresenceSet: (data: unknown) => void;
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Clears this client's presence on the server.
|
|
278
|
+
*/
|
|
279
|
+
readonly sendPresenceClear: () => void;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// =============================================================================
|
|
283
|
+
// Transport Events
|
|
284
|
+
// =============================================================================
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Events emitted by the transport for connection status.
|
|
288
|
+
*/
|
|
289
|
+
export type TransportEvent =
|
|
290
|
+
| { type: "connected" }
|
|
291
|
+
| { type: "disconnected"; reason?: string }
|
|
292
|
+
| { type: "reconnecting"; attempt: number }
|
|
293
|
+
| { type: "error"; error: Error };
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Handler for transport events.
|
|
297
|
+
*/
|
|
298
|
+
export type TransportEventHandler = (event: TransportEvent) => void;
|
|
299
|
+
|
|
300
|
+
// =============================================================================
|
|
301
|
+
// Transport Options
|
|
302
|
+
// =============================================================================
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Options for creating a transport.
|
|
306
|
+
*/
|
|
307
|
+
export interface TransportOptions {
|
|
308
|
+
/** Handler for transport lifecycle events */
|
|
309
|
+
readonly onEvent?: TransportEventHandler;
|
|
310
|
+
/** Timeout in milliseconds for connection attempts */
|
|
311
|
+
readonly connectionTimeout?: number;
|
|
312
|
+
/** Whether to automatically reconnect on disconnect */
|
|
313
|
+
readonly autoReconnect?: boolean;
|
|
314
|
+
/** Maximum number of reconnection attempts */
|
|
315
|
+
readonly maxReconnectAttempts?: number;
|
|
316
|
+
/** Base delay between reconnection attempts (ms) */
|
|
317
|
+
readonly reconnectDelay?: number;
|
|
318
|
+
}
|