@evolu/common 6.0.1-preview.2 → 6.0.1-preview.21

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 +159 -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 +209 -124
  35. package/dist/src/Evolu/Owner.d.ts.map +1 -1
  36. package/dist/src/Evolu/Owner.js +118 -104
  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 +256 -211
  40. package/dist/src/Evolu/Protocol.d.ts.map +1 -1
  41. package/dist/src/Evolu/Protocol.js +558 -359
  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 +72 -4
  49. package/dist/src/Evolu/Relay.d.ts.map +1 -1
  50. package/dist/src/Evolu/Relay.js +124 -13
  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 +68 -13
  58. package/dist/src/Evolu/Sync.d.ts.map +1 -1
  59. package/dist/src/Evolu/Sync.js +414 -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 +176 -51
  74. package/dist/src/Result.d.ts.map +1 -1
  75. package/dist/src/Result.js +30 -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 +474 -327
  86. package/dist/src/Type.d.ts.map +1 -1
  87. package/dist/src/Type.js +502 -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 +528 -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 +282 -227
  112. package/src/Evolu/Platform.ts +9 -9
  113. package/src/Evolu/Protocol.ts +788 -640
  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 +238 -18
  118. package/src/Evolu/Schema.ts +391 -191
  119. package/src/Evolu/Storage.ts +275 -55
  120. package/src/Evolu/Sync.ts +708 -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 +183 -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 +708 -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
package/src/Sqlite.ts CHANGED
@@ -1,17 +1,10 @@
1
+ import { Brand } from "./Brand.js";
1
2
  import { ConsoleDep } from "./Console.js";
3
+ import { EncryptionKey } from "./Crypto.js";
2
4
  import { createTransferableError, TransferableError } from "./Error.js";
3
5
  import { err, ok, Result, tryAsync, trySync } from "./Result.js";
4
- import {
5
- Boolean,
6
- Null,
7
- Number,
8
- SimpleName,
9
- String,
10
- transform,
11
- Uint8Array,
12
- union,
13
- } from "./Type.js";
14
- import { Brand, Predicate, IntentionalNever } from "./Types.js";
6
+ import { Null, Number, SimpleName, String, Uint8Array, union } from "./Type.js";
7
+ import { IntentionalNever, Predicate } from "./Types.js";
15
8
 
16
9
  /**
17
10
  * SQLite driver interface. This is the minimal interface that platform-specific
@@ -33,6 +26,7 @@ export interface CreateSqliteDriverDep {
33
26
 
34
27
  export interface SqliteDriverOptions {
35
28
  memory?: boolean;
29
+ encryptionKey?: EncryptionKey | undefined;
36
30
  }
37
31
 
38
32
  /**
@@ -148,13 +142,13 @@ export const createSqlite =
148
142
  exec: (query) =>
149
143
  trySync(
150
144
  () => {
151
- deps.console?.log("[sql]", query);
145
+ deps.console?.log("[sql]", { query });
152
146
 
153
147
  const result = maybeLogSqliteQueryExecutionTime(query, () =>
154
148
  driver.exec(query, isSqlMutation(query.sql)),
155
149
  );
156
150
 
157
- deps.console?.log("[sql]", result);
151
+ deps.console?.log("[sql]", { result });
158
152
 
159
153
  return result as IntentionalNever;
160
154
  },
@@ -366,8 +360,38 @@ const isSqlMutationRegEx = new RegExp(
366
360
  "i",
367
361
  );
368
362
 
363
+ /**
364
+ * Removes SQL line comments (--) from a SQL string without using regex to avoid
365
+ * ReDoS vulnerabilities.
366
+ */
367
+ const removeSqlComments = (sql: string): string => {
368
+ let result = "";
369
+ let i = 0;
370
+
371
+ while (i < sql.length) {
372
+ // Check for comment start
373
+ if (i < sql.length - 1 && sql[i] === "-" && sql[i + 1] === "-") {
374
+ // Skip until end of line or end of string
375
+ i += 2;
376
+ while (i < sql.length && sql[i] !== "\n") {
377
+ i++;
378
+ }
379
+ // Keep the newline if present
380
+ if (i < sql.length && sql[i] === "\n") {
381
+ result += "\n";
382
+ i++;
383
+ }
384
+ } else {
385
+ result += sql[i];
386
+ i++;
387
+ }
388
+ }
389
+
390
+ return result;
391
+ };
392
+
369
393
  export const isSqlMutation: Predicate<string> = (sql) =>
370
- isSqlMutationRegEx.test(sql.replace(/--.*$/gm, ""));
394
+ isSqlMutationRegEx.test(removeSqlComments(sql));
371
395
 
372
396
  export interface SqliteQueryPlanRow {
373
397
  id: number;
@@ -385,13 +409,14 @@ export const explainSqliteQueryPlan =
385
409
  if (!result.ok) return result;
386
410
 
387
411
  // eslint-disable-next-line no-console
388
- console.log("ExplainQueryPlan", query);
412
+ console.log("[explainSqliteQueryPlan]", query);
389
413
  // eslint-disable-next-line no-console
390
414
  console.log(
391
415
  drawSqliteQueryPlan(
392
416
  result.value.rows as unknown as Array<SqliteQueryPlanRow>,
393
417
  ),
394
418
  );
419
+
395
420
  return ok();
396
421
  };
397
422
 
@@ -415,15 +440,26 @@ const drawSqliteQueryPlan = (rows: Array<SqliteQueryPlanRow>): string =>
415
440
 
416
441
  /**
417
442
  * SQLite represents boolean values using `0` (false) and `1` (true) instead of
418
- * a dedicated boolean type. This transform Type ensures that values conform to
419
- * SQLite's boolean representation.
443
+ * a dedicated boolean type.
444
+ *
445
+ * Use {@link sqliteTrue} and {@link sqliteFalse} constants for better
446
+ * readability.
420
447
  *
421
448
  * See: https://www.sqlite.org/quirks.html#no_separate_boolean_datatype
422
449
  */
423
- export const SqliteBoolean = transform(
424
- Boolean,
425
- union(0, 1),
426
- (value) => ok(value ? (1 as const) : (0 as const)),
427
- (value) => value === 1,
428
- );
450
+ export const SqliteBoolean = union(0, 1);
429
451
  export type SqliteBoolean = typeof SqliteBoolean.Type;
452
+
453
+ /**
454
+ * Represents the {@link SqliteBoolean} value for `true`.
455
+ *
456
+ * See {@link SqliteBoolean}.
457
+ */
458
+ export const sqliteTrue = 1;
459
+
460
+ /**
461
+ * Represents the {@link SqliteBoolean} value for `false`.
462
+ *
463
+ * See {@link SqliteBoolean}.
464
+ */
465
+ export const sqliteFalse = 0;