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

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 = {
@@ -58,6 +57,7 @@ var _uuid = require('uuid');
58
57
  var _asyncretry = require('async-retry'); var _asyncretry2 = _interopRequireDefault(_asyncretry);
59
58
 
60
59
 
60
+
61
61
  var emmettPrefix = "emt";
62
62
  var defaultTag = `${emmettPrefix}:default`;
63
63
  var unknownTag = `${emmettPrefix}:unknown`;
@@ -282,6 +282,10 @@ var deepEquals = (left, right) => {
282
282
  var isEquatable = (left) => {
283
283
  return left !== null && left !== void 0 && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
284
284
  };
285
+ var toNormalizedString = (value) => value.toString().padStart(19, "0");
286
+ var bigInt = {
287
+ toNormalizedString
288
+ };
285
289
  var ParseError = class extends Error {
286
290
  constructor(text) {
287
291
  super(`Cannot parse! ${text}`);
@@ -315,12 +319,12 @@ var asyncRetry = async (fn, opts) => {
315
319
  );
316
320
  }
317
321
  return result;
318
- } catch (error2) {
319
- if (_optionalChain([opts, 'optionalAccess', _15 => _15.shouldRetryError]) && !opts.shouldRetryError(error2)) {
320
- bail(error2);
322
+ } catch (error) {
323
+ if (_optionalChain([opts, 'optionalAccess', _15 => _15.shouldRetryError]) && !opts.shouldRetryError(error)) {
324
+ bail(error);
321
325
  return void 0;
322
326
  }
323
- throw error2;
327
+ throw error;
324
328
  }
325
329
  },
326
330
  _nullishCoalesce(opts, () => ( { retries: 0 }))
@@ -600,73 +604,20 @@ var getInMemoryDatabase = () => {
600
604
  }
601
605
  };
602
606
  };
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
607
  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;
608
+ return message2.metadata.checkpoint;
659
609
  };
660
610
  var wasMessageHandled = (message2, checkpoint) => {
661
611
  const messageCheckpoint = getCheckpoint(message2);
662
- const checkpointBigint = checkpoint;
663
- return messageCheckpoint !== null && messageCheckpoint !== void 0 && checkpointBigint !== null && checkpointBigint !== void 0 && messageCheckpoint <= checkpointBigint;
612
+ return messageCheckpoint !== null && messageCheckpoint !== void 0 && checkpoint !== null && checkpoint !== void 0 && messageCheckpoint <= checkpoint;
664
613
  };
665
614
  var MessageProcessorType = {
666
615
  PROJECTOR: "projector",
667
616
  REACTOR: "reactor"
668
617
  };
669
618
  var defaultProcessingMessageProcessingScope = (handler, partialContext) => handler(partialContext);
619
+ var bigIntProcessorCheckpoint = (value) => bigInt.toNormalizedString(value);
620
+ var parseBigIntProcessorCheckpoint = (value) => BigInt(value);
670
621
  var defaultProcessorVersion = 1;
671
622
  var defaultProcessorPartition = defaultTag;
672
623
  var getProcessorInstanceId = (processorId) => `${processorId}:${_uuid.v7.call(void 0, )}`;
