@event-driven-io/emmett-esdb 0.38.7 → 0.39.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.js CHANGED
@@ -1,30 +1,40 @@
1
- // ../emmett/dist/chunk-O2VMBOV4.js
1
+ // ../emmett/dist/chunk-AZDDB5SF.js
2
2
  var isNumber = (val) => typeof val === "number" && val === val;
3
3
  var isBigint = (val) => typeof val === "bigint" && val === val;
4
4
  var isString = (val) => typeof val === "string";
5
5
  var EmmettError = class _EmmettError extends Error {
6
+ static Codes = {
7
+ ValidationError: 400,
8
+ IllegalStateError: 403,
9
+ NotFoundError: 404,
10
+ ConcurrencyError: 412,
11
+ InternalServerError: 500
12
+ };
6
13
  errorCode;
7
14
  constructor(options) {
8
- const errorCode = options && typeof options === "object" && "errorCode" in options ? options.errorCode : isNumber(options) ? options : 500;
15
+ const errorCode = options && typeof options === "object" && "errorCode" in options ? options.errorCode : isNumber(options) ? options : _EmmettError.Codes.InternalServerError;
9
16
  const message = options && typeof options === "object" && "message" in options ? options.message : isString(options) ? options : `Error with status code '${errorCode}' ocurred during Emmett processing`;
10
17
  super(message);
11
18
  this.errorCode = errorCode;
12
19
  Object.setPrototypeOf(this, _EmmettError.prototype);
13
20
  }
14
21
  static mapFrom(error) {
15
- if (error instanceof _EmmettError) {
22
+ if (_EmmettError.isInstanceOf(error)) {
16
23
  return error;
17
24
  }
18
25
  return new _EmmettError({
19
- errorCode: "errorCode" in error && error.errorCode !== void 0 && error.errorCode !== null ? error.errorCode : 500,
26
+ errorCode: "errorCode" in error && error.errorCode !== void 0 && error.errorCode !== null ? error.errorCode : _EmmettError.Codes.InternalServerError,
20
27
  message: error.message ?? "An unknown error occurred"
21
28
  });
22
29
  }
30
+ static isInstanceOf(error, errorCode) {
31
+ return typeof error === "object" && error !== null && "errorCode" in error && isNumber(error.errorCode) && (errorCode === void 0 || error.errorCode === errorCode);
32
+ }
23
33
  };
24
34
  var ConcurrencyError = class _ConcurrencyError extends EmmettError {
25
35
  constructor(current, expected, message) {
26
36
  super({
27
- errorCode: 412,
37
+ errorCode: EmmettError.Codes.ConcurrencyError,
28
38
  message: message ?? `Expected version ${expected.toString()} does not match current ${current?.toString()}`
29
39
  });
30
40
  this.current = current;
@@ -35,7 +45,7 @@ var ConcurrencyError = class _ConcurrencyError extends EmmettError {
35
45
  var ConcurrencyInMemoryDatabaseError = class _ConcurrencyInMemoryDatabaseError extends EmmettError {
36
46
  constructor(message) {
37
47
  super({
38
- errorCode: 412,
48
+ errorCode: EmmettError.Codes.ConcurrencyError,
39
49
  message: message ?? `Expected document state does not match current one!`
40
50
  });
41
51
  Object.setPrototypeOf(this, _ConcurrencyInMemoryDatabaseError.prototype);
@@ -43,27 +53,10 @@ var ConcurrencyInMemoryDatabaseError = class _ConcurrencyInMemoryDatabaseError e
43
53
  };
44
54
 
45
55
  // ../emmett/dist/index.js
46
- import { v4 as uuid5 } from "uuid";
56
+ import { v4 as uuid3 } from "uuid";
47
57
  import { v7 as uuid } from "uuid";
48
58
  import retry from "async-retry";
49
59
  import { v4 as uuid2 } from "uuid";
50
- import { TransformStream } from "web-streams-polyfill";
51
- import { v4 as uuid4 } from "uuid";
52
- import { v4 as uuid3 } from "uuid";
53
- import { TransformStream as TransformStream2 } from "web-streams-polyfill";
54
- import { ReadableStream } from "web-streams-polyfill";
55
- import "web-streams-polyfill";
56
- import { TransformStream as TransformStream3 } from "web-streams-polyfill";
57
- import { TransformStream as TransformStream4 } from "web-streams-polyfill";
58
- import { TransformStream as TransformStream5 } from "web-streams-polyfill";
59
- import {
60
- TransformStream as TransformStream6
61
- } from "web-streams-polyfill";
62
- import { TransformStream as TransformStream7 } from "web-streams-polyfill";
63
- import { TransformStream as TransformStream8 } from "web-streams-polyfill";
64
- import { TransformStream as TransformStream9 } from "web-streams-polyfill";
65
- import { TransformStream as TransformStream10 } from "web-streams-polyfill";
66
- import { TransformStream as TransformStream11 } from "web-streams-polyfill";
67
60
  var STREAM_EXISTS = "STREAM_EXISTS";
68
61
  var STREAM_DOES_NOT_EXIST = "STREAM_DOES_NOT_EXIST";
69
62
  var NO_CONCURRENCY_CHECK = "NO_CONCURRENCY_CHECK";
@@ -576,44 +569,6 @@ var getInMemoryDatabase = () => {
576
569
  }
577
570
  };
578
571
  };
579
- var notifyAboutNoActiveReadersStream = (onNoActiveReaderCallback, options = {}) => new NotifyAboutNoActiveReadersStream(onNoActiveReaderCallback, options);
580
- var NotifyAboutNoActiveReadersStream = class extends TransformStream2 {
581
- constructor(onNoActiveReaderCallback, options = {}) {
582
- super({
583
- cancel: (reason) => {
584
- console.log("Stream was canceled. Reason:", reason);
585
- this.stopChecking();
586
- }
587
- });
588
- this.onNoActiveReaderCallback = onNoActiveReaderCallback;
589
- this.streamId = options?.streamId ?? uuid3();
590
- this.onNoActiveReaderCallback = onNoActiveReaderCallback;
591
- this.startChecking(options?.intervalCheckInMs ?? 20);
592
- }
593
- checkInterval = null;
594
- streamId;
595
- _isStopped = false;
596
- get hasActiveSubscribers() {
597
- return !this._isStopped;
598
- }
599
- startChecking(interval) {
600
- this.checkInterval = setInterval(() => {
601
- this.checkNoActiveReader();
602
- }, interval);
603
- }
604
- stopChecking() {
605
- if (!this.checkInterval) return;
606
- clearInterval(this.checkInterval);
607
- this.checkInterval = null;
608
- this._isStopped = true;
609
- this.onNoActiveReaderCallback(this);
610
- }
611
- checkNoActiveReader() {
612
- if (!this.readable.locked && !this._isStopped) {
613
- this.stopChecking();
614
- }
615
- }
616
- };
617
572
  var getCheckpoint = (message2) => {
618
573
  return "checkpoint" in message2.metadata && // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
619
574
  isBigint(message2.metadata.checkpoint) ? (
@@ -827,153 +782,12 @@ var inMemoryReactor = (options) => {
827
782
  database
828
783
  };
829
784
  };
830
- var filter = (filter2) => new TransformStream3({
831
- transform(chunk, controller) {
832
- if (filter2(chunk)) {
833
- controller.enqueue(chunk);
834
- }
835
- }
836
- });
837
- var map = (map2) => new TransformStream4({
838
- transform(chunk, controller) {
839
- controller.enqueue(map2(chunk));
840
- }
841
- });
842
- var reduce = (reducer, initialValue) => new ReduceTransformStream(reducer, initialValue);
843
- var ReduceTransformStream = class extends TransformStream5 {
844
- accumulator;
845
- reducer;
846
- constructor(reducer, initialValue) {
847
- super({
848
- transform: (chunk) => {
849
- this.accumulator = this.reducer(this.accumulator, chunk);
850
- },
851
- flush: (controller) => {
852
- controller.enqueue(this.accumulator);
853
- controller.terminate();
854
- }
855
- });
856
- this.accumulator = initialValue;
857
- this.reducer = reducer;
858
- }
859
- };
860
- var retryStream = (createSourceStream, handleChunk2, retryOptions = { forever: true, minTimeout: 25 }) => new TransformStream6({
861
- start(controller) {
862
- asyncRetry(
863
- () => onRestream(createSourceStream, handleChunk2, controller),
864
- retryOptions
865
- ).catch((error2) => {
866
- controller.error(error2);
867
- });
868
- }
869
- });
870
- var onRestream = async (createSourceStream, handleChunk2, controller) => {
871
- const sourceStream = createSourceStream();
872
- const reader = sourceStream.getReader();
873
- try {
874
- let done;
875
- do {
876
- const result = await reader.read();
877
- done = result.done;
878
- await handleChunk2(result, controller);
879
- if (done) {
880
- controller.terminate();
881
- }
882
- } while (!done);
883
- } finally {
884
- reader.releaseLock();
885
- }
886
- };
887
- var skip = (limit) => new SkipTransformStream(limit);
888
- var SkipTransformStream = class extends TransformStream7 {
889
- count = 0;
890
- skip;
891
- constructor(skip2) {
892
- super({
893
- transform: (chunk, controller) => {
894
- this.count++;
895
- if (this.count > this.skip) {
896
- controller.enqueue(chunk);
897
- }
898
- }
899
- });
900
- this.skip = skip2;
901
- }
902
- };
903
- var stopAfter = (stopCondition) => new TransformStream8({
904
- transform(chunk, controller) {
905
- controller.enqueue(chunk);
906
- if (stopCondition(chunk)) {
907
- controller.terminate();
908
- }
909
- }
910
- });
911
- var stopOn = (stopCondition) => new TransformStream9({
912
- async transform(chunk, controller) {
913
- if (!stopCondition(chunk)) {
914
- controller.enqueue(chunk);
915
- return;
916
- }
917
- await Promise.resolve();
918
- controller.terminate();
919
- }
920
- });
921
- var take = (limit) => new TakeTransformStream(limit);
922
- var TakeTransformStream = class extends TransformStream10 {
923
- count = 0;
924
- limit;
925
- constructor(limit) {
926
- super({
927
- transform: (chunk, controller) => {
928
- if (this.count < this.limit) {
929
- this.count++;
930
- controller.enqueue(chunk);
931
- } else {
932
- controller.terminate();
933
- }
934
- }
935
- });
936
- this.limit = limit;
937
- }
938
- };
939
- var waitAtMost = (waitTimeInMs) => new TransformStream11({
940
- start(controller) {
941
- const timeoutId = setTimeout(() => {
942
- controller.terminate();
943
- }, waitTimeInMs);
944
- const originalTerminate = controller.terminate.bind(controller);
945
- controller.terminate = () => {
946
- clearTimeout(timeoutId);
947
- originalTerminate();
948
- };
949
- },
950
- transform(chunk, controller) {
951
- controller.enqueue(chunk);
952
- }
953
- });
954
- var streamTransformations = {
955
- filter,
956
- take,
957
- TakeTransformStream,
958
- skip,
959
- SkipTransformStream,
960
- map,
961
- notifyAboutNoActiveReadersStream,
962
- NotifyAboutNoActiveReadersStream,
963
- reduce,
964
- ReduceTransformStream,
965
- retry: retryStream,
966
- stopAfter,
967
- stopOn,
968
- waitAtMost
969
- };
970
- var { retry: retry2 } = streamTransformations;
971
785
 
972
786
  // src/eventStore/consumers/eventStoreDBEventStoreConsumer.ts
973
787
  import {
974
788
  EventStoreDBClient as EventStoreDBClient3
975
789
  } from "@eventstore/db-client";
976
- import { v7 as uuid6 } from "uuid";
790
+ import { v7 as uuid4 } from "uuid";
977
791
 
978
792
  // src/eventStore/consumers/subscriptions/index.ts
979
793
  import {
@@ -1223,11 +1037,11 @@ var eventStoreDBSubscription = ({
1223
1037
  }));
1224
1038
  };
1225
1039
  const pipeMessages = (options) => {
1226
- let retry3 = 0;
1040
+ let retry2 = 0;
1227
1041
  return asyncRetry(
1228
1042
  () => new Promise((resolve, reject) => {
1229
1043
  console.info(
1230
- `Starting subscription. ${retry3++} retries. From: ${JSONParser.stringify(from ?? "$all")}, Start from: ${JSONParser.stringify(
1044
+ `Starting subscription. ${retry2++} retries. From: ${JSONParser.stringify(from ?? "$all")}, Start from: ${JSONParser.stringify(
1231
1045
  options.startFrom
1232
1046
  )}`
1233
1047
  );
@@ -1324,8 +1138,8 @@ var eventStoreDBEventStoreConsumer = (options) => {
1324
1138
  reason: "No active processors"
1325
1139
  };
1326
1140
  const result = await Promise.allSettled(
1327
- activeProcessors.map((s) => {
1328
- return s.handle(messagesBatch, { client });
1141
+ activeProcessors.map(async (s) => {
1142
+ return await s.handle(messagesBatch, { client });
1329
1143
  })
1330
1144
  );
1331
1145
  const error = result.find((r) => r.status === "rejected")?.reason;
@@ -1353,7 +1167,7 @@ var eventStoreDBEventStoreConsumer = (options) => {
1353
1167
  await start;
1354
1168
  };
1355
1169
  return {
1356
- consumerId: options.consumerId ?? uuid6(),
1170
+ consumerId: options.consumerId ?? uuid4(),
1357
1171
  get isRunning() {
1358
1172
  return isRunning;
1359
1173
  },