@fraym/streams 0.7.2 → 0.9.0

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.
@@ -8,6 +8,7 @@ export interface StreamIterator {
8
8
  }
9
9
  export interface Client {
10
10
  getEvent: (tenantId: string, topic: string, eventId: string) => Promise<SubscriptionEvent>;
11
+ getLastEvent: (tenantId: string, topic: string) => Promise<SubscriptionEvent | null>;
11
12
  iterateAllEvents: (tenantId: string, topic: string, includedEventTypes: string[], perPage: number, handler: HandlerFunc) => Promise<void>;
12
13
  iterateAllEventsAfterEvent: (tenantId: string, topic: string, includedEventTypes: string[], eventId: string, perPage: number, handler: HandlerFunc) => Promise<void>;
13
14
  publish: (topic: string, events: PublishEvent[]) => Promise<void>;
@@ -16,6 +17,7 @@ export interface Client {
16
17
  subscribe: (topics?: string[], ignoreUnhandledEvents?: boolean) => Subscription;
17
18
  invalidateGdprData: (tenantId: string, topic: string, gdprId: string) => Promise<void>;
18
19
  introduceGdprOnEventField: (tenantId: string, defaultValue: string, topic: string, eventId: string, fieldName: string) => Promise<void>;
20
+ createStreamSnapshot: (tenantId: string, topic: string, stream: string, lastSnapshottedEventId: string, snapshotEvent: PublishEvent) => Promise<void>;
19
21
  close: () => void;
20
22
  }
21
23
  export declare const newClient: (config: ClientConfig) => Promise<Client>;
@@ -11,6 +11,7 @@ const introduceGdpr_1 = require("./introduceGdpr");
11
11
  const invalidateGdpr_1 = require("./invalidateGdpr");
12
12
  const stream_1 = require("./stream");
13
13
  const subscribe_1 = require("./subscribe");
14
+ const getLastEvent_1 = require("./getLastEvent");
14
15
  const newClient = async (config) => {
15
16
  config = (0, config_1.useConfigDefaults)(config);
16
17
  const serviceClient = new management_1.ServiceClient(config.serverAddress, grpc_js_1.credentials.createInsecure(), {
@@ -19,23 +20,43 @@ const newClient = async (config) => {
19
20
  "grpc.keepalive_permit_without_calls": 1,
20
21
  });
21
22
  const closeFunctions = [];
23
+ const getLastEventCheck = async (tenantId, topic) => {
24
+ const now = new Date(new Date().getTime() + 3000);
25
+ const lastEvent = await (0, getLastEvent_1.getLastEvent)(tenantId, topic, serviceClient);
26
+ if (!lastEvent) {
27
+ return null;
28
+ }
29
+ const lastOrderSerial = lastEvent.orderSerial;
30
+ return (lastEvent) => {
31
+ if (!lastEvent) {
32
+ return true;
33
+ }
34
+ if (lastOrderSerial == undefined) {
35
+ return lastEvent.raisedAt > now;
36
+ }
37
+ const orderSerial = lastEvent.orderSerial ? lastEvent.orderSerial : 0;
38
+ return orderSerial > lastOrderSerial;
39
+ };
40
+ };
22
41
  const getStreamIterator = async (topic, tenantId, stream, perPage) => {
23
42
  return {
24
43
  forEach: async (callback) => {
25
- const now = new Date(new Date().getTime() + 3000);
44
+ const lastEventCheck = await getLastEventCheck(tenantId, topic);
45
+ if (!lastEventCheck) {
46
+ return;
47
+ }
26
48
  return await (0, stream_1.getStream)(topic, tenantId, stream, perPage, async (event) => {
27
49
  callback(event);
28
- }, (lastEvent) => {
29
- return !lastEvent || lastEvent.raisedAt > now;
30
- }, serviceClient);
50
+ }, lastEventCheck, serviceClient);
31
51
  },
32
52
  forEachAfterEvent: async (eventId, callback) => {
33
- const now = new Date(new Date().getTime() + 3000);
53
+ const lastEventCheck = await getLastEventCheck(tenantId, topic);
54
+ if (!lastEventCheck) {
55
+ return;
56
+ }
34
57
  return await (0, stream_1.getStreamAfterEvent)(topic, tenantId, stream, eventId, perPage, async (event) => {
35
58
  callback(event);
36
- }, (lastEvent) => {
37
- return !lastEvent || lastEvent.raisedAt > now;
38
- }, serviceClient);
59
+ }, lastEventCheck, serviceClient);
39
60
  },
40
61
  isEmpty: async () => {
41
62
  return (0, stream_1.isStreamEmpty)(topic, tenantId, stream, serviceClient);
@@ -46,17 +67,22 @@ const newClient = async (config) => {
46
67
  getEvent: async (tenantId, topic, eventId) => {
47
68
  return await (0, getEvent_1.getEvent)(tenantId, topic, eventId, serviceClient);
48
69
  },
70
+ getLastEvent: async (tenantId, topic) => {
71
+ return await (0, getLastEvent_1.getLastEvent)(tenantId, topic, serviceClient);
72
+ },
49
73
  iterateAllEvents: async (tenantId, topic, includedEventTypes, perPage, handler) => {
50
- const now = new Date(new Date().getTime() + 3000);
51
- await (0, allEvents_1.getAllEvents)(tenantId, topic, includedEventTypes, perPage, handler, (lastEvent) => {
52
- return !lastEvent || lastEvent.raisedAt > now;
53
- }, serviceClient);
74
+ const lastEventCheck = await getLastEventCheck(tenantId, topic);
75
+ if (!lastEventCheck) {
76
+ return;
77
+ }
78
+ await (0, allEvents_1.getAllEvents)(tenantId, topic, includedEventTypes, perPage, handler, lastEventCheck, serviceClient);
54
79
  },
55
80
  iterateAllEventsAfterEvent: async (tenantId, topic, includedEventTypes, eventId, perPage, handler) => {
56
- const now = new Date(new Date().getTime() + 3000);
57
- await (0, allEvents_1.getAllEventsAfterEvent)(tenantId, topic, includedEventTypes, eventId, perPage, handler, (lastEvent) => {
58
- return !lastEvent || lastEvent.raisedAt > now;
59
- }, serviceClient);
81
+ const lastEventCheck = await getLastEventCheck(tenantId, topic);
82
+ if (!lastEventCheck) {
83
+ return;
84
+ }
85
+ await (0, allEvents_1.getAllEventsAfterEvent)(tenantId, topic, includedEventTypes, eventId, perPage, handler, lastEventCheck, serviceClient);
60
86
  },
61
87
  publish: async (topic, events) => {
62
88
  return await (0, publish_1.sendPublish)(topic, events, serviceClient);
@@ -78,6 +104,10 @@ const newClient = async (config) => {
78
104
  invalidateGdprData: async (tenantId, topic, gdprId) => {
79
105
  return await (0, invalidateGdpr_1.sendInvalidateGdpr)(tenantId, topic, gdprId, serviceClient);
80
106
  },
107
+ createStreamSnapshot: async (tenantId, topic, stream, idOfLastEventThatGotSnapshotted, snapshotEvent) => {
108
+ console.log("aaa");
109
+ return await (0, stream_1.createStreamSnapshot)(tenantId, topic, stream, idOfLastEventThatGotSnapshotted, snapshotEvent, serviceClient);
110
+ },
81
111
  close: () => {
82
112
  closeFunctions.forEach(close => close());
83
113
  },
@@ -2,6 +2,7 @@ import { Event } from "@fraym/proto/freym/streams/management";
2
2
  export interface SubscriptionEvent extends BaseEvent {
3
3
  topic: string;
4
4
  raisedAt: Date;
5
+ orderSerial?: number;
5
6
  }
6
7
  export interface PublishEvent extends BaseEvent {
7
8
  broadcast?: boolean;
@@ -38,6 +38,7 @@ const getSubscriptionEvent = (event) => {
38
38
  causationId: event.metadata ? event.metadata.causationId : undefined,
39
39
  correlationId: event.metadata ? event.metadata.correlationId : undefined,
40
40
  reason: event.reason || undefined,
41
+ orderSerial: event.metadata ? parseInt(event.metadata.orderSerial) : undefined,
41
42
  };
42
43
  };
43
44
  exports.getSubscriptionEvent = getSubscriptionEvent;
@@ -0,0 +1,3 @@
1
+ import { ServiceClient } from "@fraym/proto/freym/streams/management";
2
+ import { SubscriptionEvent } from "./event";
3
+ export declare const getLastEvent: (tenantId: string, topic: string, serviceClient: ServiceClient) => Promise<SubscriptionEvent | null>;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getLastEvent = void 0;
4
+ const event_1 = require("./event");
5
+ const util_1 = require("./util");
6
+ const getLastEvent = async (tenantId, topic, serviceClient) => {
7
+ return (0, util_1.retry)(() => new Promise((resolve, reject) => {
8
+ serviceClient.getLastEvent({
9
+ tenantId,
10
+ topic,
11
+ }, (error, response) => {
12
+ if (error === null || error === void 0 ? void 0 : error.details.includes("unable to find last event")) {
13
+ resolve(null);
14
+ return;
15
+ }
16
+ if (error) {
17
+ reject(error);
18
+ return;
19
+ }
20
+ const event = (0, event_1.getSubscriptionEvent)(response);
21
+ if (event) {
22
+ resolve(event);
23
+ return;
24
+ }
25
+ reject("unable to resolve last event from event data");
26
+ });
27
+ }));
28
+ };
29
+ exports.getLastEvent = getLastEvent;
@@ -1,3 +1,4 @@
1
1
  import { PublishEvent } from "./event";
2
- import { ServiceClient } from "@fraym/proto/freym/streams/management";
2
+ import { PublishEvent as ProtobufPublishEvent, ServiceClient } from "@fraym/proto/freym/streams/management";
3
3
  export declare const sendPublish: (topic: string, events: PublishEvent[], serviceClient: ServiceClient) => Promise<void>;
4
+ export declare const getProtobufPublishEventFromPublishedEvent: (event: PublishEvent) => ProtobufPublishEvent;
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sendPublish = void 0;
3
+ exports.getProtobufPublishEventFromPublishedEvent = exports.sendPublish = void 0;
4
4
  const event_1 = require("./event");
5
5
  const util_1 = require("./util");
6
6
  const sendPublish = async (topic, events, serviceClient) => {
7
7
  return (0, util_1.retry)(() => new Promise((resolve, reject) => {
8
8
  serviceClient.publish({
9
- events: events.map(getProtobufPublishEventFromPublishedEvent),
9
+ events: events.map(exports.getProtobufPublishEventFromPublishedEvent),
10
10
  topic,
11
11
  }, error => {
12
12
  if (error) {
@@ -42,6 +42,7 @@ const getProtobufPublishEventFromPublishedEvent = (event) => {
42
42
  metadata: {
43
43
  causationId: (_b = event.causationId) !== null && _b !== void 0 ? _b : "",
44
44
  correlationId: (_c = event.correlationId) !== null && _c !== void 0 ? _c : "",
45
+ orderSerial: "0",
45
46
  },
46
47
  options: {
47
48
  broadcast: (_d = event.broadcast) !== null && _d !== void 0 ? _d : false,
@@ -53,3 +54,4 @@ const getProtobufPublishEventFromPublishedEvent = (event) => {
53
54
  payload,
54
55
  };
55
56
  };
57
+ exports.getProtobufPublishEventFromPublishedEvent = getProtobufPublishEventFromPublishedEvent;
@@ -1,6 +1,7 @@
1
- import { HandlerFunc } from "./event";
1
+ import { HandlerFunc, PublishEvent } from "./event";
2
2
  import { ServiceClient } from "@fraym/proto/freym/streams/management";
3
3
  import { StopLoadingMoreFunc } from "./util";
4
4
  export declare const getStream: (topic: string, tenantId: string, stream: string, perPage: number, handler: HandlerFunc, stopLoadingMore: StopLoadingMoreFunc, serviceClient: ServiceClient) => Promise<void>;
5
5
  export declare const getStreamAfterEvent: (topic: string, tenantId: string, stream: string, eventId: string, perPage: number, handler: HandlerFunc, stopLoadingMore: StopLoadingMoreFunc, serviceClient: ServiceClient) => Promise<void>;
6
6
  export declare const isStreamEmpty: (topic: string, tenantId: string, stream: string, serviceClient: ServiceClient) => Promise<boolean>;
7
+ export declare const createStreamSnapshot: (tenantId: string, topic: string, stream: string, lastSnapshottedEventId: string, snapshotEvent: PublishEvent, serviceClient: ServiceClient) => Promise<void>;
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isStreamEmpty = exports.getStreamAfterEvent = exports.getStream = void 0;
3
+ exports.createStreamSnapshot = exports.isStreamEmpty = exports.getStreamAfterEvent = exports.getStream = void 0;
4
4
  const event_1 = require("./event");
5
5
  const util_1 = require("./util");
6
+ const publish_1 = require("./publish");
6
7
  const getStream = async (topic, tenantId, stream, perPage, handler, stopLoadingMore, serviceClient) => {
7
8
  let page = 0;
8
9
  while (true) {
@@ -92,3 +93,22 @@ const isStreamEmpty = async (topic, tenantId, stream, serviceClient) => {
92
93
  });
93
94
  };
94
95
  exports.isStreamEmpty = isStreamEmpty;
96
+ const createStreamSnapshot = async (tenantId, topic, stream, lastSnapshottedEventId, snapshotEvent, serviceClient) => {
97
+ console.log("creating snapshot", tenantId, topic, stream, lastSnapshottedEventId);
98
+ return new Promise((resolve, reject) => {
99
+ serviceClient.createStreamSnapshot({
100
+ topic,
101
+ stream,
102
+ tenantId,
103
+ lastSnapshottedEventId,
104
+ snapshotEvent: (0, publish_1.getProtobufPublishEventFromPublishedEvent)(snapshotEvent),
105
+ }, error => {
106
+ if (error) {
107
+ reject(error);
108
+ return;
109
+ }
110
+ resolve();
111
+ });
112
+ });
113
+ };
114
+ exports.createStreamSnapshot = createStreamSnapshot;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraym/streams",
3
- "version": "0.7.2",
3
+ "version": "0.9.0",
4
4
  "license": "MIT",
5
5
  "homepage": "https://github.com/fraym/streams-nodejs",
6
6
  "repository": {
@@ -26,8 +26,8 @@
26
26
  "main": "dist/index.js",
27
27
  "types": "dist/index.d.ts",
28
28
  "dependencies": {
29
- "@fraym/proto": "^0.14.0",
30
- "@grpc/grpc-js": "^1.10.0",
29
+ "@fraym/proto": "^0.19.0",
30
+ "@grpc/grpc-js": "^1.11.1",
31
31
  "uuid": "^9.0.1"
32
32
  },
33
33
  "devDependencies": {