@evolu/common 6.0.1-preview.3 → 6.0.1-preview.30

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 (173) hide show
  1. package/dist/src/Array.d.ts +58 -5
  2. package/dist/src/Array.d.ts.map +1 -1
  3. package/dist/src/Array.js +53 -5
  4. package/dist/src/Assert.d.ts +6 -16
  5. package/dist/src/Assert.d.ts.map +1 -1
  6. package/dist/src/Assert.js +6 -18
  7. package/dist/src/Brand.d.ts +75 -0
  8. package/dist/src/Brand.d.ts.map +1 -0
  9. package/dist/src/Brand.js +1 -0
  10. package/dist/src/Buffer.d.ts +1 -1
  11. package/dist/src/Buffer.d.ts.map +1 -1
  12. package/dist/src/Buffer.js +8 -7
  13. package/dist/src/Cache.d.ts +44 -0
  14. package/dist/src/Cache.d.ts.map +1 -0
  15. package/dist/src/Cache.js +52 -0
  16. package/dist/src/Callbacks.d.ts +45 -12
  17. package/dist/src/Callbacks.d.ts.map +1 -1
  18. package/dist/src/Callbacks.js +14 -7
  19. package/dist/src/Console.d.ts +31 -6
  20. package/dist/src/Console.d.ts.map +1 -1
  21. package/dist/src/Console.js +72 -9
  22. package/dist/src/Crypto.d.ts +61 -34
  23. package/dist/src/Crypto.d.ts.map +1 -1
  24. package/dist/src/Crypto.js +32 -45
  25. package/dist/src/Evolu/Db.d.ts +158 -65
  26. package/dist/src/Evolu/Db.d.ts.map +1 -1
  27. package/dist/src/Evolu/Db.js +286 -694
  28. package/dist/src/Evolu/Diff.d.ts +3 -3
  29. package/dist/src/Evolu/Diff.d.ts.map +1 -1
  30. package/dist/src/Evolu/Diff.js +7 -5
  31. package/dist/src/Evolu/Evolu.d.ts +208 -133
  32. package/dist/src/Evolu/Evolu.d.ts.map +1 -1
  33. package/dist/src/Evolu/Evolu.js +188 -183
  34. package/dist/src/Evolu/Internal.d.ts +0 -2
  35. package/dist/src/Evolu/Internal.d.ts.map +1 -1
  36. package/dist/src/Evolu/Internal.js +0 -2
  37. package/dist/src/Evolu/LocalAuth.d.ts +150 -0
  38. package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
  39. package/dist/src/Evolu/LocalAuth.js +174 -0
  40. package/dist/src/Evolu/Owner.d.ts +264 -120
  41. package/dist/src/Evolu/Owner.d.ts.map +1 -1
  42. package/dist/src/Evolu/Owner.js +130 -104
  43. package/dist/src/Evolu/Platform.d.ts +9 -7
  44. package/dist/src/Evolu/Platform.d.ts.map +1 -1
  45. package/dist/src/Evolu/Protocol.d.ts +277 -232
  46. package/dist/src/Evolu/Protocol.d.ts.map +1 -1
  47. package/dist/src/Evolu/Protocol.js +603 -378
  48. package/dist/src/Evolu/Public.d.ts +6 -8
  49. package/dist/src/Evolu/Public.d.ts.map +1 -1
  50. package/dist/src/Evolu/Public.js +2 -3
  51. package/dist/src/Evolu/PublicKysely.js +3 -3
  52. package/dist/src/Evolu/Query.d.ts +2 -1
  53. package/dist/src/Evolu/Query.d.ts.map +1 -1
  54. package/dist/src/Evolu/Relay.d.ts +92 -7
  55. package/dist/src/Evolu/Relay.d.ts.map +1 -1
  56. package/dist/src/Evolu/Relay.js +243 -76
  57. package/dist/src/Evolu/Schema.d.ts +129 -73
  58. package/dist/src/Evolu/Schema.d.ts.map +1 -1
  59. package/dist/src/Evolu/Schema.js +169 -89
  60. package/dist/src/Evolu/Storage.d.ts +212 -26
  61. package/dist/src/Evolu/Storage.d.ts.map +1 -1
  62. package/dist/src/Evolu/Storage.js +137 -79
  63. package/dist/src/Evolu/Sync.d.ts +68 -13
  64. package/dist/src/Evolu/Sync.d.ts.map +1 -1
  65. package/dist/src/Evolu/Sync.js +422 -20
  66. package/dist/src/Evolu/Timestamp.d.ts +85 -27
  67. package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
  68. package/dist/src/Evolu/Timestamp.js +77 -18
  69. package/dist/src/Identicon.d.ts +35 -0
  70. package/dist/src/Identicon.d.ts.map +1 -0
  71. package/dist/src/Identicon.js +143 -0
  72. package/dist/src/Instances.d.ts +34 -0
  73. package/dist/src/Instances.d.ts.map +1 -0
  74. package/dist/src/Instances.js +44 -0
  75. package/dist/src/ManyToManyMap.d.ts +71 -10
  76. package/dist/src/ManyToManyMap.d.ts.map +1 -1
  77. package/dist/src/ManyToManyMap.js +41 -6
  78. package/dist/src/Number.d.ts +4 -3
  79. package/dist/src/Number.d.ts.map +1 -1
  80. package/dist/src/Number.js +5 -4
  81. package/dist/src/Platform.d.ts +20 -0
  82. package/dist/src/Platform.d.ts.map +1 -0
  83. package/dist/src/Platform.js +22 -0
  84. package/dist/src/Random.d.ts +3 -2
  85. package/dist/src/Random.d.ts.map +1 -1
  86. package/dist/src/Resources.d.ts +118 -0
  87. package/dist/src/Resources.d.ts.map +1 -0
  88. package/dist/src/Resources.js +197 -0
  89. package/dist/src/Result.d.ts +184 -52
  90. package/dist/src/Result.d.ts.map +1 -1
  91. package/dist/src/Result.js +30 -241
  92. package/dist/src/Skiplist.js +2 -1
  93. package/dist/src/Sqlite.d.ts +63 -5
  94. package/dist/src/Sqlite.d.ts.map +1 -1
  95. package/dist/src/Sqlite.js +110 -9
  96. package/dist/src/Task.d.ts +586 -0
  97. package/dist/src/Task.d.ts.map +1 -0
  98. package/dist/src/Task.js +469 -0
  99. package/dist/src/Time.d.ts +66 -1
  100. package/dist/src/Time.d.ts.map +1 -1
  101. package/dist/src/Time.js +99 -5
  102. package/dist/src/Type.d.ts +621 -340
  103. package/dist/src/Type.d.ts.map +1 -1
  104. package/dist/src/Type.js +665 -464
  105. package/dist/src/Types.d.ts +1 -75
  106. package/dist/src/Types.d.ts.map +1 -1
  107. package/dist/src/WebSocket.d.ts +5 -2
  108. package/dist/src/WebSocket.d.ts.map +1 -1
  109. package/dist/src/WebSocket.js +12 -18
  110. package/dist/src/Worker.d.ts +39 -11
  111. package/dist/src/Worker.d.ts.map +1 -1
  112. package/dist/src/Worker.js +22 -4
  113. package/dist/src/index.d.ts +7 -2
  114. package/dist/src/index.d.ts.map +1 -1
  115. package/dist/src/index.js +7 -2
  116. package/package.json +14 -13
  117. package/src/Array.ts +76 -11
  118. package/src/Assert.ts +6 -24
  119. package/src/Brand.ts +75 -0
  120. package/src/Buffer.ts +7 -7
  121. package/src/Cache.ts +85 -0
  122. package/src/Callbacks.ts +62 -22
  123. package/src/Console.ts +91 -11
  124. package/src/Crypto.ts +97 -82
  125. package/src/Evolu/Db.ts +514 -1020
  126. package/src/Evolu/Diff.ts +7 -5
  127. package/src/Evolu/Evolu.ts +464 -355
  128. package/src/Evolu/Internal.ts +0 -2
  129. package/src/Evolu/LocalAuth.ts +463 -0
  130. package/src/Evolu/Owner.ts +369 -228
  131. package/src/Evolu/Platform.ts +9 -9
  132. package/src/Evolu/Protocol.ts +859 -676
  133. package/src/Evolu/Public.ts +7 -14
  134. package/src/Evolu/PublicKysely.ts +3 -3
  135. package/src/Evolu/Query.ts +2 -1
  136. package/src/Evolu/Relay.ts +420 -92
  137. package/src/Evolu/Schema.ts +391 -191
  138. package/src/Evolu/Storage.ts +451 -118
  139. package/src/Evolu/Sync.ts +720 -37
  140. package/src/Evolu/Timestamp.ts +88 -35
  141. package/src/Identicon.ts +197 -0
  142. package/src/Instances.ts +90 -0
  143. package/src/ManyToManyMap.ts +124 -24
  144. package/src/Number.ts +6 -10
  145. package/src/Platform.ts +26 -0
  146. package/src/Random.ts +3 -2
  147. package/src/Resources.ts +367 -0
  148. package/src/Result.ts +191 -54
  149. package/src/Skiplist.ts +1 -1
  150. package/src/Sqlite.ts +122 -17
  151. package/src/Task.ts +901 -0
  152. package/src/Time.ts +180 -5
  153. package/src/Type.ts +1083 -727
  154. package/src/Types.ts +1 -77
  155. package/src/WebSocket.ts +27 -25
  156. package/src/Worker.ts +72 -23
  157. package/src/index.ts +7 -2
  158. package/dist/src/Evolu/Config.d.ts +0 -69
  159. package/dist/src/Evolu/Config.d.ts.map +0 -1
  160. package/dist/src/Evolu/Config.js +0 -9
  161. package/dist/src/Evolu/Kysely.d.ts +0 -6
  162. package/dist/src/Evolu/Kysely.d.ts.map +0 -1
  163. package/dist/src/Evolu/Kysely.js +0 -21
  164. package/dist/src/NanoId.d.ts +0 -27
  165. package/dist/src/NanoId.d.ts.map +0 -1
  166. package/dist/src/NanoId.js +0 -6
  167. package/dist/src/Promise.d.ts +0 -180
  168. package/dist/src/Promise.d.ts.map +0 -1
  169. package/dist/src/Promise.js +0 -176
  170. package/src/Evolu/Config.ts +0 -83
  171. package/src/Evolu/Kysely.ts +0 -38
  172. package/src/NanoId.ts +0 -39
  173. package/src/Promise.ts +0 -295
