@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.
- package/dist/src/Assert.d.ts +6 -3
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +7 -4
- package/dist/src/Brand.d.ts +75 -0
- package/dist/src/Brand.d.ts.map +1 -0
- package/dist/src/Brand.js +1 -0
- package/dist/src/Buffer.d.ts +1 -1
- package/dist/src/Buffer.d.ts.map +1 -1
- package/dist/src/Buffer.js +1 -1
- package/dist/src/CallbackRegistry.d.ts +53 -0
- package/dist/src/CallbackRegistry.d.ts.map +1 -0
- package/dist/src/CallbackRegistry.js +25 -0
- package/dist/src/Console.d.ts +31 -6
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +72 -9
- package/dist/src/Crypto.d.ts +61 -34
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +29 -42
- package/dist/src/Evolu/Db.d.ts +137 -66
- package/dist/src/Evolu/Db.d.ts.map +1 -1
- package/dist/src/Evolu/Db.js +300 -705
- package/dist/src/Evolu/Diff.d.ts +3 -3
- package/dist/src/Evolu/Diff.d.ts.map +1 -1
- package/dist/src/Evolu/Diff.js +7 -5
- package/dist/src/Evolu/Evolu.d.ts +200 -115
- package/dist/src/Evolu/Evolu.d.ts.map +1 -1
- package/dist/src/Evolu/Evolu.js +287 -157
- package/dist/src/Evolu/Internal.d.ts +0 -2
- package/dist/src/Evolu/Internal.d.ts.map +1 -1
- package/dist/src/Evolu/Internal.js +0 -2
- package/dist/src/Evolu/LocalAuth.d.ts +144 -0
- package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
- package/dist/src/Evolu/LocalAuth.js +171 -0
- package/dist/src/Evolu/Owner.d.ts +152 -127
- package/dist/src/Evolu/Owner.d.ts.map +1 -1
- package/dist/src/Evolu/Owner.js +88 -107
- package/dist/src/Evolu/Platform.d.ts +9 -7
- package/dist/src/Evolu/Platform.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.d.ts +170 -186
- package/dist/src/Evolu/Protocol.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.js +487 -343
- package/dist/src/Evolu/Public.d.ts +6 -8
- package/dist/src/Evolu/Public.d.ts.map +1 -1
- package/dist/src/Evolu/Public.js +2 -3
- package/dist/src/Evolu/PublicKysely.js +3 -3
- package/dist/src/Evolu/Query.d.ts +2 -1
- package/dist/src/Evolu/Query.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.d.ts +4 -3
- package/dist/src/Evolu/Relay.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.js +50 -12
- package/dist/src/Evolu/Schema.d.ts +129 -73
- package/dist/src/Evolu/Schema.d.ts.map +1 -1
- package/dist/src/Evolu/Schema.js +169 -89
- package/dist/src/Evolu/Storage.d.ts +159 -14
- package/dist/src/Evolu/Storage.d.ts.map +1 -1
- package/dist/src/Evolu/Storage.js +42 -32
- package/dist/src/Evolu/Sync.d.ts +77 -13
- package/dist/src/Evolu/Sync.d.ts.map +1 -1
- package/dist/src/Evolu/Sync.js +453 -20
- package/dist/src/Evolu/Timestamp.d.ts +35 -27
- package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
- package/dist/src/Evolu/Timestamp.js +27 -18
- package/dist/src/ManyToManyMap.d.ts +74 -10
- package/dist/src/ManyToManyMap.d.ts.map +1 -1
- package/dist/src/ManyToManyMap.js +41 -6
- package/dist/src/Number.d.ts +2 -1
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Random.d.ts +3 -2
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/RefCountedResourceManager.d.ts +119 -0
- package/dist/src/RefCountedResourceManager.d.ts.map +1 -0
- package/dist/src/RefCountedResourceManager.js +197 -0
- package/dist/src/Result.d.ts +147 -51
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +7 -241
- package/dist/src/Sqlite.d.ts +21 -4
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +50 -8
- package/dist/src/Task.d.ts +511 -0
- package/dist/src/Task.d.ts.map +1 -0
- package/dist/src/Task.js +410 -0
- package/dist/src/Time.d.ts +59 -0
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +87 -4
- package/dist/src/Type.d.ts +462 -327
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +490 -436
- package/dist/src/Types.d.ts +0 -74
- package/dist/src/Types.d.ts.map +1 -1
- package/dist/src/WebSocket.d.ts +5 -2
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +12 -13
- package/dist/src/Worker.d.ts +39 -11
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +22 -4
- package/dist/src/index.d.ts +3 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -3
- package/package.json +14 -13
- package/src/Assert.ts +8 -7
- package/src/Brand.ts +75 -0
- package/src/Buffer.ts +1 -1
- package/src/CallbackRegistry.ts +84 -0
- package/src/Console.ts +91 -11
- package/src/Crypto.ts +94 -79
- package/src/Evolu/Db.ts +505 -1027
- package/src/Evolu/Diff.ts +7 -5
- package/src/Evolu/Evolu.ts +682 -329
- package/src/Evolu/Internal.ts +0 -2
- package/src/Evolu/LocalAuth.ts +422 -0
- package/src/Evolu/Owner.ts +221 -235
- package/src/Evolu/Platform.ts +9 -9
- package/src/Evolu/Protocol.ts +681 -614
- package/src/Evolu/Public.ts +7 -14
- package/src/Evolu/PublicKysely.ts +3 -3
- package/src/Evolu/Query.ts +2 -1
- package/src/Evolu/Relay.ts +62 -16
- package/src/Evolu/Schema.ts +391 -191
- package/src/Evolu/Storage.ts +275 -55
- package/src/Evolu/Sync.ts +758 -37
- package/src/Evolu/Timestamp.ts +38 -35
- package/src/ManyToManyMap.ts +127 -24
- package/src/Number.ts +2 -6
- package/src/Random.ts +3 -2
- package/src/RefCountedResourceManager.ts +368 -0
- package/src/Result.ts +152 -53
- package/src/Sqlite.ts +59 -23
- package/src/Task.ts +779 -0
- package/src/Time.ts +168 -4
- package/src/Type.ts +696 -660
- package/src/Types.ts +0 -76
- package/src/WebSocket.ts +23 -17
- package/src/Worker.ts +72 -23
- package/src/index.ts +3 -3
- package/dist/src/Callbacks.d.ts +0 -20
- package/dist/src/Callbacks.d.ts.map +0 -1
- package/dist/src/Callbacks.js +0 -18
- package/dist/src/Evolu/Config.d.ts +0 -69
- package/dist/src/Evolu/Config.d.ts.map +0 -1
- package/dist/src/Evolu/Config.js +0 -9
- package/dist/src/Evolu/Kysely.d.ts +0 -6
- package/dist/src/Evolu/Kysely.d.ts.map +0 -1
- package/dist/src/Evolu/Kysely.js +0 -21
- package/dist/src/NanoId.d.ts +0 -27
- package/dist/src/NanoId.d.ts.map +0 -1
- package/dist/src/NanoId.js +0 -6
- package/dist/src/Promise.d.ts +0 -180
- package/dist/src/Promise.d.ts.map +0 -1
- package/dist/src/Promise.js +0 -176
- package/src/Callbacks.ts +0 -43
- package/src/Evolu/Config.ts +0 -83
- package/src/Evolu/Kysely.ts +0 -38
- package/src/NanoId.ts +0 -39
- package/src/Promise.ts +0 -295
package/src/Evolu/Owner.ts
CHANGED
|
@@ -1,302 +1,288 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @module
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { assert } from "../Assert.js";
|
|
1
|
+
import * as bip39 from "@scure/bip39";
|
|
2
|
+
import { wordlist } from "@scure/bip39/wordlists/english.js";
|
|
3
|
+
import { NonEmptyReadonlyArray } from "../Array.js";
|
|
8
4
|
import {
|
|
9
|
-
createEncryptionKey,
|
|
10
|
-
CreateMnemonicDep,
|
|
11
|
-
CreateRandomBytesDep,
|
|
12
5
|
createSlip21,
|
|
13
|
-
createSlip21Id,
|
|
14
6
|
EncryptionKey,
|
|
15
|
-
|
|
16
|
-
|
|
7
|
+
Entropy16,
|
|
8
|
+
Entropy32,
|
|
9
|
+
RandomBytesDep,
|
|
17
10
|
} from "../Crypto.js";
|
|
18
|
-
import { NanoIdLibDep } from "../NanoId.js";
|
|
19
|
-
import { TimeDep } from "../Time.js";
|
|
20
11
|
import {
|
|
21
|
-
Base64Url,
|
|
22
12
|
brand,
|
|
23
|
-
DateIso,
|
|
24
|
-
DateIsoString,
|
|
25
13
|
Id,
|
|
26
|
-
|
|
14
|
+
IdBytes,
|
|
15
|
+
idBytesToId,
|
|
16
|
+
idToIdBytes,
|
|
27
17
|
Mnemonic,
|
|
28
18
|
NonNegativeInt,
|
|
29
|
-
Uint8Array,
|
|
30
19
|
} from "../Type.js";
|
|
31
|
-
import {
|
|
32
|
-
createInitialTimestamp,
|
|
33
|
-
TimestampString,
|
|
34
|
-
timestampToTimestampString,
|
|
35
|
-
} from "./Timestamp.js";
|
|
36
20
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
21
|
+
/**
|
|
22
|
+
* 32 bytes of cryptographic entropy used to derive {@link Owner} keys.
|
|
23
|
+
*
|
|
24
|
+
* Can be created using {@link createOwnerSecret} or converted from a
|
|
25
|
+
* {@link Mnemonic} using {@link mnemonicToOwnerSecret}.
|
|
26
|
+
*/
|
|
27
|
+
export const OwnerSecret = brand("OwnerSecret", Entropy32);
|
|
28
|
+
export type OwnerSecret = typeof OwnerSecret.Type;
|
|
29
|
+
|
|
30
|
+
/** Creates a {@link OwnerSecret}. */
|
|
31
|
+
export const createOwnerSecret = (deps: RandomBytesDep): OwnerSecret =>
|
|
32
|
+
deps.randomBytes.create(32) as OwnerSecret;
|
|
33
|
+
|
|
34
|
+
/** Converts an {@link OwnerSecret} to a {@link Mnemonic}. */
|
|
35
|
+
export const ownerSecretToMnemonic = (secret: OwnerSecret): Mnemonic =>
|
|
36
|
+
bip39.entropyToMnemonic(secret, wordlist) as Mnemonic;
|
|
37
|
+
|
|
38
|
+
/** Converts a {@link Mnemonic} to an {@link OwnerSecret}. */
|
|
39
|
+
export const mnemonicToOwnerSecret = (mnemonic: Mnemonic): OwnerSecret =>
|
|
40
|
+
bip39.mnemonicToEntropy(mnemonic, wordlist) as OwnerSecret;
|
|
44
41
|
|
|
45
42
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
43
|
+
* The Owner represents ownership of data in Evolu. Every database change is
|
|
44
|
+
* assigned to an owner, enabling sync functionality and access control.
|
|
45
|
+
*
|
|
46
|
+
* Owners enable **partial sync** - applications can choose which owners to
|
|
47
|
+
* sync, allowing selective data synchronization based on specific needs.
|
|
48
|
+
*
|
|
49
|
+
* Owners also provide **real data deletion** - while individual changes in
|
|
50
|
+
* local-first/distributed systems can only be marked as deleted, entire owners
|
|
51
|
+
* can be completely deleted from both relays and devices (except for
|
|
52
|
+
* {@link AppOwner}, which must be preserved for sync coordination).
|
|
49
53
|
*
|
|
50
|
-
*
|
|
51
|
-
* {@link EncryptionKey} are deterministically derived using SLIP-21, and an
|
|
52
|
-
* optional {@link WriteKey} that, when present, enables writing to the Evolu
|
|
53
|
-
* Relay or peers. The {@link WriteKey} can be rotated.
|
|
54
|
+
* Evolu provides different owner types depending on their use case:
|
|
54
55
|
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
56
|
+
* - **Coordination**: {@link AppOwner} for sync coordination and long-term
|
|
57
|
+
* persistence
|
|
58
|
+
* - **Data partitioning**: {@link ShardOwner} for partitioning application data
|
|
59
|
+
* - **Collaboration**: {@link SharedOwner} for collaborative write access
|
|
60
|
+
* - **Data sharing**: {@link SharedReadonlyOwner} for read-only access to shared
|
|
61
|
+
* data
|
|
58
62
|
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
+
* Owners are cryptographically derived from an {@link OwnerSecret} using
|
|
64
|
+
* SLIP-21, ensuring secure and deterministic key generation:
|
|
65
|
+
*
|
|
66
|
+
* - {@link OwnerId}: Globally unique public identifier
|
|
67
|
+
* - {@link EncryptionKey}: Symmetric encryption key for data protection
|
|
68
|
+
* - {@link OwnerWriteKey}: Authentication token for write operations (rotatable)
|
|
69
|
+
*
|
|
70
|
+
* @see {@link createOwner}
|
|
63
71
|
*/
|
|
64
72
|
export interface Owner {
|
|
65
|
-
readonly mnemonic: Mnemonic;
|
|
66
|
-
readonly createdAt: DateIsoString;
|
|
67
|
-
readonly id: OwnerId;
|
|
68
|
-
readonly encryptionKey: EncryptionKey;
|
|
69
|
-
readonly writeKey: WriteKey;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export interface OwnerWithWriteAccess {
|
|
73
73
|
readonly id: OwnerId;
|
|
74
|
-
readonly encryptionKey:
|
|
75
|
-
readonly writeKey:
|
|
74
|
+
readonly encryptionKey: OwnerEncryptionKey;
|
|
75
|
+
readonly writeKey: OwnerWriteKey;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* This branded {@link Id} type, generated by {@link createSlip21Id}, is a
|
|
82
|
-
* 21-character {@link Base64Url} string (126 bits of entropy), providing a
|
|
83
|
-
* compact, shareable, and secure identifier for UI use, tied to the owner's
|
|
84
|
-
* mnemonic and derivation path.
|
|
79
|
+
* OwnerId is a branded {@link Id} that uniquely identifies an {@link Owner}.
|
|
80
|
+
* Branded from {@link Id} to leverage existing helpers like {@link idToIdBytes}.
|
|
85
81
|
*/
|
|
86
82
|
export const OwnerId = brand("OwnerId", Id);
|
|
87
83
|
export type OwnerId = typeof OwnerId.Type;
|
|
88
84
|
|
|
89
|
-
|
|
85
|
+
/** Bytes representation of {@link OwnerId}. */
|
|
86
|
+
export const OwnerIdBytes = brand("OwnerIdBytes", IdBytes);
|
|
87
|
+
export type OwnerIdBytes = typeof OwnerIdBytes.Type;
|
|
90
88
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
89
|
+
export const ownerIdToOwnerIdBytes = (ownerId: OwnerId): OwnerIdBytes =>
|
|
90
|
+
idToIdBytes(ownerId) as OwnerIdBytes;
|
|
91
|
+
|
|
92
|
+
export const ownerIdBytesToOwnerId = (ownerIdBytes: OwnerIdBytes): OwnerId =>
|
|
93
|
+
idBytesToId(ownerIdBytes as IdBytes) as OwnerId;
|
|
94
|
+
|
|
95
|
+
export const ownerWriteKeyLength = 16 as NonNegativeInt;
|
|
96
|
+
|
|
97
|
+
export const OwnerEncryptionKey = brand("OwnerEncryptionKey", EncryptionKey);
|
|
98
|
+
export type OwnerEncryptionKey = typeof OwnerEncryptionKey.Type;
|
|
97
99
|
|
|
98
100
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
* existing mnemonic. It manages the app's core data, including the storage of
|
|
102
|
-
* other owners' mnemonics in an encrypted app table. Its `writeKey` is
|
|
103
|
-
* deterministic and rotatable. Never share the AppOwner mnemonic with anyone.
|
|
101
|
+
* A secure token for write operations. It's derived from {@link OwnerSecret} by
|
|
102
|
+
* default and can be rotated via {@link createOwnerWriteKey}.
|
|
104
103
|
*/
|
|
105
|
-
export
|
|
106
|
-
|
|
107
|
-
|
|
104
|
+
export const OwnerWriteKey = brand("OwnerWriteKey", Entropy16);
|
|
105
|
+
export type OwnerWriteKey = typeof OwnerWriteKey.Type;
|
|
106
|
+
|
|
107
|
+
/** Creates a randomly generated {@link OwnerWriteKey}. */
|
|
108
|
+
export const createOwnerWriteKey = (deps: RandomBytesDep): OwnerWriteKey =>
|
|
109
|
+
deps.randomBytes.create(16) as OwnerWriteKey;
|
|
108
110
|
|
|
109
111
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
* subsets. Not intended for sharing outside the app.
|
|
112
|
+
* Creates an {@link Owner} from a {@link OwnerSecret} using SLIP-21 key
|
|
113
|
+
* derivation.
|
|
114
|
+
*
|
|
115
|
+
* This is an internal helper function, use:
|
|
115
116
|
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
117
|
+
* - {@link createAppOwner}
|
|
118
|
+
* - {@link createShardOwner}
|
|
119
|
+
* - {@link createSharedOwner}
|
|
120
|
+
* - {@link createSharedReadonlyOwner}
|
|
118
121
|
*/
|
|
119
|
-
export
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
122
|
+
export const createOwner = (secret: OwnerSecret): Owner => ({
|
|
123
|
+
id: ownerIdBytesToOwnerId(
|
|
124
|
+
OwnerIdBytes.orThrow(
|
|
125
|
+
createSlip21(secret, ["Evolu", "OwnerIdBytes"]).slice(0, 16),
|
|
126
|
+
),
|
|
127
|
+
),
|
|
128
|
+
|
|
129
|
+
encryptionKey: OwnerEncryptionKey.orThrow(
|
|
130
|
+
createSlip21(secret, ["Evolu", "OwnerEncryptionKey"]),
|
|
131
|
+
),
|
|
132
|
+
|
|
133
|
+
writeKey: OwnerWriteKey.orThrow(
|
|
134
|
+
createSlip21(secret, ["Evolu", "OwnerWriteKey"]).slice(0, 16),
|
|
135
|
+
),
|
|
136
|
+
});
|
|
124
137
|
|
|
125
138
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
* stored
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
139
|
+
* The AppOwner represents the application owner. It's created using a
|
|
140
|
+
* cryptographically secure random generator or derived from an external source,
|
|
141
|
+
* e.g., mnemonic stored securely in a hardware device.
|
|
142
|
+
*
|
|
143
|
+
* While it's possible to store all application data in AppOwner, the better
|
|
144
|
+
* approach is to use it only for sync coordination. Storing all app data in
|
|
145
|
+
* AppOwner means that data will be stored/synced forever. And that's a problem
|
|
146
|
+
* if we want to provide real data deletion or in-app data migration without
|
|
147
|
+
* data duplication. In local-first apps/distributed systems, we can't delete
|
|
148
|
+
* individual changes, we only mark them as deleted, otherwise sync could not
|
|
149
|
+
* work.
|
|
132
150
|
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
151
|
+
* If we really want to delete data or at least avoid syncing it, we must store
|
|
152
|
+
* it using a different owner than AppOwner, e.g. {@link ShardOwner} or
|
|
153
|
+
* {@link SharedOwner}, and delete that owner. The AppOwner itself must be
|
|
154
|
+
* preserved because it coordinates deletion information across devices. Other
|
|
155
|
+
* devices need to sync the information that an owner was deleted so they can
|
|
156
|
+
* delete their local data as well.
|
|
135
157
|
*/
|
|
136
|
-
export interface
|
|
137
|
-
readonly type: "
|
|
138
|
-
|
|
139
|
-
|
|
158
|
+
export interface AppOwner extends Owner {
|
|
159
|
+
readonly type: "AppOwner";
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The mnemonic that was used to derive the AppOwner keys. Optional when the
|
|
163
|
+
* AppOwner is created from external keys to avoid sharing the mnemonic with
|
|
164
|
+
* the Evolu app.
|
|
165
|
+
*/
|
|
166
|
+
readonly mnemonic?: Mnemonic | null;
|
|
140
167
|
}
|
|
141
168
|
|
|
142
|
-
/**
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
169
|
+
/** Creates an {@link AppOwner} from an {@link OwnerSecret}. */
|
|
170
|
+
export const createAppOwner = (secret: OwnerSecret): AppOwner => ({
|
|
171
|
+
type: "AppOwner",
|
|
172
|
+
mnemonic: ownerSecretToMnemonic(secret),
|
|
173
|
+
...createOwner(secret),
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// DEV: Future transports: Bluetooth, LocalNetwork, etc.
|
|
177
|
+
export type TransportConfig = WebSocketTransportConfig;
|
|
178
|
+
|
|
179
|
+
export interface WebSocketTransportConfig {
|
|
180
|
+
readonly type: "WebSocket";
|
|
181
|
+
readonly url: string;
|
|
152
182
|
}
|
|
153
183
|
|
|
154
184
|
/**
|
|
155
|
-
*
|
|
156
|
-
*
|
|
185
|
+
* An {@link Owner} for sharding data.
|
|
186
|
+
*
|
|
187
|
+
* ShardOwners are the recommended storage location for most application data
|
|
188
|
+
* because they can be completely deleted (both on relays and devices) and
|
|
189
|
+
* conditionally synced.
|
|
190
|
+
*
|
|
191
|
+
* Can be created from {@link OwnerSecret} via {@link createShardOwner} or
|
|
192
|
+
* deterministically derived from {@link AppOwner} using
|
|
193
|
+
* {@link deriveShardOwner}.
|
|
157
194
|
*/
|
|
158
|
-
export
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return { type: "AppOwner", ...owner };
|
|
163
|
-
};
|
|
195
|
+
export interface ShardOwner extends Owner {
|
|
196
|
+
readonly type: "ShardOwner";
|
|
197
|
+
readonly transports?: ReadonlyArray<TransportConfig>;
|
|
198
|
+
}
|
|
164
199
|
|
|
165
|
-
/**
|
|
166
|
-
* Creates a {@link ShardOwner} for sharding app data with a freshly generated
|
|
167
|
-
* mnemonic. Unlike {@link createAppOwner}, it doesn’t accept an existing
|
|
168
|
-
* mnemonic because ShardOwner mnemonics are always generated and restored
|
|
169
|
-
* automatically via database sync.
|
|
170
|
-
*/
|
|
200
|
+
/** Creates a {@link ShardOwner} from an {@link OwnerSecret}. */
|
|
171
201
|
export const createShardOwner = (
|
|
172
|
-
|
|
202
|
+
secret: OwnerSecret,
|
|
203
|
+
transports?: ReadonlyArray<TransportConfig>,
|
|
173
204
|
): ShardOwner => {
|
|
174
|
-
const owner = createOwner(deps)();
|
|
175
205
|
return {
|
|
176
206
|
type: "ShardOwner",
|
|
177
|
-
|
|
178
|
-
|
|
207
|
+
...createOwner(secret),
|
|
208
|
+
...(transports && { transports }),
|
|
179
209
|
};
|
|
180
210
|
};
|
|
181
211
|
|
|
182
212
|
/**
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
213
|
+
* Derives a {@link ShardOwner} from an {@link AppOwner} using the specified path.
|
|
214
|
+
*
|
|
215
|
+
* **Advantages of derived owners:**
|
|
216
|
+
*
|
|
217
|
+
* - **Deterministic**: Same path always produces the same ShardOwner across all
|
|
218
|
+
* devices
|
|
219
|
+
* - **Immediate availability**: Can be hardcoded and used before sync occurs
|
|
220
|
+
* - **Consistent setup**: All devices start with identical data structure
|
|
221
|
+
* - **Lifecycle management**: Can implement epoch patterns for clean data
|
|
222
|
+
* deletion and recreation
|
|
223
|
+
*
|
|
224
|
+
* **Common patterns:**
|
|
225
|
+
*
|
|
226
|
+
* - Use paths like `["shard", 1]` for versioned data lifecycle
|
|
227
|
+
* - Use paths like `["project", "MyApp", 1]` for named partitions with versions
|
|
228
|
+
* - Each device can derive the same owners and set up initial structure
|
|
187
229
|
*/
|
|
188
|
-
export const
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
type: "SharedOwner",
|
|
195
|
-
mnemonic,
|
|
196
|
-
writeKey,
|
|
197
|
-
};
|
|
198
|
-
};
|
|
230
|
+
export const deriveShardOwner = (
|
|
231
|
+
owner: AppOwner,
|
|
232
|
+
path: NonEmptyReadonlyArray<string | number>,
|
|
233
|
+
transports?: ReadonlyArray<TransportConfig>,
|
|
234
|
+
): ShardOwner => {
|
|
235
|
+
const secret = createSlip21(owner.encryptionKey, path) as OwnerSecret;
|
|
199
236
|
|
|
200
|
-
/**
|
|
201
|
-
* Creates a {@link SharedReadonlyOwner} from a {@link SharedOwner} for read-only
|
|
202
|
-
* data sharing. Extracts the `mnemonic` from the provided {@link SharedOwner},
|
|
203
|
-
* omitting its `writeKey` to ensure read-only access.
|
|
204
|
-
*/
|
|
205
|
-
export const createSharedReadonlyOwner = (
|
|
206
|
-
sharedOwner: SharedOwner,
|
|
207
|
-
): SharedReadonlyOwner => {
|
|
208
237
|
return {
|
|
209
|
-
type: "
|
|
210
|
-
|
|
238
|
+
type: "ShardOwner",
|
|
239
|
+
...createOwner(secret),
|
|
240
|
+
...(transports && { transports }),
|
|
211
241
|
};
|
|
212
242
|
};
|
|
213
243
|
|
|
214
|
-
/**
|
|
215
|
-
export
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const id = createSlip21Id(seed, ["Evolu", "Owner Id"]) as OwnerId;
|
|
221
|
-
const encryptionKey = createEncryptionKey(seed);
|
|
222
|
-
|
|
223
|
-
const createdAt = DateIso.fromParent(new Date(deps.time.now()));
|
|
224
|
-
assert(createdAt.ok, "Invalid DateIso: bad system clock");
|
|
225
|
-
|
|
226
|
-
return {
|
|
227
|
-
mnemonic,
|
|
228
|
-
createdAt: createdAt.value,
|
|
229
|
-
id,
|
|
230
|
-
encryptionKey,
|
|
231
|
-
writeKey: writeKey ?? createWriteKey(deps)(seed),
|
|
232
|
-
};
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
export const createWriteKey =
|
|
236
|
-
(deps: CreateRandomBytesDep) =>
|
|
237
|
-
(seed?: MnemonicSeed): WriteKey => {
|
|
238
|
-
const key = seed
|
|
239
|
-
? createSlip21(seed, ["Evolu", "Write Key"]).slice(0, 16)
|
|
240
|
-
: deps.createRandomBytes(16);
|
|
241
|
-
|
|
242
|
-
const writeKey = WriteKey.from(key);
|
|
243
|
-
assert(writeKey.ok, "Ensure valid WriteKey");
|
|
244
|
-
|
|
245
|
-
return writeKey.value;
|
|
246
|
-
};
|
|
244
|
+
/** An {@link Owner} for collaborative data with write access. */
|
|
245
|
+
export interface SharedOwner extends Owner {
|
|
246
|
+
readonly type: "SharedOwner";
|
|
247
|
+
readonly transports?: ReadonlyArray<TransportConfig>;
|
|
248
|
+
}
|
|
247
249
|
|
|
248
250
|
/**
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
* {@link
|
|
251
|
+
* Creates a {@link SharedOwner} from an {@link OwnerSecret} for collaborative
|
|
252
|
+
* write access.
|
|
253
|
+
*
|
|
254
|
+
* Use {@link createSharedReadonlyOwner} to create a read-only version for
|
|
255
|
+
* sharing.
|
|
253
256
|
*/
|
|
254
|
-
export
|
|
255
|
-
|
|
256
|
-
|
|
257
|
+
export const createSharedOwner = (
|
|
258
|
+
secret: OwnerSecret,
|
|
259
|
+
transports?: ReadonlyArray<TransportConfig>,
|
|
260
|
+
): SharedOwner => {
|
|
261
|
+
return {
|
|
262
|
+
type: "SharedOwner",
|
|
263
|
+
...createOwner(secret),
|
|
264
|
+
...(transports && { transports }),
|
|
265
|
+
};
|
|
257
266
|
};
|
|
258
267
|
|
|
259
268
|
/**
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
269
|
+
* Read-only version of a {@link SharedOwner} for data sharing. Contains only the
|
|
270
|
+
* {@link OwnerId} and {@link EncryptionKey} needed for others to read the shared
|
|
271
|
+
* data without write access.
|
|
263
272
|
*/
|
|
264
|
-
export
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
switch (owner.type) {
|
|
271
|
-
case "AppOwner": {
|
|
272
|
-
const { type, ...rest } = owner;
|
|
273
|
-
return { ...rest, timestamp };
|
|
274
|
-
}
|
|
275
|
-
case "ShardOwner":
|
|
276
|
-
case "SharedOwner":
|
|
277
|
-
return {
|
|
278
|
-
...createOwner(deps)(owner.mnemonic, owner.writeKey),
|
|
279
|
-
timestamp,
|
|
280
|
-
};
|
|
281
|
-
case "SharedReadonlyOwner":
|
|
282
|
-
return {
|
|
283
|
-
...createOwner(deps)(owner.mnemonic),
|
|
284
|
-
writeKey: null,
|
|
285
|
-
timestamp,
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
};
|
|
273
|
+
export interface SharedReadonlyOwner {
|
|
274
|
+
readonly type: "SharedReadonlyOwner";
|
|
275
|
+
readonly id: OwnerId;
|
|
276
|
+
readonly encryptionKey: EncryptionKey;
|
|
277
|
+
readonly transports?: ReadonlyArray<TransportConfig>;
|
|
278
|
+
}
|
|
289
279
|
|
|
290
|
-
/**
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
...owner,
|
|
300
|
-
writeKey: newWriteKey,
|
|
301
|
-
} as T;
|
|
302
|
-
};
|
|
280
|
+
/** Creates a {@link SharedReadonlyOwner} from a {@link SharedOwner}. */
|
|
281
|
+
export const createSharedReadonlyOwner = (
|
|
282
|
+
sharedOwner: SharedOwner,
|
|
283
|
+
): SharedReadonlyOwner => ({
|
|
284
|
+
type: "SharedReadonlyOwner",
|
|
285
|
+
id: sharedOwner.id,
|
|
286
|
+
encryptionKey: sharedOwner.encryptionKey,
|
|
287
|
+
...(sharedOwner.transports && { transports: sharedOwner.transports }),
|
|
288
|
+
});
|
package/src/Evolu/Platform.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { Config } from "./Config.js";
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* FlushSync is for libraries like React to flush updates synchronously inside
|
|
5
3
|
* the provided callback to ensure the DOM is updated immediately.
|
|
@@ -16,12 +14,14 @@ export interface FlushSyncDep {
|
|
|
16
14
|
readonly flushSync: FlushSync;
|
|
17
15
|
}
|
|
18
16
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Reload the app in a platform-specific way.
|
|
19
|
+
*
|
|
20
|
+
* - **Web**: Redirects to the specified URL
|
|
21
|
+
* - **React Native**: Restarts the app (URL parameter ignored)
|
|
22
|
+
*/
|
|
23
|
+
export type ReloadApp = (url: string) => void;
|
|
24
24
|
|
|
25
|
-
export interface
|
|
26
|
-
readonly
|
|
25
|
+
export interface ReloadAppDep {
|
|
26
|
+
readonly reloadApp: ReloadApp;
|
|
27
27
|
}
|