@fraym/streams 0.5.2 → 0.7.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.
- package/dist/client/allEvents.d.ts +3 -2
- package/dist/client/allEvents.js +58 -17
- package/dist/client/client.d.ts +7 -8
- package/dist/client/client.js +29 -40
- package/dist/client/event.d.ts +2 -2
- package/dist/client/event.js +8 -14
- package/dist/client/getEvent.d.ts +1 -1
- package/dist/client/getEvent.js +6 -4
- package/dist/client/introduceGdpr.d.ts +1 -2
- package/dist/client/introduceGdpr.js +6 -22
- package/dist/client/invalidateGdpr.d.ts +1 -1
- package/dist/client/invalidateGdpr.js +4 -3
- package/dist/client/publish.d.ts +1 -1
- package/dist/client/publish.js +23 -23
- package/dist/client/stream.d.ts +4 -4
- package/dist/client/stream.js +47 -10
- package/dist/client/subscribe.d.ts +12 -2
- package/dist/client/subscribe.js +127 -25
- package/dist/client/util.d.ts +2 -0
- package/dist/client/util.js +21 -0
- package/package.json +3 -3
- package/dist/client/errors/alreadySubscribed.d.ts +0 -3
- package/dist/client/errors/alreadySubscribed.js +0 -9
- package/dist/client/handler.d.ts +0 -8
- package/dist/client/handler.js +0 -82
- package/dist/client/init.d.ts +0 -5
- package/dist/client/init.js +0 -34
- package/dist/client/snapshot.d.ts +0 -2
- package/dist/client/snapshot.js +0 -18
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import { ServiceClient } from "@fraym/proto/freym/streams/
|
|
1
|
+
import { ServiceClient } from "@fraym/proto/freym/streams/management";
|
|
2
2
|
import { HandlerFunc } from "./event";
|
|
3
|
-
export declare const getAllEvents: (tenantId: string, topic: string,
|
|
3
|
+
export declare const getAllEvents: (tenantId: string, topic: string, types: string[], perPage: number, handler: HandlerFunc, serviceClient: ServiceClient) => Promise<void>;
|
|
4
|
+
export declare const getAllEventsAfterEvent: (tenantId: string, topic: string, types: string[], eventId: string, perPage: number, handler: HandlerFunc, serviceClient: ServiceClient) => Promise<void>;
|
package/dist/client/allEvents.js
CHANGED
|
@@ -1,33 +1,74 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getAllEvents = void 0;
|
|
3
|
+
exports.getAllEventsAfterEvent = exports.getAllEvents = void 0;
|
|
4
4
|
const event_1 = require("./event");
|
|
5
|
-
const
|
|
5
|
+
const util_1 = require("./util");
|
|
6
|
+
const getAllEvents = async (tenantId, topic, types, perPage, handler, serviceClient) => {
|
|
6
7
|
let page = 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
while (true) {
|
|
9
|
+
const events = await getEventPage(tenantId, topic, types, perPage, page, serviceClient);
|
|
10
|
+
if (events.length === 0) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
for (const eventData of events) {
|
|
14
|
+
const event = (0, event_1.getSubscriptionEvent)(eventData);
|
|
15
|
+
if (event) {
|
|
16
|
+
await handler(event);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
page++;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
exports.getAllEvents = getAllEvents;
|
|
23
|
+
const getEventPage = async (tenantId, topic, types, perPage, page, serviceClient) => {
|
|
24
|
+
return (0, util_1.retry)(() => new Promise((resolve, reject) => {
|
|
25
|
+
serviceClient.paginateEvents({
|
|
10
26
|
tenantId,
|
|
11
27
|
topic,
|
|
12
|
-
|
|
28
|
+
types,
|
|
13
29
|
page: page.toString(),
|
|
14
30
|
perPage: perPage.toString(),
|
|
15
31
|
}, async (error, data) => {
|
|
16
32
|
if (error) {
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
if (data.events.length === 0) {
|
|
20
|
-
finished = true;
|
|
33
|
+
reject(error);
|
|
21
34
|
return;
|
|
22
35
|
}
|
|
23
|
-
|
|
24
|
-
const event = (0, event_1.getSubscriptionEvent)(eventData);
|
|
25
|
-
if (event) {
|
|
26
|
-
await handler(event);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
36
|
+
resolve(data.events);
|
|
29
37
|
});
|
|
38
|
+
}));
|
|
39
|
+
};
|
|
40
|
+
const getAllEventsAfterEvent = async (tenantId, topic, types, eventId, perPage, handler, serviceClient) => {
|
|
41
|
+
let page = 0;
|
|
42
|
+
while (true) {
|
|
43
|
+
const events = await getEventPageAfterEvent(tenantId, topic, types, eventId, perPage, page, serviceClient);
|
|
44
|
+
if (events.length === 0) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
for (const eventData of events) {
|
|
48
|
+
const event = (0, event_1.getSubscriptionEvent)(eventData);
|
|
49
|
+
if (event) {
|
|
50
|
+
await handler(event);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
30
53
|
page++;
|
|
31
54
|
}
|
|
32
55
|
};
|
|
33
|
-
exports.
|
|
56
|
+
exports.getAllEventsAfterEvent = getAllEventsAfterEvent;
|
|
57
|
+
const getEventPageAfterEvent = async (tenantId, topic, types, eventId, perPage, page, serviceClient) => {
|
|
58
|
+
return (0, util_1.retry)(() => new Promise((resolve, reject) => {
|
|
59
|
+
serviceClient.paginateEventsAfterEventId({
|
|
60
|
+
tenantId,
|
|
61
|
+
topic,
|
|
62
|
+
types,
|
|
63
|
+
eventId,
|
|
64
|
+
page: page.toString(),
|
|
65
|
+
perPage: perPage.toString(),
|
|
66
|
+
}, async (error, data) => {
|
|
67
|
+
if (error) {
|
|
68
|
+
reject(error);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
resolve(data.events);
|
|
72
|
+
});
|
|
73
|
+
}));
|
|
74
|
+
};
|
package/dist/client/client.d.ts
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
import { ServiceClient } from "@fraym/proto/freym/streams/
|
|
1
|
+
import { ServiceClient } from "@fraym/proto/freym/streams/management";
|
|
2
2
|
import { ClientConfig } from "./config";
|
|
3
3
|
import { HandlerFunc, PublishEvent, SubscriptionEvent } from "./event";
|
|
4
|
+
import { Subscription } from "./subscribe";
|
|
4
5
|
export interface StreamIterator {
|
|
5
6
|
forEach: (callback: (event: SubscriptionEvent) => void) => Promise<void>;
|
|
7
|
+
forEachAfterEvent: (eventId: string, callback: (event: SubscriptionEvent) => void) => Promise<void>;
|
|
6
8
|
isEmpty: () => Promise<boolean>;
|
|
7
9
|
}
|
|
8
10
|
export interface Client {
|
|
9
|
-
getAllEvents: (tenantId: string, topic: string, includedEventTypes: string[], perPage: number, handler: HandlerFunc) => Promise<void>;
|
|
10
11
|
getEvent: (tenantId: string, topic: string, eventId: string) => Promise<SubscriptionEvent>;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
useEventHandlerForAllEventTypes: (handler: HandlerFunc) => void;
|
|
14
|
-
subscribe: (includedTopics?: string[], excludedTopics?: string[]) => Promise<void>;
|
|
12
|
+
iterateAllEvents: (tenantId: string, topic: string, includedEventTypes: string[], perPage: number, handler: HandlerFunc) => Promise<void>;
|
|
13
|
+
iterateAllEventsAfterEvent: (tenantId: string, topic: string, includedEventTypes: string[], eventId: string, perPage: number, handler: HandlerFunc) => Promise<void>;
|
|
15
14
|
publish: (topic: string, events: PublishEvent[]) => Promise<void>;
|
|
15
|
+
getStreamItarator: (topic: string, tenantId: string, stream: string, perPage: number) => Promise<StreamIterator>;
|
|
16
|
+
subscribe: (topics?: string[], ignoreUnhandledEvents?: boolean) => Subscription;
|
|
16
17
|
invalidateGdprData: (tenantId: string, topic: string, gdprId: string) => Promise<void>;
|
|
17
|
-
introduceGdprOnField: (defaultValue: string, topic: string, eventType: string, fieldName: string, serviceClient: ServiceClient) => Promise<void>;
|
|
18
18
|
introduceGdprOnEventField: (tenantId: string, defaultValue: string, topic: string, eventId: string, fieldName: string, serviceClient: ServiceClient) => Promise<void>;
|
|
19
|
-
createSnapshot: (topic: string, toTime: Date) => Promise<void>;
|
|
20
19
|
close: () => void;
|
|
21
20
|
}
|
|
22
21
|
export declare const newClient: (config: ClientConfig) => Promise<Client>;
|
package/dist/client/client.js
CHANGED
|
@@ -1,66 +1,58 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.newClient = void 0;
|
|
4
|
-
const
|
|
4
|
+
const management_1 = require("@fraym/proto/freym/streams/management");
|
|
5
5
|
const grpc_js_1 = require("@grpc/grpc-js");
|
|
6
|
-
const allEvents_1 = require("./allEvents");
|
|
7
6
|
const config_1 = require("./config");
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const init_1 = require("./init");
|
|
11
|
-
const invalidateGdpr_1 = require("./invalidateGdpr");
|
|
7
|
+
const getEvent_1 = require("./getEvent");
|
|
8
|
+
const allEvents_1 = require("./allEvents");
|
|
12
9
|
const publish_1 = require("./publish");
|
|
13
|
-
const
|
|
10
|
+
const introduceGdpr_1 = require("./introduceGdpr");
|
|
11
|
+
const invalidateGdpr_1 = require("./invalidateGdpr");
|
|
14
12
|
const stream_1 = require("./stream");
|
|
15
13
|
const subscribe_1 = require("./subscribe");
|
|
16
|
-
const getEvent_1 = require("./getEvent");
|
|
17
|
-
const introduceGdpr_1 = require("./introduceGdpr");
|
|
18
14
|
const newClient = async (config) => {
|
|
19
15
|
config = (0, config_1.useConfigDefaults)(config);
|
|
20
|
-
const serviceClient = new
|
|
16
|
+
const serviceClient = new management_1.ServiceClient(config.serverAddress, grpc_js_1.credentials.createInsecure(), {
|
|
21
17
|
"grpc.keepalive_time_ms": config.keepaliveInterval,
|
|
22
18
|
"grpc.keepalive_timeout_ms": config.keepaliveTimeout,
|
|
23
19
|
"grpc.keepalive_permit_without_calls": 1,
|
|
24
20
|
});
|
|
25
|
-
const
|
|
26
|
-
const eventHandler = (0, handler_1.initEventHandler)(stream);
|
|
27
|
-
let hasSubscribed = false;
|
|
21
|
+
const closeFunctions = [];
|
|
28
22
|
return {
|
|
29
|
-
getAllEvents: async (tenantId, topic, includedEventTypes, perPage, handler) => {
|
|
30
|
-
await (0, allEvents_1.getAllEvents)(tenantId, topic, includedEventTypes, perPage, handler, serviceClient);
|
|
31
|
-
},
|
|
32
23
|
getEvent: async (tenantId, topic, eventId) => {
|
|
33
24
|
return await (0, getEvent_1.getEvent)(tenantId, topic, eventId, serviceClient);
|
|
34
25
|
},
|
|
35
|
-
|
|
26
|
+
iterateAllEvents: async (tenantId, topic, includedEventTypes, perPage, handler) => {
|
|
27
|
+
await (0, allEvents_1.getAllEvents)(tenantId, topic, includedEventTypes, perPage, handler, serviceClient);
|
|
28
|
+
},
|
|
29
|
+
iterateAllEventsAfterEvent: async (tenantId, topic, includedEventTypes, eventId, perPage, handler) => {
|
|
30
|
+
await (0, allEvents_1.getAllEventsAfterEvent)(tenantId, topic, includedEventTypes, eventId, perPage, handler, serviceClient);
|
|
31
|
+
},
|
|
32
|
+
publish: async (topic, events) => {
|
|
33
|
+
return (0, publish_1.sendPublish)(topic, events, serviceClient);
|
|
34
|
+
},
|
|
35
|
+
getStreamItarator: async (topic, tenantId, stream, perPage) => {
|
|
36
36
|
return {
|
|
37
37
|
forEach: async (callback) => {
|
|
38
|
-
return await (0, stream_1.getStream)(tenantId, stream, perPage, async (event) => {
|
|
38
|
+
return await (0, stream_1.getStream)(topic, tenantId, stream, perPage, async (event) => {
|
|
39
|
+
callback(event);
|
|
40
|
+
}, serviceClient);
|
|
41
|
+
},
|
|
42
|
+
forEachAfterEvent: async (eventId, callback) => {
|
|
43
|
+
return await (0, stream_1.getStreamAfterEvent)(topic, tenantId, stream, eventId, perPage, async (event) => {
|
|
39
44
|
callback(event);
|
|
40
45
|
}, serviceClient);
|
|
41
46
|
},
|
|
42
47
|
isEmpty: async () => {
|
|
43
|
-
return (0, stream_1.isStreamEmpty)(tenantId, stream, serviceClient);
|
|
48
|
+
return (0, stream_1.isStreamEmpty)(topic, tenantId, stream, serviceClient);
|
|
44
49
|
},
|
|
45
50
|
};
|
|
46
51
|
},
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
eventHandler.addHandlerForAllTypes(handler);
|
|
52
|
-
},
|
|
53
|
-
subscribe: async (includedTopics = [], excludedTopics = []) => {
|
|
54
|
-
if (hasSubscribed) {
|
|
55
|
-
throw new alreadySubscribed_1.AlreadySubscribedError();
|
|
56
|
-
}
|
|
57
|
-
return await (0, subscribe_1.sendSubscribe)(includedTopics, excludedTopics, config, stream);
|
|
58
|
-
},
|
|
59
|
-
publish: async (topic, events) => {
|
|
60
|
-
return (0, publish_1.sendPublish)(topic, events, serviceClient);
|
|
61
|
-
},
|
|
62
|
-
introduceGdprOnField: async (defaultValue, topic, eventType, fieldName) => {
|
|
63
|
-
return await (0, introduceGdpr_1.introduceGdprOnField)(defaultValue, topic, eventType, fieldName, serviceClient);
|
|
52
|
+
subscribe: (topics = [], ignoreUnhandledEvents = false) => {
|
|
53
|
+
const subscription = (0, subscribe_1.newSubscription)(topics, ignoreUnhandledEvents, config, serviceClient);
|
|
54
|
+
closeFunctions.push(subscription.stop);
|
|
55
|
+
return subscription;
|
|
64
56
|
},
|
|
65
57
|
introduceGdprOnEventField: async (tenantId, defaultValue, topic, eventId, fieldName) => {
|
|
66
58
|
return (0, introduceGdpr_1.introduceGdprOnEventField)(tenantId, defaultValue, topic, eventId, fieldName, serviceClient);
|
|
@@ -68,11 +60,8 @@ const newClient = async (config) => {
|
|
|
68
60
|
invalidateGdprData: async (tenantId, topic, gdprId) => {
|
|
69
61
|
return await (0, invalidateGdpr_1.sendInvalidateGdpr)(tenantId, topic, gdprId, serviceClient);
|
|
70
62
|
},
|
|
71
|
-
createSnapshot: async (topic, toTime) => {
|
|
72
|
-
return await (0, snapshot_1.sendSnapshot)(topic, toTime, serviceClient);
|
|
73
|
-
},
|
|
74
63
|
close: () => {
|
|
75
|
-
|
|
64
|
+
closeFunctions.forEach(close => close());
|
|
76
65
|
},
|
|
77
66
|
};
|
|
78
67
|
};
|
package/dist/client/event.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Event } from "@fraym/proto/freym/streams/management";
|
|
2
2
|
export interface SubscriptionEvent extends BaseEvent {
|
|
3
3
|
topic: string;
|
|
4
4
|
raisedAt: Date;
|
|
@@ -25,4 +25,4 @@ export interface GdprEventData {
|
|
|
25
25
|
}
|
|
26
26
|
export declare const isGdprEventData: (value: EventData) => value is GdprEventData;
|
|
27
27
|
export type HandlerFunc = (event: SubscriptionEvent) => Promise<void>;
|
|
28
|
-
export declare const getSubscriptionEvent: (
|
|
28
|
+
export declare const getSubscriptionEvent: (event: Event) => SubscriptionEvent | null;
|
package/dist/client/event.js
CHANGED
|
@@ -9,22 +9,16 @@ const isGdprEventData = (value) => {
|
|
|
9
9
|
value.hasOwnProperty("gdprDefault"));
|
|
10
10
|
};
|
|
11
11
|
exports.isGdprEventData = isGdprEventData;
|
|
12
|
-
const getSubscriptionEvent = (
|
|
13
|
-
const event = eventEnvelope.event;
|
|
14
|
-
if (!event) {
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
12
|
+
const getSubscriptionEvent = (event) => {
|
|
17
13
|
const payload = {};
|
|
18
14
|
for (const key in event.payload) {
|
|
19
15
|
if (Object.prototype.hasOwnProperty.call(event.payload, key)) {
|
|
20
16
|
const data = event.payload[key];
|
|
21
|
-
if (data.
|
|
17
|
+
if (data.gdpr) {
|
|
22
18
|
payload[key] = {
|
|
23
19
|
value: JSON.parse(data.value),
|
|
24
|
-
gdprDefault: data.
|
|
25
|
-
|
|
26
|
-
: "",
|
|
27
|
-
isInvalidated: data.metadata.gdpr.invalidated,
|
|
20
|
+
gdprDefault: data.gdpr.default ? JSON.parse(data.gdpr.default) : "",
|
|
21
|
+
isInvalidated: data.gdpr.isInvalidated,
|
|
28
22
|
};
|
|
29
23
|
}
|
|
30
24
|
else {
|
|
@@ -34,14 +28,14 @@ const getSubscriptionEvent = (eventEnvelope) => {
|
|
|
34
28
|
}
|
|
35
29
|
return {
|
|
36
30
|
id: event.id,
|
|
37
|
-
topic:
|
|
38
|
-
tenantId:
|
|
31
|
+
topic: event.topic,
|
|
32
|
+
tenantId: event.tenantId,
|
|
39
33
|
payload,
|
|
40
34
|
raisedAt: new Date(parseInt(event.raisedAt.slice(0, -6))),
|
|
41
35
|
stream: event.stream || undefined,
|
|
42
36
|
type: event.type || undefined,
|
|
43
|
-
causationId: event.causationId
|
|
44
|
-
correlationId: event.correlationId
|
|
37
|
+
causationId: event.metadata ? event.metadata.causationId : undefined,
|
|
38
|
+
correlationId: event.metadata ? event.metadata.correlationId : undefined,
|
|
45
39
|
reason: event.reason || undefined,
|
|
46
40
|
};
|
|
47
41
|
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { ServiceClient } from "@fraym/proto/freym/streams/
|
|
1
|
+
import { ServiceClient } from "@fraym/proto/freym/streams/management";
|
|
2
2
|
import { SubscriptionEvent } from "./event";
|
|
3
3
|
export declare const getEvent: (tenantId: string, topic: string, eventId: string, serviceClient: ServiceClient) => Promise<SubscriptionEvent>;
|
package/dist/client/getEvent.js
CHANGED
|
@@ -2,23 +2,25 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getEvent = void 0;
|
|
4
4
|
const event_1 = require("./event");
|
|
5
|
+
const util_1 = require("./util");
|
|
5
6
|
const getEvent = async (tenantId, topic, eventId, serviceClient) => {
|
|
6
|
-
return new Promise((resolve, reject) => {
|
|
7
|
+
return (0, util_1.retry)(() => new Promise((resolve, reject) => {
|
|
7
8
|
serviceClient.getEvent({
|
|
8
9
|
tenantId,
|
|
9
10
|
topic,
|
|
10
|
-
eventId,
|
|
11
|
+
id: eventId,
|
|
11
12
|
}, (error, response) => {
|
|
12
13
|
if (error) {
|
|
13
|
-
reject(error
|
|
14
|
+
reject(error);
|
|
14
15
|
return;
|
|
15
16
|
}
|
|
16
17
|
const event = (0, event_1.getSubscriptionEvent)(response);
|
|
17
18
|
if (event) {
|
|
18
19
|
resolve(event);
|
|
20
|
+
return;
|
|
19
21
|
}
|
|
20
22
|
reject("unable to resolve event from event data");
|
|
21
23
|
});
|
|
22
|
-
});
|
|
24
|
+
}));
|
|
23
25
|
};
|
|
24
26
|
exports.getEvent = getEvent;
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import { ServiceClient } from "@fraym/proto/freym/streams/
|
|
2
|
-
export declare const introduceGdprOnField: (defaultValue: string, topic: string, eventType: string, fieldName: string, serviceClient: ServiceClient) => Promise<void>;
|
|
1
|
+
import { ServiceClient } from "@fraym/proto/freym/streams/management";
|
|
3
2
|
export declare const introduceGdprOnEventField: (tenantId: string, defaultValue: string, topic: string, eventId: string, fieldName: string, serviceClient: ServiceClient) => Promise<void>;
|
|
@@ -1,38 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.introduceGdprOnEventField =
|
|
4
|
-
const
|
|
5
|
-
return new Promise((resolve, reject) => {
|
|
6
|
-
serviceClient.introduceGdprOnField({
|
|
7
|
-
defaultValue,
|
|
8
|
-
topic,
|
|
9
|
-
eventType,
|
|
10
|
-
fieldName,
|
|
11
|
-
}, error => {
|
|
12
|
-
if (error) {
|
|
13
|
-
reject(error.message);
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
resolve();
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
};
|
|
20
|
-
exports.introduceGdprOnField = introduceGdprOnField;
|
|
3
|
+
exports.introduceGdprOnEventField = void 0;
|
|
4
|
+
const util_1 = require("./util");
|
|
21
5
|
const introduceGdprOnEventField = async (tenantId, defaultValue, topic, eventId, fieldName, serviceClient) => {
|
|
22
|
-
return new Promise((resolve, reject) => {
|
|
6
|
+
return (0, util_1.retry)(() => new Promise((resolve, reject) => {
|
|
23
7
|
serviceClient.introduceGdprOnEventField({
|
|
24
|
-
|
|
8
|
+
tenantId,
|
|
25
9
|
defaultValue,
|
|
26
10
|
topic,
|
|
27
11
|
eventId,
|
|
28
12
|
fieldName,
|
|
29
13
|
}, error => {
|
|
30
14
|
if (error) {
|
|
31
|
-
reject(error
|
|
15
|
+
reject(error);
|
|
32
16
|
return;
|
|
33
17
|
}
|
|
34
18
|
resolve();
|
|
35
19
|
});
|
|
36
|
-
});
|
|
20
|
+
}));
|
|
37
21
|
};
|
|
38
22
|
exports.introduceGdprOnEventField = introduceGdprOnEventField;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { ServiceClient } from "@fraym/proto/freym/streams/
|
|
1
|
+
import { ServiceClient } from "@fraym/proto/freym/streams/management";
|
|
2
2
|
export declare const sendInvalidateGdpr: (tenantId: string, topic: string, gdprId: string, serviceClient: ServiceClient) => Promise<void>;
|
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.sendInvalidateGdpr = void 0;
|
|
4
|
+
const util_1 = require("./util");
|
|
4
5
|
const sendInvalidateGdpr = async (tenantId, topic, gdprId, serviceClient) => {
|
|
5
|
-
return new Promise((resolve, reject) => {
|
|
6
|
+
return (0, util_1.retry)(() => new Promise((resolve, reject) => {
|
|
6
7
|
serviceClient.invalidateGdpr({
|
|
7
8
|
tenantId,
|
|
8
9
|
topic,
|
|
9
10
|
gdprId,
|
|
10
11
|
}, error => {
|
|
11
12
|
if (error) {
|
|
12
|
-
reject(error
|
|
13
|
+
reject(error);
|
|
13
14
|
return;
|
|
14
15
|
}
|
|
15
16
|
resolve();
|
|
16
17
|
});
|
|
17
|
-
});
|
|
18
|
+
}));
|
|
18
19
|
};
|
|
19
20
|
exports.sendInvalidateGdpr = sendInvalidateGdpr;
|
package/dist/client/publish.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { PublishEvent } from "./event";
|
|
2
|
-
import { ServiceClient } from "@fraym/proto/freym/streams/
|
|
2
|
+
import { ServiceClient } from "@fraym/proto/freym/streams/management";
|
|
3
3
|
export declare const sendPublish: (topic: string, events: PublishEvent[], serviceClient: ServiceClient) => Promise<void>;
|
package/dist/client/publish.js
CHANGED
|
@@ -2,22 +2,23 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.sendPublish = void 0;
|
|
4
4
|
const event_1 = require("./event");
|
|
5
|
+
const util_1 = require("./util");
|
|
5
6
|
const sendPublish = async (topic, events, serviceClient) => {
|
|
6
|
-
return new Promise((resolve, reject) => {
|
|
7
|
+
return (0, util_1.retry)(() => new Promise((resolve, reject) => {
|
|
7
8
|
serviceClient.publish({
|
|
8
|
-
events: events.map(
|
|
9
|
+
events: events.map(getProtobufPublishEventFromPublishedEvent),
|
|
9
10
|
topic,
|
|
10
11
|
}, error => {
|
|
11
12
|
if (error) {
|
|
12
|
-
reject(error
|
|
13
|
+
reject(error);
|
|
13
14
|
return;
|
|
14
15
|
}
|
|
15
16
|
resolve();
|
|
16
17
|
});
|
|
17
|
-
});
|
|
18
|
+
}));
|
|
18
19
|
};
|
|
19
20
|
exports.sendPublish = sendPublish;
|
|
20
|
-
const
|
|
21
|
+
const getProtobufPublishEventFromPublishedEvent = (event) => {
|
|
21
22
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
22
23
|
const payload = {};
|
|
23
24
|
for (const key in event.payload) {
|
|
@@ -25,31 +26,30 @@ const getEventEnvelopeFromPublishedEvent = (event) => {
|
|
|
25
26
|
payload[key] = (0, event_1.isGdprEventData)(currentData)
|
|
26
27
|
? {
|
|
27
28
|
value: JSON.stringify(currentData.value),
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
id: (_a = currentData.id) !== null && _a !== void 0 ? _a : "",
|
|
33
|
-
invalidated: false,
|
|
34
|
-
},
|
|
29
|
+
gdpr: {
|
|
30
|
+
default: JSON.stringify(currentData.gdprDefault),
|
|
31
|
+
id: (_a = currentData.id) !== null && _a !== void 0 ? _a : "",
|
|
32
|
+
isInvalidated: false,
|
|
35
33
|
},
|
|
36
34
|
}
|
|
37
35
|
: {
|
|
38
36
|
value: JSON.stringify(currentData),
|
|
37
|
+
gdpr: undefined,
|
|
39
38
|
};
|
|
40
39
|
}
|
|
41
40
|
return {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
correlationId: (_f = event.correlationId) !== null && _f !== void 0 ? _f : "",
|
|
50
|
-
causationId: (_g = event.causationId) !== null && _g !== void 0 ? _g : "",
|
|
51
|
-
payload,
|
|
52
|
-
raisedAt: "0",
|
|
41
|
+
id: event.id,
|
|
42
|
+
metadata: {
|
|
43
|
+
causationId: (_b = event.causationId) !== null && _b !== void 0 ? _b : "",
|
|
44
|
+
correlationId: (_c = event.correlationId) !== null && _c !== void 0 ? _c : "",
|
|
45
|
+
},
|
|
46
|
+
options: {
|
|
47
|
+
broadcast: (_d = event.broadcast) !== null && _d !== void 0 ? _d : false,
|
|
53
48
|
},
|
|
49
|
+
reason: (_e = event.reason) !== null && _e !== void 0 ? _e : "",
|
|
50
|
+
stream: (_f = event.stream) !== null && _f !== void 0 ? _f : "",
|
|
51
|
+
tenantId: event.tenantId,
|
|
52
|
+
type: (_g = event.type) !== null && _g !== void 0 ? _g : "",
|
|
53
|
+
payload,
|
|
54
54
|
};
|
|
55
55
|
};
|
package/dist/client/stream.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { HandlerFunc } from "./event";
|
|
2
|
-
import { ServiceClient
|
|
3
|
-
export declare const getStream: (tenantId: string, stream: string, perPage: number, handler: HandlerFunc, serviceClient: ServiceClient) => Promise<void>;
|
|
4
|
-
export declare const
|
|
5
|
-
export declare const isStreamEmpty: (tenantId: string, stream: string, serviceClient: ServiceClient) => Promise<boolean>;
|
|
2
|
+
import { ServiceClient } from "@fraym/proto/freym/streams/management";
|
|
3
|
+
export declare const getStream: (topic: string, tenantId: string, stream: string, perPage: number, handler: HandlerFunc, serviceClient: ServiceClient) => Promise<void>;
|
|
4
|
+
export declare const getStreamAfterEvent: (topic: string, tenantId: string, stream: string, eventId: string, perPage: number, handler: HandlerFunc, serviceClient: ServiceClient) => Promise<void>;
|
|
5
|
+
export declare const isStreamEmpty: (topic: string, tenantId: string, stream: string, serviceClient: ServiceClient) => Promise<boolean>;
|
package/dist/client/stream.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isStreamEmpty = exports.
|
|
3
|
+
exports.isStreamEmpty = exports.getStreamAfterEvent = exports.getStream = void 0;
|
|
4
4
|
const event_1 = require("./event");
|
|
5
|
-
const
|
|
5
|
+
const util_1 = require("./util");
|
|
6
|
+
const getStream = async (topic, tenantId, stream, perPage, handler, serviceClient) => {
|
|
6
7
|
let page = 0;
|
|
7
8
|
while (true) {
|
|
8
|
-
const events = await (
|
|
9
|
+
const events = await getStreamPage(topic, tenantId, stream, perPage, page, serviceClient);
|
|
9
10
|
if (events.length === 0) {
|
|
10
11
|
return;
|
|
11
12
|
}
|
|
@@ -19,31 +20,67 @@ const getStream = async (tenantId, stream, perPage, handler, serviceClient) => {
|
|
|
19
20
|
}
|
|
20
21
|
};
|
|
21
22
|
exports.getStream = getStream;
|
|
22
|
-
const getStreamPage = async (tenantId, stream, perPage, page, serviceClient) => {
|
|
23
|
-
return new Promise((resolve, reject) => {
|
|
23
|
+
const getStreamPage = async (topic, tenantId, stream, perPage, page, serviceClient) => {
|
|
24
|
+
return (0, util_1.retry)(() => new Promise((resolve, reject) => {
|
|
24
25
|
serviceClient.paginateStream({
|
|
26
|
+
stream,
|
|
25
27
|
tenantId,
|
|
28
|
+
topic,
|
|
29
|
+
page: page.toString(),
|
|
30
|
+
perPage: perPage.toString(),
|
|
31
|
+
}, async (error, data) => {
|
|
32
|
+
if (error) {
|
|
33
|
+
reject(error);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
resolve(data.events);
|
|
37
|
+
});
|
|
38
|
+
}));
|
|
39
|
+
};
|
|
40
|
+
const getStreamAfterEvent = async (topic, tenantId, stream, eventId, perPage, handler, serviceClient) => {
|
|
41
|
+
let page = 0;
|
|
42
|
+
while (true) {
|
|
43
|
+
const events = await getStreamPageAfterEvent(topic, tenantId, stream, eventId, perPage, page, serviceClient);
|
|
44
|
+
if (events.length === 0) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
for (const eventData of events) {
|
|
48
|
+
const event = (0, event_1.getSubscriptionEvent)(eventData);
|
|
49
|
+
if (event) {
|
|
50
|
+
await handler(event);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
page++;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.getStreamAfterEvent = getStreamAfterEvent;
|
|
57
|
+
const getStreamPageAfterEvent = async (topic, tenantId, stream, eventId, perPage, page, serviceClient) => {
|
|
58
|
+
return (0, util_1.retry)(() => new Promise((resolve, reject) => {
|
|
59
|
+
serviceClient.paginateStreamAfterEventId({
|
|
26
60
|
stream,
|
|
61
|
+
tenantId,
|
|
62
|
+
topic,
|
|
63
|
+
eventId,
|
|
27
64
|
page: page.toString(),
|
|
28
65
|
perPage: perPage.toString(),
|
|
29
66
|
}, async (error, data) => {
|
|
30
67
|
if (error) {
|
|
31
|
-
reject(error
|
|
68
|
+
reject(error);
|
|
32
69
|
return;
|
|
33
70
|
}
|
|
34
71
|
resolve(data.events);
|
|
35
72
|
});
|
|
36
|
-
});
|
|
73
|
+
}));
|
|
37
74
|
};
|
|
38
|
-
|
|
39
|
-
const isStreamEmpty = async (tenantId, stream, serviceClient) => {
|
|
75
|
+
const isStreamEmpty = async (topic, tenantId, stream, serviceClient) => {
|
|
40
76
|
return new Promise((resolve, reject) => {
|
|
41
77
|
serviceClient.isStreamEmpty({
|
|
78
|
+
topic,
|
|
42
79
|
stream,
|
|
43
80
|
tenantId,
|
|
44
81
|
}, (error, data) => {
|
|
45
82
|
if (error) {
|
|
46
|
-
reject(error
|
|
83
|
+
reject(error);
|
|
47
84
|
return;
|
|
48
85
|
}
|
|
49
86
|
resolve(data.isEmpty);
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
import { ServiceClient, SubscribeRequest, SubscribeResponse } from "@fraym/proto/freym/streams/management";
|
|
2
|
+
import { ClientDuplexStream } from "@grpc/grpc-js";
|
|
1
3
|
import { ClientConfig } from "./config";
|
|
2
|
-
import {
|
|
3
|
-
export
|
|
4
|
+
import { HandlerFunc } from "./event";
|
|
5
|
+
export interface Subscription {
|
|
6
|
+
useHandler: (type: string, handler: HandlerFunc) => void;
|
|
7
|
+
useHandlerForAllTypes: (handler: HandlerFunc) => void;
|
|
8
|
+
start: () => void;
|
|
9
|
+
stop: () => void;
|
|
10
|
+
}
|
|
11
|
+
export type Stream = ClientDuplexStream<SubscribeRequest, SubscribeResponse>;
|
|
12
|
+
export declare const newSubscription: (topics: string[], ignoreUnhandledEvents: boolean, config: ClientConfig, serviceClient: ServiceClient) => Subscription;
|
|
13
|
+
export declare const initStream: (topics: string[], config: ClientConfig, stream: Stream) => Promise<Stream>;
|
package/dist/client/subscribe.js
CHANGED
|
@@ -1,39 +1,141 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
3
|
+
exports.initStream = exports.newSubscription = void 0;
|
|
4
|
+
const constants_1 = require("@grpc/grpc-js/build/src/constants");
|
|
5
|
+
const uuid_1 = require("uuid");
|
|
6
|
+
const event_1 = require("./event");
|
|
7
|
+
const newSubscription = (topics, ignoreUnhandledEvents, config, serviceClient) => {
|
|
8
|
+
let stream = null;
|
|
9
|
+
let closed = false;
|
|
10
|
+
const typeHandlerMap = {};
|
|
11
|
+
const globalHandlers = [];
|
|
12
|
+
const rebuildConnection = (currentStream, retries) => {
|
|
13
|
+
currentStream.cancel();
|
|
14
|
+
currentStream.removeAllListeners();
|
|
15
|
+
setTimeout(() => {
|
|
16
|
+
stream = null;
|
|
17
|
+
reconnect(retries);
|
|
18
|
+
}, 100);
|
|
19
|
+
};
|
|
20
|
+
const reconnect = async (retries) => {
|
|
21
|
+
const newStream = serviceClient.subscribe();
|
|
22
|
+
newStream.on("end", () => {
|
|
23
|
+
if (closed) {
|
|
24
|
+
newStream.cancel();
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
rebuildConnection(newStream, retries - 1);
|
|
28
|
+
});
|
|
29
|
+
newStream.on("error", (err) => {
|
|
30
|
+
if (closed) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (retries === 0 || (err && err.code && err.code === constants_1.Status.UNKNOWN)) {
|
|
34
|
+
closed = true;
|
|
35
|
+
throw err;
|
|
36
|
+
}
|
|
37
|
+
rebuildConnection(newStream, retries - 1);
|
|
38
|
+
});
|
|
39
|
+
const dataFn = async (data) => {
|
|
40
|
+
var _a, _b, _c, _d;
|
|
41
|
+
if (!data.payload ||
|
|
42
|
+
((_a = data.payload) === null || _a === void 0 ? void 0 : _a.$case) === "panic" ||
|
|
43
|
+
((_b = data.payload) === null || _b === void 0 ? void 0 : _b.$case) === "subscribed") {
|
|
44
|
+
newStream.cancel();
|
|
17
45
|
return;
|
|
18
46
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
stream.off("data", fn);
|
|
22
|
-
resolve();
|
|
47
|
+
const event = (0, event_1.getSubscriptionEvent)(data.payload.event);
|
|
48
|
+
if (!event) {
|
|
23
49
|
return;
|
|
24
50
|
}
|
|
51
|
+
const currentHandlers = (_d = typeHandlerMap[(_c = event.type) !== null && _c !== void 0 ? _c : ""]) !== null && _d !== void 0 ? _d : [];
|
|
52
|
+
currentHandlers.push(...globalHandlers);
|
|
53
|
+
if (currentHandlers.length === 0) {
|
|
54
|
+
if (ignoreUnhandledEvents) {
|
|
55
|
+
newStream.write(newHandledRequest(event.tenantId, event.topic));
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
newStream.write(newHandledRequest(event.tenantId, event.topic, "no handlers for this event, maybe you forgot to register an event handler"));
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
for (const handler of currentHandlers) {
|
|
63
|
+
await handler(event);
|
|
64
|
+
}
|
|
65
|
+
newStream.write(newHandledRequest(event.tenantId, event.topic));
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
newStream.write(newHandledRequest(event.tenantId, event.topic, err));
|
|
69
|
+
throw err;
|
|
70
|
+
}
|
|
25
71
|
};
|
|
26
|
-
stream
|
|
72
|
+
stream = newStream;
|
|
73
|
+
await (0, exports.initStream)(topics, config, newStream);
|
|
74
|
+
retries = 50;
|
|
75
|
+
newStream.on("data", dataFn);
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
useHandler: (type, handler) => {
|
|
79
|
+
if (!typeHandlerMap[type]) {
|
|
80
|
+
typeHandlerMap[type] = [handler];
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
typeHandlerMap[type].push(handler);
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
useHandlerForAllTypes: (handler) => {
|
|
87
|
+
globalHandlers.push(handler);
|
|
88
|
+
},
|
|
89
|
+
start: () => {
|
|
90
|
+
reconnect(50);
|
|
91
|
+
},
|
|
92
|
+
stop: () => {
|
|
93
|
+
if (stream) {
|
|
94
|
+
stream.cancel();
|
|
95
|
+
stream = null;
|
|
96
|
+
}
|
|
97
|
+
closed = true;
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
exports.newSubscription = newSubscription;
|
|
102
|
+
const initStream = async (topics, config, stream) => {
|
|
103
|
+
return new Promise((resolve, reject) => {
|
|
104
|
+
stream.write({
|
|
105
|
+
payload: {
|
|
106
|
+
$case: "subscribe",
|
|
107
|
+
subscribe: {
|
|
108
|
+
metadata: {
|
|
109
|
+
group: config.groupId,
|
|
110
|
+
subscriberId: (0, uuid_1.v4)(),
|
|
111
|
+
},
|
|
112
|
+
topics,
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
stream.once("data", (data) => {
|
|
117
|
+
var _a;
|
|
118
|
+
if (((_a = data.payload) === null || _a === void 0 ? void 0 : _a.$case) !== "subscribed") {
|
|
119
|
+
reject("connection to streams service was not initialized correctly");
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (data.payload.subscribed.error) {
|
|
123
|
+
reject(`unable to subscribe to streams service: ${data.payload.subscribed.error}`);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
resolve(stream);
|
|
127
|
+
});
|
|
27
128
|
});
|
|
28
129
|
};
|
|
29
|
-
exports.
|
|
30
|
-
const
|
|
130
|
+
exports.initStream = initStream;
|
|
131
|
+
const newHandledRequest = (tenantId, topic, error) => {
|
|
31
132
|
return {
|
|
32
133
|
payload: {
|
|
33
|
-
$case: "
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
134
|
+
$case: "handled",
|
|
135
|
+
handled: {
|
|
136
|
+
tenantId,
|
|
137
|
+
topic,
|
|
138
|
+
error: error !== null && error !== void 0 ? error : "",
|
|
37
139
|
},
|
|
38
140
|
},
|
|
39
141
|
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.retry = exports.sleep = void 0;
|
|
4
|
+
const constants_1 = require("@grpc/grpc-js/build/src/constants");
|
|
5
|
+
const sleep = (delay) => new Promise(resolve => {
|
|
6
|
+
setTimeout(() => resolve(), delay);
|
|
7
|
+
});
|
|
8
|
+
exports.sleep = sleep;
|
|
9
|
+
const retry = async (fn, pause = 100, retries = 50) => {
|
|
10
|
+
try {
|
|
11
|
+
return await fn();
|
|
12
|
+
}
|
|
13
|
+
catch (err) {
|
|
14
|
+
if (retries === 0 || (err && err.code && err.code === constants_1.Status.UNKNOWN)) {
|
|
15
|
+
throw err;
|
|
16
|
+
}
|
|
17
|
+
await (0, exports.sleep)(pause);
|
|
18
|
+
return await (0, exports.retry)(fn, pause, retries - 1);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
exports.retry = retry;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fraym/streams",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.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.
|
|
30
|
-
"@grpc/grpc-js": "^1.
|
|
29
|
+
"@fraym/proto": "^0.14.0",
|
|
30
|
+
"@grpc/grpc-js": "^1.10.0",
|
|
31
31
|
"uuid": "^9.0.1"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AlreadySubscribedError = void 0;
|
|
4
|
-
class AlreadySubscribedError extends Error {
|
|
5
|
-
constructor() {
|
|
6
|
-
super("streams client is already subscribed to a set of topics");
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
exports.AlreadySubscribedError = AlreadySubscribedError;
|
package/dist/client/handler.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Stream } from "./init";
|
|
2
|
-
import { HandlerFunc } from "./event";
|
|
3
|
-
interface EventHandler {
|
|
4
|
-
addHandler: (type: string, handler: HandlerFunc) => void;
|
|
5
|
-
addHandlerForAllTypes: (handler: HandlerFunc) => void;
|
|
6
|
-
}
|
|
7
|
-
export declare const initEventHandler: (stream: Stream) => EventHandler;
|
|
8
|
-
export {};
|
package/dist/client/handler.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.initEventHandler = void 0;
|
|
4
|
-
const event_1 = require("./event");
|
|
5
|
-
const initEventHandler = (stream) => {
|
|
6
|
-
const typeHandlerMap = {};
|
|
7
|
-
const globalHandlers = [];
|
|
8
|
-
stream.on("data", (data) => {
|
|
9
|
-
var _a, _b, _c;
|
|
10
|
-
if (((_a = data.data) === null || _a === void 0 ? void 0 : _a.$case) !== "event") {
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
const event = (0, event_1.getSubscriptionEvent)(data.data.event);
|
|
14
|
-
if (!event) {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
const typeHandlers = (_c = typeHandlerMap[(_b = event.type) !== null && _b !== void 0 ? _b : ""]) !== null && _c !== void 0 ? _c : [];
|
|
18
|
-
typeHandlers.forEach(handler => handleEvent(event, handler, stream));
|
|
19
|
-
globalHandlers.forEach(handler => handleEvent(event, handler, stream));
|
|
20
|
-
});
|
|
21
|
-
return {
|
|
22
|
-
addHandler: (type, handler) => {
|
|
23
|
-
if (!typeHandlerMap[type]) {
|
|
24
|
-
typeHandlerMap[type] = [handler];
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
typeHandlerMap[type].push(handler);
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
addHandlerForAllTypes: handler => {
|
|
31
|
-
globalHandlers.push(handler);
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
exports.initEventHandler = initEventHandler;
|
|
36
|
-
const handleEvent = (event, handler, stream) => {
|
|
37
|
-
stream.write(newEventReceivedRequest(event.tenantId, event.topic, event.id));
|
|
38
|
-
handler(event)
|
|
39
|
-
.then(() => {
|
|
40
|
-
stream.write(newEventHandledRequest(event.tenantId, event.topic, event.id));
|
|
41
|
-
})
|
|
42
|
-
.catch(e => {
|
|
43
|
-
stream.write(newEventNotHandledRequest(event.tenantId, event.topic, event.id, e));
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
const newEventReceivedRequest = (tenantId, topic, eventId) => {
|
|
47
|
-
return {
|
|
48
|
-
payload: {
|
|
49
|
-
$case: "eventReceived",
|
|
50
|
-
eventReceived: {
|
|
51
|
-
eventId,
|
|
52
|
-
tenantId,
|
|
53
|
-
topic,
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
};
|
|
57
|
-
};
|
|
58
|
-
const newEventHandledRequest = (tenantId, topic, eventId) => {
|
|
59
|
-
return {
|
|
60
|
-
payload: {
|
|
61
|
-
$case: "eventHandled",
|
|
62
|
-
eventHandled: {
|
|
63
|
-
eventId,
|
|
64
|
-
tenantId,
|
|
65
|
-
topic,
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
};
|
|
70
|
-
const newEventNotHandledRequest = (tenantId, topic, eventId, reason) => {
|
|
71
|
-
return {
|
|
72
|
-
payload: {
|
|
73
|
-
$case: "eventNotHandled",
|
|
74
|
-
eventNotHandled: {
|
|
75
|
-
eventId,
|
|
76
|
-
tenantId,
|
|
77
|
-
topic,
|
|
78
|
-
reason,
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
};
|
|
82
|
-
};
|
package/dist/client/init.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { ClientConfig } from "./config";
|
|
2
|
-
import { ClientDuplexStream } from "@grpc/grpc-js";
|
|
3
|
-
import { SubscribeRequest, ServiceClient, SubscribeResponse } from "@fraym/proto/freym/streams/clientchannel";
|
|
4
|
-
export type Stream = ClientDuplexStream<SubscribeRequest, SubscribeResponse>;
|
|
5
|
-
export declare const initStream: (config: ClientConfig, serviceClient: ServiceClient) => Promise<Stream>;
|
package/dist/client/init.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.initStream = void 0;
|
|
4
|
-
const uuid_1 = require("uuid");
|
|
5
|
-
const initStream = async (config, serviceClient) => {
|
|
6
|
-
const stream = serviceClient.subscribe();
|
|
7
|
-
stream.on("end", stream.end);
|
|
8
|
-
return new Promise((resolve, reject) => {
|
|
9
|
-
stream.once("data", (data) => {
|
|
10
|
-
var _a;
|
|
11
|
-
if (((_a = data.data) === null || _a === void 0 ? void 0 : _a.$case) !== "initAck") {
|
|
12
|
-
reject("connection to streams service was not initialized correctly");
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
resolve(stream);
|
|
16
|
-
});
|
|
17
|
-
stream.write(newInitRequest(config));
|
|
18
|
-
});
|
|
19
|
-
};
|
|
20
|
-
exports.initStream = initStream;
|
|
21
|
-
const newInitAction = (config) => {
|
|
22
|
-
return {
|
|
23
|
-
groupId: config.groupId,
|
|
24
|
-
subscriberId: (0, uuid_1.v4)(),
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
const newInitRequest = (config) => {
|
|
28
|
-
return {
|
|
29
|
-
payload: {
|
|
30
|
-
$case: "init",
|
|
31
|
-
init: newInitAction(config),
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
};
|
package/dist/client/snapshot.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sendSnapshot = void 0;
|
|
4
|
-
const sendSnapshot = async (topic, toTime, serviceClient) => {
|
|
5
|
-
return new Promise((resolve, reject) => {
|
|
6
|
-
serviceClient.snapshot({
|
|
7
|
-
topic,
|
|
8
|
-
toTime: toTime.toISOString(),
|
|
9
|
-
}, error => {
|
|
10
|
-
if (error) {
|
|
11
|
-
reject(error.message);
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
resolve();
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
};
|
|
18
|
-
exports.sendSnapshot = sendSnapshot;
|