@graphrefly/ts 0.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 (210) hide show
  1. package/LICENSE +21 -0
  2. package/dist/adapters/index.cjs +9015 -0
  3. package/dist/adapters/index.cjs.map +1 -0
  4. package/dist/adapters/index.d.cts +678 -0
  5. package/dist/adapters/index.d.ts +678 -0
  6. package/dist/adapters/index.js +2894 -0
  7. package/dist/adapters/index.js.map +1 -0
  8. package/dist/adapters/observe-storage.cjs +260 -0
  9. package/dist/adapters/observe-storage.cjs.map +1 -0
  10. package/dist/adapters/observe-storage.d.cts +77 -0
  11. package/dist/adapters/observe-storage.d.ts +77 -0
  12. package/dist/adapters/observe-storage.js +161 -0
  13. package/dist/adapters/observe-storage.js.map +1 -0
  14. package/dist/append-log-DisvEReb.d.cts +189 -0
  15. package/dist/append-log-DzKMmjxx.d.ts +189 -0
  16. package/dist/chunk-2ILL7HQI.js +1189 -0
  17. package/dist/chunk-2ILL7HQI.js.map +1 -0
  18. package/dist/chunk-4VLAQHZI.js +330 -0
  19. package/dist/chunk-4VLAQHZI.js.map +1 -0
  20. package/dist/chunk-6PHB5CJS.js +229 -0
  21. package/dist/chunk-6PHB5CJS.js.map +1 -0
  22. package/dist/chunk-COHO25CZ.js +2449 -0
  23. package/dist/chunk-COHO25CZ.js.map +1 -0
  24. package/dist/chunk-D6BG7U6A.js +161 -0
  25. package/dist/chunk-D6BG7U6A.js.map +1 -0
  26. package/dist/chunk-DJC4CHLQ.js +818 -0
  27. package/dist/chunk-DJC4CHLQ.js.map +1 -0
  28. package/dist/chunk-DK554R7P.js +67 -0
  29. package/dist/chunk-DK554R7P.js.map +1 -0
  30. package/dist/chunk-DKP56CSW.js +322 -0
  31. package/dist/chunk-DKP56CSW.js.map +1 -0
  32. package/dist/chunk-G4M3NHRJ.js +688 -0
  33. package/dist/chunk-G4M3NHRJ.js.map +1 -0
  34. package/dist/chunk-GAJW5I5P.js +1 -0
  35. package/dist/chunk-GAJW5I5P.js.map +1 -0
  36. package/dist/chunk-GDL26N2T.js +282 -0
  37. package/dist/chunk-GDL26N2T.js.map +1 -0
  38. package/dist/chunk-GTPCOEOZ.js +741 -0
  39. package/dist/chunk-GTPCOEOZ.js.map +1 -0
  40. package/dist/chunk-H2HTHFVN.js +1 -0
  41. package/dist/chunk-H2HTHFVN.js.map +1 -0
  42. package/dist/chunk-HDRL4VQQ.js +218 -0
  43. package/dist/chunk-HDRL4VQQ.js.map +1 -0
  44. package/dist/chunk-I3IHOHCJ.js +2267 -0
  45. package/dist/chunk-I3IHOHCJ.js.map +1 -0
  46. package/dist/chunk-JIMUDQN3.js +965 -0
  47. package/dist/chunk-JIMUDQN3.js.map +1 -0
  48. package/dist/chunk-JR4IKCR2.js +462 -0
  49. package/dist/chunk-JR4IKCR2.js.map +1 -0
  50. package/dist/chunk-K3FXTC3I.js +165 -0
  51. package/dist/chunk-K3FXTC3I.js.map +1 -0
  52. package/dist/chunk-OOEN33FB.js +65 -0
  53. package/dist/chunk-OOEN33FB.js.map +1 -0
  54. package/dist/chunk-PKVBHGBM.js +554 -0
  55. package/dist/chunk-PKVBHGBM.js.map +1 -0
  56. package/dist/chunk-RIIOUTGM.js +1857 -0
  57. package/dist/chunk-RIIOUTGM.js.map +1 -0
  58. package/dist/chunk-SCRFRCGR.js +33 -0
  59. package/dist/chunk-SCRFRCGR.js.map +1 -0
  60. package/dist/chunk-SWTLEQL6.js +53 -0
  61. package/dist/chunk-SWTLEQL6.js.map +1 -0
  62. package/dist/chunk-SZAETNNA.js +1 -0
  63. package/dist/chunk-SZAETNNA.js.map +1 -0
  64. package/dist/chunk-TEW47IVR.js +49 -0
  65. package/dist/chunk-TEW47IVR.js.map +1 -0
  66. package/dist/chunk-Y3ZLRGHC.js +1482 -0
  67. package/dist/chunk-Y3ZLRGHC.js.map +1 -0
  68. package/dist/chunk-Z2ANQ6JW.js +149 -0
  69. package/dist/chunk-Z2ANQ6JW.js.map +1 -0
  70. package/dist/codec-BB7p8fJc.d.cts +38 -0
  71. package/dist/codec-BB7p8fJc.d.ts +38 -0
  72. package/dist/composition/index.cjs +2505 -0
  73. package/dist/composition/index.cjs.map +1 -0
  74. package/dist/composition/index.d.cts +95 -0
  75. package/dist/composition/index.d.ts +95 -0
  76. package/dist/composition/index.js +19 -0
  77. package/dist/composition/index.js.map +1 -0
  78. package/dist/core/index.cjs +2475 -0
  79. package/dist/core/index.cjs.map +1 -0
  80. package/dist/core/index.d.cts +25 -0
  81. package/dist/core/index.d.ts +25 -0
  82. package/dist/core/index.js +74 -0
  83. package/dist/core/index.js.map +1 -0
  84. package/dist/cqrs/index.cjs +595 -0
  85. package/dist/cqrs/index.cjs.map +1 -0
  86. package/dist/cqrs/index.d.cts +200 -0
  87. package/dist/cqrs/index.d.ts +200 -0
  88. package/dist/cqrs/index.js +557 -0
  89. package/dist/cqrs/index.js.map +1 -0
  90. package/dist/data-structures/index.cjs +4277 -0
  91. package/dist/data-structures/index.cjs.map +1 -0
  92. package/dist/data-structures/index.d.cts +22 -0
  93. package/dist/data-structures/index.d.ts +22 -0
  94. package/dist/data-structures/index.js +31 -0
  95. package/dist/data-structures/index.js.map +1 -0
  96. package/dist/describe-BZb7AS2y.d.ts +57 -0
  97. package/dist/describe-Ciu8SUnN.d.cts +57 -0
  98. package/dist/graph/index.cjs +7832 -0
  99. package/dist/graph/index.cjs.map +1 -0
  100. package/dist/graph/index.d.cts +251 -0
  101. package/dist/graph/index.d.ts +251 -0
  102. package/dist/graph/index.js +131 -0
  103. package/dist/graph/index.js.map +1 -0
  104. package/dist/graph-DfdF9zy4.d.ts +666 -0
  105. package/dist/graph-s23RbbFO.d.cts +666 -0
  106. package/dist/index-CAEmLCYF.d.cts +166 -0
  107. package/dist/index-SnzZu3x7.d.ts +166 -0
  108. package/dist/index.cjs +15409 -0
  109. package/dist/index.cjs.map +1 -0
  110. package/dist/index.d.cts +23 -0
  111. package/dist/index.d.ts +23 -0
  112. package/dist/index.js +582 -0
  113. package/dist/index.js.map +1 -0
  114. package/dist/messages-BZqrGGm1.d.cts +55 -0
  115. package/dist/messages-BZqrGGm1.d.ts +55 -0
  116. package/dist/messaging/index.cjs +818 -0
  117. package/dist/messaging/index.cjs.map +1 -0
  118. package/dist/messaging/index.d.cts +235 -0
  119. package/dist/messaging/index.d.ts +235 -0
  120. package/dist/messaging/index.js +767 -0
  121. package/dist/messaging/index.js.map +1 -0
  122. package/dist/observe-event-log-0SVsx7JG.d.cts +88 -0
  123. package/dist/observe-event-log-Dnm76owp.d.ts +88 -0
  124. package/dist/operators/index.cjs +3774 -0
  125. package/dist/operators/index.cjs.map +1 -0
  126. package/dist/operators/index.d.cts +308 -0
  127. package/dist/operators/index.d.ts +308 -0
  128. package/dist/operators/index.js +112 -0
  129. package/dist/operators/index.js.map +1 -0
  130. package/dist/orchestration/index.cjs +3541 -0
  131. package/dist/orchestration/index.cjs.map +1 -0
  132. package/dist/orchestration/index.d.cts +302 -0
  133. package/dist/orchestration/index.d.ts +302 -0
  134. package/dist/orchestration/index.js +1004 -0
  135. package/dist/orchestration/index.js.map +1 -0
  136. package/dist/patterns/index.cjs +1045 -0
  137. package/dist/patterns/index.cjs.map +1 -0
  138. package/dist/patterns/index.d.cts +39 -0
  139. package/dist/patterns/index.d.ts +39 -0
  140. package/dist/patterns/index.js +33 -0
  141. package/dist/patterns/index.js.map +1 -0
  142. package/dist/reactive-collection-BxBqtIOE.d.cts +344 -0
  143. package/dist/reactive-collection-Cb1II1Fi.d.ts +344 -0
  144. package/dist/reactive-collection-storage-BNiEc-Gd.d.ts +217 -0
  145. package/dist/reactive-collection-storage-D-KmjE0J.d.cts +217 -0
  146. package/dist/reactive-map-BcB4KsQR.d.ts +256 -0
  147. package/dist/reactive-map-D-g3dv8I.d.cts +256 -0
  148. package/dist/read-through-B1CYPfq_.d.cts +82 -0
  149. package/dist/read-through-DwyJhGs1.d.ts +82 -0
  150. package/dist/render/index.cjs +250 -0
  151. package/dist/render/index.cjs.map +1 -0
  152. package/dist/render/index.d.cts +58 -0
  153. package/dist/render/index.d.ts +58 -0
  154. package/dist/render/index.js +20 -0
  155. package/dist/render/index.js.map +1 -0
  156. package/dist/resilience-Dig3Q7n0.d.cts +45 -0
  157. package/dist/resilience-Dig3Q7n0.d.ts +45 -0
  158. package/dist/semantic-memory-graph-BYoIlP35.d.cts +356 -0
  159. package/dist/semantic-memory-graph-CxyXLms3.d.ts +356 -0
  160. package/dist/solutions/index.cjs +3249 -0
  161. package/dist/solutions/index.cjs.map +1 -0
  162. package/dist/solutions/index.d.cts +520 -0
  163. package/dist/solutions/index.d.ts +520 -0
  164. package/dist/solutions/index.js +31 -0
  165. package/dist/solutions/index.js.map +1 -0
  166. package/dist/sources/browser.cjs +3095 -0
  167. package/dist/sources/browser.cjs.map +1 -0
  168. package/dist/sources/browser.d.cts +39 -0
  169. package/dist/sources/browser.d.ts +39 -0
  170. package/dist/sources/browser.js +137 -0
  171. package/dist/sources/browser.js.map +1 -0
  172. package/dist/sources/index.cjs +3021 -0
  173. package/dist/sources/index.cjs.map +1 -0
  174. package/dist/sources/index.d.cts +7 -0
  175. package/dist/sources/index.d.ts +7 -0
  176. package/dist/sources/index.js +63 -0
  177. package/dist/sources/index.js.map +1 -0
  178. package/dist/sources/node.cjs +675 -0
  179. package/dist/sources/node.cjs.map +1 -0
  180. package/dist/sources/node.d.cts +109 -0
  181. package/dist/sources/node.d.ts +109 -0
  182. package/dist/sources/node.js +639 -0
  183. package/dist/sources/node.js.map +1 -0
  184. package/dist/storage/browser.cjs +777 -0
  185. package/dist/storage/browser.cjs.map +1 -0
  186. package/dist/storage/browser.d.cts +40 -0
  187. package/dist/storage/browser.d.ts +40 -0
  188. package/dist/storage/browser.js +275 -0
  189. package/dist/storage/browser.js.map +1 -0
  190. package/dist/storage/index.cjs +1989 -0
  191. package/dist/storage/index.cjs.map +1 -0
  192. package/dist/storage/index.d.cts +92 -0
  193. package/dist/storage/index.d.ts +92 -0
  194. package/dist/storage/index.js +161 -0
  195. package/dist/storage/index.js.map +1 -0
  196. package/dist/storage/node.cjs +898 -0
  197. package/dist/storage/node.cjs.map +1 -0
  198. package/dist/storage/node.d.cts +66 -0
  199. package/dist/storage/node.d.ts +66 -0
  200. package/dist/storage/node.js +402 -0
  201. package/dist/storage/node.js.map +1 -0
  202. package/dist/testing/index.cjs +95 -0
  203. package/dist/testing/index.cjs.map +1 -0
  204. package/dist/testing/index.d.cts +20 -0
  205. package/dist/testing/index.d.ts +20 -0
  206. package/dist/testing/index.js +8 -0
  207. package/dist/testing/index.js.map +1 -0
  208. package/dist/types-bNMSWKqF.d.cts +732 -0
  209. package/dist/types-h12mNYbq.d.ts +732 -0
  210. package/package.json +250 -0
