@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.
Files changed (237) hide show
  1. package/dist/src/Array.d.ts +279 -173
  2. package/dist/src/Array.d.ts.map +1 -1
  3. package/dist/src/Array.js +105 -101
  4. package/dist/src/Assert.d.ts +44 -44
  5. package/dist/src/Assert.d.ts.map +1 -1
  6. package/dist/src/Assert.js +44 -32
  7. package/dist/src/BigInt.d.ts +6 -3
  8. package/dist/src/BigInt.d.ts.map +1 -1
  9. package/dist/src/BigInt.js +5 -2
  10. package/dist/src/Brand.d.ts +27 -40
  11. package/dist/src/Brand.d.ts.map +1 -1
  12. package/dist/src/Buffer.d.ts +27 -11
  13. package/dist/src/Buffer.d.ts.map +1 -1
  14. package/dist/src/Cache.d.ts +9 -4
  15. package/dist/src/Cache.d.ts.map +1 -1
  16. package/dist/src/Cache.js +8 -3
  17. package/dist/src/Callbacks.d.ts +30 -19
  18. package/dist/src/Callbacks.d.ts.map +1 -1
  19. package/dist/src/Console.d.ts +145 -66
  20. package/dist/src/Console.d.ts.map +1 -1
  21. package/dist/src/Console.js +91 -43
  22. package/dist/src/Crypto.d.ts +85 -42
  23. package/dist/src/Crypto.d.ts.map +1 -1
  24. package/dist/src/Crypto.js +44 -20
  25. package/dist/src/Eq.d.ts +87 -21
  26. package/dist/src/Eq.d.ts.map +1 -1
  27. package/dist/src/Eq.js +67 -19
  28. package/dist/src/Error.d.ts +5 -3
  29. package/dist/src/Error.d.ts.map +1 -1
  30. package/dist/src/Error.js +16 -0
  31. package/dist/src/Function.d.ts +119 -58
  32. package/dist/src/Function.d.ts.map +1 -1
  33. package/dist/src/Function.js +86 -43
  34. package/dist/src/Http.d.ts +394 -0
  35. package/dist/src/Http.d.ts.map +1 -0
  36. package/dist/src/Http.js +102 -0
  37. package/dist/src/Identicon.d.ts +16 -7
  38. package/dist/src/Identicon.d.ts.map +1 -1
  39. package/dist/src/Identicon.js +14 -5
  40. package/dist/src/LeakDetector.d.ts +88 -0
  41. package/dist/src/LeakDetector.d.ts.map +1 -0
  42. package/dist/src/LeakDetector.js +86 -0
  43. package/dist/src/LockManager.d.ts +18 -9
  44. package/dist/src/LockManager.d.ts.map +1 -1
  45. package/dist/src/LockManager.js +7 -8
  46. package/dist/src/Lookup.d.ts +43 -13
  47. package/dist/src/Lookup.d.ts.map +1 -1
  48. package/dist/src/Lookup.js +28 -8
  49. package/dist/src/Microtask.d.ts +1 -1
  50. package/dist/src/Microtask.d.ts.map +1 -1
  51. package/dist/src/Microtask.js +3 -7
  52. package/dist/src/Number.d.ts +49 -13
  53. package/dist/src/Number.d.ts.map +1 -1
  54. package/dist/src/Number.js +34 -5
  55. package/dist/src/Object.d.ts +160 -40
  56. package/dist/src/Object.d.ts.map +1 -1
  57. package/dist/src/Object.js +92 -46
  58. package/dist/src/Option.d.ts +10 -7
  59. package/dist/src/Option.d.ts.map +1 -1
  60. package/dist/src/Option.js +9 -6
  61. package/dist/src/Order.d.ts +21 -21
  62. package/dist/src/Order.d.ts.map +1 -1
  63. package/dist/src/Order.js +18 -21
  64. package/dist/src/Platform.d.ts +31 -0
  65. package/dist/src/Platform.d.ts.map +1 -1
  66. package/dist/src/Platform.js +98 -0
  67. package/dist/src/Random.d.ts +32 -17
  68. package/dist/src/Random.d.ts.map +1 -1
  69. package/dist/src/Random.js +12 -8
  70. package/dist/src/Redacted.d.ts +43 -29
  71. package/dist/src/Redacted.d.ts.map +1 -1
  72. package/dist/src/Redacted.js +14 -3
  73. package/dist/src/Ref.d.ts +11 -3
  74. package/dist/src/Ref.d.ts.map +1 -1
  75. package/dist/src/Ref.js +2 -2
  76. package/dist/src/RefCount.d.ts +2 -6
  77. package/dist/src/RefCount.d.ts.map +1 -1
  78. package/dist/src/RefCount.js +33 -90
  79. package/dist/src/Relation.d.ts +70 -5
  80. package/dist/src/Relation.d.ts.map +1 -1
  81. package/dist/src/Relation.js +77 -2
  82. package/dist/src/Resource.d.ts +590 -155
  83. package/dist/src/Resource.d.ts.map +1 -1
  84. package/dist/src/Resource.js +592 -343
  85. package/dist/src/Result.d.ts +838 -191
  86. package/dist/src/Result.d.ts.map +1 -1
  87. package/dist/src/Result.js +163 -102
  88. package/dist/src/Schedule.d.ts +684 -231
  89. package/dist/src/Schedule.d.ts.map +1 -1
  90. package/dist/src/Schedule.js +766 -295
  91. package/dist/src/Set.d.ts +89 -40
  92. package/dist/src/Set.d.ts.map +1 -1
  93. package/dist/src/Set.js +50 -29
  94. package/dist/src/Sqlite.d.ts +29 -72
  95. package/dist/src/Sqlite.d.ts.map +1 -1
  96. package/dist/src/Sqlite.js +41 -46
  97. package/dist/src/StackTrace.d.ts +40 -0
  98. package/dist/src/StackTrace.d.ts.map +1 -0
  99. package/dist/src/StackTrace.js +140 -0
  100. package/dist/src/Store.d.ts +5 -4
  101. package/dist/src/Store.d.ts.map +1 -1
  102. package/dist/src/Store.js +5 -4
  103. package/dist/src/String.d.ts +7 -1
  104. package/dist/src/String.d.ts.map +1 -1
  105. package/dist/src/String.js +20 -7
  106. package/dist/src/Task.d.ts +3572 -1770
  107. package/dist/src/Task.d.ts.map +1 -1
  108. package/dist/src/Task.js +1879 -1274
  109. package/dist/src/Test.d.ts +17 -125
  110. package/dist/src/Test.d.ts.map +1 -1
  111. package/dist/src/Test.js +17 -68
  112. package/dist/src/Time.d.ts +108 -49
  113. package/dist/src/Time.d.ts.map +1 -1
  114. package/dist/src/Time.js +172 -73
  115. package/dist/src/Type.d.ts +3542 -1926
  116. package/dist/src/Type.d.ts.map +1 -1
  117. package/dist/src/Type.js +3070 -1784
  118. package/dist/src/Types.d.ts +200 -108
  119. package/dist/src/Types.d.ts.map +1 -1
  120. package/dist/src/Types.js +39 -8
  121. package/dist/src/WebSocket.d.ts +48 -23
  122. package/dist/src/WebSocket.d.ts.map +1 -1
  123. package/dist/src/WebSocket.js +15 -6
  124. package/dist/src/Worker.d.ts +94 -36
  125. package/dist/src/Worker.d.ts.map +1 -1
  126. package/dist/src/Worker.js +58 -24
  127. package/dist/src/index.d.ts +55 -52
  128. package/dist/src/index.d.ts.map +1 -1
  129. package/dist/src/index.js +3 -1
  130. package/dist/src/intl/cs.d.ts +94 -0
  131. package/dist/src/intl/cs.d.ts.map +1 -0
  132. package/dist/src/intl/cs.js +220 -0
  133. package/dist/src/intl/index.d.ts +7 -0
  134. package/dist/src/intl/index.d.ts.map +1 -0
  135. package/dist/src/intl/index.js +6 -0
  136. package/dist/src/local-first/Db.d.ts +12 -8
  137. package/dist/src/local-first/Db.d.ts.map +1 -1
  138. package/dist/src/local-first/Db.js +109 -132
  139. package/dist/src/local-first/Error.d.ts +4 -4
  140. package/dist/src/local-first/Evolu.d.ts +338 -92
  141. package/dist/src/local-first/Evolu.d.ts.map +1 -1
  142. package/dist/src/local-first/Evolu.js +47 -55
  143. package/dist/src/local-first/LocalAuth.d.ts +4 -4
  144. package/dist/src/local-first/LocalAuth.d.ts.map +1 -1
  145. package/dist/src/local-first/Owner.d.ts +61 -24
  146. package/dist/src/local-first/Owner.d.ts.map +1 -1
  147. package/dist/src/local-first/Owner.js +45 -8
  148. package/dist/src/local-first/Protocol.d.ts +44 -31
  149. package/dist/src/local-first/Protocol.d.ts.map +1 -1
  150. package/dist/src/local-first/Protocol.js +36 -13
  151. package/dist/src/local-first/Query.d.ts +147 -62
  152. package/dist/src/local-first/Query.d.ts.map +1 -1
  153. package/dist/src/local-first/Query.js +112 -58
  154. package/dist/src/local-first/Relay.d.ts +44 -14
  155. package/dist/src/local-first/Relay.d.ts.map +1 -1
  156. package/dist/src/local-first/Schema.d.ts +92 -41
  157. package/dist/src/local-first/Schema.d.ts.map +1 -1
  158. package/dist/src/local-first/Schema.js +25 -8
  159. package/dist/src/local-first/Shared.d.ts +23 -21
  160. package/dist/src/local-first/Shared.d.ts.map +1 -1
  161. package/dist/src/local-first/Shared.js +100 -57
  162. package/dist/src/local-first/Storage.d.ts +53 -32
  163. package/dist/src/local-first/Storage.d.ts.map +1 -1
  164. package/dist/src/local-first/Storage.js +194 -234
  165. package/dist/src/local-first/Timestamp.d.ts +21 -21
  166. package/dist/src/local-first/Timestamp.d.ts.map +1 -1
  167. package/dist/src/local-first/Timestamp.js +2 -2
  168. package/dist/src/local-first/index.d.ts +11 -11
  169. package/package.json +14 -13
  170. package/src/Array.ts +306 -205
  171. package/src/Assert.ts +54 -69
  172. package/src/BigInt.ts +6 -3
  173. package/src/Brand.ts +27 -40
  174. package/src/Buffer.ts +28 -12
  175. package/src/Cache.ts +9 -4
  176. package/src/Callbacks.ts +30 -19
  177. package/src/Console.ts +156 -84
  178. package/src/Crypto.ts +83 -41
  179. package/src/Eq.ts +95 -22
  180. package/src/Error.ts +8 -4
  181. package/src/Function.ts +167 -59
  182. package/src/Http.ts +549 -0
  183. package/src/Identicon.ts +16 -7
  184. package/src/LeakDetector.ts +182 -0
  185. package/src/LockManager.ts +43 -39
  186. package/src/Lookup.ts +62 -28
  187. package/src/Microtask.ts +19 -23
  188. package/src/Number.ts +64 -12
  189. package/src/Object.ts +173 -49
  190. package/src/Option.ts +10 -7
  191. package/src/Order.ts +23 -21
  192. package/src/Platform.ts +136 -0
  193. package/src/Polyfills.ts +6 -12
  194. package/src/Random.ts +32 -19
  195. package/src/Redacted.ts +44 -30
  196. package/src/Ref.ts +14 -6
  197. package/src/RefCount.ts +38 -80
  198. package/src/Relation.ts +202 -10
  199. package/src/Resource.ts +1199 -591
  200. package/src/Result.ts +930 -271
  201. package/src/Schedule.ts +859 -343
  202. package/src/Set.ts +89 -40
  203. package/src/Sqlite.ts +144 -138
  204. package/src/StackTrace.ts +230 -0
  205. package/src/Store.ts +8 -7
  206. package/src/String.ts +22 -5
  207. package/src/Task.ts +5101 -2832
  208. package/src/Test.ts +20 -168
  209. package/src/Time.ts +293 -90
  210. package/src/Type.ts +10034 -4117
  211. package/src/Types.ts +227 -110
  212. package/src/WebSocket.ts +64 -36
  213. package/src/Worker.ts +180 -72
  214. package/src/index.ts +55 -52
  215. package/src/intl/cs.ts +388 -0
  216. package/src/intl/index.ts +7 -0
  217. package/src/local-first/Db.ts +152 -179
  218. package/src/local-first/Error.ts +4 -4
  219. package/src/local-first/Evolu.ts +495 -250
  220. package/src/local-first/LocalAuth.ts +6 -12
  221. package/src/local-first/Owner.ts +58 -21
  222. package/src/local-first/Protocol.ts +49 -31
  223. package/src/local-first/Query.ts +146 -61
  224. package/src/local-first/Relay.ts +51 -21
  225. package/src/local-first/Schema.ts +109 -51
  226. package/src/local-first/Shared.ts +266 -226
  227. package/src/local-first/Storage.ts +266 -264
  228. package/src/local-first/Timestamp.ts +25 -18
  229. package/src/local-first/index.ts +11 -11
  230. package/dist/src/Skiplist.d.ts +0 -28
  231. package/dist/src/Skiplist.d.ts.map +0 -1
  232. package/dist/src/Skiplist.js +0 -64
  233. package/dist/src/Tracer.d.ts +0 -48
  234. package/dist/src/Tracer.d.ts.map +0 -1
  235. package/dist/src/Tracer.js +0 -6
  236. package/src/Skiplist.ts +0 -108
  237. package/src/Tracer.ts +0 -54
