@evolu/common 5.4.7 → 6.0.0

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 (242) hide show
  1. package/README.md +30 -34
  2. package/dist/src/Array.d.ts +17 -0
  3. package/dist/src/Array.d.ts.map +1 -0
  4. package/dist/src/Array.js +12 -0
  5. package/dist/src/Assert.d.ts +68 -0
  6. package/dist/src/Assert.d.ts.map +1 -0
  7. package/dist/src/Assert.js +77 -0
  8. package/dist/src/BigInt.d.ts +20 -0
  9. package/dist/src/BigInt.d.ts.map +1 -0
  10. package/dist/src/BigInt.js +18 -0
  11. package/dist/src/Buffer.d.ts +92 -0
  12. package/dist/src/Buffer.d.ts.map +1 -0
  13. package/dist/src/Buffer.js +62 -0
  14. package/dist/src/Callbacks.d.ts +20 -0
  15. package/dist/src/Callbacks.d.ts.map +1 -0
  16. package/dist/src/Callbacks.js +18 -0
  17. package/dist/src/Console.d.ts +78 -0
  18. package/dist/src/Console.d.ts.map +1 -0
  19. package/dist/src/Console.js +103 -0
  20. package/dist/src/Crypto.d.ts +72 -39
  21. package/dist/src/Crypto.d.ts.map +1 -1
  22. package/dist/src/Crypto.js +89 -54
  23. package/dist/src/Eq.d.ts +97 -0
  24. package/dist/src/Eq.d.ts.map +1 -0
  25. package/dist/src/Eq.js +167 -0
  26. package/dist/src/Error.d.ts +14 -10
  27. package/dist/src/Error.d.ts.map +1 -1
  28. package/dist/src/Error.js +43 -11
  29. package/dist/src/Evolu/Config.d.ts +69 -0
  30. package/dist/src/Evolu/Config.d.ts.map +1 -0
  31. package/dist/src/Evolu/Config.js +9 -0
  32. package/dist/src/Evolu/Db.d.ts +126 -0
  33. package/dist/src/Evolu/Db.d.ts.map +1 -0
  34. package/dist/src/Evolu/Db.js +774 -0
  35. package/dist/src/Evolu/Diff.d.ts +43 -0
  36. package/dist/src/Evolu/Diff.d.ts.map +1 -0
  37. package/dist/src/Evolu/Diff.js +95 -0
  38. package/dist/src/Evolu/Evolu.d.ts +334 -0
  39. package/dist/src/Evolu/Evolu.d.ts.map +1 -0
  40. package/dist/src/Evolu/Evolu.js +434 -0
  41. package/dist/src/Evolu/Internal.d.ts +26 -0
  42. package/dist/src/Evolu/Internal.d.ts.map +1 -0
  43. package/dist/src/Evolu/Internal.js +25 -0
  44. package/dist/src/Evolu/Kysely.d.ts +6 -0
  45. package/dist/src/Evolu/Kysely.d.ts.map +1 -0
  46. package/dist/src/Evolu/Kysely.js +21 -0
  47. package/dist/src/Evolu/Owner.d.ts +155 -0
  48. package/dist/src/Evolu/Owner.d.ts.map +1 -0
  49. package/dist/src/Evolu/Owner.js +126 -0
  50. package/dist/src/Evolu/Platform.d.ts +23 -0
  51. package/dist/src/Evolu/Platform.d.ts.map +1 -0
  52. package/dist/src/Evolu/Platform.js +1 -0
  53. package/dist/src/Evolu/Protocol.d.ts +401 -0
  54. package/dist/src/Evolu/Protocol.d.ts.map +1 -0
  55. package/dist/src/Evolu/Protocol.js +1151 -0
  56. package/dist/src/Evolu/Public.d.ts +18 -0
  57. package/dist/src/Evolu/Public.d.ts.map +1 -0
  58. package/dist/src/Evolu/Public.js +11 -0
  59. package/dist/src/Evolu/PublicKysely.d.ts +148 -0
  60. package/dist/src/Evolu/PublicKysely.d.ts.map +1 -0
  61. package/dist/src/Evolu/PublicKysely.js +185 -0
  62. package/dist/src/Evolu/Query.d.ts +63 -0
  63. package/dist/src/Evolu/Query.d.ts.map +1 -0
  64. package/dist/src/Evolu/Query.js +61 -0
  65. package/dist/src/Evolu/Relay.d.ts +13 -0
  66. package/dist/src/Evolu/Relay.d.ts.map +1 -0
  67. package/dist/src/Evolu/Relay.js +109 -0
  68. package/dist/src/Evolu/Schema.d.ts +201 -0
  69. package/dist/src/Evolu/Schema.d.ts.map +1 -0
  70. package/dist/src/Evolu/Schema.js +150 -0
  71. package/dist/src/Evolu/Storage.d.ts +49 -0
  72. package/dist/src/Evolu/Storage.d.ts.map +1 -0
  73. package/dist/src/Evolu/Storage.js +1111 -0
  74. package/dist/src/Evolu/Sync.d.ts +59 -0
  75. package/dist/src/Evolu/Sync.d.ts.map +1 -0
  76. package/dist/src/Evolu/Sync.js +29 -0
  77. package/dist/src/Evolu/Timestamp.d.ts +106 -0
  78. package/dist/src/Evolu/Timestamp.d.ts.map +1 -0
  79. package/dist/src/Evolu/Timestamp.js +179 -0
  80. package/dist/src/Function.d.ts +54 -0
  81. package/dist/src/Function.d.ts.map +1 -0
  82. package/dist/src/Function.js +38 -0
  83. package/dist/src/ManyToManyMap.d.ts +26 -0
  84. package/dist/src/ManyToManyMap.d.ts.map +1 -0
  85. package/dist/src/ManyToManyMap.js +92 -0
  86. package/dist/src/NanoId.d.ts +27 -0
  87. package/dist/src/NanoId.d.ts.map +1 -0
  88. package/dist/src/NanoId.js +6 -0
  89. package/dist/src/Number.d.ts +42 -0
  90. package/dist/src/Number.d.ts.map +1 -0
  91. package/dist/src/Number.js +55 -0
  92. package/dist/src/Object.d.ts +35 -0
  93. package/dist/src/Object.d.ts.map +1 -0
  94. package/dist/src/Object.js +36 -0
  95. package/dist/src/Order.d.ts +90 -0
  96. package/dist/src/Order.d.ts.map +1 -0
  97. package/dist/src/Order.js +85 -0
  98. package/dist/src/Promise.d.ts +180 -0
  99. package/dist/src/Promise.d.ts.map +1 -0
  100. package/dist/src/Promise.js +176 -0
  101. package/dist/src/Random.d.ts +52 -0
  102. package/dist/src/Random.d.ts.map +1 -0
  103. package/dist/src/Random.js +29 -0
  104. package/dist/src/Ref.d.ts +40 -0
  105. package/dist/src/Ref.d.ts.map +1 -0
  106. package/dist/src/Ref.js +13 -0
  107. package/dist/src/Result.d.ts +421 -0
  108. package/dist/src/Result.d.ts.map +1 -0
  109. package/dist/src/Result.js +357 -0
  110. package/dist/src/Skiplist.d.ts +23 -0
  111. package/dist/src/Skiplist.d.ts.map +1 -0
  112. package/dist/src/Skiplist.js +58 -0
  113. package/dist/src/Sqlite.d.ts +116 -52
  114. package/dist/src/Sqlite.d.ts.map +1 -1
  115. package/dist/src/Sqlite.js +183 -67
  116. package/dist/src/Store.d.ts +45 -8
  117. package/dist/src/Store.d.ts.map +1 -1
  118. package/dist/src/Store.js +33 -17
  119. package/dist/src/String.d.ts +2 -0
  120. package/dist/src/String.d.ts.map +1 -0
  121. package/dist/src/String.js +14 -0
  122. package/dist/src/Time.d.ts +20 -0
  123. package/dist/src/Time.d.ts.map +1 -0
  124. package/dist/src/Time.js +25 -0
  125. package/dist/src/Type.d.ts +1937 -0
  126. package/dist/src/Type.d.ts.map +1 -0
  127. package/dist/src/Type.js +2002 -0
  128. package/dist/src/Types.d.ts +188 -0
  129. package/dist/src/Types.d.ts.map +1 -0
  130. package/dist/src/Types.js +6 -0
  131. package/dist/src/WebSocket.d.ts +112 -0
  132. package/dist/src/WebSocket.d.ts.map +1 -0
  133. package/dist/src/WebSocket.js +139 -0
  134. package/dist/src/Worker.d.ts +44 -0
  135. package/dist/src/Worker.d.ts.map +1 -0
  136. package/dist/src/Worker.js +66 -0
  137. package/dist/src/index.d.ts +24 -11
  138. package/dist/src/index.d.ts.map +1 -1
  139. package/dist/src/index.js +24 -11
  140. package/package.json +29 -38
  141. package/src/Array.ts +39 -0
  142. package/src/Assert.ts +116 -0
  143. package/src/BigInt.ts +29 -0
  144. package/src/Buffer.ts +175 -0
  145. package/src/Callbacks.ts +43 -0
  146. package/src/Console.ts +159 -0
  147. package/src/Crypto.ts +169 -115
  148. package/src/Eq.ts +204 -0
  149. package/src/Error.ts +57 -20
  150. package/src/Evolu/Config.ts +83 -0
  151. package/src/Evolu/Db.ts +1275 -0
  152. package/src/Evolu/Diff.ts +142 -0
  153. package/src/Evolu/Evolu.ts +947 -0
  154. package/src/Evolu/Internal.ts +26 -0
  155. package/src/Evolu/Kysely.ts +38 -0
  156. package/src/Evolu/Owner.ts +296 -0
  157. package/src/Evolu/Platform.ts +27 -0
  158. package/src/Evolu/Protocol.ts +1857 -0
  159. package/src/Evolu/Public.ts +43 -0
  160. package/src/Evolu/PublicKysely.ts +240 -0
  161. package/src/Evolu/Query.ts +167 -0
  162. package/src/Evolu/Relay.ts +142 -0
  163. package/src/Evolu/Schema.ts +417 -0
  164. package/src/Evolu/Storage.ts +1281 -0
  165. package/src/Evolu/Sync.ts +105 -0
  166. package/src/Evolu/Timestamp.ts +311 -0
  167. package/src/Function.ts +58 -0
  168. package/src/ManyToManyMap.ts +140 -0
  169. package/src/NanoId.ts +39 -0
  170. package/src/Number.ts +90 -0
  171. package/src/Object.ts +64 -0
  172. package/src/Order.ts +113 -0
  173. package/src/Promise.ts +295 -0
  174. package/src/Random.ts +68 -0
  175. package/src/Ref.ts +63 -0
  176. package/src/Result.ts +453 -0
  177. package/src/Skiplist.ts +102 -0
  178. package/src/Sqlite.ts +366 -153
  179. package/src/Store.ts +79 -36
  180. package/src/String.ts +10 -0
  181. package/src/Time.ts +36 -0
  182. package/src/Type.ts +3978 -0
  183. package/src/Types.ts +209 -0
  184. package/src/WebSocket.ts +273 -0
  185. package/src/Worker.ts +129 -0
  186. package/src/index.ts +24 -11
  187. package/dist/src/Config.d.ts +0 -56
  188. package/dist/src/Config.d.ts.map +0 -1
  189. package/dist/src/Config.js +0 -39
  190. package/dist/src/Crdt.d.ts +0 -89
  191. package/dist/src/Crdt.d.ts.map +0 -1
  192. package/dist/src/Crdt.js +0 -181
  193. package/dist/src/Db.d.ts +0 -107
  194. package/dist/src/Db.d.ts.map +0 -1
  195. package/dist/src/Db.js +0 -443
  196. package/dist/src/Diff.d.ts +0 -27
  197. package/dist/src/Diff.d.ts.map +0 -1
  198. package/dist/src/Diff.js +0 -84
  199. package/dist/src/Evolu.d.ts +0 -426
  200. package/dist/src/Evolu.d.ts.map +0 -1
  201. package/dist/src/Evolu.js +0 -333
  202. package/dist/src/Model.d.ts +0 -141
  203. package/dist/src/Model.d.ts.map +0 -1
  204. package/dist/src/Model.js +0 -125
  205. package/dist/src/Murmurhash.d.ts +0 -2
  206. package/dist/src/Murmurhash.d.ts.map +0 -1
  207. package/dist/src/Murmurhash.js +0 -60
  208. package/dist/src/Owner.d.ts +0 -33
  209. package/dist/src/Owner.d.ts.map +0 -1
  210. package/dist/src/Owner.js +0 -26
  211. package/dist/src/Platform.d.ts +0 -37
  212. package/dist/src/Platform.d.ts.map +0 -1
  213. package/dist/src/Platform.js +0 -11
  214. package/dist/src/Protobuf.d.ts +0 -81
  215. package/dist/src/Protobuf.d.ts.map +0 -1
  216. package/dist/src/Protobuf.js +0 -92
  217. package/dist/src/Public.d.ts +0 -13
  218. package/dist/src/Public.d.ts.map +0 -1
  219. package/dist/src/Public.js +0 -6
  220. package/dist/src/Socket.d.ts +0 -8
  221. package/dist/src/Socket.d.ts.map +0 -1
  222. package/dist/src/Socket.js +0 -51
  223. package/dist/src/Sql.d.ts +0 -12
  224. package/dist/src/Sql.d.ts.map +0 -1
  225. package/dist/src/Sql.js +0 -30
  226. package/dist/src/Sync.d.ts +0 -70
  227. package/dist/src/Sync.d.ts.map +0 -1
  228. package/dist/src/Sync.js +0 -127
  229. package/src/Config.ts +0 -119
  230. package/src/Crdt.ts +0 -361
  231. package/src/Db.ts +0 -955
  232. package/src/Diff.ts +0 -114
  233. package/src/Evolu.ts +0 -1016
  234. package/src/Model.ts +0 -233
  235. package/src/Murmurhash.ts +0 -70
  236. package/src/Owner.ts +0 -69
  237. package/src/Platform.ts +0 -47
  238. package/src/Protobuf.ts +0 -155
  239. package/src/Public.ts +0 -12
  240. package/src/Socket.ts +0 -83
  241. package/src/Sql.ts +0 -41
  242. package/src/Sync.ts +0 -315
