@evolu/common 6.0.1-preview.3 → 6.0.1-preview.31

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.
Files changed (173) hide show
  1. package/dist/src/Array.d.ts +69 -5
  2. package/dist/src/Array.d.ts.map +1 -1
  3. package/dist/src/Array.js +64 -5
  4. package/dist/src/Assert.d.ts +6 -16
  5. package/dist/src/Assert.d.ts.map +1 -1
  6. package/dist/src/Assert.js +6 -18
  7. package/dist/src/Brand.d.ts +75 -0
  8. package/dist/src/Brand.d.ts.map +1 -0
  9. package/dist/src/Brand.js +1 -0
  10. package/dist/src/Buffer.d.ts +1 -1
  11. package/dist/src/Buffer.d.ts.map +1 -1
  12. package/dist/src/Buffer.js +8 -7
  13. package/dist/src/Cache.d.ts +44 -0
  14. package/dist/src/Cache.d.ts.map +1 -0
  15. package/dist/src/Cache.js +52 -0
  16. package/dist/src/Callbacks.d.ts +45 -12
  17. package/dist/src/Callbacks.d.ts.map +1 -1
  18. package/dist/src/Callbacks.js +14 -7
  19. package/dist/src/Console.d.ts +31 -6
  20. package/dist/src/Console.d.ts.map +1 -1
  21. package/dist/src/Console.js +72 -9
  22. package/dist/src/Crypto.d.ts +61 -34
  23. package/dist/src/Crypto.d.ts.map +1 -1
  24. package/dist/src/Crypto.js +32 -45
  25. package/dist/src/Evolu/Db.d.ts +161 -65
  26. package/dist/src/Evolu/Db.d.ts.map +1 -1
  27. package/dist/src/Evolu/Db.js +286 -694
  28. package/dist/src/Evolu/Diff.d.ts +3 -3
  29. package/dist/src/Evolu/Diff.d.ts.map +1 -1
  30. package/dist/src/Evolu/Diff.js +7 -5
  31. package/dist/src/Evolu/Evolu.d.ts +208 -133
  32. package/dist/src/Evolu/Evolu.d.ts.map +1 -1
  33. package/dist/src/Evolu/Evolu.js +188 -183
  34. package/dist/src/Evolu/Internal.d.ts +0 -2
  35. package/dist/src/Evolu/Internal.d.ts.map +1 -1
  36. package/dist/src/Evolu/Internal.js +0 -2
  37. package/dist/src/Evolu/LocalAuth.d.ts +150 -0
  38. package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
  39. package/dist/src/Evolu/LocalAuth.js +174 -0
  40. package/dist/src/Evolu/Owner.d.ts +273 -120
  41. package/dist/src/Evolu/Owner.d.ts.map +1 -1
  42. package/dist/src/Evolu/Owner.js +130 -104
  43. package/dist/src/Evolu/Platform.d.ts +9 -7
  44. package/dist/src/Evolu/Platform.d.ts.map +1 -1
  45. package/dist/src/Evolu/Protocol.d.ts +277 -232
  46. package/dist/src/Evolu/Protocol.d.ts.map +1 -1
  47. package/dist/src/Evolu/Protocol.js +603 -378
  48. package/dist/src/Evolu/Public.d.ts +6 -8
  49. package/dist/src/Evolu/Public.d.ts.map +1 -1
  50. package/dist/src/Evolu/Public.js +2 -3
  51. package/dist/src/Evolu/PublicKysely.js +3 -3
  52. package/dist/src/Evolu/Query.d.ts +2 -1
  53. package/dist/src/Evolu/Query.d.ts.map +1 -1
  54. package/dist/src/Evolu/Relay.d.ts +92 -7
  55. package/dist/src/Evolu/Relay.d.ts.map +1 -1
  56. package/dist/src/Evolu/Relay.js +238 -76
  57. package/dist/src/Evolu/Schema.d.ts +129 -73
  58. package/dist/src/Evolu/Schema.d.ts.map +1 -1
  59. package/dist/src/Evolu/Schema.js +169 -89
  60. package/dist/src/Evolu/Storage.d.ts +240 -26
  61. package/dist/src/Evolu/Storage.d.ts.map +1 -1
  62. package/dist/src/Evolu/Storage.js +189 -91
  63. package/dist/src/Evolu/Sync.d.ts +67 -13
  64. package/dist/src/Evolu/Sync.d.ts.map +1 -1
  65. package/dist/src/Evolu/Sync.js +441 -20
  66. package/dist/src/Evolu/Timestamp.d.ts +85 -27
  67. package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
  68. package/dist/src/Evolu/Timestamp.js +77 -18
  69. package/dist/src/Identicon.d.ts +35 -0
  70. package/dist/src/Identicon.d.ts.map +1 -0
  71. package/dist/src/Identicon.js +143 -0
  72. package/dist/src/Instances.d.ts +34 -0
  73. package/dist/src/Instances.d.ts.map +1 -0
  74. package/dist/src/Instances.js +44 -0
  75. package/dist/src/ManyToManyMap.d.ts +71 -10
  76. package/dist/src/ManyToManyMap.d.ts.map +1 -1
  77. package/dist/src/ManyToManyMap.js +41 -6
  78. package/dist/src/Number.d.ts +4 -3
  79. package/dist/src/Number.d.ts.map +1 -1
  80. package/dist/src/Number.js +5 -4
  81. package/dist/src/Platform.d.ts +20 -0
  82. package/dist/src/Platform.d.ts.map +1 -0
  83. package/dist/src/Platform.js +22 -0
  84. package/dist/src/Random.d.ts +3 -2
  85. package/dist/src/Random.d.ts.map +1 -1
  86. package/dist/src/Resources.d.ts +118 -0
  87. package/dist/src/Resources.d.ts.map +1 -0
  88. package/dist/src/Resources.js +197 -0
  89. package/dist/src/Result.d.ts +184 -52
  90. package/dist/src/Result.d.ts.map +1 -1
  91. package/dist/src/Result.js +30 -241
  92. package/dist/src/Skiplist.js +2 -1
  93. package/dist/src/Sqlite.d.ts +63 -5
  94. package/dist/src/Sqlite.d.ts.map +1 -1
  95. package/dist/src/Sqlite.js +110 -9
  96. package/dist/src/Task.d.ts +586 -0
  97. package/dist/src/Task.d.ts.map +1 -0
  98. package/dist/src/Task.js +469 -0
  99. package/dist/src/Time.d.ts +66 -1
  100. package/dist/src/Time.d.ts.map +1 -1
  101. package/dist/src/Time.js +99 -5
  102. package/dist/src/Type.d.ts +622 -340
  103. package/dist/src/Type.d.ts.map +1 -1
  104. package/dist/src/Type.js +666 -464
  105. package/dist/src/Types.d.ts +1 -75
  106. package/dist/src/Types.d.ts.map +1 -1
  107. package/dist/src/WebSocket.d.ts +5 -2
  108. package/dist/src/WebSocket.d.ts.map +1 -1
  109. package/dist/src/WebSocket.js +12 -18
  110. package/dist/src/Worker.d.ts +39 -11
  111. package/dist/src/Worker.d.ts.map +1 -1
  112. package/dist/src/Worker.js +22 -4
  113. package/dist/src/index.d.ts +7 -2
  114. package/dist/src/index.d.ts.map +1 -1
  115. package/dist/src/index.js +7 -2
  116. package/package.json +14 -13
  117. package/src/Array.ts +90 -11
  118. package/src/Assert.ts +6 -24
  119. package/src/Brand.ts +75 -0
  120. package/src/Buffer.ts +7 -7
  121. package/src/Cache.ts +85 -0
  122. package/src/Callbacks.ts +62 -22
  123. package/src/Console.ts +91 -11
  124. package/src/Crypto.ts +97 -82
  125. package/src/Evolu/Db.ts +517 -1020
  126. package/src/Evolu/Diff.ts +7 -5
  127. package/src/Evolu/Evolu.ts +464 -355
  128. package/src/Evolu/Internal.ts +0 -2
  129. package/src/Evolu/LocalAuth.ts +463 -0
  130. package/src/Evolu/Owner.ts +355 -228
  131. package/src/Evolu/Platform.ts +9 -9
  132. package/src/Evolu/Protocol.ts +859 -676
  133. package/src/Evolu/Public.ts +7 -14
  134. package/src/Evolu/PublicKysely.ts +3 -3
  135. package/src/Evolu/Query.ts +2 -1
  136. package/src/Evolu/Relay.ts +437 -93
  137. package/src/Evolu/Schema.ts +391 -191
  138. package/src/Evolu/Storage.ts +532 -135
  139. package/src/Evolu/Sync.ts +766 -37
  140. package/src/Evolu/Timestamp.ts +88 -35
  141. package/src/Identicon.ts +197 -0
  142. package/src/Instances.ts +90 -0
  143. package/src/ManyToManyMap.ts +124 -24
  144. package/src/Number.ts +6 -10
  145. package/src/Platform.ts +26 -0
  146. package/src/Random.ts +3 -2
  147. package/src/Resources.ts +367 -0
  148. package/src/Result.ts +191 -54
  149. package/src/Skiplist.ts +1 -1
  150. package/src/Sqlite.ts +122 -17
  151. package/src/Task.ts +901 -0
  152. package/src/Time.ts +180 -5
  153. package/src/Type.ts +1084 -727
  154. package/src/Types.ts +1 -77
  155. package/src/WebSocket.ts +27 -25
  156. package/src/Worker.ts +72 -23
  157. package/src/index.ts +7 -2
  158. package/dist/src/Evolu/Config.d.ts +0 -69
  159. package/dist/src/Evolu/Config.d.ts.map +0 -1
  160. package/dist/src/Evolu/Config.js +0 -9
  161. package/dist/src/Evolu/Kysely.d.ts +0 -6
  162. package/dist/src/Evolu/Kysely.d.ts.map +0 -1
  163. package/dist/src/Evolu/Kysely.js +0 -21
  164. package/dist/src/NanoId.d.ts +0 -27
  165. package/dist/src/NanoId.d.ts.map +0 -1
  166. package/dist/src/NanoId.js +0 -6
  167. package/dist/src/Promise.d.ts +0 -180
  168. package/dist/src/Promise.d.ts.map +0 -1
  169. package/dist/src/Promise.js +0 -176
  170. package/src/Evolu/Config.ts +0 -83
  171. package/src/Evolu/Kysely.ts +0 -38
  172. package/src/NanoId.ts +0 -39
  173. package/src/Promise.ts +0 -295
