@event-driven-io/emmett-expressjs 0.43.0-apha.2 → 0.43.0-beta.10

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/index.cjs CHANGED
@@ -38,38 +38,638 @@ var EmmettError = (_class = class _EmmettError extends Error {
38
38
  return typeof error === "object" && error !== null && "errorCode" in error && isNumber(error.errorCode) && (errorCode === void 0 || error.errorCode === errorCode);
39
39
  }
40
40
  }, _class.__initStatic(), _class);
41
+ var ConcurrencyError = class _ConcurrencyError extends EmmettError {
42
+ constructor(current, expected, message) {
43
+ super({
44
+ errorCode: EmmettError.Codes.ConcurrencyError,
45
+ message: _nullishCoalesce(message, () => ( `Expected version ${expected.toString()} does not match current ${_optionalChain([current, 'optionalAccess', _ => _.toString, 'call', _2 => _2()])}`))
46
+ });
47
+ this.current = current;
48
+ this.expected = expected;
49
+ Object.setPrototypeOf(this, _ConcurrencyError.prototype);
50
+ }
51
+ };
52
+ var ConcurrencyInMemoryDatabaseError = class _ConcurrencyInMemoryDatabaseError extends EmmettError {
53
+ constructor(message) {
54
+ super({
55
+ errorCode: EmmettError.Codes.ConcurrencyError,
56
+ message: _nullishCoalesce(message, () => ( `Expected document state does not match current one!`))
57
+ });
58
+ Object.setPrototypeOf(this, _ConcurrencyInMemoryDatabaseError.prototype);
59
+ }
60
+ };
41
61
 
42
62
  // ../emmett/dist/index.js
43
63
  var _uuid = require('uuid');
44
64
 
65
+
45
66
  var _asyncretry = require('async-retry'); var _asyncretry2 = _interopRequireDefault(_asyncretry);
46
67
 
47
68
 
69
+
70
+ async function tryPublishMessagesAfterCommit(messages, options, context) {
71
+ if (_optionalChain([options, 'optionalAccess', _3 => _3.onAfterCommit]) === void 0) return false;
72
+ try {
73
+ await _optionalChain([options, 'optionalAccess', _4 => _4.onAfterCommit, 'call', _5 => _5(messages, context)]);
74
+ return true;
75
+ } catch (error) {
76
+ console.error(`Error in on after commit hook`, error);
77
+ return false;
78
+ }
79
+ }
48
80
  var emmettPrefix = "emt";
49
81
  var defaultTag = `${emmettPrefix}:default`;
50
82
  var unknownTag = `${emmettPrefix}:unknown`;
