@evolu/common 6.0.1-preview.25 → 6.0.1-preview.27
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 +0 -13
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +0 -15
- package/dist/src/Cache.d.ts +44 -0
- package/dist/src/Cache.d.ts.map +1 -0
- package/dist/src/Cache.js +52 -0
- package/dist/src/Evolu/Db.d.ts +6 -6
- package/dist/src/Evolu/Db.d.ts.map +1 -1
- package/dist/src/Evolu/Db.js +4 -0
- package/dist/src/Evolu/LocalAuth.d.ts +4 -6
- package/dist/src/Evolu/LocalAuth.d.ts.map +1 -1
- package/dist/src/Evolu/LocalAuth.js +14 -15
- package/dist/src/Evolu/Owner.d.ts +116 -86
- package/dist/src/Evolu/Owner.d.ts.map +1 -1
- package/dist/src/Evolu/Owner.js +48 -45
- package/dist/src/Evolu/Relay.d.ts +6 -5
- package/dist/src/Evolu/Relay.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.js +40 -39
- package/dist/src/Evolu/Storage.d.ts +6 -11
- package/dist/src/Evolu/Storage.d.ts.map +1 -1
- package/dist/src/Evolu/Storage.js +30 -9
- package/dist/src/Evolu/Sync.d.ts +5 -5
- package/dist/src/Evolu/Sync.d.ts.map +1 -1
- package/dist/src/Evolu/Sync.js +3 -5
- package/dist/src/Evolu/Timestamp.d.ts +24 -0
- package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
- package/dist/src/Evolu/Timestamp.js +24 -0
- package/dist/src/Identicon.d.ts +35 -0
- package/dist/src/Identicon.d.ts.map +1 -0
- package/dist/src/Identicon.js +143 -0
- package/dist/src/ManyToManyMap.d.ts +0 -3
- package/dist/src/ManyToManyMap.d.ts.map +1 -1
- package/dist/src/Platform.d.ts +20 -0
- package/dist/src/Platform.d.ts.map +1 -0
- package/dist/src/Platform.js +22 -0
- package/dist/src/Result.d.ts +13 -6
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Sqlite.d.ts +36 -1
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +56 -3
- package/dist/src/Task.d.ts.map +1 -1
- package/dist/src/Task.js +36 -0
- package/dist/src/Type.d.ts +5 -8
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +11 -11
- package/dist/src/Types.d.ts +1 -1
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +2 -7
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -0
- package/package.json +1 -1
- package/src/Assert.ts +0 -19
- package/src/Cache.ts +85 -0
- package/src/Evolu/Db.ts +11 -7
- package/src/Evolu/LocalAuth.ts +35 -22
- package/src/Evolu/Owner.ts +140 -103
- package/src/Evolu/Relay.ts +52 -48
- package/src/Evolu/Storage.ts +47 -23
- package/src/Evolu/Sync.ts +11 -12
- package/src/Evolu/Timestamp.ts +24 -0
- package/src/Identicon.ts +197 -0
- package/src/ManyToManyMap.ts +0 -3
- package/src/Platform.ts +26 -0
- package/src/Result.ts +13 -6
- package/src/Sqlite.ts +68 -5
- package/src/Task.ts +41 -0
- package/src/Type.ts +12 -15
- package/src/Types.ts +1 -1
- package/src/WebSocket.ts +6 -10
- package/src/index.ts +3 -0
package/src/Evolu/LocalAuth.ts
CHANGED
|
@@ -10,9 +10,9 @@ import {
|
|
|
10
10
|
OwnerWriteKey,
|
|
11
11
|
} from "./Owner.js";
|
|
12
12
|
|
|
13
|
-
/**
|
|
14
|
-
* Local authentication and authorization system for Evolu.
|
|
15
|
-
*
|
|
13
|
+
/**
|
|
14
|
+
* Local authentication and authorization system for Evolu. This is API is
|
|
15
|
+
* subject to change and not recommended for production use.
|
|
16
16
|
*
|
|
17
17
|
* @experimental
|
|
18
18
|
*/
|
|
@@ -84,7 +84,7 @@ export const createLocalAuth = (
|
|
|
84
84
|
id: OwnerId,
|
|
85
85
|
options?: LocalAuthOptions,
|
|
86
86
|
): Promise<void> => {
|
|
87
|
-
await deps.secureStorage.setItem(
|
|
87
|
+
await deps.secureStorage.setItem(localAuthMetakeyLastOwner, id, {
|
|
88
88
|
...buildAuthOptions(options),
|
|
89
89
|
accessControl: "none",
|
|
90
90
|
});
|
|
@@ -93,7 +93,7 @@ export const createLocalAuth = (
|
|
|
93
93
|
const getLastOwnerId = async (
|
|
94
94
|
options?: LocalAuthOptions,
|
|
95
95
|
): Promise<OwnerId | undefined> => {
|
|
96
|
-
const item = await deps.secureStorage.getItem(
|
|
96
|
+
const item = await deps.secureStorage.getItem(localAuthMetakeyLastOwner, {
|
|
97
97
|
...buildAuthOptions(options),
|
|
98
98
|
accessControl: "none",
|
|
99
99
|
});
|
|
@@ -103,7 +103,7 @@ export const createLocalAuth = (
|
|
|
103
103
|
const getOwnerNames = async (
|
|
104
104
|
options?: LocalAuthOptions,
|
|
105
105
|
): Promise<Record<OwnerId, string>> => {
|
|
106
|
-
const item = await deps.secureStorage.getItem(
|
|
106
|
+
const item = await deps.secureStorage.getItem(localAuthMetakeyOwnerNames, {
|
|
107
107
|
...buildAuthOptions(options),
|
|
108
108
|
accessControl: "none",
|
|
109
109
|
});
|
|
@@ -122,7 +122,7 @@ export const createLocalAuth = (
|
|
|
122
122
|
const names = await getOwnerNames(options);
|
|
123
123
|
names[id] = username;
|
|
124
124
|
await deps.secureStorage.setItem(
|
|
125
|
-
|
|
125
|
+
localAuthMetakeyOwnerNames,
|
|
126
126
|
JSON.stringify(names),
|
|
127
127
|
{
|
|
128
128
|
...buildAuthOptions(options),
|
|
@@ -137,7 +137,7 @@ export const createLocalAuth = (
|
|
|
137
137
|
): Promise<void> => {
|
|
138
138
|
const { [id]: _, ...names } = await getOwnerNames(options);
|
|
139
139
|
await deps.secureStorage.setItem(
|
|
140
|
-
|
|
140
|
+
localAuthMetakeyOwnerNames,
|
|
141
141
|
JSON.stringify(names),
|
|
142
142
|
{
|
|
143
143
|
...buildAuthOptions(options),
|
|
@@ -157,8 +157,8 @@ export const createLocalAuth = (
|
|
|
157
157
|
.filter(Boolean)
|
|
158
158
|
.filter(
|
|
159
159
|
(i) =>
|
|
160
|
-
i.key !==
|
|
161
|
-
i.key !==
|
|
160
|
+
i.key !== localAuthMetakeyLastOwner &&
|
|
161
|
+
i.key !== localAuthMetakeyOwnerNames,
|
|
162
162
|
)
|
|
163
163
|
.map((i) => i.key as OwnerId);
|
|
164
164
|
};
|
|
@@ -171,17 +171,29 @@ export const createLocalAuth = (
|
|
|
171
171
|
username?: string,
|
|
172
172
|
): LocalAuthOptions => {
|
|
173
173
|
const newOptions: LocalAuthOptions = {
|
|
174
|
-
...
|
|
174
|
+
...localAuthDefaultOptions,
|
|
175
175
|
...(username && { webAuthnUsername: username }),
|
|
176
176
|
...options,
|
|
177
177
|
};
|
|
178
178
|
return {
|
|
179
179
|
...newOptions,
|
|
180
180
|
authenticationPrompt: {
|
|
181
|
-
title: replaceMessageTokens(
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
181
|
+
title: replaceMessageTokens(
|
|
182
|
+
newOptions.authenticationPrompt?.title ?? "",
|
|
183
|
+
username,
|
|
184
|
+
),
|
|
185
|
+
cancel: replaceMessageTokens(
|
|
186
|
+
newOptions.authenticationPrompt?.cancel ?? "",
|
|
187
|
+
username,
|
|
188
|
+
),
|
|
189
|
+
subtitle: replaceMessageTokens(
|
|
190
|
+
newOptions.authenticationPrompt?.subtitle ?? "",
|
|
191
|
+
username,
|
|
192
|
+
),
|
|
193
|
+
description: replaceMessageTokens(
|
|
194
|
+
newOptions.authenticationPrompt?.description ?? "",
|
|
195
|
+
username,
|
|
196
|
+
),
|
|
185
197
|
},
|
|
186
198
|
};
|
|
187
199
|
};
|
|
@@ -299,13 +311,11 @@ export const createLocalAuth = (
|
|
|
299
311
|
};
|
|
300
312
|
};
|
|
301
313
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
export const
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
service: AUTH_NAMESPACE,
|
|
308
|
-
keychainGroup: AUTH_NAMESPACE,
|
|
314
|
+
export const localAuth_Namespace = "evolu";
|
|
315
|
+
|
|
316
|
+
export const localAuthDefaultOptions: LocalAuthOptions = {
|
|
317
|
+
service: localAuth_Namespace,
|
|
318
|
+
keychainGroup: localAuth_Namespace,
|
|
309
319
|
androidBiometricsStrongOnly: true,
|
|
310
320
|
iosSynchronizable: true,
|
|
311
321
|
webAuthnUsername: "Evolu User",
|
|
@@ -314,6 +324,9 @@ export const AUTH_DEFAULT_OPTIONS: LocalAuthOptions = {
|
|
|
314
324
|
},
|
|
315
325
|
};
|
|
316
326
|
|
|
327
|
+
const localAuthMetakeyLastOwner = "_last_owner";
|
|
328
|
+
const localAuthMetakeyOwnerNames = "_owner_names";
|
|
329
|
+
|
|
317
330
|
export interface AuthResult {
|
|
318
331
|
/** The app owner created during registration. */
|
|
319
332
|
readonly owner: AppOwner | undefined;
|
package/src/Evolu/Owner.ts
CHANGED
|
@@ -16,29 +16,12 @@ import {
|
|
|
16
16
|
idToIdBytes,
|
|
17
17
|
Mnemonic,
|
|
18
18
|
NonNegativeInt,
|
|
19
|
+
PositiveInt,
|
|
19
20
|
} from "../Type.js";
|
|
20
21
|
import { getOrNull } from "../Result.js";
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
*
|
|
25
|
-
* Can be created using {@link createOwnerSecret} or converted from a
|
|
26
|
-
* {@link Mnemonic} using {@link mnemonicToOwnerSecret}.
|
|
27
|
-
*/
|
|
28
|
-
export const OwnerSecret = brand("OwnerSecret", Entropy32);
|
|
29
|
-
export type OwnerSecret = typeof OwnerSecret.Type;
|
|
30
|
-
|
|
31
|
-
/** Creates a {@link OwnerSecret}. */
|
|
32
|
-
export const createOwnerSecret = (deps: RandomBytesDep): OwnerSecret =>
|
|
33
|
-
deps.randomBytes.create(32) as OwnerSecret;
|
|
34
|
-
|
|
35
|
-
/** Converts an {@link OwnerSecret} to a {@link Mnemonic}. */
|
|
36
|
-
export const ownerSecretToMnemonic = (secret: OwnerSecret): Mnemonic =>
|
|
37
|
-
bip39.entropyToMnemonic(secret, wordlist) as Mnemonic;
|
|
38
|
-
|
|
39
|
-
/** Converts a {@link Mnemonic} to an {@link OwnerSecret}. */
|
|
40
|
-
export const mnemonicToOwnerSecret = (mnemonic: Mnemonic): OwnerSecret =>
|
|
41
|
-
bip39.mnemonicToEntropy(mnemonic, wordlist) as OwnerSecret;
|
|
22
|
+
import type { Timestamp } from "./Timestamp.js";
|
|
23
|
+
import { TimestampBytes } from "./Timestamp.js";
|
|
24
|
+
import type { Storage } from "./Storage.js";
|
|
42
25
|
|
|
43
26
|
/**
|
|
44
27
|
* The Owner represents ownership of data in Evolu. Every database change is
|
|
@@ -105,6 +88,27 @@ export type OwnerEncryptionKey = typeof OwnerEncryptionKey.Type;
|
|
|
105
88
|
export const OwnerWriteKey = brand("OwnerWriteKey", Entropy16);
|
|
106
89
|
export type OwnerWriteKey = typeof OwnerWriteKey.Type;
|
|
107
90
|
|
|
91
|
+
/**
|
|
92
|
+
* 32 bytes of cryptographic entropy used to derive {@link Owner} keys.
|
|
93
|
+
*
|
|
94
|
+
* Can be created using {@link createOwnerSecret} or converted from a
|
|
95
|
+
* {@link Mnemonic} using {@link mnemonicToOwnerSecret}.
|
|
96
|
+
*/
|
|
97
|
+
export const OwnerSecret = brand("OwnerSecret", Entropy32);
|
|
98
|
+
export type OwnerSecret = typeof OwnerSecret.Type;
|
|
99
|
+
|
|
100
|
+
/** Creates a {@link OwnerSecret}. */
|
|
101
|
+
export const createOwnerSecret = (deps: RandomBytesDep): OwnerSecret =>
|
|
102
|
+
deps.randomBytes.create(32) as OwnerSecret;
|
|
103
|
+
|
|
104
|
+
/** Converts an {@link OwnerSecret} to a {@link Mnemonic}. */
|
|
105
|
+
export const ownerSecretToMnemonic = (secret: OwnerSecret): Mnemonic =>
|
|
106
|
+
bip39.entropyToMnemonic(secret, wordlist) as Mnemonic;
|
|
107
|
+
|
|
108
|
+
/** Converts a {@link Mnemonic} to an {@link OwnerSecret}. */
|
|
109
|
+
export const mnemonicToOwnerSecret = (mnemonic: Mnemonic): OwnerSecret =>
|
|
110
|
+
bip39.mnemonicToEntropy(mnemonic, wordlist) as OwnerSecret;
|
|
111
|
+
|
|
108
112
|
/** Creates a randomly generated {@link OwnerWriteKey}. */
|
|
109
113
|
export const createOwnerWriteKey = (deps: RandomBytesDep): OwnerWriteKey =>
|
|
110
114
|
deps.randomBytes.create(16) as OwnerWriteKey;
|
|
@@ -174,82 +178,6 @@ export const createAppOwner = (secret: OwnerSecret): AppOwner => ({
|
|
|
174
178
|
...createOwner(secret),
|
|
175
179
|
});
|
|
176
180
|
|
|
177
|
-
/**
|
|
178
|
-
* Transport configuration for connecting to relays.
|
|
179
|
-
*
|
|
180
|
-
* Each {@link Owner} can specify one or more transports to connect to different
|
|
181
|
-
* relays for data synchronization. Currently supports WebSocket transport, with
|
|
182
|
-
* future support planned for Bluetooth, LocalNetwork, and other protocols.
|
|
183
|
-
*/
|
|
184
|
-
export type TransportConfig = WebSocketTransportConfig;
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* WebSocket transport configuration for relay connections.
|
|
188
|
-
*
|
|
189
|
-
* Use {@link createWebSocketTransportConfig} to create a properly formatted URL
|
|
190
|
-
* with {@link OwnerId}. The relay uses {@link parseOwnerIdFromUrl} to extract the
|
|
191
|
-
* OwnerId from the query string.
|
|
192
|
-
*
|
|
193
|
-
* ### Authentication and Error Handling
|
|
194
|
-
*
|
|
195
|
-
* When a relay rejects a connection (invalid OwnerId, unauthorized owner, or
|
|
196
|
-
* server error), the browser WebSocket API does not expose the specific HTTP
|
|
197
|
-
* status code or reason - it only reports a generic connection failure. The
|
|
198
|
-
* client automatically retries with exponential backoff and jitter, eventually
|
|
199
|
-
* succeeding once the configuration or server issue is resolved.
|
|
200
|
-
*
|
|
201
|
-
* Legitimate clients will be properly configured with valid credentials, so
|
|
202
|
-
* automatic retry is appropriate.
|
|
203
|
-
*
|
|
204
|
-
* @see {@link createWebSocketTransportConfig}
|
|
205
|
-
* @see {@link parseOwnerIdFromUrl}
|
|
206
|
-
*/
|
|
207
|
-
export interface WebSocketTransportConfig {
|
|
208
|
-
readonly type: "WebSocket";
|
|
209
|
-
readonly url: string;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Creates a {@link WebSocketTransportConfig} for the given relay URL and
|
|
214
|
-
* {@link OwnerId}.
|
|
215
|
-
*
|
|
216
|
-
* ### Example
|
|
217
|
-
*
|
|
218
|
-
* ```ts
|
|
219
|
-
* const transport = createWebSocketTransportConfig({
|
|
220
|
-
* relayUrl: "wss://relay.evolu.dev",
|
|
221
|
-
* ownerId: owner.id,
|
|
222
|
-
* });
|
|
223
|
-
* // Result: { type: "WebSocket", url: "wss://relay.evolu.dev?ownerId=..." }
|
|
224
|
-
* ```
|
|
225
|
-
*/
|
|
226
|
-
export const createWebSocketTransportConfig = ({
|
|
227
|
-
relayUrl,
|
|
228
|
-
ownerId,
|
|
229
|
-
}: {
|
|
230
|
-
readonly relayUrl: string;
|
|
231
|
-
readonly ownerId: OwnerId;
|
|
232
|
-
}): WebSocketTransportConfig => ({
|
|
233
|
-
type: "WebSocket",
|
|
234
|
-
url: `${relayUrl}?ownerId=${ownerId}`,
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Extracts {@link OwnerId} from a URL query string.
|
|
239
|
-
*
|
|
240
|
-
* Parses the query string `?ownerId=...` and validates that the extracted value
|
|
241
|
-
* is a valid {@link OwnerId}.
|
|
242
|
-
*
|
|
243
|
-
* ### Example
|
|
244
|
-
*
|
|
245
|
-
* ```ts
|
|
246
|
-
* parseOwnerIdFromUrl("/sync?ownerId=_12345678abcdefgh");
|
|
247
|
-
* // Returns: OwnerId or null
|
|
248
|
-
* ```
|
|
249
|
-
*/
|
|
250
|
-
export const parseOwnerIdFromUrl = (url: string | undefined): OwnerId | null =>
|
|
251
|
-
getOrNull(OwnerId.fromUnknown(url?.split("=")[1]));
|
|
252
|
-
|
|
253
181
|
/**
|
|
254
182
|
* An {@link Owner} for sharding data.
|
|
255
183
|
*
|
|
@@ -263,13 +191,13 @@ export const parseOwnerIdFromUrl = (url: string | undefined): OwnerId | null =>
|
|
|
263
191
|
*/
|
|
264
192
|
export interface ShardOwner extends Owner {
|
|
265
193
|
readonly type: "ShardOwner";
|
|
266
|
-
readonly transports?: ReadonlyArray<
|
|
194
|
+
readonly transports?: ReadonlyArray<OwnerTransport>;
|
|
267
195
|
}
|
|
268
196
|
|
|
269
197
|
/** Creates a {@link ShardOwner} from an {@link OwnerSecret}. */
|
|
270
198
|
export const createShardOwner = (
|
|
271
199
|
secret: OwnerSecret,
|
|
272
|
-
transports?: ReadonlyArray<
|
|
200
|
+
transports?: ReadonlyArray<OwnerTransport>,
|
|
273
201
|
): ShardOwner => {
|
|
274
202
|
return {
|
|
275
203
|
type: "ShardOwner",
|
|
@@ -299,7 +227,7 @@ export const createShardOwner = (
|
|
|
299
227
|
export const deriveShardOwner = (
|
|
300
228
|
owner: AppOwner,
|
|
301
229
|
path: NonEmptyReadonlyArray<string | number>,
|
|
302
|
-
transports?: ReadonlyArray<
|
|
230
|
+
transports?: ReadonlyArray<OwnerTransport>,
|
|
303
231
|
): ShardOwner => {
|
|
304
232
|
const secret = createSlip21(owner.encryptionKey, path) as OwnerSecret;
|
|
305
233
|
|
|
@@ -313,7 +241,7 @@ export const deriveShardOwner = (
|
|
|
313
241
|
/** An {@link Owner} for collaborative data with write access. */
|
|
314
242
|
export interface SharedOwner extends Owner {
|
|
315
243
|
readonly type: "SharedOwner";
|
|
316
|
-
readonly transports?: ReadonlyArray<
|
|
244
|
+
readonly transports?: ReadonlyArray<OwnerTransport>;
|
|
317
245
|
}
|
|
318
246
|
|
|
319
247
|
/**
|
|
@@ -325,7 +253,7 @@ export interface SharedOwner extends Owner {
|
|
|
325
253
|
*/
|
|
326
254
|
export const createSharedOwner = (
|
|
327
255
|
secret: OwnerSecret,
|
|
328
|
-
transports?: ReadonlyArray<
|
|
256
|
+
transports?: ReadonlyArray<OwnerTransport>,
|
|
329
257
|
): SharedOwner => {
|
|
330
258
|
return {
|
|
331
259
|
type: "SharedOwner",
|
|
@@ -343,7 +271,7 @@ export interface SharedReadonlyOwner {
|
|
|
343
271
|
readonly type: "SharedReadonlyOwner";
|
|
344
272
|
readonly id: OwnerId;
|
|
345
273
|
readonly encryptionKey: EncryptionKey;
|
|
346
|
-
readonly transports?: ReadonlyArray<
|
|
274
|
+
readonly transports?: ReadonlyArray<OwnerTransport>;
|
|
347
275
|
}
|
|
348
276
|
|
|
349
277
|
/** Creates a {@link SharedReadonlyOwner} from a {@link SharedOwner}. */
|
|
@@ -355,3 +283,112 @@ export const createSharedReadonlyOwner = (
|
|
|
355
283
|
encryptionKey: sharedOwner.encryptionKey,
|
|
356
284
|
...(sharedOwner.transports && { transports: sharedOwner.transports }),
|
|
357
285
|
});
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Transport configuration for connecting to relays.
|
|
289
|
+
*
|
|
290
|
+
* Currently only WebSocket, in the future Bluetooth, LocalNetwork, etc.
|
|
291
|
+
*/
|
|
292
|
+
export type OwnerTransport = OwnerWebSocketTransport;
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* WebSocket transport configuration.
|
|
296
|
+
*
|
|
297
|
+
* ### Authentication and Error Handling
|
|
298
|
+
*
|
|
299
|
+
* When a relay rejects a connection (invalid OwnerId, unauthorized owner, or
|
|
300
|
+
* server error), the browser WebSocket API does not expose the specific HTTP
|
|
301
|
+
* status code or reason - it only reports a generic connection failure. The
|
|
302
|
+
* client automatically retries with exponential backoff and jitter, eventually
|
|
303
|
+
* succeeding once the configuration or server issue is resolved.
|
|
304
|
+
*
|
|
305
|
+
* Legitimate clients will be properly configured with valid credentials, so
|
|
306
|
+
* automatic retry is OK.
|
|
307
|
+
*
|
|
308
|
+
* @see {@link createOwnerWebSocketTransport}
|
|
309
|
+
* @see {@link parseOwnerIdFromOwnerWebSocketTransportUrl}
|
|
310
|
+
*/
|
|
311
|
+
export interface OwnerWebSocketTransport {
|
|
312
|
+
readonly type: "WebSocket";
|
|
313
|
+
readonly url: string;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Creates an {@link OwnerWebSocketTransport} for the given relay URL and
|
|
318
|
+
* {@link OwnerId}.
|
|
319
|
+
*
|
|
320
|
+
* ### Example
|
|
321
|
+
*
|
|
322
|
+
* ```ts
|
|
323
|
+
* const transport = createOwnerWebSocketTransport({
|
|
324
|
+
* url: "wss://relay.evolu.dev",
|
|
325
|
+
* ownerId: owner.id,
|
|
326
|
+
* });
|
|
327
|
+
* // Result: { type: "WebSocket", url: "wss://relay.evolu.dev?ownerId=..." }
|
|
328
|
+
* ```
|
|
329
|
+
*/
|
|
330
|
+
export const createOwnerWebSocketTransport = (config: {
|
|
331
|
+
readonly url: string;
|
|
332
|
+
readonly ownerId: OwnerId;
|
|
333
|
+
}): OwnerWebSocketTransport => ({
|
|
334
|
+
type: "WebSocket",
|
|
335
|
+
url: `${config.url}?ownerId=${config.ownerId}`,
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Extracts {@link OwnerId} from an {@link OwnerWebSocketTransport} URL query
|
|
340
|
+
* string.
|
|
341
|
+
*
|
|
342
|
+
* Parses the query string `?ownerId=...` and validates that the extracted value
|
|
343
|
+
* is a valid {@link OwnerId}.
|
|
344
|
+
*
|
|
345
|
+
* ### Example
|
|
346
|
+
*
|
|
347
|
+
* ```ts
|
|
348
|
+
* parseOwnerIdFromOwnerWebSocketTransportUrl(
|
|
349
|
+
* "/sync?ownerId=_12345678abcdefgh",
|
|
350
|
+
* );
|
|
351
|
+
* // Returns: OwnerId or null
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
354
|
+
export const parseOwnerIdFromOwnerWebSocketTransportUrl = (
|
|
355
|
+
url: string,
|
|
356
|
+
): OwnerId | null => getOrNull(OwnerId.fromUnknown(url.split("=")[1]));
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Usage data for an {@link OwnerId}.
|
|
360
|
+
*
|
|
361
|
+
* Tracks data consumption to monitor usage patterns and enforce quotas if
|
|
362
|
+
* needed. Used by both relays and clients.
|
|
363
|
+
*
|
|
364
|
+
* Relays and clients must handle rate limiting, connection limits, and request
|
|
365
|
+
* throttling separately with in-memory state.
|
|
366
|
+
*/
|
|
367
|
+
export interface OwnerUsage {
|
|
368
|
+
/** The {@link Owner} this usage data belongs to. */
|
|
369
|
+
readonly ownerId: OwnerIdBytes;
|
|
370
|
+
|
|
371
|
+
/** Total bytes stored in the database. */
|
|
372
|
+
readonly storedBytes: PositiveInt;
|
|
373
|
+
|
|
374
|
+
/** Total bytes received. */
|
|
375
|
+
readonly receivedBytes: number;
|
|
376
|
+
|
|
377
|
+
/** Total bytes sent. */
|
|
378
|
+
readonly sentBytes: number;
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* The minimum {@link Timestamp}.
|
|
382
|
+
*
|
|
383
|
+
* Helps {@link Storage} choose faster algorithms.
|
|
384
|
+
*/
|
|
385
|
+
readonly firstTimestamp: TimestampBytes | null;
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* The maximum {@link Timestamp}.
|
|
389
|
+
*
|
|
390
|
+
* Helps {@link Storage} choose faster algorithms. Free relays can use it to
|
|
391
|
+
* identify inactive accounts for cleanup or archival.
|
|
392
|
+
*/
|
|
393
|
+
readonly lastTimestamp: TimestampBytes | null;
|
|
394
|
+
}
|
package/src/Evolu/Relay.ts
CHANGED
|
@@ -3,13 +3,13 @@ import { ConsoleConfig, ConsoleDep } from "../Console.js";
|
|
|
3
3
|
import { TimingSafeEqualDep } from "../Crypto.js";
|
|
4
4
|
import { LazyValue } from "../Function.js";
|
|
5
5
|
import { err, ok, Result } from "../Result.js";
|
|
6
|
-
import { sql, SqliteError } from "../Sqlite.js";
|
|
6
|
+
import { sql, SqliteDep, SqliteError } from "../Sqlite.js";
|
|
7
7
|
import { SimpleName } from "../Type.js";
|
|
8
|
-
import { OwnerId,
|
|
8
|
+
import { OwnerId, OwnerTransport, OwnerWriteKey } from "./Owner.js";
|
|
9
9
|
import { ProtocolInvalidDataError } from "./Protocol.js";
|
|
10
10
|
import {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
createBaseSqliteStorage,
|
|
12
|
+
CreateBaseSqliteStorageOptions,
|
|
13
13
|
EncryptedDbChange,
|
|
14
14
|
SqliteStorageDeps,
|
|
15
15
|
Storage,
|
|
@@ -44,9 +44,9 @@ export interface RelayConfig extends ConsoleConfig {
|
|
|
44
44
|
* this only controls relay access, not write permissions. Since all data is
|
|
45
45
|
* encrypted on the relay, OwnerId exposure is safe.
|
|
46
46
|
*
|
|
47
|
-
* Owners specify which relays to connect to via {@link
|
|
47
|
+
* Owners specify which relays to connect to via {@link OwnerTransport}. In
|
|
48
48
|
* WebSocket-based implementations, this check occurs before accepting the
|
|
49
|
-
* connection, with the OwnerId typically extracted from the URL
|
|
49
|
+
* connection, with the OwnerId typically extracted from the URL Path (e.g.,
|
|
50
50
|
* `ws://localhost:4000/<ownerId>`).
|
|
51
51
|
*
|
|
52
52
|
* ### Example
|
|
@@ -68,36 +68,11 @@ export interface RelayConfig extends ConsoleConfig {
|
|
|
68
68
|
|
|
69
69
|
export const createRelaySqliteStorage =
|
|
70
70
|
(deps: SqliteStorageDeps & TimingSafeEqualDep) =>
|
|
71
|
-
(options:
|
|
72
|
-
const sqliteStorageBase =
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
sql`
|
|
77
|
-
create table if not exists evolu_writeKey (
|
|
78
|
-
"ownerId" blob not null,
|
|
79
|
-
"writeKey" blob not null,
|
|
80
|
-
primary key ("ownerId")
|
|
81
|
-
)
|
|
82
|
-
strict;
|
|
83
|
-
`,
|
|
84
|
-
|
|
85
|
-
sql`
|
|
86
|
-
create table if not exists evolu_message (
|
|
87
|
-
"ownerId" blob not null,
|
|
88
|
-
"timestamp" blob not null,
|
|
89
|
-
"change" blob not null,
|
|
90
|
-
primary key ("ownerId", "timestamp")
|
|
91
|
-
)
|
|
92
|
-
strict;
|
|
93
|
-
`,
|
|
94
|
-
]) {
|
|
95
|
-
const result = deps.sqlite.exec(query);
|
|
96
|
-
if (!result.ok) return result;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return ok({
|
|
100
|
-
...sqliteStorageBase.value,
|
|
71
|
+
(options: CreateBaseSqliteStorageOptions): Storage => {
|
|
72
|
+
const sqliteStorageBase = createBaseSqliteStorage(deps)(options);
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
...sqliteStorageBase,
|
|
101
76
|
|
|
102
77
|
/**
|
|
103
78
|
* Lazily authorizes the initiator's {@link OwnerWriteKey} for the given
|
|
@@ -159,11 +134,10 @@ export const createRelaySqliteStorage =
|
|
|
159
134
|
writeMessages: async (ownerId, messages) => {
|
|
160
135
|
const result = deps.sqlite.transaction(() => {
|
|
161
136
|
for (const message of messages) {
|
|
162
|
-
const insertTimestampResult =
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
);
|
|
137
|
+
const insertTimestampResult = sqliteStorageBase.insertTimestamp(
|
|
138
|
+
ownerId,
|
|
139
|
+
timestampToTimestampBytes(message.timestamp),
|
|
140
|
+
);
|
|
167
141
|
if (!insertTimestampResult.ok) return insertTimestampResult;
|
|
168
142
|
|
|
169
143
|
const insertMessage = deps.sqlite.exec(sql`
|
|
@@ -207,18 +181,18 @@ export const createRelaySqliteStorage =
|
|
|
207
181
|
|
|
208
182
|
deleteOwner: (ownerId) => {
|
|
209
183
|
const result = deps.sqlite.transaction(() => {
|
|
210
|
-
const
|
|
184
|
+
const deleteWriteKey = deps.sqlite.exec(sql`
|
|
211
185
|
delete from evolu_writeKey where ownerId = ${ownerId};
|
|
212
186
|
`);
|
|
213
|
-
if (!
|
|
187
|
+
if (!deleteWriteKey.ok) return deleteWriteKey;
|
|
214
188
|
|
|
215
|
-
const
|
|
189
|
+
const deleteMessages = deps.sqlite.exec(sql`
|
|
216
190
|
delete from evolu_message where ownerId = ${ownerId};
|
|
217
191
|
`);
|
|
218
|
-
if (!
|
|
192
|
+
if (!deleteMessages.ok) return deleteMessages;
|
|
219
193
|
|
|
220
|
-
const
|
|
221
|
-
if (!
|
|
194
|
+
const deleteBaseOwner = sqliteStorageBase.deleteOwner(ownerId);
|
|
195
|
+
if (!deleteBaseOwner) return err(null);
|
|
222
196
|
|
|
223
197
|
return ok();
|
|
224
198
|
});
|
|
@@ -228,9 +202,39 @@ export const createRelaySqliteStorage =
|
|
|
228
202
|
}
|
|
229
203
|
return true;
|
|
230
204
|
},
|
|
231
|
-
}
|
|
205
|
+
};
|
|
232
206
|
};
|
|
233
207
|
|
|
208
|
+
export const createRelayStorageTables = (
|
|
209
|
+
deps: SqliteDep,
|
|
210
|
+
): Result<void, SqliteError> => {
|
|
211
|
+
for (const query of [
|
|
212
|
+
sql`
|
|
213
|
+
create table evolu_writeKey (
|
|
214
|
+
"ownerId" blob not null,
|
|
215
|
+
"writeKey" blob not null,
|
|
216
|
+
primary key ("ownerId")
|
|
217
|
+
)
|
|
218
|
+
strict;
|
|
219
|
+
`,
|
|
220
|
+
|
|
221
|
+
sql`
|
|
222
|
+
create table evolu_message (
|
|
223
|
+
"ownerId" blob not null,
|
|
224
|
+
"timestamp" blob not null,
|
|
225
|
+
"change" blob not null,
|
|
226
|
+
primary key ("ownerId", "timestamp")
|
|
227
|
+
)
|
|
228
|
+
strict;
|
|
229
|
+
`,
|
|
230
|
+
]) {
|
|
231
|
+
const result = deps.sqlite.exec(query);
|
|
232
|
+
if (!result.ok) return result;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return ok();
|
|
236
|
+
};
|
|
237
|
+
|
|
234
238
|
export interface RelayLogger {
|
|
235
239
|
readonly started: (enableLogging: boolean, port: number) => void;
|
|
236
240
|
readonly storageError: (error: unknown) => void;
|