@@ -1,3 +1,32 @@
1
+ /**
2
+ * 🔒 Immutable, type-safe array operations
3
+ *
4
+ * Helpers that preserve immutability through the type system. Native array
5
+ * methods return mutable arrays even when called on readonly arrays. These
6
+ * helpers ensure transformations return readonly types.
7
+ *
8
+ * {@link NonEmptyArray} and {@link NonEmptyReadonlyArray} types represent arrays
9
+ * with at least one element, eliminating runtime length checks and making
10
+ * function requirements explicit.
11
+ *
12
+ * ### Example
13
+ *
14
+ * ```ts
15
+ * // ❌ Native methods return mutable arrays
16
+ * const readonly: ReadonlyArray<number> = [1, 2, 3];
17
+ * const mapped = readonly.map((x) => x * 2); // Array<number> (mutable!)
18
+ *
19
+ * // ✅ Helpers preserve immutability
20
+ * const filtered = filterArray(readonly, (x) => x > 1); // ReadonlyArray<number>
21
+ *
22
+ * // ✅ NonEmptyArray enforces non-emptiness
23
+ * const first = (items: NonEmptyReadonlyArray<string>) => items[0];
24
+ * first([]); // ❌ Compiler error
25
+ * first(["a"]); // ✅ Works
26
+ * ```
27
+ *
28
+ * @module
29
+ */
1
30
  /** An array with at least one element. */
2
31
  export type NonEmptyArray<T> = [T, ...Array<T>];
3
32
  /** Checks if an array is non-empty. */
@@ -6,12 +35,36 @@ export declare const isNonEmptyArray: <T>(array: Array<T>) => array is NonEmptyA
6
35
  export type NonEmptyReadonlyArray<T> = readonly [T, ...ReadonlyArray<T>];
7
36
  /** Checks if an array is non-empty. */
8
37
  export declare const isNonEmptyReadonlyArray: <T>(array: ReadonlyArray<T>) => array is NonEmptyReadonlyArray<T>;