package/src/Evolu/Sync.ts CHANGED
@@ -1,58 +1,787 @@
1
- import { ConsoleConfig, ConsoleDep } from "../Console.js";
2
- import { createWebSocket } from "../WebSocket.js";
3
- import { ProtocolMessage } from "./Protocol.js";
4
- import { Millis } from "./Timestamp.js";
1
+ import {
2
+ firstInArray,
3
+ isNonEmptyReadonlyArray,
4
+ NonEmptyArray,
5
+ NonEmptyReadonlyArray,
6
+ } from "../Array.js";
7
+ import { assert } from "../Assert.js";
8
+ import { Brand } from "../Brand.js";
9
+ import { ConsoleDep } from "../Console.js";
10
+ import {
11
+ RandomBytesDep,
12
+ SymmetricCryptoDecryptError,
13
+ SymmetricCryptoDep,
14
+ } from "../Crypto.js";
15
+ import { eqArrayNumber } from "../Eq.js";
16
+ import { createTransferableError, TransferableError } from "../Error.js";
17
+ import { constFalse, constTrue } from "../Function.js";
18
+ import { objectToEntries } from "../Object.js";
19
+ import { RandomDep } from "../Random.js";
20
+ import { createResources } from "../Resources.js";
21
+ import { err, ok, Result } from "../Result.js";
22
+ import { sql, SqliteDep, SqliteError, SqliteValue } from "../Sqlite.js";
23
+ import { AbortError, createMutex } from "../Task.js";
24
+ import { TimeDep } from "../Time.js";
25
+ import { IdBytes, idBytesToId, idToIdBytes, PositiveInt } from "../Type.js";
26
+ import { CreateWebSocketDep, WebSocket } from "../WebSocket.js";
27
+ import type { PostMessageDep } from "./Db.js";
28
+ import {
29
+ AppOwner,
30
+ OwnerEncryptionKey,
31
+ OwnerId,
32
+ OwnerIdBytes,
33
+ ownerIdBytesToOwnerId,
34
+ ownerIdToOwnerIdBytes,
35
+ OwnerTransport,
36
+ OwnerWriteKey,
37
+ ShardOwner,
38
+ SharedOwner,
39
+ SharedReadonlyOwner,
40
+ } from "./Owner.js";
41
+ import {
42
+ applyProtocolMessageAsClient,
43
+ createProtocolMessageForSync,
44
+ createProtocolMessageForUnsubscribe,
45
+ createProtocolMessageFromCrdtMessages,
46
+ decryptAndDecodeDbChange,
47
+ encodeAndEncryptDbChange,
48
+ ProtocolError,
49
+ ProtocolInvalidDataError,
50
+ ProtocolTimestampMismatchError,
51
+ SubscriptionFlags,
52
+ } from "./Protocol.js";
53
+ import { MutationChange } from "./Schema.js";
54
+ import {
55
+ BaseSqliteStorage,
56
+ CrdtMessage,
57
+ createBaseSqliteStorage,
58
+ DbChange,
59
+ getOwnerUsage,
60
+ getTimestampInsertStrategy,
61
+ Storage,
62
+ StorageInsertTimestampStrategy,
63
+ StorageWriteError,
64
+ updateOwnerUsage,
65
+ } from "./Storage.js";
66
+ import {
67
+ createInitialTimestamp,
68
+ Millis,
69
+ receiveTimestamp,
70
+ sendTimestamp,
71
+ Timestamp,
72
+ timestampBytesToTimestamp,
73
+ TimestampConfigDep,
74
+ TimestampCounterOverflowError,
75
+ TimestampDriftError,
76
+ TimestampTimeOutOfRangeError,
77
+ timestampToTimestampBytes,
78
+ timestampToTimestampString,
79
+ } from "./Timestamp.js";
5
80
 