@@ -716,6 +667,7 @@ var reactor = (options) => {
716
667
  id: processorId,
717
668
  instanceId,
718
669
  type,
670
+ canHandle,
719
671
  init,
720
672
  start: async (startOptions) => {
721
673
  if (isActive) return;
@@ -732,7 +684,7 @@ var reactor = (options) => {
732
684
  }
733
685
  if (startFrom && startFrom !== "CURRENT") return startFrom;
734
686
  if (checkpoints) {
735
- const readResult = await _optionalChain([checkpoints, 'optionalAccess', _27 => _27.read, 'call', _28 => _28(
687
+ const readResult = await _optionalChain([checkpoints, 'optionalAccess', _24 => _24.read, 'call', _25 => _25(
736
688
  {
737
689
  processorId,
738
690
  partition
@@ -760,7 +712,7 @@ var reactor = (options) => {
760
712
  const upcasted = upcastRecordedMessage(
761
713
  // TODO: Make it smarter
762
714
  message2,
763
- _optionalChain([options, 'access', _29 => _29.messageOptions, 'optionalAccess', _30 => _30.schema, 'optionalAccess', _31 => _31.versioning])
715
+ _optionalChain([options, 'access', _26 => _26.messageOptions, 'optionalAccess', _27 => _27.schema, 'optionalAccess', _28 => _28.versioning])
764
716
  );
765
717
  if (canHandle !== void 0 && !canHandle.includes(upcasted.type))
766
718
  continue;
@@ -813,13 +765,13 @@ var projector = (options) => {
813
765
  processorId,
814
766
  messageOptions: options.projection.eventsOptions,
815
767
  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) => {
768
+ onInit: _optionalChain([options, 'access', _29 => _29.hooks, 'optionalAccess', _30 => _30.onInit]),
769
+ onStart: options.truncateOnStart && options.projection.truncate || _optionalChain([options, 'access', _31 => _31.hooks, 'optionalAccess', _32 => _32.onStart]) ? async (context) => {
818
770
  if (options.truncateOnStart && options.projection.truncate)
819
771
  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)]);
772
+ 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
773
  } : void 0,
822
- onClose: _optionalChain([options, 'access', _41 => _41.hooks, 'optionalAccess', _42 => _42.onClose])
774
+ onClose: _optionalChain([options, 'access', _38 => _38.hooks, 'optionalAccess', _39 => _39.onClose])
823
775
  },
824
776
  eachMessage: async (event2, context) => projection2.handle([event2], context)
825
777
  });
@@ -829,7 +781,7 @@ var inMemoryCheckpointer = () => {
829
781
  read: async ({ processorId }, { database }) => {
830
782
  const checkpoint = await database.collection("emt_processor_checkpoints").findOne((d) => d._id === processorId);
831
783
  return Promise.resolve({
832
- lastCheckpoint: _nullishCoalesce(_optionalChain([checkpoint, 'optionalAccess', _43 => _43.lastCheckpoint]), () => ( null))
784
+ lastCheckpoint: _nullishCoalesce(_optionalChain([checkpoint, 'optionalAccess', _40 => _40.lastCheckpoint]), () => ( null))
833
785
  });
834
786
  },
835
787
  store: async (context, { database }) => {
@@ -840,7 +792,7 @@ var inMemoryCheckpointer = () => {
840
792
  const checkpoint = await checkpoints.findOne(
841
793
  (d) => d._id === processorId
842
794
  );
843
- const currentPosition = _nullishCoalesce(_optionalChain([checkpoint, 'optionalAccess', _44 => _44.lastCheckpoint]), () => ( null));
795
+ const currentPosition = _nullishCoalesce(_optionalChain([checkpoint, 'optionalAccess', _41 => _41.lastCheckpoint]), () => ( null));
844
796
  const newCheckpoint = getCheckpoint(message2);
845
797
  if (currentPosition && (currentPosition === newCheckpoint || currentPosition !== lastCheckpoint)) {
846
798
  return {
@@ -860,7 +812,7 @@ var inMemoryCheckpointer = () => {
860
812
  var inMemoryProcessingScope = (options) => {
861
813
  const processorDatabase = options.database;
862
814
  const processingScope = (handler, partialContext) => {
863
- const database = _nullishCoalesce(processorDatabase, () => ( _optionalChain([partialContext, 'optionalAccess', _45 => _45.database])));
815
+ const database = _nullishCoalesce(processorDatabase, () => ( _optionalChain([partialContext, 'optionalAccess', _42 => _42.database])));
864
816
  if (!database)
865
817
  throw new EmmettError(
866
818
  `InMemory processor '${options.processorId}' is missing database. Ensure that you passed it through options`
@@ -870,12 +822,12 @@ var inMemoryProcessingScope = (options) => {
870
822
  return processingScope;
871
823
  };
872
824
  var inMemoryProjector = (options) => {
873
- const database = _nullishCoalesce(_optionalChain([options, 'access', _46 => _46.connectionOptions, 'optionalAccess', _47 => _47.database]), () => ( getInMemoryDatabase()));
825
+ const database = _nullishCoalesce(_optionalChain([options, 'access', _43 => _43.connectionOptions, 'optionalAccess', _44 => _44.database]), () => ( getInMemoryDatabase()));
874
826
  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)]);
827
+ onInit: _optionalChain([options, 'access', _45 => _45.hooks, 'optionalAccess', _46 => _46.onInit]),
828
+ onStart: _optionalChain([options, 'access', _47 => _47.hooks, 'optionalAccess', _48 => _48.onStart]),
829
+ onClose: _optionalChain([options, 'access', _49 => _49.hooks, 'optionalAccess', _50 => _50.onClose]) ? async (context) => {
830
+ 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
831
  } : void 0
880
832
  };
881
833
  const processor = projector({
@@ -890,11 +842,11 @@ var inMemoryProjector = (options) => {
890
842
  return Object.assign(processor, { database });
891
843
  };
892
844
  var inMemoryReactor = (options) => {
893
- const database = _nullishCoalesce(_optionalChain([options, 'access', _59 => _59.connectionOptions, 'optionalAccess', _60 => _60.database]), () => ( getInMemoryDatabase()));
845
+ const database = _nullishCoalesce(_optionalChain([options, 'access', _56 => _56.connectionOptions, 'optionalAccess', _57 => _57.database]), () => ( getInMemoryDatabase()));
894
846
  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])
847
+ onInit: _optionalChain([options, 'access', _58 => _58.hooks, 'optionalAccess', _59 => _59.onInit]),
848
+ onStart: _optionalChain([options, 'access', _60 => _60.hooks, 'optionalAccess', _61 => _61.onStart]),
849
+ onClose: _optionalChain([options, 'access', _62 => _62.hooks, 'optionalAccess', _63 => _63.onClose])
898
850
  };
899
851
  const processor = reactor({
900
852
  ...options,
@@ -907,6 +859,49 @@ var inMemoryReactor = (options) => {
907
859
  });
908
860
  return Object.assign(processor, { database });
909
861
  };
862
+ var downcastRecordedMessage = (recordedMessage, options) => {
863
+ if (!_optionalChain([options, 'optionalAccess', _64 => _64.downcast]))
864
+ return recordedMessage;
865
+ const downcasted = options.downcast(
866
+ recordedMessage
867
+ );
868
+ return {
869
+ ...recordedMessage,
870
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
871
+ data: downcasted.data,
872
+ ..."metadata" in recordedMessage || "metadata" in downcasted ? {
873
+ metadata: {
874
+ ..."metadata" in recordedMessage ? recordedMessage.metadata : {},
875
+ ..."metadata" in downcasted ? downcasted.metadata : {}
876
+ }
877
+ } : {}
878
+ };
879
+ };
880
+ var downcastRecordedMessages = (recordedMessages, options) => {
881
+ if (!_optionalChain([options, 'optionalAccess', _65 => _65.downcast]))
882
+ return recordedMessages;
883
+ return recordedMessages.map(
884
+ (recordedMessage) => downcastRecordedMessage(recordedMessage, options)
885
+ );
886
+ };
887
+ var upcastRecordedMessage = (recordedMessage, options) => {
888
+ if (!_optionalChain([options, 'optionalAccess', _66 => _66.upcast]))
889
+ return recordedMessage;
890
+ const upcasted = options.upcast(
891
+ recordedMessage
892
+ );
893
+ return {
894
+ ...recordedMessage,
895
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
896
+ data: upcasted.data,
897
+ ..."metadata" in recordedMessage || "metadata" in upcasted ? {
898
+ metadata: {
899
+ ..."metadata" in recordedMessage ? recordedMessage.metadata : {},
900
+ ..."metadata" in upcasted ? upcasted.metadata : {}
901
+ }
902
+ } : {}
903
+ };
904
+ };
910
905
 
911
906
  // src/eventStore/consumers/eventStoreDBEventStoreConsumer.ts
912
907
 
@@ -1048,7 +1043,7 @@ var getEventStoreDBEventStore = (eventStore) => {
1048
1043
  } catch (error) {
1049
1044
  if (error instanceof _dbclient.WrongExpectedVersionError) {
1050
1045
  throw new ExpectedVersionConflictError(
1051
- error.actualVersion,
1046
+ BigInt(error.actualVersion),
1052
1047
  toExpectedVersion(error.expectedVersion)
1053
1048
  );
1054
1049
  }
@@ -1077,7 +1072,7 @@ var getEventStoreDBEventStore = (eventStore) => {
1077
1072
  //streamEvents: streamEvents(eventStore),
1078
1073
  };
1079
1074
  };
1080
- var getCheckpoint2 = (resolvedEvent, from) => {
1075
+ var getESDBCheckpoint = (resolvedEvent, from) => {
1081
1076
  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
1077
  };
1083
1078
  var mapFromESDBEvent = (resolvedEvent, from) => {
@@ -1091,7 +1086,9 @@ var mapFromESDBEvent = (resolvedEvent, from) => {
1091
1086
  streamName: event.streamId,
1092
1087
  streamPosition: event.revision,
1093
1088
  globalPosition: event.position.commit,
1094
- checkpoint: getCheckpoint2(resolvedEvent, from)
1089
+ checkpoint: bigIntProcessorCheckpoint(
1090
+ getESDBCheckpoint(resolvedEvent, from)
1091
+ )
1095
1092
  }
1096
1093
  };
1097
1094
  };
@@ -1114,10 +1111,10 @@ var toExpectedVersion = (expected) => {
1114
1111
  var DefaultEventStoreDBEventStoreProcessorBatchSize = 100;
1115
1112
  var DefaultEventStoreDBEventStoreProcessorPullingFrequencyInMs = 50;
1116
1113
  var toGlobalPosition = (startFrom) => startFrom === "BEGINNING" ? _dbclient.START : startFrom === "END" ? _dbclient.END : {
1117
- prepare: startFrom.lastCheckpoint,
1118
- commit: startFrom.lastCheckpoint
1114
+ prepare: parseBigIntProcessorCheckpoint(startFrom.lastCheckpoint),
1115
+ commit: parseBigIntProcessorCheckpoint(startFrom.lastCheckpoint)
1119
1116
  };
1120
- var toStreamPosition = (startFrom) => startFrom === "BEGINNING" ? _dbclient.START : startFrom === "END" ? _dbclient.END : startFrom.lastCheckpoint;
1117
+ var toStreamPosition = (startFrom) => startFrom === "BEGINNING" ? _dbclient.START : startFrom === "END" ? _dbclient.END : parseBigIntProcessorCheckpoint(startFrom.lastCheckpoint);
1121
1118
  var subscribe = (client, from, options) => from == void 0 || from.stream == $all ? client.subscribeToAll({
1122
1119
  ..._nullishCoalesce(_optionalChain([from, 'optionalAccess', _87 => _87.options]), () => ( {})),
1123
1120
  fromPosition: toGlobalPosition(options.startFrom),
@@ -1215,7 +1212,7 @@ var eventStoreDBSubscription = ({
1215
1212
  const handler = new class extends _stream.Writable {
1216
1213
  async _write(result, _encoding, done) {
1217
1214
  if (!isRunning) return;
1218
- if (isBigint(result)) {
1215
+ if (isString(result)) {
1219
1216
  options.startFrom = {
1220
1217
  lastCheckpoint: result
1221
1218
  };