@@ -62,14 +62,21 @@ export const createConsole = ({ name = "", level = "log", output = createNativeC
62
62
  * ### Example
63
63
  *
64
64
  * ```ts
65
+ * import {
66
+ * createNativeConsoleOutput,
67
+ * type ConsoleOutput,
68
+ * } from "@evolu/common";
69
+ *
65
70
  * const output = createNativeConsoleOutput();
71
+ *
72
+ * expectTypeOf(output).toEqualTypeOf<ConsoleOutput>();
73
+ * expect(output.write).toBeTypeOf("function");
66
74
  * ```
67
75
  */
68
76
  export const createNativeConsoleOutput = () => ({
69
77
  write: (entry, formatter) => {
70
78
  const args = formatter ? formatter(entry) : entry.args;
71
- const fn = globalThis.console[entry.method];
72
- fn(...args);
79
+ globalThis.console[entry.method](...args);
73
80
  },
74
81
  });
75
82
  /**
@@ -80,23 +87,52 @@ export const createNativeConsoleOutput = () => ({
80
87
  * ### Example
81
88
  *
82
89
  * ```ts
83
- * const root = createConsole({
84
- * formatter: createConsoleFormatter()({
85
- * timestampFormat: "relative",
86
- * }),
87
- * });
90
+ * import {
91
+ * createConsoleFormatter,
92
+ * Millis,
93
+ * testCreateTime,
94
+ * type ConsoleEntry,
95
+ * } from "@evolu/common";
88
96
  *
89
- * // Relative elapsed since start
90
- * const relay = root.child("relay");
91
- * relay.log("connected"); // +0.000s [relay] connected
92
- * relay.log("synced"); // +1.500s [relay] synced
97
+ * const time = testCreateTime({ startAt: Millis.orThrow(0) });
98
+ * const formatRelative = createConsoleFormatter({ time })({
99
+ * timestampFormat: "relative",
100
+ * });
101
+ * const connected: ConsoleEntry = {
102
+ * method: "log",
103
+ * path: ["relay"],
104
+ * args: ["connected"],
105
+ * };
93
106
  *
94
- * // Nested children
95
- * const db = relay.child("db");
96
- * db.log("opened"); // +1.500s [relay] [db] opened
107
+ * expect(formatRelative(connected)).toEqual([
108
+ * "+0.000s [relay]",
109
+ * "connected",
110
+ * ]);
111
+ * time.advance("1.5s");
112
+ * expect(
113
+ * formatRelative({
114
+ * ...connected,
115
+ * path: ["relay", "db"],
116
+ * args: ["opened"],
117
+ * }),
118
+ * ).toEqual(["+1.500s [relay] [db]", "opened"]);
97
119
  *
98
- * // Absolute local clock time (HH:MM:SS.mmm)
99
- * // relay.log("connected"); // 15:30:15.123 [relay] connected
120
+ * const localTimestamp = new globalThis.Date(
121
+ * 2026,
122
+ * 0,
123
+ * 28,
124
+ * 14,
125
+ * 30,
126
+ * 15,
127
+ * 123,
128
+ * ).getTime();
129
+ * const formatAbsolute = createConsoleFormatter({
130
+ * time: testCreateTime({ startAt: Millis.orThrow(localTimestamp) }),
131
+ * })({ timestampFormat: "absolute" });
132
+ * expect(formatAbsolute(connected)).toEqual([
133
+ * "14:30:15.123 [relay]",
134
+ * "connected",
135
+ * ]);
100
136
  * ```
101
137
  */
102
138
  export const createConsoleFormatter = ({ time = createTime() } = {}) => (config = {}) => {
@@ -141,11 +177,20 @@ export const createConsoleStoreOutput = () => {
141
177
  * ### Example
142
178
  *
143
179
  * ```ts
144
- * const entries: Array<ConsoleEntry> = [];
145
- * const output = createConsoleArrayOutput(entries);
180
+ * import {
181
+ * createConsole,
182
+ * createConsoleArrayOutput,
183
+ * type ConsoleEntry,
184
+ * } from "@evolu/common";
146
185
  *
147
- * // After logging...
148
- * expect(entries).toMatchInlineSnapshot();
186
+ * const entries: Array<ConsoleEntry> = [];
187
+ * const console = createConsole({
188
+ * output: createConsoleArrayOutput(entries),
189
+ * });
190
+ * console.info("connected");
191
+ * expect(entries).toEqual([
192
+ * { method: "info", path: [], args: ["connected"] },
193
+ * ]);
149
194
  * ```
150
195
  */
151
196
  export const createConsoleArrayOutput = (entries) => ({
@@ -162,10 +207,25 @@ export const createConsoleArrayOutput = (entries) => ({
162
207
  * ### Example
163
208
  *
164
209
  * ```ts
210
+ * import {
211
+ * createConsole,
212
+ * createConsoleArrayOutput,
213
+ * createConsoleStoreOutput,
214
+ * createMultiOutput,
215
+ * type ConsoleEntry,
216
+ * } from "@evolu/common";
217
+ *
218
+ * const entries: Array<ConsoleEntry> = [];
165
219
  * const storeOutput = createConsoleStoreOutput();
166
220
  * const console = createConsole({
167
- * output: createMultiOutput([createNativeConsoleOutput(), storeOutput]),
221
+ * output: createMultiOutput([
222
+ * createConsoleArrayOutput(entries),
223
+ * storeOutput,
224
+ * ]),
168
225
  * });
226
+ * console.error("disconnected");
227
+ *
228
+ * expect(storeOutput.entry.get()).toEqual(entries[0]);
169
229
  * ```
170
230
  */
171
231
  export const createMultiOutput = (outputs) => ({
@@ -177,33 +237,21 @@ export const createMultiOutput = (outputs) => ({
177
237
  /**
178
238
  * Creates a {@link TestConsole} that captures all output for testing.
179
239
  *
180
- * Unlike {@link createConsole}, this doesn't require dependencies and uses a
181
- * simple incrementing counter for timestamps (starting at 0).
240
+ * Unlike {@link createConsole}, this doesn't require dependencies and captures
241
+ * entries in memory.
182
242
  *
183
243
  * ### Example
184
244
  *
185
245
  * ```ts
186
- * test("logging", () => {
187
- * const console = testCreateConsole();
188
- * console.info("Hello");
189
- *
190
- * expect(console.getEntriesSnapshot()).toMatchInlineSnapshot(`
191
- * [
192
- * {
193
- * "method": "info",
194
- * "path": [],
195
- * "args": ["Hello"]
196
- * }
197
- * ]
198
- * `);
199
- * });
246
+ * import { testCreateConsole } from "@evolu/common";
200
247
  *
201
- * test("level filtering", () => {
202
- * const console = testCreateConsole({ level: "warn" });
203
- * console.debug("ignored");
204
- * console.warn("logged");
205
- * expect(console.getEntriesSnapshot()).toHaveLength(1);
206
- * });
248
+ * const console = testCreateConsole({ level: "info" });
249
+ * console.debug("ignored");
250
+ * console.child("relay").info("connected");
251
+ *
252
+ * expect(console.getEntriesSnapshot()).toEqual([
253
+ * { method: "info", path: ["relay"], args: ["connected"] },
254
+ * ]);
207
255
  * ```
208
256
  */
209
257
  export const testCreateConsole = ({ level = "trace", } = {}) => {
@@ -7,9 +7,9 @@
7
7
  *
8
8
  * @module
9
9
  */
10
- import type { RandomLibDep } from "./Random.js";
11
- import type { Result } from "./Result.js";
12
- import { NonNegativeInt, type Typed } from "./Type.js";
10
+ import type { RandomLibDep } from "./Random.ts";
11
+ import type { Result } from "./Result.ts";
12
+ import { NonNegativeInt, type Typed } from "./Type.ts";
13
13
  export interface RandomBytes {
14
14
  /**
15
15
  * Creates cryptographically secure random bytes with type-safe length
@@ -23,20 +23,35 @@ export interface RandomBytes {
23
23
  *
24
24
  * Returns specific branded types for common sizes:
25
25
  *
26
- * - `Random16` for 16-byte values (128 bits)
27
- * - `Random24` for 24-byte values (192 bits)
28
- * - `Random32` for 32-byte values (256 bits)
29
- * - `Random64` for 64-byte values (512 bits)
30
- * - `Random` for any other size
26
+ * - `Entropy16` for 16-byte values (128 bits)
27
+ * - `Entropy24` for 24-byte values (192 bits)
28
+ * - `Entropy32` for 32-byte values (256 bits)
29
+ * - `Entropy64` for 64-byte values (512 bits)
30
+ * - `Entropy` for any other size
31
31
  *
32
32
  * ### Example
33
33
  *
34
34
  * ```ts
35
- * const nonce = randomBytes.create(16); // Type: Random16
36
- * const nonce24 = randomBytes.create(24); // Type: Random24
37
- * const key = randomBytes.create(32); // Type: Random32
38
- * const seed = randomBytes.create(64); // Type: Random64
39
- * const custom = randomBytes.create(48); // Type: Random
35
+ * import {
36
+ * createRandomBytes,
37
+ * type Entropy16,
38
+ * type Entropy24,
39
+ * type Entropy32,
40
+ * type Entropy64,
41
+ * } from "@evolu/common";
42
+ *
43
+ * const randomBytes = createRandomBytes();
44
+ * const nonce16 = randomBytes.create(16);
45
+ * const nonce24 = randomBytes.create(24);
46
+ * const key = randomBytes.create(32);
47
+ * const seed = randomBytes.create(64);
48
+ * const custom = randomBytes.create(48);
49
+ *
50
+ * expectTypeOf(nonce16).toEqualTypeOf<Entropy16>();
51
+ * expectTypeOf(nonce24).toEqualTypeOf<Entropy24>();
52
+ * expectTypeOf(key).toEqualTypeOf<Entropy32>();
53
+ * expectTypeOf(seed).toEqualTypeOf<Entropy64>();
54
+ * expect(custom).toHaveLength(48);
40
55
  * ```
41
56
  */
42
57
  create(bytesLength: 16): Entropy16;
@@ -48,16 +63,16 @@ export interface RandomBytes {
48
63
  export interface RandomBytesDep {
49
64
  readonly randomBytes: RandomBytes;
50
65
  }
51
- declare const Entropy: import("./Type.js").BrandType<import("./Type.js").Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, import("./Type.js").Uint8ArrayError, Uint8Array<ArrayBufferLike>, import("./Type.js").Uint8ArrayError>, "Entropy", import("./Type.js").BrandWithoutRefineError<"Entropy", import("./Type.js").Uint8ArrayError>, never>;
52
- type Entropy = typeof Entropy.Type;
53
- export declare const Entropy16: import("./Type.js").BrandType<import("./Type.js").Type<"Brand", Uint8Array<ArrayBufferLike> & import("./Brand.js").Brand<"Entropy">, Uint8Array<ArrayBufferLike>, import("./Type.js").BrandWithoutRefineError<"Entropy", import("./Type.js").Uint8ArrayError>, Uint8Array<ArrayBufferLike>, never>, "Length16", import("./Type.js").LengthError<16>, import("./Type.js").BrandWithoutRefineError<"Entropy", import("./Type.js").Uint8ArrayError>>;
54
- export type Entropy16 = typeof Entropy16.Type;
55
- export declare const Entropy24: import("./Type.js").BrandType<import("./Type.js").Type<"Brand", Uint8Array<ArrayBufferLike> & import("./Brand.js").Brand<"Entropy">, Uint8Array<ArrayBufferLike>, import("./Type.js").BrandWithoutRefineError<"Entropy", import("./Type.js").Uint8ArrayError>, Uint8Array<ArrayBufferLike>, never>, "Length24", import("./Type.js").LengthError<24>, import("./Type.js").BrandWithoutRefineError<"Entropy", import("./Type.js").Uint8ArrayError>>;
56
- export type Entropy24 = typeof Entropy24.Type;
57
- export declare const Entropy32: import("./Type.js").BrandType<import("./Type.js").Type<"Brand", Uint8Array<ArrayBufferLike> & import("./Brand.js").Brand<"Entropy">, Uint8Array<ArrayBufferLike>, import("./Type.js").BrandWithoutRefineError<"Entropy", import("./Type.js").Uint8ArrayError>, Uint8Array<ArrayBufferLike>, never>, "Length32", import("./Type.js").LengthError<32>, import("./Type.js").BrandWithoutRefineError<"Entropy", import("./Type.js").Uint8ArrayError>>;
58
- export type Entropy32 = typeof Entropy32.Type;
59
- export declare const Entropy64: import("./Type.js").BrandType<import("./Type.js").Type<"Brand", Uint8Array<ArrayBufferLike> & import("./Brand.js").Brand<"Entropy">, Uint8Array<ArrayBufferLike>, import("./Type.js").BrandWithoutRefineError<"Entropy", import("./Type.js").Uint8ArrayError>, Uint8Array<ArrayBufferLike>, never>, "Length64", import("./Type.js").LengthError<64>, import("./Type.js").BrandWithoutRefineError<"Entropy", import("./Type.js").Uint8ArrayError>>;
60
- export type Entropy64 = typeof Entropy64.Type;
66
+ declare const Entropy: import("./Type.ts").BrandType<import("./Type.ts").Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, import("./Type.ts").ObjectTagError<"Uint8Array">, null, import("./Type.ts").ObjectTagError<"Uint8Array">, never, Uint8Array<ArrayBufferLike>, true>, "Entropy", never>;
67
+ type Entropy = typeof Entropy.Output;
68
+ export declare const Entropy16: import("./Type.ts").BrandType<import("./Type.ts").BrandType<import("./Type.ts").Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, import("./Type.ts").ObjectTagError<"Uint8Array">, null, import("./Type.ts").ObjectTagError<"Uint8Array">, never, Uint8Array<ArrayBufferLike>, true>, "Entropy", never>, "Length16", import("./Type.ts").LengthError<16>>;
69
+ export type Entropy16 = typeof Entropy16.Output;
70
+ export declare const Entropy24: import("./Type.ts").BrandType<import("./Type.ts").BrandType<import("./Type.ts").Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, import("./Type.ts").ObjectTagError<"Uint8Array">, null, import("./Type.ts").ObjectTagError<"Uint8Array">, never, Uint8Array<ArrayBufferLike>, true>, "Entropy", never>, "Length24", import("./Type.ts").LengthError<24>>;
71
+ export type Entropy24 = typeof Entropy24.Output;
72
+ export declare const Entropy32: import("./Type.ts").BrandType<import("./Type.ts").BrandType<import("./Type.ts").Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, import("./Type.ts").ObjectTagError<"Uint8Array">, null, import("./Type.ts").ObjectTagError<"Uint8Array">, never, Uint8Array<ArrayBufferLike>, true>, "Entropy", never>, "Length32", import("./Type.ts").LengthError<32>>;
73
+ export type Entropy32 = typeof Entropy32.Output;
74
+ export declare const Entropy64: import("./Type.ts").BrandType<import("./Type.ts").BrandType<import("./Type.ts").Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, import("./Type.ts").ObjectTagError<"Uint8Array">, null, import("./Type.ts").ObjectTagError<"Uint8Array">, never, Uint8Array<ArrayBufferLike>, true>, "Entropy", never>, "Length64", import("./Type.ts").LengthError<64>>;
75
+ export type Entropy64 = typeof Entropy64.Output;
61
76
  export declare const createRandomBytes: () => RandomBytes;
62
77
  /** Creates seeded random bytes for deterministic tests. */
63
78
  export declare const testCreateRandomBytes: (deps: RandomLibDep) => RandomBytes;
@@ -74,8 +89,8 @@ export declare const createSlip21: (seed: Entropy16 | Entropy32 | Entropy64, pat
74
89
  */
75
90
  export declare const deriveSlip21Node: (label: string, parentNode: Entropy64) => Entropy64;
76
91
  /** The encryption key for symmetric encryption. */
77
- export declare const EncryptionKey: import("./Type.js").BrandType<import("./Type.js").BrandType<import("./Type.js").Type<"Brand", Uint8Array<ArrayBufferLike> & import("./Brand.js").Brand<"Entropy">, Uint8Array<ArrayBufferLike>, import("./Type.js").BrandWithoutRefineError<"Entropy", import("./Type.js").Uint8ArrayError>, Uint8Array<ArrayBufferLike>, never>, "Length32", import("./Type.js").LengthError<32>, import("./Type.js").BrandWithoutRefineError<"Entropy", import("./Type.js").Uint8ArrayError>>, "EncryptionKey", import("./Type.js").BrandWithoutRefineError<"EncryptionKey", import("./Type.js").BrandWithoutRefineError<"Entropy", import("./Type.js").Uint8ArrayError> | import("./Type.js").LengthError<32>>, never>;
78
- export type EncryptionKey = typeof EncryptionKey.Type;
92
+ export declare const EncryptionKey: import("./Type.ts").BrandType<import("./Type.ts").BrandType<import("./Type.ts").BrandType<import("./Type.ts").Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, import("./Type.ts").ObjectTagError<"Uint8Array">, null, import("./Type.ts").ObjectTagError<"Uint8Array">, never, Uint8Array<ArrayBufferLike>, true>, "Entropy", never>, "Length32", import("./Type.ts").LengthError<32>>, "EncryptionKey", never>;
93
+ export type EncryptionKey = typeof EncryptionKey.Output;
79
94
  /** The nonce length for XChaCha20-Poly1305 encryption. */
80
95
  export declare const xChaCha20Poly1305NonceLength = 24;
81
96
  /**
@@ -83,8 +98,8 @@ export declare const xChaCha20Poly1305NonceLength = 24;
83
98
  *
84
99
  * @see {@link encryptWithXChaCha20Poly1305}
85
100
  */
86
- export declare const XChaCha20Poly1305Ciphertext: import("./Type.js").BrandType<import("./Type.js").Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, import("./Type.js").Uint8ArrayError, Uint8Array<ArrayBufferLike>, import("./Type.js").Uint8ArrayError>, "XChaCha20Poly1305Ciphertext", import("./Type.js").BrandWithoutRefineError<"XChaCha20Poly1305Ciphertext", import("./Type.js").Uint8ArrayError>, never>;
87
- export type XChaCha20Poly1305Ciphertext = typeof XChaCha20Poly1305Ciphertext.Type;
101
+ export declare const XChaCha20Poly1305Ciphertext: import("./Type.ts").BrandType<import("./Type.ts").Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, import("./Type.ts").ObjectTagError<"Uint8Array">, null, import("./Type.ts").ObjectTagError<"Uint8Array">, never, Uint8Array<ArrayBufferLike>, true>, "XChaCha20Poly1305Ciphertext", never>;
102
+ export type XChaCha20Poly1305Ciphertext = typeof XChaCha20Poly1305Ciphertext.Output;
88
103
  /**
89
104
  * Encrypts plaintext with XChaCha20-Poly1305.
90
105
  *
@@ -94,11 +109,22 @@ export type XChaCha20Poly1305Ciphertext = typeof XChaCha20Poly1305Ciphertext.Typ
94
109
  * ### Example
95
110
  *
96
111
  * ```ts
97
- * const deps = { randomBytes: createRandomBytes() };
98
- * const [ciphertext, nonce] = encryptWithXChaCha20Poly1305(deps)(
99
- * utf8ToBytes("secret message"),
100
- * encryptionKey,
101
- * );
112
+ * import {
113
+ * createRandomBytes,
114
+ * EncryptionKey,
115
+ * encryptWithXChaCha20Poly1305,
116
+ * type XChaCha20Poly1305Ciphertext,
117
+ * utf8ToBytes,
118
+ * } from "@evolu/common";
119
+ *
120
+ * const plaintext = utf8ToBytes("secret message");
121
+ * const encryptionKey = EncryptionKey.orThrow(new Uint8Array(32).fill(7));
122
+ * const [ciphertext, nonce] = encryptWithXChaCha20Poly1305({
123
+ * randomBytes: createRandomBytes(),
124
+ * })(plaintext, encryptionKey);
125
+ *
126
+ * expectTypeOf(ciphertext).toEqualTypeOf<XChaCha20Poly1305Ciphertext>();
127
+ * expect(nonce).toHaveLength(24);
102
128
  * ```
103
129
  *
104
130
  * @see https://github.com/paulmillr/noble-ciphers
@@ -117,16 +143,33 @@ export interface DecryptWithXChaCha20Poly1305Error extends Typed<"DecryptWithXCh
117
143
  * ### Example
118
144
  *
119
145
  * ```ts
120
- * const result = decryptWithXChaCha20Poly1305(
121
- * ciphertext,
122
- * nonce,
123
- * encryptionKey,
124
- * );
125
- * if (!result.ok) {
126
- * // Handle decryption error
127
- * return result;
128
- * }
129
- * const plaintext = result.value;
146
+ * import {
147
+ * bytesToUtf8,
148
+ * createRandomBytes,
149
+ * decryptWithXChaCha20Poly1305,
150
+ * EncryptionKey,
151
+ * encryptWithXChaCha20Poly1305,
152
+ * ok,
153
+ * utf8ToBytes,
154
+ * } from "@evolu/common";
155
+ *
156
+ * const plaintext = utf8ToBytes("secret message");
157
+ * const encryptionKey = EncryptionKey.orThrow(new Uint8Array(32).fill(7));
158
+ * const [ciphertext, nonce] = encryptWithXChaCha20Poly1305({
159
+ * randomBytes: createRandomBytes(),
160
+ * })(plaintext, encryptionKey);
161
+ *
162
+ * const decryptMessage = () => {
163
+ * const result = decryptWithXChaCha20Poly1305(
164
+ * ciphertext,
165
+ * nonce,
166
+ * encryptionKey,
167
+ * );
168
+ * if (!result.ok) return result;
169
+ * return ok(bytesToUtf8(result.value));
170
+ * };
171
+ *
172
+ * expectOk(decryptMessage(), "secret message");
130
173
  * ```
131
174
  */
132
175
  export declare const decryptWithXChaCha20Poly1305: (ciphertext: XChaCha20Poly1305Ciphertext, nonce: Entropy24, encryptionKey: EncryptionKey) => Result<Uint8Array, DecryptWithXChaCha20Poly1305Error>;
@@ -1 +1 @@
1
- {"version":3,"file":"Crypto.d.ts","sourceRoot":"","sources":["../../src/Crypto.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAOH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,EAGL,cAAc,EAEd,KAAK,KAAK,EAEX,MAAM,WAAW,CAAC;AAEnB,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ACjC,QAAQ,CAAC,eAAe,EAAE,eAAe,CAAC;CAC3C"}
@@ -11,7 +11,6 @@ import { xchacha20poly1305 } from "@noble/ciphers/chacha.js";
11
11
  import { hmac } from "@noble/hashes/hmac.js";
12
12
  import { sha512 } from "@noble/hashes/sha2.js";
13
13
  import { randomBytes, utf8ToBytes } from "@noble/hashes/utils.js";
14
- import { arrayFrom } from "./Array.js";
15
14
  import { trySync } from "./Result.js";
16
15
  import { brand, length, NonNegativeInt, Uint8Array, zeroNonNegativeInt, } from "./Type.js";
17
16
  const Entropy = /*#__PURE__*/ brand("Entropy", Uint8Array);
@@ -24,10 +23,7 @@ export const createRandomBytes = () => ({
24
23
  });
25
24
  /** Creates seeded random bytes for deterministic tests. */
26
25
  export const testCreateRandomBytes = (deps) => ({
27
- create: (bytesLength) => {
28
- const array = arrayFrom(bytesLength, () => deps.randomLib.int(0, 255));
29
- return new globalThis.Uint8Array(array);
30
- },
26
+ create: (bytesLength) => globalThis.Uint8Array.from({ length: bytesLength }, () => deps.randomLib.int(0, 255)),
31
27
  });
32
28
  /**
33
29
  * SLIP21.
@@ -73,11 +69,22 @@ export const XChaCha20Poly1305Ciphertext = /*#__PURE__*/ brand("XChaCha20Poly130
73
69
  * ### Example
74
70
  *
75
71
  * ```ts
76
- * const deps = { randomBytes: createRandomBytes() };
77
- * const [ciphertext, nonce] = encryptWithXChaCha20Poly1305(deps)(
78
- * utf8ToBytes("secret message"),
79
- * encryptionKey,
80
- * );
72
+ * import {
73
+ * createRandomBytes,
74
+ * EncryptionKey,
75
+ * encryptWithXChaCha20Poly1305,
76
+ * type XChaCha20Poly1305Ciphertext,
77
+ * utf8ToBytes,
78
+ * } from "@evolu/common";
79
+ *
80
+ * const plaintext = utf8ToBytes("secret message");
81
+ * const encryptionKey = EncryptionKey.orThrow(new Uint8Array(32).fill(7));
82
+ * const [ciphertext, nonce] = encryptWithXChaCha20Poly1305({
83
+ * randomBytes: createRandomBytes(),
84
+ * })(plaintext, encryptionKey);
85
+ *
86
+ * expectTypeOf(ciphertext).toEqualTypeOf<XChaCha20Poly1305Ciphertext>();
87
+ * expect(nonce).toHaveLength(24);
81
88
  * ```
82
89
  *
83
90
  * @see https://github.com/paulmillr/noble-ciphers
@@ -97,16 +104,33 @@ export const encryptWithXChaCha20Poly1305 = (deps) => (plaintext, encryptionKey)
97
104
  * ### Example
98
105
  *
99
106
  * ```ts
100
- * const result = decryptWithXChaCha20Poly1305(
101
- * ciphertext,
102
- * nonce,
103
- * encryptionKey,
104
- * );
105
- * if (!result.ok) {
106
- * // Handle decryption error
107
- * return result;
108
- * }
109
- * const plaintext = result.value;
107
+ * import {
108
+ * bytesToUtf8,
109
+ * createRandomBytes,
110
+ * decryptWithXChaCha20Poly1305,
111
+ * EncryptionKey,
112
+ * encryptWithXChaCha20Poly1305,
113
+ * ok,
114
+ * utf8ToBytes,
115
+ * } from "@evolu/common";
116
+ *
117
+ * const plaintext = utf8ToBytes("secret message");
118
+ * const encryptionKey = EncryptionKey.orThrow(new Uint8Array(32).fill(7));
119
+ * const [ciphertext, nonce] = encryptWithXChaCha20Poly1305({
120
+ * randomBytes: createRandomBytes(),
121
+ * })(plaintext, encryptionKey);
122
+ *
123
+ * const decryptMessage = () => {
124
+ * const result = decryptWithXChaCha20Poly1305(
125
+ * ciphertext,
126
+ * nonce,
127
+ * encryptionKey,
128
+ * );
129
+ * if (!result.ok) return result;
130
+ * return ok(bytesToUtf8(result.value));
131
+ * };
132
+ *
133
+ * expectOk(decryptMessage(), "secret message");
110
134
  * ```
111
135
  */
112
136
  export const decryptWithXChaCha20Poly1305 = (ciphertext, nonce, encryptionKey) => trySync(() => xchacha20poly1305(encryptionKey, nonce).decrypt(ciphertext), (error) => ({