@event-driven-io/emmett 0.20.1-alpha.5 → 0.20.2-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-AEEEXE2R.js +130 -0
- package/dist/{chunk-6TXKRKDJ.js.map → chunk-AEEEXE2R.js.map} +1 -1
- package/dist/cli.cjs +3 -4
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.d.cts +1 -1
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +195 -11
- package/dist/cli.js.map +1 -1
- package/dist/{index-D9zse73H.d.cts → index-DKT3pluX.d.cts} +5 -4
- package/dist/{index-D9zse73H.d.ts → index-DKT3pluX.d.ts} +5 -4
- package/dist/index.cjs +0 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -4
- package/dist/index.d.ts +3 -4
- package/dist/index.js +1329 -13
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/dist/chunk-6TXKRKDJ.js +0 -2
package/dist/index.js
CHANGED
|
@@ -1,17 +1,1333 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import {
|
|
2
|
+
ConcurrencyError,
|
|
3
|
+
EmmettError,
|
|
4
|
+
IllegalStateError,
|
|
5
|
+
NotFoundError,
|
|
6
|
+
ValidationError,
|
|
7
|
+
ValidationErrors,
|
|
8
|
+
assertNotEmptyString,
|
|
9
|
+
assertPositiveNumber,
|
|
10
|
+
assertUnsignedBigInt,
|
|
11
|
+
formatDateToUtcYYYYMMDD,
|
|
12
|
+
isErrorConstructor,
|
|
13
|
+
isNumber,
|
|
14
|
+
isPluginConfig,
|
|
15
|
+
isString,
|
|
16
|
+
isValidYYYYMMDD,
|
|
17
|
+
parseDateFromUtcYYYYMMDD
|
|
18
|
+
} from "./chunk-AEEEXE2R.js";
|
|
19
|
+
|
|
20
|
+
// src/eventStore/eventStore.ts
|
|
21
|
+
var canCreateEventStoreSession = (eventStore) => "withSession" in eventStore;
|
|
22
|
+
var nulloSessionFactory = (eventStore) => ({
|
|
23
|
+
withSession: (callback) => {
|
|
24
|
+
const nulloSession = {
|
|
25
|
+
eventStore,
|
|
26
|
+
close: () => Promise.resolve()
|
|
27
|
+
};
|
|
28
|
+
return callback(nulloSession);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// src/typing/command.ts
|
|
33
|
+
var command = (type, data, metadata) => {
|
|
34
|
+
return {
|
|
35
|
+
type,
|
|
36
|
+
data,
|
|
37
|
+
metadata
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/typing/event.ts
|
|
42
|
+
var event = (type, data, metadata) => {
|
|
43
|
+
return {
|
|
44
|
+
type,
|
|
45
|
+
data,
|
|
46
|
+
metadata
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// src/typing/workflow.ts
|
|
51
|
+
var reply = (message) => {
|
|
52
|
+
return {
|
|
53
|
+
kind: "Reply",
|
|
54
|
+
message
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
var send = (message) => {
|
|
58
|
+
return {
|
|
59
|
+
kind: "Send",
|
|
60
|
+
message
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
var publish = (message) => {
|
|
64
|
+
return {
|
|
65
|
+
kind: "Publish",
|
|
66
|
+
message
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
var schedule = (message, when) => {
|
|
70
|
+
return {
|
|
71
|
+
kind: "Schedule",
|
|
72
|
+
message,
|
|
73
|
+
when
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
var complete = () => {
|
|
77
|
+
return {
|
|
78
|
+
kind: "Complete"
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
var ignore = (reason) => {
|
|
82
|
+
return {
|
|
83
|
+
kind: "Ignore",
|
|
84
|
+
reason
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
var error = (reason) => {
|
|
88
|
+
return {
|
|
89
|
+
kind: "Error",
|
|
90
|
+
reason
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
var accept = () => {
|
|
94
|
+
return { kind: "Accept" };
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// src/eventStore/events/index.ts
|
|
98
|
+
var GlobalStreamCaughtUpType = "__emt:GlobalStreamCaughtUp";
|
|
99
|
+
var isGlobalStreamCaughtUp = (event2) => event2.type === GlobalStreamCaughtUpType;
|
|
100
|
+
var caughtUpEventFrom = (position) => (event2) => event2.type === GlobalStreamCaughtUpType && event2.metadata?.globalPosition >= position;
|
|
101
|
+
var globalStreamCaughtUp = (data) => event(GlobalStreamCaughtUpType, data, {
|
|
102
|
+
globalPosition: data.globalPosition
|
|
103
|
+
});
|
|
104
|
+
var isSubscriptionEvent = (event2) => isGlobalStreamCaughtUp(event2);
|
|
105
|
+
var isNotInternalEvent = (event2) => !isGlobalStreamCaughtUp(event2);
|
|
106
|
+
|
|
107
|
+
// src/eventStore/expectedVersion.ts
|
|
108
|
+
var STREAM_EXISTS = "STREAM_EXISTS";
|
|
109
|
+
var STREAM_DOES_NOT_EXIST = "STREAM_DOES_NOT_EXIST";
|
|
110
|
+
var NO_CONCURRENCY_CHECK = "NO_CONCURRENCY_CHECK";
|
|
111
|
+
var matchesExpectedVersion = (current, expected, defaultVersion) => {
|
|
112
|
+
if (expected === NO_CONCURRENCY_CHECK) return true;
|
|
113
|
+
if (expected == STREAM_DOES_NOT_EXIST) return current === defaultVersion;
|
|
114
|
+
if (expected == STREAM_EXISTS) return current !== defaultVersion;
|
|
115
|
+
return current === expected;
|
|
116
|
+
};
|
|
117
|
+
var assertExpectedVersionMatchesCurrent = (current, expected, defaultVersion) => {
|
|
118
|
+
expected ??= NO_CONCURRENCY_CHECK;
|
|
119
|
+
if (!matchesExpectedVersion(current, expected, defaultVersion))
|
|
120
|
+
throw new ExpectedVersionConflictError(current, expected);
|
|
121
|
+
};
|
|
122
|
+
var ExpectedVersionConflictError = class _ExpectedVersionConflictError extends ConcurrencyError {
|
|
123
|
+
constructor(current, expected) {
|
|
124
|
+
super(current?.toString(), expected?.toString());
|
|
125
|
+
Object.setPrototypeOf(this, _ExpectedVersionConflictError.prototype);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
var isExpectedVersionConflictError = (error2) => error2 instanceof ExpectedVersionConflictError;
|
|
129
|
+
|
|
130
|
+
// src/eventStore/inMemoryEventStore.ts
|
|
131
|
+
import { v4 as uuid3 } from "uuid";
|
|
132
|
+
|
|
133
|
+
// src/eventStore/subscriptions/caughtUpTransformStream.ts
|
|
134
|
+
import { TransformStream } from "web-streams-polyfill";
|
|
135
|
+
var streamTrackingGlobalPosition = (currentEvents) => new CaughtUpTransformStream(currentEvents);
|
|
136
|
+
var CaughtUpTransformStream = class extends TransformStream {
|
|
137
|
+
_currentPosition;
|
|
138
|
+
_logPosition;
|
|
139
|
+
constructor(events) {
|
|
140
|
+
super({
|
|
141
|
+
start: (controller) => {
|
|
142
|
+
let globalPosition = 0n;
|
|
143
|
+
for (const event2 of events) {
|
|
144
|
+
controller.enqueue(event2);
|
|
145
|
+
globalPosition = event2.metadata.globalPosition;
|
|
146
|
+
}
|
|
147
|
+
controller.enqueue(globalStreamCaughtUp({ globalPosition }));
|
|
148
|
+
},
|
|
149
|
+
transform: (event2, controller) => {
|
|
150
|
+
this._currentPosition = event2.metadata.globalPosition;
|
|
151
|
+
controller.enqueue(event2);
|
|
152
|
+
if (this._currentPosition < this._logPosition) return;
|
|
153
|
+
controller.enqueue(
|
|
154
|
+
globalStreamCaughtUp({ globalPosition: this._currentPosition })
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
this._currentPosition = this._logPosition = events.length > 0 ? events[events.length - 1].metadata.globalPosition : 0n;
|
|
159
|
+
}
|
|
160
|
+
set logPosition(value) {
|
|
161
|
+
this._logPosition = value;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// src/eventStore/subscriptions/streamingCoordinator.ts
|
|
166
|
+
import { v4 as uuid2 } from "uuid";
|
|
167
|
+
|
|
168
|
+
// src/streaming/transformations/notifyAboutNoActiveReaders.ts
|
|
169
|
+
import { v4 as uuid } from "uuid";
|
|
170
|
+
import { TransformStream as TransformStream2 } from "web-streams-polyfill";
|
|
171
|
+
var notifyAboutNoActiveReadersStream = (onNoActiveReaderCallback, options = {}) => new NotifyAboutNoActiveReadersStream(onNoActiveReaderCallback, options);
|
|
172
|
+
var NotifyAboutNoActiveReadersStream = class extends TransformStream2 {
|
|
173
|
+
constructor(onNoActiveReaderCallback, options = {}) {
|
|
174
|
+
super({
|
|
175
|
+
cancel: (reason) => {
|
|
176
|
+
console.log("Stream was canceled. Reason:", reason);
|
|
177
|
+
this.stopChecking();
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
this.onNoActiveReaderCallback = onNoActiveReaderCallback;
|
|
181
|
+
this.streamId = options?.streamId ?? uuid();
|
|
182
|
+
this.onNoActiveReaderCallback = onNoActiveReaderCallback;
|
|
183
|
+
this.startChecking(options?.intervalCheckInMs ?? 20);
|
|
184
|
+
}
|
|
185
|
+
checkInterval = null;
|
|
186
|
+
streamId;
|
|
187
|
+
_isStopped = false;
|
|
188
|
+
get hasActiveSubscribers() {
|
|
189
|
+
return !this._isStopped;
|
|
190
|
+
}
|
|
191
|
+
startChecking(interval) {
|
|
192
|
+
this.checkInterval = setInterval(() => {
|
|
193
|
+
this.checkNoActiveReader();
|
|
194
|
+
}, interval);
|
|
195
|
+
}
|
|
196
|
+
stopChecking() {
|
|
197
|
+
if (!this.checkInterval) return;
|
|
198
|
+
clearInterval(this.checkInterval);
|
|
199
|
+
this.checkInterval = null;
|
|
200
|
+
this._isStopped = true;
|
|
201
|
+
this.onNoActiveReaderCallback(this);
|
|
202
|
+
}
|
|
203
|
+
checkNoActiveReader() {
|
|
204
|
+
if (!this.readable.locked && !this._isStopped) {
|
|
205
|
+
this.stopChecking();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
// src/streaming/writers/writeToStream.ts
|
|
211
|
+
var writeToStream = async (stream, items) => {
|
|
212
|
+
if (stream.writable.locked) return false;
|
|
213
|
+
const writer = stream.writable.getWriter();
|
|
214
|
+
await writer.ready;
|
|
215
|
+
if (!stream.readable.locked) return false;
|
|
216
|
+
try {
|
|
217
|
+
for (const item of items) {
|
|
218
|
+
await writer.write(item);
|
|
219
|
+
}
|
|
220
|
+
} catch (error2) {
|
|
221
|
+
console.log(error2);
|
|
222
|
+
} finally {
|
|
223
|
+
await writer.close();
|
|
224
|
+
}
|
|
225
|
+
return true;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// src/eventStore/subscriptions/streamingCoordinator.ts
|
|
229
|
+
var StreamingCoordinator = () => {
|
|
230
|
+
const allEvents = [];
|
|
231
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
232
|
+
return {
|
|
233
|
+
notify: async (events) => {
|
|
234
|
+
if (events.length === 0) return;
|
|
235
|
+
allEvents.push(...events);
|
|
236
|
+
for (const listener of listeners.values()) {
|
|
237
|
+
listener.logPosition = events[events.length - 1].metadata.globalPosition;
|
|
238
|
+
await writeToStream(listener, events);
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
stream: () => {
|
|
242
|
+
const streamId = uuid2();
|
|
243
|
+
const transformStream = streamTrackingGlobalPosition(allEvents);
|
|
244
|
+
listeners.set(streamId, transformStream);
|
|
245
|
+
return transformStream.readable.pipeThrough(
|
|
246
|
+
notifyAboutNoActiveReadersStream(
|
|
247
|
+
(stream) => {
|
|
248
|
+
if (listeners.has(stream.streamId))
|
|
249
|
+
listeners.delete(stream.streamId);
|
|
250
|
+
},
|
|
251
|
+
{ streamId }
|
|
252
|
+
)
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
// src/eventStore/inMemoryEventStore.ts
|
|
259
|
+
var InMemoryEventStoreDefaultStreamVersion = 0n;
|
|
260
|
+
var getInMemoryEventStore = () => {
|
|
261
|
+
const streams = /* @__PURE__ */ new Map();
|
|
262
|
+
const streamingCoordinator = StreamingCoordinator();
|
|
263
|
+
const getAllEventsCount = () => {
|
|
264
|
+
return Array.from(streams.values()).map((s) => s.length).reduce((p, c) => p + c, 0);
|
|
265
|
+
};
|
|
266
|
+
return {
|
|
267
|
+
async aggregateStream(streamName, options) {
|
|
268
|
+
const { evolve, initialState, read } = options;
|
|
269
|
+
const result = await this.readStream(streamName, read);
|
|
270
|
+
const events = result?.events ?? [];
|
|
271
|
+
return {
|
|
272
|
+
currentStreamVersion: BigInt(events.length),
|
|
273
|
+
state: events.reduce(evolve, initialState()),
|
|
274
|
+
streamExists: result.streamExists
|
|
275
|
+
};
|
|
276
|
+
},
|
|
277
|
+
readStream: (streamName, options) => {
|
|
278
|
+
const events = streams.get(streamName);
|
|
279
|
+
const currentStreamVersion = events ? BigInt(events.length) : InMemoryEventStoreDefaultStreamVersion;
|
|
280
|
+
assertExpectedVersionMatchesCurrent(
|
|
281
|
+
currentStreamVersion,
|
|
282
|
+
options?.expectedStreamVersion,
|
|
283
|
+
InMemoryEventStoreDefaultStreamVersion
|
|
284
|
+
);
|
|
285
|
+
const from = Number(options && "from" in options ? options.from : 0);
|
|
286
|
+
const to = Number(
|
|
287
|
+
options && "to" in options ? options.to : options && "maxCount" in options && options.maxCount ? options.from + options.maxCount : events?.length ?? 1
|
|
288
|
+
);
|
|
289
|
+
const resultEvents = events !== void 0 && events.length > 0 ? events.map(
|
|
290
|
+
(e) => e
|
|
291
|
+
).slice(from, to) : [];
|
|
292
|
+
const result = {
|
|
293
|
+
currentStreamVersion,
|
|
294
|
+
events: resultEvents,
|
|
295
|
+
streamExists: events !== void 0 && events.length > 0
|
|
296
|
+
};
|
|
297
|
+
return Promise.resolve(result);
|
|
298
|
+
},
|
|
299
|
+
appendToStream: async (streamName, events, options) => {
|
|
300
|
+
const currentEvents = streams.get(streamName) ?? [];
|
|
301
|
+
const currentStreamVersion = currentEvents.length > 0 ? BigInt(currentEvents.length) : InMemoryEventStoreDefaultStreamVersion;
|
|
302
|
+
assertExpectedVersionMatchesCurrent(
|
|
303
|
+
currentStreamVersion,
|
|
304
|
+
options?.expectedStreamVersion,
|
|
305
|
+
InMemoryEventStoreDefaultStreamVersion
|
|
306
|
+
);
|
|
307
|
+
const newEvents = events.map((event2, index) => {
|
|
308
|
+
return {
|
|
309
|
+
...event2,
|
|
310
|
+
metadata: {
|
|
311
|
+
...event2.metadata ?? {},
|
|
312
|
+
streamName,
|
|
313
|
+
eventId: uuid3(),
|
|
314
|
+
streamPosition: BigInt(currentEvents.length + index + 1),
|
|
315
|
+
globalPosition: BigInt(getAllEventsCount() + index + 1)
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
});
|
|
319
|
+
const positionOfLastEventInTheStream = BigInt(
|
|
320
|
+
newEvents.slice(-1)[0].metadata.streamPosition
|
|
321
|
+
);
|
|
322
|
+
streams.set(streamName, [...currentEvents, ...newEvents]);
|
|
323
|
+
await streamingCoordinator.notify(newEvents);
|
|
324
|
+
const result = {
|
|
325
|
+
nextExpectedStreamVersion: positionOfLastEventInTheStream,
|
|
326
|
+
createdNewStream: currentStreamVersion === InMemoryEventStoreDefaultStreamVersion
|
|
327
|
+
};
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
330
|
+
//streamEvents: streamingCoordinator.stream,
|
|
331
|
+
};
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
// src/utils/deepEquals.ts
|
|
335
|
+
var deepEquals = (left, right) => {
|
|
336
|
+
if (isEquatable(left)) {
|
|
337
|
+
return left.equals(right);
|
|
338
|
+
}
|
|
339
|
+
if (Array.isArray(left)) {
|
|
340
|
+
return Array.isArray(right) && left.length === right.length && left.every((val, index) => deepEquals(val, right[index]));
|
|
341
|
+
}
|
|
342
|
+
if (typeof left !== "object" || typeof right !== "object" || left === null || right === null) {
|
|
343
|
+
return left === right;
|
|
344
|
+
}
|
|
345
|
+
if (Array.isArray(right)) return false;
|
|
346
|
+
const keys1 = Object.keys(left);
|
|
347
|
+
const keys2 = Object.keys(right);
|
|
348
|
+
if (keys1.length !== keys2.length || !keys1.every((key) => keys2.includes(key)))
|
|
349
|
+
return false;
|
|
350
|
+
for (const key in left) {
|
|
351
|
+
if (left[key] instanceof Function && right[key] instanceof Function)
|
|
352
|
+
continue;
|
|
353
|
+
const isEqual = deepEquals(left[key], right[key]);
|
|
354
|
+
if (!isEqual) {
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return true;
|
|
359
|
+
};
|
|
360
|
+
var isEquatable = (left) => {
|
|
361
|
+
return left && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
// src/utils/iterators.ts
|
|
365
|
+
var sum = (iterator) => {
|
|
366
|
+
let value, done, sum2 = 0;
|
|
367
|
+
do {
|
|
368
|
+
({ value, done } = iterator.next());
|
|
369
|
+
sum2 += value || 0;
|
|
370
|
+
} while (!done);
|
|
371
|
+
return sum2;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
// src/utils/merge.ts
|
|
375
|
+
var merge = (array, item, where, onExisting, onNotFound = () => void 0) => {
|
|
376
|
+
let wasFound = false;
|
|
377
|
+
const result = array.map((p) => {
|
|
378
|
+
if (!where(p)) return p;
|
|
379
|
+
wasFound = true;
|
|
380
|
+
return onExisting(p);
|
|
381
|
+
}).filter((p) => p !== void 0).map((p) => {
|
|
382
|
+
if (!p) throw Error("That should not happen");
|
|
383
|
+
return p;
|
|
384
|
+
});
|
|
385
|
+
if (!wasFound) {
|
|
386
|
+
const result2 = onNotFound();
|
|
387
|
+
if (result2 !== void 0) return [...array, item];
|
|
388
|
+
}
|
|
389
|
+
return result;
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
// src/utils/retry.ts
|
|
393
|
+
import retry from "async-retry";
|
|
394
|
+
var NoRetries = { retries: 0 };
|
|
395
|
+
var asyncRetry = async (fn, opts) => {
|
|
396
|
+
if (opts === void 0 || opts.retries === 0) return fn();
|
|
397
|
+
return retry(
|
|
398
|
+
async (bail) => {
|
|
399
|
+
try {
|
|
400
|
+
return await fn();
|
|
401
|
+
} catch (error2) {
|
|
402
|
+
if (opts?.shouldRetryError && !opts.shouldRetryError(error2)) {
|
|
403
|
+
bail(error2);
|
|
404
|
+
}
|
|
405
|
+
throw error2;
|
|
406
|
+
}
|
|
407
|
+
},
|
|
408
|
+
opts ?? { retries: 0 }
|
|
409
|
+
);
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
// src/commandHandling/handleCommand.ts
|
|
413
|
+
var CommandHandlerStreamVersionConflictRetryOptions = {
|
|
414
|
+
retries: 3,
|
|
415
|
+
minTimeout: 100,
|
|
416
|
+
factor: 1.5,
|
|
417
|
+
shouldRetryError: isExpectedVersionConflictError
|
|
418
|
+
};
|
|
419
|
+
var fromCommandHandlerRetryOptions = (retryOptions) => {
|
|
420
|
+
if (retryOptions === void 0) return NoRetries;
|
|
421
|
+
if ("onVersionConflict" in retryOptions) {
|
|
422
|
+
if (typeof retryOptions.onVersionConflict === "boolean")
|
|
423
|
+
return CommandHandlerStreamVersionConflictRetryOptions;
|
|
424
|
+
else if (typeof retryOptions.onVersionConflict === "number")
|
|
425
|
+
return {
|
|
426
|
+
...CommandHandlerStreamVersionConflictRetryOptions,
|
|
427
|
+
retries: retryOptions.onVersionConflict
|
|
428
|
+
};
|
|
429
|
+
else return retryOptions.onVersionConflict;
|
|
430
|
+
}
|
|
431
|
+
return retryOptions;
|
|
432
|
+
};
|
|
433
|
+
var CommandHandler = (options) => async (store, id, handle, handleOptions) => asyncRetry(
|
|
434
|
+
async () => {
|
|
435
|
+
const result = await withSession(store, async ({ eventStore }) => {
|
|
436
|
+
const { evolve, initialState } = options;
|
|
437
|
+
const mapToStreamId = options.mapToStreamId ?? ((id2) => id2);
|
|
438
|
+
const streamName = mapToStreamId(id);
|
|
439
|
+
const aggregationResult = await eventStore.aggregateStream(streamName, {
|
|
440
|
+
evolve,
|
|
441
|
+
initialState,
|
|
442
|
+
read: {
|
|
443
|
+
// expected stream version is passed to fail fast
|
|
444
|
+
// if stream is in the wrong state
|
|
445
|
+
expectedStreamVersion: handleOptions?.expectedStreamVersion ?? NO_CONCURRENCY_CHECK
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
const state = aggregationResult.state;
|
|
449
|
+
const currentStreamVersion = aggregationResult.currentStreamVersion;
|
|
450
|
+
const result2 = await handle(state);
|
|
451
|
+
const newEvents = Array.isArray(result2) ? result2 : [result2];
|
|
452
|
+
if (newEvents.length === 0) {
|
|
453
|
+
return {
|
|
454
|
+
newEvents: [],
|
|
455
|
+
newState: state,
|
|
456
|
+
nextExpectedStreamVersion: currentStreamVersion,
|
|
457
|
+
createdNewStream: false
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
const expectedStreamVersion = handleOptions?.expectedStreamVersion ?? (aggregationResult.streamExists ? currentStreamVersion : STREAM_DOES_NOT_EXIST);
|
|
461
|
+
const appendResult = await eventStore.appendToStream(
|
|
462
|
+
streamName,
|
|
463
|
+
newEvents,
|
|
464
|
+
{
|
|
465
|
+
...handleOptions,
|
|
466
|
+
expectedStreamVersion
|
|
467
|
+
}
|
|
468
|
+
);
|
|
469
|
+
return {
|
|
470
|
+
...appendResult,
|
|
471
|
+
newEvents,
|
|
472
|
+
newState: newEvents.reduce(evolve, state)
|
|
473
|
+
};
|
|
474
|
+
});
|
|
475
|
+
return result;
|
|
476
|
+
},
|
|
477
|
+
fromCommandHandlerRetryOptions(
|
|
478
|
+
handleOptions && "retry" in handleOptions ? handleOptions.retry : options.retry
|
|
479
|
+
)
|
|
480
|
+
);
|
|
481
|
+
var withSession = (eventStore, callback) => {
|
|
482
|
+
const sessionFactory = canCreateEventStoreSession(eventStore) ? eventStore : nulloSessionFactory(eventStore);
|
|
483
|
+
return sessionFactory.withSession(callback);
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
// src/commandHandling/handleCommandWithDecider.ts
|
|
487
|
+
var DeciderCommandHandler = (options) => async (eventStore, id, command2, handleOptions) => {
|
|
488
|
+
const { decide, ...rest } = options;
|
|
489
|
+
return CommandHandler(rest)(
|
|
490
|
+
eventStore,
|
|
491
|
+
id,
|
|
492
|
+
(state) => decide(command2, state),
|
|
493
|
+
handleOptions
|
|
494
|
+
);
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
// src/messageBus/index.ts
|
|
498
|
+
var getInMemoryMessageBus = () => {
|
|
499
|
+
const allHandlers = /* @__PURE__ */ new Map();
|
|
500
|
+
let pendingMessages = [];
|
|
501
|
+
return {
|
|
502
|
+
send: async (command2) => {
|
|
503
|
+
const handlers = allHandlers.get(command2.type);
|
|
504
|
+
if (handlers === void 0 || handlers.length === 0)
|
|
505
|
+
throw new EmmettError(
|
|
506
|
+
`No handler registered for command ${command2.type}!`
|
|
507
|
+
);
|
|
508
|
+
const commandHandler = handlers[0];
|
|
509
|
+
await commandHandler(command2);
|
|
510
|
+
},
|
|
511
|
+
publish: async (event2) => {
|
|
512
|
+
const handlers = allHandlers.get(event2.type) ?? [];
|
|
513
|
+
for (const handler of handlers) {
|
|
514
|
+
const eventHandler = handler;
|
|
515
|
+
await eventHandler(event2);
|
|
516
|
+
}
|
|
517
|
+
},
|
|
518
|
+
schedule: (message, when) => {
|
|
519
|
+
pendingMessages = [...pendingMessages, { message, options: when }];
|
|
520
|
+
},
|
|
521
|
+
handle: (commandHandler, ...commandTypes) => {
|
|
522
|
+
const alreadyRegistered = [...allHandlers.keys()].filter(
|
|
523
|
+
(registered) => commandTypes.includes(registered)
|
|
524
|
+
);
|
|
525
|
+
if (alreadyRegistered.length > 0)
|
|
526
|
+
throw new EmmettError(
|
|
527
|
+
`Cannot register handler for commands ${alreadyRegistered.join(", ")} as they're already registered!`
|
|
528
|
+
);
|
|
529
|
+
for (const commandType of commandTypes) {
|
|
530
|
+
allHandlers.set(commandType, [commandHandler]);
|
|
531
|
+
}
|
|
532
|
+
},
|
|
533
|
+
subscribe(eventHandler, ...eventTypes) {
|
|
534
|
+
for (const eventType of eventTypes) {
|
|
535
|
+
if (!allHandlers.has(eventType)) allHandlers.set(eventType, []);
|
|
536
|
+
allHandlers.set(eventType, [
|
|
537
|
+
...allHandlers.get(eventType) ?? [],
|
|
538
|
+
eventHandler
|
|
539
|
+
]);
|
|
540
|
+
}
|
|
541
|
+
},
|
|
542
|
+
dequeue: () => {
|
|
543
|
+
const pending = pendingMessages;
|
|
544
|
+
pendingMessages = [];
|
|
545
|
+
return pending;
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
// src/projections/index.ts
|
|
551
|
+
var projection = (definition) => definition;
|
|
552
|
+
var inlineProjections = (definitions) => definitions.map((projection2) => ({ type: "inline", projection: projection2 }));
|
|
553
|
+
var asyncProjections = (definitions) => definitions.map((projection2) => ({ type: "async", projection: projection2 }));
|
|
554
|
+
var projections = {
|
|
555
|
+
inline: inlineProjections,
|
|
556
|
+
async: asyncProjections
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
// src/serialization/json/JSONParser.ts
|
|
560
|
+
var ParseError = class extends Error {
|
|
561
|
+
constructor(text) {
|
|
562
|
+
super(`Cannot parse! ${text}`);
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
var JSONParser = {
|
|
566
|
+
stringify: (value, options) => {
|
|
567
|
+
return JSON.stringify(
|
|
568
|
+
options?.map ? options.map(value) : value,
|
|
569
|
+
//TODO: Consider adding support to DateTime and adding specific format to mark that's a bigint
|
|
570
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
571
|
+
(_, v) => typeof v === "bigint" ? v.toString() : v
|
|
572
|
+
);
|
|
573
|
+
},
|
|
574
|
+
parse: (text, options) => {
|
|
575
|
+
const parsed = JSON.parse(text, options?.reviver);
|
|
576
|
+
if (options?.typeCheck && !options?.typeCheck(parsed))
|
|
577
|
+
throw new ParseError(text);
|
|
578
|
+
return options?.map ? options.map(parsed) : parsed;
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
// src/streaming/binaryArrays.ts
|
|
583
|
+
var concatUint8Arrays = (chunks) => {
|
|
584
|
+
const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
|
|
585
|
+
const result = new Uint8Array(totalLength);
|
|
586
|
+
let offset = 0;
|
|
587
|
+
for (const chunk of chunks) {
|
|
588
|
+
result.set(chunk, offset);
|
|
589
|
+
offset += chunk.length;
|
|
590
|
+
}
|
|
591
|
+
return result;
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
// src/streaming/collectors/collect.ts
|
|
595
|
+
var collect = async (stream) => {
|
|
596
|
+
const results = [];
|
|
597
|
+
for await (const value of stream) {
|
|
598
|
+
results.push(value);
|
|
599
|
+
}
|
|
600
|
+
return results;
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
// src/streaming/decoders/binary.ts
|
|
604
|
+
var BinaryJsonDecoder = class {
|
|
605
|
+
buffer = [];
|
|
606
|
+
addToBuffer(data) {
|
|
607
|
+
this.buffer.push(data);
|
|
608
|
+
}
|
|
609
|
+
clearBuffer() {
|
|
610
|
+
this.buffer = [];
|
|
611
|
+
}
|
|
612
|
+
hasCompleteMessage() {
|
|
613
|
+
const combined = concatUint8Arrays(this.buffer);
|
|
614
|
+
const text = new TextDecoder().decode(combined);
|
|
615
|
+
return text.includes("\n");
|
|
616
|
+
}
|
|
617
|
+
decode() {
|
|
618
|
+
if (!this.hasCompleteMessage()) {
|
|
619
|
+
return null;
|
|
620
|
+
}
|
|
621
|
+
const combined = concatUint8Arrays(this.buffer);
|
|
622
|
+
const text = new TextDecoder().decode(combined);
|
|
623
|
+
const delimiterIndex = text.indexOf("\n");
|
|
624
|
+
if (delimiterIndex === -1) {
|
|
625
|
+
return null;
|
|
626
|
+
}
|
|
627
|
+
const jsonString = text.slice(0, delimiterIndex);
|
|
628
|
+
const remaining = new Uint8Array(combined.buffer, delimiterIndex + 1);
|
|
629
|
+
this.buffer = remaining.byteLength > 0 ? [remaining] : [];
|
|
630
|
+
return JSON.parse(jsonString);
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
// src/streaming/decoders/string.ts
|
|
635
|
+
var StringDecoder = class {
|
|
636
|
+
constructor(transform) {
|
|
637
|
+
this.transform = transform;
|
|
638
|
+
this.transform = transform;
|
|
639
|
+
}
|
|
640
|
+
buffer = [];
|
|
641
|
+
addToBuffer(data) {
|
|
642
|
+
this.buffer.push(data);
|
|
643
|
+
}
|
|
644
|
+
clearBuffer() {
|
|
645
|
+
this.buffer = [];
|
|
646
|
+
}
|
|
647
|
+
hasCompleteMessage() {
|
|
648
|
+
return this.buffer.some((chunk) => chunk.includes("\n"));
|
|
649
|
+
}
|
|
650
|
+
decode() {
|
|
651
|
+
const completeString = this.buffer.join("");
|
|
652
|
+
if (!this.hasCompleteMessage()) {
|
|
653
|
+
if (completeString.trim().length > 0) {
|
|
654
|
+
throw new Error("Unterminated string in JSON at position");
|
|
655
|
+
}
|
|
656
|
+
return null;
|
|
657
|
+
}
|
|
658
|
+
const delimiterIndex = completeString.indexOf("\n");
|
|
659
|
+
const message = completeString.slice(0, delimiterIndex).trim();
|
|
660
|
+
this.buffer = [completeString.slice(delimiterIndex + 1)];
|
|
661
|
+
return this.transform(message);
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
|
|
665
|
+
// src/streaming/decoders/json.ts
|
|
666
|
+
var JsonDecoder = class extends StringDecoder {
|
|
667
|
+
constructor() {
|
|
668
|
+
super((jsonString) => JSON.parse(jsonString));
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
|
|
672
|
+
// src/streaming/decoders/object.ts
|
|
673
|
+
var ObjectDecoder = class {
|
|
674
|
+
buffer = null;
|
|
675
|
+
addToBuffer(data) {
|
|
676
|
+
this.buffer = data;
|
|
677
|
+
}
|
|
678
|
+
clearBuffer() {
|
|
679
|
+
this.buffer = null;
|
|
680
|
+
}
|
|
681
|
+
hasCompleteMessage() {
|
|
682
|
+
return this.buffer !== null;
|
|
683
|
+
}
|
|
684
|
+
decode() {
|
|
685
|
+
if (!this.hasCompleteMessage() || !this.buffer) {
|
|
686
|
+
return null;
|
|
687
|
+
}
|
|
688
|
+
const data = this.buffer;
|
|
689
|
+
this.clearBuffer();
|
|
690
|
+
return data;
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
|
|
694
|
+
// src/streaming/decoders/composite.ts
|
|
695
|
+
var CompositeDecoder = class {
|
|
696
|
+
constructor(decoders) {
|
|
697
|
+
this.decoders = decoders;
|
|
698
|
+
}
|
|
699
|
+
decoderFor(data) {
|
|
700
|
+
const decoder = this.decoders.find((d) => d[0](data));
|
|
701
|
+
if (!decoder) return null;
|
|
702
|
+
return decoder[1];
|
|
703
|
+
}
|
|
704
|
+
addToBuffer(data) {
|
|
705
|
+
this.decoderFor(data)?.addToBuffer(data);
|
|
706
|
+
}
|
|
707
|
+
clearBuffer() {
|
|
708
|
+
for (const decoder of this.decoders.map((d) => d[1])) {
|
|
709
|
+
decoder.clearBuffer();
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
hasCompleteMessage() {
|
|
713
|
+
return this.decoders.some((d) => d[1].hasCompleteMessage());
|
|
714
|
+
}
|
|
715
|
+
decode() {
|
|
716
|
+
const decoder = this.decoders.map((d) => d[1]).find((d) => d.hasCompleteMessage());
|
|
717
|
+
return decoder?.decode() ?? null;
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
var DefaultDecoder = class extends CompositeDecoder {
|
|
721
|
+
constructor() {
|
|
722
|
+
super([
|
|
723
|
+
[(data) => typeof data === "string", new JsonDecoder()],
|
|
724
|
+
[(data) => data instanceof Uint8Array, new BinaryJsonDecoder()],
|
|
725
|
+
[(data) => typeof data === "object", new ObjectDecoder()]
|
|
726
|
+
]);
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
// src/streaming/generators/fromArray.ts
|
|
731
|
+
import { ReadableStream } from "web-streams-polyfill";
|
|
732
|
+
var fromArray = (chunks) => new ReadableStream({
|
|
733
|
+
start(controller) {
|
|
734
|
+
for (const chunk of chunks) controller.enqueue(chunk);
|
|
735
|
+
controller.close();
|
|
736
|
+
}
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
// src/streaming/generators/index.ts
|
|
740
|
+
var streamGenerators = { fromArray };
|
|
741
|
+
|
|
742
|
+
// src/streaming/restream.ts
|
|
743
|
+
import "web-streams-polyfill";
|
|
744
|
+
|
|
745
|
+
// src/streaming/transformations/filter.ts
|
|
746
|
+
import { TransformStream as TransformStream3 } from "web-streams-polyfill";
|
|
747
|
+
var filter = (filter2) => new TransformStream3({
|
|
748
|
+
transform(chunk, controller) {
|
|
749
|
+
if (filter2(chunk)) {
|
|
750
|
+
controller.enqueue(chunk);
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
// src/streaming/transformations/map.ts
|
|
756
|
+
import { TransformStream as TransformStream4 } from "web-streams-polyfill";
|
|
757
|
+
var map = (map2) => new TransformStream4({
|
|
758
|
+
transform(chunk, controller) {
|
|
759
|
+
controller.enqueue(map2(chunk));
|
|
760
|
+
}
|
|
761
|
+
});
|
|
762
|
+
|
|
763
|
+
// src/streaming/transformations/reduce.ts
|
|
764
|
+
import { TransformStream as TransformStream5 } from "web-streams-polyfill";
|
|
765
|
+
var reduce = (reducer, initialValue) => new ReduceTransformStream(reducer, initialValue);
|
|
766
|
+
var ReduceTransformStream = class extends TransformStream5 {
|
|
767
|
+
accumulator;
|
|
768
|
+
reducer;
|
|
769
|
+
constructor(reducer, initialValue) {
|
|
770
|
+
super({
|
|
771
|
+
transform: (chunk) => {
|
|
772
|
+
this.accumulator = this.reducer(this.accumulator, chunk);
|
|
773
|
+
},
|
|
774
|
+
flush: (controller) => {
|
|
775
|
+
controller.enqueue(this.accumulator);
|
|
776
|
+
controller.terminate();
|
|
777
|
+
}
|
|
778
|
+
});
|
|
779
|
+
this.accumulator = initialValue;
|
|
780
|
+
this.reducer = reducer;
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
|
|
784
|
+
// src/streaming/transformations/retry.ts
|
|
785
|
+
import {
|
|
786
|
+
TransformStream as TransformStream6
|
|
787
|
+
} from "web-streams-polyfill";
|
|
788
|
+
var retryStream = (createSourceStream, handleChunk2, retryOptions = { forever: true, minTimeout: 25 }) => new TransformStream6({
|
|
789
|
+
start(controller) {
|
|
790
|
+
asyncRetry(
|
|
791
|
+
() => onRestream(createSourceStream, handleChunk2, controller),
|
|
792
|
+
retryOptions
|
|
793
|
+
).catch((error2) => {
|
|
794
|
+
controller.error(error2);
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
});
|
|
798
|
+
var onRestream = async (createSourceStream, handleChunk2, controller) => {
|
|
799
|
+
const sourceStream = createSourceStream();
|
|
800
|
+
const reader = sourceStream.getReader();
|
|
801
|
+
try {
|
|
802
|
+
let done;
|
|
803
|
+
do {
|
|
804
|
+
const result = await reader.read();
|
|
805
|
+
done = result.done;
|
|
806
|
+
await handleChunk2(result, controller);
|
|
807
|
+
if (done) {
|
|
808
|
+
controller.terminate();
|
|
809
|
+
}
|
|
810
|
+
} while (!done);
|
|
811
|
+
} finally {
|
|
812
|
+
reader.releaseLock();
|
|
813
|
+
}
|
|
814
|
+
};
|
|
815
|
+
|
|
816
|
+
// src/streaming/transformations/skip.ts
|
|
817
|
+
import { TransformStream as TransformStream7 } from "web-streams-polyfill";
|
|
818
|
+
var skip = (limit) => new SkipTransformStream(limit);
|
|
819
|
+
var SkipTransformStream = class extends TransformStream7 {
|
|
820
|
+
count = 0;
|
|
821
|
+
skip;
|
|
822
|
+
constructor(skip2) {
|
|
823
|
+
super({
|
|
824
|
+
transform: (chunk, controller) => {
|
|
825
|
+
this.count++;
|
|
826
|
+
if (this.count > this.skip) {
|
|
827
|
+
controller.enqueue(chunk);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
});
|
|
831
|
+
this.skip = skip2;
|
|
832
|
+
}
|
|
833
|
+
};
|
|
834
|
+
|
|
835
|
+
// src/streaming/transformations/stopAfter.ts
|
|
836
|
+
import { TransformStream as TransformStream8 } from "web-streams-polyfill";
|
|
837
|
+
var stopAfter = (stopCondition) => new TransformStream8({
|
|
838
|
+
transform(chunk, controller) {
|
|
839
|
+
controller.enqueue(chunk);
|
|
840
|
+
if (stopCondition(chunk)) {
|
|
841
|
+
controller.terminate();
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
});
|
|
845
|
+
|
|
846
|
+
// src/streaming/transformations/stopOn.ts
|
|
847
|
+
import { TransformStream as TransformStream9 } from "web-streams-polyfill";
|
|
848
|
+
var stopOn = (stopCondition) => new TransformStream9({
|
|
849
|
+
async transform(chunk, controller) {
|
|
850
|
+
if (!stopCondition(chunk)) {
|
|
851
|
+
controller.enqueue(chunk);
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
await Promise.resolve();
|
|
855
|
+
controller.terminate();
|
|
856
|
+
}
|
|
857
|
+
});
|
|
858
|
+
|
|
859
|
+
// src/streaming/transformations/take.ts
|
|
860
|
+
import { TransformStream as TransformStream10 } from "web-streams-polyfill";
|
|
861
|
+
var take = (limit) => new TakeTransformStream(limit);
|
|
862
|
+
var TakeTransformStream = class extends TransformStream10 {
|
|
863
|
+
count = 0;
|
|
864
|
+
limit;
|
|
865
|
+
constructor(limit) {
|
|
866
|
+
super({
|
|
867
|
+
transform: (chunk, controller) => {
|
|
868
|
+
if (this.count < this.limit) {
|
|
869
|
+
this.count++;
|
|
870
|
+
controller.enqueue(chunk);
|
|
871
|
+
} else {
|
|
872
|
+
controller.terminate();
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
});
|
|
876
|
+
this.limit = limit;
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
// src/streaming/transformations/waitAtMost.ts
|
|
881
|
+
import { TransformStream as TransformStream11 } from "web-streams-polyfill";
|
|
882
|
+
var waitAtMost = (waitTimeInMs) => new TransformStream11({
|
|
883
|
+
start(controller) {
|
|
884
|
+
const timeoutId = setTimeout(() => {
|
|
885
|
+
controller.terminate();
|
|
886
|
+
}, waitTimeInMs);
|
|
887
|
+
const originalTerminate = controller.terminate.bind(controller);
|
|
888
|
+
controller.terminate = () => {
|
|
889
|
+
clearTimeout(timeoutId);
|
|
890
|
+
originalTerminate();
|
|
891
|
+
};
|
|
892
|
+
},
|
|
893
|
+
transform(chunk, controller) {
|
|
894
|
+
controller.enqueue(chunk);
|
|
895
|
+
}
|
|
896
|
+
});
|
|
897
|
+
|
|
898
|
+
// src/streaming/transformations/index.ts
|
|
899
|
+
var streamTransformations = {
|
|
900
|
+
filter,
|
|
901
|
+
take,
|
|
902
|
+
TakeTransformStream,
|
|
903
|
+
skip,
|
|
904
|
+
SkipTransformStream,
|
|
905
|
+
map,
|
|
906
|
+
notifyAboutNoActiveReadersStream,
|
|
907
|
+
NotifyAboutNoActiveReadersStream,
|
|
908
|
+
reduce,
|
|
909
|
+
ReduceTransformStream,
|
|
910
|
+
retry: retryStream,
|
|
911
|
+
stopAfter,
|
|
912
|
+
stopOn,
|
|
913
|
+
waitAtMost
|
|
914
|
+
};
|
|
915
|
+
|
|
916
|
+
// src/streaming/restream.ts
|
|
917
|
+
var { retry: retry2 } = streamTransformations;
|
|
918
|
+
var restream = (createSourceStream, transform = (source) => source, retryOptions = { forever: true, minTimeout: 25 }, decoder = new DefaultDecoder()) => retry2(createSourceStream, handleChunk(transform, decoder), retryOptions).readable;
|
|
919
|
+
var handleChunk = (transform = (source) => source, decoder = new DefaultDecoder()) => (readResult, controller) => {
|
|
920
|
+
const { done: isDone, value } = readResult;
|
|
921
|
+
if (value) decoder.addToBuffer(value);
|
|
922
|
+
if (!isDone && !decoder.hasCompleteMessage()) return;
|
|
923
|
+
decodeAndTransform(decoder, transform, controller);
|
|
924
|
+
};
|
|
925
|
+
var decodeAndTransform = (decoder, transform, controller) => {
|
|
926
|
+
try {
|
|
927
|
+
const decoded = decoder.decode();
|
|
928
|
+
if (!decoded) return;
|
|
929
|
+
const transformed = transform(decoded);
|
|
930
|
+
controller.enqueue(transformed);
|
|
931
|
+
} catch (error2) {
|
|
932
|
+
controller.error(new Error(`Decoding error: ${error2?.toString()}`));
|
|
933
|
+
}
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
// src/testing/assertions.ts
|
|
937
|
+
var AssertionError = class extends Error {
|
|
938
|
+
constructor(message) {
|
|
939
|
+
super(message);
|
|
940
|
+
}
|
|
941
|
+
};
|
|
942
|
+
var isSubset = (superObj, subObj) => {
|
|
943
|
+
const sup = superObj;
|
|
944
|
+
const sub = subObj;
|
|
945
|
+
assertOk(sup);
|
|
946
|
+
assertOk(sub);
|
|
947
|
+
return Object.keys(sub).every((ele) => {
|
|
948
|
+
if (typeof sub[ele] == "object") {
|
|
949
|
+
return isSubset(sup[ele], sub[ele]);
|
|
950
|
+
}
|
|
951
|
+
return sub[ele] === sup[ele];
|
|
952
|
+
});
|
|
953
|
+
};
|
|
954
|
+
var assertFails = (message) => {
|
|
955
|
+
throw new AssertionError(message ?? "That should not ever happened, right?");
|
|
956
|
+
};
|
|
957
|
+
var assertThrowsAsync = async (fun, errorCheck) => {
|
|
958
|
+
try {
|
|
959
|
+
await fun();
|
|
960
|
+
throw new AssertionError("Function didn't throw expected error");
|
|
961
|
+
} catch (error2) {
|
|
962
|
+
const typedError = error2;
|
|
963
|
+
if (errorCheck) assertTrue(errorCheck(typedError));
|
|
964
|
+
return typedError;
|
|
965
|
+
}
|
|
966
|
+
};
|
|
967
|
+
var assertThrows = (fun, errorCheck) => {
|
|
968
|
+
try {
|
|
969
|
+
fun();
|
|
970
|
+
throw new AssertionError("Function didn't throw expected error");
|
|
971
|
+
} catch (error2) {
|
|
972
|
+
const typedError = error2;
|
|
973
|
+
if (errorCheck) assertTrue(errorCheck(typedError));
|
|
974
|
+
return typedError;
|
|
975
|
+
}
|
|
976
|
+
};
|
|
977
|
+
var assertRejects = async (promise, errorCheck) => {
|
|
978
|
+
try {
|
|
979
|
+
await promise;
|
|
980
|
+
throw new AssertionError("Function didn't throw expected error");
|
|
981
|
+
} catch (error2) {
|
|
982
|
+
if (!errorCheck) return;
|
|
983
|
+
if (errorCheck instanceof Error) assertDeepEqual(error2, errorCheck);
|
|
984
|
+
else assertTrue(errorCheck(error2));
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
var assertMatches = (actual, expected, message) => {
|
|
988
|
+
if (!isSubset(actual, expected))
|
|
989
|
+
throw new AssertionError(
|
|
990
|
+
message ?? `subObj:
|
|
991
|
+
${JSONParser.stringify(expected)}
|
|
7
992
|
is not subset of
|
|
8
|
-
${
|
|
9
|
-
|
|
993
|
+
${JSONParser.stringify(actual)}`
|
|
994
|
+
);
|
|
995
|
+
};
|
|
996
|
+
var assertDeepEqual = (actual, expected, message) => {
|
|
997
|
+
if (!deepEquals(actual, expected))
|
|
998
|
+
throw new AssertionError(
|
|
999
|
+
message ?? `subObj:
|
|
1000
|
+
${JSONParser.stringify(expected)}
|
|
10
1001
|
is not equal to
|
|
11
|
-
${
|
|
12
|
-
|
|
1002
|
+
${JSONParser.stringify(actual)}`
|
|
1003
|
+
);
|
|
1004
|
+
};
|
|
1005
|
+
var assertNotDeepEqual = (actual, expected, message) => {
|
|
1006
|
+
if (deepEquals(actual, expected))
|
|
1007
|
+
throw new AssertionError(
|
|
1008
|
+
message ?? `subObj:
|
|
1009
|
+
${JSONParser.stringify(expected)}
|
|
13
1010
|
is equals to
|
|
14
|
-
${
|
|
15
|
-
|
|
16
|
-
|
|
1011
|
+
${JSONParser.stringify(actual)}`
|
|
1012
|
+
);
|
|
1013
|
+
};
|
|
1014
|
+
var assertThat = (item) => {
|
|
1015
|
+
return {
|
|
1016
|
+
isEqualTo: (other) => assertTrue(deepEquals(item, other))
|
|
1017
|
+
};
|
|
1018
|
+
};
|
|
1019
|
+
function assertFalse(condition, message) {
|
|
1020
|
+
if (condition) throw new AssertionError(message ?? `Condition is false`);
|
|
1021
|
+
}
|
|
1022
|
+
function assertTrue(condition, message) {
|
|
1023
|
+
if (!condition) throw new AssertionError(message ?? `Condition is false`);
|
|
1024
|
+
}
|
|
1025
|
+
function assertOk(obj, message) {
|
|
1026
|
+
if (!obj) throw new AssertionError(message ?? `Condition is not truthy`);
|
|
1027
|
+
}
|
|
1028
|
+
function assertEqual(expected, actual, message) {
|
|
1029
|
+
if (expected !== actual)
|
|
1030
|
+
throw new AssertionError(
|
|
1031
|
+
`${message ?? "Objects are not equal"}:
|
|
1032
|
+
Expected: ${JSONParser.stringify(expected)}
|
|
1033
|
+
Actual:${JSONParser.stringify(actual)}`
|
|
1034
|
+
);
|
|
1035
|
+
}
|
|
1036
|
+
function assertNotEqual(obj, other, message) {
|
|
1037
|
+
if (obj === other)
|
|
1038
|
+
throw new AssertionError(
|
|
1039
|
+
message ?? `Objects are equal: ${JSONParser.stringify(obj)}`
|
|
1040
|
+
);
|
|
1041
|
+
}
|
|
1042
|
+
function assertIsNotNull(result) {
|
|
1043
|
+
assertNotEqual(result, null);
|
|
1044
|
+
assertOk(result);
|
|
1045
|
+
}
|
|
1046
|
+
function assertIsNull(result) {
|
|
1047
|
+
assertEqual(result, null);
|
|
1048
|
+
}
|
|
1049
|
+
var argValue = (value) => (arg) => deepEquals(arg, value);
|
|
1050
|
+
var argMatches = (matches) => (arg) => matches(arg);
|
|
1051
|
+
function verifyThat(fn) {
|
|
1052
|
+
return {
|
|
1053
|
+
calledTimes: (times) => {
|
|
1054
|
+
assertEqual(fn.mock?.calls?.length, times);
|
|
1055
|
+
},
|
|
1056
|
+
notCalled: () => {
|
|
1057
|
+
assertEqual(fn?.mock?.calls?.length, 0);
|
|
1058
|
+
},
|
|
1059
|
+
called: () => {
|
|
1060
|
+
assertTrue(
|
|
1061
|
+
fn.mock?.calls.length !== void 0 && fn.mock.calls.length > 0
|
|
1062
|
+
);
|
|
1063
|
+
},
|
|
1064
|
+
calledWith: (...args) => {
|
|
1065
|
+
assertTrue(
|
|
1066
|
+
fn.mock?.calls.length !== void 0 && fn.mock.calls.length >= 1 && fn.mock.calls.some((call) => deepEquals(call.arguments, args))
|
|
1067
|
+
);
|
|
1068
|
+
},
|
|
1069
|
+
calledOnceWith: (...args) => {
|
|
1070
|
+
assertTrue(
|
|
1071
|
+
fn.mock?.calls.length !== void 0 && fn.mock.calls.length === 1 && fn.mock.calls.some((call) => deepEquals(call.arguments, args))
|
|
1072
|
+
);
|
|
1073
|
+
},
|
|
1074
|
+
calledWithArgumentMatching: (...matches) => {
|
|
1075
|
+
assertTrue(
|
|
1076
|
+
fn.mock?.calls.length !== void 0 && fn.mock.calls.length >= 1
|
|
1077
|
+
);
|
|
1078
|
+
assertTrue(
|
|
1079
|
+
fn.mock?.calls.length !== void 0 && fn.mock.calls.length >= 1 && fn.mock.calls.some(
|
|
1080
|
+
(call) => call.arguments && call.arguments.length >= matches.length && matches.every((match, index) => match(call.arguments[index]))
|
|
1081
|
+
)
|
|
1082
|
+
);
|
|
1083
|
+
},
|
|
1084
|
+
notCalledWithArgumentMatching: (...matches) => {
|
|
1085
|
+
assertFalse(
|
|
1086
|
+
fn.mock?.calls.length !== void 0 && fn.mock.calls.length >= 1 && fn.mock.calls[0].arguments && fn.mock.calls[0].arguments.length >= matches.length && matches.every(
|
|
1087
|
+
(match, index) => match(fn.mock.calls[0].arguments[index])
|
|
1088
|
+
)
|
|
1089
|
+
);
|
|
1090
|
+
}
|
|
1091
|
+
};
|
|
1092
|
+
}
|
|
1093
|
+
var assertThatArray = (array) => {
|
|
1094
|
+
return {
|
|
1095
|
+
isEmpty: () => assertEqual(array.length, 0),
|
|
1096
|
+
isNotEmpty: () => assertNotEqual(array.length, 0),
|
|
1097
|
+
hasSize: (length) => assertEqual(array.length, length),
|
|
1098
|
+
containsElements: (...other) => {
|
|
1099
|
+
assertTrue(other.every((ts) => other.some((o) => deepEquals(ts, o))));
|
|
1100
|
+
},
|
|
1101
|
+
containsExactlyInAnyOrder: (...other) => {
|
|
1102
|
+
assertEqual(array.length, other.length);
|
|
1103
|
+
assertTrue(array.every((ts) => other.some((o) => deepEquals(ts, o))));
|
|
1104
|
+
},
|
|
1105
|
+
containsExactlyInAnyOrderElementsOf: (other) => {
|
|
1106
|
+
assertEqual(array.length, other.length);
|
|
1107
|
+
assertTrue(array.every((ts) => other.some((o) => deepEquals(ts, o))));
|
|
1108
|
+
},
|
|
1109
|
+
containsExactlyElementsOf: (other) => {
|
|
1110
|
+
assertEqual(array.length, other.length);
|
|
1111
|
+
for (let i = 0; i < array.length; i++) {
|
|
1112
|
+
assertTrue(deepEquals(array[i], other[i]));
|
|
1113
|
+
}
|
|
1114
|
+
},
|
|
1115
|
+
containsExactly: (elem) => {
|
|
1116
|
+
assertEqual(array.length, 1);
|
|
1117
|
+
assertTrue(deepEquals(array[0], elem));
|
|
1118
|
+
},
|
|
1119
|
+
contains: (elem) => {
|
|
1120
|
+
assertTrue(array.some((a) => deepEquals(a, elem)));
|
|
1121
|
+
},
|
|
1122
|
+
containsOnlyOnceElementsOf: (other) => {
|
|
1123
|
+
assertTrue(
|
|
1124
|
+
other.map((o) => array.filter((a) => deepEquals(a, o)).length).filter((a) => a === 1).length === other.length
|
|
1125
|
+
);
|
|
1126
|
+
},
|
|
1127
|
+
containsAnyOf: (...other) => {
|
|
1128
|
+
assertTrue(array.some((a) => other.some((o) => deepEquals(a, o))));
|
|
1129
|
+
},
|
|
1130
|
+
allMatch: (matches) => {
|
|
1131
|
+
assertTrue(array.every(matches));
|
|
1132
|
+
},
|
|
1133
|
+
anyMatches: (matches) => {
|
|
1134
|
+
assertTrue(array.some(matches));
|
|
1135
|
+
},
|
|
1136
|
+
allMatchAsync: async (matches) => {
|
|
1137
|
+
for (const item of array) {
|
|
1138
|
+
assertTrue(await matches(item));
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
};
|
|
1142
|
+
};
|
|
1143
|
+
|
|
1144
|
+
// src/testing/deciderSpecification.ts
|
|
1145
|
+
var DeciderSpecification = {
|
|
1146
|
+
for: (decider) => {
|
|
1147
|
+
{
|
|
1148
|
+
return (givenEvents) => {
|
|
1149
|
+
return {
|
|
1150
|
+
when: (command2) => {
|
|
1151
|
+
const handle = () => {
|
|
1152
|
+
const existingEvents = Array.isArray(givenEvents) ? givenEvents : [givenEvents];
|
|
1153
|
+
const currentState = existingEvents.reduce(
|
|
1154
|
+
decider.evolve,
|
|
1155
|
+
decider.initialState()
|
|
1156
|
+
);
|
|
1157
|
+
return decider.decide(command2, currentState);
|
|
1158
|
+
};
|
|
1159
|
+
return {
|
|
1160
|
+
then: (expectedEvents) => {
|
|
1161
|
+
const resultEvents = handle();
|
|
1162
|
+
const resultEventsArray = Array.isArray(resultEvents) ? resultEvents : [resultEvents];
|
|
1163
|
+
const expectedEventsArray = Array.isArray(expectedEvents) ? expectedEvents : [expectedEvents];
|
|
1164
|
+
assertMatches(resultEventsArray, expectedEventsArray);
|
|
1165
|
+
},
|
|
1166
|
+
thenThrows: (...args) => {
|
|
1167
|
+
try {
|
|
1168
|
+
handle();
|
|
1169
|
+
throw new AssertionError("Handler did not fail as expected");
|
|
1170
|
+
} catch (error2) {
|
|
1171
|
+
if (error2 instanceof AssertionError) throw error2;
|
|
1172
|
+
if (args.length === 0) return;
|
|
1173
|
+
if (!isErrorConstructor(args[0])) {
|
|
1174
|
+
assertTrue(
|
|
1175
|
+
args[0](error2),
|
|
1176
|
+
`Error didn't match the error condition: ${error2?.toString()}`
|
|
1177
|
+
);
|
|
1178
|
+
return;
|
|
1179
|
+
}
|
|
1180
|
+
assertTrue(
|
|
1181
|
+
error2 instanceof args[0],
|
|
1182
|
+
`Caught error is not an instance of the expected type: ${error2?.toString()}`
|
|
1183
|
+
);
|
|
1184
|
+
if (args[1]) {
|
|
1185
|
+
assertTrue(
|
|
1186
|
+
args[1](error2),
|
|
1187
|
+
`Error didn't match the error condition: ${error2?.toString()}`
|
|
1188
|
+
);
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
};
|
|
1193
|
+
}
|
|
1194
|
+
};
|
|
1195
|
+
};
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
};
|
|
1199
|
+
|
|
1200
|
+
// src/testing/wrapEventStore.ts
|
|
1201
|
+
var WrapEventStore = (eventStore) => {
|
|
1202
|
+
const appendedEvents = /* @__PURE__ */ new Map();
|
|
1203
|
+
return {
|
|
1204
|
+
async aggregateStream(streamName, options) {
|
|
1205
|
+
return eventStore.aggregateStream(streamName, options);
|
|
1206
|
+
},
|
|
1207
|
+
readStream(streamName, options) {
|
|
1208
|
+
return eventStore.readStream(streamName, options);
|
|
1209
|
+
},
|
|
1210
|
+
appendToStream: async (streamName, events, options) => {
|
|
1211
|
+
const result = await eventStore.appendToStream(
|
|
1212
|
+
streamName,
|
|
1213
|
+
events,
|
|
1214
|
+
options
|
|
1215
|
+
);
|
|
1216
|
+
const currentStream = appendedEvents.get(streamName) ?? [streamName, []];
|
|
1217
|
+
appendedEvents.set(streamName, [
|
|
1218
|
+
streamName,
|
|
1219
|
+
[...currentStream[1], ...events]
|
|
1220
|
+
]);
|
|
1221
|
+
return result;
|
|
1222
|
+
},
|
|
1223
|
+
appendedEvents,
|
|
1224
|
+
setup: async (streamName, events) => {
|
|
1225
|
+
return eventStore.appendToStream(streamName, events);
|
|
1226
|
+
}
|
|
1227
|
+
// streamEvents: (): ReadableStream<
|
|
1228
|
+
// // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
1229
|
+
// ReadEvent<Event, ReadEventMetadataType> | GlobalSubscriptionEvent
|
|
1230
|
+
// > => {
|
|
1231
|
+
// return eventStore.streamEvents();
|
|
1232
|
+
// },
|
|
1233
|
+
};
|
|
1234
|
+
};
|
|
1235
|
+
export {
|
|
1236
|
+
AssertionError,
|
|
1237
|
+
BinaryJsonDecoder,
|
|
1238
|
+
CaughtUpTransformStream,
|
|
1239
|
+
CommandHandler,
|
|
1240
|
+
CommandHandlerStreamVersionConflictRetryOptions,
|
|
1241
|
+
CompositeDecoder,
|
|
1242
|
+
ConcurrencyError,
|
|
1243
|
+
DeciderCommandHandler,
|
|
1244
|
+
DeciderSpecification,
|
|
1245
|
+
DefaultDecoder,
|
|
1246
|
+
EmmettError,
|
|
1247
|
+
ExpectedVersionConflictError,
|
|
1248
|
+
GlobalStreamCaughtUpType,
|
|
1249
|
+
IllegalStateError,
|
|
1250
|
+
InMemoryEventStoreDefaultStreamVersion,
|
|
1251
|
+
JSONParser,
|
|
1252
|
+
JsonDecoder,
|
|
1253
|
+
NO_CONCURRENCY_CHECK,
|
|
1254
|
+
NoRetries,
|
|
1255
|
+
NotFoundError,
|
|
1256
|
+
ObjectDecoder,
|
|
1257
|
+
ParseError,
|
|
1258
|
+
STREAM_DOES_NOT_EXIST,
|
|
1259
|
+
STREAM_EXISTS,
|
|
1260
|
+
StreamingCoordinator,
|
|
1261
|
+
StringDecoder,
|
|
1262
|
+
ValidationError,
|
|
1263
|
+
ValidationErrors,
|
|
1264
|
+
WrapEventStore,
|
|
1265
|
+
accept,
|
|
1266
|
+
argMatches,
|
|
1267
|
+
argValue,
|
|
1268
|
+
assertDeepEqual,
|
|
1269
|
+
assertEqual,
|
|
1270
|
+
assertExpectedVersionMatchesCurrent,
|
|
1271
|
+
assertFails,
|
|
1272
|
+
assertFalse,
|
|
1273
|
+
assertIsNotNull,
|
|
1274
|
+
assertIsNull,
|
|
1275
|
+
assertMatches,
|
|
1276
|
+
assertNotDeepEqual,
|
|
1277
|
+
assertNotEmptyString,
|
|
1278
|
+
assertNotEqual,
|
|
1279
|
+
assertOk,
|
|
1280
|
+
assertPositiveNumber,
|
|
1281
|
+
assertRejects,
|
|
1282
|
+
assertThat,
|
|
1283
|
+
assertThatArray,
|
|
1284
|
+
assertThrows,
|
|
1285
|
+
assertThrowsAsync,
|
|
1286
|
+
assertTrue,
|
|
1287
|
+
assertUnsignedBigInt,
|
|
1288
|
+
asyncProjections,
|
|
1289
|
+
asyncRetry,
|
|
1290
|
+
canCreateEventStoreSession,
|
|
1291
|
+
caughtUpEventFrom,
|
|
1292
|
+
collect,
|
|
1293
|
+
command,
|
|
1294
|
+
complete,
|
|
1295
|
+
concatUint8Arrays,
|
|
1296
|
+
deepEquals,
|
|
1297
|
+
error,
|
|
1298
|
+
event,
|
|
1299
|
+
formatDateToUtcYYYYMMDD,
|
|
1300
|
+
getInMemoryEventStore,
|
|
1301
|
+
getInMemoryMessageBus,
|
|
1302
|
+
globalStreamCaughtUp,
|
|
1303
|
+
ignore,
|
|
1304
|
+
inlineProjections,
|
|
1305
|
+
isEquatable,
|
|
1306
|
+
isErrorConstructor,
|
|
1307
|
+
isExpectedVersionConflictError,
|
|
1308
|
+
isGlobalStreamCaughtUp,
|
|
1309
|
+
isNotInternalEvent,
|
|
1310
|
+
isNumber,
|
|
1311
|
+
isPluginConfig,
|
|
1312
|
+
isString,
|
|
1313
|
+
isSubscriptionEvent,
|
|
1314
|
+
isSubset,
|
|
1315
|
+
isValidYYYYMMDD,
|
|
1316
|
+
matchesExpectedVersion,
|
|
1317
|
+
merge,
|
|
1318
|
+
nulloSessionFactory,
|
|
1319
|
+
parseDateFromUtcYYYYMMDD,
|
|
1320
|
+
projection,
|
|
1321
|
+
projections,
|
|
1322
|
+
publish,
|
|
1323
|
+
reply,
|
|
1324
|
+
restream,
|
|
1325
|
+
schedule,
|
|
1326
|
+
send,
|
|
1327
|
+
streamGenerators,
|
|
1328
|
+
streamTrackingGlobalPosition,
|
|
1329
|
+
streamTransformations,
|
|
1330
|
+
sum,
|
|
1331
|
+
verifyThat
|
|
1332
|
+
};
|
|
17
1333
|
//# sourceMappingURL=index.js.map
|