@@ -0,0 +1,74 @@
1
+ import {
2
+ Dispatcher,
3
+ Node,
4
+ batch,
5
+ defaultDispatcher,
6
+ defaultNodeVersionHash,
7
+ dynamicNode,
8
+ node
9
+ } from "../chunk-I3IHOHCJ.js";
10
+ import "../chunk-4VLAQHZI.js";
11
+ import {
12
+ depBatch,
13
+ depCount,
14
+ depLatest,
15
+ depTerminal,
16
+ depWaves,
17
+ isTerminalComplete,
18
+ isTerminalError,
19
+ isTerminalNone,
20
+ terminalErrorValue
21
+ } from "../chunk-SWTLEQL6.js";
22
+ import {
23
+ SENTINEL,
24
+ TIER_CONTROL,
25
+ TIER_NOTIFICATION,
26
+ TIER_SETTLE,
27
+ TIER_START,
28
+ TIER_TEARDOWN,
29
+ TIER_TERMINAL,
30
+ TIER_VALUE,
31
+ errorPayload,
32
+ isDeferredTier,
33
+ isInvalidErrorPayload,
34
+ isPauseBufferedTier,
35
+ isTerminal,
36
+ isUpAllowed,
37
+ isValueTier,
38
+ messageTier
39
+ } from "../chunk-DK554R7P.js";
40
+ export {
41
+ Dispatcher,
42
+ Node,
43
+ SENTINEL,
44
+ TIER_CONTROL,
45
+ TIER_NOTIFICATION,
46
+ TIER_SETTLE,
47
+ TIER_START,
48
+ TIER_TEARDOWN,
49
+ TIER_TERMINAL,
50
+ TIER_VALUE,
51
+ batch,
52
+ defaultDispatcher,
53
+ defaultNodeVersionHash,
54
+ depBatch,
55
+ depCount,
56
+ depLatest,
57
+ depTerminal,
58
+ depWaves,
59
+ dynamicNode,
60
+ errorPayload,
61
+ isDeferredTier,
62
+ isInvalidErrorPayload,
63
+ isPauseBufferedTier,
64
+ isTerminal,
65
+ isTerminalComplete,
66
+ isTerminalError,
67
+ isTerminalNone,
68
+ isUpAllowed,
69
+ isValueTier,
70
+ messageTier,
71
+ node,
72
+ terminalErrorValue
73
+ };
74
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,595 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/cqrs/index.ts
21
+ var cqrs_exports = {};
22
+ __export(cqrs_exports, {
23
+ cqrs: () => cqrs,
24
+ cqrsCommandHandler: () => cqrsCommandHandler,
25
+ cqrsProjection: () => cqrsProjection
26
+ });
27
+ module.exports = __toCommonJS(cqrs_exports);
28
+
29
+ // src/protocol/messages.ts
30
+ var SENTINEL = void 0;
31
+
32
+ // src/ctx/types.ts
33
+ function depWaves(ctx, depIndex) {
34
+ return ctx.waveData[depIndex] ?? [];
35
+ }
36
+ function depBatch(ctx, depIndex) {
37
+ const waves = depWaves(ctx, depIndex);
38
+ if (waves.length === 0) return null;
39
+ const flattened = waves.flat().filter((v) => v !== SENTINEL);
40
+ return flattened.length === 0 ? [] : flattened;
41
+ }
42
+
43
+ // src/cqrs/index.ts
44
+ function cqrsCommandHandler(type, handle) {
45
+ if (type.length === 0) throw new Error("cqrsCommandHandler: type must be non-empty");
46
+ return { type, handle };
47
+ }
48
+ function cqrs(graph, opts = {}) {
49
+ const name = opts.name ?? "cqrs";
50
+ const handlers = normalizeHandlers(opts.handlers ?? []);
51
+ const knownEvents = opts.events === void 0 ? void 0 : new Set(uniqueNames(opts.events, "cqrs.events"));
52
+ const now = opts.now ?? Date.now;
53
+ const dedupe = normalizeDedupePolicy(opts.dedupe);
54
+ const command = graph.node([], null, {
55
+ name: `${name}/command`,
56
+ factory: "cqrsCommand",
57
+ completeWhenDepsComplete: false,
58
+ errorWhenDepsError: false
59
+ });
60
+ const runtime = graph.node(
61
+ [command],
62
+ (ctx) => {
63
+ const state = ctx.state.get() ?? {
64
+ eventSeq: 0,
65
+ commandCount: 0,
66
+ errorCount: 0,
67
+ auditSeq: 0,
68
+ seenCommandIds: [],
69
+ seenEventIds: []
70
+ };
71
+ ctx.state.persist(true);
72
+ for (const raw of depBatch(ctx, 0) ?? []) {
73
+ reduceCommandFact(
74
+ raw,
75
+ state,
76
+ handlers,
77
+ knownEvents,
78
+ now,
79
+ dedupe,
80
+ (fact) => ctx.down([["DATA", fact]])
81
+ );
82
+ }
83
+ ctx.state.set(state);
84
+ },
85
+ {
86
+ name: `${name}/runtime`,
87
+ factory: "cqrsRuntime",
88
+ meta: {
89
+ commandTypes: [...handlers.keys()],
90
+ eventTypes: knownEvents ? [...knownEvents] : "open",
91
+ dedupe: dedupeMeta(dedupe)
92
+ },
93
+ completeWhenDepsComplete: false,
94
+ errorWhenDepsError: false
95
+ }
96
+ );
97
+ const events = runtimeProjection(
98
+ graph,
99
+ runtime,
100
+ "event",
101
+ `${name}/events`,
102
+ "cqrsEvents",
103
+ (fact) => fact.kind === "event" ? fact.event : void 0
104
+ );
105
+ const status = runtimeProjection(
106
+ graph,
107
+ runtime,
108
+ "status",
109
+ `${name}/status`,
110
+ "cqrsStatus",
111
+ (fact) => fact.kind === "status" ? fact.status : void 0
112
+ );
113
+ const errors = runtimeProjection(
114
+ graph,
115
+ runtime,
116
+ "error",
117
+ `${name}/errors`,
118
+ "cqrsErrors",
119
+ (fact) => fact.kind === "error" ? fact.error : void 0
120
+ );
121
+ const audit = runtimeProjection(
122
+ graph,
123
+ runtime,
124
+ "audit",
125
+ `${name}/audit`,
126
+ "cqrsAudit",
127
+ (fact) => fact.kind === "audit" ? fact.audit : void 0
128
+ );
129
+ const cursor = runtimeProjection(
130
+ graph,
131
+ runtime,
132
+ "cursor",
133
+ `${name}/cursor`,
134
+ "cqrsCursor",
135
+ (fact) => fact.kind === "cursor" ? fact.cursor : void 0
136
+ );
137
+ graph.retain(runtime, { reason: `${name}.cqrs.runtime` });
138
+ graph.retain(events, { reason: `${name}.cqrs.events` });
139
+ graph.retain(status, { reason: `${name}.cqrs.status` });
140
+ graph.retain(errors, { reason: `${name}.cqrs.errors` });
141
+ graph.retain(audit, { reason: `${name}.cqrs.audit` });
142
+ graph.retain(cursor, { reason: `${name}.cqrs.cursor` });
143
+ return {
144
+ command,
145
+ runtime,
146
+ events,
147
+ status,
148
+ errors,
149
+ audit,
150
+ cursor,
151
+ dispatch(commandFact) {
152
+ command.down([["DATA", commandFact]]);
153
+ return commandFact;
154
+ }
155
+ };
156
+ }
157
+ function cqrsProjection(graph, source, opts) {
158
+ const name = opts.name ?? "cqrsProjection";
159
+ const eventFilter = opts.events === void 0 ? void 0 : new Set(uniqueNames(opts.events, "cqrsProjection.events"));
160
+ const frames = graph.node(
161
+ [source.events],
162
+ (ctx) => {
163
+ const state = ctx.state.get() ?? { value: opts.initial };
164
+ ctx.state.persist(true);
165
+ for (const raw of depBatch(ctx, 0) ?? []) {
166
+ const event = raw;
167
+ if (eventFilter !== void 0 && !eventFilter.has(event.type)) continue;
168
+ try {
169
+ state.value = opts.reducer(state.value, event);
170
+ ctx.state.set(state);
171
+ ctx.down([
172
+ [
173
+ "DATA",
174
+ {
175
+ kind: "value",
176
+ state: state.value,
177
+ eventId: event.id,
178
+ cursor: event.runtimeCursor
179
+ }
180
+ ]
181
+ ]);
182
+ } catch (error) {
183
+ rethrowGraphRuntimeInvariant(error);
184
+ ctx.down([
185
+ [
186
+ "DATA",
187
+ {
188
+ kind: "error",
189
+ error: {
190
+ code: "projection-threw",
191
+ message: errorMessage(error),
192
+ eventId: event.id,
193
+ eventType: event.type,
194
+ cursor: event.runtimeCursor
195
+ }
196
+ }
197
+ ]
198
+ ]);
199
+ return;
200
+ }
201
+ }
202
+ },
203
+ {
204
+ name,
205
+ factory: "cqrsProjection",
206
+ meta: { eventTypes: eventFilter ? [...eventFilter] : "open" },
207
+ completeWhenDepsComplete: false,
208
+ errorWhenDepsError: false
209
+ }
210
+ );
211
+ const value = graph.node(
212
+ [frames],
213
+ (ctx) => {
214
+ for (const frame of depBatch(ctx, 0) ?? []) {
215
+ const typed = frame;
216
+ if (typed.kind === "value") ctx.down([["DATA", typed.state]]);
217
+ }
218
+ },
219
+ {
220
+ name: `${name}/value`,
221
+ factory: "cqrsProjectionValue",
222
+ completeWhenDepsComplete: false,
223
+ errorWhenDepsError: false
224
+ }
225
+ );
226
+ const errors = graph.node(
227
+ [frames],
228
+ (ctx) => {
229
+ for (const frame of depBatch(ctx, 0) ?? []) {
230
+ const typed = frame;
231
+ if (typed.kind === "error") ctx.down([["DATA", typed.error]]);
232
+ }
233
+ },
234
+ {
235
+ name: `${name}/errors`,
236
+ factory: "cqrsProjectionErrors",
237
+ completeWhenDepsComplete: false,
238
+ errorWhenDepsError: false
239
+ }
240
+ );
241
+ const status = graph.node(
242
+ [frames],
243
+ (ctx) => {
244
+ for (const frame of depBatch(ctx, 0) ?? []) {
245
+ const typed = frame;
246
+ if (typed.kind === "value") {
247
+ ctx.down([
248
+ [
249
+ "DATA",
250
+ {
251
+ state: "updated",
252
+ eventId: typed.eventId,
253
+ cursor: typed.cursor
254
+ }
255
+ ]
256
+ ]);
257
+ } else {
258
+ ctx.down([
259
+ [
260
+ "DATA",
261
+ {
262
+ state: "errored",
263
+ eventId: typed.error.eventId,
264
+ eventType: typed.error.eventType,
265
+ cursor: typed.error.cursor
266
+ }
267
+ ]
268
+ ]);
269
+ }
270
+ }
271
+ },
272
+ {
273
+ name: `${name}/status`,
274
+ factory: "cqrsProjectionStatus",
275
+ completeWhenDepsComplete: false,
276
+ errorWhenDepsError: false
277
+ }
278
+ );
279
+ graph.retain(frames, { reason: `${name}.cqrsProjection.frames` });
280
+ graph.retain(value, { reason: `${name}.cqrsProjection.value` });
281
+ graph.retain(status, { reason: `${name}.cqrsProjection.status` });
282
+ graph.retain(errors, { reason: `${name}.cqrsProjection.errors` });
283
+ return { frames, value, status, errors };
284
+ }
285
+ function normalizeHandlers(definitions) {
286
+ const handlers = /* @__PURE__ */ new Map();
287
+ for (const definition of definitions) {
288
+ if (definition.type.length === 0) throw new Error("cqrs: handler type must be non-empty");
289
+ if (handlers.has(definition.type))
290
+ throw new Error(`cqrs: duplicate handler '${definition.type}'`);
291
+ handlers.set(definition.type, {
292
+ type: definition.type,
293
+ handle: definition.handle
294
+ });
295
+ }
296
+ return handlers;
297
+ }
298
+ function reduceCommandFact(raw, state, handlers, knownEvents, now, dedupe, emit) {
299
+ state.commandCount += 1;
300
+ const parsed = parseCommand(raw);
301
+ if (typeof parsed === "string") {
302
+ emitFailure(state, emit, raw, void 0, "malformed-command", parsed, dedupe);
303
+ return;
304
+ }
305
+ const command = parsed;
306
+ if (state.seenCommandIds.includes(command.id)) {
307
+ emitFailure(
308
+ state,
309
+ emit,
310
+ command,
311
+ command,
312
+ "duplicate-command",
313
+ `cqrs: duplicate command '${command.id}'`,
314
+ dedupe
315
+ );
316
+ return;
317
+ }
318
+ state.seenCommandIds.push(command.id);
319
+ trimDedupeWindow(state.seenCommandIds, dedupe.commandMaxEntries, (evicted) => {
320
+ state.commandDedupeEvicted = (state.commandDedupeEvicted ?? 0) + evicted;
321
+ });
322
+ const handler = handlers.get(command.type);
323
+ if (handler === void 0) {
324
+ emitFailure(
325
+ state,
326
+ emit,
327
+ command,
328
+ command,
329
+ "unknown-command",
330
+ `cqrs: unknown command '${command.type}'`,
331
+ dedupe
332
+ );
333
+ return;
334
+ }
335
+ let drafts;
336
+ try {
337
+ drafts = handler.handle(command);
338
+ } catch (error) {
339
+ rethrowGraphRuntimeInvariant(error);
340
+ emitFailure(state, emit, command, command, "handler-threw", errorMessage(error), dedupe);
341
+ return;
342
+ }
343
+ if (!Array.isArray(drafts)) {
344
+ emitFailure(
345
+ state,
346
+ emit,
347
+ command,
348
+ command,
349
+ "malformed-event",
350
+ "cqrs: command handler must return an event draft array",
351
+ dedupe
352
+ );
353
+ return;
354
+ }
355
+ const prepared = prepareEvents(command, drafts, state, knownEvents);
356
+ if (typeof prepared === "string") {
357
+ emitFailure(state, emit, command, command, preparedCode(prepared), prepared, dedupe);
358
+ return;
359
+ }
360
+ const timestampMs = readTimestampMs(now);
361
+ if (typeof timestampMs === "string") {
362
+ emitFailure(state, emit, command, command, "clock-threw", timestampMs, dedupe);
363
+ return;
364
+ }
365
+ const events = [];
366
+ for (const draft of prepared) {
367
+ state.eventSeq += 1;
368
+ state.seenEventIds.push(draft.id);
369
+ trimDedupeWindow(state.seenEventIds, dedupe.eventMaxEntries, (evicted) => {
370
+ state.eventDedupeEvicted = (state.eventDedupeEvicted ?? 0) + evicted;
371
+ });
372
+ const runtimeCursor = cursorOf(state, dedupe);
373
+ events.push({
374
+ id: draft.id,
375
+ type: draft.type,
376
+ seq: state.eventSeq,
377
+ cursor: state.eventSeq,
378
+ runtimeCursor,
379
+ commandId: command.id,
380
+ commandType: command.type,
381
+ payload: draft.payload,
382
+ timestampMs,
383
+ ...draft.aggregateId === void 0 ? {} : { aggregateId: draft.aggregateId },
384
+ ...draft.correlationId === void 0 ? {} : { correlationId: draft.correlationId },
385
+ ...draft.causationId === void 0 ? {} : { causationId: draft.causationId },
386
+ ...draft.metadata === void 0 ? {} : { metadata: draft.metadata }
387
+ });
388
+ }
389
+ for (const event of events) emit({ kind: "event", event });
390
+ emitStatus(state, emit, command, "accepted", events.length, dedupe);
391
+ emitAudit(state, emit, command, "success", events, void 0, void 0, dedupe);
392
+ emit({ kind: "cursor", cursor: cursorOf(state, dedupe) });
393
+ }
394
+ function parseCommand(value) {
395
+ if (!isObjectRecord(value)) return "cqrs: command fact must be an object";
396
+ if (typeof value.id !== "string" || value.id.length === 0) {
397
+ return "cqrs: command id must be a non-empty string";
398
+ }
399
+ if (typeof value.type !== "string" || value.type.length === 0) {
400
+ return "cqrs: command type must be a non-empty string";
401
+ }
402
+ return {
403
+ id: value.id,
404
+ type: value.type,
405
+ payload: value.payload,
406
+ ...typeof value.aggregateId === "string" ? { aggregateId: value.aggregateId } : {},
407
+ ...typeof value.correlationId === "string" ? { correlationId: value.correlationId } : {},
408
+ ...typeof value.causationId === "string" ? { causationId: value.causationId } : {},
409
+ ...isObjectRecord(value.metadata) ? { metadata: value.metadata } : {}
410
+ };
411
+ }
412
+ function prepareEvents(command, drafts, state, knownEvents) {
413
+ const seenInCommand = /* @__PURE__ */ new Set();
414
+ const prepared = [];
415
+ for (let i = 0; i < drafts.length; i += 1) {
416
+ const draft = drafts[i];
417
+ if (!isObjectRecord(draft) || typeof draft.type !== "string" || draft.type.length === 0) {
418
+ return "cqrs: event draft must have a non-empty type";
419
+ }
420
+ if (knownEvents !== void 0 && !knownEvents.has(draft.type)) {
421
+ return `cqrs: unknown event '${draft.type}'`;
422
+ }
423
+ const id = typeof draft.id === "string" && draft.id.length > 0 ? draft.id : `${command.id}:${i + 1}`;
424
+ if (state.seenEventIds.includes(id) || seenInCommand.has(id)) {
425
+ return `cqrs: duplicate event '${id}'`;
426
+ }
427
+ seenInCommand.add(id);
428
+ prepared.push({
429
+ id,
430
+ type: draft.type,
431
+ payload: draft.payload,
432
+ ...typeof draft.aggregateId === "string" ? { aggregateId: draft.aggregateId } : {},
433
+ ...typeof draft.correlationId === "string" ? { correlationId: draft.correlationId } : {},
434
+ ...typeof draft.causationId === "string" ? { causationId: draft.causationId } : {},
435
+ ...isObjectRecord(draft.metadata) ? { metadata: draft.metadata } : {}
436
+ });
437
+ }
438
+ return prepared;
439
+ }
440
+ function preparedCode(message) {
441
+ if (message.includes("unknown event")) return "unknown-event";
442
+ if (message.includes("duplicate event")) return "duplicate-event";
443
+ return "malformed-event";
444
+ }
445
+ function rethrowGraphRuntimeInvariant(error) {
446
+ const message = errorMessage(error);
447
+ if (message.includes("R-reentrancy") || message.includes("R-rewire") || message.includes("R-graph-domain") || message.includes("D37") || message.includes("D22") || message.includes("different graph") || message.includes("cross-graph") || message.includes("wire bridge") || message.includes("mid-fn topology mutation") || message.includes("reentrant dep mutation") || message.includes("feedback cycle")) {
448
+ throw error;
449
+ }
450
+ }
451
+ function emitFailure(state, emit, raw, command, code, message, dedupe) {
452
+ state.errorCount += 1;
453
+ const cursor = cursorOf(state, dedupe);
454
+ const error = {
455
+ code,
456
+ message,
457
+ command: raw,
458
+ ...command === void 0 ? {} : { commandId: command.id, commandType: command.type },
459
+ cursor
460
+ };
461
+ emit({ kind: "error", error });
462
+ emit({
463
+ kind: "status",
464
+ status: {
465
+ state: "rejected",
466
+ ...command === void 0 ? {} : { commandId: command.id, commandType: command.type },
467
+ eventCount: 0,
468
+ errorCode: code,
469
+ cursor
470
+ }
471
+ });
472
+ emitAudit(state, emit, command, "failure", [], code, message, dedupe);
473
+ emit({ kind: "cursor", cursor: cursorOf(state, dedupe) });
474
+ }
475
+ function emitStatus(state, emit, command, statusState, eventCount, dedupe) {
476
+ emit({
477
+ kind: "status",
478
+ status: {
479
+ state: statusState,
480
+ commandId: command.id,
481
+ commandType: command.type,
482
+ eventCount,
483
+ cursor: cursorOf(state, dedupe)
484
+ }
485
+ });
486
+ }
487
+ function emitAudit(state, emit, command, outcome, events, errorCode, errorMessageValue, dedupe) {
488
+ state.auditSeq += 1;
489
+ emit({
490
+ kind: "audit",
491
+ audit: {
492
+ seq: state.auditSeq,
493
+ ...command === void 0 ? {} : { commandId: command.id, commandType: command.type },
494
+ outcome,
495
+ eventIds: Object.freeze(events.map((event) => event.id)),
496
+ eventTypes: Object.freeze(events.map((event) => event.type)),
497
+ ...errorCode === void 0 ? {} : { errorCode },
498
+ ...errorMessageValue === void 0 ? {} : { errorMessage: errorMessageValue },
499
+ cursor: cursorOf(state, dedupe)
500
+ }
501
+ });
502
+ }
503
+ function runtimeProjection(graph, runtime, kind, name, factory, select) {
504
+ return graph.node(
505
+ [runtime],
506
+ (ctx) => {
507
+ for (const fact of depBatch(ctx, 0) ?? []) {
508
+ const typed = fact;
509
+ if (typed.kind !== kind) continue;
510
+ const selected = select(typed);
511
+ if (selected !== void 0) ctx.down([["DATA", selected]]);
512
+ }
513
+ },
514
+ {
515
+ name,
516
+ factory,
517
+ completeWhenDepsComplete: false,
518
+ errorWhenDepsError: false
519
+ }
520
+ );
521
+ }
522
+ function cursorOf(state, dedupe) {
523
+ return {
524
+ eventSeq: state.eventSeq,
525
+ commandCount: state.commandCount,
526
+ errorCount: state.errorCount,
527
+ auditSeq: state.auditSeq,
528
+ ...dedupe.bounded ? {
529
+ dedupe: {
530
+ commandIdsRetained: state.seenCommandIds.length,
531
+ eventIdsRetained: state.seenEventIds.length,
532
+ commandIdsEvicted: state.commandDedupeEvicted ?? 0,
533
+ eventIdsEvicted: state.eventDedupeEvicted ?? 0
534
+ }
535
+ } : {}
536
+ };
537
+ }
538
+ function normalizeDedupePolicy(policy) {
539
+ const commandMaxEntries = normalizeDedupeWindow(policy?.commands, "cqrs.dedupe.commands");
540
+ const eventMaxEntries = normalizeDedupeWindow(policy?.events, "cqrs.dedupe.events");
541
+ return {
542
+ ...commandMaxEntries === void 0 ? {} : { commandMaxEntries },
543
+ ...eventMaxEntries === void 0 ? {} : { eventMaxEntries },
544
+ bounded: commandMaxEntries !== void 0 || eventMaxEntries !== void 0
545
+ };
546
+ }
547
+ function normalizeDedupeWindow(window, owner) {
548
+ if (window === void 0 || window === "unbounded") return void 0;
549
+ if (!Number.isInteger(window.maxEntries) || window.maxEntries < 0) {
550
+ throw new RangeError(`${owner}: maxEntries must be a non-negative integer`);
551
+ }
552
+ return window.maxEntries;
553
+ }
554
+ function dedupeMeta(dedupe) {
555
+ if (!dedupe.bounded) return "unbounded";
556
+ return {
557
+ commands: dedupe.commandMaxEntries === void 0 ? "unbounded" : { maxEntries: dedupe.commandMaxEntries },
558
+ events: dedupe.eventMaxEntries === void 0 ? "unbounded" : { maxEntries: dedupe.eventMaxEntries }
559
+ };
560
+ }
561
+ function trimDedupeWindow(ids, maxEntries, onEvicted) {
562
+ if (maxEntries === void 0 || ids.length <= maxEntries) return;
563
+ const evicted = ids.length - maxEntries;
564
+ ids.splice(0, evicted);
565
+ onEvicted(evicted);
566
+ }
567
+ function readTimestampMs(now) {
568
+ try {
569
+ const timestampMs = now();
570
+ return Number.isFinite(timestampMs) ? timestampMs : "cqrs: now() must return a finite number";
571
+ } catch (error) {
572
+ return `cqrs: now() threw: ${errorMessage(error)}`;
573
+ }
574
+ }
575
+ function uniqueNames(values, owner) {
576
+ const unique = [...new Set(values)];
577
+ if (unique.length !== values.length) throw new Error(`${owner}: duplicate value`);
578
+ for (const value of unique) {
579
+ if (value.length === 0) throw new Error(`${owner}: values must be non-empty`);
580
+ }
581
+ return Object.freeze(unique);
582
+ }
583
+ function errorMessage(error) {
584
+ return error instanceof Error ? error.message : String(error);
585
+ }
586
+ function isObjectRecord(value) {
587
+ return typeof value === "object" && value !== null && !Array.isArray(value);
588
+ }
589
+ // Annotate the CommonJS export names for ESM import in node:
590
+ 0 && (module.exports = {
591
+ cqrs,
592
+ cqrsCommandHandler,
593
+ cqrsProjection
594
+ });
595
+ //# sourceMappingURL=index.cjs.map