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

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 +58 -5
  2. package/dist/src/Array.d.ts.map +1 -1
  3. package/dist/src/Array.js +53 -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 +158 -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 +264 -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 +243 -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 +212 -26
  61. package/dist/src/Evolu/Storage.d.ts.map +1 -1
  62. package/dist/src/Evolu/Storage.js +137 -79
  63. package/dist/src/Evolu/Sync.d.ts +68 -13
  64. package/dist/src/Evolu/Sync.d.ts.map +1 -1
  65. package/dist/src/Evolu/Sync.js +422 -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 +621 -340
  103. package/dist/src/Type.d.ts.map +1 -1
  104. package/dist/src/Type.js +665 -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 +76 -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 +514 -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 +369 -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 +420 -92
  137. package/src/Evolu/Schema.ts +391 -191
  138. package/src/Evolu/Storage.ts +451 -118
  139. package/src/Evolu/Sync.ts +720 -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 +1083 -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
@@ -1,23 +1,78 @@
1
- import { ConsoleConfig, ConsoleDep } from "../Console.js";
2
- import { ProtocolMessage } from "./Protocol.js";
3
- import { Millis } from "./Timestamp.js";
4
- export interface Sync {
5
- readonly send: (message: ProtocolMessage) => void;
1
+ import { NonEmptyReadonlyArray } from "../Array.js";
2
+ import { ConsoleDep } from "../Console.js";
3
+ import { RandomBytesDep, SymmetricCryptoDecryptError, SymmetricCryptoDep } from "../Crypto.js";
4
+ import { TransferableError } from "../Error.js";
5
+ import { RandomDep } from "../Random.js";
6
+ import { Result } from "../Result.js";
7
+ import { SqliteDep, SqliteError } from "../Sqlite.js";
8
+ import { TimeDep } from "../Time.js";
9
+ import { CreateWebSocketDep } from "../WebSocket.js";
10
+ import type { PostMessageDep } from "./Db.js";
11
+ import { AppOwner, OwnerEncryptionKey, OwnerId, OwnerIdBytes, OwnerTransport, OwnerWriteKey } from "./Owner.js";
12
+ import { ProtocolError, ProtocolInvalidDataError, ProtocolTimestampMismatchError } from "./Protocol.js";
13
+ import { MutationChange } from "./Schema.js";
14
+ import { BaseSqliteStorage, CrdtMessage, Storage } from "./Storage.js";
15
+ import { Millis, Timestamp, TimestampConfigDep, TimestampCounterOverflowError, TimestampDriftError, TimestampTimeOutOfRangeError } from "./Timestamp.js";
16
+ export interface Sync extends Disposable {
17
+ /**
18
+ * Assigns or removes an owner to/from transports with reference counting.
19
+ *
20
+ * Owners are only "active" if assigned to at least one transport. Uses
21
+ * `owner.transports` or falls back to config transports. Multiple calls
22
+ * increment/decrement reference counts (useful for React Hooks).
23
+ */
24
+ readonly useOwner: (use: boolean, owner: SyncOwner) => void;
25
+ readonly applyChanges: (changes: NonEmptyReadonlyArray<MutationChange>) => Result<void, SqliteError | TimestampCounterOverflowError | TimestampDriftError | TimestampTimeOutOfRangeError>;
6
26
  }
7
27
  export interface SyncDep {
8
28
  readonly sync: Sync;
9
29
  }
10
- export type CreateSync = (deps: ConsoleDep) => (config: SyncConfig) => Sync;
11
- export interface CreateSyncDep {
12
- readonly createSync: CreateSync;
30
+ /**
31
+ * Represents an owner for sync operations. This is a unified interface that
32
+ * abstracts over the specific owner types ({@link ShardOwner},
33
+ * {@link SharedOwner}, {@link SharedReadonlyOwner}) for the sync layer.
34
+ *
35
+ * The sync layer only needs the essential data for synchronization and doesn't
36
+ * need to distinguish between different owner types.
37
+ */
38
+ export interface SyncOwner {
39
+ readonly id: OwnerId;
40
+ readonly encryptionKey: OwnerEncryptionKey;
41
+ /** Optional for read-only owners like {@link SharedReadonlyOwner}. */
42
+ readonly writeKey?: OwnerWriteKey;
43
+ readonly transports?: ReadonlyArray<OwnerTransport>;
44
+ }
45
+ export interface SyncConfig {
46
+ readonly appOwner: AppOwner;
47
+ readonly transports: ReadonlyArray<OwnerTransport>;
48
+ /**
49
+ * Delay in milliseconds before disposing unused WebSocket connections.
50
+ * Defaults to 100ms.
51
+ */
52
+ readonly disposalDelayMs?: number;
53
+ readonly onError: (error: ProtocolError | ProtocolInvalidDataError | ProtocolTimestampMismatchError | SqliteError | SymmetricCryptoDecryptError | TimestampCounterOverflowError | TimestampDriftError | TimestampTimeOutOfRangeError | TransferableError) => void;
54
+ readonly onReceive: () => void;
13
55
  }
14
- export interface SyncConfig extends ConsoleConfig {
15
- readonly syncUrl: string;
16
- readonly onOpen: (send: Sync["send"]) => void;
17
- readonly onMessage: (message: Uint8Array, send: Sync["send"]) => void;
56
+ export declare const createSync: (deps: ClockDep & ConsoleDep & CreateWebSocketDep & PostMessageDep & RandomBytesDep & RandomDep & SqliteDep & SymmetricCryptoDep & TimeDep & TimestampConfigDep) => (config: SyncConfig) => Result<Sync, SqliteError>;
57
+ export interface ClockDep {
58
+ readonly clock: Clock;
18
59
  }
19
- export declare const createWebSocketSync: CreateSync;
60
+ export interface Clock {
61
+ readonly get: () => Timestamp;
62
+ readonly save: (timestamp: Timestamp) => Result<void, SqliteError>;
63
+ }
64
+ export declare const createClock: (deps: RandomBytesDep & SqliteDep) => (initialTimestamp?: Timestamp) => Clock;
65
+ export interface ClientStorage extends Storage, BaseSqliteStorage {
66
+ }
67
+ export interface ClientStorageDep {
68
+ readonly storage: ClientStorage;
69
+ }
70
+ export declare const applyLocalOnlyChange: (deps: SqliteDep & TimeDep) => (change: MutationChange) => Result<void, SqliteError>;
71
+ export declare const applyMessages: (deps: ClientStorageDep & ClockDep & RandomDep & SqliteDep) => (ownerId: OwnerId, messages: ReadonlyArray<CrdtMessage>) => Result<void, SqliteError>;
72
+ export declare const applyMessageToTimestampAndHistoryTables: (deps: ClientStorageDep & SqliteDep) => (ownerId: OwnerIdBytes, message: CrdtMessage) => Result<void, SqliteError>;
20
73
  /**
74
+ * TODO: Rework for the new owners API.
75
+ *
21
76
  * The possible states of a synchronization process. The `SyncState` can be one
22
77
  * of the following:
23
78
  *
@@ -1 +1 @@
1
- {"version":3,"file":"Sync.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Sync.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE1D,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAExC,MAAM,WAAW,IAAI;IACnB,QAAQ,CAAC,IAAI,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC;CACnD;AAED,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,IAAI,EAAE,IA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AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,IAAI,EAAE,oBAAoB,CAAC;CACrC;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IACnC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,IAAI,EAAE,sBAAsB,CAAC;IACtC,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,WAAW,GAAG,oBAAoB,CAAC;CACnE;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;CAC/B;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,IAAI,EAAE,sBAAsB,CAAC;CACvC;AAED,eAAO,MAAM,gBAAgB,EAAE,gBAA+C,CAAC"}
@@ -1,29 +1,431 @@
1
- import { createWebSocket } from "../WebSocket.js";
2
- export const createWebSocketSync = (_deps) => (config) => {
1
+ import { assert } from "../Assert.js";
2
+ import { eqArrayNumber } from "../Eq.js";
3
+ import { createTransferableError } from "../Error.js";
4
+ import { constFalse, constTrue } from "../Function.js";
5
+ import { objectToEntries } from "../Object.js";
6
+ import { createResources } from "../Resources.js";
7
+ import { err, ok } from "../Result.js";
8
+ import { sql } from "../Sqlite.js";
9
+ import { createMutex } from "../Task.js";
10
+ import { idBytesToId, idToIdBytes } from "../Type.js";
11
+ import { ownerIdBytesToOwnerId, ownerIdToOwnerIdBytes, } from "./Owner.js";
12
+ import { applyProtocolMessageAsClient, createProtocolMessageForSync, createProtocolMessageForUnsubscribe, createProtocolMessageFromCrdtMessages, decryptAndDecodeDbChange, encodeAndEncryptDbChange, SubscriptionFlags, } from "./Protocol.js";
13
+ import { createBaseSqliteStorage, } from "./Storage.js";
14
+ import { createInitialTimestamp, receiveTimestamp, sendTimestamp, timestampBytesToTimestamp, timestampToTimestampBytes, timestampToTimestampString, } from "./Timestamp.js";
15
+ export const createSync = (deps) => (config) => {
16
+ let isDisposed = false;
17
+ /** Returns owner data only if actively assigned to at least one transport. */
18
+ const getSyncOwner = (ownerId) => {
19
+ if (isDisposed)
20
+ return null;
21
+ return transports.getConsumer(ownerId);
22
+ };
23
+ const storageResult = createClientStorage({
24
+ ...deps,
25
+ getSyncOwner,
26
+ })(config);
27
+ if (!storageResult.ok)
28
+ return storageResult;
29
+ const storage = storageResult.value;
30
+ const createResource = (transportConfig) => {
31
+ const transportKey = createTransportKey(transportConfig);
32
+ deps.console.log("[sync]", "createWebSocket", {
33
+ transportKey,
34
+ url: transportConfig.url,
35
+ });
36
+ return deps.createWebSocket(transportConfig.url, {
37
+ binaryType: "arraybuffer",
38
+ onOpen: () => {
39
+ if (isDisposed)
40
+ return;
41
+ const webSocket = transports.getResource(transportKey);
42
+ if (!webSocket)
43
+ return;
44
+ const ownerIds = transports.getConsumersForResource(transportKey);
45
+ deps.console.log("[sync]", "onOpen", { transportKey, ownerIds });
46
+ for (const ownerId of ownerIds) {
47
+ const message = createProtocolMessageForSync({ storage })(ownerId, SubscriptionFlags.Subscribe);
48
+ if (!message)
49
+ continue;
50
+ deps.console.log("[sync]", "send", { message });
51
+ webSocket.send(message);
52
+ }
53
+ },
54
+ onClose: (event) => {
55
+ deps.console.log("[sync]", "onClose", {
56
+ transportKey,
57
+ code: event.code,
58
+ reason: event.reason,
59
+ wasClean: event.wasClean,
60
+ });
61
+ },
62
+ onError: (error) => {
63
+ deps.console.warn("[sync]", "onError", { transportKey, error });
64
+ },
65
+ onMessage: (data) => {
66
+ // Only handle ArrayBuffer data for sync messages
67
+ if (isDisposed || !(data instanceof ArrayBuffer))
68
+ return;
69
+ const webSocket = transports.getResource(transportKey);
70
+ if (!webSocket)
71
+ return;
72
+ const input = new Uint8Array(data);
73
+ deps.console.log("[sync]", "onMessage", {
74
+ transportKey,
75
+ message: input,
76
+ });
77
+ applyProtocolMessageAsClient({ storage })(input, {
78
+ // No write key, no sync (for a case when an owner was unused).
79
+ getWriteKey: (ownerId) => getSyncOwner(ownerId)?.writeKey ?? null,
80
+ })
81
+ .then((message) => {
82
+ if (!message.ok) {
83
+ config.onError(message.error);
84
+ return;
85
+ }
86
+ switch (message.value.type) {
87
+ case "response":
88
+ webSocket.send(message.value.message);
89
+ break;
90
+ case "no-response":
91
+ // Sync complete, no response needed
92
+ break;
93
+ case "broadcast":
94
+ // This was a broadcast message, don't affect sync counter
95
+ break;
96
+ }
97
+ })
98
+ .catch((error) => {
99
+ config.onError(createTransferableError(error));
100
+ });
101
+ },
102
+ });
103
+ };
104
+ const transports = createResources({
105
+ createResource,
106
+ getResourceKey: createTransportKey,
107
+ getConsumerId: (owner) => owner.id,
108
+ disposalDelay: config.disposalDelayMs ?? 100,
109
+ onConsumerAdded: (owner, webSocket) => {
110
+ deps.console.log("[sync]", "onConsumerAdded", {
111
+ ownerId: owner.id,
112
+ isOpen: webSocket.isOpen(),
113
+ });
114
+ // The onOpen handler will sync it.
115
+ if (!webSocket.isOpen())
116
+ return;
117
+ const message = createProtocolMessageForSync({ storage })(owner.id, SubscriptionFlags.Subscribe);
118
+ if (message)
119
+ webSocket.send(message);
120
+ },
121
+ onConsumerRemoved: (owner, webSocket) => {
122
+ deps.console.log("[sync]", "onConsumerRemoved", {
123
+ ownerId: owner.id,
124
+ isOpen: webSocket.isOpen(),
125
+ });
126
+ const message = createProtocolMessageForUnsubscribe(owner.id);
127
+ webSocket.send(message);
128
+ },
129
+ });
3
130
  const sync = {
4
- send: (message) => {
5
- /**
6
- * We don't need an in-memory queue; apps can be offline for a long time,
7
- * and mutations are stored in SQLite. Dropped CRDT messages are synced
8
- * when the web socket connection is open.
9
- */
10
- if (socket.getReadyState() !== "open")
131
+ useOwner: (use, owner) => {
132
+ if (isDisposed) {
133
+ deps.console.warn("[sync]", "useOwner called on disposed Sync instance", { owner });
11
134
  return;
12
- socket.send(message);
135
+ }
136
+ deps.console.log("[sync]", "useOwner", { use, owner });
137
+ const transportsToUse = owner.transports ?? config.transports;
138
+ if (use) {
139
+ transports.addConsumer(owner, transportsToUse);
140
+ }
141
+ else {
142
+ const result = transports.removeConsumer(owner, transportsToUse);
143
+ if (!result.ok) {
144
+ deps.console.warn("[sync]", "Failed to remove consumer", {
145
+ transportsToUse,
146
+ ownerId: owner.id,
147
+ error: result.error,
148
+ });
149
+ }
150
+ }
151
+ },
152
+ applyChanges: (changes) => {
153
+ deps.console.log("[sync]", "applyChanges", { changes });
154
+ let clockTimestamp = deps.clock.get();
155
+ const ownerMessages = new Map();
156
+ for (const change of changes) {
157
+ const nextTimestamp = sendTimestamp(deps)(clockTimestamp);
158
+ if (!nextTimestamp.ok)
159
+ return nextTimestamp;
160
+ clockTimestamp = nextTimestamp.value;
161
+ const { ownerId = config.appOwner.id, ...dbChange } = change;
162
+ const message = { timestamp: clockTimestamp, change: dbChange };
163
+ const messages = ownerMessages.get(ownerId);
164
+ if (messages)
165
+ messages.push(message);
166
+ else
167
+ ownerMessages.set(ownerId, [message]);
168
+ }
169
+ for (const [ownerId, messages] of ownerMessages) {
170
+ const result = applyMessages({ ...deps, storage })(ownerId, messages);
171
+ if (!result.ok)
172
+ return result;
173
+ const owner = getSyncOwner(ownerId);
174
+ if (!owner?.writeKey)
175
+ continue;
176
+ const message = createProtocolMessageFromCrdtMessages(deps)({
177
+ id: owner.id,
178
+ encryptionKey: owner.encryptionKey,
179
+ writeKey: owner.writeKey,
180
+ }, messages);
181
+ const transportsToUse = owner.transports ?? config.transports;
182
+ // Send message to all transports for this owner
183
+ for (const transportConfig of transportsToUse) {
184
+ const transportKey = createTransportKey(transportConfig);
185
+ const webSocket = transports.getResource(transportKey);
186
+ if (!webSocket)
187
+ continue;
188
+ if (webSocket.isOpen()) {
189
+ deps.console.log("[sync]", "send", { transportKey, message });
190
+ webSocket.send(message);
191
+ }
192
+ }
193
+ }
194
+ return deps.clock.save(clockTimestamp);
195
+ },
196
+ [Symbol.dispose]: () => {
197
+ if (isDisposed)
198
+ return;
199
+ isDisposed = true;
200
+ transports[Symbol.dispose]();
13
201
  },
14
202
  };
15
- const socket = createWebSocket(config.syncUrl, {
16
- binaryType: "arraybuffer",
17
- onOpen: () => {
18
- config.onOpen(sync.send);
203
+ return ok(sync);
204
+ };
205
+ export const createClock = (deps) => (initialTimestamp = createInitialTimestamp(deps)) => {
206
+ let currentTimestamp = initialTimestamp;
207
+ return {
208
+ get: () => currentTimestamp,
209
+ save: (timestamp) => {
210
+ currentTimestamp = timestamp;
211
+ const timestampString = timestampToTimestampString(timestamp);
212
+ const result = deps.sqlite.exec(sql.prepared `
213
+ update evolu_config set "clock" = ${timestampString};
214
+ `);
215
+ if (!result.ok)
216
+ return result;
217
+ return ok();
19
218
  },
20
- onMessage: (data) => {
21
- if (data instanceof ArrayBuffer) {
22
- const messages = new Uint8Array(data);
23
- config.onMessage(messages, sync.send);
219
+ };
220
+ };
221
+ const createClientStorage = (deps) => (config) => {
222
+ const sqliteStorageBase = createBaseSqliteStorage(deps)({
223
+ onStorageError: config.onError,
224
+ isOwnerWithinQuota: constTrue, // Clients don't have quota limits
225
+ });
226
+ // TODO: Mutex per OwnerId
227
+ const mutex = createMutex();
228
+ const storage = {
229
+ ...sqliteStorageBase,
230
+ // Not implemented yet.
231
+ validateWriteKey: constFalse,
232
+ setWriteKey: constFalse,
233
+ writeMessages: async (ownerIdBytes, encryptedMessages) => {
234
+ const ownerId = ownerIdBytesToOwnerId(ownerIdBytes);
235
+ // Everything is sync now, but we will need async crypto in the future.
236
+ const writeResult = await mutex.withLock(async () => {
237
+ const owner = deps.getSyncOwner(ownerId);
238
+ // Owner can be removed during syncing.
239
+ // `ok(true)` means success, we just skipped the write.
240
+ if (!owner)
241
+ return ok(true);
242
+ // TODO: Add quota checking for collaborative scenarios.
243
+ // When receiving messages from other owners via relay broadcast,
244
+ // check if this owner is within quota before accepting the data.
245
+ // This prevents an owner from exceeding storage limits when receiving
246
+ // data shared by other collaborators.
247
+ const messages = [];
248
+ for (const message of encryptedMessages) {
249
+ const change = decryptAndDecodeDbChange(deps)(message, owner.encryptionKey);
250
+ if (!change.ok)
251
+ return change;
252
+ messages.push({
253
+ timestamp: message.timestamp,
254
+ change: change.value,
255
+ });
256
+ }
257
+ const transaction = deps.sqlite.transaction(() => {
258
+ let clockTimestamp = deps.clock.get();
259
+ for (const message of messages) {
260
+ const nextTimestamp = receiveTimestamp(deps)(clockTimestamp, message.timestamp);
261
+ if (!nextTimestamp.ok)
262
+ return nextTimestamp;
263
+ clockTimestamp = nextTimestamp.value;
264
+ }
265
+ const applyMessagesResult = applyMessages({ ...deps, storage })(owner.id, messages);
266
+ if (!applyMessagesResult.ok)
267
+ return applyMessagesResult;
268
+ // // Apply local mutations atomically with approved messages
269
+ // for (const change of localMutations) {
270
+ // const result = applyLocalOnlyChange(deps)(change);
271
+ // if (!result.ok) return result;
272
+ // }
273
+ return deps.clock.save(clockTimestamp);
274
+ });
275
+ if (!transaction.ok)
276
+ return transaction;
277
+ return ok(true);
278
+ })();
279
+ if (!writeResult.ok) {
280
+ if (writeResult.error.type !== "AbortError") {
281
+ config.onError(writeResult.error);
282
+ }
283
+ return err({ type: "StorageWriteError", ownerId });
24
284
  }
285
+ config.onReceive();
286
+ return ok();
25
287
  },
26
- });
27
- return sync;
288
+ readDbChange: (ownerId, timestamp) => {
289
+ const owner = deps.getSyncOwner(ownerIdBytesToOwnerId(ownerId));
290
+ // Owner can be removed to stop syncing.
291
+ if (!owner)
292
+ return null;
293
+ const result = deps.sqlite.exec(sql `
294
+ select "table", "id", "column", "value"
295
+ from evolu_history
296
+ where "ownerId" = ${ownerId} and "timestamp" = ${timestamp};
297
+ `);
298
+ if (!result.ok) {
299
+ config.onError(result.error);
300
+ return null;
301
+ }
302
+ const { rows } = result.value;
303
+ assert(rows.length > 0, "Rows must not be empty");
304
+ const { table, id } = rows[0];
305
+ const values = {};
306
+ for (const r of rows) {
307
+ assert(r.table === table, "All rows must have the same table");
308
+ assert(eqArrayNumber(r.id, id), "All rows must have the same Id");
309
+ values[r.column] = r.value;
310
+ }
311
+ const message = {
312
+ timestamp: timestampBytesToTimestamp(timestamp),
313
+ change: {
314
+ table: rows[0].table,
315
+ id: idBytesToId(rows[0].id),
316
+ values,
317
+ },
318
+ };
319
+ return encodeAndEncryptDbChange(deps)(message, owner.encryptionKey);
320
+ },
321
+ };
322
+ return ok(storage);
323
+ };
324
+ /** Creates a unique identifier for a transport configuration. */
325
+ const createTransportKey = (transportConfig) => {
326
+ return `${transportConfig.type}:${transportConfig.url}`;
327
+ };
328
+ export const applyLocalOnlyChange = (deps) => (change) => {
329
+ const dbChange = {
330
+ table: change.table,
331
+ id: change.id,
332
+ values: change.values,
333
+ };
334
+ const isDeletion = "isDeleted" in dbChange.values && dbChange.values.isDeleted === 1;
335
+ if (isDeletion) {
336
+ const result = deps.sqlite.exec(sql `
337
+ delete from ${sql.identifier(dbChange.table)}
338
+ where id = ${dbChange.id};
339
+ `);
340
+ if (!result.ok)
341
+ return result;
342
+ }
343
+ else {
344
+ const date = new Date(deps.time.now()).toISOString();
345
+ for (const [column, value] of objectToEntries(dbChange.values)) {
346
+ const result = deps.sqlite.exec(sql.prepared `
347
+ insert into ${sql.identifier(dbChange.table)}
348
+ ("id", ${sql.identifier(column)}, createdAt, updatedAt)
349
+ values (${dbChange.id}, ${value}, ${date}, ${date})
350
+ on conflict ("id") do update
351
+ set
352
+ ${sql.identifier(column)} = ${value},
353
+ updatedAt = ${date};
354
+ `);
355
+ if (!result.ok)
356
+ return result;
357
+ }
358
+ }
359
+ return ok();
360
+ };
361
+ export const applyMessages = (deps) => (ownerId, messages) => {
362
+ const ownerIdBytes = ownerIdToOwnerIdBytes(ownerId);
363
+ for (const message of messages) {
364
+ const result1 = applyMessageToAppTable(deps)(ownerIdBytes, message);
365
+ if (!result1.ok)
366
+ return result1;
367
+ const result2 = applyMessageToTimestampAndHistoryTables(deps)(ownerIdBytes, message);
368
+ if (!result2.ok)
369
+ return result2;
370
+ }
371
+ return ok();
372
+ };
373
+ const applyMessageToAppTable = (deps) => (ownerId, message) => {
374
+ const timestamp = timestampToTimestampBytes(message.timestamp);
375
+ const updatedAt = new Date(message.timestamp.millis).toISOString();
376
+ for (const [column, value] of objectToEntries(message.change.values)) {
377
+ const result = deps.sqlite.exec(sql.prepared `
378
+ with
379
+ existingTimestamp as (
380
+ select 1
381
+ from evolu_history
382
+ where
383
+ "ownerId" = ${ownerId}
384
+ and "table" = ${message.change.table}
385
+ and "id" = ${idToIdBytes(message.change.id)}
386
+ and "column" = ${column}
387
+ and "timestamp" >= ${timestamp}
388
+ limit 1
389
+ )
390
+ insert into ${sql.identifier(message.change.table)}
391
+ ("id", ${sql.identifier(column)}, updatedAt)
392
+ select ${message.change.id}, ${value}, ${updatedAt}
393
+ where not exists (select 1 from existingTimestamp)
394
+ on conflict ("id") do update
395
+ set
396
+ ${sql.identifier(column)} = ${value},
397
+ updatedAt = ${updatedAt}
398
+ where not exists (select 1 from existingTimestamp);
399
+ `);
400
+ if (!result.ok)
401
+ return result;
402
+ }
403
+ return ok();
404
+ };
405
+ export const applyMessageToTimestampAndHistoryTables = (deps) => (ownerId, message) => {
406
+ const timestamp = timestampToTimestampBytes(message.timestamp);
407
+ const id = idToIdBytes(message.change.id);
408
+ const result = deps.storage.insertTimestamp(ownerId, timestamp);
409
+ if (!result.ok)
410
+ return result;
411
+ for (const [column, value] of Object.entries(message.change.values)) {
412
+ const result = deps.sqlite.exec(sql.prepared `
413
+ insert into evolu_history
414
+ ("ownerId", "table", "id", "column", "value", "timestamp")
415
+ values
416
+ (
417
+ ${ownerId},
418
+ ${message.change.table},
419
+ ${id},
420
+ ${column},
421
+ ${value},
422
+ ${timestamp}
423
+ )
424
+ on conflict do nothing;
425
+ `);
426
+ if (!result.ok)
427
+ return result;
428
+ }
429
+ return ok();
28
430
  };
29
431
  export const initialSyncState = { type: "SyncStateInitial" };