@evolu/common 6.0.1-preview.2 → 6.0.1-preview.20

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 (154) hide show
  1. package/dist/src/Assert.d.ts +6 -3
  2. package/dist/src/Assert.d.ts.map +1 -1
  3. package/dist/src/Assert.js +7 -4
  4. package/dist/src/Brand.d.ts +75 -0
  5. package/dist/src/Brand.d.ts.map +1 -0
  6. package/dist/src/Brand.js +1 -0
  7. package/dist/src/Buffer.d.ts +1 -1
  8. package/dist/src/Buffer.d.ts.map +1 -1
  9. package/dist/src/Buffer.js +1 -1
  10. package/dist/src/CallbackRegistry.d.ts +53 -0
  11. package/dist/src/CallbackRegistry.d.ts.map +1 -0
  12. package/dist/src/CallbackRegistry.js +25 -0
  13. package/dist/src/Console.d.ts +31 -6
  14. package/dist/src/Console.d.ts.map +1 -1
  15. package/dist/src/Console.js +72 -9
  16. package/dist/src/Crypto.d.ts +61 -34
  17. package/dist/src/Crypto.d.ts.map +1 -1
  18. package/dist/src/Crypto.js +29 -42
  19. package/dist/src/Evolu/Db.d.ts +137 -66
  20. package/dist/src/Evolu/Db.d.ts.map +1 -1
  21. package/dist/src/Evolu/Db.js +300 -705
  22. package/dist/src/Evolu/Diff.d.ts +3 -3
  23. package/dist/src/Evolu/Diff.d.ts.map +1 -1
  24. package/dist/src/Evolu/Diff.js +7 -5
  25. package/dist/src/Evolu/Evolu.d.ts +200 -115
  26. package/dist/src/Evolu/Evolu.d.ts.map +1 -1
  27. package/dist/src/Evolu/Evolu.js +287 -157
  28. package/dist/src/Evolu/Internal.d.ts +0 -2
  29. package/dist/src/Evolu/Internal.d.ts.map +1 -1
  30. package/dist/src/Evolu/Internal.js +0 -2
  31. package/dist/src/Evolu/LocalAuth.d.ts +144 -0
  32. package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
  33. package/dist/src/Evolu/LocalAuth.js +171 -0
  34. package/dist/src/Evolu/Owner.d.ts +152 -127
  35. package/dist/src/Evolu/Owner.d.ts.map +1 -1
  36. package/dist/src/Evolu/Owner.js +88 -107
  37. package/dist/src/Evolu/Platform.d.ts +9 -7
  38. package/dist/src/Evolu/Platform.d.ts.map +1 -1
  39. package/dist/src/Evolu/Protocol.d.ts +170 -186
  40. package/dist/src/Evolu/Protocol.d.ts.map +1 -1
  41. package/dist/src/Evolu/Protocol.js +487 -343
  42. package/dist/src/Evolu/Public.d.ts +6 -8
  43. package/dist/src/Evolu/Public.d.ts.map +1 -1
  44. package/dist/src/Evolu/Public.js +2 -3
  45. package/dist/src/Evolu/PublicKysely.js +3 -3
  46. package/dist/src/Evolu/Query.d.ts +2 -1
  47. package/dist/src/Evolu/Query.d.ts.map +1 -1
  48. package/dist/src/Evolu/Relay.d.ts +4 -3
  49. package/dist/src/Evolu/Relay.d.ts.map +1 -1
  50. package/dist/src/Evolu/Relay.js +50 -12
  51. package/dist/src/Evolu/Schema.d.ts +129 -73
  52. package/dist/src/Evolu/Schema.d.ts.map +1 -1
  53. package/dist/src/Evolu/Schema.js +169 -89
  54. package/dist/src/Evolu/Storage.d.ts +159 -14
  55. package/dist/src/Evolu/Storage.d.ts.map +1 -1
  56. package/dist/src/Evolu/Storage.js +42 -32
  57. package/dist/src/Evolu/Sync.d.ts +77 -13
  58. package/dist/src/Evolu/Sync.d.ts.map +1 -1
  59. package/dist/src/Evolu/Sync.js +453 -20
  60. package/dist/src/Evolu/Timestamp.d.ts +35 -27
  61. package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
  62. package/dist/src/Evolu/Timestamp.js +27 -18
  63. package/dist/src/ManyToManyMap.d.ts +74 -10
  64. package/dist/src/ManyToManyMap.d.ts.map +1 -1
  65. package/dist/src/ManyToManyMap.js +41 -6
  66. package/dist/src/Number.d.ts +2 -1
  67. package/dist/src/Number.d.ts.map +1 -1
  68. package/dist/src/Random.d.ts +3 -2
  69. package/dist/src/Random.d.ts.map +1 -1
  70. package/dist/src/RefCountedResourceManager.d.ts +119 -0
  71. package/dist/src/RefCountedResourceManager.d.ts.map +1 -0
  72. package/dist/src/RefCountedResourceManager.js +197 -0
  73. package/dist/src/Result.d.ts +147 -51
  74. package/dist/src/Result.d.ts.map +1 -1
  75. package/dist/src/Result.js +7 -241
  76. package/dist/src/Sqlite.d.ts +21 -4
  77. package/dist/src/Sqlite.d.ts.map +1 -1
  78. package/dist/src/Sqlite.js +50 -8
  79. package/dist/src/Task.d.ts +511 -0
  80. package/dist/src/Task.d.ts.map +1 -0
  81. package/dist/src/Task.js +410 -0
  82. package/dist/src/Time.d.ts +59 -0
  83. package/dist/src/Time.d.ts.map +1 -1
  84. package/dist/src/Time.js +87 -4
  85. package/dist/src/Type.d.ts +462 -327
  86. package/dist/src/Type.d.ts.map +1 -1
  87. package/dist/src/Type.js +490 -436
  88. package/dist/src/Types.d.ts +0 -74
  89. package/dist/src/Types.d.ts.map +1 -1
  90. package/dist/src/WebSocket.d.ts +5 -2
  91. package/dist/src/WebSocket.d.ts.map +1 -1
  92. package/dist/src/WebSocket.js +12 -13
  93. package/dist/src/Worker.d.ts +39 -11
  94. package/dist/src/Worker.d.ts.map +1 -1
  95. package/dist/src/Worker.js +22 -4
  96. package/dist/src/index.d.ts +3 -3
  97. package/dist/src/index.d.ts.map +1 -1
  98. package/dist/src/index.js +3 -3
  99. package/package.json +14 -13
  100. package/src/Assert.ts +8 -7
  101. package/src/Brand.ts +75 -0
  102. package/src/Buffer.ts +1 -1
  103. package/src/CallbackRegistry.ts +84 -0
  104. package/src/Console.ts +91 -11
  105. package/src/Crypto.ts +94 -79
  106. package/src/Evolu/Db.ts +505 -1027
  107. package/src/Evolu/Diff.ts +7 -5
  108. package/src/Evolu/Evolu.ts +682 -329
  109. package/src/Evolu/Internal.ts +0 -2
  110. package/src/Evolu/LocalAuth.ts +422 -0
  111. package/src/Evolu/Owner.ts +221 -235
  112. package/src/Evolu/Platform.ts +9 -9
  113. package/src/Evolu/Protocol.ts +681 -614
  114. package/src/Evolu/Public.ts +7 -14
  115. package/src/Evolu/PublicKysely.ts +3 -3
  116. package/src/Evolu/Query.ts +2 -1
  117. package/src/Evolu/Relay.ts +62 -16
  118. package/src/Evolu/Schema.ts +391 -191
  119. package/src/Evolu/Storage.ts +275 -55
  120. package/src/Evolu/Sync.ts +758 -37
  121. package/src/Evolu/Timestamp.ts +38 -35
  122. package/src/ManyToManyMap.ts +127 -24
  123. package/src/Number.ts +2 -6
  124. package/src/Random.ts +3 -2
  125. package/src/RefCountedResourceManager.ts +368 -0
  126. package/src/Result.ts +152 -53
  127. package/src/Sqlite.ts +59 -23
  128. package/src/Task.ts +779 -0
  129. package/src/Time.ts +168 -4
  130. package/src/Type.ts +696 -660
  131. package/src/Types.ts +0 -76
  132. package/src/WebSocket.ts +23 -17
  133. package/src/Worker.ts +72 -23
  134. package/src/index.ts +3 -3
  135. package/dist/src/Callbacks.d.ts +0 -20
  136. package/dist/src/Callbacks.d.ts.map +0 -1
  137. package/dist/src/Callbacks.js +0 -18
  138. package/dist/src/Evolu/Config.d.ts +0 -69
  139. package/dist/src/Evolu/Config.d.ts.map +0 -1
  140. package/dist/src/Evolu/Config.js +0 -9
  141. package/dist/src/Evolu/Kysely.d.ts +0 -6
  142. package/dist/src/Evolu/Kysely.d.ts.map +0 -1
  143. package/dist/src/Evolu/Kysely.js +0 -21
  144. package/dist/src/NanoId.d.ts +0 -27
  145. package/dist/src/NanoId.d.ts.map +0 -1
  146. package/dist/src/NanoId.js +0 -6
  147. package/dist/src/Promise.d.ts +0 -180
  148. package/dist/src/Promise.d.ts.map +0 -1
  149. package/dist/src/Promise.js +0 -176
  150. package/src/Callbacks.ts +0 -43
  151. package/src/Evolu/Config.ts +0 -83
  152. package/src/Evolu/Kysely.ts +0 -38
  153. package/src/NanoId.ts +0 -39
  154. package/src/Promise.ts +0 -295