@@ -0,0 +1,774 @@
1
+ import { isNonEmptyArray, isNonEmptyReadonlyArray, } from "../Array.js";
2
+ import { assert, assertNonEmptyReadonlyArray } from "../Assert.js";
3
+ import { createSymmetricCrypto, } from "../Crypto.js";
4
+ import { eqArrayNumber } from "../Eq.js";
5
+ import { constFalse, exhaustiveCheck } from "../Function.js";
6
+ import { objectToEntries } from "../Object.js";
7
+ import { createRef } from "../Ref.js";
8
+ import { err, ok } from "../Result.js";
9
+ import { createSqlite, explainSqliteQueryPlan, sql, } from "../Sqlite.js";
10
+ import { object, String } from "../Type.js";
11
+ import { createInitializedWorker, } from "../Worker.js";
12
+ import { makePatches } from "./Diff.js";
13
+ import { createAppOwner, createOwnerRow, } from "./Owner.js";
14
+ import { applyProtocolMessageAsClient, binaryIdToId, createProtocolMessageForSync, createProtocolMessageFromCrdtMessages, decryptAndDecodeDbChange, encodeAndEncryptDbChange, idToBinaryId, ownerIdToBinaryOwnerId, protocolVersion, } from "./Protocol.js";
15
+ import { createQueryRowsCache, deserializeQuery, emptyRows, } from "./Query.js";
16
+ import { createSqliteStorageBase, } from "./Storage.js";
17
+ import { Millis, receiveTimestamp, sendTimestamp, timestampStringToTimestamp, timestampToBinaryTimestamp, timestampToTimestampString, } from "./Timestamp.js";
18
+ export const DbIndex = object({ name: String, sql: String });
19
+ export const createDbWorkerForPlatform = (platformDeps) => {
20
+ const tabQueryRowsCacheMap = new Map();
21
+ const getQueryRowsCache = (tabId) => {
22
+ let cache = tabQueryRowsCacheMap.get(tabId);
23
+ if (!cache) {
24
+ cache = createQueryRowsCache();
25
+ tabQueryRowsCacheMap.set(tabId, cache);
26
+ }
27
+ return cache;
28
+ };
29
+ return createInitializedWorker({
30
+ init: async (initMessage, postMessage, safeHandler) => {
31
+ platformDeps.console.enabled = initMessage.config.enableLogging ?? false;
32
+ const sqliteResult = await createSqlite(platformDeps)(initMessage.config.name);
33
+ if (!sqliteResult.ok) {
34
+ postMessage({ type: "onError", error: sqliteResult.error });
35
+ return null;
36
+ }
37
+ const sqlite = sqliteResult.value;
38
+ const deps = sqlite.transaction(() => {
39
+ const currentDbSchema = getDbSchema({ sqlite })();
40
+ if (!currentDbSchema.ok)
41
+ return currentDbSchema;
42
+ const ensureDbSchemaResult = ensureDbSchema({ sqlite })(initMessage.dbSchema, currentDbSchema.value);
43
+ if (!ensureDbSchemaResult.ok)
44
+ return ensureDbSchemaResult;
45
+ const maybeMigrateToVersion0Result = maybeMigrateToVersion0({ sqlite })(currentDbSchema.value);
46
+ if (!maybeMigrateToVersion0Result.ok)
47
+ return maybeMigrateToVersion0Result;
48
+ const ownerExists = currentDbSchema.value.tables.some((table) => table.name === "evolu_owner");
49
+ const appOwnerAndOwnerRow = ownerExists
50
+ ? selectAppOwner({ sqlite })
51
+ : initializeDb({ ...platformDeps, sqlite })(maybeMigrateToVersion0Result.value?.mnemonic ??
52
+ initMessage.config.mnemonic);
53
+ if (!appOwnerAndOwnerRow.ok)
54
+ return appOwnerAndOwnerRow;
55
+ const [appOwner, ownerRow] = appOwnerAndOwnerRow.value;
56
+ const depsWithoutSyncAndStorage = {
57
+ ...platformDeps,
58
+ postMessage,
59
+ sqlite,
60
+ timestampConfig: initMessage.config,
61
+ symmetricCrypto: createSymmetricCrypto(platformDeps),
62
+ getQueryRowsCache,
63
+ ownerRowRef: createRef(ownerRow),
64
+ };
65
+ const storage = createClientStorage(depsWithoutSyncAndStorage)({
66
+ onStorageError: (error) => {
67
+ postMessage({ type: "onError", error });
68
+ },
69
+ });
70
+ if (!storage.ok)
71
+ return storage;
72
+ const depsWithoutSync = {
73
+ ...depsWithoutSyncAndStorage,
74
+ storage: storage.value,
75
+ };
76
+ if (maybeMigrateToVersion0Result.value) {
77
+ const result = applyMessages(depsWithoutSync)(maybeMigrateToVersion0Result.value.messages, maybeMigrateToVersion0Result.value.lastTimestamp);
78
+ if (!result.ok)
79
+ return result;
80
+ }
81
+ if (!ownerExists && isNonEmptyReadonlyArray(initMessage.initialData)) {
82
+ const result = applyChanges(depsWithoutSync)(initMessage.initialData);
83
+ if (!result.ok)
84
+ return result;
85
+ }
86
+ postMessage({ type: "onInit", owner: appOwner });
87
+ const sync = platformDeps.createSync(platformDeps)({
88
+ ...initMessage.config,
89
+ onOpen: safeHandler(handleSyncOpen(depsWithoutSync)),
90
+ onMessage: safeHandler(createHandleSyncMessage(depsWithoutSync)),
91
+ });
92
+ return ok({ ...depsWithoutSync, sync });
93
+ });
94
+ if (!deps.ok) {
95
+ postMessage({ type: "onError", error: deps.error });
96
+ return null;
97
+ }
98
+ return deps.value;
99
+ },
100
+ onMessage: (deps) => (message) => {
101
+ switch (message.type) {
102
+ case "mutate": {
103
+ const mutate = deps.sqlite.transaction(() => {
104
+ const toSyncChanges = [];
105
+ const localOnlyChanges = [];
106
+ for (const change of message.changes) {
107
+ // Table name starting with '_' is local-only (not synced).
108
+ if (change.table.startsWith("_"))
109
+ localOnlyChanges.push(change);
110
+ else
111
+ toSyncChanges.push(change);
112
+ }
113
+ for (const change of localOnlyChanges) {
114
+ if ("isDeleted" in change.values &&
115
+ change.values.isDeleted === 1) {
116
+ const result = deps.sqlite.exec(sql `
117
+ delete from ${sql.identifier(change.table)}
118
+ where id = ${change.id};
119
+ `);
120
+ if (!result.ok)
121
+ return result;
122
+ }
123
+ else {
124
+ const millis = Millis.from(deps.time.now());
125
+ if (!millis.ok) {
126
+ return err({
127
+ type: "TimestampTimeOutOfRangeError",
128
+ });
129
+ }
130
+ const date = new Date(millis.value).toISOString();
131
+ for (const [column, value] of objectToEntries(change.values)) {
132
+ const result = deps.sqlite.exec(sql.prepared `
133
+ insert into ${sql.identifier(change.table)}
134
+ ("id", ${sql.identifier(column)}, createdAt, updatedAt)
135
+ values (${change.id}, ${value}, ${date}, ${date})
136
+ on conflict ("id") do update
137
+ set
138
+ ${sql.identifier(column)} = ${value},
139
+ updatedAt = ${date};
140
+ `);
141
+ if (!result.ok)
142
+ return result;
143
+ }
144
+ }
145
+ }
146
+ /**
147
+ * We don't have to wait for the transaction to end because changes
148
+ * are idempotent, so there is no reason why they should fail. We
149
+ * want to call onChange ASAP.
150
+ */
151
+ const onChange = () => {
152
+ deps.console.log("[db]", "onChange", {
153
+ subscribedQueries: message.subscribedQueries,
154
+ });
155
+ const patches = loadQueries(deps)(message.tabId, message.subscribedQueries);
156
+ if (!patches.ok) {
157
+ deps.postMessage({ type: "onError", error: patches.error });
158
+ return;
159
+ }
160
+ // Notify the tab that performed the mutation.
161
+ deps.postMessage({
162
+ type: "onChange",
163
+ tabId: message.tabId,
164
+ patches: patches.value,
165
+ onCompleteIds: message.onCompleteIds,
166
+ });
167
+ // Notify other tabs to refresh their queries.
168
+ deps.postMessage({ type: "onReceive", tabId: message.tabId });
169
+ };
170
+ if (!isNonEmptyArray(toSyncChanges)) {
171
+ onChange();
172
+ return ok();
173
+ }
174
+ const messages = applyChanges(deps)(toSyncChanges, onChange);
175
+ if (!messages.ok)
176
+ return messages;
177
+ const owner = deps.ownerRowRef.get();
178
+ // TODO: Check owner whether it's allowed to write, return an
179
+ // error if not.
180
+ if (owner.writeKey == null) {
181
+ return ok();
182
+ }
183
+ const protocolMessage = createProtocolMessageFromCrdtMessages(deps)(owner, messages.value);
184
+ deps.console.log("[db]", "send data message", messages.value, protocolMessage);
185
+ deps.sync.send(protocolMessage);
186
+ return ok();
187
+ });
188
+ if (!mutate.ok) {
189
+ deps.postMessage({ type: "onError", error: mutate.error });
190
+ return;
191
+ }
192
+ break;
193
+ }
194
+ case "query": {
195
+ const patches = loadQueries(deps)(message.tabId, message.queries);
196
+ if (!patches.ok) {
197
+ deps.postMessage({ type: "onError", error: patches.error });
198
+ return;
199
+ }
200
+ deps.postMessage({
201
+ type: "onChange",
202
+ tabId: message.tabId,
203
+ patches: patches.value,
204
+ onCompleteIds: [],
205
+ });
206
+ break;
207
+ }
208
+ case "reset": {
209
+ const resetResult = deps.sqlite.transaction(() => {
210
+ const dropAllTablesResult = dropAllTables(deps);
211
+ if (!dropAllTablesResult.ok)
212
+ return dropAllTablesResult;
213
+ if (message.restore) {
214
+ const dbSchema = getDbSchema(deps)();
215
+ if (!dbSchema.ok)
216
+ return dbSchema;
217
+ const ensureDbSchemaResult = ensureDbSchema(deps)(message.restore.dbSchema, dbSchema.value);
218
+ if (!ensureDbSchemaResult.ok)
219
+ return ensureDbSchemaResult;
220
+ const initializeDbResult = initializeDb(deps)(message.restore.mnemonic);
221
+ if (!initializeDbResult.ok)
222
+ return initializeDbResult;
223
+ }
224
+ return ok();
225
+ });
226
+ if (!resetResult.ok) {
227
+ deps.postMessage({ type: "onError", error: resetResult.error });
228
+ return;
229
+ }
230
+ deps.postMessage({
231
+ type: "onReset",
232
+ onCompleteId: message.onCompleteId,
233
+ reload: message.reload,
234
+ });
235
+ deps.sqlite[Symbol.dispose]();
236
+ break;
237
+ }
238
+ case "ensureDbSchema": {
239
+ const ensureSchema = deps.sqlite.transaction(() => {
240
+ const dbSchema = getDbSchema(deps)();
241
+ if (!dbSchema.ok)
242
+ return dbSchema;
243
+ const ensureDbSchemaResult = ensureDbSchema(deps)(message.dbSchema, dbSchema.value);
244
+ if (!ensureDbSchemaResult.ok)
245
+ return ensureDbSchemaResult;
246
+ return ok();
247
+ });
248
+ if (!ensureSchema.ok) {
249
+ deps.postMessage({ type: "onError", error: ensureSchema.error });
250
+ return;
251
+ }
252
+ break;
253
+ }
254
+ case "export": {
255
+ const file = deps.sqlite.export();
256
+ if (!file.ok) {
257
+ deps.postMessage({ type: "onError", error: file.error });
258
+ return;
259
+ }
260
+ deps.postMessage({
261
+ type: "onExport",
262
+ onCompleteId: message.onCompleteId,
263
+ file: file.value,
264
+ });
265
+ break;
266
+ }
267
+ default:
268
+ exhaustiveCheck(message);
269
+ }
270
+ },
271
+ });
272
+ };
273
+ /**
274
+ * Get the current database schema by reading SQLite metadata.
275
+ *
276
+ * TODO: Refactor out Evolu stuff and move it to Sqlite.
277
+ */
278
+ export const getDbSchema = (deps) => ({ allIndexes = false } = {}) => {
279
+ const map = new Map();
280
+ const tableAndColumnInfoRows = deps.sqlite.exec(sql `
281
+ select
282
+ sqlite_master.name as tableName,
283
+ table_info.name as columnName
284
+ from
285
+ sqlite_master
286
+ join pragma_table_info(sqlite_master.name) as table_info;
287
+ `);
288
+ if (!tableAndColumnInfoRows.ok)
289
+ return tableAndColumnInfoRows;
290
+ tableAndColumnInfoRows.value.rows.forEach((row) => {
291
+ const { tableName, columnName } = row;
292
+ if (!map.has(tableName))
293
+ map.set(tableName, []);
294
+ map.get(tableName)?.push(columnName);
295
+ });
296
+ const tables = Array.from(map, ([name, columns]) => ({ name, columns }));
297
+ const indexesRows = deps.sqlite.exec(allIndexes
298
+ ? sql `
299
+ select name, sql
300
+ from sqlite_master
301
+ where type = 'index' and name not like 'sqlite_%';
302
+ `
303
+ : sql `
304
+ select name, sql
305
+ from sqlite_master
306
+ where
307
+ type = 'index'
308
+ and name not like 'sqlite_%'
309
+ and name not like 'evolu_%';
310
+ `);
311
+ if (!indexesRows.ok)
312
+ return indexesRows;
313
+ const indexes = indexesRows.value.rows.map((row) => ({
314
+ name: row.name,
315
+ /**
316
+ * SQLite returns "CREATE INDEX" for "create index" for some reason.
317
+ * Other keywords remain unchanged. We have to normalize the casing for
318
+ * {@link indexesAreEqual} manually.
319
+ */
320
+ sql: row.sql
321
+ .replace("CREATE INDEX", "create index")
322
+ .replace("CREATE UNIQUE INDEX", "create unique index"),
323
+ }));
324
+ return ok({ tables, indexes });
325
+ };
326
+ const indexesAreEqual = (self, that) => self.name === that.name && self.sql === that.sql;
327
+ // TODO: Move it to Sqlite.
328
+ export const getDbSnapshot = (deps) => {
329
+ const schema = getDbSchema(deps)({ allIndexes: true });
330
+ if (!schema.ok)
331
+ return schema;
332
+ const rows = [];
333
+ for (const table of schema.value.tables) {
334
+ const result = deps.sqlite.exec(sql `
335
+ select * from ${sql.identifier(table.name)};
336
+ `);
337
+ if (!result.ok)
338
+ return result;
339
+ rows.push({
340
+ rows: result.value.rows,
341
+ name: table.name,
342
+ });
343
+ }
344
+ return ok({ schema: schema.value, rows });
345
+ };
346
+ const ensureDbSchema = (deps) => (newSchema, currentSchema, options) => {
347
+ const queries = [];
348
+ newSchema.tables.forEach((newTable) => {
349
+ const currentTable = currentSchema.tables.find((t) => t.name === newTable.name);
350
+ if (!currentTable) {
351
+ queries.push({
352
+ sql: createAppTable(newTable.name, newTable.columns),
353
+ parameters: [],
354
+ });
355
+ }
356
+ else {
357
+ newTable.columns
358
+ .filter((newColumn) => !currentTable.columns.includes(newColumn))
359
+ .forEach((newColumn) => {
360
+ queries.push(sql `
361
+ alter table ${sql.identifier(newTable.name)}
362
+ add column ${sql.identifier(newColumn)} blob;
363
+ `);
364
+ });
365
+ }
366
+ });
367
+ if (options?.ignoreIndexes !== true) {
368
+ // Remove current indexes that are not in the newSchema.
369
+ currentSchema.indexes
370
+ .filter((currentIndex) => !newSchema.indexes.some((newIndex) => indexesAreEqual(newIndex, currentIndex)))
371
+ .forEach((index) => {
372
+ queries.push(sql `drop index ${sql.identifier(index.name)};`);
373
+ });
374
+ // Add new indexes that are not in the currentSchema.
375
+ newSchema.indexes
376
+ .filter((newIndex) => !currentSchema.indexes.some((currentIndex) => indexesAreEqual(newIndex, currentIndex)))
377
+ .forEach((newIndex) => {
378
+ queries.push({ sql: `${newIndex.sql};`, parameters: [] });
379
+ });
380
+ }
381
+ for (const query of queries) {
382
+ const result = deps.sqlite.exec(query);
383
+ if (!result.ok)
384
+ return result;
385
+ }
386
+ return ok();
387
+ };
388
+ export const createAppTable = (tableName, columns) => `
389
+ create table ${sql.identifier(tableName).sql} (
390
+ "id" text primary key,
391
+ ${columns
392
+ // Add default columns.
393
+ .concat(["createdAt", "updatedAt", "isDeleted"])
394
+ .filter((c) => c !== "id")
395
+ // "A column with affinity BLOB does not prefer one storage class over another
396
+ // and no attempt is made to coerce data from one storage class into another."
397
+ // https://www.sqlite.org/datatype3.html
398
+ .map((name) => `${sql.identifier(name).sql} blob`)
399
+ .join(", ")}
400
+ );
401
+ `;
402
+ const selectAppOwner = (deps) => {
403
+ const result = deps.sqlite.exec(sql `
404
+ select mnemonic, id, createdAt, encryptionKey, writeKey, timestamp
405
+ from evolu_owner
406
+ order by createdAt asc
407
+ limit 1;
408
+ `);
409
+ if (!result.ok)
410
+ return result;
411
+ const { rows: [ownerRow], } = result.value;
412
+ assert(ownerRow.writeKey != null, "The writeKey is null");
413
+ const appOwner = {
414
+ type: "AppOwner",
415
+ mnemonic: ownerRow.mnemonic,
416
+ createdAt: ownerRow.createdAt,
417
+ id: ownerRow.id,
418
+ encryptionKey: ownerRow.encryptionKey,
419
+ writeKey: ownerRow.writeKey,
420
+ };
421
+ return ok([appOwner, ownerRow]);
422
+ };
423
+ const initializeDb = (deps) => (mnemonic) => {
424
+ for (const query of [
425
+ sql `
426
+ create table evolu_config (
427
+ "key" text not null primary key,
428
+ "value" any not null
429
+ )
430
+ strict;
431
+ `,
432
+ sql `
433
+ insert into evolu_config ("key", "value")
434
+ values ('protocolVersion', ${protocolVersion});
435
+ `,
436
+ /**
437
+ * The History table stores all values per timestamp, table, row, and
438
+ * column. It's required for merging without conflicts. Evolu uses
439
+ * last-write-win CRDT. In case of last-write-win value isn't what we
440
+ * want, we can use time travel. The current implementation prefers
441
+ * performance over storage size. The History table denormalizes Timestamp
442
+ * and DbChange to leverage a covering index. Hence, every value change
443
+ * has its timestamp, table, row, and column. In the future, we will
444
+ * rethink that and store history more efficiently.
445
+ *
446
+ * There is no need to use `OwnerId` on the client, because timestamps
447
+ * (which include a `NodeId`) are globally unique. The relay needs
448
+ * `OwnerId` because it hosts multiple apps/owners, but client storage
449
+ * represents one DB, and even when it hosts multiple owners, their
450
+ * timestamps remain unique.
451
+ */
452
+ sql `
453
+ create table evolu_history (
454
+ "timestamp" blob not null,
455
+ "table" text not null,
456
+ "row" blob not null,
457
+ "column" text not null,
458
+ "value" any
459
+ )
460
+ strict;
461
+ `,
462
+ sql `
463
+ create index evolu_history_timestamp on evolu_history ("timestamp");
464
+ `,
465
+ sql `
466
+ create unique index evolu_history_row_column_table_timestampDesc on evolu_history (
467
+ "row",
468
+ "column",
469
+ "table",
470
+ "timestamp" desc
471
+ );
472
+ `,
473
+ sql `
474
+ create table evolu_owner (
475
+ "mnemonic" text not null primary key,
476
+ "id" text not null,
477
+ "encryptionKey" blob not null,
478
+ "createdAt" text not null,
479
+ "writeKey" blob,
480
+ "timestamp" text not null
481
+ )
482
+ strict;
483
+ `,
484
+ ]) {
485
+ const result = deps.sqlite.exec(query);
486
+ if (!result.ok)
487
+ return result;
488
+ }
489
+ const appOwner = createAppOwner(deps)(mnemonic);
490
+ const ownerRow = createOwnerRow(deps)(appOwner);
491
+ const result = deps.sqlite.exec(sql `
492
+ insert into evolu_owner
493
+ (mnemonic, id, encryptionKey, createdAt, writeKey, timestamp)
494
+ values
495
+ (
496
+ ${ownerRow.mnemonic},
497
+ ${ownerRow.id},
498
+ ${ownerRow.encryptionKey},
499
+ ${ownerRow.createdAt},
500
+ ${ownerRow.writeKey},
501
+ ${ownerRow.timestamp}
502
+ );
503
+ `);
504
+ if (!result.ok)
505
+ return result;
506
+ return ok([appOwner, ownerRow]);
507
+ };
508
+ const applyChanges = (deps) => (changes, onChange) => {
509
+ let lastTimestamp = timestampStringToTimestamp(deps.ownerRowRef.get().timestamp);
510
+ const messages = [];
511
+ for (const change of changes) {
512
+ const nextTimestamp = sendTimestamp(deps)(lastTimestamp);
513
+ if (!nextTimestamp.ok)
514
+ return nextTimestamp;
515
+ lastTimestamp = nextTimestamp.value;
516
+ messages.push({ timestamp: lastTimestamp, change });
517
+ }
518
+ const apply = applyMessages(deps)(messages, lastTimestamp);
519
+ if (!apply.ok)
520
+ return apply;
521
+ if (onChange)
522
+ onChange();
523
+ assertNonEmptyReadonlyArray(messages);
524
+ return ok(messages);
525
+ };
526
+ const applyMessages = (deps) => (messages, lastTimestamp, options = {}) => {
527
+ for (const message of messages) {
528
+ if (!options.isMigrationFromVersion0) {
529
+ const apply1 = applyMessageToAppTable(deps)(message);
530
+ if (!apply1.ok)
531
+ return apply1;
532
+ }
533
+ const apply2 = applyMessageToTimestampAndHistoryTables(deps)(message);
534
+ if (!apply2.ok)
535
+ return apply2;
536
+ }
537
+ const timestamp = timestampToTimestampString(lastTimestamp);
538
+ deps.ownerRowRef.modify((owner) => ({ ...owner, timestamp }));
539
+ const saveTimestamp = deps.sqlite.exec(sql.prepared `
540
+ update evolu_owner set "timestamp" = ${timestamp};
541
+ `);
542
+ if (!saveTimestamp.ok)
543
+ return saveTimestamp;
544
+ return ok();
545
+ };
546
+ const applyMessageToAppTable = (deps) => (message) => {
547
+ const date = new Date(message.timestamp.millis).toISOString();
548
+ const timestamp = timestampToBinaryTimestamp(message.timestamp);
549
+ for (const [column, value] of objectToEntries(message.change.values)) {
550
+ const result = deps.sqlite.exec(sql.prepared `
551
+ with
552
+ lastTimestamp as (
553
+ select timestamp
554
+ from evolu_history
555
+ where
556
+ "row" = ${message.change.id}
557
+ and "column" = ${column}
558
+ and "table" = ${message.change.table}
559
+ order by timestamp desc
560
+ limit 1
561
+ )
562
+ insert into ${sql.identifier(message.change.table)}
563
+ ("id", ${sql.identifier(column)}, createdAt, updatedAt)
564
+ select ${message.change.id}, ${value}, ${date}, ${date}
565
+ where
566
+ (select timestamp from lastTimestamp) is null
567
+ or (select timestamp from lastTimestamp) < ${timestamp}
568
+ on conflict ("id") do update
569
+ set
570
+ ${sql.identifier(column)} = ${value},
571
+ updatedAt = ${date}
572
+ where
573
+ (select timestamp from lastTimestamp) is null
574
+ or (select timestamp from lastTimestamp) < ${timestamp};
575
+ `);
576
+ if (!result.ok)
577
+ return result;
578
+ }
579
+ return ok();
580
+ };
581
+ export const applyMessageToTimestampAndHistoryTables = (deps) => (message) => {
582
+ const timestamp = timestampToBinaryTimestamp(message.timestamp);
583
+ const ownerId = ownerIdToBinaryOwnerId(deps.ownerRowRef.get().id);
584
+ const id = idToBinaryId(message.change.id);
585
+ const result = deps.storage.insertTimestamp(ownerId, timestamp);
586
+ if (!result.ok)
587
+ return result;
588
+ for (const [column, value] of Object.entries(message.change.values)) {
589
+ const result = deps.sqlite.exec(sql.prepared `
590
+ insert into evolu_history
591
+ ("timestamp", "table", "row", "column", "value")
592
+ values
593
+ (${timestamp}, ${message.change.table}, ${id}, ${column}, ${value})
594
+ on conflict do nothing;
595
+ `);
596
+ if (!result.ok)
597
+ return result;
598
+ }
599
+ return ok();
600
+ };
601
+ const loadQueries = (deps) => (tabId, queries) => {
602
+ const queriesRows = [];
603
+ for (const query of queries) {
604
+ const sqlQuery = deserializeQuery(query);
605
+ const result = deps.sqlite.exec(sqlQuery);
606
+ if (!result.ok)
607
+ return result;
608
+ queriesRows.push([query, result.value.rows]);
609
+ if (sqlQuery.options?.logExplainQueryPlan) {
610
+ explainSqliteQueryPlan(deps)(sqlQuery);
611
+ }
612
+ }
613
+ const queryRowsCache = deps.getQueryRowsCache(tabId);
614
+ const previousState = queryRowsCache.get();
615
+ queryRowsCache.set(queriesRows);
616
+ const currentState = queryRowsCache.get();
617
+ const queryPatchesArray = queries.map((query) => ({
618
+ query,
619
+ patches: makePatches(previousState.get(query), currentState.get(query) ?? emptyRows),
620
+ }));
621
+ return ok(queryPatchesArray);
622
+ };
623
+ export const maybeMigrateToVersion0 = (deps) => (schema) => {
624
+ // evolu_history is a new table
625
+ const hasOwnerButNoHistory = schema.tables.some((t) => t.name === "evolu_owner") &&
626
+ !schema.tables.some((t) => t.name === "evolu_history");
627
+ if (!hasOwnerButNoHistory) {
628
+ return ok(null);
629
+ }
630
+ const mnemonicAndLastTimestamp = deps.sqlite.exec(sql ` select mnemonic, timestamp from evolu_owner limit 1; `);
631
+ if (!mnemonicAndLastTimestamp.ok)
632
+ return mnemonicAndLastTimestamp;
633
+ const messagesRows = deps.sqlite.exec(sql `
634
+ select "timestamp", "table", "row", "column", "value" from evolu_message;
635
+ `);
636
+ if (!messagesRows.ok)
637
+ return messagesRows;
638
+ for (const query of [
639
+ sql `drop table evolu_owner;`,
640
+ sql `drop table evolu_message;`,
641
+ ]) {
642
+ const result = deps.sqlite.exec(query);
643
+ if (!result.ok)
644
+ return result;
645
+ }
646
+ const messages = messagesRows.value.rows.map((message) => ({
647
+ timestamp: timestampStringToTimestamp(message.timestamp),
648
+ change: {
649
+ id: message.row,
650
+ table: message.table,
651
+ values: { [message.column]: message.value },
652
+ },
653
+ }));
654
+ const { rows: [{ mnemonic, timestamp }], } = mnemonicAndLastTimestamp.value;
655
+ const lastTimestamp = timestampStringToTimestamp(timestamp);
656
+ return ok({ messages, mnemonic, lastTimestamp });
657
+ };
658
+ const dropAllTables = (deps) => {
659
+ const schema = getDbSchema(deps)();
660
+ if (!schema.ok)
661
+ return schema;
662
+ for (const table of schema.value.tables) {
663
+ /**
664
+ * The dropped table is completely removed from the database schema and the
665
+ * disk file. The table can not be recovered. All indices and triggers
666
+ * associated with the table are also deleted.
667
+ * https://sqlite.org/lang_droptable.html
668
+ */
669
+ const result = deps.sqlite.exec(sql `
670
+ drop table ${sql.identifier(table.name)};
671
+ `);
672
+ if (!result.ok)
673
+ return result;
674
+ }
675
+ return ok();
676
+ };
677
+ const handleSyncOpen = (deps) => (send) => {
678
+ const ownerId = deps.ownerRowRef.get().id;
679
+ const message = createProtocolMessageForSync(deps)(ownerId);
680
+ if (message) {
681
+ deps.console.log("[db]", "send initial sync message", message);
682
+ send(message);
683
+ }
684
+ };
685
+ const createHandleSyncMessage = (deps) => (input, send) => {
686
+ deps.console.log("[db]", "receive sync message", input);
687
+ const { writeKey } = deps.ownerRowRef.get();
688
+ const output = deps.sqlite.transaction(() => applyProtocolMessageAsClient(deps)(input, {
689
+ getWriteKey: (_ownerId) => writeKey,
690
+ }));
691
+ if (!output.ok) {
692
+ deps.postMessage({ type: "onError", error: output.error });
693
+ return;
694
+ }
695
+ if (output.value) {
696
+ deps.console.log("[db]", "respond sync message", output.value);
697
+ send(output.value);
698
+ }
699
+ };
700
+ const createClientStorage = (deps) => (options) => {
701
+ const sqliteStorageBase = createSqliteStorageBase(deps)(options);
702
+ if (!sqliteStorageBase.ok)
703
+ return sqliteStorageBase;
704
+ const storage = {
705
+ ...sqliteStorageBase.value,
706
+ validateWriteKey: constFalse,
707
+ writeMessages: (_ownerId, messages) => {
708
+ // TODO: Get owner by _ownerId when we support more.
709
+ const owner = deps.ownerRowRef.get();
710
+ const decodedAndDecryptedMessages = [];
711
+ for (const message of messages) {
712
+ const dbChange = decryptAndDecodeDbChange(deps)(message.change, owner.encryptionKey);
713
+ if (!dbChange.ok) {
714
+ deps.postMessage({
715
+ type: "onError",
716
+ error: dbChange.error,
717
+ });
718
+ return false;
719
+ }
720
+ decodedAndDecryptedMessages.push({
721
+ timestamp: message.timestamp,
722
+ change: dbChange.value,
723
+ });
724
+ }
725
+ let timestamp = timestampStringToTimestamp(owner.timestamp);
726
+ for (const message of messages) {
727
+ const receive = receiveTimestamp(deps)(timestamp, message.timestamp);
728
+ if (!receive.ok) {
729
+ deps.postMessage({ type: "onError", error: receive.error });
730
+ return false;
731
+ }
732
+ timestamp = receive.value;
733
+ }
734
+ const applyMessagesResult = applyMessages({ ...deps, storage })(decodedAndDecryptedMessages, timestamp);
735
+ if (!applyMessagesResult.ok) {
736
+ deps.postMessage({
737
+ type: "onError",
738
+ error: applyMessagesResult.error,
739
+ });
740
+ return false;
741
+ }
742
+ deps.postMessage({ type: "onReceive" });
743
+ return true;
744
+ },
745
+ readDbChange: (_ownerId, timestamp) => {
746
+ const result = deps.sqlite.exec(sql `
747
+ select "table", "row", "column", "value"
748
+ from evolu_history
749
+ where "timestamp" = ${timestamp};
750
+ `);
751
+ if (!result.ok) {
752
+ deps.postMessage({ type: "onError", error: result.error });
753
+ return null;
754
+ }
755
+ const { rows } = result.value;
756
+ assert(rows.length > 0, "Rows must not be empty");
757
+ const { table, row } = rows[0];
758
+ const values = {};
759
+ for (const r of rows) {
760
+ assert(r.table === table, "All rows must have the same table");
761
+ assert(eqArrayNumber(r.row, row), "All rows must have the same Id");
762
+ values[r.column] = r.value;
763
+ }
764
+ const change = {
765
+ table: rows[0].table,
766
+ id: binaryIdToId(rows[0].row),
767
+ values,
768
+ };
769
+ const { encryptionKey } = deps.ownerRowRef.get();
770
+ return encodeAndEncryptDbChange(deps)(change, encryptionKey);
771
+ },
772
+ };
773
+ return ok(storage);
774
+ };