@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.
Files changed (154) hide show
  1. package/dist/src/Assert.d.ts +6 -3
  2. package/dist/src/Assert.d.ts.map +1 -1
  3. package/dist/src/Assert.js +7 -4
  4. package/dist/src/Brand.d.ts +75 -0
  5. package/dist/src/Brand.d.ts.map +1 -0
  6. package/dist/src/Brand.js +1 -0
  7. package/dist/src/Buffer.d.ts +1 -1
  8. package/dist/src/Buffer.d.ts.map +1 -1
  9. package/dist/src/Buffer.js +1 -1
  10. package/dist/src/CallbackRegistry.d.ts +53 -0
  11. package/dist/src/CallbackRegistry.d.ts.map +1 -0
  12. package/dist/src/CallbackRegistry.js +25 -0
  13. package/dist/src/Console.d.ts +31 -6
  14. package/dist/src/Console.d.ts.map +1 -1
  15. package/dist/src/Console.js +72 -9
  16. package/dist/src/Crypto.d.ts +61 -34
  17. package/dist/src/Crypto.d.ts.map +1 -1
  18. package/dist/src/Crypto.js +29 -42
  19. package/dist/src/Evolu/Db.d.ts +137 -66
  20. package/dist/src/Evolu/Db.d.ts.map +1 -1
  21. package/dist/src/Evolu/Db.js +300 -705
  22. package/dist/src/Evolu/Diff.d.ts +3 -3
  23. package/dist/src/Evolu/Diff.d.ts.map +1 -1
  24. package/dist/src/Evolu/Diff.js +7 -5
  25. package/dist/src/Evolu/Evolu.d.ts +200 -115
  26. package/dist/src/Evolu/Evolu.d.ts.map +1 -1
  27. package/dist/src/Evolu/Evolu.js +287 -157
  28. package/dist/src/Evolu/Internal.d.ts +0 -2
  29. package/dist/src/Evolu/Internal.d.ts.map +1 -1
  30. package/dist/src/Evolu/Internal.js +0 -2
  31. package/dist/src/Evolu/LocalAuth.d.ts +144 -0
  32. package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
  33. package/dist/src/Evolu/LocalAuth.js +171 -0
  34. package/dist/src/Evolu/Owner.d.ts +152 -127
  35. package/dist/src/Evolu/Owner.d.ts.map +1 -1
  36. package/dist/src/Evolu/Owner.js +88 -107
  37. package/dist/src/Evolu/Platform.d.ts +9 -7
  38. package/dist/src/Evolu/Platform.d.ts.map +1 -1
  39. package/dist/src/Evolu/Protocol.d.ts +170 -186
  40. package/dist/src/Evolu/Protocol.d.ts.map +1 -1
  41. package/dist/src/Evolu/Protocol.js +487 -343
  42. package/dist/src/Evolu/Public.d.ts +6 -8
  43. package/dist/src/Evolu/Public.d.ts.map +1 -1
  44. package/dist/src/Evolu/Public.js +2 -3
  45. package/dist/src/Evolu/PublicKysely.js +3 -3
  46. package/dist/src/Evolu/Query.d.ts +2 -1
  47. package/dist/src/Evolu/Query.d.ts.map +1 -1
  48. package/dist/src/Evolu/Relay.d.ts +4 -3
  49. package/dist/src/Evolu/Relay.d.ts.map +1 -1
  50. package/dist/src/Evolu/Relay.js +50 -12
  51. package/dist/src/Evolu/Schema.d.ts +129 -73
  52. package/dist/src/Evolu/Schema.d.ts.map +1 -1
  53. package/dist/src/Evolu/Schema.js +169 -89
  54. package/dist/src/Evolu/Storage.d.ts +159 -14
  55. package/dist/src/Evolu/Storage.d.ts.map +1 -1
  56. package/dist/src/Evolu/Storage.js +42 -32
  57. package/dist/src/Evolu/Sync.d.ts +77 -13
  58. package/dist/src/Evolu/Sync.d.ts.map +1 -1
  59. package/dist/src/Evolu/Sync.js +453 -20
  60. package/dist/src/Evolu/Timestamp.d.ts +35 -27
  61. package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
  62. package/dist/src/Evolu/Timestamp.js +27 -18
  63. package/dist/src/ManyToManyMap.d.ts +74 -10
  64. package/dist/src/ManyToManyMap.d.ts.map +1 -1
  65. package/dist/src/ManyToManyMap.js +41 -6
  66. package/dist/src/Number.d.ts +2 -1
  67. package/dist/src/Number.d.ts.map +1 -1
  68. package/dist/src/Random.d.ts +3 -2
  69. package/dist/src/Random.d.ts.map +1 -1
  70. package/dist/src/RefCountedResourceManager.d.ts +119 -0
  71. package/dist/src/RefCountedResourceManager.d.ts.map +1 -0
  72. package/dist/src/RefCountedResourceManager.js +197 -0
  73. package/dist/src/Result.d.ts +147 -51
  74. package/dist/src/Result.d.ts.map +1 -1
  75. package/dist/src/Result.js +7 -241
  76. package/dist/src/Sqlite.d.ts +21 -4
  77. package/dist/src/Sqlite.d.ts.map +1 -1
  78. package/dist/src/Sqlite.js +50 -8
  79. package/dist/src/Task.d.ts +511 -0
  80. package/dist/src/Task.d.ts.map +1 -0
  81. package/dist/src/Task.js +410 -0
  82. package/dist/src/Time.d.ts +59 -0
  83. package/dist/src/Time.d.ts.map +1 -1
  84. package/dist/src/Time.js +87 -4
  85. package/dist/src/Type.d.ts +462 -327
  86. package/dist/src/Type.d.ts.map +1 -1
  87. package/dist/src/Type.js +490 -436
  88. package/dist/src/Types.d.ts +0 -74
  89. package/dist/src/Types.d.ts.map +1 -1
  90. package/dist/src/WebSocket.d.ts +5 -2
  91. package/dist/src/WebSocket.d.ts.map +1 -1
  92. package/dist/src/WebSocket.js +12 -13
  93. package/dist/src/Worker.d.ts +39 -11
  94. package/dist/src/Worker.d.ts.map +1 -1
  95. package/dist/src/Worker.js +22 -4
  96. package/dist/src/index.d.ts +3 -3
  97. package/dist/src/index.d.ts.map +1 -1
  98. package/dist/src/index.js +3 -3
  99. package/package.json +14 -13
  100. package/src/Assert.ts +8 -7
  101. package/src/Brand.ts +75 -0
  102. package/src/Buffer.ts +1 -1
  103. package/src/CallbackRegistry.ts +84 -0
  104. package/src/Console.ts +91 -11
  105. package/src/Crypto.ts +94 -79
  106. package/src/Evolu/Db.ts +505 -1027
  107. package/src/Evolu/Diff.ts +7 -5
  108. package/src/Evolu/Evolu.ts +682 -329
  109. package/src/Evolu/Internal.ts +0 -2
  110. package/src/Evolu/LocalAuth.ts +422 -0
  111. package/src/Evolu/Owner.ts +221 -235
  112. package/src/Evolu/Platform.ts +9 -9
  113. package/src/Evolu/Protocol.ts +681 -614
  114. package/src/Evolu/Public.ts +7 -14
  115. package/src/Evolu/PublicKysely.ts +3 -3
  116. package/src/Evolu/Query.ts +2 -1
  117. package/src/Evolu/Relay.ts +62 -16
  118. package/src/Evolu/Schema.ts +391 -191
  119. package/src/Evolu/Storage.ts +275 -55
  120. package/src/Evolu/Sync.ts +758 -37
  121. package/src/Evolu/Timestamp.ts +38 -35
  122. package/src/ManyToManyMap.ts +127 -24
  123. package/src/Number.ts +2 -6
  124. package/src/Random.ts +3 -2
  125. package/src/RefCountedResourceManager.ts +368 -0
  126. package/src/Result.ts +152 -53
  127. package/src/Sqlite.ts +59 -23
  128. package/src/Task.ts +779 -0
  129. package/src/Time.ts +168 -4
  130. package/src/Type.ts +696 -660
  131. package/src/Types.ts +0 -76
  132. package/src/WebSocket.ts +23 -17
  133. package/src/Worker.ts +72 -23
  134. package/src/index.ts +3 -3
  135. package/dist/src/Callbacks.d.ts +0 -20
  136. package/dist/src/Callbacks.d.ts.map +0 -1
  137. package/dist/src/Callbacks.js +0 -18
  138. package/dist/src/Evolu/Config.d.ts +0 -69
  139. package/dist/src/Evolu/Config.d.ts.map +0 -1
  140. package/dist/src/Evolu/Config.js +0 -9
  141. package/dist/src/Evolu/Kysely.d.ts +0 -6
  142. package/dist/src/Evolu/Kysely.d.ts.map +0 -1
  143. package/dist/src/Evolu/Kysely.js +0 -21
  144. package/dist/src/NanoId.d.ts +0 -27
  145. package/dist/src/NanoId.d.ts.map +0 -1
  146. package/dist/src/NanoId.js +0 -6
  147. package/dist/src/Promise.d.ts +0 -180
  148. package/dist/src/Promise.d.ts.map +0 -1
  149. package/dist/src/Promise.js +0 -176
  150. package/src/Callbacks.ts +0 -43
  151. package/src/Evolu/Config.ts +0 -83
  152. package/src/Evolu/Kysely.ts +0 -38
  153. package/src/NanoId.ts +0 -39
  154. package/src/Promise.ts +0 -295