@@ -1,23 +1,77 @@
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, OwnerWriteKey, TransportConfig } from "./Owner.js";
12
+ import { ProtocolError, ProtocolInvalidDataError, ProtocolTimestampMismatchError } from "./Protocol.js";
13
+ import { MutationChange } from "./Schema.js";
14
+ import { CrdtMessage, SqliteStorageBase, Storage } from "./Storage.js";
15
+ import { Millis, Timestamp, TimestampBytes, 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<TransportConfig>;
44
+ }
45
+ export interface SyncConfig {
46
+ readonly appOwner: AppOwner;
47
+ readonly transports: ReadonlyArray<TransportConfig>;
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;
55
+ }
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;
59
+ }
60
+ export interface Clock {
61
+ readonly get: () => Timestamp;
62
+ readonly save: (timestamp: Timestamp) => Result<void, SqliteError>;
13
63
  }
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;
64
+ export declare const createClock: (deps: RandomBytesDep & SqliteDep) => (initialTimestamp?: Timestamp) => Clock;
65
+ export interface ClientStorage extends SqliteStorageBase, Storage {
18
66
  }
19
- export declare const createWebSocketSync: CreateSync;
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 applyMessageToTimestampAndHistoryTables: (deps: ClientStorageDep & SqliteDep) => (ownerId: OwnerIdBytes, message: CrdtMessage) => Result<void, SqliteError>;
20
72
  /**
73
+ * TODO: Rework for the new owners API.
74
+ *
21
75
  * The possible states of a synchronization process. The `SyncState` can be one
22
76
  * of the following:
23
77
  *
@@ -56,4 +110,14 @@ export interface PaymentRequiredError {
56
110
  readonly type: "PaymentRequiredError";
57
111
  }
58
112
  export declare const initialSyncState: SyncStateInitial;
113
+ /**
114
+ * Efficiently checks which binary timestamps already exist in the database
115
+ * using a single CTE query instead of N individual queries. Crucial for WASM
116
+ * SQLite performance where JS↔WASM boundary crossings are expensive.
117
+ *
118
+ * Used for fast idempotency detection in writeMessages before onMessage
119
+ * validation. While applyMessages ensures internal idempotency, this pre-check
120
+ * is faster and required for main thread message validation.
121
+ */
122
+ export declare const getExistingTimestamps: (deps: SqliteDep) => (ownerIdBytes: OwnerIdBytes, timestampsBytes: NonEmptyReadonlyArray<TimestampBytes>) => Result<ReadonlyArray<TimestampBytes>, SqliteError>;
59
123
  //# sourceMappingURL=Sync.d.ts.map
