@evolu/common 7.4.1 → 8.0.0-next.1

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 (250) hide show
  1. package/README.md +4 -61
  2. package/dist/src/Array.d.ts +343 -102
  3. package/dist/src/Array.d.ts.map +1 -1
  4. package/dist/src/Array.js +181 -85
  5. package/dist/src/Assert.d.ts +57 -11
  6. package/dist/src/Assert.d.ts.map +1 -1
  7. package/dist/src/Assert.js +47 -11
  8. package/dist/src/BigInt.d.ts +6 -1
  9. package/dist/src/BigInt.d.ts.map +1 -1
  10. package/dist/src/BigInt.js +5 -0
  11. package/dist/src/Brand.d.ts +40 -12
  12. package/dist/src/Brand.d.ts.map +1 -1
  13. package/dist/src/Brand.js +5 -0
  14. package/dist/src/Buffer.d.ts +6 -1
  15. package/dist/src/Buffer.d.ts.map +1 -1
  16. package/dist/src/Buffer.js +7 -2
  17. package/dist/src/Cache.d.ts +10 -2
  18. package/dist/src/Cache.d.ts.map +1 -1
  19. package/dist/src/Cache.js +6 -0
  20. package/dist/src/Callbacks.d.ts +10 -7
  21. package/dist/src/Callbacks.d.ts.map +1 -1
  22. package/dist/src/Callbacks.js +23 -13
  23. package/dist/src/Console.d.ts +361 -69
  24. package/dist/src/Console.d.ts.map +1 -1
  25. package/dist/src/Console.js +217 -117
  26. package/dist/src/Crypto.d.ts +70 -21
  27. package/dist/src/Crypto.d.ts.map +1 -1
  28. package/dist/src/Crypto.js +72 -27
  29. package/dist/src/Eq.d.ts +22 -2
  30. package/dist/src/Eq.d.ts.map +1 -1
  31. package/dist/src/Eq.js +21 -1
  32. package/dist/src/Error.d.ts +23 -12
  33. package/dist/src/Error.d.ts.map +1 -1
  34. package/dist/src/Error.js +27 -11
  35. package/dist/src/Function.d.ts +128 -56
  36. package/dist/src/Function.d.ts.map +1 -1
  37. package/dist/src/Function.js +118 -14
  38. package/dist/src/Identicon.d.ts +5 -0
  39. package/dist/src/Identicon.d.ts.map +1 -1
  40. package/dist/src/Identicon.js +6 -1
  41. package/dist/src/Lookup.d.ts +162 -0
  42. package/dist/src/Lookup.d.ts.map +1 -0
  43. package/dist/src/Lookup.js +200 -0
  44. package/dist/src/Microtask.d.ts +21 -0
  45. package/dist/src/Microtask.d.ts.map +1 -0
  46. package/dist/src/Microtask.js +37 -0
  47. package/dist/src/Number.d.ts +18 -4
  48. package/dist/src/Number.d.ts.map +1 -1
  49. package/dist/src/Number.js +23 -1
  50. package/dist/src/Object.d.ts +110 -5
  51. package/dist/src/Object.d.ts.map +1 -1
  52. package/dist/src/Object.js +108 -5
  53. package/dist/src/Option.d.ts +58 -0
  54. package/dist/src/Option.d.ts.map +1 -0
  55. package/dist/src/Option.js +43 -0
  56. package/dist/src/Order.d.ts +5 -0
  57. package/dist/src/Order.d.ts.map +1 -1
  58. package/dist/src/Order.js +8 -3
  59. package/dist/src/Platform.d.ts +36 -2
  60. package/dist/src/Platform.d.ts.map +1 -1
  61. package/dist/src/Platform.js +10 -6
  62. package/dist/src/Polyfills.d.ts +29 -0
  63. package/dist/src/Polyfills.d.ts.map +1 -0
  64. package/dist/src/Polyfills.js +329 -0
  65. package/dist/src/Random.d.ts +18 -7
  66. package/dist/src/Random.d.ts.map +1 -1
  67. package/dist/src/Random.js +9 -7
  68. package/dist/src/Redacted.d.ts +7 -4
  69. package/dist/src/Redacted.d.ts.map +1 -1
  70. package/dist/src/Redacted.js +5 -0
  71. package/dist/src/Ref.d.ts +31 -16
  72. package/dist/src/Ref.d.ts.map +1 -1
  73. package/dist/src/Ref.js +35 -7
  74. package/dist/src/RefCount.d.ts +62 -0
  75. package/dist/src/RefCount.d.ts.map +1 -0
  76. package/dist/src/RefCount.js +83 -0
  77. package/dist/src/Relation.d.ts +84 -29
  78. package/dist/src/Relation.d.ts.map +1 -1
  79. package/dist/src/Relation.js +83 -105
  80. package/dist/src/Resource.d.ts +263 -0
  81. package/dist/src/Resource.d.ts.map +1 -0
  82. package/dist/src/Resource.js +389 -0
  83. package/dist/src/Result.d.ts +390 -374
  84. package/dist/src/Result.d.ts.map +1 -1
  85. package/dist/src/Result.js +142 -70
  86. package/dist/src/Schedule.d.ts +953 -0
  87. package/dist/src/Schedule.d.ts.map +1 -0
  88. package/dist/src/Schedule.js +1199 -0
  89. package/dist/src/Set.d.ts +181 -0
  90. package/dist/src/Set.d.ts.map +1 -0
  91. package/dist/src/Set.js +137 -0
  92. package/dist/src/Skiplist.d.ts +6 -1
  93. package/dist/src/Skiplist.d.ts.map +1 -1
  94. package/dist/src/Skiplist.js +5 -0
  95. package/dist/src/Sqlite.d.ts +160 -60
  96. package/dist/src/Sqlite.d.ts.map +1 -1
  97. package/dist/src/Sqlite.js +290 -190
  98. package/dist/src/Store.d.ts +36 -28
  99. package/dist/src/Store.d.ts.map +1 -1
  100. package/dist/src/Store.js +55 -15
  101. package/dist/src/String.d.ts +5 -0
  102. package/dist/src/String.d.ts.map +1 -1
  103. package/dist/src/String.js +5 -0
  104. package/dist/src/Task.d.ts +2274 -423
  105. package/dist/src/Task.d.ts.map +1 -1
  106. package/dist/src/Task.js +1499 -348
  107. package/dist/src/Test.d.ts +150 -0
  108. package/dist/src/Test.d.ts.map +1 -0
  109. package/dist/src/Test.js +97 -0
  110. package/dist/src/Time.d.ts +202 -48
  111. package/dist/src/Time.d.ts.map +1 -1
  112. package/dist/src/Time.js +199 -88
  113. package/dist/src/Tracer.d.ts +48 -0
  114. package/dist/src/Tracer.d.ts.map +1 -0
  115. package/dist/src/Tracer.js +6 -0
  116. package/dist/src/Type.d.ts +587 -209
  117. package/dist/src/Type.d.ts.map +1 -1
  118. package/dist/src/Type.js +370 -240
  119. package/dist/src/Types.d.ts +160 -6
  120. package/dist/src/Types.d.ts.map +1 -1
  121. package/dist/src/Types.js +22 -2
  122. package/dist/src/WebSocket.d.ts +130 -58
  123. package/dist/src/WebSocket.d.ts.map +1 -1
  124. package/dist/src/WebSocket.js +274 -113
  125. package/dist/src/Worker.d.ts +245 -60
  126. package/dist/src/Worker.d.ts.map +1 -1
  127. package/dist/src/Worker.js +220 -64
  128. package/dist/src/index.d.ts +25 -3
  129. package/dist/src/index.d.ts.map +1 -1
  130. package/dist/src/index.js +19 -3
  131. package/dist/src/local-first/Db.d.ts +24 -213
  132. package/dist/src/local-first/Db.d.ts.map +1 -1
  133. package/dist/src/local-first/Db.js +621 -293
  134. package/dist/src/local-first/Error.d.ts +12 -0
  135. package/dist/src/local-first/Error.d.ts.map +1 -0
  136. package/dist/src/local-first/Error.js +6 -0
  137. package/dist/src/local-first/Evolu.d.ts +330 -285
  138. package/dist/src/local-first/Evolu.d.ts.map +1 -1
  139. package/dist/src/local-first/Evolu.js +472 -402
  140. package/dist/src/local-first/LocalAuth.d.ts +10 -10
  141. package/dist/src/local-first/LocalAuth.d.ts.map +1 -1
  142. package/dist/src/local-first/LocalAuth.js +6 -1
  143. package/dist/src/local-first/Owner.d.ts +70 -27
  144. package/dist/src/local-first/Owner.d.ts.map +1 -1
  145. package/dist/src/local-first/Owner.js +27 -14
  146. package/dist/src/local-first/Protocol.d.ts +58 -50
  147. package/dist/src/local-first/Protocol.d.ts.map +1 -1
  148. package/dist/src/local-first/Protocol.js +253 -155
  149. package/dist/src/local-first/Query.d.ts +133 -43
  150. package/dist/src/local-first/Query.d.ts.map +1 -1
  151. package/dist/src/local-first/Query.js +139 -90
  152. package/dist/src/local-first/Relay.d.ts +34 -49
  153. package/dist/src/local-first/Relay.d.ts.map +1 -1
  154. package/dist/src/local-first/Relay.js +59 -177
  155. package/dist/src/local-first/Schema.d.ts +139 -144
  156. package/dist/src/local-first/Schema.d.ts.map +1 -1
  157. package/dist/src/local-first/Schema.js +78 -163
  158. package/dist/src/local-first/Shared.d.ts +125 -0
  159. package/dist/src/local-first/Shared.d.ts.map +1 -0
  160. package/dist/src/local-first/Shared.js +569 -0
  161. package/dist/src/local-first/Storage.d.ts +54 -58
  162. package/dist/src/local-first/Storage.d.ts.map +1 -1
  163. package/dist/src/local-first/Storage.js +127 -179
  164. package/dist/src/local-first/Timestamp.d.ts +28 -35
  165. package/dist/src/local-first/Timestamp.d.ts.map +1 -1
  166. package/dist/src/local-first/Timestamp.js +24 -25
  167. package/dist/src/local-first/index.d.ts +2 -15
  168. package/dist/src/local-first/index.d.ts.map +1 -1
  169. package/dist/src/local-first/index.js +2 -15
  170. package/package.json +37 -24
  171. package/src/Array.ts +467 -106
  172. package/src/Assert.ts +87 -12
  173. package/src/BigInt.ts +7 -1
  174. package/src/Brand.ts +41 -12
  175. package/src/Buffer.ts +10 -4
  176. package/src/Cache.ts +11 -2
  177. package/src/Callbacks.ts +33 -23
  178. package/src/Console.ts +567 -178
  179. package/src/Crypto.ts +111 -62
  180. package/src/Eq.ts +30 -3
  181. package/src/Error.ts +50 -34
  182. package/src/Function.ts +140 -68
  183. package/src/Identicon.ts +7 -1
  184. package/src/Lookup.ts +429 -0
  185. package/src/Microtask.ts +58 -0
  186. package/src/Number.ts +40 -7
  187. package/src/Object.ts +155 -16
  188. package/src/Option.ts +74 -0
  189. package/src/Order.ts +11 -3
  190. package/src/Platform.ts +43 -7
  191. package/src/Polyfills.ts +520 -0
  192. package/src/Random.ts +25 -11
  193. package/src/Redacted.ts +8 -4
  194. package/src/Ref.ts +72 -21
  195. package/src/RefCount.ts +170 -0
  196. package/src/Relation.ts +188 -136
  197. package/src/Resource.ts +864 -0
  198. package/src/Result.ts +548 -381
  199. package/src/Schedule.ts +1471 -0
  200. package/src/Set.ts +247 -0
  201. package/src/Skiplist.ts +7 -1
  202. package/src/Sqlite.ts +409 -304
  203. package/src/Store.ts +94 -48
  204. package/src/String.ts +6 -0
  205. package/src/Task.ts +3696 -740
  206. package/src/Test.ts +202 -0
  207. package/src/Time.ts +357 -147
  208. package/src/Tracer.ts +54 -0
  209. package/src/Type.ts +866 -430
  210. package/src/Types.ts +202 -7
  211. package/src/WebSocket.ts +404 -167
  212. package/src/Worker.ts +527 -147
  213. package/src/index.ts +74 -3
  214. package/src/local-first/Db.ts +867 -597
  215. package/src/local-first/Error.ts +17 -0
  216. package/src/local-first/Evolu.ts +884 -824
  217. package/src/local-first/LocalAuth.ts +10 -10
  218. package/src/local-first/Owner.ts +94 -42
  219. package/src/local-first/Protocol.ts +441 -307
  220. package/src/local-first/Query.ts +243 -194
  221. package/src/local-first/Relay.ts +119 -273
  222. package/src/local-first/Schema.ts +284 -394
  223. package/src/local-first/Shared.ts +932 -0
  224. package/src/local-first/Storage.ts +211 -283
  225. package/src/local-first/Timestamp.ts +38 -45
  226. package/src/local-first/index.ts +2 -16
  227. package/dist/src/Instances.d.ts +0 -34
  228. package/dist/src/Instances.d.ts.map +0 -1
  229. package/dist/src/Instances.js +0 -44
  230. package/dist/src/Resources.d.ts +0 -118
  231. package/dist/src/Resources.d.ts.map +0 -1
  232. package/dist/src/Resources.js +0 -197
  233. package/dist/src/local-first/Platform.d.ts +0 -25
  234. package/dist/src/local-first/Platform.d.ts.map +0 -1
  235. package/dist/src/local-first/Platform.js +0 -1
  236. package/dist/src/local-first/Public.d.ts +0 -22
  237. package/dist/src/local-first/Public.d.ts.map +0 -1
  238. package/dist/src/local-first/Public.js +0 -15
  239. package/dist/src/local-first/PublicKysely.d.ts +0 -148
  240. package/dist/src/local-first/PublicKysely.d.ts.map +0 -1
  241. package/dist/src/local-first/PublicKysely.js +0 -184
  242. package/dist/src/local-first/Sync.d.ts +0 -112
  243. package/dist/src/local-first/Sync.d.ts.map +0 -1
  244. package/dist/src/local-first/Sync.js +0 -526
  245. package/src/Instances.ts +0 -92
  246. package/src/Resources.ts +0 -367
  247. package/src/local-first/Platform.ts +0 -27
  248. package/src/local-first/Public.ts +0 -42
  249. package/src/local-first/PublicKysely.ts +0 -239
  250. package/src/local-first/Sync.ts +0 -949
