@evolu/common 6.0.1-preview.15 → 6.0.1-preview.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/Crypto.d.ts +0 -1
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +2 -1
- package/dist/src/Evolu/Config.d.ts +7 -7
- package/dist/src/Evolu/Config.d.ts.map +1 -1
- package/dist/src/Evolu/Db.d.ts +4 -8
- package/dist/src/Evolu/Db.d.ts.map +1 -1
- package/dist/src/Evolu/Db.js +136 -112
- package/dist/src/Evolu/Evolu.d.ts.map +1 -1
- package/dist/src/Evolu/Evolu.js +5 -4
- package/dist/src/Evolu/Owner.d.ts +84 -105
- package/dist/src/Evolu/Owner.d.ts.map +1 -1
- package/dist/src/Evolu/Owner.js +78 -89
- package/dist/src/Evolu/Protocol.d.ts +2 -2
- package/dist/src/Evolu/Protocol.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.js +1 -1
- package/dist/src/Evolu/Schema.d.ts +0 -7
- package/dist/src/Evolu/Schema.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/Crypto.ts +6 -4
- package/src/Evolu/Config.ts +7 -7
- package/src/Evolu/Db.ts +204 -159
- package/src/Evolu/Evolu.ts +6 -4
- package/src/Evolu/Owner.ts +128 -203
- package/src/Evolu/Protocol.ts +2 -8
- package/src/Evolu/Schema.ts +6 -7
package/dist/src/Crypto.d.ts
CHANGED
|
@@ -43,7 +43,6 @@ export declare const createSlip21Id: (seed: MnemonicSeed, path: ReadonlyArray<st
|
|
|
43
43
|
/** The encryption key for {@link SymmetricCrypto}. */
|
|
44
44
|
export declare const EncryptionKey: import("./Type.js").BrandType<import("./Type.js").BrandType<import("./Type.js").Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, import("./Type.js").Uint8ArrayError, Uint8Array<ArrayBufferLike>, import("./Type.js").Uint8ArrayError>, "Length32", import("./Type.js").LengthError<32>, import("./Type.js").Uint8ArrayError>, "EncryptionKey", import("./Type.js").BrandWithoutRefineError<"EncryptionKey", import("./Type.js").Uint8ArrayError | import("./Type.js").LengthError<32>>, never>;
|
|
45
45
|
export type EncryptionKey = typeof EncryptionKey.Type;
|
|
46
|
-
export declare const createEncryptionKey: (seed: MnemonicSeed) => EncryptionKey;
|
|
47
46
|
/** Symmetric cryptography. */
|
|
48
47
|
export interface SymmetricCrypto {
|
|
49
48
|
readonly nonceLength: NonNegativeInt;
|
package/dist/src/Crypto.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Crypto.d.ts","sourceRoot":"","sources":["../../src/Crypto.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AASH,OAAO,EAAc,MAAM,EAAW,MAAM,aAAa,CAAC;AAC1D,OAAO,EAEL,EAAE,EAEF,QAAQ,EACR,cAAc,EAEf,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"Crypto.d.ts","sourceRoot":"","sources":["../../src/Crypto.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AASH,OAAO,EAAc,MAAM,EAAW,MAAM,aAAa,CAAC;AAC1D,OAAO,EAEL,EAAE,EAEF,QAAQ,EACR,cAAc,EAEf,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGnC,yDAAyD;AACzD,MAAM,MAAM,WAAW,GAAG,UAAU,GAAG,KAAK,CAAC,aAAa,CAAC,CAAC;AAE5D,MAAM,MAAM,iBAAiB,GAAG,CAAC,WAAW,CAAC,EAAE,MAAM,KAAK,WAAW,CAAC;AAEtE,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,iBAAiB,EAAE,iBAAiB,CAAC;CAC/C;AAED,oFAAoF;AACpF,eAAO,MAAM,iBAAiB,EAAE,iBACS,CAAC;AAE1C,MAAM,MAAM,cAAc,GA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AE,OAAO,CAAC;CACzB;AAED;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,GAChC,MAAM,oBAAoB,KACzB,eAyBF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,GAAI,QAAQ,cAAc,KAAG,cAO1D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,kBAAkB,GAAI,QAAQ,cAAc,KAAG,cAE3D,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,KAAK,OAAO,CAAC;AAExE,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,eAAe,EAAE,eAAe,CAAC;CAC3C"}
|
package/dist/src/Crypto.js
CHANGED
|
@@ -12,6 +12,7 @@ import { wordlist } from "@scure/bip39/wordlists/english";
|
|
|
12
12
|
import { urlAlphabet } from "nanoid";
|
|
13
13
|
import { getOrThrow, trySync } from "./Result.js";
|
|
14
14
|
import { brand, length, NonNegativeInt, Uint8Array, } from "./Type.js";
|
|
15
|
+
import { assert } from "./Assert.js";
|
|
15
16
|
/** Cryptographically secure PRNG. Uses internal OS-level crypto.getRandomValues. */
|
|
16
17
|
export const createRandomBytes = (bytesLength = 32) => randomBytes(bytesLength);
|
|
17
18
|
export const createEnglishMnemonic = () => bip39.generateMnemonic(wordlist, 128);
|
|
@@ -22,6 +23,7 @@ export const mnemonicToMnemonicSeed = (mnemonic) => bip39.mnemonicToSeedSync(mne
|
|
|
22
23
|
* https://github.com/satoshilabs/slips/blob/master/slip-0021.md
|
|
23
24
|
*/
|
|
24
25
|
export const createSlip21 = (seed, path) => {
|
|
26
|
+
assert(seed.length >= 16 && seed.length <= 64, `Unusual SLIP-0021 seed length: ${seed.length} bytes`);
|
|
25
27
|
let m = hmac(sha512, "Symmetric key seed", seed);
|
|
26
28
|
for (const component of path) {
|
|
27
29
|
const p = new TextEncoder().encode(component);
|
|
@@ -55,7 +57,6 @@ export const createSlip21Id = (seed, path) => {
|
|
|
55
57
|
};
|
|
56
58
|
/** The encryption key for {@link SymmetricCrypto}. */
|
|
57
59
|
export const EncryptionKey = brand("EncryptionKey", length(32)(Uint8Array));
|
|
58
|
-
export const createEncryptionKey = (seed) => createSlip21(seed, ["Evolu", "Encryption Key"]);
|
|
59
60
|
/**
|
|
60
61
|
* XChaCha20-Poly1305 encryption
|
|
61
62
|
*
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ConsoleConfig } from "../Console.js";
|
|
2
|
-
import {
|
|
2
|
+
import { SimpleName } from "../Type.js";
|
|
3
3
|
import type { DbIndexesBuilder } from "./Kysely.js";
|
|
4
|
+
import type { AppOwner } from "./Owner.js";
|
|
4
5
|
export interface Config extends ConsoleConfig {
|
|
5
6
|
/**
|
|
6
7
|
* The name of the Evolu instance. Evolu is multitenant - it can run multiple
|
|
@@ -59,17 +60,16 @@ export interface Config extends ConsoleConfig {
|
|
|
59
60
|
*/
|
|
60
61
|
readonly indexes?: DbIndexesBuilder;
|
|
61
62
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* until special UX requirements are needed (e.g., multitenancy).
|
|
63
|
+
* Initial AppOwner to use when creating Evolu instance. If omitted, a new
|
|
64
|
+
* AppOwner will be generated automatically.
|
|
65
65
|
*/
|
|
66
|
-
readonly
|
|
66
|
+
readonly initialAppOwner?: AppOwner;
|
|
67
67
|
/**
|
|
68
68
|
* Use in-memory SQLite database instead of persistent storage. Useful for
|
|
69
69
|
* testing or temporary data that doesn't need persistence.
|
|
70
70
|
*
|
|
71
|
-
* In-memory databases exist only in RAM and are completely destroyed when
|
|
72
|
-
*
|
|
71
|
+
* In-memory databases exist only in RAM and are completely destroyed when the
|
|
72
|
+
* process ends, making them forensically safe for sensitive data.
|
|
73
73
|
*
|
|
74
74
|
* The default value is: `false`.
|
|
75
75
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE9C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE9C,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,MAAM,WAAW,MAAO,SAAQ,aAAa;IAC3C;;;;;;;;;;;;;;;OAeG;IACH,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAE1B;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAEzB;;;;OAIG;IACH,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE3B;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B;;;;;;;;;;;;;;;;;;;OAmBG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAEpC;;;OAGG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,QAAQ,CAAC;IAEpC;;;;;;;;OAQG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,eAAO,MAAM,aAAa,EAAE,MAM3B,CAAC"}
|
package/dist/src/Evolu/Db.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ import { CreateMnemonicDep, CreateRandomBytesDep, SymmetricCryptoDecryptError }
|
|
|
5
5
|
import { TransferableError } from "../Error.js";
|
|
6
6
|
import { NanoIdLibDep } from "../NanoId.js";
|
|
7
7
|
import { RandomDep } from "../Random.js";
|
|
8
|
-
import { Ref } from "../Ref.js";
|
|
9
8
|
import { Result } from "../Result.js";
|
|
10
9
|
import { CreateSqliteDriverDep, SafeSql, SqliteDep, SqliteError, SqliteRow } from "../Sqlite.js";
|
|
11
10
|
import { TimeDep } from "../Time.js";
|
|
@@ -13,7 +12,7 @@ import { Id, Mnemonic, SimpleName } from "../Type.js";
|
|
|
13
12
|
import { Worker } from "../Worker.js";
|
|
14
13
|
import { Config } from "./Config.js";
|
|
15
14
|
import { QueryPatches } from "./Diff.js";
|
|
16
|
-
import { AppOwner
|
|
15
|
+
import { AppOwner } from "./Owner.js";
|
|
17
16
|
import { BinaryOwnerId, CrdtMessage, DbChange, ProtocolError, Storage } from "./Protocol.js";
|
|
18
17
|
import { Query } from "./Query.js";
|
|
19
18
|
import { SqliteStorageBase } from "./Storage.js";
|
|
@@ -77,7 +76,7 @@ export type DbWorkerInput = {
|
|
|
77
76
|
};
|
|
78
77
|
export type DbWorkerOutput = {
|
|
79
78
|
readonly type: "onInit";
|
|
80
|
-
readonly
|
|
79
|
+
readonly appOwner: AppOwner;
|
|
81
80
|
} | {
|
|
82
81
|
readonly type: "onError";
|
|
83
82
|
readonly error: SqliteError | TransferableError | TimestampError | ProtocolError | SymmetricCryptoDecryptError;
|
|
@@ -99,9 +98,6 @@ export type DbWorkerOutput = {
|
|
|
99
98
|
readonly file: Uint8Array;
|
|
100
99
|
};
|
|
101
100
|
export type DbWorkerPlatformDeps = CreateSqliteDriverDep & CreateSyncDep & ConsoleDep & TimeDep & RandomDep & NanoIdLibDep & CreateMnemonicDep & CreateRandomBytesDep;
|
|
102
|
-
export interface OwnerRowRefDep {
|
|
103
|
-
readonly ownerRowRef: Ref<OwnerRow>;
|
|
104
|
-
}
|
|
105
101
|
export declare const createDbWorkerForPlatform: (platformDeps: DbWorkerPlatformDeps) => DbWorker;
|
|
106
102
|
/**
|
|
107
103
|
* Get the current database schema by reading SQLite metadata.
|
|
@@ -121,9 +117,9 @@ export interface DbSnapshot {
|
|
|
121
117
|
export declare const getDbSnapshot: (deps: SqliteDep) => DbSnapshot;
|
|
122
118
|
export declare const createAppTable: (tableName: string, columns: ReadonlyArray<string>) => SafeSql;
|
|
123
119
|
export declare const applyMessageToTimestampAndHistoryTables: (deps: SqliteDep & ClientStorageDep) => (ownerId: BinaryOwnerId, message: CrdtMessage) => Result<void, SqliteError>;
|
|
124
|
-
export interface ClientStorage extends SqliteStorageBase, Storage {
|
|
125
|
-
}
|
|
126
120
|
export interface ClientStorageDep {
|
|
127
121
|
readonly storage: ClientStorage;
|
|
128
122
|
}
|
|
123
|
+
export interface ClientStorage extends SqliteStorageBase, Storage {
|
|
124
|
+
}
|
|
129
125
|
//# sourceMappingURL=Db.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Db.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Db.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,qBAAqB,EACtB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EACL,iBAAiB,EACjB,oBAAoB,
|
|
1
|
+
{"version":3,"file":"Db.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Db.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,qBAAqB,EACtB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EAGpB,2BAA2B,EAE5B,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,O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,CAAC;AAKJ,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC;QACrB,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,aAAa,CAAC,SAAS,CAAC,CAAC;KAChC,CAAC,CAAC;CACJ;AAGD,eAAO,MAAM,aAAa,GAAI,MAAM,SAAS,KAAG,UAmB/C,CAAC;AAiEF,eAAO,MAAM,cAAc,GACzB,WAAW,MAAM,EACjB,SAAS,aAAa,CAAC,MAAM,CAAC,KAC7B,OAcW,CAAC;AA0Mf,eAAO,MAAM,uCAAuC,GACjD,MAAM,SAAS,GAAG,gBAAgB,MAClC,SAAS,aAAa,EAAE,SAAS,WAAW,KAAG,MAAM,CAAC,IAAI,EAAE,WAAW,CA0BvE,CAAC;AA4FJ,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC;CACjC;AAED,MAAM,WAAW,aAAc,SAAQ,iBAAiB,EAAE,OAAO;CAAG"}
|
package/dist/src/Evolu/Db.js
CHANGED
|
@@ -4,17 +4,16 @@ import { createSymmetricCrypto, } from "../Crypto.js";
|
|
|
4
4
|
import { eqArrayNumber } from "../Eq.js";
|
|
5
5
|
import { constFalse, exhaustiveCheck } from "../Function.js";
|
|
6
6
|
import { objectToEntries } from "../Object.js";
|
|
7
|
-
import { createRef } from "../Ref.js";
|
|
8
7
|
import { ok } from "../Result.js";
|
|
9
8
|
import { createSqlite, explainSqliteQueryPlan, sql, } from "../Sqlite.js";
|
|
10
9
|
import { array, object, String } from "../Type.js";
|
|
11
10
|
import { createInitializedWorker, } from "../Worker.js";
|
|
12
11
|
import { makePatches } from "./Diff.js";
|
|
13
|
-
import { createAppOwner
|
|
14
|
-
import { applyProtocolMessageAsClient, Base64Url256, binaryIdToId, createProtocolMessageForSync, createProtocolMessageFromCrdtMessages, decryptAndDecodeDbChange, encodeAndEncryptDbChange, idToBinaryId, ownerIdToBinaryOwnerId, protocolVersion, } from "./Protocol.js";
|
|
12
|
+
import { createAppOwner } from "./Owner.js";
|
|
13
|
+
import { applyProtocolMessageAsClient, Base64Url256, binaryIdToId, binaryOwnerIdToOwnerId, createProtocolMessageForSync, createProtocolMessageFromCrdtMessages, decryptAndDecodeDbChange, encodeAndEncryptDbChange, idToBinaryId, ownerIdToBinaryOwnerId, protocolVersion, } from "./Protocol.js";
|
|
15
14
|
import { createQueryRowsCache, deserializeQuery, emptyRows, } from "./Query.js";
|
|
16
15
|
import { createSqliteStorageBase, } from "./Storage.js";
|
|
17
|
-
import { binaryTimestampToTimestamp, receiveTimestamp, sendTimestamp, timestampStringToTimestamp, timestampToBinaryTimestamp, timestampToTimestampString, } from "./Timestamp.js";
|
|
16
|
+
import { binaryTimestampToTimestamp, createInitialTimestamp, receiveTimestamp, sendTimestamp, timestampStringToTimestamp, timestampToBinaryTimestamp, timestampToTimestampString, } from "./Timestamp.js";
|
|
18
17
|
export const DbTable = object({
|
|
19
18
|
name: Base64Url256,
|
|
20
19
|
columns: array(Base64Url256),
|
|
@@ -24,6 +23,22 @@ export const DbSchema = object({
|
|
|
24
23
|
tables: array(DbTable),
|
|
25
24
|
indexes: array(DbIndex),
|
|
26
25
|
});
|
|
26
|
+
const createClock = (deps) => (initialTimestamp = createInitialTimestamp(deps)) => {
|
|
27
|
+
let currentTimestamp = initialTimestamp;
|
|
28
|
+
return {
|
|
29
|
+
get: () => currentTimestamp,
|
|
30
|
+
save: (timestamp) => {
|
|
31
|
+
currentTimestamp = timestamp;
|
|
32
|
+
const timestampString = timestampToTimestampString(timestamp);
|
|
33
|
+
const saveTimestamp = deps.sqlite.exec(sql.prepared `
|
|
34
|
+
update evolu_config set "clock" = ${timestampString};
|
|
35
|
+
`);
|
|
36
|
+
if (!saveTimestamp.ok)
|
|
37
|
+
return saveTimestamp;
|
|
38
|
+
return ok();
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
};
|
|
27
42
|
export const createDbWorkerForPlatform = (platformDeps) => {
|
|
28
43
|
const tabQueryRowsCacheMap = new Map();
|
|
29
44
|
const getQueryRowsCache = (tabId) => {
|
|
@@ -43,20 +58,63 @@ export const createDbWorkerForPlatform = (platformDeps) => {
|
|
|
43
58
|
return null;
|
|
44
59
|
}
|
|
45
60
|
const sqlite = sqliteResult.value;
|
|
61
|
+
const platformDepsWithSqlite = { ...platformDeps, sqlite };
|
|
46
62
|
const deps = sqlite.transaction(() => {
|
|
47
63
|
const currentDbSchema = getDbSchema({ sqlite })();
|
|
48
64
|
if (!currentDbSchema.ok)
|
|
49
65
|
return currentDbSchema;
|
|
66
|
+
let appOwner;
|
|
67
|
+
let clock;
|
|
68
|
+
const versionTableExists = currentDbSchema.value.tables.some((table) => table.name === "evolu_version");
|
|
69
|
+
if (versionTableExists) {
|
|
70
|
+
const versionResult = sqlite.exec(sql `select protocolVersion from evolu_version limit 1;`);
|
|
71
|
+
if (!versionResult.ok)
|
|
72
|
+
return versionResult;
|
|
73
|
+
// TODO: Handle version migrations here if needed
|
|
74
|
+
// const [{ protocolVersion }] = protocolVersionResult.value.rows;
|
|
75
|
+
// if (protocolVersion < currentProtocolVersion) {
|
|
76
|
+
// const migrateResult = migrateDatabase({ sqlite })(
|
|
77
|
+
// protocolVersion,
|
|
78
|
+
// currentProtocolVersion
|
|
79
|
+
// );
|
|
80
|
+
// if (!migrateResult.ok) return migrateResult;
|
|
81
|
+
// }
|
|
82
|
+
const configResult = sqlite.exec(sql `
|
|
83
|
+
select
|
|
84
|
+
clock,
|
|
85
|
+
appOwnerId,
|
|
86
|
+
appOwnerEncryptionKey,
|
|
87
|
+
appOwnerWriteKey,
|
|
88
|
+
appOwnerMnemonic
|
|
89
|
+
from evolu_config
|
|
90
|
+
limit 1;
|
|
91
|
+
`);
|
|
92
|
+
if (!configResult.ok)
|
|
93
|
+
return configResult;
|
|
94
|
+
const [config] = configResult.value.rows;
|
|
95
|
+
appOwner = {
|
|
96
|
+
type: "AppOwner",
|
|
97
|
+
id: config.appOwnerId,
|
|
98
|
+
encryptionKey: config.appOwnerEncryptionKey,
|
|
99
|
+
writeKey: config.appOwnerWriteKey,
|
|
100
|
+
mnemonic: config.appOwnerMnemonic,
|
|
101
|
+
};
|
|
102
|
+
clock = createClock(platformDepsWithSqlite)(timestampStringToTimestamp(config.clock));
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
appOwner =
|
|
106
|
+
initMessage.config.initialAppOwner ??
|
|
107
|
+
createAppOwner(platformDeps.createMnemonic());
|
|
108
|
+
clock = createClock(platformDepsWithSqlite)();
|
|
109
|
+
const initializeDbResult = initializeDb(platformDepsWithSqlite)(appOwner, clock);
|
|
110
|
+
if (!initializeDbResult.ok)
|
|
111
|
+
return initializeDbResult;
|
|
112
|
+
}
|
|
50
113
|
const ensureDbSchemaResult = ensureDbSchema({ sqlite })(initMessage.dbSchema, currentDbSchema.value);
|
|
51
114
|
if (!ensureDbSchemaResult.ok)
|
|
52
115
|
return ensureDbSchemaResult;
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
? selectAppOwner({ sqlite })
|
|
56
|
-
: initializeDb({ ...platformDeps, sqlite })(initMessage.config.mnemonic);
|
|
57
|
-
if (!appOwnerAndOwnerRow.ok)
|
|
58
|
-
return appOwnerAndOwnerRow;
|
|
59
|
-
const [appOwner, ownerRow] = appOwnerAndOwnerRow.value;
|
|
116
|
+
const owners = new Map();
|
|
117
|
+
owners.set(appOwner.id, appOwner);
|
|
60
118
|
const depsWithoutSyncAndStorage = {
|
|
61
119
|
...platformDeps,
|
|
62
120
|
postMessage,
|
|
@@ -64,7 +122,8 @@ export const createDbWorkerForPlatform = (platformDeps) => {
|
|
|
64
122
|
timestampConfig: initMessage.config,
|
|
65
123
|
symmetricCrypto: createSymmetricCrypto(platformDeps),
|
|
66
124
|
getQueryRowsCache,
|
|
67
|
-
|
|
125
|
+
clock,
|
|
126
|
+
owners,
|
|
68
127
|
};
|
|
69
128
|
const storage = createClientStorage(depsWithoutSyncAndStorage)({
|
|
70
129
|
onStorageError: (error) => {
|
|
@@ -77,12 +136,13 @@ export const createDbWorkerForPlatform = (platformDeps) => {
|
|
|
77
136
|
...depsWithoutSyncAndStorage,
|
|
78
137
|
storage: storage.value,
|
|
79
138
|
};
|
|
80
|
-
if (!
|
|
139
|
+
if (!versionTableExists &&
|
|
140
|
+
isNonEmptyReadonlyArray(initMessage.initialData)) {
|
|
81
141
|
const result = applyChanges(depsWithoutSync)(initMessage.initialData);
|
|
82
142
|
if (!result.ok)
|
|
83
143
|
return result;
|
|
84
144
|
}
|
|
85
|
-
postMessage({ type: "onInit",
|
|
145
|
+
postMessage({ type: "onInit", appOwner });
|
|
86
146
|
const sync = platformDeps.createSync(platformDeps)({
|
|
87
147
|
...initMessage.config,
|
|
88
148
|
onOpen: safeHandler(handleSyncOpen(depsWithoutSync)),
|
|
@@ -167,12 +227,8 @@ export const createDbWorkerForPlatform = (platformDeps) => {
|
|
|
167
227
|
const messages = applyChanges(deps)(toSyncChanges, onChange);
|
|
168
228
|
if (!messages.ok)
|
|
169
229
|
return messages;
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
// error if not.
|
|
173
|
-
if (owner.writeKey == null) {
|
|
174
|
-
return ok();
|
|
175
|
-
}
|
|
230
|
+
// TODO: Use owner from db change or AppOwner
|
|
231
|
+
const owner = Array.from(deps.owners.values())[0];
|
|
176
232
|
const protocolMessage = createProtocolMessageFromCrdtMessages(deps)(owner, messages.value);
|
|
177
233
|
deps.console.log("[db]", "send data message", messages.value, protocolMessage);
|
|
178
234
|
deps.sync.send(protocolMessage);
|
|
@@ -210,7 +266,9 @@ export const createDbWorkerForPlatform = (platformDeps) => {
|
|
|
210
266
|
const ensureDbSchemaResult = ensureDbSchema(deps)(message.restore.dbSchema, dbSchema.value);
|
|
211
267
|
if (!ensureDbSchemaResult.ok)
|
|
212
268
|
return ensureDbSchemaResult;
|
|
213
|
-
const
|
|
269
|
+
const appOwner = createAppOwner(message.restore.mnemonic);
|
|
270
|
+
const clock = createClock(deps)();
|
|
271
|
+
const initializeDbResult = initializeDb(deps)(appOwner, clock);
|
|
214
272
|
if (!initializeDbResult.ok)
|
|
215
273
|
return initializeDbResult;
|
|
216
274
|
}
|
|
@@ -389,39 +447,46 @@ export const createAppTable = (tableName, columns) => `
|
|
|
389
447
|
.join(", ")}
|
|
390
448
|
);
|
|
391
449
|
`;
|
|
392
|
-
const
|
|
393
|
-
const result = deps.sqlite.exec(sql `
|
|
394
|
-
select mnemonic, id, createdAt, encryptionKey, writeKey, timestamp
|
|
395
|
-
from evolu_owner
|
|
396
|
-
order by createdAt asc
|
|
397
|
-
limit 1;
|
|
398
|
-
`);
|
|
399
|
-
if (!result.ok)
|
|
400
|
-
return result;
|
|
401
|
-
const { rows: [ownerRow], } = result.value;
|
|
402
|
-
assert(ownerRow.writeKey != null, "The writeKey is null");
|
|
403
|
-
const appOwner = {
|
|
404
|
-
type: "AppOwner",
|
|
405
|
-
mnemonic: ownerRow.mnemonic,
|
|
406
|
-
createdAt: ownerRow.createdAt,
|
|
407
|
-
id: ownerRow.id,
|
|
408
|
-
encryptionKey: ownerRow.encryptionKey,
|
|
409
|
-
writeKey: ownerRow.writeKey,
|
|
410
|
-
};
|
|
411
|
-
return ok([appOwner, ownerRow]);
|
|
412
|
-
};
|
|
413
|
-
const initializeDb = (deps) => (mnemonic) => {
|
|
450
|
+
const initializeDb = (deps) => (initialAppOwner, initialClock) => {
|
|
414
451
|
for (const query of [
|
|
452
|
+
// Never change structure to ensure all versions can read it.
|
|
453
|
+
sql `
|
|
454
|
+
create table evolu_version (
|
|
455
|
+
"protocolVersion" integer not null
|
|
456
|
+
)
|
|
457
|
+
strict;
|
|
458
|
+
`,
|
|
459
|
+
sql `
|
|
460
|
+
insert into evolu_version ("protocolVersion")
|
|
461
|
+
values (${protocolVersion});
|
|
462
|
+
`,
|
|
415
463
|
sql `
|
|
416
464
|
create table evolu_config (
|
|
417
|
-
"
|
|
418
|
-
"
|
|
465
|
+
"clock" text not null,
|
|
466
|
+
"appOwnerId" text not null,
|
|
467
|
+
"appOwnerEncryptionKey" blob not null,
|
|
468
|
+
"appOwnerWriteKey" blob not null,
|
|
469
|
+
"appOwnerMnemonic" text
|
|
419
470
|
)
|
|
420
471
|
strict;
|
|
421
472
|
`,
|
|
422
473
|
sql `
|
|
423
|
-
insert into evolu_config
|
|
424
|
-
|
|
474
|
+
insert into evolu_config
|
|
475
|
+
(
|
|
476
|
+
"clock",
|
|
477
|
+
"appOwnerId",
|
|
478
|
+
"appOwnerEncryptionKey",
|
|
479
|
+
"appOwnerWriteKey",
|
|
480
|
+
"appOwnerMnemonic"
|
|
481
|
+
)
|
|
482
|
+
values
|
|
483
|
+
(
|
|
484
|
+
${timestampToTimestampString(initialClock.get())},
|
|
485
|
+
${initialAppOwner.id},
|
|
486
|
+
${initialAppOwner.encryptionKey},
|
|
487
|
+
${initialAppOwner.writeKey},
|
|
488
|
+
${initialAppOwner.mnemonic ?? null}
|
|
489
|
+
);
|
|
425
490
|
`,
|
|
426
491
|
/**
|
|
427
492
|
* The History table stores all values per ownerId, timestamp, table, id,
|
|
@@ -458,59 +523,24 @@ const initializeDb = (deps) => (mnemonic) => {
|
|
|
458
523
|
"timestamp" desc
|
|
459
524
|
);
|
|
460
525
|
`,
|
|
461
|
-
sql `
|
|
462
|
-
create table evolu_owner (
|
|
463
|
-
"mnemonic" text not null primary key,
|
|
464
|
-
"id" text not null,
|
|
465
|
-
"encryptionKey" blob not null,
|
|
466
|
-
"createdAt" text not null,
|
|
467
|
-
"writeKey" blob,
|
|
468
|
-
"timestamp" text not null
|
|
469
|
-
)
|
|
470
|
-
strict;
|
|
471
|
-
`,
|
|
472
526
|
]) {
|
|
473
527
|
const result = deps.sqlite.exec(query);
|
|
474
528
|
if (!result.ok)
|
|
475
529
|
return result;
|
|
476
530
|
}
|
|
477
|
-
|
|
478
|
-
const ownerRow = createOwnerRow(deps)(appOwner);
|
|
479
|
-
const result = deps.sqlite.exec(sql `
|
|
480
|
-
insert into evolu_owner
|
|
481
|
-
(
|
|
482
|
-
"mnemonic",
|
|
483
|
-
"id",
|
|
484
|
-
"encryptionKey",
|
|
485
|
-
"createdAt",
|
|
486
|
-
"writeKey",
|
|
487
|
-
"timestamp"
|
|
488
|
-
)
|
|
489
|
-
values
|
|
490
|
-
(
|
|
491
|
-
${ownerRow.mnemonic},
|
|
492
|
-
${ownerRow.id},
|
|
493
|
-
${ownerRow.encryptionKey},
|
|
494
|
-
${ownerRow.createdAt},
|
|
495
|
-
${ownerRow.writeKey},
|
|
496
|
-
${ownerRow.timestamp}
|
|
497
|
-
);
|
|
498
|
-
`);
|
|
499
|
-
if (!result.ok)
|
|
500
|
-
return result;
|
|
501
|
-
return ok([appOwner, ownerRow]);
|
|
531
|
+
return ok();
|
|
502
532
|
};
|
|
503
533
|
const applyChanges = (deps) => (changes, onChange) => {
|
|
504
|
-
let
|
|
534
|
+
let clockTimestamp = deps.clock.get();
|
|
505
535
|
const messages = [];
|
|
506
536
|
for (const change of changes) {
|
|
507
|
-
const nextTimestamp = sendTimestamp(deps)(
|
|
537
|
+
const nextTimestamp = sendTimestamp(deps)(clockTimestamp);
|
|
508
538
|
if (!nextTimestamp.ok)
|
|
509
539
|
return nextTimestamp;
|
|
510
|
-
|
|
511
|
-
messages.push({ timestamp:
|
|
540
|
+
clockTimestamp = nextTimestamp.value;
|
|
541
|
+
messages.push({ timestamp: clockTimestamp, change });
|
|
512
542
|
}
|
|
513
|
-
const apply = applyMessages(deps)(messages,
|
|
543
|
+
const apply = applyMessages(deps)(messages, clockTimestamp);
|
|
514
544
|
if (!apply.ok)
|
|
515
545
|
return apply;
|
|
516
546
|
if (onChange)
|
|
@@ -518,8 +548,8 @@ const applyChanges = (deps) => (changes, onChange) => {
|
|
|
518
548
|
assertNonEmptyReadonlyArray(messages);
|
|
519
549
|
return ok(messages);
|
|
520
550
|
};
|
|
521
|
-
const applyMessages = (deps) => (messages,
|
|
522
|
-
const ownerId = ownerIdToBinaryOwnerId(deps.
|
|
551
|
+
const applyMessages = (deps) => (messages, clockTimestamp) => {
|
|
552
|
+
const ownerId = ownerIdToBinaryOwnerId(Array.from(deps.owners.values())[0].id);
|
|
523
553
|
for (const message of messages) {
|
|
524
554
|
const result1 = applyMessageToAppTable(deps)(ownerId, message);
|
|
525
555
|
if (!result1.ok)
|
|
@@ -528,14 +558,7 @@ const applyMessages = (deps) => (messages, lastTimestamp) => {
|
|
|
528
558
|
if (!result2.ok)
|
|
529
559
|
return result2;
|
|
530
560
|
}
|
|
531
|
-
|
|
532
|
-
deps.ownerRowRef.modify((owner) => ({ ...owner, timestamp }));
|
|
533
|
-
const saveTimestamp = deps.sqlite.exec(sql.prepared `
|
|
534
|
-
update evolu_owner set "timestamp" = ${timestamp};
|
|
535
|
-
`);
|
|
536
|
-
if (!saveTimestamp.ok)
|
|
537
|
-
return saveTimestamp;
|
|
538
|
-
return ok();
|
|
561
|
+
return deps.clock.save(clockTimestamp);
|
|
539
562
|
};
|
|
540
563
|
const applyMessageToAppTable = (deps) => (ownerId, message) => {
|
|
541
564
|
const timestamp = timestampToBinaryTimestamp(message.timestamp);
|
|
@@ -641,18 +664,18 @@ const dropAllTables = (deps) => {
|
|
|
641
664
|
return ok();
|
|
642
665
|
};
|
|
643
666
|
const handleSyncOpen = (deps) => (send) => {
|
|
644
|
-
const
|
|
645
|
-
|
|
646
|
-
|
|
667
|
+
for (const [id] of deps.owners) {
|
|
668
|
+
const message = createProtocolMessageForSync(deps)(id);
|
|
669
|
+
if (!message)
|
|
670
|
+
return;
|
|
647
671
|
deps.console.log("[db]", "send initial sync message", message);
|
|
648
672
|
send(message);
|
|
649
673
|
}
|
|
650
674
|
};
|
|
651
675
|
const createHandleSyncMessage = (deps) => (input, send) => {
|
|
652
676
|
deps.console.log("[db]", "receive sync message", input);
|
|
653
|
-
const { writeKey } = deps.ownerRowRef.get();
|
|
654
677
|
const output = deps.sqlite.transaction(() => applyProtocolMessageAsClient(deps)(input, {
|
|
655
|
-
getWriteKey: (
|
|
678
|
+
getWriteKey: (ownerId) => deps.owners.get(ownerId)?.writeKey ?? null,
|
|
656
679
|
}));
|
|
657
680
|
if (!output.ok) {
|
|
658
681
|
deps.postMessage({ type: "onError", error: output.error });
|
|
@@ -671,9 +694,9 @@ const createClientStorage = (deps) => (options) => {
|
|
|
671
694
|
...sqliteStorageBase.value,
|
|
672
695
|
validateWriteKey: constFalse,
|
|
673
696
|
setWriteKey: constFalse,
|
|
674
|
-
writeMessages: (
|
|
675
|
-
|
|
676
|
-
|
|
697
|
+
writeMessages: (ownerId, messages) => {
|
|
698
|
+
const owner = deps.owners.get(binaryOwnerIdToOwnerId(ownerId));
|
|
699
|
+
assert(owner, "Missing owner");
|
|
677
700
|
const decodedAndDecryptedMessages = [];
|
|
678
701
|
for (const message of messages) {
|
|
679
702
|
const dbChange = decryptAndDecodeDbChange(deps)(message, owner.encryptionKey);
|
|
@@ -689,16 +712,16 @@ const createClientStorage = (deps) => (options) => {
|
|
|
689
712
|
change: dbChange.value,
|
|
690
713
|
});
|
|
691
714
|
}
|
|
692
|
-
let
|
|
715
|
+
let clockTimestamp = deps.clock.get();
|
|
693
716
|
for (const message of messages) {
|
|
694
|
-
const receive = receiveTimestamp(deps)(
|
|
717
|
+
const receive = receiveTimestamp(deps)(clockTimestamp, message.timestamp);
|
|
695
718
|
if (!receive.ok) {
|
|
696
719
|
deps.postMessage({ type: "onError", error: receive.error });
|
|
697
720
|
return false;
|
|
698
721
|
}
|
|
699
|
-
|
|
722
|
+
clockTimestamp = receive.value;
|
|
700
723
|
}
|
|
701
|
-
const applyMessagesResult = applyMessages({ ...deps, storage })(decodedAndDecryptedMessages,
|
|
724
|
+
const applyMessagesResult = applyMessages({ ...deps, storage })(decodedAndDecryptedMessages, clockTimestamp);
|
|
702
725
|
if (!applyMessagesResult.ok) {
|
|
703
726
|
deps.postMessage({
|
|
704
727
|
type: "onError",
|
|
@@ -710,6 +733,8 @@ const createClientStorage = (deps) => (options) => {
|
|
|
710
733
|
return true;
|
|
711
734
|
},
|
|
712
735
|
readDbChange: (ownerId, timestamp) => {
|
|
736
|
+
const owner = deps.owners.get(binaryOwnerIdToOwnerId(ownerId));
|
|
737
|
+
assert(owner, "Missing owner");
|
|
713
738
|
const result = deps.sqlite.exec(sql `
|
|
714
739
|
select "table", "id", "column", "value"
|
|
715
740
|
from evolu_history
|
|
@@ -736,8 +761,7 @@ const createClientStorage = (deps) => (options) => {
|
|
|
736
761
|
values,
|
|
737
762
|
},
|
|
738
763
|
};
|
|
739
|
-
|
|
740
|
-
return encodeAndEncryptDbChange(deps)(message, encryptionKey);
|
|
764
|
+
return encodeAndEncryptDbChange(deps)(message, owner.encryptionKey);
|
|
741
765
|
},
|
|
742
766
|
};
|
|
743
767
|
return ok(storage);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Evolu.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Evolu.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,2BAA2B,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EAA0B,WAAW,EAAe,MAAM,cAAc,CAAC;AAChF,OAAO,EAAe,cAAc,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAML,QAAQ,EAET,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,MAAM,EAAiB,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAG5C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAChE,OAAO,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AAS,WAAW,EACpB,QAAQ,cAAc,CAAC,CAAC,CAAC,SAAS,KAAK,GAAG,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,EAC/D,gBAAe,OAAO,CAAC,0BAA0B,CAAC,CAAC,CAAC,CAAM,KACzD,KAAK,CAAC,CAAC,CAiBT,CAAC;AAmZJ,eAAO,MAAM,mBAAmB,GAC7B,QAAQ,MAAM,MACd,MAAM,MAAM,KAAG,MACgB,CAAC"}
|
package/dist/src/Evolu/Evolu.js
CHANGED
|
@@ -79,10 +79,11 @@ export const createEvolu = (deps) => (schema, partialConfig = {}) => {
|
|
|
79
79
|
const createEvoluInstance = (deps) => (schema, evoluConfig) => {
|
|
80
80
|
deps.console.enabled = evoluConfig.enableLogging ?? false;
|
|
81
81
|
deps.console.log("[evolu]", "createEvoluInstance");
|
|
82
|
+
// evoluConfig.mnemonic
|
|
82
83
|
const { initialData, indexes, ...config } = evoluConfig;
|
|
83
84
|
const errorStore = createStore(null);
|
|
84
85
|
const rowsStore = createStore(new Map());
|
|
85
|
-
const
|
|
86
|
+
const appOwnerStore = createStore(null);
|
|
86
87
|
const syncStore = createStore(initialSyncState);
|
|
87
88
|
const subscribedQueries = createSubscribedQueries(rowsStore);
|
|
88
89
|
const loadingPromises = createLoadingPromises(subscribedQueries);
|
|
@@ -96,7 +97,7 @@ const createEvoluInstance = (deps) => (schema, evoluConfig) => {
|
|
|
96
97
|
dbWorker.onMessage((message) => {
|
|
97
98
|
switch (message.type) {
|
|
98
99
|
case "onInit": {
|
|
99
|
-
|
|
100
|
+
appOwnerStore.set(message.appOwner);
|
|
100
101
|
break;
|
|
101
102
|
}
|
|
102
103
|
case "onError": {
|
|
@@ -318,8 +319,8 @@ const createEvoluInstance = (deps) => (schema, evoluConfig) => {
|
|
|
318
319
|
};
|
|
319
320
|
},
|
|
320
321
|
getQueryRows: (query) => (rowsStore.get().get(query) ?? emptyRows),
|
|
321
|
-
subscribeAppOwner:
|
|
322
|
-
getAppOwner:
|
|
322
|
+
subscribeAppOwner: appOwnerStore.subscribe,
|
|
323
|
+
getAppOwner: appOwnerStore.get,
|
|
323
324
|
subscribeSyncState: syncStore.subscribe,
|
|
324
325
|
getSyncState: syncStore.get,
|
|
325
326
|
insert: createMutation("insert"),
|