@kronos-ts/messaging 0.1.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 (266) hide show
  1. package/dist/command-bus.d.ts +30 -0
  2. package/dist/command-bus.d.ts.map +1 -0
  3. package/dist/command-bus.js +2 -0
  4. package/dist/command-bus.js.map +1 -0
  5. package/dist/command-handler.d.ts +58 -0
  6. package/dist/command-handler.d.ts.map +1 -0
  7. package/dist/command-handler.js +12 -0
  8. package/dist/command-handler.js.map +1 -0
  9. package/dist/command-handling-module.d.ts +53 -0
  10. package/dist/command-handling-module.d.ts.map +1 -0
  11. package/dist/command-handling-module.js +130 -0
  12. package/dist/command-handling-module.js.map +1 -0
  13. package/dist/correlation-data.d.ts +79 -0
  14. package/dist/correlation-data.d.ts.map +1 -0
  15. package/dist/correlation-data.js +133 -0
  16. package/dist/correlation-data.js.map +1 -0
  17. package/dist/dead-letter-queue.d.ts +134 -0
  18. package/dist/dead-letter-queue.d.ts.map +1 -0
  19. package/dist/dead-letter-queue.js +176 -0
  20. package/dist/dead-letter-queue.js.map +1 -0
  21. package/dist/dead-lettering-handler.d.ts +42 -0
  22. package/dist/dead-lettering-handler.d.ts.map +1 -0
  23. package/dist/dead-lettering-handler.js +67 -0
  24. package/dist/dead-lettering-handler.js.map +1 -0
  25. package/dist/descriptor.d.ts +135 -0
  26. package/dist/descriptor.d.ts.map +1 -0
  27. package/dist/descriptor.js +36 -0
  28. package/dist/descriptor.js.map +1 -0
  29. package/dist/emit-update.d.ts +22 -0
  30. package/dist/emit-update.d.ts.map +1 -0
  31. package/dist/emit-update.js +23 -0
  32. package/dist/emit-update.js.map +1 -0
  33. package/dist/event-bus.d.ts +29 -0
  34. package/dist/event-bus.d.ts.map +1 -0
  35. package/dist/event-bus.js +22 -0
  36. package/dist/event-bus.js.map +1 -0
  37. package/dist/event-criteria.d.ts +87 -0
  38. package/dist/event-criteria.d.ts.map +1 -0
  39. package/dist/event-criteria.js +90 -0
  40. package/dist/event-criteria.js.map +1 -0
  41. package/dist/event-gateway.d.ts +19 -0
  42. package/dist/event-gateway.d.ts.map +1 -0
  43. package/dist/event-gateway.js +22 -0
  44. package/dist/event-gateway.js.map +1 -0
  45. package/dist/event-handler.d.ts +30 -0
  46. package/dist/event-handler.d.ts.map +1 -0
  47. package/dist/event-handler.js +18 -0
  48. package/dist/event-handler.js.map +1 -0
  49. package/dist/event-processor-builder.d.ts +148 -0
  50. package/dist/event-processor-builder.d.ts.map +1 -0
  51. package/dist/event-processor-builder.js +175 -0
  52. package/dist/event-processor-builder.js.map +1 -0
  53. package/dist/event-processor.d.ts +10 -0
  54. package/dist/event-processor.d.ts.map +1 -0
  55. package/dist/event-processor.js +2 -0
  56. package/dist/event-processor.js.map +1 -0
  57. package/dist/event-sink.d.ts +23 -0
  58. package/dist/event-sink.d.ts.map +1 -0
  59. package/dist/event-sink.js +2 -0
  60. package/dist/event-sink.js.map +1 -0
  61. package/dist/event-source.d.ts +98 -0
  62. package/dist/event-source.d.ts.map +1 -0
  63. package/dist/event-source.js +191 -0
  64. package/dist/event-source.js.map +1 -0
  65. package/dist/gateway.d.ts +68 -0
  66. package/dist/gateway.d.ts.map +1 -0
  67. package/dist/gateway.js +62 -0
  68. package/dist/gateway.js.map +1 -0
  69. package/dist/handler-enhancer.d.ts +53 -0
  70. package/dist/handler-enhancer.d.ts.map +1 -0
  71. package/dist/handler-enhancer.js +17 -0
  72. package/dist/handler-enhancer.js.map +1 -0
  73. package/dist/handler.d.ts +51 -0
  74. package/dist/handler.d.ts.map +1 -0
  75. package/dist/handler.js +26 -0
  76. package/dist/handler.js.map +1 -0
  77. package/dist/index.d.ts +53 -0
  78. package/dist/index.d.ts.map +1 -0
  79. package/dist/index.js +103 -0
  80. package/dist/index.js.map +1 -0
  81. package/dist/intercepting-command-bus.d.ts +17 -0
  82. package/dist/intercepting-command-bus.d.ts.map +1 -0
  83. package/dist/intercepting-command-bus.js +54 -0
  84. package/dist/intercepting-command-bus.js.map +1 -0
  85. package/dist/intercepting-event-bus.d.ts +8 -0
  86. package/dist/intercepting-event-bus.d.ts.map +1 -0
  87. package/dist/intercepting-event-bus.js +22 -0
  88. package/dist/intercepting-event-bus.js.map +1 -0
  89. package/dist/intercepting-query-bus.d.ts +17 -0
  90. package/dist/intercepting-query-bus.d.ts.map +1 -0
  91. package/dist/intercepting-query-bus.js +68 -0
  92. package/dist/intercepting-query-bus.js.map +1 -0
  93. package/dist/interceptor.d.ts +46 -0
  94. package/dist/interceptor.d.ts.map +1 -0
  95. package/dist/interceptor.js +2 -0
  96. package/dist/interceptor.js.map +1 -0
  97. package/dist/message-monitor-registry.d.ts +28 -0
  98. package/dist/message-monitor-registry.d.ts.map +1 -0
  99. package/dist/message-monitor-registry.js +37 -0
  100. package/dist/message-monitor-registry.js.map +1 -0
  101. package/dist/message-monitor.d.ts +36 -0
  102. package/dist/message-monitor.d.ts.map +1 -0
  103. package/dist/message-monitor.js +39 -0
  104. package/dist/message-monitor.js.map +1 -0
  105. package/dist/message.d.ts +42 -0
  106. package/dist/message.d.ts.map +1 -0
  107. package/dist/message.js +2 -0
  108. package/dist/message.js.map +1 -0
  109. package/dist/processing-state.d.ts +115 -0
  110. package/dist/processing-state.d.ts.map +1 -0
  111. package/dist/processing-state.js +205 -0
  112. package/dist/processing-state.js.map +1 -0
  113. package/dist/processor-configuration.d.ts +51 -0
  114. package/dist/processor-configuration.d.ts.map +1 -0
  115. package/dist/processor-configuration.js +2 -0
  116. package/dist/processor-configuration.js.map +1 -0
  117. package/dist/query-bus.d.ts +51 -0
  118. package/dist/query-bus.d.ts.map +1 -0
  119. package/dist/query-bus.js +2 -0
  120. package/dist/query-bus.js.map +1 -0
  121. package/dist/query-handler.d.ts +35 -0
  122. package/dist/query-handler.d.ts.map +1 -0
  123. package/dist/query-handler.js +19 -0
  124. package/dist/query-handler.js.map +1 -0
  125. package/dist/query-handling-module.d.ts +24 -0
  126. package/dist/query-handling-module.d.ts.map +1 -0
  127. package/dist/query-handling-module.js +32 -0
  128. package/dist/query-handling-module.js.map +1 -0
  129. package/dist/replay-token.d.ts +31 -0
  130. package/dist/replay-token.d.ts.map +1 -0
  131. package/dist/replay-token.js +37 -0
  132. package/dist/replay-token.js.map +1 -0
  133. package/dist/retrying-command-bus.d.ts +32 -0
  134. package/dist/retrying-command-bus.d.ts.map +1 -0
  135. package/dist/retrying-command-bus.js +58 -0
  136. package/dist/retrying-command-bus.js.map +1 -0
  137. package/dist/routing-strategy.d.ts +30 -0
  138. package/dist/routing-strategy.d.ts.map +1 -0
  139. package/dist/routing-strategy.js +37 -0
  140. package/dist/routing-strategy.js.map +1 -0
  141. package/dist/segment.d.ts +72 -0
  142. package/dist/segment.d.ts.map +1 -0
  143. package/dist/segment.js +103 -0
  144. package/dist/segment.js.map +1 -0
  145. package/dist/send.d.ts +28 -0
  146. package/dist/send.d.ts.map +1 -0
  147. package/dist/send.js +36 -0
  148. package/dist/send.js.map +1 -0
  149. package/dist/serializer.d.ts +40 -0
  150. package/dist/serializer.d.ts.map +1 -0
  151. package/dist/serializer.js +90 -0
  152. package/dist/serializer.js.map +1 -0
  153. package/dist/simple-command-bus.d.ts +23 -0
  154. package/dist/simple-command-bus.d.ts.map +1 -0
  155. package/dist/simple-command-bus.js +49 -0
  156. package/dist/simple-command-bus.js.map +1 -0
  157. package/dist/simple-query-bus.d.ts +16 -0
  158. package/dist/simple-query-bus.d.ts.map +1 -0
  159. package/dist/simple-query-bus.js +122 -0
  160. package/dist/simple-query-bus.js.map +1 -0
  161. package/dist/span-factory.d.ts +58 -0
  162. package/dist/span-factory.d.ts.map +1 -0
  163. package/dist/span-factory.js +19 -0
  164. package/dist/span-factory.js.map +1 -0
  165. package/dist/streaming-event-processor.d.ts +65 -0
  166. package/dist/streaming-event-processor.d.ts.map +1 -0
  167. package/dist/streaming-event-processor.js +239 -0
  168. package/dist/streaming-event-processor.js.map +1 -0
  169. package/dist/subscribing-event-processor.d.ts +57 -0
  170. package/dist/subscribing-event-processor.d.ts.map +1 -0
  171. package/dist/subscribing-event-processor.js +100 -0
  172. package/dist/subscribing-event-processor.js.map +1 -0
  173. package/dist/subscription-query.d.ts +63 -0
  174. package/dist/subscription-query.d.ts.map +1 -0
  175. package/dist/subscription-query.js +119 -0
  176. package/dist/subscription-query.js.map +1 -0
  177. package/dist/token-store.d.ts +83 -0
  178. package/dist/token-store.d.ts.map +1 -0
  179. package/dist/token-store.js +112 -0
  180. package/dist/token-store.js.map +1 -0
  181. package/dist/tracing-command-bus.d.ts +16 -0
  182. package/dist/tracing-command-bus.d.ts.map +1 -0
  183. package/dist/tracing-command-bus.js +44 -0
  184. package/dist/tracing-command-bus.js.map +1 -0
  185. package/dist/tracing-handler-enhancer.d.ts +11 -0
  186. package/dist/tracing-handler-enhancer.d.ts.map +1 -0
  187. package/dist/tracing-handler-enhancer.js +27 -0
  188. package/dist/tracing-handler-enhancer.js.map +1 -0
  189. package/dist/tracking-event-processor.d.ts +72 -0
  190. package/dist/tracking-event-processor.d.ts.map +1 -0
  191. package/dist/tracking-event-processor.js +223 -0
  192. package/dist/tracking-event-processor.js.map +1 -0
  193. package/dist/tracking-token.d.ts +120 -0
  194. package/dist/tracking-token.d.ts.map +1 -0
  195. package/dist/tracking-token.js +132 -0
  196. package/dist/tracking-token.js.map +1 -0
  197. package/dist/transaction.d.ts +60 -0
  198. package/dist/transaction.d.ts.map +1 -0
  199. package/dist/transaction.js +74 -0
  200. package/dist/transaction.js.map +1 -0
  201. package/dist/unit-of-work.d.ts +41 -0
  202. package/dist/unit-of-work.d.ts.map +1 -0
  203. package/dist/unit-of-work.js +96 -0
  204. package/dist/unit-of-work.js.map +1 -0
  205. package/dist/upcaster.d.ts +91 -0
  206. package/dist/upcaster.d.ts.map +1 -0
  207. package/dist/upcaster.js +114 -0
  208. package/dist/upcaster.js.map +1 -0
  209. package/dist/with-namespace.d.ts +59 -0
  210. package/dist/with-namespace.d.ts.map +1 -0
  211. package/dist/with-namespace.js +42 -0
  212. package/dist/with-namespace.js.map +1 -0
  213. package/package.json +65 -0
  214. package/src/command-bus.ts +34 -0
  215. package/src/command-handler.ts +116 -0
  216. package/src/command-handling-module.ts +183 -0
  217. package/src/correlation-data.ts +169 -0
  218. package/src/dead-letter-queue.ts +330 -0
  219. package/src/dead-lettering-handler.ts +109 -0
  220. package/src/descriptor.ts +176 -0
  221. package/src/emit-update.ts +35 -0
  222. package/src/event-bus.ts +45 -0
  223. package/src/event-criteria.ts +141 -0
  224. package/src/event-gateway.ts +42 -0
  225. package/src/event-handler.ts +44 -0
  226. package/src/event-processor-builder.ts +246 -0
  227. package/src/event-processor.ts +9 -0
  228. package/src/event-sink.ts +23 -0
  229. package/src/event-source.ts +301 -0
  230. package/src/gateway.ts +144 -0
  231. package/src/handler-enhancer.ts +70 -0
  232. package/src/handler.ts +133 -0
  233. package/src/index.ts +356 -0
  234. package/src/intercepting-command-bus.ts +73 -0
  235. package/src/intercepting-event-bus.ts +29 -0
  236. package/src/intercepting-query-bus.ts +104 -0
  237. package/src/interceptor.ts +48 -0
  238. package/src/message-monitor-registry.ts +64 -0
  239. package/src/message-monitor.ts +68 -0
  240. package/src/message.ts +41 -0
  241. package/src/processing-state.ts +258 -0
  242. package/src/processor-configuration.ts +59 -0
  243. package/src/query-bus.ts +69 -0
  244. package/src/query-handler.ts +49 -0
  245. package/src/query-handling-module.ts +44 -0
  246. package/src/replay-token.ts +53 -0
  247. package/src/retrying-command-bus.ts +80 -0
  248. package/src/routing-strategy.ts +59 -0
  249. package/src/segment.ts +136 -0
  250. package/src/send.ts +44 -0
  251. package/src/serializer.ts +122 -0
  252. package/src/simple-command-bus.ts +59 -0
  253. package/src/simple-query-bus.ts +158 -0
  254. package/src/span-factory.ts +81 -0
  255. package/src/streaming-event-processor.ts +351 -0
  256. package/src/subscribing-event-processor.ts +169 -0
  257. package/src/subscription-query.ts +173 -0
  258. package/src/token-store.ts +211 -0
  259. package/src/tracing-command-bus.ts +52 -0
  260. package/src/tracing-handler-enhancer.ts +34 -0
  261. package/src/tracking-event-processor.ts +336 -0
  262. package/src/tracking-token.ts +231 -0
  263. package/src/transaction.ts +98 -0
  264. package/src/unit-of-work.ts +138 -0
  265. package/src/upcaster.ts +174 -0
  266. package/src/with-namespace.ts +75 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retrying-command-bus.js","sourceRoot":"","sources":["../src/retrying-command-bus.ts"],"names":[],"mappings":"AAeA;;;GAGG;AACH,MAAM,UAAU,6BAA6B,CAAC,OAI7C;IACC,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,IAAI,CAAC,CAAA;IAC3C,MAAM,cAAc,GAAG,OAAO,EAAE,cAAc,IAAI,EAAE,CAAA;IACpD,MAAM,WAAW,GAAG,OAAO,EAAE,WAAW,IAAI,kBAAkB,CAAA;IAE9D,OAAO;QACL,WAAW,CAAC,KAAc,EAAE,OAAe;YACzC,IAAI,OAAO,IAAI,UAAU;gBAAE,OAAO,SAAS,CAAA;YAC3C,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC;gBAAE,OAAO,SAAS,CAAA;YACzC,OAAO,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,CAAA;QAC9C,CAAC;KACF,CAAA;AACH,CAAC;AAED,SAAS,kBAAkB,CAAC,KAAc;IACxC,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAC,IAAI,KAAK,sBAAsB,CAAA;IAC9C,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,wBAAwB,CACtC,QAAoB,EACpB,MAAmB;IAEnB,OAAO;QACL,KAAK,CAAC,QAAQ,CAAC,OAAuB;YACpC,IAAI,OAAO,GAAG,CAAC,CAAA;YACf,OAAO,IAAI,EAAE,CAAC;gBACZ,IAAI,CAAC;oBACH,OAAO,MAAM,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAA;gBACzC,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;oBAChD,IAAI,KAAK,KAAK,SAAS;wBAAE,MAAM,KAAK,CAAA;oBAEpC,OAAO,EAAE,CAAA;oBACT,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;wBACd,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAA;oBAC5D,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;QAED,SAAS,CAAC,WAAmB,EAAE,OAAsD;YACnF,QAAQ,CAAC,SAAS,CAAC,WAAW,EAAE,OAAO,CAAC,CAAA;QAC1C,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -0,0 +1,30 @@
