woonplan-packages-redishelper 2.0.95 → 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,331 +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
- async 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
- await this.createListener.call(this, client, stream, callback, group, deleteOnCompletion);
104
- return this;
105
- }
106
- async createListener(client, stream, callback, group, deleteOnCompletion = false) {
107
- if (group)
108
- await this.createGroup.call(this, stream, group);
109
- this.listeners.set(stream, new Listener_1.default(this, client, stream, callback, group, deleteOnCompletion));
110
- console.log(`redishelper : listener added for ${stream}`);
111
- }
112
- addListListener(event, callback, finishedevent = '', itemsPerCall = 1) {
113
- const client = new ioredis_1.default({
114
- host: this.redisConfig.REDISURL,
115
- password: this.redisConfig.REDISPW ?? ''
116
- });
117
- const channel = this.getListChannel(event);
118
- this.listeners.set(channel, new ListListener_1.default(this, client, channel, callback, finishedevent, itemsPerCall));
119
- console.log(`redishelper : listlistener added for ${channel}`);
120
- return this;
121
- }
122
- async getList(listname, start = 0, stop = -1) {
123
- const list = await this.reader.lrange(listname, start, stop);
124
- return list;
125
- }
126
- async getSet(listname) {
127
- const set = await this.reader.smembers(listname);
128
- return set;
129
- }
130
- throwError(error) {
131
- if (!this.rollbar)
132
- throw new Error('Rollbar not initialized');
133
- this.rollbar.error(error);
134
- }
135
- sendMessage(stream, data) {
136
- let msg = '';
137
- if (data.request)
138
- msg = data.request;
139
- if (data.endpoint)
140
- msg = data.endpoint;
141
- console.log(`sending message : ${msg} to ${stream}`);
142
- return this.writer.xadd(stream, '*', ...(0, utils_1.createRedisMessage)(data));
143
- }
144
- async sendMessageAndSubscribeForResponse(channel, target, messagedata, n = 2000) {
145
- await this.subscribe(channel);
146
- // send the message to the correct service
147
- this.sendMessage(target, messagedata);
148
- let resolver;
149
- let rejecter;
150
- // create a promise to be able to pass on to resolve later
151
- const promise = new Promise((r, rj) => {
152
- resolver = r;
153
- rejecter = rj;
154
- });
155
- if (!resolver || !rejecter)
156
- return null;
157
- //setup a timeout
158
- const timeout = this.setupTimeout(resolver, channel, n);
159
- //setup a response
160
- this.requestMessageResponse(channel, resolver, rejecter, timeout);
161
- // return a promise that will resolve when the message returns or times out
162
- return promise;
163
- }
164
- async getRequest(targetservice, key, data, timeout = 2000) {
165
- // create a message id to subscribe to
166
- const messageid = (0, uuid_1.v4)();
167
- //subscribe to message response
168
- const channel = this.getRequestSubscriptionName(messageid);
169
- return this.sendMessageAndSubscribeForResponse.call(this, channel, this.getRequestStream(targetservice), {
170
- request: key,
171
- messageid: messageid,
172
- data: (0, utils_1.sanitizeValue)(data)
173
- }, timeout);
174
- }
175
- async getApiRequest(endpoint, method, data, jwt = '', timeout = 2000) {
176
- // create a message id to subscribe to
177
- const messageid = (0, uuid_1.v4)();
178
- //subscribe to message response
179
- const channel = this.getRequestSubscriptionName(messageid);
180
- return this.sendMessageAndSubscribeForResponse.call(this, channel, this.getRequestStream('api'), {
181
- endpoint: endpoint,
182
- method: method,
183
- messageid: messageid,
184
- data: data,
185
- jwt: jwt
186
- }, timeout);
187
- }
188
- requestMessageResponse(channel, resolver, rejector, timeout) {
189
- this.resolvers.set(channel, resolver);
190
- this.rejectors.set(channel, rejector);
191
- this.timeouts.set(channel, timeout);
192
- }
193
- cleanupMessageReponse(channel) {
194
- this.resolvers.delete(channel);
195
- this.rejectors.delete(channel);
196
- this.timeouts.delete(channel);
197
- }
198
- onMessage(channel, message) {
199
- const rejector = this.rejectors.get(channel);
200
- const resolver = this.resolvers.get(channel);
201
- const timeout = this.timeouts.get(channel);
202
- if (!rejector || !resolver || !timeout) {
203
- this.cleanupMessageReponse.call(this, channel);
204
- return;
205
- }
206
- // check if the request has been rejected
207
- if ((0, class_validator_1.isJSON)(message)) {
208
- const resolvemessage = JSON.parse(message);
209
- if (resolvemessage.rejected && resolvemessage.error) {
210
- rejector(new Error(resolvemessage.error?.title ?? 'unknown-reason'));
211
- clearTimeout(timeout);
212
- return;
213
- }
214
- }
215
- if (message.length)
216
- resolver(message);
217
- else
218
- resolver(null);
219
- this.unsubscribe(channel);
220
- clearTimeout(timeout);
221
- }
222
- unsubscribe(channel) {
223
- this.reader.unsubscribe(channel);
224
- this.subscriptions = this.subscriptions.filter(s => s != channel);
225
- }
226
- setupTimeout(resolve, channel, n = 2000) {
227
- return setTimeout(() => {
228
- if (!this.subscriptions.includes(channel))
229
- return;
230
- console.log(`sub timedout: ${channel}`);
231
- resolve(null);
232
- this.unsubscribe(channel);
233
- }, n);
234
- }
235
- subscribe(channel) {
236
- this.subscriptions.push(channel);
237
- return this.reader.subscribe(channel);
238
- }
239
- getRequestSubscriptionName(messageid) {
240
- return `messageresponse${messageid}`;
241
- }
242
- async setKey(key, value) {
243
- try {
244
- return await this.writer.set(key, (0, utils_1.sanitizeValue)(value));
245
- }
246
- catch { }
247
- return;
248
- }
249
- async readKey(key) {
250
- try {
251
- return await this.writer.get(key);
252
- }
253
- catch { }
254
- return null;
255
- }
256
- async deleteKey(key) {
257
- try {
258
- return await this.writer.del(key);
259
- }
260
- catch { }
261
- return;
262
- }
263
- async sendListEvent(event, listitems, data = {}, listname = "") {
264
- if (listitems.length == 0)
265
- return;
266
- // if a listname exist, we first empty it
267
- if (listname.length > 0)
268
- await this.deleteKey.call(this, listname);
269
- const list = await this.addToSet.call(this, listitems, listname.length > 0 ? listname : undefined);
270
- return this.sendMessage(this.getListChannel(event), {
271
- ...data,
272
- listname: list
273
- });
274
- }
275
- async addToSet(listitems, listname) {
276
- const list = listname ?? (0, uuid_1.v4)();
277
- await this.writer.sadd(list, ...listitems.map(utils_1.sanitizeValue));
278
- return list;
279
- }
280
- async addToList(listitems, listname) {
281
- const list = listname ?? (0, uuid_1.v4)();
282
- await this.writer.lpush(list, ...listitems.map(utils_1.sanitizeValue));
283
- return list;
284
- }
285
- getListChannel(event) {
286
- return `${this.listprefix}${event}`;
287
- }
288
- async getStreamMessages(stream) {
289
- const streaminfo = await this.getStreamInfo.call(this, stream);
290
- if (!streaminfo || !(0, class_validator_1.isArray)(streaminfo) || streaminfo.length < 10)
291
- return [];
292
- const params = this.decypherParameters(streaminfo);
293
- if (!params.entries)
294
- return [];
295
- return this.decyperMessages(params.entries);
296
- }
297
- getStreamInfo(stream, count = 0) {
298
- return this.reader.xinfo('STREAM', stream, 'FULL', 'COUNT', count);
299
- }
300
- async filterStream(stream, key, value) {
301
- const messages = await this.getStreamMessages.call(this, stream);
302
- return messages.filter(message => message.parameters?.[key] != null && message.parameters[key] == value);
303
- }
304
- decypherResponse(...responses) {
305
- return responses.reduce((resp, response) => [
306
- ...resp,
307
- {
308
- stream: response[0],
309
- messages: this.decyperMessages(response[1])
310
- }
311
- ], []);
312
- }
313
- decyperMessages(messages) {
314
- return messages.map((message) => ({
315
- id: message[0],
316
- parameters: this.decypherParameters(message[1])
317
- }));
318
- }
319
- decypherParameters(parameters) {
320
- return parameters.reduce((params, v, n) => (n == 0 || (n % 2 == 0)) && parameters.length >= n + 1 ? ({
321
- ...params,
322
- [v]: parameters[n + 1]
323
- }) : params, {});
324
- }
325
- async isSetPickedUp(setname) {
326
- const pickedup = await this.reader.get((0, utils_1.getSetPickedUpName)(setname));
327
- return pickedup !== null;
328
- }
329
- }
330
- 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;
331
333
  //# sourceMappingURL=Broker.js.map