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.
- package/.vscode/settings.json +2 -2
- package/dist/tsc/classes/Broker.d.ts +63 -63
- package/dist/tsc/classes/Broker.js +332 -330
- package/dist/tsc/classes/Broker.js.map +1 -1
- package/dist/tsc/classes/BrokerClient.d.ts +20 -20
- package/dist/tsc/classes/BrokerClient.js +67 -67
- package/dist/tsc/classes/ListListener.d.ts +10 -10
- package/dist/tsc/classes/ListListener.js +21 -21
- package/dist/tsc/classes/ListRunner.d.ts +19 -19
- package/dist/tsc/classes/ListRunner.js +89 -89
- package/dist/tsc/classes/Listener.d.ts +8 -8
- package/dist/tsc/classes/Listener.js +13 -13
- package/dist/tsc/classes/SetWatcher.d.ts +29 -29
- package/dist/tsc/classes/SetWatcher.js +137 -137
- package/dist/tsc/main.d.ts +2 -2
- package/dist/tsc/main.js +6 -6
- package/dist/tsc/services/lock.d.ts +5 -5
- package/dist/tsc/services/lock.js +35 -35
- package/dist/tsc/services/utils.d.ts +15 -15
- package/dist/tsc/services/utils.js +62 -62
- package/package.json +41 -41
- package/tsconfig.json +39 -39
- package/woonplan-packages-redishelper-2.0.96.tgz +0 -0
- package/dist/tsc/types.d.ts +0 -54
|
@@ -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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
this.
|
|
38
|
-
this.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
this.rollbar
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (data.
|
|
140
|
-
msg = data.
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
this.
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
//setup a
|
|
160
|
-
this.
|
|
161
|
-
//
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
//
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
//
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
this.
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
this.
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
resolver(
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
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
|