woonplan-packages-redishelper 2.0.96 → 2.0.97

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.
@@ -1,332 +1,333 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- const class_validator_1 = require("class-validator");
5
- const ioredis_1 = tslib_1.__importDefault(require("ioredis"));
6
- const rollbar_1 = tslib_1.__importDefault(require("rollbar"));
7
- const utils_1 = require("../services/utils");
8
- const Listener_1 = tslib_1.__importDefault(require("./Listener"));
9
- const ListListener_1 = tslib_1.__importDefault(require("./ListListener"));
10
- const uuid_1 = require("uuid");
11
- const lock_1 = require("../services/lock");
12
- const SetWatcher_1 = tslib_1.__importDefault(require("./SetWatcher"));
13
- class Broker {
14
- constructor(redisConfig, rollbarConfig, service, consumer) {
15
- this.consumername = '';
16
- this.listeners = new Map();
17
- this.listprefix = 'listUpdated';
18
- this.subscriptions = [];
19
- this.rejectors = new Map;
20
- this.resolvers = new Map;
21
- this.timeouts = new Map;
22
- this.redisConfig = redisConfig;
23
- this.rollbar = new rollbar_1.default({
24
- accessToken: rollbarConfig.accessToken,
25
- environment: rollbarConfig.environment,
26
- });
27
- this.writer = new ioredis_1.default({
28
- host: redisConfig.REDISURL,
29
- password: redisConfig.REDISPW ?? ''
30
- });
31
- this.reader = new ioredis_1.default({
32
- host: redisConfig.REDISURL,
33
- password: redisConfig.REDISPW ?? ''
34
- });
35
- (0, lock_1.setupLock)(redisConfig);
36
- this.reader.on('message', (channel, message) => this.onMessage.call(this, channel, message));
37
- this.consumername = consumer;
38
- this.service = service;
39
- }
40
- get requeststream() {
41
- return this.getRequestStream(this.service);
42
- }
43
- getRequestStream(service) {
44
- return `keyRequestedFrom${(0, utils_1.capitalizeFirstLetter)(service)}Service`;
45
- }
46
- createClient() {
47
- return new ioredis_1.default({
48
- host: this.redisConfig.REDISURL,
49
- password: this.redisConfig.REDISPW ?? ''
50
- });
51
- }
52
- createSetWatcher(setname, callback, finishedCallback, itemsPerCall = 1) {
53
- new SetWatcher_1.default(this.createClient.call(this), setname, callback, finishedCallback, itemsPerCall);
54
- }
55
- setRequestEndpoint(callback) {
56
- this.addListener(this.requeststream, this.getRequestCallback(callback), this.service);
57
- this.addListener(this.requeststream, this.getRequestCallback(callback), this.service);
58
- this.addListener(this.requeststream, this.getRequestCallback(callback), this.service);
59
- this.addListener(this.requeststream, this.getRequestCallback(callback), this.service);
60
- this.addListener(this.requeststream, this.getRequestCallback(callback), this.service);
61
- return this;
62
- }
63
- getRequestCallback(callback) {
64
- return async (id, parameters) => {
65
- let result = null;
66
- try {
67
- result = await callback(id, parameters);
68
- }
69
- catch (error) {
70
- this.throwError(error);
71
- result = JSON.stringify({
72
- rejected: true,
73
- error: {
74
- title: error?.message ?? ''
75
- }
76
- });
77
- }
78
- finally {
79
- if (!parameters.messageid)
80
- return null;
81
- const channel = this.getRequestSubscriptionName(parameters.messageid);
82
- return this.publish(channel, result);
83
- }
84
- };
85
- }
86
- publish(channel, result) {
87
- return this.writer.publish(channel, result);
88
- }
89
- async createGroup(stream, group) {
90
- try {
91
- await this.writer.xgroup('CREATE', stream, group, '$', 'MKSTREAM');
92
- return;
93
- }
94
- catch {
95
- return;
96
- }
97
- }
98
- addListener(stream, callback, group, deleteOnCompletion = false) {
99
- const client = new ioredis_1.default({
100
- host: this.redisConfig.REDISURL,
101
- password: this.redisConfig.REDISPW ?? ''
102
- });
103
- this.createListener.call(this, client, stream, callback, group, deleteOnCompletion);
104
- // dont wait for the result of create listener, just return this so we can chain
105
- return this;
106
- }
107
- async createListener(client, stream, callback, group, deleteOnCompletion = false) {
108
- if (group)
109
- await this.createGroup.call(this, stream, group);
110
- this.listeners.set(stream, new Listener_1.default(this, client, stream, callback, group, deleteOnCompletion));
111
- console.log(`redishelper : listener added for ${stream}`);
112
- }
113
- addListListener(event, callback, finishedevent = '', itemsPerCall = 1) {
114
- const client = new ioredis_1.default({
115
- host: this.redisConfig.REDISURL,
116
- password: this.redisConfig.REDISPW ?? ''
117
- });
118
- const channel = this.getListChannel(event);
119
- this.listeners.set(channel, new ListListener_1.default(this, client, channel, callback, finishedevent, itemsPerCall));
120
- console.log(`redishelper : listlistener added for ${channel}`);
121
- return this;
122
- }
123
- async getList(listname, start = 0, stop = -1) {
124
- const list = await this.reader.lrange(listname, start, stop);
125
- return list;
126
- }
127
- async getSet(listname) {
128
- const set = await this.reader.smembers(listname);
129
- return set;
130
- }
131
- throwError(error) {
132
- if (!this.rollbar)
133
- throw new Error('Rollbar not initialized');
134
- this.rollbar.error(error);
135
- }
136
- sendMessage(stream, data) {
137
- let msg = '';
138
- if (data.request)
139
- msg = data.request;
140
- if (data.endpoint)
141
- msg = data.endpoint;
142
- console.log(`sending message : ${msg} to ${stream}`);
143
- return this.writer.xadd(stream, '*', ...(0, utils_1.createRedisMessage)(data));
144
- }
145
- async sendMessageAndSubscribeForResponse(channel, target, messagedata, n = 2000) {
146
- await this.subscribe(channel);
147
- // send the message to the correct service
148
- this.sendMessage(target, messagedata);
149
- let resolver;
150
- let rejecter;
151
- // create a promise to be able to pass on to resolve later
152
- const promise = new Promise((r, rj) => {
153
- resolver = r;
154
- rejecter = rj;
155
- });
156
- if (!resolver || !rejecter)
157
- return null;
158
- //setup a timeout
159
- const timeout = this.setupTimeout(resolver, channel, n);
160
- //setup a response
161
- this.requestMessageResponse(channel, resolver, rejecter, timeout);
162
- // return a promise that will resolve when the message returns or times out
163
- return promise;
164
- }
165
- async getRequest(targetservice, key, data, timeout = 2000) {
166
- // create a message id to subscribe to
167
- const messageid = (0, uuid_1.v4)();
168
- //subscribe to message response
169
- const channel = this.getRequestSubscriptionName(messageid);
170
- return this.sendMessageAndSubscribeForResponse.call(this, channel, this.getRequestStream(targetservice), {
171
- request: key,
172
- messageid: messageid,
173
- data: (0, utils_1.sanitizeValue)(data)
174
- }, timeout);
175
- }
176
- async getApiRequest(endpoint, method, data, jwt = '', timeout = 2000) {
177
- // create a message id to subscribe to
178
- const messageid = (0, uuid_1.v4)();
179
- //subscribe to message response
180
- const channel = this.getRequestSubscriptionName(messageid);
181
- return this.sendMessageAndSubscribeForResponse.call(this, channel, this.getRequestStream('api'), {
182
- endpoint: endpoint,
183
- method: method,
184
- messageid: messageid,
185
- data: data,
186
- jwt: jwt
187
- }, timeout);
188
- }
189
- requestMessageResponse(channel, resolver, rejector, timeout) {
190
- this.resolvers.set(channel, resolver);
191
- this.rejectors.set(channel, rejector);
192
- this.timeouts.set(channel, timeout);
193
- }
194
- cleanupMessageReponse(channel) {
195
- this.resolvers.delete(channel);
196
- this.rejectors.delete(channel);
197
- this.timeouts.delete(channel);
198
- }
199
- onMessage(channel, message) {
200
- const rejector = this.rejectors.get(channel);
201
- const resolver = this.resolvers.get(channel);
202
- const timeout = this.timeouts.get(channel);
203
- if (!rejector || !resolver || !timeout) {
204
- this.cleanupMessageReponse.call(this, channel);
205
- return;
206
- }
207
- // check if the request has been rejected
208
- if ((0, class_validator_1.isJSON)(message)) {
209
- const resolvemessage = JSON.parse(message);
210
- if (resolvemessage.rejected && resolvemessage.error) {
211
- rejector(new Error(resolvemessage.error?.title ?? 'unknown-reason'));
212
- clearTimeout(timeout);
213
- return;
214
- }
215
- }
216
- if (message.length)
217
- resolver(message);
218
- else
219
- resolver(null);
220
- this.unsubscribe(channel);
221
- clearTimeout(timeout);
222
- }
223
- unsubscribe(channel) {
224
- this.reader.unsubscribe(channel);
225
- this.subscriptions = this.subscriptions.filter(s => s != channel);
226
- }
227
- setupTimeout(resolve, channel, n = 2000) {
228
- return setTimeout(() => {
229
- if (!this.subscriptions.includes(channel))
230
- return;
231
- console.log(`sub timedout: ${channel}`);
232
- resolve(null);
233
- this.unsubscribe(channel);
234
- }, n);
235
- }
236
- subscribe(channel) {
237
- this.subscriptions.push(channel);
238
- return this.reader.subscribe(channel);
239
- }
240
- getRequestSubscriptionName(messageid) {
241
- return `messageresponse${messageid}`;
242
- }
243
- async setKey(key, value) {
244
- try {
245
- return await this.writer.set(key, (0, utils_1.sanitizeValue)(value));
246
- }
247
- catch { }
248
- return;
249
- }
250
- async readKey(key) {
251
- try {
252
- return await this.writer.get(key);
253
- }
254
- catch { }
255
- return null;
256
- }
257
- async deleteKey(key) {
258
- try {
259
- return await this.writer.del(key);
260
- }
261
- catch { }
262
- return;
263
- }
264
- async sendListEvent(event, listitems, data = {}, listname = "") {
265
- if (listitems.length == 0)
266
- return;
267
- // if a listname exist, we first empty it
268
- if (listname.length > 0)
269
- await this.deleteKey.call(this, listname);
270
- const list = await this.addToSet.call(this, listitems, listname.length > 0 ? listname : undefined);
271
- return this.sendMessage(this.getListChannel(event), {
272
- ...data,
273
- listname: list
274
- });
275
- }
276
- async addToSet(listitems, listname) {
277
- const list = listname ?? (0, uuid_1.v4)();
278
- await this.writer.sadd(list, ...listitems.map(utils_1.sanitizeValue));
279
- return list;
280
- }
281
- async addToList(listitems, listname) {
282
- const list = listname ?? (0, uuid_1.v4)();
283
- await this.writer.lpush(list, ...listitems.map(utils_1.sanitizeValue));
284
- return list;
285
- }
286
- getListChannel(event) {
287
- return `${this.listprefix}${event}`;
288
- }
289
- async getStreamMessages(stream) {
290
- const streaminfo = await this.getStreamInfo.call(this, stream);
291
- if (!streaminfo || !(0, class_validator_1.isArray)(streaminfo) || streaminfo.length < 10)
292
- return [];
293
- const params = this.decypherParameters(streaminfo);
294
- if (!params.entries)
295
- return [];
296
- return this.decyperMessages(params.entries);
297
- }
298
- getStreamInfo(stream, count = 0) {
299
- return this.reader.xinfo('STREAM', stream, 'FULL', 'COUNT', count);
300
- }
301
- async filterStream(stream, key, value) {
302
- const messages = await this.getStreamMessages.call(this, stream);
303
- return messages.filter(message => message.parameters?.[key] != null && message.parameters[key] == value);
304
- }
305
- decypherResponse(...responses) {
306
- return responses.reduce((resp, response) => [
307
- ...resp,
308
- {
309
- stream: response[0],
310
- messages: this.decyperMessages(response[1])
311
- }
312
- ], []);
313
- }
314
- decyperMessages(messages) {
315
- return messages.map((message) => ({
316
- id: message[0],
317
- parameters: this.decypherParameters(message[1])
318
- }));
319
- }
320
- decypherParameters(parameters) {
321
- return parameters.reduce((params, v, n) => (n == 0 || (n % 2 == 0)) && parameters.length >= n + 1 ? ({
322
- ...params,
323
- [v]: parameters[n + 1]
324
- }) : params, {});
325
- }
326
- async isSetPickedUp(setname) {
327
- const pickedup = await this.reader.get((0, utils_1.getSetPickedUpName)(setname));
328
- return pickedup !== null;
329
- }
330
- }
331
- exports.default = Broker;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const class_validator_1 = require("class-validator");
5
+ const ioredis_1 = tslib_1.__importDefault(require("ioredis"));
6
+ const rollbar_1 = tslib_1.__importDefault(require("rollbar"));
7
+ const utils_1 = require("../services/utils");
8
+ const Listener_1 = tslib_1.__importDefault(require("./Listener"));
9
+ const ListListener_1 = tslib_1.__importDefault(require("./ListListener"));
10
+ const uuid_1 = require("uuid");
11
+ const lock_1 = require("../services/lock");
12
+ const SetWatcher_1 = tslib_1.__importDefault(require("./SetWatcher"));
13
+ process.on('unhandledRejection', error => console.log(error));
14
+ class Broker {
15
+ constructor(redisConfig, rollbarConfig, service, consumer) {
16
+ this.consumername = '';
17
+ this.listeners = new Map();
18
+ this.listprefix = 'listUpdated';
19
+ this.subscriptions = [];
20
+ this.rejectors = new Map;
21
+ this.resolvers = new Map;
22
+ this.timeouts = new Map;
23
+ this.redisConfig = redisConfig;
24
+ this.rollbar = new rollbar_1.default({
25
+ accessToken: rollbarConfig.accessToken,
26
+ environment: rollbarConfig.environment,
27
+ });
28
+ this.writer = new ioredis_1.default({
29
+ host: redisConfig.REDISURL,
30
+ password: redisConfig.REDISPW ?? ''
31
+ });
32
+ this.reader = new ioredis_1.default({
33
+ host: redisConfig.REDISURL,
34
+ password: redisConfig.REDISPW ?? ''
35
+ });
36
+ (0, lock_1.setupLock)(redisConfig);
37
+ this.reader.on('message', (channel, message) => this.onMessage.call(this, channel, message));
38
+ this.consumername = consumer;
39
+ this.service = service;
40
+ }
41
+ get requeststream() {
42
+ return this.getRequestStream(this.service);
43
+ }
44
+ getRequestStream(service) {
45
+ return `keyRequestedFrom${(0, utils_1.capitalizeFirstLetter)(service)}Service`;
46
+ }
47
+ createClient() {
48
+ return new ioredis_1.default({
49
+ host: this.redisConfig.REDISURL,
50
+ password: this.redisConfig.REDISPW ?? ''
51
+ });
52
+ }
53
+ createSetWatcher(setname, callback, finishedCallback, itemsPerCall = 1) {
54
+ new SetWatcher_1.default(this.createClient.call(this), setname, callback, finishedCallback, itemsPerCall);
55
+ }
56
+ setRequestEndpoint(callback) {
57
+ this.addListener(this.requeststream, this.getRequestCallback(callback), this.service);
58
+ this.addListener(this.requeststream, this.getRequestCallback(callback), this.service);
59
+ this.addListener(this.requeststream, this.getRequestCallback(callback), this.service);
60
+ this.addListener(this.requeststream, this.getRequestCallback(callback), this.service);
61
+ this.addListener(this.requeststream, this.getRequestCallback(callback), this.service);
62
+ return this;
63
+ }
64
+ getRequestCallback(callback) {
65
+ return async (id, parameters) => {
66
+ let result = null;
67
+ try {
68
+ result = await callback(id, parameters);
69
+ }
70
+ catch (error) {
71
+ this.throwError(error);
72
+ result = JSON.stringify({
73
+ rejected: true,
74
+ error: {
75
+ title: error?.message ?? ''
76
+ }
77
+ });
78
+ }
79
+ finally {
80
+ if (!parameters.messageid)
81
+ return null;
82
+ const channel = this.getRequestSubscriptionName(parameters.messageid);
83
+ return this.publish(channel, result);
84
+ }
85
+ };
86
+ }
87
+ publish(channel, result) {
88
+ return this.writer.publish(channel, result);
89
+ }
90
+ async createGroup(stream, group) {
91
+ try {
92
+ await this.writer.xgroup('CREATE', stream, group, '$', 'MKSTREAM');
93
+ return;
94
+ }
95
+ catch {
96
+ return;
97
+ }
98
+ }
99
+ addListener(stream, callback, group, deleteOnCompletion = false) {
100
+ const client = new ioredis_1.default({
101
+ host: this.redisConfig.REDISURL,
102
+ password: this.redisConfig.REDISPW ?? ''
103
+ });
104
+ this.createListener.call(this, client, stream, callback, group, deleteOnCompletion);
105
+ // dont wait for the result of create listener, just return this so we can chain
106
+ return this;
107
+ }
108
+ async createListener(client, stream, callback, group, deleteOnCompletion = false) {
109
+ if (group)
110
+ await this.createGroup.call(this, stream, group);
111
+ this.listeners.set(stream, new Listener_1.default(this, client, stream, callback, group, deleteOnCompletion));
112
+ console.log(`redishelper : listener added for ${stream}`);
113
+ }
114
+ addListListener(event, callback, finishedevent = '', itemsPerCall = 1) {
115
+ const client = new ioredis_1.default({
116
+ host: this.redisConfig.REDISURL,
117
+ password: this.redisConfig.REDISPW ?? ''
118
+ });
119
+ const channel = this.getListChannel(event);
120
+ this.listeners.set(channel, new ListListener_1.default(this, client, channel, callback, finishedevent, itemsPerCall));
121
+ console.log(`redishelper : listlistener added for ${channel}`);
122
+ return this;
123
+ }
124
+ async getList(listname, start = 0, stop = -1) {
125
+ const list = await this.reader.lrange(listname, start, stop);
126
+ return list;
127
+ }
128
+ async getSet(listname) {
129
+ const set = await this.reader.smembers(listname);
130
+ return set;
131
+ }
132
+ throwError(error) {
133
+ if (!this.rollbar)
134
+ throw new Error('Rollbar not initialized');
135
+ this.rollbar.error(error);
136
+ }
137
+ sendMessage(stream, data) {
138
+ let msg = '';
139
+ if (data.request)
140
+ msg = data.request;
141
+ if (data.endpoint)
142
+ msg = data.endpoint;
143
+ console.log(`sending message : ${msg} to ${stream}`);
144
+ return this.writer.xadd(stream, '*', ...(0, utils_1.createRedisMessage)(data));
145
+ }
146
+ async sendMessageAndSubscribeForResponse(channel, target, messagedata, n = 2000) {
147
+ await this.subscribe(channel);
148
+ // send the message to the correct service
149
+ this.sendMessage(target, messagedata);
150
+ let resolver;
151
+ let rejecter;
152
+ // create a promise to be able to pass on to resolve later
153
+ const promise = new Promise((r, rj) => {
154
+ resolver = r;
155
+ rejecter = rj;
156
+ });
157
+ if (!resolver || !rejecter)
158
+ return null;
159
+ //setup a timeout
160
+ const timeout = this.setupTimeout(resolver, channel, n);
161
+ //setup a response
162
+ this.requestMessageResponse(channel, resolver, rejecter, timeout);
163
+ // return a promise that will resolve when the message returns or times out
164
+ return promise;
165
+ }
166
+ async getRequest(targetservice, key, data, timeout = 2000) {
167
+ // create a message id to subscribe to
168
+ const messageid = (0, uuid_1.v4)();
169
+ //subscribe to message response
170
+ const channel = this.getRequestSubscriptionName(messageid);
171
+ return this.sendMessageAndSubscribeForResponse.call(this, channel, this.getRequestStream(targetservice), {
172
+ request: key,
173
+ messageid: messageid,
174
+ data: (0, utils_1.sanitizeValue)(data)
175
+ }, timeout);
176
+ }
177
+ async getApiRequest(endpoint, method, data, jwt = '', timeout = 2000) {
178
+ // create a message id to subscribe to
179
+ const messageid = (0, uuid_1.v4)();
180
+ //subscribe to message response
181
+ const channel = this.getRequestSubscriptionName(messageid);
182
+ return this.sendMessageAndSubscribeForResponse.call(this, channel, this.getRequestStream('api'), {
183
+ endpoint: endpoint,
184
+ method: method,
185
+ messageid: messageid,
186
+ data: data,
187
+ jwt: jwt
188
+ }, timeout);
189
+ }
190
+ requestMessageResponse(channel, resolver, rejector, timeout) {
191
+ this.resolvers.set(channel, resolver);
192
+ this.rejectors.set(channel, rejector);
193
+ this.timeouts.set(channel, timeout);
194
+ }
195
+ cleanupMessageReponse(channel) {
196
+ this.resolvers.delete(channel);
197
+ this.rejectors.delete(channel);
198
+ this.timeouts.delete(channel);
199
+ }
200
+ onMessage(channel, message) {
201
+ const rejector = this.rejectors.get(channel);
202
+ const resolver = this.resolvers.get(channel);
203
+ const timeout = this.timeouts.get(channel);
204
+ if (!rejector || !resolver || !timeout) {
205
+ this.cleanupMessageReponse.call(this, channel);
206
+ return;
207
+ }
208
+ // check if the request has been rejected
209
+ if ((0, class_validator_1.isJSON)(message)) {
210
+ const resolvemessage = JSON.parse(message);
211
+ if (resolvemessage.rejected && resolvemessage.error) {
212
+ rejector(new Error(resolvemessage.error?.title ?? 'unknown-reason'));
213
+ clearTimeout(timeout);
214
+ return;
215
+ }
216
+ }
217
+ if (message.length)
218
+ resolver(message);
219
+ else
220
+ resolver(null);
221
+ this.unsubscribe(channel);
222
+ clearTimeout(timeout);
223
+ }
224
+ unsubscribe(channel) {
225
+ this.reader.unsubscribe(channel);
226
+ this.subscriptions = this.subscriptions.filter(s => s != channel);
227
+ }
228
+ setupTimeout(resolve, channel, n = 2000) {
229
+ return setTimeout(() => {
230
+ if (!this.subscriptions.includes(channel))
231
+ return;
232
+ console.log(`sub timedout: ${channel}`);
233
+ resolve(null);
234
+ this.unsubscribe(channel);
235
+ }, n);
236
+ }
237
+ subscribe(channel) {
238
+ this.subscriptions.push(channel);
239
+ return this.reader.subscribe(channel);
240
+ }
241
+ getRequestSubscriptionName(messageid) {
242
+ return `messageresponse${messageid}`;
243
+ }
244
+ async setKey(key, value) {
245
+ try {
246
+ return await this.writer.set(key, (0, utils_1.sanitizeValue)(value));
247
+ }
248
+ catch { }
249
+ return;
250
+ }
251
+ async readKey(key) {
252
+ try {
253
+ return await this.writer.get(key);
254
+ }
255
+ catch { }
256
+ return null;
257
+ }
258
+ async deleteKey(key) {
259
+ try {
260
+ return await this.writer.del(key);
261
+ }
262
+ catch { }
263
+ return;
264
+ }
265
+ async sendListEvent(event, listitems, data = {}, listname = "") {
266
+ if (listitems.length == 0)
267
+ return;
268
+ // if a listname exist, we first empty it
269
+ if (listname.length > 0)
270
+ await this.deleteKey.call(this, listname);
271
+ const list = await this.addToSet.call(this, listitems, listname.length > 0 ? listname : undefined);
272
+ return this.sendMessage(this.getListChannel(event), {
273
+ ...data,
274
+ listname: list
275
+ });
276
+ }
277
+ async addToSet(listitems, listname) {
278
+ const list = listname ?? (0, uuid_1.v4)();
279
+ await this.writer.sadd(list, ...listitems.map(utils_1.sanitizeValue));
280
+ return list;
281
+ }
282
+ async addToList(listitems, listname) {
283
+ const list = listname ?? (0, uuid_1.v4)();
284
+ await this.writer.lpush(list, ...listitems.map(utils_1.sanitizeValue));
285
+ return list;
286
+ }
287
+ getListChannel(event) {
288
+ return `${this.listprefix}${event}`;
289
+ }
290
+ async getStreamMessages(stream) {
291
+ const streaminfo = await this.getStreamInfo.call(this, stream);
292
+ if (!streaminfo || !(0, class_validator_1.isArray)(streaminfo) || streaminfo.length < 10)
293
+ return [];
294
+ const params = this.decypherParameters(streaminfo);
295
+ if (!params.entries)
296
+ return [];
297
+ return this.decyperMessages(params.entries);
298
+ }
299
+ getStreamInfo(stream, count = 0) {
300
+ return this.reader.xinfo('STREAM', stream, 'FULL', 'COUNT', count);
301
+ }
302
+ async filterStream(stream, key, value) {
303
+ const messages = await this.getStreamMessages.call(this, stream);
304
+ return messages.filter(message => message.parameters?.[key] != null && message.parameters[key] == value);
305
+ }
306
+ decypherResponse(...responses) {
307
+ return responses.reduce((resp, response) => [
308
+ ...resp,
309
+ {
310
+ stream: response[0],
311
+ messages: this.decyperMessages(response[1])
312
+ }
313
+ ], []);
314
+ }
315
+ decyperMessages(messages) {
316
+ return messages.map((message) => ({
317
+ id: message[0],
318
+ parameters: this.decypherParameters(message[1])
319
+ }));
320
+ }
321
+ decypherParameters(parameters) {
322
+ return parameters.reduce((params, v, n) => (n == 0 || (n % 2 == 0)) && parameters.length >= n + 1 ? ({
323
+ ...params,
324
+ [v]: parameters[n + 1]
325
+ }) : params, {});
326
+ }
327
+ async isSetPickedUp(setname) {
328
+ const pickedup = await this.reader.get((0, utils_1.getSetPickedUpName)(setname));
329
+ return pickedup !== null;
330
+ }
331
+ }
332
+ exports.default = Broker;
332
333
  //# sourceMappingURL=Broker.js.map