@@ -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 { binaryIdToId, idToBinaryId } from "./Protocol.js";
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 { formatValidMutationSizeError } from "./Schema.js";
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
- timestampToBinaryTimestamp,
27
+ timestampBytesToTimestamp,
28
+ timestampToTimestampBytes,
35
29
  } from "./Timestamp.js";
36
30
  export type {
37
- BinaryTimestamp,
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
- // SQL formatting breaks generated SQL for some reason.
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
- // SQL formatting breaks generated SQL for some reason.
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
- // SQL formatting breaks generated SQL for some reason.
175
+ // Kysely expects strict AST.
176
176
  // prettier-ignore
177
177
  export function jsonBuildObject<O extends Record<string, Expression<unknown>>>(
178
178
  obj: O,
@@ -8,7 +8,8 @@ import {
8
8
  SqliteValue,
9
9
  } from "../Sqlite.js";
10
10
  import { Store, StoreSubscribe } from "../Store.js";
11
- import { Brand, Simplify } from "../Types.js";
11
+ import { Simplify } from "../Types.js";
12
+ import { Brand } from "../Brand.js";
12
13
 
13
14
  /**
14
15
  * A type-safe SQL query.
@@ -1,17 +1,18 @@
1
1
  import { isNonEmptyReadonlyArray } from "../Array.js";
2
2
  import { ConsoleConfig } from "../Console.js";
3
- import { eqArrayNumber } from "../Eq.js";
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, WriteKey } from "./Owner.js";
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 { timestampToBinaryTimestamp } from "./Timestamp.js";
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: SqliteStorageDeps) =>
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 WriteKey} for the given
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: WriteKey }>(sql`
66
- select writeKey
67
- from evolu_writeKey
68
- where ownerId = ${ownerId};
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 eqArrayNumber(rows[0].writeKey, writeKey);
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
- writeMessages: (ownerId, messages) => {
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
- timestampToBinaryTimestamp(message.timestamp),
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
- ${timestampToBinaryTimestamp(message.timestamp)},
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
  };