@@ -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,IAAI,CAAC;CACrB;AAED,MAAM,MAAM,UAAU,GAAG,CAAC,IAAI,EAAE,UAAU,KAAK,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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;AAE/E;;;;;;;;GAQG;AACH,eAAO,MAAM,qBAAqB,GAC/B,MAAM,SAAS,MAEd,cAAc,YAAY,EAC1B,iBAAiB,qBAAqB,CAAC,cAAc,CAAC,KACrD,MAAM,CAAC,aAAa,CAAC,cAAc,CAAC,EAAE,WAAW,CA4BnD,CAAC"}
@@ -1,29 +1,462 @@
1
- import { createWebSocket } from "../WebSocket.js";
2
- export const createWebSocketSync = (_deps) => (config) => {
1
+ import { assert } from "../Assert.js";
2
+ import { concatBytes } from "../Buffer.js";
3
+ import { eqArrayNumber } from "../Eq.js";
4
+ import { createTransferableError } from "../Error.js";
5
+ import { constFalse } from "../Function.js";
6
+ import { objectToEntries } from "../Object.js";
7
+ import { createRefCountedResourceManager } from "../RefCountedResourceManager.js";
8
+ import { ok } from "../Result.js";
9
+ import { sql } from "../Sqlite.js";
10
+ import { createMutex } from "../Task.js";
11
+ import { idBytesToId, idToIdBytes } from "../Type.js";
12
+ import { ownerIdBytesToOwnerId, ownerIdToOwnerIdBytes, } from "./Owner.js";
13
+ import { applyProtocolMessageAsClient, createProtocolMessageForSync, createProtocolMessageForUnsubscribe, createProtocolMessageFromCrdtMessages, decryptAndDecodeDbChange, encodeAndEncryptDbChange, SubscriptionFlags, } from "./Protocol.js";
14
+ import { createSqliteStorageBase, } from "./Storage.js";
15
+ import { createInitialTimestamp, receiveTimestamp, sendTimestamp, timestampBytesToTimestamp, timestampToTimestampBytes, timestampToTimestampString, } from "./Timestamp.js";
16
+ export const createSync = (deps) => (config) => {
17
+ let isDisposed = false;
18
+ /** Returns owner data only if actively assigned to at least one transport. */
19
+ const getSyncOwner = (ownerId) => {
20
+ if (isDisposed)
21
+ return null;
22
+ return transports.getConsumer(ownerId);
23
+ };
24
+ const storageResult = createClientStorage({
25
+ ...deps,
26
+ getSyncOwner,
27
+ })(config);
28
+ if (!storageResult.ok)
29
+ return storageResult;
30
+ const storage = storageResult.value;
31
+ const createResource = (transportConfig) => {
32
+ const transportKey = createTransportKey(transportConfig);
33
+ deps.console.log("[sync]", "createWebSocket", {
34
+ transportKey,
35
+ url: transportConfig.url,
36
+ });
37
+ return deps.createWebSocket(transportConfig.url, {
38
+ binaryType: "arraybuffer",
39
+ onOpen: () => {
40
+ if (isDisposed)
41
+ return;
42
+ const webSocket = transports.getResource(transportKey);
43
+ if (!webSocket)
44
+ return;
45
+ const ownerIds = transports.getConsumersForResource(transportKey);
46
+ deps.console.log("[sync]", "onOpen", { transportKey, ownerIds });
47
+ for (const ownerId of ownerIds) {
48
+ const message = createProtocolMessageForSync({ storage })(ownerId, SubscriptionFlags.Subscribe);
49
+ if (!message)
50
+ continue;
51
+ deps.console.log("[sync]", "send", { message });
52
+ webSocket.send(message);
53
+ }
54
+ },
55
+ onClose: (event) => {
56
+ deps.console.log("[sync]", "onClose", {
57
+ transportKey,
58
+ code: event.code,
59
+ reason: event.reason,
60
+ wasClean: event.wasClean,
61
+ });
62
+ },
63
+ onError: (error) => {
64
+ deps.console.warn("[sync]", "onError", { transportKey, error });
65
+ },
66
+ onMessage: (data) => {
67
+ // Only handle ArrayBuffer data for sync messages
68
+ if (isDisposed || !(data instanceof ArrayBuffer))
69
+ return;
70
+ const webSocket = transports.getResource(transportKey);
71
+ if (!webSocket)
72
+ return;
73
+ const input = new Uint8Array(data);
74
+ deps.console.log("[sync]", "onMessage", {
75
+ transportKey,
76
+ message: input,
77
+ });
78
+ applyProtocolMessageAsClient({ storage })(input, {
79
+ // No write key, no sync (for a case when an owner was unused).
80
+ getWriteKey: (ownerId) => getSyncOwner(ownerId)?.writeKey ?? null,
81
+ })
82
+ .then((message) => {
83
+ if (!message.ok) {
84
+ config.onError(message.error);
85
+ return;
86
+ }
87
+ switch (message.value.type) {
88
+ case "response":
89
+ webSocket.send(message.value.message);
90
+ break;
91
+ case "no-response":
92
+ // Sync complete, no response needed
93
+ break;
94
+ case "broadcast":
95
+ // This was a broadcast message, don't affect sync counter
96
+ break;
97
+ }
98
+ })
99
+ .catch((error) => {
100
+ config.onError(createTransferableError(error));
101
+ });
102
+ },
103
+ });
104
+ };
105
+ const transports = createRefCountedResourceManager({
106
+ createResource,
107
+ getResourceKey: createTransportKey,
108
+ getConsumerId: (owner) => owner.id,
109
+ disposalDelay: config.disposalDelayMs ?? 100,
110
+ onConsumerAdded: (owner, webSocket) => {
111
+ deps.console.log("[sync]", "onConsumerAdded", {
112
+ ownerId: owner.id,
113
+ isOpen: webSocket.isOpen(),
114
+ });
115
+ // The onOpen handler will sync it.
116
+ if (!webSocket.isOpen())
117
+ return;
118
+ const message = createProtocolMessageForSync({ storage })(owner.id, SubscriptionFlags.Subscribe);
119
+ if (message)
120
+ webSocket.send(message);
121
+ },
122
+ onConsumerRemoved: (owner, webSocket) => {
123
+ deps.console.log("[sync]", "onConsumerRemoved", {
124
+ ownerId: owner.id,
125
+ isOpen: webSocket.isOpen(),
126
+ });
127
+ const message = createProtocolMessageForUnsubscribe(owner.id);
128
+ webSocket.send(message);
129
+ },
130
+ });
3
131
  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")
132
+ useOwner: (use, owner) => {
133
+ if (isDisposed) {
134
+ deps.console.warn("[sync]", "useOwner called on disposed Sync instance", { owner });
11
135
  return;
12
- socket.send(message);
136
+ }
137
+ deps.console.log("[sync]", "useOwner", { use, owner });
138
+ const transportsToUse = owner.transports ?? config.transports;
139
+ if (use) {
140
+ transports.addConsumer(owner, transportsToUse);
141
+ }
142
+ else {
143
+ const result = transports.removeConsumer(owner, transportsToUse);
144
+ if (!result.ok) {
145
+ deps.console.warn("[sync]", "Failed to remove consumer", {
146
+ transportsToUse,
147
+ ownerId: owner.id,
148
+ error: result.error,
149
+ });
150
+ }
151
+ }
152
+ },
153
+ applyChanges: (changes) => {
154
+ deps.console.log("[sync]", "applyChanges", { changes });
155
+ let clockTimestamp = deps.clock.get();
156
+ const ownerMessages = new Map();
157
+ for (const change of changes) {
158
+ const nextTimestamp = sendTimestamp(deps)(clockTimestamp);
159
+ if (!nextTimestamp.ok)
160
+ return nextTimestamp;
161
+ clockTimestamp = nextTimestamp.value;
162
+ const { ownerId = config.appOwner.id, ...dbChange } = change;
163
+ const message = { timestamp: clockTimestamp, change: dbChange };
164
+ const messages = ownerMessages.get(ownerId);
165
+ if (messages)
166
+ messages.push(message);
167
+ else
168
+ ownerMessages.set(ownerId, [message]);
169
+ }
170
+ for (const [ownerId, messages] of ownerMessages) {
171
+ const result = applyMessages({ ...deps, storage })(ownerId, messages);
172
+ if (!result.ok)
173
+ return result;
174
+ const owner = getSyncOwner(ownerId);
175
+ if (!owner?.writeKey)
176
+ continue;
177
+ const message = createProtocolMessageFromCrdtMessages(deps)({
178
+ id: owner.id,
179
+ encryptionKey: owner.encryptionKey,
180
+ writeKey: owner.writeKey,
181
+ }, messages);
182
+ const transportsToUse = owner.transports ?? config.transports;
183
+ // Send message to all transports for this owner
184
+ for (const transportConfig of transportsToUse) {
185
+ const transportKey = createTransportKey(transportConfig);
186
+ const webSocket = transports.getResource(transportKey);
187
+ if (!webSocket)
188
+ continue;
189
+ if (webSocket.isOpen()) {
190
+ deps.console.log("[sync]", "send", { transportKey, message });
191
+ webSocket.send(message);
192
+ }
193
+ }
194
+ }
195
+ return deps.clock.save(clockTimestamp);
196
+ },
197
+ [Symbol.dispose]: () => {
198
+ if (isDisposed)
199
+ return;
200
+ isDisposed = true;
201
+ transports[Symbol.dispose]();
202
+ },
203
+ };
204
+ return ok(sync);
205
+ };
206
+ export const createClock = (deps) => (initialTimestamp = createInitialTimestamp(deps)) => {
207
+ let currentTimestamp = initialTimestamp;
208
+ return {
209
+ get: () => currentTimestamp,
210
+ save: (timestamp) => {
211
+ currentTimestamp = timestamp;
212
+ const timestampString = timestampToTimestampString(timestamp);
213
+ const result = deps.sqlite.exec(sql.prepared `
214
+ update evolu_config set "clock" = ${timestampString};
215
+ `);
216
+ if (!result.ok)
217
+ return result;
218
+ return ok();
13
219
  },
14
220
  };
15
- const socket = createWebSocket(config.syncUrl, {
16
- binaryType: "arraybuffer",
17
- onOpen: () => {
18
- config.onOpen(sync.send);
221
+ };
222
+ const createClientStorage = (deps) => (config) => {
223
+ const sqliteStorageBase = createSqliteStorageBase(deps)({
224
+ onStorageError: config.onError,
225
+ });
226
+ if (!sqliteStorageBase.ok)
227
+ return sqliteStorageBase;
228
+ const mutex = createMutex();
229
+ const storage = {
230
+ ...sqliteStorageBase.value,
231
+ validateWriteKey: constFalse,
232
+ setWriteKey: constFalse,
233
+ writeMessages: async (ownerIdBytes, encryptedMessages) => {
234
+ const writeResult = await mutex.withLock(async () => {
235
+ const ownerId = ownerIdBytesToOwnerId(ownerIdBytes);
236
+ const owner = deps.getSyncOwner(ownerId);
237
+ // Owner can be removed during syncing.
238
+ if (!owner)
239
+ return ok(false);
240
+ const messages = [];
241
+ for (const message of encryptedMessages) {
242
+ const change = decryptAndDecodeDbChange(deps)(message, owner.encryptionKey);
243
+ if (!change.ok)
244
+ return change;
245
+ messages.push({
246
+ timestamp: message.timestamp,
247
+ change: change.value,
248
+ });
249
+ }
250
+ const transaction = deps.sqlite.transaction(() => {
251
+ let clockTimestamp = deps.clock.get();
252
+ for (const message of messages) {
253
+ const nextTimestamp = receiveTimestamp(deps)(clockTimestamp, message.timestamp);
254
+ if (!nextTimestamp.ok)
255
+ return nextTimestamp;
256
+ clockTimestamp = nextTimestamp.value;
257
+ }
258
+ const applyMessagesResult = applyMessages({ ...deps, storage })(owner.id, messages);
259
+ if (!applyMessagesResult.ok)
260
+ return applyMessagesResult;
261
+ // // Apply local mutations atomically with approved messages
262
+ // for (const change of localMutations) {
263
+ // const result = applyLocalOnlyChange(deps)(change);
264
+ // if (!result.ok) return result;
265
+ // }
266
+ return deps.clock.save(clockTimestamp);
267
+ });
268
+ if (!transaction.ok)
269
+ return transaction;
270
+ return ok(true);
271
+ })();
272
+ if (!writeResult.ok) {
273
+ if (writeResult.error.type !== "AbortError") {
274
+ config.onError(writeResult.error);
275
+ }
276
+ return false;
277
+ }
278
+ config.onReceive();
279
+ return true;
19
280
  },
20
- onMessage: (data) => {
21
- if (data instanceof ArrayBuffer) {
22
- const messages = new Uint8Array(data);
23
- config.onMessage(messages, sync.send);
281
+ readDbChange: (ownerId, timestamp) => {
282
+ const owner = deps.getSyncOwner(ownerIdBytesToOwnerId(ownerId));
283
+ // Owner can be removed to stop syncing.
284
+ if (!owner)
285
+ return null;
286
+ const result = deps.sqlite.exec(sql `
287
+ select "table", "id", "column", "value"
288
+ from evolu_history
289
+ where "ownerId" = ${ownerId} and "timestamp" = ${timestamp};
290
+ `);
291
+ if (!result.ok) {
292
+ config.onError(result.error);
293
+ return null;
294
+ }
295
+ const { rows } = result.value;
296
+ assert(rows.length > 0, "Rows must not be empty");
297
+ const { table, id } = rows[0];
298
+ const values = {};
299
+ for (const r of rows) {
300
+ assert(r.table === table, "All rows must have the same table");
301
+ assert(eqArrayNumber(r.id, id), "All rows must have the same Id");
302
+ values[r.column] = r.value;
24
303
  }
304
+ const message = {
305
+ timestamp: timestampBytesToTimestamp(timestamp),
306
+ change: {
307
+ table: rows[0].table,
308
+ id: idBytesToId(rows[0].id),
309
+ values,
310
+ },
311
+ };
312
+ return encodeAndEncryptDbChange(deps)(message, owner.encryptionKey);
25
313
  },
26
- });
27
- return sync;
314
+ };
315
+ return ok(storage);
316
+ };
317
+ /** Creates a unique identifier for a transport configuration. */
318
+ const createTransportKey = (transportConfig) => {
319
+ return `${transportConfig.type}:${transportConfig.url}`;
320
+ };
321
+ export const applyLocalOnlyChange = (deps) => (change) => {
322
+ const dbChange = {
323
+ table: change.table,
324
+ id: change.id,
325
+ values: change.values,
326
+ };
327
+ const isDeletion = "isDeleted" in dbChange.values && dbChange.values.isDeleted === 1;
328
+ if (isDeletion) {
329
+ const result = deps.sqlite.exec(sql `
330
+ delete from ${sql.identifier(dbChange.table)}
331
+ where id = ${dbChange.id};
332
+ `);
333
+ if (!result.ok)
334
+ return result;
335
+ }
336
+ else {
337
+ const date = new Date(deps.time.now()).toISOString();
338
+ for (const [column, value] of objectToEntries(dbChange.values)) {
339
+ const result = deps.sqlite.exec(sql.prepared `
340
+ insert into ${sql.identifier(dbChange.table)}
341
+ ("id", ${sql.identifier(column)}, createdAt, updatedAt)
342
+ values (${dbChange.id}, ${value}, ${date}, ${date})
343
+ on conflict ("id") do update
344
+ set
345
+ ${sql.identifier(column)} = ${value},
346
+ updatedAt = ${date};
347
+ `);
348
+ if (!result.ok)
349
+ return result;
350
+ }
351
+ }
352
+ return ok();
353
+ };
354
+ const applyMessages = (deps) => (ownerId, messages) => {
355
+ const ownerIdBytes = ownerIdToOwnerIdBytes(ownerId);
356
+ for (const message of messages) {
357
+ const result1 = applyMessageToAppTable(deps)(ownerIdBytes, message);
358
+ if (!result1.ok)
359
+ return result1;
360
+ const result2 = applyMessageToTimestampAndHistoryTables(deps)(ownerIdBytes, message);
361
+ if (!result2.ok)
362
+ return result2;
363
+ }
364
+ return ok();
365
+ };
366
+ const applyMessageToAppTable = (deps) => (ownerId, message) => {
367
+ const timestamp = timestampToTimestampBytes(message.timestamp);
368
+ const updatedAt = new Date(message.timestamp.millis).toISOString();
369
+ for (const [column, value] of objectToEntries(message.change.values)) {
370
+ const result = deps.sqlite.exec(sql.prepared `
371
+ with
372
+ lastTimestamp as (
373
+ select "timestamp"
374
+ from evolu_history
375
+ where
376
+ "ownerId" = ${ownerId}
377
+ and "table" = ${message.change.table}
378
+ and "id" = ${message.change.id}
379
+ and "column" = ${column}
380
+ order by "timestamp" desc
381
+ limit 1
382
+ )
383
+ insert into ${sql.identifier(message.change.table)}
384
+ ("id", ${sql.identifier(column)}, updatedAt)
385
+ select ${message.change.id}, ${value}, ${updatedAt}
386
+ where
387
+ (select "timestamp" from lastTimestamp) is null
388
+ or (select "timestamp" from lastTimestamp) < ${timestamp}
389
+ on conflict ("id") do update
390
+ set
391
+ ${sql.identifier(column)} = ${value},
392
+ updatedAt = ${updatedAt}
393
+ where
394
+ (select "timestamp" from lastTimestamp) is null
395
+ or (select "timestamp" from lastTimestamp) < ${timestamp};
396
+ `);
397
+ if (!result.ok)
398
+ return result;
399
+ }
400
+ return ok();
401
+ };
402
+ export const applyMessageToTimestampAndHistoryTables = (deps) => (ownerId, message) => {
403
+ const timestamp = timestampToTimestampBytes(message.timestamp);
404
+ const id = idToIdBytes(message.change.id);
405
+ const result = deps.storage.insertTimestamp(ownerId, timestamp);
406
+ if (!result.ok)
407
+ return result;
408
+ for (const [column, value] of Object.entries(message.change.values)) {
409
+ const result = deps.sqlite.exec(sql.prepared `
410
+ insert into evolu_history
411
+ ("ownerId", "table", "id", "column", "value", "timestamp")
412
+ values
413
+ (
414
+ ${ownerId},
415
+ ${message.change.table},
416
+ ${id},
417
+ ${column},
418
+ ${value},
419
+ ${timestamp}
420
+ )
421
+ on conflict do nothing;
422
+ `);
423
+ if (!result.ok)
424
+ return result;
425
+ }
426
+ return ok();
28
427
  };
29
428
  export const initialSyncState = { type: "SyncStateInitial" };
429
+ /**
430
+ * Efficiently checks which binary timestamps already exist in the database
431
+ * using a single CTE query instead of N individual queries. Crucial for WASM
432
+ * SQLite performance where JS↔WASM boundary crossings are expensive.
433
+ *
434
+ * Used for fast idempotency detection in writeMessages before onMessage
435
+ * validation. While applyMessages ensures internal idempotency, this pre-check
436
+ * is faster and required for main thread message validation.
437
+ */
438
+ export const getExistingTimestamps = (deps) => (ownerIdBytes, timestampsBytes) => {
439
+ const concatenatedTimestamps = concatBytes(...timestampsBytes);
440
+ const result = deps.sqlite.exec(sql `
441
+ with recursive
442
+ split_timestamps(timestampBytes, pos) as (
443
+ select
444
+ substr(${concatenatedTimestamps}, 1, 16),
445
+ 17 as pos
446
+ union all
447
+ select
448
+ substr(${concatenatedTimestamps}, pos, 16),
449
+ pos + 16
450
+ from split_timestamps
451
+ where pos <= length(${concatenatedTimestamps})
452
+ )
453
+ select s.timestampBytes
454
+ from
455
+ split_timestamps s
456
+ join evolu_timestamp t
457
+ on t.ownerId = ${ownerIdBytes} and s.timestampBytes = t.t;
458
+ `);
459
+ if (!result.ok)
460
+ return result;
461
+ return ok(result.value.rows.map((row) => row.timestampBytes));
462
+ };