1
+ import type { CommandMessage } from "./message.js";
2
+ /**
3
+ * Determines the routing key for a command message.
4
+ *
5
+ * Used by the distributed command bus to route commands to the correct
6
+ * handler instance via consistent hashing. Commands with the same routing
7
+ * key are routed to the same handler.
8
+ */
9
+ export interface RoutingStrategy {
10
+ /**
11
+ * Get the routing key for the given command message.
12
+ * Returns a string that identifies the target for this command
13
+ * (typically an aggregate identifier).
14
+ */
15
+ getRoutingKey(message: CommandMessage): string;
16
+ }
17
+ /**
18
+ * Extracts the routing key from a command message's metadata.
19
+ *
20
+ * @param metadataKey The metadata key to extract the routing key from.
21
+ */
22
+ export declare function metadataRoutingStrategy(metadataKey: string): RoutingStrategy;
23
+ /**
24
+ * Extracts the routing key from a field of the command payload.
25
+ * The field name is specified explicitly on the descriptor/configuration.
26
+ *
27
+ * @param field The payload field to extract the routing key from.
28
+ */
29
+ export declare function payloadFieldRoutingStrategy(field: string): RoutingStrategy;
30
+ //# sourceMappingURL=routing-strategy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routing-strategy.d.ts","sourceRoot":"","sources":["../src/routing-strategy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAElD;;;;;;GAMG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;OAIG;IACH,aAAa,CAAC,OAAO,EAAE,cAAc,GAAG,MAAM,CAAA;CAC/C;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,WAAW,EAAE,MAAM,GAAG,eAAe,CAa5E;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,CAc1E"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Extracts the routing key from a command message's metadata.
3
+ *
4
+ * @param metadataKey The metadata key to extract the routing key from.
5
+ */
6
+ export function metadataRoutingStrategy(metadataKey) {
7
+ return {
8
+ getRoutingKey(message) {
9
+ const value = message.metadata[metadataKey];
10
+ if (value == null) {
11
+ throw new Error(`No routing key found in metadata key "${metadataKey}" ` +
12
+ `for command "${String(message.name)}"`);
13
+ }
14
+ return String(value);
15
+ },
16
+ };
17
+ }
18
+ /**
19
+ * Extracts the routing key from a field of the command payload.
20
+ * The field name is specified explicitly on the descriptor/configuration.
21
+ *
22
+ * @param field The payload field to extract the routing key from.
23
+ */
24
+ export function payloadFieldRoutingStrategy(field) {
25
+ return {
26
+ getRoutingKey(message) {
27
+ const payload = message.payload;
28
+ const value = payload?.[field];
29
+ if (value == null) {
30
+ throw new Error(`No routing key found in payload field "${field}" ` +
31
+ `for command "${String(message.name)}"`);
32
+ }
33
+ return String(value);
34
+ },
35
+ };
36
+ }
37
+ //# sourceMappingURL=routing-strategy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routing-strategy.js","sourceRoot":"","sources":["../src/routing-strategy.ts"],"names":[],"mappings":"AAkBA;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CAAC,WAAmB;IACzD,OAAO;QACL,aAAa,CAAC,OAAuB;YACnC,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAA;YAC3C,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;gBAClB,MAAM,IAAI,KAAK,CACb,yCAAyC,WAAW,IAAI;oBACxD,gBAAgB,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CACxC,CAAA;YACH,CAAC;YACD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAA;QACtB,CAAC;KACF,CAAA;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,2BAA2B,CAAC,KAAa;IACvD,OAAO;QACL,aAAa,CAAC,OAAuB;YACnC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAkC,CAAA;YAC1D,MAAM,KAAK,GAAG,OAAO,EAAE,CAAC,KAAK,CAAC,CAAA;YAC9B,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;gBAClB,MAAM,IAAI,KAAK,CACb,0CAA0C,KAAK,IAAI;oBACnD,gBAAgB,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CACxC,CAAA;YACH,CAAC;YACD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAA;QACtB,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Represents a fraction of the event stream assigned to a processor instance.
3
+ *
4
+ * Segments use bitmask-based routing to deterministically assign events
5
+ * to processors. An event matches a segment when `(hash(event) & mask) === segmentId`.
6
+ *
7
+ * Segments can be split (doubling parallelism) and merged (halving parallelism).
8
+ * This is the foundation for horizontal scaling — multiple instances of the
9
+ * same processor each claim different segments.
10
+ *
11
+ * The ROOT_SEGMENT (segmentId=0, mask=0) matches ALL events and is the
12
+ * starting point before any splitting.
13
+ */
14
+ export interface Segment {
15
+ /** The unique identifier of this segment. */
16
+ readonly segmentId: number;
17
+ /**
18
+ * The bitmask used to match events to this segment.
19
+ * An event matches when `(hash & mask) === segmentId`.
20
+ */
21
+ readonly mask: number;
22
+ }
23
+ /** The root segment — matches all events. Starting point before splitting. */
24
+ export declare const ROOT_SEGMENT: Segment;
25
+ /**
26
+ * Create a segment with the given id and mask.
27
+ */
28
+ export declare function segment(segmentId: number, mask: number): Segment;
29
+ /**
30
+ * Check if a hash value matches this segment.
31
+ * Used to route events to the correct processor instance.
32
+ *
33
+ * @param seg The segment to check against
34
+ * @param hash The hash of the event's sequence identifier (e.g., aggregate ID hash)
35
+ */
36
+ export declare function segmentMatches(seg: Segment, hash: number): boolean;
37
+ /**
38
+ * Split a segment into two child segments.
39
+ * Doubles the processing parallelism for this segment's portion of the stream.
40
+ *
41
+ * Returns a tuple of [segment keeping the original ID, new sibling segment].
42
+ */
43
+ export declare function splitSegment(seg: Segment): [Segment, Segment];
44
+ /**
45
+ * Check if two segments can be merged (they are siblings from the same split).
46
+ */
47
+ export declare function isMergeable(a: Segment, b: Segment): boolean;
48
+ /**
49
+ * Merge two sibling segments back into their parent.
50
+ * Halves the processing parallelism.
51
+ *
52
+ * @throws Error if segments are not mergeable
53
+ */
54
+ export declare function mergeSegments(a: Segment, b: Segment): Segment;
55
+ /**
56
+ * Compute the total number of segments at the current split level.
57
+ * For a segment with mask M, the total count is M + 1.
58
+ */
59
+ export declare function segmentCount(seg: Segment): number;
60
+ /**
61
+ * Compute a hash from a string value (for event routing).
62
+ * Uses a simple but well-distributed hash function.
63
+ */
64
+ export declare function hashOf(value: string): number;
65
+ /**
66
+ * Create N balanced segments by splitting the root segment.
67
+ *
68
+ * @param count Number of segments (must be a power of 2)
69
+ * @returns Array of segments that together cover the entire event stream
70
+ */
71
+ export declare function createSegments(count: number): Segment[];
72
+ //# sourceMappingURL=segment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"segment.d.ts","sourceRoot":"","sources":["../src/segment.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,OAAO;IACtB,6CAA6C;IAC7C,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CACtB;AAED,8EAA8E;AAC9E,eAAO,MAAM,YAAY,EAAE,OAAmC,CAAA;AAE9D;;GAEG;AACH,wBAAgB,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAEhE;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAElE;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAQ7D;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAK3D;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAW7D;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAEjD;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAO5C;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,EAAE,CAmBvD"}
@@ -0,0 +1,103 @@
1
+ /** The root segment — matches all events. Starting point before splitting. */
2
+ export const ROOT_SEGMENT = { segmentId: 0, mask: 0 };
3
+ /**
4
+ * Create a segment with the given id and mask.
5
+ */
6
+ export function segment(segmentId, mask) {
7
+ return { segmentId, mask };
8
+ }
9
+ /**
10
+ * Check if a hash value matches this segment.
11
+ * Used to route events to the correct processor instance.
12
+ *
13
+ * @param seg The segment to check against
14
+ * @param hash The hash of the event's sequence identifier (e.g., aggregate ID hash)
15
+ */
16
+ export function segmentMatches(seg, hash) {
17
+ return (hash & seg.mask) === seg.segmentId;
18
+ }
19
+ /**
20
+ * Split a segment into two child segments.
21
+ * Doubles the processing parallelism for this segment's portion of the stream.
22
+ *
23
+ * Returns a tuple of [segment keeping the original ID, new sibling segment].
24
+ */
25
+ export function splitSegment(seg) {
26
+ const newMask = (seg.mask << 1) | 1;
27
+ const newSegmentId = seg.segmentId | (seg.mask + 1);
28
+ return [
29
+ { segmentId: seg.segmentId, mask: newMask },
30
+ { segmentId: newSegmentId, mask: newMask },
31
+ ];
32
+ }
33
+ /**
34
+ * Check if two segments can be merged (they are siblings from the same split).
35
+ */
36
+ export function isMergeable(a, b) {
37
+ if (a.mask !== b.mask)
38
+ return false;
39
+ if (a.mask === 0)
40
+ return false;
41
+ // Siblings differ only in the highest bit of the mask
42
+ return (a.segmentId ^ b.segmentId) === (a.mask >>> 0) - (a.mask >>> 1);
43
+ }
44
+ /**
45
+ * Merge two sibling segments back into their parent.
46
+ * Halves the processing parallelism.
47
+ *
48
+ * @throws Error if segments are not mergeable
49
+ */
50
+ export function mergeSegments(a, b) {
51
+ if (!isMergeable(a, b)) {
52
+ throw new Error(`Segments ${a.segmentId}/${a.mask} and ${b.segmentId}/${b.mask} are not mergeable`);
53
+ }
54
+ return {
55
+ segmentId: Math.min(a.segmentId, b.segmentId),
56
+ mask: a.mask >>> 1,
57
+ };
58
+ }
59
+ /**
60
+ * Compute the total number of segments at the current split level.
61
+ * For a segment with mask M, the total count is M + 1.
62
+ */
63
+ export function segmentCount(seg) {
64
+ return seg.mask + 1;
65
+ }
66
+ /**
67
+ * Compute a hash from a string value (for event routing).
68
+ * Uses a simple but well-distributed hash function.
69
+ */
70
+ export function hashOf(value) {
71
+ let hash = 0;
72
+ for (let i = 0; i < value.length; i++) {
73
+ const char = value.charCodeAt(i);
74
+ hash = ((hash << 5) - hash + char) | 0;
75
+ }
76
+ return hash >>> 0; // Ensure unsigned
77
+ }
78
+ /**
79
+ * Create N balanced segments by splitting the root segment.
80
+ *
81
+ * @param count Number of segments (must be a power of 2)
82
+ * @returns Array of segments that together cover the entire event stream
83
+ */
84
+ export function createSegments(count) {
85
+ if (count <= 0)
86
+ throw new Error("Segment count must be positive");
87
+ if (count === 1)
88
+ return [ROOT_SEGMENT];
89
+ // Find the nearest power of 2
90
+ const power = Math.ceil(Math.log2(count));
91
+ const actualCount = Math.pow(2, power);
92
+ let segments = [ROOT_SEGMENT];
93
+ while (segments.length < actualCount) {
94
+ const next = [];
95
+ for (const seg of segments) {
96
+ const [a, b] = splitSegment(seg);
97
+ next.push(a, b);
98
+ }
99
+ segments = next;
100
+ }
101
+ return segments;
102
+ }
103
+ //# sourceMappingURL=segment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"segment.js","sourceRoot":"","sources":["../src/segment.ts"],"names":[],"mappings":"AAuBA,8EAA8E;AAC9E,MAAM,CAAC,MAAM,YAAY,GAAY,EAAE,SAAS,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAA;AAE9D;;GAEG;AACH,MAAM,UAAU,OAAO,CAAC,SAAiB,EAAE,IAAY;IACrD,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,CAAA;AAC5B,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,GAAY,EAAE,IAAY;IACvD,OAAO,CAAC,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,SAAS,CAAA;AAC5C,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,GAAY;IACvC,MAAM,OAAO,GAAG,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;IACnC,MAAM,YAAY,GAAG,GAAG,CAAC,SAAS,GAAG,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,CAAA;IAEnD,OAAO;QACL,EAAE,SAAS,EAAE,GAAG,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE;QAC3C,EAAE,SAAS,EAAE,YAAY,EAAE,IAAI,EAAE,OAAO,EAAE;KAC3C,CAAA;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,CAAU,EAAE,CAAU;IAChD,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,IAAI;QAAE,OAAO,KAAK,CAAA;IACnC,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC;QAAE,OAAO,KAAK,CAAA;IAC9B,sDAAsD;IACtD,OAAO,CAAC,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAA;AACxE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,CAAU,EAAE,CAAU;IAClD,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CACb,YAAY,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,IAAI,QAAQ,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,IAAI,oBAAoB,CACnF,CAAA;IACH,CAAC;IAED,OAAO;QACL,SAAS,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,SAAS,CAAC;QAC7C,IAAI,EAAE,CAAC,CAAC,IAAI,KAAK,CAAC;KACnB,CAAA;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,GAAY;IACvC,OAAO,GAAG,CAAC,IAAI,GAAG,CAAC,CAAA;AACrB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,MAAM,CAAC,KAAa;IAClC,IAAI,IAAI,GAAG,CAAC,CAAA;IACZ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACtC,MAAM,IAAI,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;QAChC,IAAI,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAA;IACxC,CAAC;IACD,OAAO,IAAI,KAAK,CAAC,CAAA,CAAC,kBAAkB;AACtC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,KAAa;IAC1C,IAAI,KAAK,IAAI,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAA;IACjE,IAAI,KAAK,KAAK,CAAC;QAAE,OAAO,CAAC,YAAY,CAAC,CAAA;IAEtC,8BAA8B;IAC9B,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;IACzC,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAA;IAEtC,IAAI,QAAQ,GAAc,CAAC,YAAY,CAAC,CAAA;IACxC,OAAO,QAAQ,CAAC,MAAM,GAAG,WAAW,EAAE,CAAC;QACrC,MAAM,IAAI,GAAc,EAAE,CAAA;QAC1B,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;YAC3B,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,YAAY,CAAC,GAAG,CAAC,CAAA;YAChC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;QACjB,CAAC;QACD,QAAQ,GAAG,IAAI,CAAA;IACjB,CAAC;IAED,OAAO,QAAQ,CAAA;AACjB,CAAC"}
package/dist/send.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ import { type ResourceKey } from "@kronos-ts/common";
2
+ import type { CommandBus } from "./command-bus.js";
3
+ import type { CommandDescriptor } from "./descriptor.js";
4
+ import type { z } from "zod";
5
+ type CommandDispatchFunction = <P extends z.ZodType, R extends z.ZodType | undefined = undefined>(descriptor: CommandDescriptor<P, R>, payload: z.infer<P>) => Promise<unknown>;
6
+ /**
7
+ * Resource key for the command bus component.
8
+ * Written by handling modules + processors at handler-invocation entry (D-44).
9
+ */
10
+ export declare const COMMAND_BUS_KEY: ResourceKey<CommandBus>;
11
+ /**
12
+ * Send a command from inside a handler.
13
+ *
14
+ * AF5-aligned semantics: every command is handled in its own fresh
15
+ * UnitOfWork (`commandBus.dispatch` always starts a new one — see
16
+ * `createSimpleCommandBus`). The command handler is therefore its own
17
+ * atomic boundary: it loads state, decides, appends events, and commits
18
+ * once — independent of the caller's UnitOfWork.
19
+ *
20
+ * The caller's `metadata` IS carried onto the outgoing command, so
21
+ * correlation/causation lineage propagates the AF5 way — through message
22
+ * metadata, applied by the correlation-data dispatch interceptor — across
23
+ * any transport, local or distributed. No processing-context object is
24
+ * threaded through the command API or over the wire.
25
+ */
26
+ export declare const send: CommandDispatchFunction;
27
+ export {};
28
+ //# sourceMappingURL=send.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send.d.ts","sourceRoot":"","sources":["../src/send.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmC,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAErF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAClD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AACxD,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAE5B,KAAK,uBAAuB,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,CAAC,SAAS,CAAC,CAAC,OAAO,GAAG,SAAS,GAAG,SAAS,EAC9F,UAAU,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,EACnC,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAChB,OAAO,CAAC,OAAO,CAAC,CAAA;AAErB;;;GAGG;AACH,eAAO,MAAM,eAAe,EAAE,WAAW,CAAC,UAAU,CAA6B,CAAA;AAEjF;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,IAAI,EAAE,uBAWlB,CAAA"}
package/dist/send.js ADDED
@@ -0,0 +1,36 @@
1
+ import { resourceKey, generateIdentifier } from "@kronos-ts/common";
2
+ import { requireInvocationPhase } from "./processing-state.js";
3
+ /**
4
+ * Resource key for the command bus component.
5
+ * Written by handling modules + processors at handler-invocation entry (D-44).
6
+ */
7
+ export const COMMAND_BUS_KEY = resourceKey("commandBus");
8
+ /**
9
+ * Send a command from inside a handler.
10
+ *
11
+ * AF5-aligned semantics: every command is handled in its own fresh
12
+ * UnitOfWork (`commandBus.dispatch` always starts a new one — see
13
+ * `createSimpleCommandBus`). The command handler is therefore its own
14
+ * atomic boundary: it loads state, decides, appends events, and commits
15
+ * once — independent of the caller's UnitOfWork.
16
+ *
17
+ * The caller's `metadata` IS carried onto the outgoing command, so
18
+ * correlation/causation lineage propagates the AF5 way — through message
19
+ * metadata, applied by the correlation-data dispatch interceptor — across
20
+ * any transport, local or distributed. No processing-context object is
21
+ * threaded through the command API or over the wire.
22
+ */
23
+ export const send = async (descriptor, payload) => {
24
+ const state = requireInvocationPhase(); // D-43 mutator guard
25
+ const bus = state.resources.get(COMMAND_BUS_KEY.symbol);
26
+ if (!bus)
27
+ throw new Error("No command bus configured");
28
+ return bus.dispatch({
29
+ identifier: generateIdentifier(),
30
+ name: descriptor.name,
31
+ payload,
32
+ metadata: state.metadata,
33
+ timestamp: Date.now(),
34
+ });
35
+ };
36
+ //# sourceMappingURL=send.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send.js","sourceRoot":"","sources":["../src/send.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAoB,MAAM,mBAAmB,CAAA;AACrF,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAA;AAU9D;;;GAGG;AACH,MAAM,CAAC,MAAM,eAAe,GAA4B,WAAW,CAAC,YAAY,CAAC,CAAA;AAEjF;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,IAAI,GAA4B,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,EAAE;IACzE,MAAM,KAAK,GAAG,sBAAsB,EAAE,CAAA,CAAC,qBAAqB;IAC5D,MAAM,GAAG,GAAG,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,eAAe,CAAC,MAAM,CAA2B,CAAA;IACjF,IAAI,CAAC,GAAG;QAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAA;IACtD,OAAO,GAAG,CAAC,QAAQ,CAAC;QAClB,UAAU,EAAE,kBAAkB,EAAE;QAChC,IAAI,EAAE,UAAU,CAAC,IAAI;QACrB,OAAO;QACP,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;KACtB,CAAC,CAAA;AACJ,CAAC,CAAA"}
@@ -0,0 +1,40 @@
1
+ import type { Serializer } from "@kronos-ts/common";
2
+ import type { z } from "zod";
3
+ /**
4
+ * JSON serializer — the default serializer for the TypeScript framework.
5
+ *
6
+ * Serializes values as JSON-encoded Uint8Array. Handles `undefined`,
7
+ * `null`, and all JSON-compatible values.
8
+ */
9
+ export declare function jsonSerializer(): Serializer;
10
+ /**
11
+ * A registry of Zod schemas indexed by type name + revision.
12
+ * Used by the validating serializer decorator to validate
13
+ * deserialized payloads against their expected schema.
14
+ */
15
+ export interface SchemaRegistry {
16
+ register(typeName: string, revision: string, schema: z.ZodType): void;
17
+ get(typeName: string, revision: string): z.ZodType | undefined;
18
+ }
19
+ /** Schema registry for event payloads. */
20
+ export declare function createEventSchemaRegistry(): SchemaRegistry;
21
+ /** Schema registry for command payloads. */
22
+ export declare function createCommandSchemaRegistry(): SchemaRegistry;
23
+ /** Schema registry for query payloads. */
24
+ export declare function createQuerySchemaRegistry(): SchemaRegistry;
25
+ /**
26
+ * Wraps a delegate serializer with Zod validation on deserialization.
27
+ *
28
+ * When deserializing, looks up the schema in the registry by type name
29
+ * and revision. If found, validates the deserialized value against it.
30
+ * If not found, passes through without validation.
31
+ *
32
+ * ```typescript
33
+ * const serializer = zodValidatingSerializer(
34
+ * jsonSerializer(),
35
+ * mySchemaRegistry,
36
+ * )
37
+ * ```
38
+ */
39
+ export declare function zodValidatingSerializer(delegate: Serializer, schemaRegistry: SchemaRegistry): Serializer;
40
+ //# sourceMappingURL=serializer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serializer.d.ts","sourceRoot":"","sources":["../src/serializer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAoB,MAAM,mBAAmB,CAAA;AACrE,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAK5B;;;;;GAKG;AACH,wBAAgB,cAAc,IAAI,UAAU,CAmB3C;AAMD;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,OAAO,GAAG,IAAI,CAAA;IACrE,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,CAAC,CAAC,OAAO,GAAG,SAAS,CAAA;CAC/D;AAED,0CAA0C;AAC1C,wBAAgB,yBAAyB,IAAI,cAAc,CAE1D;AAED,4CAA4C;AAC5C,wBAAgB,2BAA2B,IAAI,cAAc,CAE5D;AAED,0CAA0C;AAC1C,wBAAgB,yBAAyB,IAAI,cAAc,CAE1D;AAyBD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,UAAU,EACpB,cAAc,EAAE,cAAc,GAC7B,UAAU,CAmBZ"}
@@ -0,0 +1,90 @@
1
+ const encoder = new TextEncoder();
2
+ const decoder = new TextDecoder();
3
+ /**
4
+ * JSON serializer — the default serializer for the TypeScript framework.
5
+ *
6
+ * Serializes values as JSON-encoded Uint8Array. Handles `undefined`,
7
+ * `null`, and all JSON-compatible values.
8
+ */
9
+ export function jsonSerializer() {
10
+ return {
11
+ serialize(value, type, revision = "") {
12
+ return {
13
+ type,
14
+ revision,
15
+ data: encoder.encode(JSON.stringify(value)),
16
+ };
17
+ },
18
+ deserialize(data) {
19
+ if (data.data.length === 0)
20
+ return undefined;
21
+ return JSON.parse(decoder.decode(data.data));
22
+ },
23
+ canConvert() {
24
+ return true;
25
+ },
26
+ };
27
+ }
28
+ /** Schema registry for event payloads. */
29
+ export function createEventSchemaRegistry() {
30
+ return createSchemaRegistry();
31
+ }
32
+ /** Schema registry for command payloads. */
33
+ export function createCommandSchemaRegistry() {
34
+ return createSchemaRegistry();
35
+ }
36
+ /** Schema registry for query payloads. */
37
+ export function createQuerySchemaRegistry() {
38
+ return createSchemaRegistry();
39
+ }
40
+ function createSchemaRegistry() {
41
+ const schemas = new Map();
42
+ function key(typeName, revision) {
43
+ return `${typeName}@${revision}`;
44
+ }
45
+ return {
46
+ register(typeName, revision, schema) {
47
+ schemas.set(key(typeName, revision), schema);
48
+ },
49
+ get(typeName, revision) {
50
+ // Try exact match first, then fallback to no revision
51
+ return schemas.get(key(typeName, revision)) ?? schemas.get(key(typeName, ""));
52
+ },
53
+ };
54
+ }
55
+ // ---------------------------------------------------------------------------
56
+ // Zod-validating serializer decorator
57
+ // ---------------------------------------------------------------------------
58
+ /**
59
+ * Wraps a delegate serializer with Zod validation on deserialization.
60
+ *
61
+ * When deserializing, looks up the schema in the registry by type name
62
+ * and revision. If found, validates the deserialized value against it.
63
+ * If not found, passes through without validation.
64
+ *
65
+ * ```typescript
66
+ * const serializer = zodValidatingSerializer(
67
+ * jsonSerializer(),
68
+ * mySchemaRegistry,
69
+ * )
70
+ * ```
71
+ */
72
+ export function zodValidatingSerializer(delegate, schemaRegistry) {
73
+ return {
74
+ serialize(value, type, revision) {
75
+ return delegate.serialize(value, type, revision);
76
+ },
77
+ deserialize(data) {
78
+ const raw = delegate.deserialize(data);
79
+ const schema = schemaRegistry.get(data.type, data.revision);
80
+ if (schema) {
81
+ return schema.parse(raw);
82
+ }
83
+ return raw;
84
+ },
85
+ canConvert(type) {
86
+ return delegate.canConvert(type);
87
+ },
88
+ };
89
+ }
90
+ //# sourceMappingURL=serializer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serializer.js","sourceRoot":"","sources":["../src/serializer.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;AACjC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;AAEjC;;;;;GAKG;AACH,MAAM,UAAU,cAAc;IAC5B,OAAO;QACL,SAAS,CAAC,KAAc,EAAE,IAAY,EAAE,WAAmB,EAAE;YAC3D,OAAO;gBACL,IAAI;gBACJ,QAAQ;gBACR,IAAI,EAAE,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;aAC5C,CAAA;QACH,CAAC;QAED,WAAW,CAAI,IAAsB;YACnC,IAAI,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO,SAAc,CAAA;YACjD,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAM,CAAA;QACnD,CAAC;QAED,UAAU;YACR,OAAO,IAAI,CAAA;QACb,CAAC;KACF,CAAA;AACH,CAAC;AAgBD,0CAA0C;AAC1C,MAAM,UAAU,yBAAyB;IACvC,OAAO,oBAAoB,EAAE,CAAA;AAC/B,CAAC;AAED,4CAA4C;AAC5C,MAAM,UAAU,2BAA2B;IACzC,OAAO,oBAAoB,EAAE,CAAA;AAC/B,CAAC;AAED,0CAA0C;AAC1C,MAAM,UAAU,yBAAyB;IACvC,OAAO,oBAAoB,EAAE,CAAA;AAC/B,CAAC;AAED,SAAS,oBAAoB;IAC3B,MAAM,OAAO,GAAG,IAAI,GAAG,EAAqB,CAAA;IAE5C,SAAS,GAAG,CAAC,QAAgB,EAAE,QAAgB;QAC7C,OAAO,GAAG,QAAQ,IAAI,QAAQ,EAAE,CAAA;IAClC,CAAC;IAED,OAAO;QACL,QAAQ,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM;YACjC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,CAAA;QAC9C,CAAC;QAED,GAAG,CAAC,QAAQ,EAAE,QAAQ;YACpB,sDAAsD;YACtD,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAA;QAC/E,CAAC;KACF,CAAA;AACH,CAAC;AAED,8EAA8E;AAC9E,sCAAsC;AACtC,8EAA8E;AAE9E;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,uBAAuB,CACrC,QAAoB,EACpB,cAA8B;IAE9B,OAAO;QACL,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,QAAQ;YAC7B,OAAO,QAAQ,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAA;QAClD,CAAC;QAED,WAAW,CAAI,IAAsB;YACnC,MAAM,GAAG,GAAG,QAAQ,CAAC,WAAW,CAAU,IAAI,CAAC,CAAA;YAC/C,MAAM,MAAM,GAAG,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;YAC3D,IAAI,MAAM,EAAE,CAAC;gBACX,OAAO,MAAM,CAAC,KAAK,CAAC,GAAG,CAAM,CAAA;YAC/B,CAAC;YACD,OAAO,GAAQ,CAAA;QACjB,CAAC;QAED,UAAU,CAAC,IAAI;YACb,OAAO,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;QAClC,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -0,0 +1,23 @@
1
+ import type { CommandBus } from "./command-bus.js";
2
+ /**
3
+ * Simple in-process command bus.
4
+ *
5
+ * Maintains a local handler map and dispatches commands directly,
6
+ * wrapping each dispatch in a fresh UnitOfWork via `runInNewUoW`.
7
+ *
8
+ * AF5 parity: like `SimpleCommandBus`, every command — primary OR nested
9
+ * (dispatched from inside another handler via `send()`) — is handled in
10
+ * its own independent UnitOfWork with its own commit boundary. A command
11
+ * handler is the atomic unit; commands compose by independent commit, not
12
+ * by sharing a transaction. DCB read-set / append-condition merging
13
+ * happens only WITHIN a single handler's UnitOfWork.
14
+ *
15
+ * Transactional wiring composes at the runner level via
16
+ * `transactionalUnitOfWorkFactory(runInNewUoW, txManager)` and is consumed
17
+ * by extensions / processors directly, not by the bus.
18
+ *
19
+ * Interceptor support is provided by wrapping with
20
+ * {@link createInterceptingCommandBus}.
21
+ */
22
+ export declare function createSimpleCommandBus(): CommandBus;
23
+ //# sourceMappingURL=simple-command-bus.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"simple-command-bus.d.ts","sourceRoot":"","sources":["../src/simple-command-bus.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAKlD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,sBAAsB,IAAI,UAAU,CAiCnD"}
@@ -0,0 +1,49 @@
1
+ import { runInNewUoW } from "./unit-of-work.js";
2
+ import { qualifiedNameToString } from "@kronos-ts/common";
3
+ /**
4
+ * Simple in-process command bus.
5
+ *
6
+ * Maintains a local handler map and dispatches commands directly,
7
+ * wrapping each dispatch in a fresh UnitOfWork via `runInNewUoW`.
8
+ *
9
+ * AF5 parity: like `SimpleCommandBus`, every command — primary OR nested
10
+ * (dispatched from inside another handler via `send()`) — is handled in
11
+ * its own independent UnitOfWork with its own commit boundary. A command
12
+ * handler is the atomic unit; commands compose by independent commit, not
13
+ * by sharing a transaction. DCB read-set / append-condition merging
14
+ * happens only WITHIN a single handler's UnitOfWork.
15
+ *
16
+ * Transactional wiring composes at the runner level via
17
+ * `transactionalUnitOfWorkFactory(runInNewUoW, txManager)` and is consumed
18
+ * by extensions / processors directly, not by the bus.
19
+ *
20
+ * Interceptor support is provided by wrapping with
21
+ * {@link createInterceptingCommandBus}.
22
+ */
23
+ export function createSimpleCommandBus() {
24
+ const handlers = new Map();
25
+ return {
26
+ async dispatch(message) {
27
+ const key = qualifiedNameToString(message.name);
28
+ const handler = handlers.get(key);
29
+ if (!handler) {
30
+ throw new Error(`No handler registered for command "${key}"`);
31
+ }
32
+ // AF5 parity: every command gets its own fresh UnitOfWork, even when
33
+ // dispatched from inside another handler. Dispatch interceptors have
34
+ // already run in the caller's context (the intercepting bus wraps
35
+ // this one), so correlation data is carried on `message.metadata`
36
+ // before we cross into the new UoW.
37
+ return runInNewUoW(message.metadata, () => handler(message));
38
+ },
39
+ subscribe(commandName, handler) {
40
+ const existing = handlers.get(commandName);
41
+ if (existing && existing !== handler) {
42
+ throw new Error(`A different handler is already registered for command "${commandName}". ` +
43
+ `Duplicate command handler subscriptions are not allowed.`);
44
+ }
45
+ handlers.set(commandName, handler);
46
+ },
47
+ };
48
+ }
49
+ //# sourceMappingURL=simple-command-bus.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"simple-command-bus.js","sourceRoot":"","sources":["../src/simple-command-bus.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAA;AAEzD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,sBAAsB;IACpC,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAyD,CAAA;IAEjF,OAAO;QACL,KAAK,CAAC,QAAQ,CAAC,OAAuB;YACpC,MAAM,GAAG,GAAG,qBAAqB,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;YAC/C,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;YACjC,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,MAAM,IAAI,KAAK,CAAC,sCAAsC,GAAG,GAAG,CAAC,CAAA;YAC/D,CAAC;YAED,qEAAqE;YACrE,qEAAqE;YACrE,kEAAkE;YAClE,kEAAkE;YAClE,oCAAoC;YACpC,OAAO,WAAW,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAA;QAC9D,CAAC;QAED,SAAS,CACP,WAAmB,EACnB,OAAsD;YAEtD,MAAM,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,CAAA;YAC1C,IAAI,QAAQ,IAAI,QAAQ,KAAK,OAAO,EAAE,CAAC;gBACrC,MAAM,IAAI,KAAK,CACb,0DAA0D,WAAW,KAAK;oBAC1E,0DAA0D,CAC3D,CAAA;YACH,CAAC;YACD,QAAQ,CAAC,GAAG,CAAC,WAAW,EAAE,OAAO,CAAC,CAAA;QACpC,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { QueryBus } from "./query-bus.js";
2
+ /**
3
+ * Simple in-process query bus with subscription query support.
4
+ *
5
+ * Direct queries are dispatched within a UnitOfWork via `runInUoW`.
6
+ * Subscription queries receive an initial result plus a stream of
7
+ * incremental updates emitted via `emitUpdate()`.
8
+ *
9
+ * Plan 03-04 (CTX-04 / D-34): the explicit `unitOfWorkFactory`
10
+ * parameter and branch are gone. `runInUoW` is the only codepath.
11
+ *
12
+ * Interceptor support is provided by wrapping with
13
+ * {@link createInterceptingQueryBus}.
14
+ */
15
+ export declare function createSimpleQueryBus(): QueryBus;
16
+ //# sourceMappingURL=simple-query-bus.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"simple-query-bus.d.ts","sourceRoot":"","sources":["../src/simple-query-bus.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAO9C;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,IAAI,QAAQ,CAyI/C"}