package/src/Test.ts ADDED
@@ -0,0 +1,202 @@
1
+ /**
2
+ * Test utilities for deterministic testing.
3
+ *
4
+ * @module
5
+ */
6
+
7
+ import type { Brand } from "./Brand.js";
8
+ import {
9
+ testCreateConsole,
10
+ type TestConsole,
11
+ type TestConsoleDep,
12
+ } from "./Console.js";
13
+ import { testCreateRandomBytes, type RandomBytes } from "./Crypto.js";
14
+ import {
15
+ testCreateRandom,
16
+ testCreateRandomLib,
17
+ type Random,
18
+ type RandomLibDep,
19
+ } from "./Random.js";
20
+ import { createRun, type Run, type RunDeps } from "./Task.js";
21
+ import {
22
+ minMillis,
23
+ setTimeout,
24
+ testCreateTime,
25
+ type Duration,
26
+ type TestTime,
27
+ type TestTimeDep,
28
+ } from "./Time.js";
29
+ import { createId, type Id } from "./Type.js";
30
+
31
+ export type TestCreateId = <B extends string = never>() => [B] extends [never]
32
+ ? Id
33
+ : Id & Brand<B>;
34
+
35
+ /**
36
+ * Cheap deterministic baseline deps for tests.
37
+ *
38
+ * `TestDeps` includes test-friendly replacements for {@link RunDeps}, such as
39
+ * {@link TestConsole} and {@link TestTime}, plus extra helpers commonly useful in
40
+ * tests and fixtures, such as `randomLib` (only that for now).
41
+ *
42
+ * Use it directly for synchronous test setup, fixtures, and helpers. It is also
43
+ * intentionally the base deps used by {@link testCreateRun}.
44
+ */
45
+ export type TestDeps = Omit<RunDeps, "console" | "time"> &
46
+ TestConsoleDep &
47
+ TestTimeDep &
48
+ RandomLibDep;
49
+
50
+ /**
51
+ * Creates deterministic {@link TestDeps}.
52
+ *
53
+ * Each call creates fresh instances, so tests don't share state.
54
+ *
55
+ * Includes these deps:
56
+ *
57
+ * - `console`: {@link TestConsole}
58
+ * - `random`: {@link Random}
59
+ * - `randomLib`: seeded `random` package instance
60
+ * - `randomBytes`: {@link RandomBytes}
61
+ * - `time`: {@link TestTime}
62
+ *
63
+ * Use this for synchronous code that accepts deps directly. For tests that run
64
+ * Tasks, use {@link testCreateRun}.
65
+ *
66
+ * ### Example
67
+ *
68
+ * ```ts
69
+ * test("Callbacks with no argument", () => {
70
+ * const deps = testCreateDeps();
71
+ * const callbacks = createCallbacks(deps);
72
+ *
73
+ * let called = false;
74
+ * const id = callbacks.register(() => {
75
+ * called = true;
76
+ * });
77
+ *
78
+ * callbacks.execute(id);
79
+ * expect(called).toBe(true);
80
+ * });
81
+ * ```
82
+ */
83
+ export const testCreateDeps = (options?: {
84
+ readonly seed?: string;
85
+ }): TestDeps => {
86
+ const seed = options?.seed ?? "evolu";
87
+ const console = testCreateConsole();
88
+ const random = testCreateRandom(seed);
89
+ const randomLib = testCreateRandomLib(seed);
90
+ const randomBytes = testCreateRandomBytes({ randomLib });
91
+ const time = testCreateTime();
92
+ return { console, randomBytes, random, randomLib, time };
93
+ };
94
+
95
+ /**
96
+ * Creates a test {@link Run} with deterministic default deps or optional custom
97
+ * deps.
98
+ *
99
+ * Use this as the default composition root in tests. Each call creates fresh
100
+ * {@link TestDeps} via {@link testCreateDeps}, then merges any provided custom
101
+ * deps.
102
+ *
103
+ * ### Example
104
+ *
105
+ * ```ts
106
+ * // Built-in TestTime
107
+ * await using run = testCreateRun();
108
+ * const fiber = run(sleep("1s"));
109
+ * run.deps.time.advance("1s");
110
+ * await fiber;
111
+ *
112
+ * // For a single test, create the dependency using the Run.
113
+ * await using run = testCreateRun();
114
+ * await using foo = await run.orThrow(createFoo());
115
+ * const runWithFoo = run.addDeps({ foo });
116
+ *
117
+ * expect(runWithFoo.deps.foo).toBe(foo);
118
+ *
119
+ * // If multiple tests need the same setup, create a disposable helper.
120
+ * // Then `await using setup` disposes everything the helper owns.
121
+ * const setupFoo = async () => {
122
+ * await using stack = new AsyncDisposableStack();
123
+ * const run = stack.use(testCreateRun());
124
+ * const foo = stack.use(await run.orThrow(createFoo()));
125
+ * const moved = stack.move();
126
+ *
127
+ * // Return whatever the tests need: a Run with the dependency,
128
+ * // the dependency itself, or both.
129
+ * return {
130
+ * run: run.addDeps({ foo }),
131
+ * foo,
132
+ * [Symbol.asyncDispose]: () => moved.disposeAsync(),
133
+ * };
134
+ * };
135
+ *
136
+ * await using setup = await setupFoo();
137
+ * const { run, foo } = setup;
138
+ * expect(run.deps.foo).toBe(foo);
139
+ * ```
140
+ *
141
+ * Name reusable setup helpers after what they set up:
142
+ *
143
+ * - `setupFoo` for reusable test setup for `Foo`
144
+ * - `testSetupFoo` when a library module exports the helper as part of its public
145
+ * test API, e.g. `testSetupSqlite`.
146
+ */
147
+ export function testCreateRun(): Run<TestDeps>;
148
+
149
+ /** With custom dependencies merged into {@link TestDeps}. */
150
+ export function testCreateRun<D>(deps: D): Run<TestDeps & D>;
151
+
152
+ export function testCreateRun<D>(deps?: D): Run<TestDeps & D> {
153
+ const defaults = testCreateDeps();
154
+ return createRun<TestDeps & D>({ ...defaults, ...deps } as TestDeps & D);
155
+ }
156
+
157
+ /**
158
+ * Returns a Promise that resolves after a macrotask delay.
159
+ *
160
+ * Use this to model a real async boundary in a test double, for example an
161
+ * async disposer that should not complete in the same turn.
162
+ *
163
+ * Avoid using it to coordinate assertions. Waiting for a macrotask tends to
164
+ * make tests indirect and brittle.
165
+ */
166
+ export const testWaitForMacrotask = (
167
+ duration: Duration = minMillis,
168
+ ): Promise<void> => setTimeout(duration);
169
+
170
+ /**
171
+ * Creates a deterministic `createId` helper.
172
+ *
173
+ * The returned function mirrors {@link createId}, but uses stable test entropy
174
+ * so each call yields the next deterministic pseudo-random id.
175
+ *
176
+ * Create one helper per test, or per reusable test setup helper such as
177
+ * `setupFoo`, so deterministic ids stay local to that setup.
178
+ *
179
+ * Avoid sharing one helper across a whole test file. Adding an extra `createId`
180
+ * call in one test would shift ids used by unrelated tests later in the file.
181
+ *
182
+ * ### Example
183
+ *
184
+ * ```ts
185
+ * test("creates stable ids", () => {
186
+ * const createId = testCreateId();
187
+ *
188
+ * const callbackId = createId();
189
+ * const secondCallbackId = createId();
190
+ * const todoId = createId<"Todo">();
191
+ * });
192
+ * ```
193
+ */
194
+ export const testCreateId = (): TestCreateId => {
195
+ const randomBytes = testCreateRandomBytes({
196
+ randomLib: testCreateRandomLib(),
197
+ });
198
+
199
+ return <B extends string = never>(): [B] extends [never]
200
+ ? Id
201
+ : Id & Brand<B> => createId<B>({ randomBytes });
202
+ };