@evolu/common 8.0.0-next.4 → 8.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.
- package/dist/src/Array.d.ts +279 -173
- package/dist/src/Array.d.ts.map +1 -1
- package/dist/src/Array.js +105 -101
- package/dist/src/Assert.d.ts +44 -44
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +44 -32
- package/dist/src/BigInt.d.ts +6 -3
- package/dist/src/BigInt.d.ts.map +1 -1
- package/dist/src/BigInt.js +5 -2
- package/dist/src/Brand.d.ts +27 -40
- package/dist/src/Brand.d.ts.map +1 -1
- package/dist/src/Buffer.d.ts +27 -11
- package/dist/src/Buffer.d.ts.map +1 -1
- package/dist/src/Cache.d.ts +9 -4
- package/dist/src/Cache.d.ts.map +1 -1
- package/dist/src/Cache.js +8 -3
- package/dist/src/Callbacks.d.ts +30 -19
- package/dist/src/Callbacks.d.ts.map +1 -1
- package/dist/src/Console.d.ts +145 -66
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +91 -43
- package/dist/src/Crypto.d.ts +85 -42
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +44 -20
- package/dist/src/Eq.d.ts +87 -21
- package/dist/src/Eq.d.ts.map +1 -1
- package/dist/src/Eq.js +67 -19
- package/dist/src/Error.d.ts +5 -3
- package/dist/src/Error.d.ts.map +1 -1
- package/dist/src/Error.js +16 -0
- package/dist/src/Function.d.ts +119 -58
- package/dist/src/Function.d.ts.map +1 -1
- package/dist/src/Function.js +86 -43
- package/dist/src/Http.d.ts +394 -0
- package/dist/src/Http.d.ts.map +1 -0
- package/dist/src/Http.js +102 -0
- package/dist/src/Identicon.d.ts +16 -7
- package/dist/src/Identicon.d.ts.map +1 -1
- package/dist/src/Identicon.js +14 -5
- package/dist/src/LeakDetector.d.ts +88 -0
- package/dist/src/LeakDetector.d.ts.map +1 -0
- package/dist/src/LeakDetector.js +86 -0
- package/dist/src/LockManager.d.ts +18 -9
- package/dist/src/LockManager.d.ts.map +1 -1
- package/dist/src/LockManager.js +7 -8
- package/dist/src/Lookup.d.ts +43 -13
- package/dist/src/Lookup.d.ts.map +1 -1
- package/dist/src/Lookup.js +28 -8
- package/dist/src/Microtask.d.ts +1 -1
- package/dist/src/Microtask.d.ts.map +1 -1
- package/dist/src/Microtask.js +3 -7
- package/dist/src/Number.d.ts +49 -13
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Number.js +34 -5
- package/dist/src/Object.d.ts +160 -40
- package/dist/src/Object.d.ts.map +1 -1
- package/dist/src/Object.js +92 -46
- package/dist/src/Option.d.ts +10 -7
- package/dist/src/Option.d.ts.map +1 -1
- package/dist/src/Option.js +9 -6
- package/dist/src/Order.d.ts +21 -21
- package/dist/src/Order.d.ts.map +1 -1
- package/dist/src/Order.js +18 -21
- package/dist/src/Platform.d.ts +31 -0
- package/dist/src/Platform.d.ts.map +1 -1
- package/dist/src/Platform.js +98 -0
- package/dist/src/Random.d.ts +32 -17
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/Random.js +12 -8
- package/dist/src/Redacted.d.ts +43 -29
- package/dist/src/Redacted.d.ts.map +1 -1
- package/dist/src/Redacted.js +14 -3
- package/dist/src/Ref.d.ts +11 -3
- package/dist/src/Ref.d.ts.map +1 -1
- package/dist/src/Ref.js +2 -2
- package/dist/src/RefCount.d.ts +2 -6
- package/dist/src/RefCount.d.ts.map +1 -1
- package/dist/src/RefCount.js +33 -90
- package/dist/src/Relation.d.ts +70 -5
- package/dist/src/Relation.d.ts.map +1 -1
- package/dist/src/Relation.js +77 -2
- package/dist/src/Resource.d.ts +590 -155
- package/dist/src/Resource.d.ts.map +1 -1
- package/dist/src/Resource.js +592 -343
- package/dist/src/Result.d.ts +838 -191
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +163 -102
- package/dist/src/Schedule.d.ts +684 -231
- package/dist/src/Schedule.d.ts.map +1 -1
- package/dist/src/Schedule.js +766 -295
- package/dist/src/Set.d.ts +89 -40
- package/dist/src/Set.d.ts.map +1 -1
- package/dist/src/Set.js +50 -29
- package/dist/src/Sqlite.d.ts +29 -72
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +41 -46
- package/dist/src/StackTrace.d.ts +40 -0
- package/dist/src/StackTrace.d.ts.map +1 -0
- package/dist/src/StackTrace.js +140 -0
- package/dist/src/Store.d.ts +5 -4
- package/dist/src/Store.d.ts.map +1 -1
- package/dist/src/Store.js +5 -4
- package/dist/src/String.d.ts +7 -1
- package/dist/src/String.d.ts.map +1 -1
- package/dist/src/String.js +20 -7
- package/dist/src/Task.d.ts +3572 -1770
- package/dist/src/Task.d.ts.map +1 -1
- package/dist/src/Task.js +1879 -1274
- package/dist/src/Test.d.ts +17 -125
- package/dist/src/Test.d.ts.map +1 -1
- package/dist/src/Test.js +17 -68
- package/dist/src/Time.d.ts +108 -49
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +172 -73
- package/dist/src/Type.d.ts +3542 -1926
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +3070 -1784
- package/dist/src/Types.d.ts +200 -108
- package/dist/src/Types.d.ts.map +1 -1
- package/dist/src/Types.js +39 -8
- package/dist/src/WebSocket.d.ts +48 -23
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +15 -6
- package/dist/src/Worker.d.ts +94 -36
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +58 -24
- package/dist/src/index.d.ts +55 -52
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -1
- package/dist/src/intl/cs.d.ts +94 -0
- package/dist/src/intl/cs.d.ts.map +1 -0
- package/dist/src/intl/cs.js +220 -0
- package/dist/src/intl/index.d.ts +7 -0
- package/dist/src/intl/index.d.ts.map +1 -0
- package/dist/src/intl/index.js +6 -0
- package/dist/src/local-first/Db.d.ts +12 -8
- package/dist/src/local-first/Db.d.ts.map +1 -1
- package/dist/src/local-first/Db.js +109 -132
- package/dist/src/local-first/Error.d.ts +4 -4
- package/dist/src/local-first/Evolu.d.ts +338 -92
- package/dist/src/local-first/Evolu.d.ts.map +1 -1
- package/dist/src/local-first/Evolu.js +47 -55
- package/dist/src/local-first/LocalAuth.d.ts +4 -4
- package/dist/src/local-first/LocalAuth.d.ts.map +1 -1
- package/dist/src/local-first/Owner.d.ts +61 -24
- package/dist/src/local-first/Owner.d.ts.map +1 -1
- package/dist/src/local-first/Owner.js +45 -8
- package/dist/src/local-first/Protocol.d.ts +44 -31
- package/dist/src/local-first/Protocol.d.ts.map +1 -1
- package/dist/src/local-first/Protocol.js +36 -13
- package/dist/src/local-first/Query.d.ts +147 -62
- package/dist/src/local-first/Query.d.ts.map +1 -1
- package/dist/src/local-first/Query.js +112 -58
- package/dist/src/local-first/Relay.d.ts +44 -14
- package/dist/src/local-first/Relay.d.ts.map +1 -1
- package/dist/src/local-first/Schema.d.ts +92 -41
- package/dist/src/local-first/Schema.d.ts.map +1 -1
- package/dist/src/local-first/Schema.js +25 -8
- package/dist/src/local-first/Shared.d.ts +23 -21
- package/dist/src/local-first/Shared.d.ts.map +1 -1
- package/dist/src/local-first/Shared.js +100 -57
- package/dist/src/local-first/Storage.d.ts +53 -32
- package/dist/src/local-first/Storage.d.ts.map +1 -1
- package/dist/src/local-first/Storage.js +194 -234
- package/dist/src/local-first/Timestamp.d.ts +21 -21
- package/dist/src/local-first/Timestamp.d.ts.map +1 -1
- package/dist/src/local-first/Timestamp.js +2 -2
- package/dist/src/local-first/index.d.ts +11 -11
- package/package.json +14 -13
- package/src/Array.ts +306 -205
- package/src/Assert.ts +54 -69
- package/src/BigInt.ts +6 -3
- package/src/Brand.ts +27 -40
- package/src/Buffer.ts +28 -12
- package/src/Cache.ts +9 -4
- package/src/Callbacks.ts +30 -19
- package/src/Console.ts +156 -84
- package/src/Crypto.ts +83 -41
- package/src/Eq.ts +95 -22
- package/src/Error.ts +8 -4
- package/src/Function.ts +167 -59
- package/src/Http.ts +549 -0
- package/src/Identicon.ts +16 -7
- package/src/LeakDetector.ts +182 -0
- package/src/LockManager.ts +43 -39
- package/src/Lookup.ts +62 -28
- package/src/Microtask.ts +19 -23
- package/src/Number.ts +64 -12
- package/src/Object.ts +173 -49
- package/src/Option.ts +10 -7
- package/src/Order.ts +23 -21
- package/src/Platform.ts +136 -0
- package/src/Polyfills.ts +6 -12
- package/src/Random.ts +32 -19
- package/src/Redacted.ts +44 -30
- package/src/Ref.ts +14 -6
- package/src/RefCount.ts +38 -80
- package/src/Relation.ts +202 -10
- package/src/Resource.ts +1199 -591
- package/src/Result.ts +930 -271
- package/src/Schedule.ts +859 -343
- package/src/Set.ts +89 -40
- package/src/Sqlite.ts +144 -138
- package/src/StackTrace.ts +230 -0
- package/src/Store.ts +8 -7
- package/src/String.ts +22 -5
- package/src/Task.ts +5101 -2832
- package/src/Test.ts +20 -168
- package/src/Time.ts +293 -90
- package/src/Type.ts +10034 -4117
- package/src/Types.ts +227 -110
- package/src/WebSocket.ts +64 -36
- package/src/Worker.ts +180 -72
- package/src/index.ts +55 -52
- package/src/intl/cs.ts +388 -0
- package/src/intl/index.ts +7 -0
- package/src/local-first/Db.ts +152 -179
- package/src/local-first/Error.ts +4 -4
- package/src/local-first/Evolu.ts +495 -250
- package/src/local-first/LocalAuth.ts +6 -12
- package/src/local-first/Owner.ts +58 -21
- package/src/local-first/Protocol.ts +49 -31
- package/src/local-first/Query.ts +146 -61
- package/src/local-first/Relay.ts +51 -21
- package/src/local-first/Schema.ts +109 -51
- package/src/local-first/Shared.ts +266 -226
- package/src/local-first/Storage.ts +266 -264
- package/src/local-first/Timestamp.ts +25 -18
- package/src/local-first/index.ts +11 -11
- package/dist/src/Skiplist.d.ts +0 -28
- package/dist/src/Skiplist.d.ts.map +0 -1
- package/dist/src/Skiplist.js +0 -64
- package/dist/src/Tracer.d.ts +0 -48
- package/dist/src/Tracer.d.ts.map +0 -1
- package/dist/src/Tracer.js +0 -6
- package/src/Skiplist.ts +0 -108
- package/src/Tracer.ts +0 -54
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import type { RandomBytesDep } from "../Crypto.
|
|
8
|
-
import type { Mnemonic } from "../Type.
|
|
9
|
-
import type { AppOwner } from "./Owner.
|
|
7
|
+
import type { RandomBytesDep } from "../Crypto.ts";
|
|
8
|
+
import type { Mnemonic } from "../Type.ts";
|
|
9
|
+
import type { AppOwner } from "./Owner.ts";
|
|
10
10
|
import {
|
|
11
11
|
createAppOwner,
|
|
12
12
|
createOwnerSecret,
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
OwnerEncryptionKey,
|
|
15
15
|
OwnerId,
|
|
16
16
|
OwnerWriteKey,
|
|
17
|
-
} from "./Owner.
|
|
17
|
+
} from "./Owner.ts";
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Local authentication and authorization system for Evolu. This is API is
|
|
@@ -439,17 +439,11 @@ export interface MutationResult {
|
|
|
439
439
|
* storing a value.
|
|
440
440
|
*/
|
|
441
441
|
export type SecurityLevel =
|
|
442
|
-
| "
|
|
443
|
-
| "strongBox"
|
|
444
|
-
| "biometry"
|
|
445
|
-
| "deviceCredential"
|
|
446
|
-
| "software";
|
|
442
|
+
"secureEnclave" | "strongBox" | "biometry" | "deviceCredential" | "software";
|
|
447
443
|
|
|
448
444
|
/** Enumerates the native storage backend used to persist sensitive data. */
|
|
449
445
|
export type StorageBackend =
|
|
450
|
-
| "
|
|
451
|
-
| "androidKeystore"
|
|
452
|
-
| "encryptedSharedPreferences";
|
|
446
|
+
"keychain" | "androidKeystore" | "encryptedSharedPreferences";
|
|
453
447
|
|
|
454
448
|
/**
|
|
455
449
|
* Enumerates the access-control policy enforced by the underlying secure
|
package/src/local-first/Owner.ts
CHANGED
|
@@ -6,17 +6,17 @@
|
|
|
6
6
|
|
|
7
7
|
import * as bip39 from "@scure/bip39";
|
|
8
8
|
import { wordlist } from "@scure/bip39/wordlists/english.js";
|
|
9
|
-
import type { NonEmptyReadonlyArray } from "../Array.
|
|
10
|
-
import type { RandomBytesDep } from "../Crypto.
|
|
9
|
+
import type { NonEmptyReadonlyArray } from "../Array.ts";
|
|
10
|
+
import type { RandomBytesDep } from "../Crypto.ts";
|
|
11
11
|
import {
|
|
12
12
|
createSlip21,
|
|
13
13
|
EncryptionKey,
|
|
14
14
|
Entropy16,
|
|
15
15
|
Entropy32,
|
|
16
16
|
testCreateRandomBytes,
|
|
17
|
-
} from "../Crypto.
|
|
18
|
-
import { testCreateRandomLib } from "../Random.
|
|
19
|
-
import { getOrNull } from "../Result.
|
|
17
|
+
} from "../Crypto.ts";
|
|
18
|
+
import { testCreateRandomLib } from "../Random.ts";
|
|
19
|
+
import { getOrNull } from "../Result.ts";
|
|
20
20
|
import {
|
|
21
21
|
brand,
|
|
22
22
|
Id,
|
|
@@ -26,9 +26,9 @@ import {
|
|
|
26
26
|
Mnemonic,
|
|
27
27
|
NonNegativeInt,
|
|
28
28
|
type Typed,
|
|
29
|
-
} from "../Type.
|
|
30
|
-
import type { EncryptedDbChange, Storage } from "./Storage.
|
|
31
|
-
import { TimestampBytes } from "./Timestamp.
|
|
29
|
+
} from "../Type.ts";
|
|
30
|
+
import type { EncryptedDbChange, Storage } from "./Storage.ts";
|
|
31
|
+
import { TimestampBytes } from "./Timestamp.ts";
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
34
|
* {@link Owner} without a {@link OwnerWriteKey}.
|
|
@@ -88,11 +88,11 @@ export interface SyncOwner {
|
|
|
88
88
|
|
|
89
89
|
/** OwnerId is a branded {@link Id} that uniquely identifies an {@link Owner}. */
|
|
90
90
|
export const OwnerId = /*#__PURE__*/ brand("OwnerId", Id);
|
|
91
|
-
export type OwnerId = typeof OwnerId.
|
|
91
|
+
export type OwnerId = typeof OwnerId.Output;
|
|
92
92
|
|
|
93
93
|
/** Bytes representation of {@link OwnerId}. */
|
|
94
94
|
export const OwnerIdBytes = /*#__PURE__*/ brand("OwnerIdBytes", IdBytes);
|
|
95
|
-
export type OwnerIdBytes = typeof OwnerIdBytes.
|
|
95
|
+
export type OwnerIdBytes = typeof OwnerIdBytes.Output;
|
|
96
96
|
|
|
97
97
|
/** Converts {@link OwnerId} to {@link OwnerIdBytes}. */
|
|
98
98
|
export const ownerIdToOwnerIdBytes = (ownerId: OwnerId): OwnerIdBytes =>
|
|
@@ -109,14 +109,14 @@ export const OwnerEncryptionKey = /*#__PURE__*/ brand(
|
|
|
109
109
|
"OwnerEncryptionKey",
|
|
110
110
|
EncryptionKey,
|
|
111
111
|
);
|
|
112
|
-
export type OwnerEncryptionKey = typeof OwnerEncryptionKey.
|
|
112
|
+
export type OwnerEncryptionKey = typeof OwnerEncryptionKey.Output;
|
|
113
113
|
|
|
114
114
|
/**
|
|
115
115
|
* A secure token for write operations. It's derived from {@link OwnerSecret} by
|
|
116
116
|
* default and can be rotated via {@link createOwnerWriteKey}.
|
|
117
117
|
*/
|
|
118
118
|
export const OwnerWriteKey = /*#__PURE__*/ brand("OwnerWriteKey", Entropy16);
|
|
119
|
-
export type OwnerWriteKey = typeof OwnerWriteKey.
|
|
119
|
+
export type OwnerWriteKey = typeof OwnerWriteKey.Output;
|
|
120
120
|
|
|
121
121
|
/**
|
|
122
122
|
* Creates a new random {@link OwnerWriteKey} for rotation.
|
|
@@ -135,7 +135,7 @@ export const createOwnerWriteKey = (deps: RandomBytesDep): OwnerWriteKey =>
|
|
|
135
135
|
* {@link Mnemonic} using {@link mnemonicToOwnerSecret}.
|
|
136
136
|
*/
|
|
137
137
|
export const OwnerSecret = /*#__PURE__*/ brand("OwnerSecret", Entropy32);
|
|
138
|
-
export type OwnerSecret = typeof OwnerSecret.
|
|
138
|
+
export type OwnerSecret = typeof OwnerSecret.Output;
|
|
139
139
|
|
|
140
140
|
/** Creates a {@link OwnerSecret}. */
|
|
141
141
|
export const createOwnerSecret = (deps: RandomBytesDep): OwnerSecret =>
|
|
@@ -359,15 +359,37 @@ export interface OwnerWebSocketTransport extends Typed<"WebSocket"> {
|
|
|
359
359
|
* ### Example
|
|
360
360
|
*
|
|
361
361
|
* ```ts
|
|
362
|
-
*
|
|
362
|
+
* import {
|
|
363
|
+
* AppName,
|
|
364
|
+
* createAppOwner,
|
|
365
|
+
* createEvolu,
|
|
366
|
+
* createOwnerWebSocketTransport,
|
|
367
|
+
* createOwnerSecret,
|
|
368
|
+
* createRandomBytes,
|
|
369
|
+
* id,
|
|
370
|
+
* } from "@evolu/common";
|
|
371
|
+
*
|
|
372
|
+
* // Create once, persist the mnemonic securely, and restore it on later runs.
|
|
373
|
+
* const appOwner = createAppOwner(
|
|
374
|
+
* createOwnerSecret({ randomBytes: createRandomBytes() }),
|
|
375
|
+
* );
|
|
363
376
|
* const transport = createOwnerWebSocketTransport({
|
|
364
377
|
* url: "wss://relay.evolu.dev",
|
|
365
|
-
* ownerId:
|
|
378
|
+
* ownerId: appOwner.id,
|
|
366
379
|
* });
|
|
380
|
+
* const createTodoEvolu = createEvolu(
|
|
381
|
+
* { todo: { id: id("Todo") } },
|
|
382
|
+
* {
|
|
383
|
+
* appName: AppName.orThrow("OwnerTransportExample"),
|
|
384
|
+
* appOwner,
|
|
385
|
+
* transports: [transport],
|
|
386
|
+
* },
|
|
387
|
+
* );
|
|
367
388
|
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
389
|
+
* expect(createTodoEvolu).toBeTypeOf("function");
|
|
390
|
+
* expect(transport).toEqual({
|
|
391
|
+
* type: "WebSocket",
|
|
392
|
+
* url: `wss://relay.evolu.dev?ownerId=${appOwner.id}`,
|
|
371
393
|
* });
|
|
372
394
|
* ```
|
|
373
395
|
*/
|
|
@@ -389,10 +411,25 @@ export const createOwnerWebSocketTransport = (config: {
|
|
|
389
411
|
* ### Example
|
|
390
412
|
*
|
|
391
413
|
* ```ts
|
|
392
|
-
*
|
|
393
|
-
*
|
|
414
|
+
* import {
|
|
415
|
+
* createAppOwner,
|
|
416
|
+
* createOwnerSecret,
|
|
417
|
+
* createRandomBytes,
|
|
418
|
+
* parseOwnerIdFromOwnerWebSocketTransportUrl,
|
|
419
|
+
* } from "@evolu/common";
|
|
420
|
+
*
|
|
421
|
+
* // Create once, persist the mnemonic securely, and restore it on later runs.
|
|
422
|
+
* const appOwner = createAppOwner(
|
|
423
|
+
* createOwnerSecret({ randomBytes: createRandomBytes() }),
|
|
424
|
+
* );
|
|
425
|
+
* const url = `/sync?ownerId=${appOwner.id}`;
|
|
426
|
+
*
|
|
427
|
+
* expect(parseOwnerIdFromOwnerWebSocketTransportUrl(url)).toBe(
|
|
428
|
+
* appOwner.id,
|
|
394
429
|
* );
|
|
395
|
-
*
|
|
430
|
+
* expect(
|
|
431
|
+
* parseOwnerIdFromOwnerWebSocketTransportUrl("/sync?ownerId=invalid"),
|
|
432
|
+
* ).toBeNull();
|
|
396
433
|
* ```
|
|
397
434
|
*/
|
|
398
435
|
export const parseOwnerIdFromOwnerWebSocketTransportUrl = (
|
|
@@ -178,9 +178,9 @@
|
|
|
178
178
|
*/
|
|
179
179
|
|
|
180
180
|
import { Packr } from "msgpackr";
|
|
181
|
-
import { isNonEmptyArray, type NonEmptyReadonlyArray } from "../Array.
|
|
182
|
-
import { assert } from "../Assert.
|
|
183
|
-
import type { Brand } from "../Brand.
|
|
181
|
+
import { isNonEmptyArray, type NonEmptyReadonlyArray } from "../Array.ts";
|
|
182
|
+
import { assert, assertNonNullable } from "../Assert.ts";
|
|
183
|
+
import type { Brand } from "../Brand.ts";
|
|
184
184
|
import {
|
|
185
185
|
type Buffer,
|
|
186
186
|
bytesToHex,
|
|
@@ -188,8 +188,8 @@ import {
|
|
|
188
188
|
createBuffer,
|
|
189
189
|
hexToBytes,
|
|
190
190
|
utf8ToBytes,
|
|
191
|
-
} from "../Buffer.
|
|
192
|
-
import type { ConsoleDep } from "../Console.
|
|
191
|
+
} from "../Buffer.ts";
|
|
192
|
+
import type { ConsoleDep } from "../Console.ts";
|
|
193
193
|
import {
|
|
194
194
|
createPadmePadding,
|
|
195
195
|
decryptWithXChaCha20Poly1305,
|
|
@@ -200,14 +200,14 @@ import {
|
|
|
200
200
|
type RandomBytesDep,
|
|
201
201
|
XChaCha20Poly1305Ciphertext,
|
|
202
202
|
xChaCha20Poly1305NonceLength,
|
|
203
|
-
} from "../Crypto.
|
|
204
|
-
import { eqArrayNumber } from "../Eq.
|
|
205
|
-
import { computeBalancedBuckets } from "../Number.
|
|
206
|
-
import {
|
|
207
|
-
import { err, ok, type Result } from "../Result.
|
|
208
|
-
import { SqliteValue } from "../Sqlite.
|
|
209
|
-
import type
|
|
210
|
-
import { Millis } from "../Time.
|
|
203
|
+
} from "../Crypto.ts";
|
|
204
|
+
import { eqArrayNumber } from "../Eq.ts";
|
|
205
|
+
import { computeBalancedBuckets } from "../Number.ts";
|
|
206
|
+
import { createMutableRecord, objectToEntries } from "../Object.ts";
|
|
207
|
+
import { err, ok, type Result } from "../Result.ts";
|
|
208
|
+
import { SqliteValue } from "../Sqlite.ts";
|
|
209
|
+
import { AbortError, type Task } from "../Task.ts";
|
|
210
|
+
import { Millis } from "../Time.ts";
|
|
211
211
|
import {
|
|
212
212
|
Base64Url,
|
|
213
213
|
base64UrlToUint8Array,
|
|
@@ -228,8 +228,8 @@ import {
|
|
|
228
228
|
type Typed,
|
|
229
229
|
uint8ArrayToBase64Url,
|
|
230
230
|
zeroNonNegativeInt,
|
|
231
|
-
} from "../Type.
|
|
232
|
-
import type { Predicate } from "../Types.
|
|
231
|
+
} from "../Type.ts";
|
|
232
|
+
import type { Predicate } from "../Types.ts";
|
|
233
233
|
import {
|
|
234
234
|
type Owner,
|
|
235
235
|
type OwnerError,
|
|
@@ -238,7 +238,7 @@ import {
|
|
|
238
238
|
ownerIdToOwnerIdBytes,
|
|
239
239
|
OwnerWriteKey,
|
|
240
240
|
ownerWriteKeyLength,
|
|
241
|
-
} from "./Owner.
|
|
241
|
+
} from "./Owner.ts";
|
|
242
242
|
import {
|
|
243
243
|
type BaseRange,
|
|
244
244
|
type CrdtMessage,
|
|
@@ -255,7 +255,7 @@ import {
|
|
|
255
255
|
type SkipRange,
|
|
256
256
|
type StorageDep,
|
|
257
257
|
type TimestampsRange,
|
|
258
|
-
} from "./Storage.
|
|
258
|
+
} from "./Storage.ts";
|
|
259
259
|
import {
|
|
260
260
|
Counter,
|
|
261
261
|
eqTimestamp,
|
|
@@ -265,7 +265,7 @@ import {
|
|
|
265
265
|
timestampBytesLength,
|
|
266
266
|
timestampBytesToTimestamp,
|
|
267
267
|
timestampToTimestampBytes,
|
|
268
|
-
} from "./Timestamp.
|
|
268
|
+
} from "./Timestamp.ts";
|
|
269
269
|
|
|
270
270
|
/**
|
|
271
271
|
* Evolu uses MessagePack for numbers and JSONs.
|
|
@@ -299,7 +299,7 @@ export const ProtocolMessageMaxSize = /*#__PURE__*/ between(
|
|
|
299
299
|
maxProtocolMessageMaxSize,
|
|
300
300
|
)(Int);
|
|
301
301
|
|
|
302
|
-
export type ProtocolMessageMaxSize = typeof ProtocolMessageMaxSize.
|
|
302
|
+
export type ProtocolMessageMaxSize = typeof ProtocolMessageMaxSize.Output;
|
|
303
303
|
|
|
304
304
|
/**
|
|
305
305
|
* Default {@link ProtocolMessageMaxSize} (1MB).
|
|
@@ -326,7 +326,7 @@ export const ProtocolMessageRangesMaxSize = /*#__PURE__*/ between(
|
|
|
326
326
|
100_000,
|
|
327
327
|
)(Int);
|
|
328
328
|
export type ProtocolMessageRangesMaxSize =
|
|
329
|
-
typeof ProtocolMessageRangesMaxSize.
|
|
329
|
+
typeof ProtocolMessageRangesMaxSize.Output;
|
|
330
330
|
|
|
331
331
|
/**
|
|
332
332
|
* Default {@link ProtocolMessageRangesMaxSize} (30KB).
|
|
@@ -1014,6 +1014,7 @@ export const applyProtocolMessageAsClient =
|
|
|
1014
1014
|
// Quota errors are handled by Storage; protocol just stops syncing.
|
|
1015
1015
|
if (!result.ok) return ok({ type: "NoResponse" });
|
|
1016
1016
|
} catch (error) {
|
|
1017
|
+
if (AbortError.is(error)) throw error;
|
|
1017
1018
|
run.deps.console.error(error);
|
|
1018
1019
|
return ok({ type: "NoResponse" });
|
|
1019
1020
|
}
|
|
@@ -1054,6 +1055,7 @@ export const applyProtocolMessageAsClient =
|
|
|
1054
1055
|
|
|
1055
1056
|
return ok({ type: "Response", message: output.unwrap() });
|
|
1056
1057
|
} catch (error) {
|
|
1058
|
+
if (AbortError.is(error)) throw error;
|
|
1057
1059
|
return err<ProtocolInvalidDataError>({
|
|
1058
1060
|
type: "ProtocolInvalidDataError",
|
|
1059
1061
|
data: inputMessage,
|
|
@@ -1180,6 +1182,7 @@ export const applyProtocolMessageAsRelay =
|
|
|
1180
1182
|
return ok({ type: "Response", message });
|
|
1181
1183
|
}
|
|
1182
1184
|
} catch (error) {
|
|
1185
|
+
if (AbortError.is(error)) throw error;
|
|
1183
1186
|
run.deps.console.error(error);
|
|
1184
1187
|
const message = createProtocolMessageBuffer(ownerId, {
|
|
1185
1188
|
messageType: MessageType.Response,
|
|
@@ -1246,6 +1249,7 @@ export const applyProtocolMessageAsRelay =
|
|
|
1246
1249
|
// Non-initiators always respond to provide sync completion feedback,
|
|
1247
1250
|
return ok({ type: "Response", message });
|
|
1248
1251
|
} catch (error) {
|
|
1252
|
+
if (AbortError.is(error)) throw error;
|
|
1249
1253
|
return err<ProtocolInvalidDataError>({
|
|
1250
1254
|
type: "ProtocolInvalidDataError",
|
|
1251
1255
|
data: inputMessage,
|
|
@@ -1699,14 +1703,14 @@ const decodeTimestamps = (
|
|
|
1699
1703
|
const millises = new Array<Millis>(length);
|
|
1700
1704
|
for (let i = 0; i < length; i++) {
|
|
1701
1705
|
const deltaMillis = decodeNonNegativeInt(buffer);
|
|
1702
|
-
const millis = Millis.
|
|
1706
|
+
const millis = Millis.fromUnknown(previousMillis + deltaMillis);
|
|
1703
1707
|
if (!millis.ok) throw new ProtocolDecodeError(millis.error.type);
|
|
1704
1708
|
millises[i] = millis.value;
|
|
1705
1709
|
previousMillis = millis.value;
|
|
1706
1710
|
}
|
|
1707
1711
|
|
|
1708
1712
|
const counters = decodeRle(buffer, length, (): Counter => {
|
|
1709
|
-
const counter = Counter.
|
|
1713
|
+
const counter = Counter.fromUnknown(decodeNonNegativeInt(buffer));
|
|
1710
1714
|
if (!counter.ok) throw new ProtocolDecodeError(counter.error.type);
|
|
1711
1715
|
return counter.value;
|
|
1712
1716
|
});
|
|
@@ -1803,7 +1807,13 @@ export const decodeNumber = (buffer: Buffer): number => {
|
|
|
1803
1807
|
* ### Example
|
|
1804
1808
|
*
|
|
1805
1809
|
* ```ts
|
|
1806
|
-
*
|
|
1810
|
+
* import { createBuffer } from "@evolu/common";
|
|
1811
|
+
* import { encodeFlags } from "@evolu/common/local-first";
|
|
1812
|
+
*
|
|
1813
|
+
* const buffer = createBuffer();
|
|
1814
|
+
* encodeFlags(buffer, [true, false, true]);
|
|
1815
|
+
*
|
|
1816
|
+
* expect(buffer.unwrap()).toEqual(new Uint8Array([0b101]));
|
|
1807
1817
|
* ```
|
|
1808
1818
|
*/
|
|
1809
1819
|
export const encodeFlags = (
|
|
@@ -1825,7 +1835,14 @@ export const encodeFlags = (
|
|
|
1825
1835
|
* ### Example
|
|
1826
1836
|
*
|
|
1827
1837
|
* ```ts
|
|
1828
|
-
*
|
|
1838
|
+
* import { createBuffer, PositiveInt } from "@evolu/common";
|
|
1839
|
+
* import { decodeFlags } from "@evolu/common/local-first";
|
|
1840
|
+
*
|
|
1841
|
+
* const buffer = createBuffer([0b101]);
|
|
1842
|
+
* const flags = decodeFlags(buffer, PositiveInt.orThrow(3));
|
|
1843
|
+
*
|
|
1844
|
+
* expect(flags).toEqual([true, false, true]);
|
|
1845
|
+
* expect(buffer.getLength()).toBe(0);
|
|
1829
1846
|
* ```
|
|
1830
1847
|
*/
|
|
1831
1848
|
export const decodeFlags = (
|
|
@@ -1874,6 +1891,7 @@ export const encodeAndEncryptDbChange =
|
|
|
1874
1891
|
|
|
1875
1892
|
encodeLength(buffer, entries);
|
|
1876
1893
|
for (const [column, value] of entries) {
|
|
1894
|
+
assertNonNullable(value);
|
|
1877
1895
|
encodeString(buffer, column);
|
|
1878
1896
|
encodeSqliteValue(buffer, value);
|
|
1879
1897
|
}
|
|
@@ -1944,7 +1962,7 @@ export const decryptAndDecodeDbChange = (
|
|
|
1944
1962
|
const id = decodeId(buffer);
|
|
1945
1963
|
|
|
1946
1964
|
const length = decodeLength(buffer);
|
|
1947
|
-
const values =
|
|
1965
|
+
const values = createMutableRecord<string, SqliteValue>();
|
|
1948
1966
|
|
|
1949
1967
|
for (let i = 0; i < length; i++) {
|
|
1950
1968
|
const column = decodeString(buffer);
|
|
@@ -2019,7 +2037,7 @@ export const decodeNonNegativeInt = (buffer: Buffer): NonNegativeInt => {
|
|
|
2019
2037
|
shift += 7n;
|
|
2020
2038
|
}
|
|
2021
2039
|
|
|
2022
|
-
const int = NonNegativeInt.
|
|
2040
|
+
const int = NonNegativeInt.fromUnknown(globalThis.Number(result));
|
|
2023
2041
|
if (!int.ok) throw new ProtocolDecodeError(int.error.type);
|
|
2024
2042
|
|
|
2025
2043
|
return int.value;
|
|
@@ -2098,7 +2116,7 @@ export const encodeSqliteValue = (buffer: Buffer, value: SqliteValue): void => {
|
|
|
2098
2116
|
return;
|
|
2099
2117
|
}
|
|
2100
2118
|
|
|
2101
|
-
const dateIso = DateIso.
|
|
2119
|
+
const dateIso = DateIso.from.parent(value);
|
|
2102
2120
|
if (dateIso.ok) {
|
|
2103
2121
|
const time = new Date(dateIso.value).getTime();
|
|
2104
2122
|
if (NonNegativeInt.is(time)) {
|
|
@@ -2117,14 +2135,14 @@ export const encodeSqliteValue = (buffer: Buffer, value: SqliteValue): void => {
|
|
|
2117
2135
|
return;
|
|
2118
2136
|
}
|
|
2119
2137
|
|
|
2120
|
-
const id = Id.
|
|
2138
|
+
const id = Id.from.parent(value);
|
|
2121
2139
|
if (id.ok) {
|
|
2122
2140
|
encodeNonNegativeInt(buffer, ProtocolValueType.Id);
|
|
2123
2141
|
buffer.extend(idToIdBytes(id.value));
|
|
2124
2142
|
return;
|
|
2125
2143
|
}
|
|
2126
2144
|
|
|
2127
|
-
const json = Json.
|
|
2145
|
+
const json = Json.from.parent(value);
|
|
2128
2146
|
// Only encode as Json if it survives JSON.parse/JSON.stringify round-trip.
|
|
2129
2147
|
// Some valid JSON strings like "-0E0" get normalized to "0" during parsing,
|
|
2130
2148
|
// which would cause data corruption if we don't verify round-trip safety.
|
|
@@ -2136,7 +2154,7 @@ export const encodeSqliteValue = (buffer: Buffer, value: SqliteValue): void => {
|
|
|
2136
2154
|
return;
|
|
2137
2155
|
}
|
|
2138
2156
|
|
|
2139
|
-
const base64Url = Base64Url.
|
|
2157
|
+
const base64Url = Base64Url.from.parent(value);
|
|
2140
2158
|
if (base64Url.ok) {
|
|
2141
2159
|
encodeNonNegativeInt(buffer, ProtocolValueType.Base64Url);
|
|
2142
2160
|
const bytes = base64UrlToUint8Array(base64Url.value);
|
|
@@ -2211,7 +2229,7 @@ export const decodeSqliteValue = (buffer: Buffer): SqliteValue => {
|
|
|
2211
2229
|
type === ProtocolValueType.DateIsoWithNonNegativeTime
|
|
2212
2230
|
? decodeNonNegativeInt(buffer)
|
|
2213
2231
|
: decodeNumber(buffer);
|
|
2214
|
-
const dateIso = DateIso.
|
|
2232
|
+
const dateIso = DateIso.from.parent(new Date(time).toISOString());
|
|
2215
2233
|
if (!dateIso.ok) throw new ProtocolDecodeError(dateIso.error.type);
|
|
2216
2234
|
return dateIso.value;
|
|
2217
2235
|
}
|