@iobroker/db-base 4.0.0-alpha.45-20220114-88aaebdb → 4.0.0-alpha.49-20220121-e7aa4308
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/lib/constants.js +6 -0
- package/lib/redisHandler.js +144 -11
- package/package.json +3 -3
package/lib/constants.js
ADDED
package/lib/redisHandler.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const Resp = require('respjs');
|
|
2
2
|
const { EventEmitter } = require('events');
|
|
3
|
+
const { QUEUED_STR_BUF, OK_STR_BUF } = require('./constants');
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Class to handle a redis connection and provide events to react on for
|
|
@@ -40,7 +41,7 @@ class RedisHandler extends EventEmitter {
|
|
|
40
41
|
this.resp.on('error', err => {
|
|
41
42
|
this.log.error(`${this.socketId} (Init=${this.initialized}) Redis error:${err}`);
|
|
42
43
|
if (this.initialized) {
|
|
43
|
-
this.sendError(null, new Error(
|
|
44
|
+
this.sendError(null, new Error(`PARSER ERROR ${err}`)); // TODO
|
|
44
45
|
} else {
|
|
45
46
|
this.close();
|
|
46
47
|
}
|
|
@@ -99,25 +100,49 @@ class RedisHandler extends EventEmitter {
|
|
|
99
100
|
}
|
|
100
101
|
}
|
|
101
102
|
}
|
|
102
|
-
|
|
103
|
-
this.initialized = true;
|
|
104
|
-
}
|
|
103
|
+
|
|
105
104
|
const t = process.hrtime();
|
|
106
105
|
const responseId = t[0] * 1e3 + t[1] / 1e6;
|
|
106
|
+
|
|
107
107
|
if (this.options.enhancedLogging) {
|
|
108
108
|
this.log.silly(
|
|
109
109
|
`${this.socketId} Parser result: id=${responseId}, command=${command}, data=${
|
|
110
110
|
JSON.stringify(data).length > 1024
|
|
111
|
-
? JSON.stringify(data).substring(0, 100)
|
|
111
|
+
? `${JSON.stringify(data).substring(0, 100)} -- ${JSON.stringify(data).length} bytes`
|
|
112
112
|
: JSON.stringify(data)
|
|
113
113
|
}`
|
|
114
114
|
);
|
|
115
115
|
}
|
|
116
|
-
|
|
116
|
+
|
|
117
|
+
if (command === 'multi') {
|
|
118
|
+
this._handleMulti();
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// multi active and exec not called yet
|
|
123
|
+
if (this.multiActive && !this.execCalled && command !== 'exec') {
|
|
124
|
+
// store all response ids so we know which need to be in the multi call
|
|
125
|
+
this.multiResponseIds.push(responseId);
|
|
126
|
+
// add it for the correct order will be overwritten with correct response
|
|
127
|
+
this.multiResponseMap.set(responseId, null);
|
|
128
|
+
} else {
|
|
129
|
+
// multi response ids should not be pushed - we will answer combined
|
|
130
|
+
this.writeQueue.push({ id: responseId, data: false });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (command === 'exec') {
|
|
134
|
+
this._handleExec(responseId);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (command === 'info') {
|
|
139
|
+
this.initialized = true;
|
|
140
|
+
}
|
|
141
|
+
|
|
117
142
|
if (this.listenerCount(command) !== 0) {
|
|
118
143
|
setImmediate(() => this.emit(command, data, responseId));
|
|
119
144
|
} else {
|
|
120
|
-
this.sendError(responseId, new Error(command
|
|
145
|
+
this.sendError(responseId, new Error(`${command} NOT SUPPORTED`));
|
|
121
146
|
}
|
|
122
147
|
}
|
|
123
148
|
|
|
@@ -130,6 +155,7 @@ class RedisHandler extends EventEmitter {
|
|
|
130
155
|
*/
|
|
131
156
|
_sendQueued(responseId, data) {
|
|
132
157
|
let idx = 0;
|
|
158
|
+
|
|
133
159
|
while (this.writeQueue.length && idx < this.writeQueue.length) {
|
|
134
160
|
// we found the queue entry that matches with the responseId, so store the data so be sent out
|
|
135
161
|
if (this.writeQueue[idx].id === responseId) {
|
|
@@ -148,11 +174,12 @@ class RedisHandler extends EventEmitter {
|
|
|
148
174
|
this.log.silly(
|
|
149
175
|
`${this.socketId} Redis response (${response.id}): ${
|
|
150
176
|
response.data.length > 1024
|
|
151
|
-
? data.length
|
|
177
|
+
? `${data.length} bytes`
|
|
152
178
|
: response.data.toString().replace(/[\r\n]+/g, '')
|
|
153
179
|
}`
|
|
154
180
|
);
|
|
155
181
|
}
|
|
182
|
+
|
|
156
183
|
this._write(response.data);
|
|
157
184
|
// We sended out first queue entry but no further response is ready
|
|
158
185
|
// and we do not need to check the whole queue, so we are done here
|
|
@@ -199,8 +226,9 @@ class RedisHandler extends EventEmitter {
|
|
|
199
226
|
}
|
|
200
227
|
if (!data) {
|
|
201
228
|
this.log.warn(`${this.socketId} Not able to write ${JSON.stringify(data)}`);
|
|
202
|
-
data = Resp.encodeError(new Error(
|
|
229
|
+
data = Resp.encodeError(new Error(`INVALID RESPONSE: ${JSON.stringify(data)}`));
|
|
203
230
|
}
|
|
231
|
+
|
|
204
232
|
setImmediate(() => this._sendQueued(responseId, data));
|
|
205
233
|
}
|
|
206
234
|
|
|
@@ -226,6 +254,11 @@ class RedisHandler extends EventEmitter {
|
|
|
226
254
|
* @param responseId ID of the response
|
|
227
255
|
*/
|
|
228
256
|
sendNull(responseId) {
|
|
257
|
+
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
|
|
258
|
+
this._handleMultiResponse(responseId, Resp.encodeNull());
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
|
|
229
262
|
this.sendResponse(responseId, Resp.encodeNull());
|
|
230
263
|
}
|
|
231
264
|
|
|
@@ -234,6 +267,11 @@ class RedisHandler extends EventEmitter {
|
|
|
234
267
|
* @param responseId ID of the response
|
|
235
268
|
*/
|
|
236
269
|
sendNullArray(responseId) {
|
|
270
|
+
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
|
|
271
|
+
this._handleMultiResponse(responseId, Resp.encodeNullArray());
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
|
|
237
275
|
this.sendResponse(responseId, Resp.encodeNullArray());
|
|
238
276
|
}
|
|
239
277
|
|
|
@@ -243,6 +281,11 @@ class RedisHandler extends EventEmitter {
|
|
|
243
281
|
* @param str String to encode
|
|
244
282
|
*/
|
|
245
283
|
sendString(responseId, str) {
|
|
284
|
+
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
|
|
285
|
+
this._handleMultiResponse(responseId, Resp.encodeString(str));
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
|
|
246
289
|
this.sendResponse(responseId, Resp.encodeString(str));
|
|
247
290
|
}
|
|
248
291
|
|
|
@@ -253,6 +296,12 @@ class RedisHandler extends EventEmitter {
|
|
|
253
296
|
*/
|
|
254
297
|
sendError(responseId, error) {
|
|
255
298
|
this.log.warn(`${this.socketId} Error from InMemDB: ${error}`);
|
|
299
|
+
|
|
300
|
+
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
|
|
301
|
+
this._handleMultiResponse(responseId, Resp.encodeError(error));
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
|
|
256
305
|
this.sendResponse(responseId, Resp.encodeError(error));
|
|
257
306
|
}
|
|
258
307
|
|
|
@@ -262,6 +311,11 @@ class RedisHandler extends EventEmitter {
|
|
|
262
311
|
* @param num Integer to send out
|
|
263
312
|
*/
|
|
264
313
|
sendInteger(responseId, num) {
|
|
314
|
+
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
|
|
315
|
+
this._handleMultiResponse(responseId, Resp.encodeInteger(num));
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
|
|
265
319
|
this.sendResponse(responseId, Resp.encodeInteger(num));
|
|
266
320
|
}
|
|
267
321
|
|
|
@@ -271,6 +325,11 @@ class RedisHandler extends EventEmitter {
|
|
|
271
325
|
* @param str String to send out
|
|
272
326
|
*/
|
|
273
327
|
sendBulk(responseId, str) {
|
|
328
|
+
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
|
|
329
|
+
this._handleMultiResponse(responseId, Resp.encodeBulk(str));
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
|
|
274
333
|
this.sendResponse(responseId, Resp.encodeBulk(str));
|
|
275
334
|
}
|
|
276
335
|
|
|
@@ -280,6 +339,11 @@ class RedisHandler extends EventEmitter {
|
|
|
280
339
|
* @param buf Buffer to send out
|
|
281
340
|
*/
|
|
282
341
|
sendBufBulk(responseId, buf) {
|
|
342
|
+
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
|
|
343
|
+
this._handleMultiResponse(responseId, Resp.encodeBufBulk(buf));
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
|
|
283
347
|
this.sendResponse(responseId, Resp.encodeBufBulk(buf));
|
|
284
348
|
}
|
|
285
349
|
|
|
@@ -307,12 +371,81 @@ class RedisHandler extends EventEmitter {
|
|
|
307
371
|
|
|
308
372
|
/**
|
|
309
373
|
* Encode a array values to buffers and send out
|
|
310
|
-
* @param responseId ID of the response
|
|
311
|
-
* @param arr Array to send out
|
|
374
|
+
* @param {string} responseId ID of the response
|
|
375
|
+
* @param {any[]} arr Array to send out
|
|
312
376
|
*/
|
|
313
377
|
sendArray(responseId, arr) {
|
|
378
|
+
if (this.multiActive && this.multiResponseIds.includes(responseId)) {
|
|
379
|
+
this._handleMultiResponse(responseId, Resp.encodeArray(this.encodeRespArray(arr)));
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
|
|
314
383
|
this.sendResponse(responseId, Resp.encodeArray(this.encodeRespArray(arr)));
|
|
315
384
|
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Handles a 'multi' command
|
|
388
|
+
*
|
|
389
|
+
* @private
|
|
390
|
+
*/
|
|
391
|
+
_handleMulti() {
|
|
392
|
+
if (this.multiActive) {
|
|
393
|
+
this.log.warn(`${this.socketId} Conflicting multi call`);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
this.multiActive = true;
|
|
397
|
+
this.execCalled = false;
|
|
398
|
+
this.multiResponseIds = [];
|
|
399
|
+
this.multiResponseCount = 0;
|
|
400
|
+
this.multiResponseMap = new Map();
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Handles an 'exec' command
|
|
405
|
+
*
|
|
406
|
+
* @param {number} responseId ID of the response
|
|
407
|
+
* @private
|
|
408
|
+
*/
|
|
409
|
+
_handleExec(responseId) {
|
|
410
|
+
this.execCalled = true;
|
|
411
|
+
this.execId = responseId;
|
|
412
|
+
|
|
413
|
+
// maybe we have all fullfilled yet
|
|
414
|
+
if (this.multiResponseCount === this.multiResponseIds.length) {
|
|
415
|
+
this._sendExecReponse();
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Builds up the exec response and sends it
|
|
421
|
+
*
|
|
422
|
+
* @private
|
|
423
|
+
*/
|
|
424
|
+
_sendExecReponse() {
|
|
425
|
+
this.multiActive = false;
|
|
426
|
+
// collect all 'QUEUED' answers
|
|
427
|
+
const queuedStrArr = new Array(this.multiResponseCount).fill(QUEUED_STR_BUF);
|
|
428
|
+
|
|
429
|
+
this._sendQueued(
|
|
430
|
+
this.execId,
|
|
431
|
+
Buffer.concat([OK_STR_BUF, ...queuedStrArr, Resp.encodeArray(Array.from(this.multiResponseMap.values()))])
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Handles a multi response
|
|
437
|
+
*
|
|
438
|
+
* @param {number} responseId ID of the response
|
|
439
|
+
* @param {Buffer} buf buffer to include in response
|
|
440
|
+
* @private
|
|
441
|
+
*/
|
|
442
|
+
_handleMultiResponse(responseId, buf) {
|
|
443
|
+
this.multiResponseMap.set(responseId, buf);
|
|
444
|
+
this.multiResponseCount++;
|
|
445
|
+
if (this.execCalled && this.multiResponseCount === this.multiResponseIds.length) {
|
|
446
|
+
this._sendExecReponse();
|
|
447
|
+
}
|
|
448
|
+
}
|
|
316
449
|
}
|
|
317
450
|
|
|
318
451
|
module.exports = RedisHandler;
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iobroker/db-base",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.49-20220121-e7aa4308",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=12.0.0"
|
|
6
6
|
},
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@iobroker/js-controller-common": "4.0.0-alpha.
|
|
8
|
+
"@iobroker/js-controller-common": "4.0.0-alpha.49-20220121-e7aa4308",
|
|
9
9
|
"deep-clone": "^3.0.3",
|
|
10
10
|
"fs-extra": "^10.0.0",
|
|
11
11
|
"respjs": "^4.2.0"
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
"lib/",
|
|
36
36
|
"index.js"
|
|
37
37
|
],
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "f42f117b74d3c4bfac0bc872c087d382ca897b7c"
|
|
39
39
|
}
|