@fraym/streams 0.8.0 → 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>;
@@ -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);
@@ -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;
@@ -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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraym/streams",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "license": "MIT",
5
5
  "homepage": "https://github.com/fraym/streams-nodejs",
6
6
  "repository": {
@@ -26,7 +26,7 @@
26
26
  "main": "dist/index.js",
27
27
  "types": "dist/index.d.ts",
28
28
  "dependencies": {
29
- "@fraym/proto": "^0.18.1",
29
+ "@fraym/proto": "^0.19.0",
30
30
  "@grpc/grpc-js": "^1.11.1",
31
31
  "uuid": "^9.0.1"
32
32
  },