51
- var ParseError = class extends Error {
52
- constructor(text) {
53
- super(`Cannot parse! ${text}`);
83
+ var STREAM_EXISTS = "STREAM_EXISTS";
84
+ var STREAM_DOES_NOT_EXIST = "STREAM_DOES_NOT_EXIST";
85
+ var NO_CONCURRENCY_CHECK = "NO_CONCURRENCY_CHECK";
86
+ var matchesExpectedVersion = (current, expected, defaultVersion) => {
87
+ if (expected === NO_CONCURRENCY_CHECK) return true;
88
+ if (expected == STREAM_DOES_NOT_EXIST) return current === defaultVersion;
89
+ if (expected == STREAM_EXISTS) return current !== defaultVersion;
90
+ return current === expected;
91
+ };
92
+ var assertExpectedVersionMatchesCurrent = (current, expected, defaultVersion) => {
93
+ expected ??= NO_CONCURRENCY_CHECK;
94
+ if (!matchesExpectedVersion(current, expected, defaultVersion))
95
+ throw new ExpectedVersionConflictError(current, expected);
96
+ };
97
+ var ExpectedVersionConflictError = class _ExpectedVersionConflictError extends ConcurrencyError {
98
+ constructor(current, expected) {
99
+ super(_optionalChain([current, 'optionalAccess', _6 => _6.toString, 'call', _7 => _7()]), _optionalChain([expected, 'optionalAccess', _8 => _8.toString, 'call', _9 => _9()]));
100
+ Object.setPrototypeOf(this, _ExpectedVersionConflictError.prototype);
54
101
  }
55
102
  };
56
- var JSONParser = {
57
- stringify: (value, options) => {
58
- return JSON.stringify(
59
- _optionalChain([options, 'optionalAccess', _2 => _2.map]) ? options.map(value) : value,
60
- //TODO: Consider adding support to DateTime and adding specific format to mark that's a bigint
61
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
62
- (_, v) => typeof v === "bigint" ? v.toString() : v
63
- );
64
- },
65
- parse: (text, options) => {
66
- const parsed = JSON.parse(text, _optionalChain([options, 'optionalAccess', _3 => _3.reviver]));
67
- if (_optionalChain([options, 'optionalAccess', _4 => _4.typeCheck]) && !_optionalChain([options, 'optionalAccess', _5 => _5.typeCheck, 'call', _6 => _6(parsed)]))
68
- throw new ParseError(text);
69
- return _optionalChain([options, 'optionalAccess', _7 => _7.map]) ? options.map(parsed) : parsed;
103
+ var isPrimitive = (value) => {
104
+ const type = typeof value;
105
+ return value === null || value === void 0 || type === "boolean" || type === "number" || type === "string" || type === "symbol" || type === "bigint";
106
+ };
107
+ var compareArrays = (left, right) => {
108
+ if (left.length !== right.length) {
109
+ return false;
110
+ }
111
+ for (let i = 0; i < left.length; i++) {
112
+ const leftHas = i in left;
113
+ const rightHas = i in right;
114
+ if (leftHas !== rightHas) return false;
115
+ if (leftHas && !deepEquals(left[i], right[i])) return false;
70
116
  }
117
+ return true;
118
+ };
119
+ var compareDates = (left, right) => {
120
+ return left.getTime() === right.getTime();
71
121
  };
122
+ var compareRegExps = (left, right) => {
123
+ return left.toString() === right.toString();
124
+ };
125
+ var compareErrors = (left, right) => {
126
+ if (left.message !== right.message || left.name !== right.name) {
127
+ return false;
128
+ }
129
+ const leftKeys = Object.keys(left);
130
+ const rightKeys = Object.keys(right);
131
+ if (leftKeys.length !== rightKeys.length) return false;
132
+ const rightKeySet = new Set(rightKeys);
133
+ for (const key of leftKeys) {
134
+ if (!rightKeySet.has(key)) return false;
135
+ if (!deepEquals(left[key], right[key])) return false;
136
+ }
137
+ return true;
138
+ };
139
+ var compareMaps = (left, right) => {
140
+ if (left.size !== right.size) return false;
141
+ for (const [key, value] of left) {
142
+ if (isPrimitive(key)) {
143
+ if (!right.has(key) || !deepEquals(value, right.get(key))) {
144
+ return false;
145
+ }
146
+ } else {
147
+ let found = false;
148
+ for (const [rightKey, rightValue] of right) {
149
+ if (deepEquals(key, rightKey) && deepEquals(value, rightValue)) {
150
+ found = true;
151
+ break;
152
+ }
153
+ }
154
+ if (!found) return false;
155
+ }
156
+ }
157
+ return true;
158
+ };
159
+ var compareSets = (left, right) => {
160
+ if (left.size !== right.size) return false;
161
+ for (const leftItem of left) {
162
+ if (isPrimitive(leftItem)) {
163
+ if (!right.has(leftItem)) return false;
164
+ } else {
165
+ let found = false;
166
+ for (const rightItem of right) {
167
+ if (deepEquals(leftItem, rightItem)) {
168
+ found = true;
169
+ break;
170
+ }
171
+ }
172
+ if (!found) return false;
173
+ }
174
+ }
175
+ return true;
176
+ };
177
+ var compareArrayBuffers = (left, right) => {
178
+ if (left.byteLength !== right.byteLength) return false;
179
+ const leftView = new Uint8Array(left);
180
+ const rightView = new Uint8Array(right);
181
+ for (let i = 0; i < leftView.length; i++) {
182
+ if (leftView[i] !== rightView[i]) return false;
183
+ }
184
+ return true;
185
+ };
186
+ var compareTypedArrays = (left, right) => {
187
+ if (left.constructor !== right.constructor) return false;
188
+ if (left.byteLength !== right.byteLength) return false;
189
+ const leftArray = new Uint8Array(
190
+ left.buffer,
191
+ left.byteOffset,
192
+ left.byteLength
193
+ );
194
+ const rightArray = new Uint8Array(
195
+ right.buffer,
196
+ right.byteOffset,
197
+ right.byteLength
198
+ );
199
+ for (let i = 0; i < leftArray.length; i++) {
200
+ if (leftArray[i] !== rightArray[i]) return false;
201
+ }
202
+ return true;
203
+ };
204
+ var compareObjects = (left, right) => {
205
+ const keys1 = Object.keys(left);
206
+ const keys2 = Object.keys(right);
207
+ if (keys1.length !== keys2.length) {
208
+ return false;
209
+ }
210
+ for (const key of keys1) {
211
+ if (left[key] instanceof Function && right[key] instanceof Function) {
212
+ continue;
213
+ }
214
+ const isEqual = deepEquals(left[key], right[key]);
215
+ if (!isEqual) {
216
+ return false;
217
+ }
218
+ }
219
+ return true;
220
+ };
221
+ var getType = (value) => {
222
+ if (value === null) return "null";
223
+ if (value === void 0) return "undefined";
224
+ const primitiveType = typeof value;
225
+ if (primitiveType !== "object") return primitiveType;
226
+ if (Array.isArray(value)) return "array";
227
+ if (value instanceof Boolean) return "boxed-boolean";
228
+ if (value instanceof Number) return "boxed-number";
229
+ if (value instanceof String) return "boxed-string";
230
+ if (value instanceof Date) return "date";
231
+ if (value instanceof RegExp) return "regexp";
232
+ if (value instanceof Error) return "error";
233
+ if (value instanceof Map) return "map";
234
+ if (value instanceof Set) return "set";
235
+ if (value instanceof ArrayBuffer) return "arraybuffer";
236
+ if (value instanceof DataView) return "dataview";
237
+ if (value instanceof WeakMap) return "weakmap";
238
+ if (value instanceof WeakSet) return "weakset";
239
+ if (ArrayBuffer.isView(value)) return "typedarray";
240
+ return "object";
241
+ };
242
+ var deepEquals = (left, right) => {
243
+ if (left === right) return true;
244
+ if (isEquatable(left)) {
245
+ return left.equals(right);
246
+ }
247
+ const leftType = getType(left);
248
+ const rightType = getType(right);
249
+ if (leftType !== rightType) return false;
250
+ switch (leftType) {
251
+ case "null":
252
+ case "undefined":
253
+ case "boolean":
254
+ case "number":
255
+ case "bigint":
256
+ case "string":
257
+ case "symbol":
258
+ case "function":
259
+ return left === right;
260
+ case "array":
261
+ return compareArrays(left, right);
262
+ case "date":
263
+ return compareDates(left, right);
264
+ case "regexp":
265
+ return compareRegExps(left, right);
266
+ case "error":
267
+ return compareErrors(left, right);
268
+ case "map":
269
+ return compareMaps(
270
+ left,
271
+ right
272
+ );
273
+ case "set":
274
+ return compareSets(left, right);
275
+ case "arraybuffer":
276
+ return compareArrayBuffers(left, right);
277
+ case "dataview":
278
+ case "weakmap":
279
+ case "weakset":
280
+ return false;
281
+ case "typedarray":
282
+ return compareTypedArrays(
283
+ left,
284
+ right
285
+ );
286
+ case "boxed-boolean":
287
+ return left.valueOf() === right.valueOf();
288
+ case "boxed-number":
289
+ return left.valueOf() === right.valueOf();
290
+ case "boxed-string":
291
+ return left.valueOf() === right.valueOf();
292
+ case "object":
293
+ return compareObjects(
294
+ left,
295
+ right
296
+ );
297
+ default:
298
+ return false;
299
+ }
300
+ };
301
+ var isEquatable = (left) => {
302
+ return left !== null && left !== void 0 && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
303
+ };
304
+ var toNormalizedString = (value) => value.toString().padStart(19, "0");
305
+ var bigInt = {
306
+ toNormalizedString
307
+ };
308
+ var bigIntReplacer = (_key, value) => {
309
+ return typeof value === "bigint" ? value.toString() : value;
310
+ };
311
+ var dateReplacer = (_key, value) => {
312
+ return value instanceof Date ? value.toISOString() : value;
313
+ };
314
+ var isFirstLetterNumeric = (str) => {
315
+ const c = str.charCodeAt(0);
316
+ return c >= 48 && c <= 57;
317
+ };
318
+ var isFirstLetterNumericOrMinus = (str) => {
319
+ const c = str.charCodeAt(0);
320
+ return c >= 48 && c <= 57 || c === 45;
321
+ };
322
+ var bigIntReviver = (_key, value, context) => {
323
+ if (typeof value === "number" && Number.isInteger(value) && !Number.isSafeInteger(value)) {
324
+ try {
325
+ return BigInt(_nullishCoalesce(_optionalChain([context, 'optionalAccess', _10 => _10.source]), () => ( value.toString())));
326
+ } catch (e2) {
327
+ return value;
328
+ }
329
+ }
330
+ if (typeof value === "string" && value.length > 15) {
331
+ if (isFirstLetterNumericOrMinus(value)) {
332
+ const num = Number(value);
333
+ if (Number.isFinite(num) && !Number.isSafeInteger(num)) {
334
+ try {
335
+ return BigInt(value);
336
+ } catch (e3) {
337
+ }
338
+ }
339
+ }
340
+ }
341
+ return value;
342
+ };
343
+ var dateReviver = (_key, value) => {
344
+ if (typeof value === "string" && value.length === 24 && isFirstLetterNumeric(value) && value[10] === "T" && value[23] === "Z") {
345
+ const date = new Date(value);
346
+ if (!isNaN(date.getTime())) {
347
+ return date;
348
+ }
349
+ }
350
+ return value;
351
+ };
352
+ var composeJSONReplacers = (...replacers) => {
353
+ const filteredReplacers = replacers.filter((r) => r !== void 0);
354
+ if (filteredReplacers.length === 0) return void 0;
355
+ return (key, value) => (
356
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
357
+ filteredReplacers.reduce(
358
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
359
+ (accValue, replacer) => replacer(key, accValue),
360
+ value
361
+ )
362
+ );
363
+ };
364
+ var composeJSONRevivers = (...revivers) => {
365
+ const filteredRevivers = revivers.filter((r) => r !== void 0);
366
+ if (filteredRevivers.length === 0) return void 0;
367
+ return (key, value, context) => (
368
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
369
+ filteredRevivers.reduce(
370
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
371
+ (accValue, reviver) => reviver(key, accValue, context),
372
+ value
373
+ )
374
+ );
375
+ };
376
+ var JSONReplacer = (opts) => composeJSONReplacers(
377
+ _optionalChain([opts, 'optionalAccess', _11 => _11.replacer]),
378
+ _optionalChain([opts, 'optionalAccess', _12 => _12.failOnBigIntSerialization]) !== true ? JSONReplacers.bigInt : void 0,
379
+ _optionalChain([opts, 'optionalAccess', _13 => _13.useDefaultDateSerialization]) !== true ? JSONReplacers.date : void 0
380
+ );
381
+ var JSONReviver = (opts) => composeJSONRevivers(
382
+ _optionalChain([opts, 'optionalAccess', _14 => _14.reviver]),
383
+ _optionalChain([opts, 'optionalAccess', _15 => _15.parseBigInts]) === true ? JSONRevivers.bigInt : void 0,
384
+ _optionalChain([opts, 'optionalAccess', _16 => _16.parseDates]) === true ? JSONRevivers.date : void 0
385
+ );
386
+ var JSONReplacers = {
387
+ bigInt: bigIntReplacer,
388
+ date: dateReplacer
389
+ };
390
+ var JSONRevivers = {
391
+ bigInt: bigIntReviver,
392
+ date: dateReviver
393
+ };
394
+ var jsonSerializer = (options) => {
395
+ const defaultReplacer = JSONReplacer(options);
396
+ const defaultReviver = JSONReviver(options);
397
+ return {
398
+ serialize: (object, serializerOptions) => JSON.stringify(
399
+ object,
400
+ serializerOptions ? JSONReplacer(serializerOptions) : defaultReplacer
401
+ ),
402
+ deserialize: (payload, deserializerOptions) => JSON.parse(
403
+ payload,
404
+ deserializerOptions ? JSONReviver(deserializerOptions) : defaultReviver
405
+ )
406
+ };
407
+ };
408
+ var JSONSerializer = Object.assign(jsonSerializer(), {
409
+ from: (options) => _nullishCoalesce(_optionalChain([options, 'optionalAccess', _17 => _17.serialization, 'optionalAccess', _18 => _18.serializer]), () => ( (_optionalChain([options, 'optionalAccess', _19 => _19.serialization, 'optionalAccess', _20 => _20.options]) ? jsonSerializer(_optionalChain([options, 'optionalAccess', _21 => _21.serialization, 'optionalAccess', _22 => _22.options])) : JSONSerializer)))
410
+ });
72
411
  var textEncoder = new TextEncoder();
412
+ var isGeneralExpectedDocumentVersion = (version) => {
413
+ return version === "DOCUMENT_DOES_NOT_EXIST" || version === "DOCUMENT_EXISTS" || version === "NO_CONCURRENCY_CHECK";
414
+ };
415
+ var expectedVersionValue = (version) => version === void 0 || isGeneralExpectedDocumentVersion(version) ? null : version;
416
+ var operationResult = (result, options) => {
417
+ const operationResult2 = {
418
+ ...result,
419
+ acknowledged: true,
420
+ successful: result.successful,
421
+ assertSuccessful: (errorMessage) => {
422
+ const { successful } = result;
423
+ const { operationName, collectionName } = options;
424
+ if (!successful)
425
+ throw new ConcurrencyInMemoryDatabaseError(
426
+ _nullishCoalesce(errorMessage, () => ( `${operationName} on ${collectionName} failed. Expected document state does not match current one! Result: ${JSONSerializer.serialize(result)}!`))
427
+ );
428
+ }
429
+ };
430
+ if (_optionalChain([options, 'access', _23 => _23.errors, 'optionalAccess', _24 => _24.throwOnOperationFailures]))
431
+ operationResult2.assertSuccessful();
432
+ return operationResult2;
433
+ };
434
+ var getInMemoryDatabase = () => {
435
+ const storage = /* @__PURE__ */ new Map();
436
+ return {
437
+ collection: (collectionName, collectionOptions = {}) => {
438
+ const ensureCollectionCreated = () => {
439
+ if (!storage.has(collectionName)) storage.set(collectionName, []);
440
+ };
441
+ const errors = collectionOptions.errors;
442
+ const collection = {
443
+ collectionName,
444
+ insertOne: async (document) => {
445
+ ensureCollectionCreated();
446
+ const _id = _nullishCoalesce(document._id, () => ( _uuid.v7.call(void 0, )));
447
+ const _version = _nullishCoalesce(document._version, () => ( 1n));
448
+ const existing = await collection.findOne((c) => c._id === _id);
449
+ if (existing) {
450
+ return operationResult(
451
+ {
452
+ successful: false,
453
+ insertedId: null,
454
+ nextExpectedVersion: _version
455
+ },
456
+ { operationName: "insertOne", collectionName, errors }
457
+ );
458
+ }
459
+ const documentsInCollection = storage.get(collectionName);
460
+ const newDocument = { ...document, _id, _version };
461
+ const newCollection = [...documentsInCollection, newDocument];
462
+ storage.set(collectionName, newCollection);
463
+ return operationResult(
464
+ {
465
+ successful: true,
466
+ insertedId: _id,
467
+ nextExpectedVersion: _version
468
+ },
469
+ { operationName: "insertOne", collectionName, errors }
470
+ );
471
+ },
472
+ findOne: (predicate) => {
473
+ ensureCollectionCreated();
474
+ const documentsInCollection = storage.get(collectionName);
475
+ const filteredDocuments = predicate ? _optionalChain([documentsInCollection, 'optionalAccess', _25 => _25.filter, 'call', _26 => _26((doc) => predicate(doc))]) : documentsInCollection;
476
+ const firstOne = _nullishCoalesce(_optionalChain([filteredDocuments, 'optionalAccess', _27 => _27[0]]), () => ( null));
477
+ return Promise.resolve(firstOne);
478
+ },
479
+ find: (predicate) => {
480
+ ensureCollectionCreated();
481
+ const documentsInCollection = storage.get(collectionName);
482
+ const filteredDocuments = predicate ? _optionalChain([documentsInCollection, 'optionalAccess', _28 => _28.filter, 'call', _29 => _29((doc) => predicate(doc))]) : documentsInCollection;
483
+ return Promise.resolve(filteredDocuments);
484
+ },
485
+ deleteOne: (predicate) => {
486
+ ensureCollectionCreated();
487
+ const documentsInCollection = storage.get(collectionName);
488
+ if (predicate) {
489
+ const foundIndex = documentsInCollection.findIndex(
490
+ (doc) => predicate(doc)
491
+ );
492
+ if (foundIndex === -1) {
493
+ return Promise.resolve(
494
+ operationResult(
495
+ {
496
+ successful: false,
497
+ matchedCount: 0,
498
+ deletedCount: 0
499
+ },
500
+ { operationName: "deleteOne", collectionName, errors }
501
+ )
502
+ );
503
+ } else {
504
+ const newCollection2 = documentsInCollection.toSpliced(
505
+ foundIndex,
506
+ 1
507
+ );
508
+ storage.set(collectionName, newCollection2);
509
+ return Promise.resolve(
510
+ operationResult(
511
+ {
512
+ successful: true,
513
+ matchedCount: 1,
514
+ deletedCount: 1
515
+ },
516
+ { operationName: "deleteOne", collectionName, errors }
517
+ )
518
+ );
519
+ }
520
+ }
521
+ const newCollection = documentsInCollection.slice(1);
522
+ storage.set(collectionName, newCollection);
523
+ return Promise.resolve(
524
+ operationResult(
525
+ {
526
+ successful: true,
527
+ matchedCount: 1,
528
+ deletedCount: 1
529
+ },
530
+ { operationName: "deleteOne", collectionName, errors }
531
+ )
532
+ );
533
+ },
534
+ replaceOne: (predicate, document, options) => {
535
+ ensureCollectionCreated();
536
+ const documentsInCollection = storage.get(collectionName);
537
+ const firstIndex = documentsInCollection.findIndex(
538
+ (doc) => predicate(doc)
539
+ );
540
+ if (firstIndex === void 0 || firstIndex === -1) {
541
+ return Promise.resolve(
542
+ operationResult(
543
+ {
544
+ successful: false,
545
+ matchedCount: 0,
546
+ modifiedCount: 0,
547
+ nextExpectedVersion: 0n
548
+ },
549
+ { operationName: "replaceOne", collectionName, errors }
550
+ )
551
+ );
552
+ }
553
+ const existing = documentsInCollection[firstIndex];
554
+ if (typeof _optionalChain([options, 'optionalAccess', _30 => _30.expectedVersion]) === "bigint" && existing._version !== options.expectedVersion) {
555
+ return Promise.resolve(
556
+ operationResult(
557
+ {
558
+ successful: false,
559
+ matchedCount: 1,
560
+ modifiedCount: 0,
561
+ nextExpectedVersion: existing._version
562
+ },
563
+ { operationName: "replaceOne", collectionName, errors }
564
+ )
565
+ );
566
+ }
567
+ const newVersion = existing._version + 1n;
568
+ const newCollection = documentsInCollection.with(firstIndex, {
569
+ _id: existing._id,
570
+ ...document,
571
+ _version: newVersion
572
+ });
573
+ storage.set(collectionName, newCollection);
574
+ return Promise.resolve(
575
+ operationResult(
576
+ {
577
+ successful: true,
578
+ modifiedCount: 1,
579
+ matchedCount: firstIndex,
580
+ nextExpectedVersion: newVersion
581
+ },
582
+ { operationName: "replaceOne", collectionName, errors }
583
+ )
584
+ );
585
+ },
586
+ handle: async (id, handle, options) => {
587
+ const { expectedVersion: version, ...operationOptions } = _nullishCoalesce(options, () => ( {}));
588
+ ensureCollectionCreated();
589
+ const existing = await collection.findOne(({ _id }) => _id === id);
590
+ const expectedVersion = expectedVersionValue(version);
591
+ if (existing == null && version === "DOCUMENT_EXISTS" || existing == null && expectedVersion != null || existing != null && version === "DOCUMENT_DOES_NOT_EXIST" || existing != null && expectedVersion !== null && existing._version !== expectedVersion) {
592
+ return operationResult(
593
+ {
594
+ successful: false,
595
+ document: existing
596
+ },
597
+ { operationName: "handle", collectionName, errors }
598
+ );
599
+ }
600
+ const result = handle(existing !== null ? { ...existing } : null);
601
+ if (deepEquals(existing, result))
602
+ return operationResult(
603
+ {
604
+ successful: true,
605
+ document: existing
606
+ },
607
+ { operationName: "handle", collectionName, errors }
608
+ );
609
+ if (!existing && result) {
610
+ const newDoc = { ...result, _id: id };
611
+ const insertResult = await collection.insertOne({
612
+ ...newDoc,
613
+ _id: id
614
+ });
615
+ return {
616
+ ...insertResult,
617
+ document: {
618
+ ...newDoc,
619
+ _version: insertResult.nextExpectedVersion
620
+ }
621
+ };
622
+ }
623
+ if (existing && !result) {
624
+ const deleteResult = await collection.deleteOne(
625
+ ({ _id }) => id === _id
626
+ );
627
+ return { ...deleteResult, document: null };
628
+ }
629
+ if (existing && result) {
630
+ const replaceResult = await collection.replaceOne(
631
+ ({ _id }) => id === _id,
632
+ result,
633
+ {
634
+ ...operationOptions,
635
+ expectedVersion: _nullishCoalesce(expectedVersion, () => ( "DOCUMENT_EXISTS"))
636
+ }
637
+ );
638
+ return {
639
+ ...replaceResult,
640
+ document: {
641
+ ...result,
642
+ _version: replaceResult.nextExpectedVersion
643
+ }
644
+ };
645
+ }
646
+ return operationResult(
647
+ {
648
+ successful: true,
649
+ document: existing
650
+ },
651
+ { operationName: "handle", collectionName, errors }
652
+ );
653
+ }
654
+ };
655
+ return collection;
656
+ }
657
+ };
658
+ };
659
+ var bigIntProcessorCheckpoint = (value) => bigInt.toNormalizedString(value);
660
+ var handleInMemoryProjections = async (options) => {
661
+ const { projections: projections2, events, database, eventStore } = options;
662
+ const eventTypes = events.map((e) => e.type);
663
+ const relevantProjections = projections2.filter(
664
+ (p) => p.canHandle.some((type) => eventTypes.includes(type))
665
+ );
666
+ for (const projection2 of relevantProjections) {
667
+ await projection2.handle(events, {
668
+ eventStore,
669
+ database
670
+ });
671
+ }
672
+ };
73
673
  var AssertionError = class extends Error {
74
674
  constructor(message2) {
75
675
  super(message2);
@@ -81,7 +681,7 @@ var isSubset = (superObj, subObj) => {
81
681
  assertOk(sup);
82
682
  assertOk(sub);
83
683
  return Object.keys(sub).every((ele) => {
84
- if (typeof sub[ele] == "object") {
684
+ if (sub[ele] !== null && typeof sub[ele] == "object") {
85
685
  return isSubset(sup[ele], sub[ele]);
86
686
  }
87
687
  return sub[ele] === sup[ele];
@@ -94,9 +694,9 @@ var assertMatches = (actual, expected, message2) => {
94
694
  if (!isSubset(actual, expected))
95
695
  throw new AssertionError(
96
696
  _nullishCoalesce(message2, () => ( `subObj:
97
- ${JSONParser.stringify(expected)}
697
+ ${JSONSerializer.serialize(expected)}
98
698
  is not subset of
99
- ${JSONParser.stringify(actual)}`))
699
+ ${JSONSerializer.serialize(actual)}`))
100
700
  );
101
701
  };
102
702
  function assertOk(obj, message2) {
@@ -106,8 +706,8 @@ function assertEqual(expected, actual, message2) {
106
706
  if (expected !== actual)
107
707
  throw new AssertionError(
108
708
  `${_nullishCoalesce(message2, () => ( "Objects are not equal"))}:
109
- Expected: ${JSONParser.stringify(expected)}
110
- Actual: ${JSONParser.stringify(actual)}`
709
+ Expected: ${JSONSerializer.serialize(expected)}
710
+ Actual: ${JSONSerializer.serialize(actual)}`
111
711
  );
112
712
  }
113
713
  var WrapEventStore = (eventStore) => {
@@ -149,6 +749,161 @@ var WrapEventStore = (eventStore) => {
149
749
  };
150
750
  return wrapped;
151
751
  };
752
+ var downcastRecordedMessage = (recordedMessage, options) => {
753
+ if (!_optionalChain([options, 'optionalAccess', _31 => _31.downcast]))
754
+ return recordedMessage;
755
+ const downcasted = options.downcast(
756
+ recordedMessage
757
+ );
758
+ return {
759
+ ...recordedMessage,
760
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
761
+ data: downcasted.data,
762
+ ..."metadata" in recordedMessage || "metadata" in downcasted ? {
763
+ metadata: {
764
+ ..."metadata" in recordedMessage ? recordedMessage.metadata : {},
765
+ ..."metadata" in downcasted ? downcasted.metadata : {}
766
+ }
767
+ } : {}
768
+ };
769
+ };
770
+ var downcastRecordedMessages = (recordedMessages, options) => {
771
+ if (!_optionalChain([options, 'optionalAccess', _32 => _32.downcast]))
772
+ return recordedMessages;
773
+ return recordedMessages.map(
774
+ (recordedMessage) => downcastRecordedMessage(recordedMessage, options)
775
+ );
776
+ };
777
+ var upcastRecordedMessage = (recordedMessage, options) => {
778
+ if (!_optionalChain([options, 'optionalAccess', _33 => _33.upcast]))
779
+ return recordedMessage;
780
+ const upcasted = options.upcast(
781
+ recordedMessage
782
+ );
783
+ return {
784
+ ...recordedMessage,
785
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
786
+ data: upcasted.data,
787
+ ..."metadata" in recordedMessage || "metadata" in upcasted ? {
788
+ metadata: {
789
+ ..."metadata" in recordedMessage ? recordedMessage.metadata : {},
790
+ ..."metadata" in upcasted ? upcasted.metadata : {}
791
+ }
792
+ } : {}
793
+ };
794
+ };
795
+ var upcastRecordedMessages = (recordedMessages, options) => {
796
+ if (!_optionalChain([options, 'optionalAccess', _34 => _34.upcast]))
797
+ return recordedMessages;
798
+ return recordedMessages.map(
799
+ (recordedMessage) => upcastRecordedMessage(recordedMessage, options)
800
+ );
801
+ };
802
+ var InMemoryEventStoreDefaultStreamVersion = 0n;
803
+ var getInMemoryEventStore = (eventStoreOptions) => {
804
+ const streams = /* @__PURE__ */ new Map();
805
+ const getAllEventsCount = () => {
806
+ return Array.from(streams.values()).map((s) => s.length).reduce((p, c) => p + c, 0);
807
+ };
808
+ const database = _optionalChain([eventStoreOptions, 'optionalAccess', _35 => _35.database]) || getInMemoryDatabase();
809
+ const inlineProjections2 = (_nullishCoalesce(_optionalChain([eventStoreOptions, 'optionalAccess', _36 => _36.projections]), () => ( []))).filter(({ type }) => type === "inline").map(({ projection: projection2 }) => projection2);
810
+ const eventStore = {
811
+ database,
812
+ async aggregateStream(streamName, options) {
813
+ const { evolve, initialState, read } = options;
814
+ const result = await this.readStream(
815
+ streamName,
816
+ read
817
+ );
818
+ const events = _nullishCoalesce(_optionalChain([result, 'optionalAccess', _37 => _37.events]), () => ( []));
819
+ const state = events.reduce((s, e) => evolve(s, e), initialState());
820
+ return {
821
+ currentStreamVersion: BigInt(events.length),
822
+ state,
823
+ streamExists: result.streamExists
824
+ };
825
+ },
826
+ readStream: (streamName, readOptions) => {
827
+ const events = streams.get(streamName);
828
+ const currentStreamVersion = events ? BigInt(events.length) : InMemoryEventStoreDefaultStreamVersion;
829
+ assertExpectedVersionMatchesCurrent(
830
+ currentStreamVersion,
831
+ _optionalChain([readOptions, 'optionalAccess', _38 => _38.expectedStreamVersion]),
832
+ InMemoryEventStoreDefaultStreamVersion
833
+ );
834
+ const from = Number(_nullishCoalesce(_optionalChain([readOptions, 'optionalAccess', _39 => _39.from]), () => ( 0)));
835
+ const to = Number(
836
+ _nullishCoalesce(_optionalChain([readOptions, 'optionalAccess', _40 => _40.to]), () => ( (_optionalChain([readOptions, 'optionalAccess', _41 => _41.maxCount]) ? (_nullishCoalesce(readOptions.from, () => ( 0n))) + readOptions.maxCount : _nullishCoalesce(_optionalChain([events, 'optionalAccess', _42 => _42.length]), () => ( 1)))))
837
+ );
838
+ const resultEvents = events !== void 0 && events.length > 0 ? upcastRecordedMessages(
839
+ events.slice(from, to),
840
+ _optionalChain([readOptions, 'optionalAccess', _43 => _43.schema, 'optionalAccess', _44 => _44.versioning])
841
+ ) : [];
842
+ const result = {
843
+ currentStreamVersion,
844
+ events: resultEvents,
845
+ streamExists: events !== void 0 && events.length > 0
846
+ };
847
+ return Promise.resolve(result);
848
+ },
849
+ appendToStream: async (streamName, events, options) => {
850
+ const currentEvents = _nullishCoalesce(streams.get(streamName), () => ( []));
851
+ const currentStreamVersion = currentEvents.length > 0 ? BigInt(currentEvents.length) : InMemoryEventStoreDefaultStreamVersion;
852
+ assertExpectedVersionMatchesCurrent(
853
+ currentStreamVersion,
854
+ _optionalChain([options, 'optionalAccess', _45 => _45.expectedStreamVersion]),
855
+ InMemoryEventStoreDefaultStreamVersion
856
+ );
857
+ const newEvents = events.map((event2, index) => {
858
+ const globalPosition = BigInt(getAllEventsCount() + index + 1);
859
+ const metadata = {
860
+ streamName,
861
+ messageId: _uuid.v4.call(void 0, ),
862
+ streamPosition: BigInt(currentEvents.length + index + 1),
863
+ globalPosition,
864
+ checkpoint: bigIntProcessorCheckpoint(globalPosition)
865
+ };
866
+ return {
867
+ ...event2,
868
+ kind: _nullishCoalesce(event2.kind, () => ( "Event")),
869
+ metadata: {
870
+ ..."metadata" in event2 ? _nullishCoalesce(event2.metadata, () => ( {})) : {},
871
+ ...metadata
872
+ }
873
+ };
874
+ });
875
+ const positionOfLastEventInTheStream = BigInt(
876
+ newEvents.slice(-1)[0].metadata.streamPosition
877
+ );
878
+ streams.set(streamName, [
879
+ ...currentEvents,
880
+ ...downcastRecordedMessages(newEvents, _optionalChain([options, 'optionalAccess', _46 => _46.schema, 'optionalAccess', _47 => _47.versioning]))
881
+ ]);
882
+ if (inlineProjections2.length > 0) {
883
+ await handleInMemoryProjections({
884
+ projections: inlineProjections2,
885
+ events: newEvents,
886
+ database: eventStore.database,
887
+ eventStore
888
+ });
889
+ }
890
+ const result = {
891
+ nextExpectedStreamVersion: positionOfLastEventInTheStream,
892
+ createdNewStream: currentStreamVersion === InMemoryEventStoreDefaultStreamVersion
893
+ };
894
+ await tryPublishMessagesAfterCommit(
895
+ newEvents,
896
+ _optionalChain([eventStoreOptions, 'optionalAccess', _48 => _48.hooks])
897
+ );
898
+ return result;
899
+ },
900
+ streamExists: (streamName) => {
901
+ const events = streams.get(streamName);
902
+ return Promise.resolve(events !== void 0 && events.length > 0);
903
+ }
904
+ };
905
+ return eventStore;
906
+ };
152
907
 
153
908
  // src/middlewares/problemDetailsMiddleware.ts
154
909
  var _httpproblemdetails = require('http-problem-details');
@@ -264,7 +1019,6 @@ var getETagValueFromIfMatch = (request) => {
264
1019
  };
265
1020
 
266
1021
  // src/handler.ts
267
-
268
1022
  var on = (handle) => async (request, response, _next) => {
269
1023
  const setResponse = await Promise.resolve(handle(request));
270
1024
  return setResponse(response);
@@ -293,17 +1047,17 @@ var HttpProblem = (statusCode, options) => (response) => {
293
1047
 
294
1048
  // src/responses.ts
295
1049
 
296
-
297
1050
  var DefaultHttpResponseOptions = {};
298
1051
  var DefaultHttpProblemResponseOptions = {
299
1052
  problemDetails: "Error occured!"
300
1053
  };
301
1054
  var sendCreated = (response, { eTag, ...options }) => send(response, 201, {
302
1055
  location: "url" in options ? options.url : `${response.req.url}/${options.createdId}`,
303
- body: "createdId" in options ? { id: options.createdId } : void 0,
1056
+ body: "createdId" in options ? { id: options.createdId, ..._nullishCoalesce(options.body, () => ( {})) } : options.body,
304
1057
  eTag
305
1058
  });
306
1059
  var sendAccepted = (response, options) => send(response, 202, options);
1060
+ var sendNoContent = (response, options) => send(response, 204, options);
307
1061
  var send = (response, statusCode, options) => {
308
1062
  const { location, body, eTag } = _nullishCoalesce(options, () => ( DefaultHttpResponseOptions));
309
1063
  if (eTag) setETag(response, eTag);
@@ -332,39 +1086,43 @@ var sendProblem = (response, statusCode, options) => {
332
1086
  // src/testing/apiE2ESpecification.ts
333
1087
  var _supertest = require('supertest'); var _supertest2 = _interopRequireDefault(_supertest);
334
1088
  var _assert = require('assert'); var _assert2 = _interopRequireDefault(_assert);
335
- var ApiE2ESpecification = {
336
- for: (getEventStore, getApplication2) => {
337
- {
338
- return (...givenRequests) => {
339
- const eventStore = getEventStore();
340
- const application = getApplication2(eventStore);
1089
+ function apiE2ESpecificationFor(optionsOrGetApplication) {
1090
+ const resolveApplication = () => {
1091
+ if (typeof optionsOrGetApplication === "function") {
1092
+ return optionsOrGetApplication();
1093
+ }
1094
+ const eventStore = _nullishCoalesce(_optionalChain([optionsOrGetApplication, 'access', _49 => _49.getEventStore, 'optionalCall', _50 => _50()]), () => ( getInMemoryEventStore()));
1095
+ return optionsOrGetApplication.getApplication(eventStore);
1096
+ };
1097
+ return (...givenRequests) => {
1098
+ const application = resolveApplication();
1099
+ return {
1100
+ when: (setupRequest) => {
1101
+ const handle = async () => {
1102
+ for (const requestFn of givenRequests) {
1103
+ await requestFn(_supertest2.default.call(void 0, application));
1104
+ }
1105
+ return setupRequest(_supertest2.default.call(void 0, application));
1106
+ };
341
1107
  return {
342
- when: (setupRequest) => {
343
- const handle = async () => {
344
- for (const requestFn of givenRequests) {
345
- await requestFn(_supertest2.default.call(void 0, application));
346
- }
347
- return setupRequest(_supertest2.default.call(void 0, application));
348
- };
349
- return {
350
- then: async (verify) => {
351
- const response = await handle();
352
- verify.forEach((assertion) => {
353
- const succeeded = assertion(response);
354
- if (succeeded === false) _assert2.default.fail();
355
- });
356
- }
357
- };
1108
+ then: async (verify) => {
1109
+ const response = await handle();
1110
+ verify.forEach((assertion) => {
1111
+ const succeeded = assertion(response);
1112
+ if (succeeded === false) _assert2.default.fail();
1113
+ });
358
1114
  }
359
1115
  };
360
- };
361
- }
362
- }
1116
+ }
1117
+ };
1118
+ };
1119
+ }
1120
+ var ApiE2ESpecification = {
1121
+ for: apiE2ESpecificationFor
363
1122
  };
364
1123
 
365
1124
  // src/testing/apiSpecification.ts
366
1125
 
367
-
368
1126
  var existingStream = (streamId, events) => {
369
1127
  return [streamId, events];
370
1128
  };
@@ -384,45 +1142,54 @@ var expectError = (errorCode, problemDetails) => expectResponse(
384
1142
  errorCode,
385
1143
  problemDetails ? { body: problemDetails } : void 0
386
1144
  );
387
- var ApiSpecification = {
388
- for: (getEventStore, getApplication2) => {
389
- {
390
- return (...givenStreams) => {
391
- const eventStore = WrapEventStore(getEventStore());
392
- const application = getApplication2(eventStore);
1145
+ function apiSpecificationFor(optionsOrGetEventStore, getApplication2) {
1146
+ const resolveStoreAndApplication = () => {
1147
+ if (typeof optionsOrGetEventStore === "function") {
1148
+ const eventStore2 = WrapEventStore(optionsOrGetEventStore());
1149
+ return { eventStore: eventStore2, application: getApplication2(eventStore2) };
1150
+ }
1151
+ const eventStore = WrapEventStore(optionsOrGetEventStore.getEventStore());
1152
+ return {
1153
+ eventStore,
1154
+ application: optionsOrGetEventStore.getApplication(eventStore)
1155
+ };
1156
+ };
1157
+ return (...givenStreams) => {
1158
+ const { eventStore, application } = resolveStoreAndApplication();
1159
+ return {
1160
+ when: (setupRequest) => {
1161
+ const handle = async () => {
1162
+ for (const [streamName, events] of givenStreams) {
1163
+ await eventStore.setup(streamName, events);
1164
+ }
1165
+ return setupRequest(_supertest2.default.call(void 0, application));
1166
+ };
393
1167
  return {
394
- when: (setupRequest) => {
395
- const handle = async () => {
396
- for (const [streamName, events] of givenStreams) {
397
- await eventStore.setup(streamName, events);
398
- }
399
- return setupRequest(_supertest2.default.call(void 0, application));
400
- };
401
- return {
402
- then: async (verify) => {
403
- const response = await handle();
404
- if (typeof verify === "function") {
405
- const succeeded = verify(response);
406
- if (succeeded === false) assertFails();
407
- } else if (Array.isArray(verify)) {
408
- const [first, ...rest] = verify;
409
- if (typeof first === "function") {
410
- const succeeded = first(response);
411
- if (succeeded === false) assertFails();
412
- }
413
- const events = typeof first === "function" ? rest : verify;
414
- assertMatches(
415
- Array.from(eventStore.appendedEvents.values()),
416
- events
417
- );
418
- }
1168
+ then: async (verify) => {
1169
+ const response = await handle();
1170
+ if (typeof verify === "function") {
1171
+ const succeeded = verify(response);
1172
+ if (succeeded === false) assertFails();
1173
+ } else if (Array.isArray(verify)) {
1174
+ const [first, ...rest] = verify;
1175
+ if (typeof first === "function") {
1176
+ const succeeded = first(response);
1177
+ if (succeeded === false) assertFails();
419
1178
  }
420
- };
1179
+ const events = typeof first === "function" ? rest : verify;
1180
+ assertMatches(
1181
+ Array.from(eventStore.appendedEvents.values()),
1182
+ events
1183
+ );
1184
+ }
421
1185
  }
422
1186
  };
423
- };
424
- }
425
- }
1187
+ }
1188
+ };
1189
+ };
1190
+ }
1191
+ var ApiSpecification = {
1192
+ for: apiSpecificationFor
426
1193
  };
427
1194
 
428
1195
 
@@ -462,5 +1229,6 @@ var ApiSpecification = {
462
1229
 
463
1230
 
464
1231
 
465
- exports.Accepted = Accepted; exports.ApiE2ESpecification = ApiE2ESpecification; exports.ApiSpecification = ApiSpecification; exports.BadRequest = BadRequest; exports.Conflict = Conflict; exports.Created = Created; exports.DefaultHttpProblemResponseOptions = DefaultHttpProblemResponseOptions; exports.DefaultHttpResponseOptions = DefaultHttpResponseOptions; exports.ETagErrors = ETagErrors; exports.Forbidden = Forbidden; exports.HeaderNames = HeaderNames; exports.HttpProblem = HttpProblem; exports.HttpResponse = HttpResponse; exports.NoContent = NoContent; exports.NotFound = NotFound; exports.OK = OK; exports.PreconditionFailed = PreconditionFailed; exports.WeakETagRegex = WeakETagRegex; exports.existingStream = existingStream; exports.expect = expect; exports.expectError = expectError; exports.expectNewEvents = expectNewEvents; exports.expectResponse = expectResponse; exports.getApplication = getApplication; exports.getETagFromIfMatch = getETagFromIfMatch; exports.getETagFromIfNotMatch = getETagFromIfNotMatch; exports.getETagValueFromIfMatch = getETagValueFromIfMatch; exports.getWeakETagValue = getWeakETagValue; exports.isWeakETag = isWeakETag; exports.on = on; exports.send = send; exports.sendAccepted = sendAccepted; exports.sendCreated = sendCreated; exports.sendProblem = sendProblem; exports.setETag = setETag; exports.startAPI = startAPI; exports.toWeakETag = toWeakETag;
1232
+
1233
+ exports.Accepted = Accepted; exports.ApiE2ESpecification = ApiE2ESpecification; exports.ApiSpecification = ApiSpecification; exports.BadRequest = BadRequest; exports.Conflict = Conflict; exports.Created = Created; exports.DefaultHttpProblemResponseOptions = DefaultHttpProblemResponseOptions; exports.DefaultHttpResponseOptions = DefaultHttpResponseOptions; exports.ETagErrors = ETagErrors; exports.Forbidden = Forbidden; exports.HeaderNames = HeaderNames; exports.HttpProblem = HttpProblem; exports.HttpResponse = HttpResponse; exports.NoContent = NoContent; exports.NotFound = NotFound; exports.OK = OK; exports.PreconditionFailed = PreconditionFailed; exports.WeakETagRegex = WeakETagRegex; exports.existingStream = existingStream; exports.expect = expect; exports.expectError = expectError; exports.expectNewEvents = expectNewEvents; exports.expectResponse = expectResponse; exports.getApplication = getApplication; exports.getETagFromIfMatch = getETagFromIfMatch; exports.getETagFromIfNotMatch = getETagFromIfNotMatch; exports.getETagValueFromIfMatch = getETagValueFromIfMatch; exports.getWeakETagValue = getWeakETagValue; exports.isWeakETag = isWeakETag; exports.on = on; exports.send = send; exports.sendAccepted = sendAccepted; exports.sendCreated = sendCreated; exports.sendNoContent = sendNoContent; exports.sendProblem = sendProblem; exports.setETag = setETag; exports.startAPI = startAPI; exports.toWeakETag = toWeakETag;
466
1234
  //# sourceMappingURL=index.cjs.map