@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/Public.ts
CHANGED
|
@@ -5,23 +5,17 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
export { createEvolu } from "./Evolu.js";
|
|
8
|
-
export type {
|
|
9
|
-
Evolu,
|
|
10
|
-
EvoluConfigWithInitialData,
|
|
11
|
-
EvoluDeps,
|
|
12
|
-
EvoluError,
|
|
13
|
-
} from "./Evolu.js";
|
|
8
|
+
export type { Evolu, EvoluConfig, EvoluDeps, EvoluError } from "./Evolu.js";
|
|
14
9
|
export * from "./Owner.js";
|
|
15
|
-
export
|
|
16
|
-
export type { BinaryId } from "./Protocol.js";
|
|
10
|
+
export * from "./LocalAuth.js";
|
|
17
11
|
export * as kysely from "./PublicKysely.js";
|
|
18
12
|
export type { InferRow, Query, QueryRows, Row } from "./Query.js";
|
|
19
|
-
export {
|
|
20
|
-
export type { EvoluSchema, ValidMutationSizeError } from "./Schema.js";
|
|
13
|
+
export type { EvoluSchema } from "./Schema.js";
|
|
21
14
|
export type {
|
|
22
15
|
NetworkError,
|
|
23
16
|
PaymentRequiredError,
|
|
24
17
|
ServerError,
|
|
18
|
+
SyncOwner,
|
|
25
19
|
SyncState,
|
|
26
20
|
SyncStateInitial,
|
|
27
21
|
SyncStateIsNotSynced,
|
|
@@ -29,15 +23,14 @@ export type {
|
|
|
29
23
|
SyncStateIsSyncing,
|
|
30
24
|
} from "./Sync.js";
|
|
31
25
|
export {
|
|
32
|
-
binaryTimestampToTimestamp,
|
|
33
26
|
Timestamp,
|
|
34
|
-
|
|
27
|
+
timestampBytesToTimestamp,
|
|
28
|
+
timestampToTimestampBytes,
|
|
35
29
|
} from "./Timestamp.js";
|
|
36
30
|
export type {
|
|
37
|
-
|
|
31
|
+
TimestampBytes,
|
|
38
32
|
TimestampCounterOverflowError,
|
|
39
33
|
TimestampDriftError,
|
|
40
|
-
TimestampDuplicateNodeError,
|
|
41
34
|
TimestampError,
|
|
42
35
|
TimestampTimeOutOfRangeError,
|
|
43
36
|
} from "./Timestamp.js";
|
|
@@ -65,7 +65,7 @@ export type { NotNull } from "kysely";
|
|
|
65
65
|
* from "person"
|
|
66
66
|
* ```
|
|
67
67
|
*/
|
|
68
|
-
//
|
|
68
|
+
// Kysely expects strict AST.
|
|
69
69
|
// prettier-ignore
|
|
70
70
|
export function jsonArrayFrom<O>(
|
|
71
71
|
expr: SelectQueryBuilderExpression<O>,
|
|
@@ -122,7 +122,7 @@ export function jsonArrayFrom<O>(
|
|
|
122
122
|
* from "person";
|
|
123
123
|
* ```
|
|
124
124
|
*/
|
|
125
|
-
//
|
|
125
|
+
// Kysely expects strict AST.
|
|
126
126
|
// prettier-ignore
|
|
127
127
|
export function jsonObjectFrom<O>(
|
|
128
128
|
expr: SelectQueryBuilderExpression<O>,
|
|
@@ -172,7 +172,7 @@ export function jsonObjectFrom<O>(
|
|
|
172
172
|
* from "person"
|
|
173
173
|
* ```
|
|
174
174
|
*/
|
|
175
|
-
//
|
|
175
|
+
// Kysely expects strict AST.
|
|
176
176
|
// prettier-ignore
|
|
177
177
|
export function jsonBuildObject<O extends Record<string, Expression<unknown>>>(
|
|
178
178
|
obj: O,
|
package/src/Evolu/Query.ts
CHANGED
|
@@ -8,7 +8,8 @@ import {
|
|
|
8
8
|
SqliteValue,
|
|
9
9
|
} from "../Sqlite.js";
|
|
10
10
|
import { Store, StoreSubscribe } from "../Store.js";
|
|
11
|
-
import {
|
|
11
|
+
import { Simplify } from "../Types.js";
|
|
12
|
+
import { Brand } from "../Brand.js";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* A type-safe SQL query.
|
package/src/Evolu/Relay.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { isNonEmptyReadonlyArray } from "../Array.js";
|
|
2
2
|
import { ConsoleConfig } from "../Console.js";
|
|
3
|
-
import {
|
|
4
|
-
import { ok, Result } from "../Result.js";
|
|
3
|
+
import { TimingSafeEqualDep } from "../Crypto.js";
|
|
4
|
+
import { err, ok, Result } from "../Result.js";
|
|
5
5
|
import { sql, SqliteError } from "../Sqlite.js";
|
|
6
6
|
import { SimpleName } from "../Type.js";
|
|
7
|
-
import { OwnerId,
|
|
8
|
-
import { EncryptedDbChange, Storage } from "./Protocol.js";
|
|
7
|
+
import { OwnerId, OwnerWriteKey } from "./Owner.js";
|
|
9
8
|
import {
|
|
10
9
|
createSqliteStorageBase,
|
|
11
10
|
CreateSqliteStorageBaseOptions,
|
|
11
|
+
EncryptedDbChange,
|
|
12
12
|
SqliteStorageDeps,
|
|
13
|
+
Storage,
|
|
13
14
|
} from "./Storage.js";
|
|
14
|
-
import {
|
|
15
|
+
import { timestampToTimestampBytes } from "./Timestamp.js";
|
|
15
16
|
|
|
16
17
|
export interface Relay extends Disposable {}
|
|
17
18
|
|
|
@@ -19,8 +20,10 @@ export interface RelayConfig extends ConsoleConfig {
|
|
|
19
20
|
readonly name?: SimpleName;
|
|
20
21
|
}
|
|
21
22
|
|
|
23
|
+
export type RelaySqliteStorageDeps = SqliteStorageDeps & TimingSafeEqualDep;
|
|
24
|
+
|
|
22
25
|
export const createRelayStorage =
|
|
23
|
-
(deps:
|
|
26
|
+
(deps: RelaySqliteStorageDeps) =>
|
|
24
27
|
(options: CreateSqliteStorageBaseOptions): Result<Storage, SqliteError> => {
|
|
25
28
|
const sqliteStorageBase = createSqliteStorageBase(deps)(options);
|
|
26
29
|
if (!sqliteStorageBase.ok) return sqliteStorageBase;
|
|
@@ -53,7 +56,7 @@ export const createRelayStorage =
|
|
|
53
56
|
...sqliteStorageBase.value,
|
|
54
57
|
|
|
55
58
|
/**
|
|
56
|
-
* Lazily authorizes the initiator's {@link
|
|
59
|
+
* Lazily authorizes the initiator's {@link OwnerWriteKey} for the given
|
|
57
60
|
* {@link OwnerId}.
|
|
58
61
|
*
|
|
59
62
|
* - If the {@link OwnerId} does not exist, it is created and associated with
|
|
@@ -62,11 +65,13 @@ export const createRelayStorage =
|
|
|
62
65
|
* stored key.
|
|
63
66
|
*/
|
|
64
67
|
validateWriteKey: (ownerId, writeKey) => {
|
|
65
|
-
const selectWriteKey = deps.sqlite.exec<{ writeKey:
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
const selectWriteKey = deps.sqlite.exec<{ writeKey: OwnerWriteKey }>(
|
|
69
|
+
sql`
|
|
70
|
+
select writeKey
|
|
71
|
+
from evolu_writeKey
|
|
72
|
+
where ownerId = ${ownerId};
|
|
73
|
+
`,
|
|
74
|
+
);
|
|
70
75
|
if (!selectWriteKey.ok) {
|
|
71
76
|
options.onStorageError(selectWriteKey.error);
|
|
72
77
|
return false;
|
|
@@ -87,16 +92,33 @@ export const createRelayStorage =
|
|
|
87
92
|
return true;
|
|
88
93
|
}
|
|
89
94
|
|
|
90
|
-
return
|
|
95
|
+
return deps.timingSafeEqual(rows[0].writeKey, writeKey);
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
setWriteKey: (ownerId, writeKey) => {
|
|
99
|
+
const upsertWriteKey = deps.sqlite.exec(sql`
|
|
100
|
+
insert into evolu_writeKey (ownerId, writeKey)
|
|
101
|
+
values (${ownerId}, ${writeKey})
|
|
102
|
+
on conflict (ownerId) do update
|
|
103
|
+
set writeKey = excluded.writeKey;
|
|
104
|
+
`);
|
|
105
|
+
if (!upsertWriteKey.ok) {
|
|
106
|
+
options.onStorageError(upsertWriteKey.error);
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return true;
|
|
91
111
|
},
|
|
92
112
|
|
|
93
|
-
|
|
113
|
+
// https://eslint.org/docs/latest/rules/require-await#when-not-to-use-it
|
|
114
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
115
|
+
writeMessages: async (ownerId, messages) => {
|
|
94
116
|
const result = deps.sqlite.transaction(() => {
|
|
95
117
|
for (const message of messages) {
|
|
96
118
|
const insertTimestampResult =
|
|
97
119
|
sqliteStorageBase.value.insertTimestamp(
|
|
98
120
|
ownerId,
|
|
99
|
-
|
|
121
|
+
timestampToTimestampBytes(message.timestamp),
|
|
100
122
|
);
|
|
101
123
|
if (!insertTimestampResult.ok) return insertTimestampResult;
|
|
102
124
|
|
|
@@ -105,7 +127,7 @@ export const createRelayStorage =
|
|
|
105
127
|
values
|
|
106
128
|
(
|
|
107
129
|
${ownerId},
|
|
108
|
-
${
|
|
130
|
+
${timestampToTimestampBytes(message.timestamp)},
|
|
109
131
|
${message.change}
|
|
110
132
|
)
|
|
111
133
|
on conflict do nothing;
|
|
@@ -138,5 +160,29 @@ export const createRelayStorage =
|
|
|
138
160
|
|
|
139
161
|
return result.value.rows[0]?.change;
|
|
140
162
|
},
|
|
163
|
+
|
|
164
|
+
deleteOwner: (ownerId) => {
|
|
165
|
+
const result = deps.sqlite.transaction(() => {
|
|
166
|
+
const del1 = deps.sqlite.exec(sql`
|
|
167
|
+
delete from evolu_writeKey where ownerId = ${ownerId};
|
|
168
|
+
`);
|
|
169
|
+
if (!del1.ok) return del1;
|
|
170
|
+
|
|
171
|
+
const del2 = deps.sqlite.exec(sql`
|
|
172
|
+
delete from evolu_message where ownerId = ${ownerId};
|
|
173
|
+
`);
|
|
174
|
+
if (!del2.ok) return del2;
|
|
175
|
+
|
|
176
|
+
const del3 = sqliteStorageBase.value.deleteOwner(ownerId);
|
|
177
|
+
if (!del3) return err(null);
|
|
178
|
+
|
|
179
|
+
return ok();
|
|
180
|
+
});
|
|
181
|
+
if (!result.ok) {
|
|
182
|
+
if (result.error) options.onStorageError(result.error);
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
return true;
|
|
186
|
+
},
|
|
141
187
|
});
|
|
142
188
|
};
|