9
- /** Appends an item to an array returning a non-empty array. */
38
+ /**
39
+ * Appends an item to an array, returning a new non-empty readonly array.
40
+ *
41
+ * Accepts both mutable and readonly arrays. Does not mutate the original array.
42
+ */
10
43
  export declare const appendToArray: <T>(item: T, array: ReadonlyArray<T>) => NonEmptyReadonlyArray<T>;
11
- /** Prepends an item to an array returning a non-empty array. */
44
+ /**
45
+ * Prepends an item to an array, returning a new non-empty readonly array.
46
+ *
47
+ * Accepts both mutable and readonly arrays. Does not mutate the original array.
48
+ */
12
49
  export declare const prependToArray: <T>(item: T, array: ReadonlyArray<T>) => NonEmptyReadonlyArray<T>;
13
- /** Maps a non-empty array using a mapper function. */
14
- export declare const mapNonEmptyArray: <T, U>(array: NonEmptyReadonlyArray<T>, mapper: (item: T, index: number) => U) => NonEmptyReadonlyArray<U>;
15
- /** Shifts an item from a non-empty array, guaranteed to return T. */
50
+ /**
51
+ * Maps an array using a mapper function, preserving non-emptiness when
52
+ * applicable.
53
+ *
54
+ * Accepts both mutable and readonly arrays. Does not mutate the original array.
55
+ */
56
+ export declare function mapArray<T, U>(array: NonEmptyReadonlyArray<T> | NonEmptyArray<T>, mapper: (item: T, index: number) => U): NonEmptyReadonlyArray<U>;
57
+ export declare function mapArray<T, U>(array: ReadonlyArray<T> | Array<T>, mapper: (item: T, index: number) => U): ReadonlyArray<U>;
58
+ /**
59
+ * Filters an array using a predicate function, returning a new readonly array.
60
+ *
61
+ * Accepts both mutable and readonly arrays. Does not mutate the original array.
62
+ */
63
+ export declare const filterArray: <T>(array: ReadonlyArray<T>, predicate: (item: T, index: number) => boolean) => ReadonlyArray<T>;
64
+ /**
65
+ * Shifts an item from a non-empty mutable array, guaranteed to return T.
66
+ *
67
+ * **Mutates** the original array. Use only with mutable arrays.
68
+ */
16
69
  export declare const shiftArray: <T>(array: NonEmptyArray<T>) => T;
17
70
  //# sourceMappingURL=Array.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Array.d.ts","sourceRoot":"","sources":["../../src/Array.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AAEhD,uCAAuC;AACvC,eAAO,MAAM,eAAe,GAAI,CAAC,EAC/B,OAAO,KAAK,CAAC,CAAC,CAAC,KACd,KAAK,IAAI,aAAa,CAAC,CAAC,CAAqB,CAAC;AAEjD,kDAAkD;AAClD,MAAM,MAAM,qBAAqB,CAAC,CAAC,IAAI,SAAS,CAAC,CAAC,EAAE,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;AAEzE,uCAAuC;AACvC,eAAO,MAAM,uBAAuB,GAAI,CAAC,EACvC,OAAO,aAAa,CAAC,CAAC,CAAC,KACtB,KAAK,IAAI,qBAAqB,CAAC,CAAC,CAAqB,CAAC;AAEzD,+DAA+D;AAC/D,eAAO,MAAM,aAAa,GAAI,CAAC,EAC7B,MAAM,CAAC,EACP,OAAO,aAAa,CAAC,CAAC,CAAC,KACtB,qBAAqB,CAAC,CAAC,CAC+B,CAAC;AAE1D,gEAAgE;AAChE,eAAO,MAAM,cAAc,GAAI,CAAC,EAC9B,MAAM,CAAC,EACP,OAAO,aAAa,CAAC,CAAC,CAAC,KACtB,qBAAqB,CAAC,CAAC,CAC+B,CAAC;AAE1D,sDAAsD;AACtD,eAAO,MAAM,gBAAgB,GAAI,CAAC,EAAE,CAAC,EACnC,OAAO,qBAAqB,CAAC,CAAC,CAAC,EAC/B,QAAQ,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,CAAC,KACpC,qBAAqB,CAAC,CAAC,CACgC,CAAC;AAE3D,qEAAqE;AACrE,eAAO,MAAM,UAAU,GAAI,CAAC,EAAE,OAAO,aAAa,CAAC,CAAC,CAAC,KAAG,CAAuB,CAAC"}
