@event-driven-io/emmett-esdb 0.43.0-apha.2 → 0.43.0-beta.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/index.cjs CHANGED
@@ -1,6 +1,5 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;// ../emmett/dist/chunk-AZDDB5SF.js
2
2
  var isNumber = (val) => typeof val === "number" && val === val;
3
- var isBigint = (val) => typeof val === "bigint" && val === val;
4
3
  var isString = (val) => typeof val === "string";
5
4
  var EmmettError = (_class = class _EmmettError extends Error {
6
5
  static __initStatic() {this.Codes = {
@@ -282,6 +281,10 @@ var deepEquals = (left, right) => {
282
281
  var isEquatable = (left) => {
283
282
  return left !== null && left !== void 0 && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
284
283
  };
284
+ var toNormalizedString = (value) => value.toString().padStart(19, "0");
285
+ var bigInt = {
286
+ toNormalizedString
287
+ };
285
288
  var ParseError = class extends Error {
286
289
  constructor(text) {
287
290
  super(`Cannot parse! ${text}`);
@@ -600,73 +603,20 @@ var getInMemoryDatabase = () => {
600
603
  }
601
604
  };
602
605
  };
603
- var downcastRecordedMessage = (recordedMessage, options) => {
604
- if (!_optionalChain([options, 'optionalAccess', _24 => _24.downcast]))
605
- return recordedMessage;
606
- const downcasted = options.downcast(
607
- recordedMessage
608
- );
609
- return {
610
- ...recordedMessage,
611
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
612
- data: downcasted.data,
613
- ..."metadata" in recordedMessage || "metadata" in downcasted ? {
614
- metadata: {
615
- ..."metadata" in recordedMessage ? recordedMessage.metadata : {},
616
- ..."metadata" in downcasted ? downcasted.metadata : {}
617
- }
618
- } : {}
619
- };
620
- };
621
- var downcastRecordedMessages = (recordedMessages, options) => {
622
- if (!_optionalChain([options, 'optionalAccess', _25 => _25.downcast]))
623
- return recordedMessages;
624
- return recordedMessages.map(
625
- (recordedMessage) => downcastRecordedMessage(recordedMessage, options)
626
- );
627
- };
628
- var upcastRecordedMessage = (recordedMessage, options) => {
629
- if (!_optionalChain([options, 'optionalAccess', _26 => _26.upcast]))
630
- return recordedMessage;
631
- const upcasted = options.upcast(
632
- recordedMessage
633
- );
634
- return {
635
- ...recordedMessage,
636
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
637
- data: upcasted.data,
638
- ..."metadata" in recordedMessage || "metadata" in upcasted ? {
639
- metadata: {
640
- ..."metadata" in recordedMessage ? recordedMessage.metadata : {},
641
- ..."metadata" in upcasted ? upcasted.metadata : {}
642
- }
643
- } : {}
644
- };
645
- };
646
606
  var getCheckpoint = (message2) => {
647
- return "checkpoint" in message2.metadata ? (
648
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
649
- message2.metadata.checkpoint
650
- ) : "globalPosition" in message2.metadata && // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
651
- isBigint(message2.metadata.globalPosition) ? (
652
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
653
- message2.metadata.globalPosition
654
- ) : "streamPosition" in message2.metadata && // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
655
- isBigint(message2.metadata.streamPosition) ? (
656
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
657
- message2.metadata.streamPosition
658
- ) : null;
607
+ return message2.metadata.checkpoint;
659
608
  };
660
609
  var wasMessageHandled = (message2, checkpoint) => {
661
610
  const messageCheckpoint = getCheckpoint(message2);
662
- const checkpointBigint = checkpoint;
663
- return messageCheckpoint !== null && messageCheckpoint !== void 0 && checkpointBigint !== null && checkpointBigint !== void 0 && messageCheckpoint <= checkpointBigint;
611
+ return messageCheckpoint !== null && messageCheckpoint !== void 0 && checkpoint !== null && checkpoint !== void 0 && messageCheckpoint <= checkpoint;
664
612
  };
665
613
  var MessageProcessorType = {
666
614
  PROJECTOR: "projector",
667
615
  REACTOR: "reactor"
668
616
  };
669
617
  var defaultProcessingMessageProcessingScope = (handler, partialContext) => handler(partialContext);
618
+ var bigIntProcessorCheckpoint = (value) => bigInt.toNormalizedString(value);
619
+ var parseBigIntProcessorCheckpoint = (value) => BigInt(value);
670
620
  var defaultProcessorVersion = 1;
671
621
  var defaultProcessorPartition = defaultTag;
672
622
  var getProcessorInstanceId = (processorId) => `${processorId}:${_uuid.v7.call(void 0, )}`;
@@ -732,7 +682,7 @@ var reactor = (options) => {
732
682
  }
733
683
  if (startFrom && startFrom !== "CURRENT") return startFrom;
734
684
  if (checkpoints) {
735
- const readResult = await _optionalChain([checkpoints, 'optionalAccess', _27 => _27.read, 'call', _28 => _28(
685
+ const readResult = await _optionalChain([checkpoints, 'optionalAccess', _24 => _24.read, 'call', _25 => _25(
736
686
  {
737
687
  processorId,
738
688
  partition
@@ -760,7 +710,7 @@ var reactor = (options) => {
760
710
  const upcasted = upcastRecordedMessage(
761
711
  // TODO: Make it smarter
762
712
  message2,
763
- _optionalChain([options, 'access', _29 => _29.messageOptions, 'optionalAccess', _30 => _30.schema, 'optionalAccess', _31 => _31.versioning])
713
+ _optionalChain([options, 'access', _26 => _26.messageOptions, 'optionalAccess', _27 => _27.schema, 'optionalAccess', _28 => _28.versioning])
764
714
  );
765
715
  if (canHandle !== void 0 && !canHandle.includes(upcasted.type))
766
716
  continue;
@@ -813,13 +763,13 @@ var projector = (options) => {
813
763
  processorId,
814
764
  messageOptions: options.projection.eventsOptions,
815
765
  hooks: {
816
- onInit: _optionalChain([options, 'access', _32 => _32.hooks, 'optionalAccess', _33 => _33.onInit]),
817
- onStart: options.truncateOnStart && options.projection.truncate || _optionalChain([options, 'access', _34 => _34.hooks, 'optionalAccess', _35 => _35.onStart]) ? async (context) => {
766
+ onInit: _optionalChain([options, 'access', _29 => _29.hooks, 'optionalAccess', _30 => _30.onInit]),
767
+ onStart: options.truncateOnStart && options.projection.truncate || _optionalChain([options, 'access', _31 => _31.hooks, 'optionalAccess', _32 => _32.onStart]) ? async (context) => {
818
768
  if (options.truncateOnStart && options.projection.truncate)
819
769
  await options.projection.truncate(context);
820
- if (_optionalChain([options, 'access', _36 => _36.hooks, 'optionalAccess', _37 => _37.onStart])) await _optionalChain([options, 'access', _38 => _38.hooks, 'optionalAccess', _39 => _39.onStart, 'call', _40 => _40(context)]);
770
+ if (_optionalChain([options, 'access', _33 => _33.hooks, 'optionalAccess', _34 => _34.onStart])) await _optionalChain([options, 'access', _35 => _35.hooks, 'optionalAccess', _36 => _36.onStart, 'call', _37 => _37(context)]);
821
771
  } : void 0,
822
- onClose: _optionalChain([options, 'access', _41 => _41.hooks, 'optionalAccess', _42 => _42.onClose])
772
+ onClose: _optionalChain([options, 'access', _38 => _38.hooks, 'optionalAccess', _39 => _39.onClose])
823
773
  },
824
774
  eachMessage: async (event2, context) => projection2.handle([event2], context)
825
775
  });
@@ -829,7 +779,7 @@ var inMemoryCheckpointer = () => {
829
779
  read: async ({ processorId }, { database }) => {
830
780
  const checkpoint = await database.collection("emt_processor_checkpoints").findOne((d) => d._id === processorId);
831
781
  return Promise.resolve({
832
- lastCheckpoint: _nullishCoalesce(_optionalChain([checkpoint, 'optionalAccess', _43 => _43.lastCheckpoint]), () => ( null))
782
+ lastCheckpoint: _nullishCoalesce(_optionalChain([checkpoint, 'optionalAccess', _40 => _40.lastCheckpoint]), () => ( null))
833
783
  });
834
784
  },
835
785
  store: async (context, { database }) => {
@@ -840,7 +790,7 @@ var inMemoryCheckpointer = () => {
840
790
  const checkpoint = await checkpoints.findOne(
841
791
  (d) => d._id === processorId
842
792
  );
843
- const currentPosition = _nullishCoalesce(_optionalChain([checkpoint, 'optionalAccess', _44 => _44.lastCheckpoint]), () => ( null));
793
+ const currentPosition = _nullishCoalesce(_optionalChain([checkpoint, 'optionalAccess', _41 => _41.lastCheckpoint]), () => ( null));
844
794
  const newCheckpoint = getCheckpoint(message2);
845
795
  if (currentPosition && (currentPosition === newCheckpoint || currentPosition !== lastCheckpoint)) {
846
796
  return {
@@ -860,7 +810,7 @@ var inMemoryCheckpointer = () => {
860
810
  var inMemoryProcessingScope = (options) => {
861
811
  const processorDatabase = options.database;
862
812
  const processingScope = (handler, partialContext) => {
863
- const database = _nullishCoalesce(processorDatabase, () => ( _optionalChain([partialContext, 'optionalAccess', _45 => _45.database])));
813
+ const database = _nullishCoalesce(processorDatabase, () => ( _optionalChain([partialContext, 'optionalAccess', _42 => _42.database])));
864
814
  if (!database)
865
815
  throw new EmmettError(
866
816
  `InMemory processor '${options.processorId}' is missing database. Ensure that you passed it through options`
@@ -870,12 +820,12 @@ var inMemoryProcessingScope = (options) => {
870
820
  return processingScope;
871
821
  };
872
822
  var inMemoryProjector = (options) => {
873
- const database = _nullishCoalesce(_optionalChain([options, 'access', _46 => _46.connectionOptions, 'optionalAccess', _47 => _47.database]), () => ( getInMemoryDatabase()));
823
+ const database = _nullishCoalesce(_optionalChain([options, 'access', _43 => _43.connectionOptions, 'optionalAccess', _44 => _44.database]), () => ( getInMemoryDatabase()));
874
824
  const hooks = {
875
- onInit: _optionalChain([options, 'access', _48 => _48.hooks, 'optionalAccess', _49 => _49.onInit]),
876
- onStart: _optionalChain([options, 'access', _50 => _50.hooks, 'optionalAccess', _51 => _51.onStart]),
877
- onClose: _optionalChain([options, 'access', _52 => _52.hooks, 'optionalAccess', _53 => _53.onClose]) ? async (context) => {
878
- if (_optionalChain([options, 'access', _54 => _54.hooks, 'optionalAccess', _55 => _55.onClose])) await _optionalChain([options, 'access', _56 => _56.hooks, 'optionalAccess', _57 => _57.onClose, 'call', _58 => _58(context)]);
825
+ onInit: _optionalChain([options, 'access', _45 => _45.hooks, 'optionalAccess', _46 => _46.onInit]),
826
+ onStart: _optionalChain([options, 'access', _47 => _47.hooks, 'optionalAccess', _48 => _48.onStart]),
827
+ onClose: _optionalChain([options, 'access', _49 => _49.hooks, 'optionalAccess', _50 => _50.onClose]) ? async (context) => {
828
+ if (_optionalChain([options, 'access', _51 => _51.hooks, 'optionalAccess', _52 => _52.onClose])) await _optionalChain([options, 'access', _53 => _53.hooks, 'optionalAccess', _54 => _54.onClose, 'call', _55 => _55(context)]);
879
829
  } : void 0
880
830
  };
881
831
  const processor = projector({
@@ -890,11 +840,11 @@ var inMemoryProjector = (options) => {
890
840
  return Object.assign(processor, { database });
891
841
  };
892
842
  var inMemoryReactor = (options) => {
893
- const database = _nullishCoalesce(_optionalChain([options, 'access', _59 => _59.connectionOptions, 'optionalAccess', _60 => _60.database]), () => ( getInMemoryDatabase()));
843
+ const database = _nullishCoalesce(_optionalChain([options, 'access', _56 => _56.connectionOptions, 'optionalAccess', _57 => _57.database]), () => ( getInMemoryDatabase()));
894
844
  const hooks = {
895
- onInit: _optionalChain([options, 'access', _61 => _61.hooks, 'optionalAccess', _62 => _62.onInit]),
896
- onStart: _optionalChain([options, 'access', _63 => _63.hooks, 'optionalAccess', _64 => _64.onStart]),
897
- onClose: _optionalChain([options, 'access', _65 => _65.hooks, 'optionalAccess', _66 => _66.onClose])
845
+ onInit: _optionalChain([options, 'access', _58 => _58.hooks, 'optionalAccess', _59 => _59.onInit]),
846
+ onStart: _optionalChain([options, 'access', _60 => _60.hooks, 'optionalAccess', _61 => _61.onStart]),
847
+ onClose: _optionalChain([options, 'access', _62 => _62.hooks, 'optionalAccess', _63 => _63.onClose])
898
848
  };
899
849
  const processor = reactor({
900
850
  ...options,
@@ -907,6 +857,49 @@ var inMemoryReactor = (options) => {
907
857
  });
908
858
  return Object.assign(processor, { database });
909
859
  };
860
+ var downcastRecordedMessage = (recordedMessage, options) => {
861
+ if (!_optionalChain([options, 'optionalAccess', _64 => _64.downcast]))
862
+ return recordedMessage;
863
+ const downcasted = options.downcast(
864
+ recordedMessage
865
+ );
866
+ return {
867
+ ...recordedMessage,
868
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
869
+ data: downcasted.data,
870
+ ..."metadata" in recordedMessage || "metadata" in downcasted ? {
871
+ metadata: {
872
+ ..."metadata" in recordedMessage ? recordedMessage.metadata : {},
873
+ ..."metadata" in downcasted ? downcasted.metadata : {}
874
+ }
875
+ } : {}
876
+ };
877
+ };
878
+ var downcastRecordedMessages = (recordedMessages, options) => {
879
+ if (!_optionalChain([options, 'optionalAccess', _65 => _65.downcast]))
880
+ return recordedMessages;
881
+ return recordedMessages.map(
882
+ (recordedMessage) => downcastRecordedMessage(recordedMessage, options)
883
+ );
884
+ };
885
+ var upcastRecordedMessage = (recordedMessage, options) => {
886
+ if (!_optionalChain([options, 'optionalAccess', _66 => _66.upcast]))
887
+ return recordedMessage;
888
+ const upcasted = options.upcast(
889
+ recordedMessage
890
+ );
891
+ return {
892
+ ...recordedMessage,
893
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
894
+ data: upcasted.data,
895
+ ..."metadata" in recordedMessage || "metadata" in upcasted ? {
896
+ metadata: {
897
+ ..."metadata" in recordedMessage ? recordedMessage.metadata : {},
898
+ ..."metadata" in upcasted ? upcasted.metadata : {}
899
+ }
900
+ } : {}
901
+ };
902
+ };
910
903
 
911
904
  // src/eventStore/consumers/eventStoreDBEventStoreConsumer.ts
912
905
 
@@ -1048,7 +1041,7 @@ var getEventStoreDBEventStore = (eventStore) => {
1048
1041
  } catch (error) {
1049
1042
  if (error instanceof _dbclient.WrongExpectedVersionError) {
1050
1043
  throw new ExpectedVersionConflictError(
1051
- error.actualVersion,
1044
+ BigInt(error.actualVersion),
1052
1045
  toExpectedVersion(error.expectedVersion)
1053
1046
  );
1054
1047
  }
@@ -1077,7 +1070,7 @@ var getEventStoreDBEventStore = (eventStore) => {
1077
1070
  //streamEvents: streamEvents(eventStore),
1078
1071
  };
1079
1072
  };
1080
- var getCheckpoint2 = (resolvedEvent, from) => {
1073
+ var getESDBCheckpoint = (resolvedEvent, from) => {
1081
1074
  return !from || _optionalChain([from, 'optionalAccess', _78 => _78.stream]) === $all ? _nullishCoalesce(_optionalChain([resolvedEvent, 'access', _79 => _79.link, 'optionalAccess', _80 => _80.position, 'optionalAccess', _81 => _81.commit]), () => ( _optionalChain([resolvedEvent, 'access', _82 => _82.event, 'optionalAccess', _83 => _83.position, 'optionalAccess', _84 => _84.commit]))) : _nullishCoalesce(_optionalChain([resolvedEvent, 'access', _85 => _85.link, 'optionalAccess', _86 => _86.revision]), () => ( resolvedEvent.event.revision));
1082
1075
  };
1083
1076
  var mapFromESDBEvent = (resolvedEvent, from) => {
@@ -1091,7 +1084,9 @@ var mapFromESDBEvent = (resolvedEvent, from) => {
1091
1084
  streamName: event.streamId,
1092
1085
  streamPosition: event.revision,
1093
1086
  globalPosition: event.position.commit,
1094
- checkpoint: getCheckpoint2(resolvedEvent, from)
1087
+ checkpoint: bigIntProcessorCheckpoint(
1088
+ getESDBCheckpoint(resolvedEvent, from)
1089
+ )
1095
1090
  }
1096
1091
  };
1097
1092
  };
@@ -1114,10 +1109,10 @@ var toExpectedVersion = (expected) => {
1114
1109
  var DefaultEventStoreDBEventStoreProcessorBatchSize = 100;
1115
1110
  var DefaultEventStoreDBEventStoreProcessorPullingFrequencyInMs = 50;
1116
1111
  var toGlobalPosition = (startFrom) => startFrom === "BEGINNING" ? _dbclient.START : startFrom === "END" ? _dbclient.END : {
1117
- prepare: startFrom.lastCheckpoint,
1118
- commit: startFrom.lastCheckpoint
1112
+ prepare: parseBigIntProcessorCheckpoint(startFrom.lastCheckpoint),
1113
+ commit: parseBigIntProcessorCheckpoint(startFrom.lastCheckpoint)
1119
1114
  };
1120
- var toStreamPosition = (startFrom) => startFrom === "BEGINNING" ? _dbclient.START : startFrom === "END" ? _dbclient.END : startFrom.lastCheckpoint;
1115
+ var toStreamPosition = (startFrom) => startFrom === "BEGINNING" ? _dbclient.START : startFrom === "END" ? _dbclient.END : parseBigIntProcessorCheckpoint(startFrom.lastCheckpoint);
1121
1116
  var subscribe = (client, from, options) => from == void 0 || from.stream == $all ? client.subscribeToAll({
1122
1117
  ..._nullishCoalesce(_optionalChain([from, 'optionalAccess', _87 => _87.options]), () => ( {})),
1123
1118
  fromPosition: toGlobalPosition(options.startFrom),
@@ -1215,7 +1210,7 @@ var eventStoreDBSubscription = ({
1215
1210
  const handler = new class extends _stream.Writable {
1216
1211
  async _write(result, _encoding, done) {
1217
1212
  if (!isRunning) return;
1218
- if (isBigint(result)) {
1213
+ if (isString(result)) {
1219
1214
  options.startFrom = {
1220
1215
  lastCheckpoint: result
1221
1216
  };