6
- export interface Sync {
7
- readonly send: (message: ProtocolMessage) => void;
81
+ export interface Sync extends Disposable {
82
+ /**
83
+ * Assigns or removes an owner to/from transports with reference counting.
84
+ *
85
+ * Owners are only "active" if assigned to at least one transport. Uses
86
+ * `owner.transports` or falls back to config transports. Multiple calls
87
+ * increment/decrement reference counts (useful for React Hooks).
88
+ */
89
+ readonly useOwner: (use: boolean, owner: SyncOwner) => void;
90
+
91
+ readonly applyChanges: (
92
+ changes: NonEmptyReadonlyArray<MutationChange>,
93
+ ) => Result<
94
+ void,
95
+ | SqliteError
96
+ | TimestampCounterOverflowError
97
+ | TimestampDriftError
98
+ | TimestampTimeOutOfRangeError
99
+ >;
8
100
  }
9
101
 
10
102
  export interface SyncDep {
11
103
  readonly sync: Sync;
12
104
  }
13
105
 
14
- export type CreateSync = (deps: ConsoleDep) => (config: SyncConfig) => Sync;
106
+ /**
107
+ * Represents an owner for sync operations. This is a unified interface that
108
+ * abstracts over the specific owner types ({@link ShardOwner},
109
+ * {@link SharedOwner}, {@link SharedReadonlyOwner}) for the sync layer.
110
+ *
111
+ * The sync layer only needs the essential data for synchronization and doesn't
112
+ * need to distinguish between different owner types.
113
+ */
114
+ export interface SyncOwner {
115
+ readonly id: OwnerId;
116
+ readonly encryptionKey: OwnerEncryptionKey;
117
+ /** Optional for read-only owners like {@link SharedReadonlyOwner}. */
118
+ readonly writeKey?: OwnerWriteKey;
119
+ readonly transports?: ReadonlyArray<OwnerTransport>;
120
+ }
121
+
122
+ export interface SyncConfig {
123
+ readonly appOwner: AppOwner;
124
+
125
+ readonly transports: ReadonlyArray<OwnerTransport>;
126
+
127
+ /**
128
+ * Delay in milliseconds before disposing unused WebSocket connections.
129
+ * Defaults to 100ms.
130
+ */
131
+ readonly disposalDelayMs?: number;
15
132
 
16
- export interface CreateSyncDep {
17
- readonly createSync: CreateSync;
133
+ readonly onError: (
134
+ error:
135
+ | ProtocolError
136
+ | ProtocolInvalidDataError
137
+ | ProtocolTimestampMismatchError
138
+ | SqliteError
139
+ | SymmetricCryptoDecryptError
140
+ | TimestampCounterOverflowError
141
+ | TimestampDriftError
142
+ | TimestampTimeOutOfRangeError
143
+ | TransferableError,
144
+ ) => void;
145
+
146
+ readonly onReceive: () => void;
18
147
  }
19
148
 
20
- export interface SyncConfig extends ConsoleConfig {
21
- readonly syncUrl: string;
22
- readonly onOpen: (send: Sync["send"]) => void;
23
- readonly onMessage: (message: Uint8Array, send: Sync["send"]) => void;
149
+ export const createSync =
150
+ (
151
+ deps: ClockDep &
152
+ ConsoleDep &
153
+ CreateWebSocketDep &
154
+ PostMessageDep &
155
+ RandomBytesDep &
156
+ RandomDep &
157
+ SqliteDep &
158
+ SymmetricCryptoDep &
159
+ TimeDep &
160
+ TimestampConfigDep,
161
+ ) =>
162
+ (config: SyncConfig): Result<Sync, SqliteError> => {
163
+ let isDisposed = false;
164
+
165
+ /** Returns owner data only if actively assigned to at least one transport. */
166
+ const getSyncOwner = (ownerId: OwnerId): SyncOwner | null => {
167
+ if (isDisposed) return null;
168
+ return transports.getConsumer(ownerId);
169
+ };
170
+
171
+ const storageResult = createClientStorage({
172
+ ...deps,
173
+ getSyncOwner,
174
+ })(config);
175
+
176
+ if (!storageResult.ok) return storageResult;
177
+ const storage = storageResult.value;
178
+
179
+ const createResource = (transportConfig: OwnerTransport): WebSocket => {
180
+ const transportKey = createTransportKey(transportConfig);
181
+
182
+ deps.console.log("[sync]", "createWebSocket", {
183
+ transportKey,
184
+ url: transportConfig.url,
185
+ });
186
+
187
+ return deps.createWebSocket(transportConfig.url, {
188
+ binaryType: "arraybuffer",
189
+
190
+ onOpen: () => {
191
+ if (isDisposed) return;
192
+
193
+ const webSocket = transports.getResource(transportKey);
194
+ if (!webSocket) return;
195
+
196
+ const ownerIds = transports.getConsumersForResource(transportKey);
197
+ deps.console.log("[sync]", "onOpen", { transportKey, ownerIds });
198
+
199
+ for (const ownerId of ownerIds) {
200
+ const message = createProtocolMessageForSync({ storage })(
201
+ ownerId,
202
+ SubscriptionFlags.Subscribe,
203
+ );
204
+ if (!message) continue;
205
+ deps.console.log("[sync]", "send", { message });
206
+ webSocket.send(message);
207
+ }
208
+ },
209
+
210
+ onClose: (event) => {
211
+ deps.console.log("[sync]", "onClose", {
212
+ transportKey,
213
+ code: event.code,
214
+ reason: event.reason,
215
+ wasClean: event.wasClean,
216
+ });
217
+ },
218
+
219
+ onError: (error) => {
220
+ deps.console.warn("[sync]", "onError", { transportKey, error });
221
+ },
222
+
223
+ onMessage: (data: string | ArrayBuffer | Blob) => {
224
+ // Only handle ArrayBuffer data for sync messages
225
+ if (isDisposed || !(data instanceof ArrayBuffer)) return;
226
+
227
+ const webSocket = transports.getResource(transportKey);
228
+ if (!webSocket) return;
229
+
230
+ const input = new Uint8Array(data);
231
+ deps.console.log("[sync]", "onMessage", {
232
+ transportKey,
233
+ message: input,
234
+ });
235
+
236
+ applyProtocolMessageAsClient({ storage })(input, {
237
+ // No write key, no sync (for a case when an owner was unused).
238
+ getWriteKey: (ownerId) => getSyncOwner(ownerId)?.writeKey ?? null,
239
+ })
240
+ .then((message) => {
241
+ if (!message.ok) {
242
+ config.onError(message.error);
243
+ return;
244
+ }
245
+
246
+ switch (message.value.type) {
247
+ case "response":
248
+ webSocket.send(message.value.message);
249
+ break;
250
+ case "no-response":
251
+ // Sync complete, no response needed
252
+ break;
253
+ case "broadcast":
254
+ // This was a broadcast message, don't affect sync counter
255
+ break;
256
+ }
257
+ })
258
+ .catch((error: unknown) => {
259
+ config.onError(createTransferableError(error));
260
+ });
261
+ },
262
+ });
263
+ };
264
+
265
+ const transports = createResources<
266
+ WebSocket,
267
+ TransportKey,
268
+ OwnerTransport,
269
+ SyncOwner,
270
+ OwnerId
271
+ >({
272
+ createResource,
273
+ getResourceKey: createTransportKey,
274
+ getConsumerId: (owner) => owner.id,
275
+ disposalDelay: config.disposalDelayMs ?? 100,
276
+
277
+ onConsumerAdded: (owner, webSocket) => {
278
+ deps.console.log("[sync]", "onConsumerAdded", {
279
+ ownerId: owner.id,
280
+ isOpen: webSocket.isOpen(),
281
+ });
282
+
283
+ // The onOpen handler will sync it.
284
+ if (!webSocket.isOpen()) return;
285
+ const message = createProtocolMessageForSync({ storage })(
286
+ owner.id,
287
+ SubscriptionFlags.Subscribe,
288
+ );
289
+ if (message) webSocket.send(message);
290
+ },
291
+
292
+ onConsumerRemoved: (owner, webSocket) => {
293
+ deps.console.log("[sync]", "onConsumerRemoved", {
294
+ ownerId: owner.id,
295
+ isOpen: webSocket.isOpen(),
296
+ });
297
+
298
+ const message = createProtocolMessageForUnsubscribe(owner.id);
299
+ webSocket.send(message);
300
+ },
301
+ });
302
+
303
+ const sync: Sync = {
304
+ useOwner: (use, owner) => {
305
+ if (isDisposed) {
306
+ deps.console.warn(
307
+ "[sync]",
308
+ "useOwner called on disposed Sync instance",
309
+ { owner },
310
+ );
311
+ return;
312
+ }
313
+
314
+ deps.console.log("[sync]", "useOwner", { use, owner });
315
+ const transportsToUse = owner.transports ?? config.transports;
316
+
317
+ if (use) {
318
+ transports.addConsumer(owner, transportsToUse);
319
+ } else {
320
+ const result = transports.removeConsumer(owner, transportsToUse);
321
+
322
+ if (!result.ok) {
323
+ deps.console.warn("[sync]", "Failed to remove consumer", {
324
+ transportsToUse,
325
+ ownerId: owner.id,
326
+ error: result.error,
327
+ });
328
+ }
329
+ }
330
+ },
331
+
332
+ applyChanges: (changes) => {
333
+ deps.console.log("[sync]", "applyChanges", { changes });
334
+
335
+ let clockTimestamp = deps.clock.get();
336
+ const ownerMessages = new Map<OwnerId, NonEmptyArray<CrdtMessage>>();
337
+
338
+ for (const change of changes) {
339
+ const nextTimestamp = sendTimestamp(deps)(clockTimestamp);
340
+ if (!nextTimestamp.ok) return nextTimestamp;
341
+ clockTimestamp = nextTimestamp.value;
342
+
343
+ const { ownerId = config.appOwner.id, ...dbChange } = change;
344
+ const message = { timestamp: clockTimestamp, change: dbChange };
345
+
346
+ const messages = ownerMessages.get(ownerId);
347
+ if (messages) messages.push(message);
348
+ else ownerMessages.set(ownerId, [message]);
349
+ }
350
+
351
+ for (const [ownerId, messages] of ownerMessages) {
352
+ const result = applyMessages({ ...deps, storage })(ownerId, messages);
353
+ if (!result.ok) return result;
354
+
355
+ const owner = getSyncOwner(ownerId);
356
+ if (!owner?.writeKey) continue;
357
+
358
+ const message = createProtocolMessageFromCrdtMessages(deps)(
359
+ {
360
+ id: owner.id,
361
+ encryptionKey: owner.encryptionKey,
362
+ writeKey: owner.writeKey,
363
+ },
364
+ messages,
365
+ );
366
+
367
+ const transportsToUse = owner.transports ?? config.transports;
368
+
369
+ // Send message to all transports for this owner
370
+ for (const transportConfig of transportsToUse) {
371
+ const transportKey = createTransportKey(transportConfig);
372
+
373
+ const webSocket = transports.getResource(transportKey);
374
+ if (!webSocket) continue;
375
+
376
+ if (webSocket.isOpen()) {
377
+ deps.console.log("[sync]", "send", { transportKey, message });
378
+ webSocket.send(message);
379
+ }
380
+ }
381
+ }
382
+
383
+ return deps.clock.save(clockTimestamp);
384
+ },
385
+
386
+ [Symbol.dispose]: () => {
387
+ if (isDisposed) return;
388
+ isDisposed = true;
389
+ transports[Symbol.dispose]();
390
+ },
391
+ };
392
+
393
+ return ok(sync);
394
+ };
395
+
396
+ export interface ClockDep {
397
+ readonly clock: Clock;
398
+ }
399
+
400
+ // HLC
401
+ export interface Clock {
402
+ readonly get: () => Timestamp;
403
+ readonly save: (timestamp: Timestamp) => Result<void, SqliteError>;
24
404
  }
25
405
 
26
- export const createWebSocketSync: CreateSync = (_deps) => (config) => {
27
- const sync: Sync = {
28
- send: (message) => {
29
- /**
30
- * We don't need an in-memory queue; apps can be offline for a long time,
31
- * and mutations are stored in SQLite. Dropped CRDT messages are synced
32
- * when the web socket connection is open.
33
- */
34
- if (socket.getReadyState() !== "open") return;
35
- socket.send(message);
36
- },
406
+ export const createClock =
407
+ (deps: RandomBytesDep & SqliteDep) =>
408
+ (initialTimestamp = createInitialTimestamp(deps)): Clock => {
409
+ let currentTimestamp = initialTimestamp;
410
+
411
+ return {
412
+ get: () => currentTimestamp,
413
+ save: (timestamp) => {
414
+ currentTimestamp = timestamp;
415
+
416
+ const timestampString = timestampToTimestampString(timestamp);
417
+ const result = deps.sqlite.exec(sql.prepared`
418
+ update evolu_config set "clock" = ${timestampString};
419
+ `);
420
+ if (!result.ok) return result;
421
+
422
+ return ok();
423
+ },
424
+ };
37
425
  };
38
426
 
39
- const socket = createWebSocket(config.syncUrl, {
40
- binaryType: "arraybuffer",
41
- onOpen: () => {
42
- config.onOpen(sync.send);
43
- },
44
- onMessage: (data) => {
45
- if (data instanceof ArrayBuffer) {
46
- const messages = new Uint8Array(data);
47
- config.onMessage(messages, sync.send);
48
- }
49
- },
50
- });
427
+ interface GetSyncOwnerDep {
428
+ readonly getSyncOwner: (ownerId: OwnerId) => SyncOwner | null;
429
+ }
430
+
431
+ export interface ClientStorage extends Storage, BaseSqliteStorage {}
51
432
 
52
- return sync;
433
+ export interface ClientStorageDep {
434
+ readonly storage: ClientStorage;
435
+ }
436
+
437
+ const createClientStorage =
438
+ (
439
+ deps: ClockDep &
440
+ GetSyncOwnerDep &
441
+ RandomDep &
442
+ SqliteDep &
443
+ SymmetricCryptoDep &
444
+ TimeDep &
445
+ TimestampConfigDep &
446
+ PostMessageDep,
447
+ ) =>
448
+ (config: {
449
+ onError: (
450
+ error:
451
+ | ProtocolInvalidDataError
452
+ | ProtocolTimestampMismatchError
453
+ | SqliteError
454
+ | SymmetricCryptoDecryptError
455
+ | TimestampCounterOverflowError
456
+ | TimestampDriftError
457
+ | TimestampTimeOutOfRangeError,
458
+ ) => void;
459
+ onReceive: () => void;
460
+ }): Result<ClientStorage, SqliteError> => {
461
+ const sqliteStorageBase = createBaseSqliteStorage(deps)({
462
+ onStorageError: config.onError,
463
+ isOwnerWithinQuota: constTrue, // Clients don't have quota limits
464
+ });
465
+
466
+ // TODO: Mutex per OwnerId
467
+ const mutex = createMutex();
468
+
469
+ const storage: ClientStorage = {
470
+ ...sqliteStorageBase,
471
+
472
+ // Not implemented yet.
473
+ validateWriteKey: constFalse,
474
+ setWriteKey: constFalse,
475
+
476
+ writeMessages: async (ownerIdBytes, encryptedMessages) => {
477
+ const ownerId = ownerIdBytesToOwnerId(ownerIdBytes);
478
+
479
+ // Everything is sync now, but we will need async crypto in the future.
480
+ const writeResult = await mutex.withLock<
481
+ boolean,
482
+ | AbortError
483
+ | ProtocolInvalidDataError
484
+ | ProtocolTimestampMismatchError
485
+ | SqliteError
486
+ | SymmetricCryptoDecryptError
487
+ | TimestampCounterOverflowError
488
+ | TimestampDriftError
489
+ | TimestampTimeOutOfRangeError
490
+ // eslint-disable-next-line @typescript-eslint/require-await
491
+ >(async () => {
492
+ const owner = deps.getSyncOwner(ownerId);
493
+ // Owner can be removed during syncing.
494
+ // `ok(true)` means success, we just skipped the write.
495
+ if (!owner) return ok(true);
496
+
497
+ // TODO: Add quota checking for collaborative scenarios.
498
+ // When receiving messages from other owners via relay broadcast,
499
+ // check if this owner is within quota before accepting the data.
500
+ // This prevents an owner from exceeding storage limits when receiving
501
+ // data shared by other collaborators.
502
+
503
+ const messages: Array<CrdtMessage> = [];
504
+
505
+ for (const message of encryptedMessages) {
506
+ const change = decryptAndDecodeDbChange(deps)(
507
+ message,
508
+ owner.encryptionKey,
509
+ );
510
+ if (!change.ok) return change;
511
+
512
+ messages.push({
513
+ timestamp: message.timestamp,
514
+ change: change.value,
515
+ });
516
+ }
517
+
518
+ const transaction = deps.sqlite.transaction(() => {
519
+ let clockTimestamp = deps.clock.get();
520
+
521
+ for (const message of messages) {
522
+ const nextTimestamp = receiveTimestamp(deps)(
523
+ clockTimestamp,
524
+ message.timestamp,
525
+ );
526
+ if (!nextTimestamp.ok) return nextTimestamp;
527
+
528
+ clockTimestamp = nextTimestamp.value;
529
+ }
530
+
531
+ if (isNonEmptyReadonlyArray(messages)) {
532
+ const applyMessagesResult = applyMessages({ ...deps, storage })(
533
+ owner.id,
534
+ messages,
535
+ );
536
+ if (!applyMessagesResult.ok) return applyMessagesResult;
537
+ }
538
+
539
+ // // Apply local mutations atomically with approved messages
540
+ // for (const change of localMutations) {
541
+ // const result = applyLocalOnlyChange(deps)(change);
542
+ // if (!result.ok) return result;
543
+ // }
544
+
545
+ return deps.clock.save(clockTimestamp);
546
+ });
547
+
548
+ if (!transaction.ok) return transaction;
549
+
550
+ return ok(true);
551
+ })();
552
+
553
+ if (!writeResult.ok) {
554
+ if (writeResult.error.type !== "AbortError") {
555
+ config.onError(writeResult.error);
556
+ }
557
+ return err<StorageWriteError>({ type: "StorageWriteError", ownerId });
558
+ }
559
+
560
+ config.onReceive();
561
+
562
+ return ok();
563
+ },
564
+
565
+ readDbChange: (ownerId, timestamp) => {
566
+ const owner = deps.getSyncOwner(ownerIdBytesToOwnerId(ownerId));
567
+ // Owner can be removed to stop syncing.
568
+ if (!owner) return null;
569
+
570
+ const result = deps.sqlite.exec<{
571
+ table: string;
572
+ id: IdBytes;
573
+ column: string;
574
+ value: SqliteValue;
575
+ }>(sql`
576
+ select "table", "id", "column", "value"
577
+ from evolu_history
578
+ where "ownerId" = ${ownerId} and "timestamp" = ${timestamp};
579
+ `);
580
+ if (!result.ok) {
581
+ config.onError(result.error);
582
+ return null;
583
+ }
584
+
585
+ const { rows } = result.value;
586
+ assert(rows.length > 0, "Rows must not be empty");
587
+
588
+ const { table, id } = rows[0];
589
+ const values: Record<string, SqliteValue> = {};
590
+
591
+ for (const r of rows) {
592
+ assert(r.table === table, "All rows must have the same table");
593
+ assert(eqArrayNumber(r.id, id), "All rows must have the same Id");
594
+ values[r.column] = r.value;
595
+ }
596
+
597
+ const message: CrdtMessage = {
598
+ timestamp: timestampBytesToTimestamp(timestamp),
599
+ change: {
600
+ table: rows[0].table,
601
+ id: idBytesToId(rows[0].id),
602
+ values,
603
+ },
604
+ };
605
+
606
+ return encodeAndEncryptDbChange(deps)(message, owner.encryptionKey);
607
+ },
608
+ };
609
+
610
+ return ok(storage);
611
+ };
612
+
613
+ type TransportKey = string & Brand<"TransportKey">;
614
+
615
+ /** Creates a unique identifier for a transport configuration. */
616
+ const createTransportKey = (transportConfig: OwnerTransport): TransportKey => {
617
+ return `${transportConfig.type}:${transportConfig.url}` as TransportKey;
53
618
  };
54
619
 
620
+ export const applyLocalOnlyChange =
621
+ (deps: SqliteDep & TimeDep) =>
622
+ (change: MutationChange): Result<void, SqliteError> => {
623
+ const dbChange: DbChange = {
624
+ table: change.table,
625
+ id: change.id,
626
+ values: change.values,
627
+ };
628
+
629
+ const isDeletion =
630
+ "isDeleted" in dbChange.values && dbChange.values.isDeleted === 1;
631
+
632
+ if (isDeletion) {
633
+ const result = deps.sqlite.exec(sql`
634
+ delete from ${sql.identifier(dbChange.table)}
635
+ where id = ${dbChange.id};
636
+ `);
637
+ if (!result.ok) return result;
638
+ } else {
639
+ const date = new Date(deps.time.now()).toISOString();
640
+
641
+ for (const [column, value] of objectToEntries(dbChange.values)) {
642
+ const result = deps.sqlite.exec(sql.prepared`
643
+ insert into ${sql.identifier(dbChange.table)}
644
+ ("id", ${sql.identifier(column)}, createdAt, updatedAt)
645
+ values (${dbChange.id}, ${value}, ${date}, ${date})
646
+ on conflict ("id") do update
647
+ set
648
+ ${sql.identifier(column)} = ${value},
649
+ updatedAt = ${date};
650
+ `);
651
+ if (!result.ok) return result;
652
+ }
653
+ }
654
+
655
+ return ok();
656
+ };
657
+
658
+ const applyMessages =
659
+ (deps: ClientStorageDep & ClockDep & RandomDep & SqliteDep) =>
660
+ (
661
+ ownerId: OwnerId,
662
+ messages: NonEmptyReadonlyArray<CrdtMessage>,
663
+ ): Result<void, SqliteError> => {
664
+ const ownerIdBytes = ownerIdToOwnerIdBytes(ownerId);
665
+
666
+ const usageResult = getOwnerUsage(deps)(
667
+ ownerIdBytes,
668
+ timestampToTimestampBytes(firstInArray(messages).timestamp),
669
+ );
670
+ if (!usageResult.ok) return usageResult;
671
+
672
+ let { firstTimestamp, lastTimestamp } = usageResult.value;
673
+
674
+ for (const message of messages) {
675
+ const result1 = applyMessageToAppTable(deps)(ownerIdBytes, message);
676
+ if (!result1.ok) return result1;
677
+
678
+ const timestamp = timestampToTimestampBytes(message.timestamp);
679
+
680
+ let strategy;
681
+ [strategy, firstTimestamp, lastTimestamp] = getTimestampInsertStrategy(
682
+ timestamp,
683
+ firstTimestamp,
684
+ lastTimestamp,
685
+ );
686
+
687
+ const result2 = applyMessageToTimestampAndHistoryTables(deps)(
688
+ ownerIdBytes,
689
+ message,
690
+ strategy,
691
+ );
692
+ if (!result2.ok) return result2;
693
+ }
694
+
695
+ /**
696
+ * TODO: Implement proper storedBytes tracking for client using encrypted
697
+ * message sizes (need to figure out how to reuse received or postpone
698
+ * client...).
699
+ */
700
+ const updateUsage = updateOwnerUsage(deps)(
701
+ ownerIdBytes,
702
+ 1 as PositiveInt, // Placeholder until proper tracking implemented
703
+ firstTimestamp,
704
+ lastTimestamp,
705
+ );
706
+ if (!updateUsage.ok) return updateUsage;
707
+
708
+ return ok();
709
+ };
710
+
711
+ const applyMessageToAppTable =
712
+ (deps: SqliteDep) =>
713
+ (ownerId: OwnerIdBytes, message: CrdtMessage): Result<void, SqliteError> => {
714
+ const timestamp = timestampToTimestampBytes(message.timestamp);
715
+ const updatedAt = new Date(message.timestamp.millis).toISOString();
716
+
717
+ for (const [column, value] of objectToEntries(message.change.values)) {
718
+ const result = deps.sqlite.exec(sql.prepared`
719
+ with
720
+ existingTimestamp as (
721
+ select 1
722
+ from evolu_history
723
+ where
724
+ "ownerId" = ${ownerId}
725
+ and "table" = ${message.change.table}
726
+ and "id" = ${idToIdBytes(message.change.id)}
727
+ and "column" = ${column}
728
+ and "timestamp" >= ${timestamp}
729
+ limit 1
730
+ )
731
+ insert into ${sql.identifier(message.change.table)}
732
+ ("id", ${sql.identifier(column)}, updatedAt)
733
+ select ${message.change.id}, ${value}, ${updatedAt}
734
+ where not exists (select 1 from existingTimestamp)
735
+ on conflict ("id") do update
736
+ set
737
+ ${sql.identifier(column)} = ${value},
738
+ updatedAt = ${updatedAt}
739
+ where not exists (select 1 from existingTimestamp);
740
+ `);
741
+
742
+ if (!result.ok) return result;
743
+ }
744
+
745
+ return ok();
746
+ };
747
+
748
+ export const applyMessageToTimestampAndHistoryTables =
749
+ (deps: ClientStorageDep & SqliteDep) =>
750
+ (
751
+ ownerId: OwnerIdBytes,
752
+ message: CrdtMessage,
753
+ strategy: StorageInsertTimestampStrategy,
754
+ ): Result<void, SqliteError> => {
755
+ const timestamp = timestampToTimestampBytes(message.timestamp);
756
+ const id = idToIdBytes(message.change.id);
757
+
758
+ const result = deps.storage.insertTimestamp(ownerId, timestamp, strategy);
759
+ if (!result.ok) return result;
760
+
761
+ for (const [column, value] of Object.entries(message.change.values)) {
762
+ const result = deps.sqlite.exec(sql.prepared`
763
+ insert into evolu_history
764
+ ("ownerId", "table", "id", "column", "value", "timestamp")
765
+ values
766
+ (
767
+ ${ownerId},
768
+ ${message.change.table},
769
+ ${id},
770
+ ${column},
771
+ ${value},
772
+ ${timestamp}
773
+ )
774
+ on conflict do nothing;
775
+ `);
776
+ if (!result.ok) return result;
777
+ }
778
+
779
+ return ok();
780
+ };
781
+
55
782
  /**
783
+ * TODO: Rework for the new owners API.
784
+ *
56
785
  * The possible states of a synchronization process. The `SyncState` can be one
57
786
  * of the following:
58
787
  *