1
+ {"version":3,"file":"Array.d.ts","sourceRoot":"","sources":["../../src/Array.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,0CAA0C;AAC1C,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AAEhD,uCAAuC;AACvC,eAAO,MAAM,eAAe,GAAI,CAAC,EAC/B,OAAO,KAAK,CAAC,CAAC,CAAC,KACd,KAAK,IAAI,aAAa,CAAC,CAAC,CAAqB,CAAC;AAEjD,kDAAkD;AAClD,MAAM,MAAM,qBAAqB,CAAC,CAAC,IAAI,SAAS,CAAC,CAAC,EAAE,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;AAEzE,uCAAuC;AACvC,eAAO,MAAM,uBAAuB,GAAI,CAAC,EACvC,OAAO,aAAa,CAAC,CAAC,CAAC,KACtB,KAAK,IAAI,qBAAqB,CAAC,CAAC,CAAqB,CAAC;AAEzD;;;;GAIG;AACH,eAAO,MAAM,aAAa,GAAI,CAAC,EAC7B,MAAM,CAAC,EACP,OAAO,aAAa,CAAC,CAAC,CAAC,KACtB,qBAAqB,CAAC,CAAC,CACwC,CAAC;AAEnE;;;;GAIG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,EAC9B,MAAM,CAAC,EACP,OAAO,aAAa,CAAC,CAAC,CAAC,KACtB,qBAAqB,CAAC,CAAC,CAAiD,CAAC;AAE5E;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,EAC3B,KAAK,EAAE,qBAAqB,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,EAClD,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,CAAC,GACpC,qBAAqB,CAAC,CAAC,CAAC,CAAC;AAC5B,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,EAC3B,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,EAClC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,CAAC,GACpC,aAAa,CAAC,CAAC,CAAC,CAAC;AAQpB;;;;GAIG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,EAC3B,OAAO,aAAa,CAAC,CAAC,CAAC,EACvB,WAAW,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,KAC7C,aAAa,CAAC,CAAC,CAAgD,CAAC;AAEnE;;;;GAIG;AACH,eAAO,MAAM,UAAU,GAAI,CAAC,EAAE,OAAO,aAAa,CAAC,CAAC,CAAC,KAAG,CAAuB,CAAC"}
package/dist/src/Array.js CHANGED
@@ -1,12 +1,60 @@
1
+ /**
2
+ * 🔒 Immutable, type-safe array operations
3
+ *
4
+ * Helpers that preserve immutability through the type system. Native array
5
+ * methods return mutable arrays even when called on readonly arrays. These
6
+ * helpers ensure transformations return readonly types.
7
+ *
8
+ * {@link NonEmptyArray} and {@link NonEmptyReadonlyArray} types represent arrays
9
+ * with at least one element, eliminating runtime length checks and making
10
+ * function requirements explicit.
11
+ *
12
+ * ### Example
13
+ *
14
+ * ```ts
15
+ * // ❌ Native methods return mutable arrays
16
+ * const readonly: ReadonlyArray<number> = [1, 2, 3];
17
+ * const mapped = readonly.map((x) => x * 2); // Array<number> (mutable!)
18
+ *
19
+ * // ✅ Helpers preserve immutability
20
+ * const filtered = filterArray(readonly, (x) => x > 1); // ReadonlyArray<number>
21
+ *
22
+ * // ✅ NonEmptyArray enforces non-emptiness
23
+ * const first = (items: NonEmptyReadonlyArray<string>) => items[0];
24
+ * first([]); // ❌ Compiler error
25
+ * first(["a"]); // ✅ Works
26
+ * ```
27
+ *
28
+ * @module
29
+ */
1
30
  /** Checks if an array is non-empty. */
2
31
  export const isNonEmptyArray = (array) => array.length > 0;
3
32
  /** Checks if an array is non-empty. */
4
33
  export const isNonEmptyReadonlyArray = (array) => array.length > 0;
5
- /** Appends an item to an array returning a non-empty array. */
34
+ /**
35
+ * Appends an item to an array, returning a new non-empty readonly array.
36
+ *
37
+ * Accepts both mutable and readonly arrays. Does not mutate the original array.
38
+ */
6
39
  export const appendToArray = (item, array) => [...array, item];
7
- /** Prepends an item to an array returning a non-empty array. */
40
+ /**
41
+ * Prepends an item to an array, returning a new non-empty readonly array.
42
+ *
43
+ * Accepts both mutable and readonly arrays. Does not mutate the original array.
44
+ */
8
45
  export const prependToArray = (item, array) => [item, ...array];
9
- /** Maps a non-empty array using a mapper function. */
10
- export const mapNonEmptyArray = (array, mapper) => array.map(mapper);
11
- /** Shifts an item from a non-empty array, guaranteed to return T. */
46
+ export function mapArray(array, mapper) {
47
+ return array.map(mapper);
48
+ }
49
+ /**
50
+ * Filters an array using a predicate function, returning a new readonly array.
51
+ *
52
+ * Accepts both mutable and readonly arrays. Does not mutate the original array.
53
+ */
54
+ export const filterArray = (array, predicate) => array.filter(predicate);
55
+ /**
56
+ * Shifts an item from a non-empty mutable array, guaranteed to return T.
57
+ *
58
+ * **Mutates** the original array. Use only with mutable arrays.
59
+ */
12
60
  export const shiftArray = (array) => array.shift();
@@ -16,9 +16,12 @@
16
16
  * assert(true, "true is not true"); // no-op
17
17
  * assert(false, "true is not true"); // throws Error
18
18
  *
19
- * const size = fooSize - buffer.getLength();
20
- * // Ensure required type
21
- * assert(PositiveInt.is(size), "size is not positive int");
19
+ * const length = buffer.getLength();
20
+ * // We know length is logically non-negative, but TypeScript doesn't
21
+ * assert(
22
+ * NonNegativeInt.is(length),
23
+ * "buffer length should be non-negative",
24
+ * );
22
25
  * ```
23
26
  */
24
27
  export declare const assert: (condition: unknown, message: string) => asserts condition;
@@ -52,17 +55,4 @@ export declare const assertNonEmptyArray: <T>(arr: Array<T>, message?: string) =
52
55
  * ```
53
56
  */
54
57
  export declare const assertNonEmptyReadonlyArray: <T>(arr: ReadonlyArray<T>, message?: string) => asserts arr is readonly [T, ...Array<T>];
55
- /**
56
- * Asserts no error reaches a `.catch` block, throwing a developer error if it
57
- * does. Used in Promise chains where errors indicate bugs to be fixed.
58
- *
59
- * ### Example
60
- *
61
- * ```ts
62
- * Promise.reject("test").catch((e) =>
63
- * assertNoErrorInCatch("WebSocket retry", e),
64
- * );
65
- * ```
66
- */
67
- export declare function assertNoErrorInCatch(context: string, error: unknown): never;
68
58
  //# sourceMappingURL=Assert.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Assert.d.ts","sourceRoot":"","sources":["../../src/Assert.ts"],"names":[],"mappings":"AAgBA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,MAAM,EAAE,CACnB,SAAS,EAAE,OAAO,EAClB,OAAO,EAAE,MAAM,KACZ,OAAO,CAAC,SAIZ,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,mBAAmB,EAAE,CAAC,CAAC,EAClC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,EACb,OAAO,CAAC,EAAE,MAAM,KACb,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAKlC,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,2BAA2B,EAAE,CAAC,CAAC,EAC1C,GAAG,EAAE,aAAa,CAAC,CAAC,CAAC,EACrB,OAAO,CAAC,EAAE,MAAM,KACb,OAAO,CAAC,GAAG,IAAI,SAAS,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAK3C,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,KAAK,CAO3E"}
1
+ {"version":3,"file":"Assert.d.ts","sourceRoot":"","sources":["../../src/Assert.ts"],"names":[],"mappings":"AAgBA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,MAAM,EAAE,CACnB,SAAS,EAAE,OAAO,EAClB,OAAO,EAAE,MAAM,KACZ,OAAO,CAAC,SAIZ,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,mBAAmB,EAAE,CAAC,CAAC,EAClC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,EACb,OAAO,CAAC,EAAE,MAAM,KACb,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAKlC,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,2BAA2B,EAAE,CAAC,CAAC,EAC1C,GAAG,EAAE,aAAa,CAAC,CAAC,CAAC,EACrB,OAAO,CAAC,EAAE,MAAM,KACb,OAAO,CAAC,GAAG,IAAI,SAAS,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAK3C,CAAC"}
@@ -16,9 +16,12 @@
16
16
  * assert(true, "true is not true"); // no-op
17
17
  * assert(false, "true is not true"); // throws Error
18
18
  *
19
- * const size = fooSize - buffer.getLength();
20
- * // Ensure required type
21
- * assert(PositiveInt.is(size), "size is not positive int");
19
+ * const length = buffer.getLength();
20
+ * // We know length is logically non-negative, but TypeScript doesn't
21
+ * assert(
22
+ * NonNegativeInt.is(length),
23
+ * "buffer length should be non-negative",
24
+ * );
22
25
  * ```
23
26
  */
24
27
  export const assert = (condition, message) => {
@@ -60,18 +63,3 @@ export const assertNonEmptyArray = (arr, message = "Expected a non-empty array."
60
63
  export const assertNonEmptyReadonlyArray = (arr, message = "Expected a non-empty readonly array.") => {
61
64
  assert(arr.length > 0, message);
62
65
  };
63
- /**
64
- * Asserts no error reaches a `.catch` block, throwing a developer error if it
65
- * does. Used in Promise chains where errors indicate bugs to be fixed.
66
- *
67
- * ### Example
68
- *
69
- * ```ts
70
- * Promise.reject("test").catch((e) =>
71
- * assertNoErrorInCatch("WebSocket retry", e),
72
- * );
73
- * ```
74
- */
75
- export function assertNoErrorInCatch(context, error) {
76
- throw Object.assign(new Error(`Error in ${context}: an unexpected error reached a catch block and requires a fix`), { cause: error });
77
- }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * A utility interface for creating branded types.
3
+ *
4
+ * Branded types enhance type safety by differentiating otherwise identical base
5
+ * types, such as `number` or `string`, to enforce stricter type checks.
6
+ *
7
+ * Supports multiple brands, allowing types to act like flags.
8
+ *
9
+ * ### Example 1: Single Brand
10
+ *
11
+ * ```ts
12
+ * // A branded type definition
13
+ * type UserId = number & Brand<"UserId">;
14
+ *
15
+ * // A function that creates `UserId` values.
16
+ * // Casting with `as UserId` is unsafe, so `createUserId` must be unit-tested.
17
+ * const createUserId = (): UserId => {
18
+ * return 123 as UserId; // Unsafe casting
19
+ * };
20
+ *
21
+ * const userId = createUserId();
22
+ *
23
+ * // A function that accepts only `UserId`.
24
+ * const getUser = (id: UserId) => {
25
+ * // Implementation
26
+ * };
27
+ *
28
+ * getUser(userId); // ✅ Valid
29
+ * getUser(123); // ❌ TypeScript error
30
+ * getUser("123"); // ❌ TypeScript error
31
+ * ```
32
+ *
33
+ * ### Example 2: Multiple Brands
34
+ *
35
+ * ```ts
36
+ * // Define branded types
37
+ * type Min1 = string & Brand<"Min1">;
38
+ * type Max100 = string & Brand<"Max100">;
39
+ * type Min1Max100 = string & Brand<"Min1" | "Max100">;
40
+ *
41
+ * // Functions requiring specific brands
42
+ * const requiresMin1 = (value: Min1): void => {};
43
+ * const requiresMax100 = (value: Max100): void => {};
44
+ *
45
+ * // Values with single brands
46
+ * const min1Value: Min1 = "hello" as Min1;
47
+ * const max100Value: Max100 = "world" as Max100;
48
+ *
49
+ * // Value with multiple brands
50
+ * const min1Max100Value: Min1Max100 = "typescript" as Min1Max100;
51
+ *
52
+ * // Valid cases
53
+ * requiresMin1(min1Value); // ✅ Valid
54
+ * requiresMax100(max100Value); // ✅ Valid
55
+ * requiresMin1(min1Max100Value); // ✅ Valid: Min1Max100 satisfies Min1
56
+ * requiresMax100(min1Max100Value); // ✅ Valid: Min1Max100 satisfies Max100
57
+ * ```
58
+ */
59
+ export interface Brand<B extends string> {
60
+ readonly [__brand]: Readonly<Record<B, true>>;
61
+ }
62
+ declare const __brand: unique symbol;
63
+ /**
64
+ * Determines whether a type `T` is a branded type.
65
+ *
66
+ * Works with any base type intersected with a `Brand`.
67
+ *
68
+ * ### Examples
69
+ *
70
+ * - `IsBranded<string>` -> false
71
+ * - `IsBranded<string & Brand<"X">>` -> true
72
+ */
73
+ export type IsBranded<T> = T extends Brand<string> ? true : false;
74
+ export {};
75
+ //# sourceMappingURL=Brand.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Brand.d.ts","sourceRoot":"","sources":["../../src/Brand.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyDG;AACH,MAAM,WAAW,KAAK,CAAC,CAAC,SAAS,MAAM;IACrC,QAAQ,CAAC,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;CAC/C;AAED,OAAO,CAAC,MAAM,OAAO,EAAE,OAAO,MAAM,CAAC;AAErC;;;;;;;;;GASG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -1,5 +1,5 @@
1
1
  import { NonNegativeInt } from "./Type.js";
2
- export { bytesToHex, bytesToUtf8, concatBytes, hexToBytes, utf8ToBytes, } from "@noble/ciphers/utils";
2
+ export { bytesToHex, bytesToUtf8, concatBytes, hexToBytes, utf8ToBytes, } from "@noble/ciphers/utils.js";
3
3
  /**
4
4
  * Custom error for {@link Buffer}-related failures like premature end of data.
5
5
  * Provides better stack traces for debugging binary protocol issues.
@@ -1 +1 @@
1
- {"version":3,"file":"Buffer.d.ts","sourceRoot":"","sources":["../../src/Buffer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EACL,UAAU,EACV,WAAW,EACX,WAAW,EACX,UAAU,EACV,WAAW,GACZ,MAAM,sBAAsB,CAAC;AAE9B;;;GAGG;AACH,qBAAa,WAAY,SAAQ,KAAK;gBACxB,OAAO,EAAE,MAAM;CAM5B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,WAAW,MAAM;IACrB,sDAAsD;IACtD,WAAW,EAAE,MAAM,cAAc,CAAC;IAElC,gEAAgE;IAChE,SAAS,EAAE,MAAM,cAAc,CAAC;IAEhC;;;OAGG;IACH,MAAM,EAAE,CAAC,GAAG,EAAE,UAAU,GAAG,SAAS,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;IAEtD;;;OAGG;IACH,KAAK,EAAE,MAAM,cAAc,CAAC;IAE5B;;;OAGG;IACH,MAAM,EAAE,CAAC,CAAC,EAAE,cAAc,KAAK,UAAU,CAAC;IAE1C;;;OAGG;IACH,QAAQ,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,IAAI,CAAC;IAE3C;;;;;;OAMG;IACH,KAAK,EAAE,MAAM,IAAI,CAAC;IAElB;;;;OAIG;IACH,MAAM,EAAE,MAAM,UAAU,CAAC;CAC1B;AAED,eAAO,MAAM,YAAY,GACvB,YAAY,UAAU,GAAG,SAAS,CAAC,MAAM,CAAC,KACzC,MA4DF,CAAC"}
1
+ {"version":3,"file":"Buffer.d.ts","sourceRoot":"","sources":["../../src/Buffer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EACL,UAAU,EACV,WAAW,EACX,WAAW,EACX,UAAU,EACV,WAAW,GACZ,MAAM,yBAAyB,CAAC;AAEjC;;;GAGG;AACH,qBAAa,WAAY,SAAQ,KAAK;gBACxB,OAAO,EAAE,MAAM;CAM5B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,WAAW,MAAM;IACrB,sDAAsD;IACtD,WAAW,EAAE,MAAM,cAAc,CAAC;IAElC,gEAAgE;IAChE,SAAS,EAAE,MAAM,cAAc,CAAC;IAEhC;;;OAGG;IACH,MAAM,EAAE,CAAC,GAAG,EAAE,UAAU,GAAG,SAAS,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;IAEtD;;;OAGG;IACH,KAAK,EAAE,MAAM,cAAc,CAAC;IAE5B;;;OAGG;IACH,MAAM,EAAE,CAAC,CAAC,EAAE,cAAc,KAAK,UAAU,CAAC;IAE1C;;;OAGG;IACH,QAAQ,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,IAAI,CAAC;IAE3C;;;;;;OAMG;IACH,KAAK,EAAE,MAAM,IAAI,CAAC;IAElB;;;;OAIG;IACH,MAAM,EAAE,MAAM,UAAU,CAAC;CAC1B;AAED,eAAO,MAAM,YAAY,GACvB,YAAY,UAAU,GAAG,SAAS,CAAC,MAAM,CAAC,KACzC,MA4DF,CAAC"}
@@ -1,4 +1,5 @@
1
- export { bytesToHex, bytesToUtf8, concatBytes, hexToBytes, utf8ToBytes, } from "@noble/ciphers/utils";
1
+ import { NonNegativeInt } from "./Type.js";
2
+ export { bytesToHex, bytesToUtf8, concatBytes, hexToBytes, utf8ToBytes, } from "@noble/ciphers/utils.js";
2
3
  /**
3
4
  * Custom error for {@link Buffer}-related failures like premature end of data.
4
5
  * Provides better stack traces for debugging binary protocol issues.
@@ -14,9 +15,9 @@ export const createBuffer = (arrayLike) => {
14
15
  let value = arrayLike
15
16
  ? new globalThis.Uint8Array(arrayLike)
16
17
  : new globalThis.Uint8Array(512);
17
- let length = (arrayLike ? arrayLike.length : 0);
18
+ let length = NonNegativeInt.orThrow(arrayLike ? arrayLike.length : 0);
18
19
  const buffer = {
19
- getCapacity: () => value.length,
20
+ getCapacity: () => NonNegativeInt.orThrow(value.length),
20
21
  getLength: () => length,
21
22
  extend: (arg) => {
22
23
  const targetSize = length + arg.length;
@@ -27,7 +28,7 @@ export const createBuffer = (arrayLike) => {
27
28
  value.set(oldValue);
28
29
  }
29
30
  value.set(arg, length);
30
- length = (length + arg.length);
31
+ length = NonNegativeInt.orThrow(length + arg.length);
31
32
  },
32
33
  shift: () => {
33
34
  if (length === 0) {
@@ -36,7 +37,7 @@ export const createBuffer = (arrayLike) => {
36
37
  const first = value[0];
37
38
  value = value.subarray(1);
38
39
  length--;
39
- return first;
40
+ return NonNegativeInt.orThrow(first);
40
41
  },
41
42
  shiftN: (n) => {
42
43
  if (length < n) {
@@ -44,7 +45,7 @@ export const createBuffer = (arrayLike) => {
44
45
  }
45
46
  const subarray = value.subarray(0, n);
46
47
  value = value.subarray(n);
47
- length = (length - n);
48
+ length = NonNegativeInt.orThrow(length - n);
48
49
  return subarray;
49
50
  },
50
51
  truncate: (newLength) => {
@@ -54,7 +55,7 @@ export const createBuffer = (arrayLike) => {
54
55
  length = newLength;
55
56
  },
56
57
  reset: () => {
57
- length = 0;
58
+ length = NonNegativeInt.orThrow(0);
58
59
  },
59
60
  unwrap: () => value.subarray(0, length),
60
61
  };
@@ -0,0 +1,44 @@
1
+ /**
2
+ * 🗄️ Generic cache interface and LRU cache implementation.
3
+ *
4
+ * @module
5
+ */
6
+ import { PositiveInt } from "./Type.js";
7
+ /**
8
+ * Generic cache interface providing basic key-value storage operations.
9
+ *
10
+ * Keys are compared by reference (standard Map semantics).
11
+ *
12
+ * Note: Cache does not extend Map because eviction policies (like in LRU)
13
+ * violate the Liskov Substitution Principle.
14
+ */
15
+ export interface Cache<K, V> {
16
+ /** Checks if a key exists in the cache. */
17
+ has: (key: K) => boolean;
18
+ /** Retrieves the value for a key, or undefined if not present. */
19
+ get: (key: K) => V | undefined;
20
+ /** Stores a key-value pair in the cache. */
21
+ set: (key: K, val: V) => void;
22
+ /** Removes a key from the cache. */
23
+ delete: (key: K) => void;
24
+ /** Returns a readonly view of the internal Map. */
25
+ readonly map: ReadonlyMap<K, V>;
26
+ }
27
+ /**
28
+ * Creates a Least Recently Used (LRU) cache with a maximum capacity.
29
+ *
30
+ * When the cache reaches capacity, the least recently used entry is evicted.
31
+ * Both `get` and `set` operations update the access order.
32
+ *
33
+ * ### Example
34
+ *
35
+ * ```ts
36
+ * const cache = createLruCache<string, number>(2);
37
+ * cache.set("a", 1);
38
+ * cache.set("b", 2);
39
+ * cache.set("c", 3); // Evicts "a"
40
+ * cache.has("a"); // false
41
+ * ```
42
+ */
43
+ export declare const createLruCache: <K, V>(capacity: PositiveInt) => Cache<K, V>;
44
+ //# sourceMappingURL=Cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Cache.d.ts","sourceRoot":"","sources":["../../src/Cache.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAExC;;;;;;;GAOG;AACH,MAAM,WAAW,KAAK,CAAC,CAAC,EAAE,CAAC;IACzB,2CAA2C;IAC3C,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,OAAO,CAAC;IAEzB,kEAAkE;IAClE,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;IAE/B,4CAA4C;IAC5C,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,KAAK,IAAI,CAAC;IAE9B,oCAAoC;IACpC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,CAAC;IAEzB,mDAAmD;IACnD,QAAQ,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;CACjC;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,EAAE,CAAC,EAAE,UAAU,WAAW,KAAG,KAAK,CAAC,CAAC,EAAE,CAAC,CAmCtE,CAAC"}
@@ -0,0 +1,52 @@
1
+ /**
2
+ * 🗄️ Generic cache interface and LRU cache implementation.
3
+ *
4
+ * @module
5
+ */
6
+ /**
7
+ * Creates a Least Recently Used (LRU) cache with a maximum capacity.
8
+ *
9
+ * When the cache reaches capacity, the least recently used entry is evicted.
10
+ * Both `get` and `set` operations update the access order.
11
+ *
12
+ * ### Example
13
+ *
14
+ * ```ts
15
+ * const cache = createLruCache<string, number>(2);
16
+ * cache.set("a", 1);
17
+ * cache.set("b", 2);
18
+ * cache.set("c", 3); // Evicts "a"
19
+ * cache.has("a"); // false
20
+ * ```
21
+ */
22
+ export const createLruCache = (capacity) => {
23
+ const internalMap = new Map();
24
+ return {
25
+ has: (key) => internalMap.has(key),
26
+ get: (key) => {
27
+ const value = internalMap.get(key);
28
+ if (value === undefined)
29
+ return undefined;
30
+ // Move to end (most recently used)
31
+ internalMap.delete(key);
32
+ internalMap.set(key, value);
33
+ return value;
34
+ },
35
+ set: (key, val) => {
36
+ // If key exists, delete it first to update order
37
+ if (internalMap.has(key)) {
38
+ internalMap.delete(key);
39
+ }
40
+ else if (internalMap.size === capacity) {
41
+ // Evict least recently used (first entry)
42
+ const firstKey = internalMap.keys().next().value;
43
+ internalMap.delete(firstKey);
44
+ }
45
+ internalMap.set(key, val);
46
+ },
47
+ delete: (key) => {
48
+ internalMap.delete(key);
49
+ },
50
+ map: internalMap,
51
+ };
52
+ };
@@ -1,20 +1,53 @@
1
- import { NanoIdLibDep } from "./NanoId.js";
2
- import { Brand } from "./Types.js";
1
+ import { Brand } from "./Brand.js";
2
+ import { RandomBytesDep } from "./Crypto.js";
3
+ import { Id } from "./Type.js";
3
4
  /**
4
- * Manages one-time callback functions.
5
+ * Request-response correlation for callbacks across boundaries.
5
6
  *
6
- * Allows registering callbacks with a unique ID and executing them once with an
7
- * optional argument. Executed callbacks are automatically removed.
7
+ * Stores callbacks with unique IDs and executes them once with an optional
8
+ * argument. Executed callbacks are automatically removed.
8
9
  *
9
- * This is useful for managing event-driven or asynchronous workflows where
10
- * callbacks need to be invoked only once.
10
+ * This is useful for correlating asynchronous request-response operations
11
+ * across boundaries where callback functions cannot be passed directly (e.g.,
12
+ * web workers, message queues).
13
+ *
14
+ * The `execute` method intentionally does not use try-catch or {@link Result}
15
+ * because it's the callback's responsibility to handle its own errors.
16
+ *
17
+ * ### Example
18
+ *
19
+ * ```ts
20
+ * // No-argument callbacks
21
+ * const callbacks = createCallbacks(deps);
22
+ * const id = callbacks.register(() => console.log("called"));
23
+ * callbacks.execute(id);
24
+ *
25
+ * // With argument callbacks
26
+ * const stringCallbacks = createCallbacks<string>(deps);
27
+ * const id = stringCallbacks.register((value) => {
28
+ * console.log(value);
29
+ * });
30
+ * stringCallbacks.execute(id, "hello");
31
+ *
32
+ * // Promise.withResolvers pattern
33
+ * const promiseCallbacks = createCallbacks<string>(deps);
34
+ * const { promise, resolve } = Promise.withResolvers<string>();
35
+ * const id = promiseCallbacks.register(resolve);
36
+ * promiseCallbacks.execute(id, "resolved value");
37
+ * await promise; // "resolved value"
38
+ * ```
39
+ *
40
+ * @template T - The type of argument passed to callbacks (defaults to undefined
41
+ * for no-argument callbacks)
11
42
  */
12
- export interface Callbacks {
43
+ export interface Callbacks<T = undefined> {
13
44
  /** Registers a callback function and returns a unique ID. */
14
- readonly register: (callback: (arg?: unknown) => void) => CallbackId;
45
+ readonly register: (callback: (arg: T) => void) => CallbackId;
15
46
  /** Executes and removes a callback associated with the given ID. */
16
- readonly execute: (id: CallbackId, arg?: unknown) => void;
47
+ readonly execute: T extends undefined ? (id: CallbackId) => undefined : (id: CallbackId, arg: T) => undefined;
17
48
  }
18
- export type CallbackId = string & Brand<"CallbackId">;
19
- export declare const createCallbacks: (deps: NanoIdLibDep) => Callbacks;
49
+ /** Unique identifier for a callback in {@link Callbacks}. */
50
+ export type CallbackId = Id & Brand<"Callback">;
51
+ /** Creates a new {@link Callbacks}. */
52
+ export declare const createCallbacks: <T = undefined>(deps: RandomBytesDep) => Callbacks<T>;
20
53
  //# sourceMappingURL=Callbacks.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Callbacks.d.ts","sourceRoot":"","sources":["../../src/Callbacks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;;;;;GAQG;AACH,MAAM,WAAW,SAAS;IACxB,6DAA6D;IAC7D,QAAQ,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,EAAE,OAAO,KAAK,IAAI,KAAK,UAAU,CAAC;IAErE,oEAAoE;IACpE,QAAQ,CAAC,OAAO,EAAE,CAAC,EAAE,EAAE,UAAU,EAAE,GAAG,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;CAC3D;AAED,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,KAAK,CAAC,YAAY,CAAC,CAAC;AAEtD,eAAO,MAAM,eAAe,GAAI,MAAM,YAAY,KAAG,SAoBpD,CAAC"}
1
+ {"version":3,"file":"Callbacks.d.ts","sourceRoot":"","sources":["../../src/Callbacks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,EAAY,EAAE,EAAE,MAAM,WAAW,CAAC;AAEzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,WAAW,SAAS,CAAC,CAAC,GAAG,SAAS;IACtC,6DAA6D;IAC7D,QAAQ,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,KAAK,UAAU,CAAC;IAE9D,oEAAoE;IACpE,QAAQ,CAAC,OAAO,EAAE,CAAC,SAAS,SAAS,GACjC,CAAC,EAAE,EAAE,UAAU,KAAK,SAAS,GAC7B,CAAC,EAAE,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC,KAAK,SAAS,CAAC;CAC3C;AAED,6DAA6D;AAC7D,MAAM,MAAM,UAAU,GAAG,EAAE,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;AAEhD,uCAAuC;AACvC,eAAO,MAAM,eAAe,GAAI,CAAC,GAAG,SAAS,EAC3C,MAAM,cAAc,KACnB,SAAS,CAAC,CAAC,CAsBb,CAAC"}
@@ -1,18 +1,25 @@
1
+ import { createId } from "./Type.js";
2
+ /** Creates a new {@link Callbacks}. */
1
3
  export const createCallbacks = (deps) => {
2
4
  const callbackMap = new Map();
3
- const callbacks = {
5
+ return {
4
6
  register: (callback) => {
5
- const id = deps.nanoIdLib.nanoid();
7
+ const id = createId(deps);
6
8
  callbackMap.set(id, callback);
7
9
  return id;
8
10
  },
9
- execute: (id, arg) => {
11
+ execute: (id, ...args) => {
10
12
  const callback = callbackMap.get(id);
11
- if (callback) {
12
- callbackMap.delete(id);
13
- callback(arg);
13
+ if (!callback)
14
+ return;
15
+ callbackMap.delete(id);
16
+ if (args.length === 0) {
17
+ // Called without argument (undefined case)
18
+ callback();
19
+ }
20
+ else {
21
+ callback(args[0]);
14
22
  }
15
23
  },
16
24
  };
17
- return callbacks;
18
25
  };
@@ -13,14 +13,28 @@
13
13
  * ### Example
14
14
  *
15
15
  * ```ts
16
- * deps.console.log("[db]", "send data message", protocolMessage);
16
+ * deps.console.log("[evolu]", "createEvoluInstance", { name });
17
17
  * ```
18
18
  *
19
19
  * **Tip**: In browser dev tools, you can filter logs by tag (e.g., `[db]`) to
20
- * quickly find relevant messages.
20
+ * quickly find relevant messages. In Node.js, use `grep` to filter output:
21
21
  *
22
- * **Warning**: If you encounter platform-specific issues or missing methods,
23
- * please contribute a PR with details about the environment and behavior.
22
+ * ```bash
23
+ * node app.js | grep "\[relay\]" # Show only relay logs
24
+ * node app.js | grep -E "\[db\]|\[sql\]" # Show db and sql logs
25
+ * node app.js | grep -v "\[debug\]" # Hide debug logs
26
+ * ```
27
+ *
28
+ * Or add to package.json scripts:
29
+ *
30
+ * ```json
31
+ * {
32
+ * "scripts": {
33
+ * "dev:relay": "node app.js | grep \"\\[relay\\]\"",
34
+ * "dev:db": "node app.js | grep -E \"\\[db\\]|\\[sql\\]\""
35
+ * }
36
+ * }
37
+ * ```
24
38
  *
25
39
  * @module
26
40
  */
@@ -43,6 +57,8 @@ export interface Console {
43
57
  debug: (...args: Array<any>) => void;
44
58
  /** Starts a timer with an optional label */
45
59
  time: (label?: string) => void;
60
+ /** Logs the elapsed time for a timer without ending it */
61
+ timeLog: (label?: string, ...data: Array<any>) => void;
46
62
  /** Ends a timer and logs the elapsed time */
47
63
  timeEnd: (label?: string) => void;
48
64
  /** Displays an object's properties in a detailed format */
@@ -55,8 +71,6 @@ export interface Console {
55
71
  countReset: (label?: string) => void;
56
72
  /** Writes a message if the value is falsy, otherwise does nothing */
57
73
  assert: (value: any, message?: string, ...optionalParams: Array<any>) => void;
58
- /** Logs the elapsed time for a timer without ending it */
59
- timeLog: (label?: string, ...data: Array<any>) => void;
60
74
  /** Prints a stack trace with an optional message */
61
75
  trace: (message?: any, ...optionalParams: Array<any>) => void;
62
76
  }
@@ -75,4 +89,15 @@ export interface ConsoleConfig {
75
89
  }
76
90
  /** Creates a console instance using the global console. */
77
91
  export declare const createConsole: (config?: ConsoleConfig) => Console;
92
+ export interface ConsoleWithTimeConfig extends ConsoleConfig {
93
+ /**
94
+ * Type of timestamp to prepend to log messages.
95
+ *
96
+ * - 'absolute': Shows actual time (e.g., "14:32:15.234")
97
+ * - 'relative': Shows time since console creation (e.g., "+1.234s")
98
+ */
99
+ readonly timestampType: "absolute" | "relative";
100
+ }
101
+ /** Creates a console instance with timestamp prefixes. */
102
+ export declare const createConsoleWithTime: (config?: ConsoleWithTimeConfig) => Console;
78
103
  //# sourceMappingURL=Console.d.ts.map