@iobroker/socket-classes 1.3.3 → 1.4.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/README.md CHANGED
@@ -978,6 +978,9 @@ Read all instances of the given adapter, or all instances of all adapters if ada
978
978
  -->
979
979
 
980
980
  ## Changelog
981
+ ### 1.4.0 (2023-09-11)
982
+ * (foxriver76) fixed crash on invalid patterns with js-controller version 5
983
+
981
984
  ### 1.3.3 (2023-08-01)
982
985
  * (bluefox) Implemented subscribes of a client on messages from specific instance
983
986
  * (bluefox) Moved checkFeatureSupported to regular connection and not only admin
@@ -117,7 +117,7 @@ class SocketAdmin extends SocketCommon {
117
117
  socket.emit(SocketCommon.COMMAND_RE_AUTHENTICATE);
118
118
  }
119
119
  });
120
- }, 60000);
120
+ }, 60_000);
121
121
  }
122
122
 
123
123
  return true;
@@ -262,7 +262,7 @@ class SocketCommands {
262
262
  return this.logEnabled;
263
263
  }
264
264
 
265
- subscribe(socket, type, pattern, patternFile) {
265
+ async subscribe(socket, type, pattern, patternFile) {
266
266
  if (!pattern) {
267
267
  return this.adapter.log.warn('Empty pattern on subscribe!');
268
268
  }
@@ -306,23 +306,38 @@ class SocketCommands {
306
306
  if (this.subscribes[type][key] === undefined) {
307
307
  this.subscribes[type][key] = 1;
308
308
  if (type === 'stateChange') {
309
- this.adapter.subscribeForeignStates(pattern, options);
309
+ try {
310
+ await this.adapter.subscribeForeignStatesAsync(pattern, options);
311
+ } catch (e) {
312
+ this.adapter.log.error(`Invalid pattern on subscribe: ${e.message}`)
313
+ return
314
+ }
310
315
  } else if (type === 'objectChange') {
311
- this.adapter.subscribeForeignObjects && this.adapter.subscribeForeignObjects(pattern, options);
316
+ try {
317
+ this.adapter.subscribeForeignObjects && await this.adapter.subscribeForeignObjects(pattern, options);
318
+ } catch (e) {
319
+ this.adapter.log.error(`Invalid pattern on subscribe: ${e.message}`)
320
+ return
321
+ }
312
322
  } else if (type === 'log') {
313
323
  if (!this.logEnabled && this.adapter.requireLog) {
314
324
  this.logEnabled = true;
315
325
  this.adapter.requireLog(true, options);
316
326
  }
317
327
  } else if (type === 'fileChange' && this.adapter.subscribeForeignFiles) {
318
- this.adapter.subscribeForeignFiles(pattern, patternFile, options);
328
+ try {
329
+ await this.adapter.subscribeForeignFiles(pattern, patternFile, options);
330
+ } catch (e) {
331
+ this.adapter.log.error(`Invalid pattern on subscribe: ${e.message}`)
332
+ return
333
+ }
319
334
  }
320
335
  } else {
321
336
  this.subscribes[type][key]++;
322
337
  }
323
338
  };
324
339
 
325
- unsubscribe(socket, type, pattern, patternFile) {
340
+ async unsubscribe(socket, type, pattern, patternFile) {
326
341
  if (!pattern) {
327
342
  return this.adapter.log.warn('Empty pattern on subscribe!');
328
343
  }
@@ -349,16 +364,24 @@ class SocketCommands {
349
364
 
350
365
  for (let i = socket.subscribe[type].length - 1; i >= 0; i--) {
351
366
  if (socket.subscribe[type][i].pattern === key) {
352
- // Remove pattern from global list
367
+ // Remove a pattern from a global list
353
368
  if (this.subscribes[type][key] !== undefined) {
354
369
  this.subscribes[type][key]--;
355
370
  if (this.subscribes[type][key] <= 0) {
356
371
  if (type === 'stateChange') {
357
- //console.log((socket._name || socket.id) + ' unsubscribeForeignStates ' + pattern);
358
- this.adapter.unsubscribeForeignStates(pattern, options);
372
+ try {
373
+ await this.adapter.unsubscribeForeignStatesAsync(pattern, options);
374
+ } catch (e) {
375
+ this.adapter.log.error(`Invalid pattern on unsubscribe: ${e.message}`)
376
+ return
377
+ }
359
378
  } else if (type === 'objectChange') {
360
- //console.log((socket._name || socket.id) + ' unsubscribeForeignObjects ' + pattern);
361
- this.adapter.unsubscribeForeignObjects && this.adapter.unsubscribeForeignObjects(pattern, options);
379
+ try {
380
+ this.adapter.unsubscribeForeignObjects && await this.adapter.unsubscribeForeignObjectsAsync(pattern, options);
381
+ } catch (e) {
382
+ this.adapter.log.error(`Invalid pattern on unsubscribe: ${e.message}`)
383
+ return
384
+ }
362
385
  } else if (type === 'log') {
363
386
  //console.log((socket._name || socket.id) + ' requireLog false');
364
387
  if (this.logEnabled && this.adapter.requireLog) {
@@ -366,8 +389,12 @@ class SocketCommands {
366
389
  this.adapter.requireLog(false, options);
367
390
  }
368
391
  } else if (type === 'fileChange') {
369
- //console.log((socket._name || socket.id) + ' requireLog false');
370
- this.adapter.unsubscribeForeignFiles && this.adapter.unsubscribeForeignFiles(pattern, patternFile, options);
392
+ try {
393
+ this.adapter.unsubscribeForeignFiles && await this.adapter.unsubscribeForeignFiles(pattern, patternFile, options);
394
+ } catch (e) {
395
+ this.adapter.log.error(`Invalid pattern on unsubscribe: ${e.message}`)
396
+ return
397
+ }
371
398
  }
372
399
  delete this.subscribes[type][pattern];
373
400
  }
@@ -384,45 +411,73 @@ class SocketCommands {
384
411
  this.subscribes[type][key]--;
385
412
  if (this.subscribes[type][key] <= 0) {
386
413
  if (type === 'stateChange') {
387
- this.adapter.unsubscribeForeignStates(pattern, options);
414
+ try {
415
+ await this.adapter.unsubscribeForeignStatesAsync(pattern, options);
416
+ } catch (e) {
417
+ this.adapter.log.error(`Invalid pattern on unsubscribe: ${e.message}`)
418
+ return
419
+ }
388
420
  } else if (type === 'objectChange') {
389
- this.adapter.unsubscribeForeignObjects && this.adapter.unsubscribeForeignObjects(pattern, options);
421
+ try {
422
+ this.adapter.unsubscribeForeignObjects && await this.adapter.unsubscribeForeignObjectsAsync(pattern, options);
423
+ } catch (e) {
424
+ this.adapter.log.error(`Invalid pattern on unsubscribe: ${e.message}`)
425
+ return
426
+ }
390
427
  } else if (type === 'log') {
391
428
  if (this.adapter.requireLog && this.logEnabled) {
392
429
  this.logEnabled = false;
393
430
  this.adapter.requireLog(false, options);
394
431
  }
395
432
  } else if (type === 'fileChange') {
396
- this.adapter.unsubscribeForeignFiles && this.adapter.unsubscribeForeignFiles(pattern, patternFile, options);
433
+ try {
434
+ this.adapter.unsubscribeForeignFiles && await this.adapter.unsubscribeForeignFiles(pattern, patternFile, options);
435
+ } catch (e) {
436
+ this.adapter.log.error(`Invalid pattern on unsubscribe: ${e.message}`)
437
+ return
438
+ }
397
439
  }
398
440
  delete this.subscribes[type][key];
399
441
  }
400
442
  }
401
443
  } else {
402
- Object.keys(this.subscribes[type]).forEach(pattern => {
444
+ for (const pattern of Object.keys(this.subscribes[type])) {
403
445
  if (type === 'stateChange') {
404
- //console.log((socket._name || socket.id) + ' unsubscribeForeignStates ' + pattern);
405
- this.adapter.unsubscribeForeignStates(pattern, options);
446
+ try {
447
+ await this.adapter.unsubscribeForeignStatesAsync(pattern, options);
448
+ } catch (e) {
449
+ this.adapter.log.error(`Invalid pattern on unsubscribe: ${e.message}`)
450
+ return
451
+ }
406
452
  } else if (type === 'objectChange') {
407
- //console.log((socket._name || socket.id) + ' unsubscribeForeignObjects ' + pattern);
408
- this.adapter.unsubscribeForeignObjects && this.adapter.unsubscribeForeignObjects(pattern, options);
453
+ try {
454
+ this.adapter.unsubscribeForeignObjects && await this.adapter.unsubscribeForeignObjectsAsync(pattern, options);
455
+ } catch (e) {
456
+ this.adapter.log.error(`Invalid pattern on unsubscribe: ${e.message}`)
457
+ return
458
+ }
409
459
  } else if (type === 'log') {
410
- //console.log((socket._name || socket.id) + ' requireLog false');
460
+ // console.log((socket._name || socket.id) + ' requireLog false');
411
461
  if (this.adapter.requireLog && this.logEnabled) {
412
462
  this.logEnabled = false;
413
463
  this.adapter.requireLog(false, options);
414
464
  }
415
465
  } else if (type === 'fileChange') {
416
466
  const [id, fileName] = pattern.split('####');
417
- this.adapter.unsubscribeForeignFiles && this.adapter.unsubscribeForeignFiles(id, fileName, options);
467
+ try {
468
+ this.adapter.unsubscribeForeignFiles && await this.adapter.unsubscribeForeignFiles(id, fileName, options);
469
+ } catch (e) {
470
+ this.adapter.log.error(`Invalid pattern on unsubscribe: ${e.message}`)
471
+ return
472
+ }
418
473
  }
419
- });
474
+ }
420
475
 
421
476
  this.subscribes[type] = {}
422
477
  }
423
478
  };
424
479
 
425
- subscribeSocket(socket, type) {
480
+ async subscribeSocket(socket, type) {
426
481
  if (!socket || !socket.subscribe) {
427
482
  return;
428
483
  }
@@ -444,9 +499,19 @@ class SocketCommands {
444
499
  if (this.subscribes[type][pattern] === undefined) {
445
500
  this.subscribes[type][pattern] = 1;
446
501
  if (type === 'stateChange') {
447
- this.adapter.subscribeForeignStates(pattern, options);
502
+ try {
503
+ await this.adapter.subscribeForeignStatesAsync(pattern, options);
504
+ } catch (e) {
505
+ this.adapter.log.error(`Invalid pattern on subscribe: ${e.message}`)
506
+ return
507
+ }
448
508
  } else if (type === 'objectChange') {
449
- this.adapter.subscribeForeignObjects && this.adapter.subscribeForeignObjects(pattern, options);
509
+ try {
510
+ this.adapter.subscribeForeignObjects && await this.adapter.subscribeForeignObjectsAsync(pattern, options);
511
+ } catch (e) {
512
+ this.adapter.log.error(`Invalid pattern on subscribe: ${e.message}`)
513
+ return
514
+ }
450
515
  } else if (type === 'log') {
451
516
  if (this.adapter.requireLog && !this.logEnabled) {
452
517
  this.logEnabled = true;
@@ -454,7 +519,12 @@ class SocketCommands {
454
519
  }
455
520
  } else if (type === 'fileChange') {
456
521
  const [id, fileName] = pattern.split('####');
457
- this.adapter.subscribeForeignFiles && this.adapter.subscribeForeignFiles(id, fileName, options);
522
+ try {
523
+ this.adapter.subscribeForeignFiles && await this.adapter.subscribeForeignFiles(id, fileName, options);
524
+ } catch (e) {
525
+ this.adapter.log.error(`Invalid pattern on subscribe: ${e.message}`)
526
+ return
527
+ }
458
528
  }
459
529
  } else {
460
530
  this.subscribes[type][pattern]++;
@@ -462,7 +532,7 @@ class SocketCommands {
462
532
  }
463
533
  }
464
534
 
465
- unsubscribeSocket(socket, type) {
535
+ async unsubscribeSocket(socket, type) {
466
536
  if (!socket || !socket.subscribe) {
467
537
  return;
468
538
  }
@@ -487,9 +557,19 @@ class SocketCommands {
487
557
  this.subscribes[type][pattern]--;
488
558
  if (this.subscribes[type][pattern] <= 0) {
489
559
  if (type === 'stateChange') {
490
- this.adapter.unsubscribeForeignStates(pattern, options);
560
+ try {
561
+ await this.adapter.unsubscribeForeignStatesAsync(pattern, options);
562
+ } catch (e) {
563
+ this.adapter.log.error(`Invalid pattern on unsubscribe: ${e.message}`)
564
+ return
565
+ }
491
566
  } else if (type === 'objectChange') {
492
- this.adapter.unsubscribeForeignObjects && this.adapter.unsubscribeForeignObjects(pattern, options);
567
+ try {
568
+ this.adapter.unsubscribeForeignObjects && await this.adapter.unsubscribeForeignObjectsAsync(pattern, options);
569
+ } catch (e) {
570
+ this.adapter.log.error(`Invalid pattern on unsubscribe: ${e.message}`)
571
+ return
572
+ }
493
573
  } else if (type === 'log') {
494
574
  if (this.adapter.requireLog && !this.logEnabled) {
495
575
  this.logEnabled = true;
@@ -497,7 +577,12 @@ class SocketCommands {
497
577
  }
498
578
  } else if (type === 'fileChange') {
499
579
  const [id, fileName] = pattern.split('####');
500
- this.adapter.unsubscribeForeignFiles && this.adapter.unsubscribeForeignFiles(id, fileName, options);
580
+ try {
581
+ this.adapter.unsubscribeForeignFiles && await this.adapter.unsubscribeForeignFiles(id, fileName, options);
582
+ } catch (e) {
583
+ this.adapter.log.error(`Invalid pattern on unsubscribe: ${e.message}`)
584
+ return
585
+ }
501
586
  }
502
587
  delete this.subscribes[type][pattern];
503
588
  }
@@ -523,7 +608,7 @@ class SocketCommands {
523
608
 
524
609
  _unsubscribeStates(socket, pattern, callback) {
525
610
  if (this._checkPermissions(socket, 'unsubscribe', callback, pattern)) {
526
- if (pattern && typeof pattern === 'object' && pattern instanceof Array) {
611
+ if (Array.isArray(pattern)) {
527
612
  for (let p = 0; p < pattern.length; p++) {
528
613
  this.unsubscribe(socket, 'stateChange', pattern[p]);
529
614
  }
@@ -1495,7 +1580,7 @@ class SocketCommands {
1495
1580
  // @param {function} callback - `function (error)`
1496
1581
  if (this._checkPermissions(socket, 'subscribeObjects', callback, pattern)) {
1497
1582
  try {
1498
- if (pattern && typeof pattern === 'object' && pattern instanceof Array) {
1583
+ if (Array.isArray(pattern)) {
1499
1584
  for (let p = 0; p < pattern.length; p++) {
1500
1585
  this.subscribe(socket, 'objectChange', pattern[p]);
1501
1586
  }
@@ -1519,7 +1604,7 @@ class SocketCommands {
1519
1604
  // @param {function} callback - `function (error)`
1520
1605
  if (this._checkPermissions(socket, 'unsubscribeObjects', callback, pattern)) {
1521
1606
  try {
1522
- if (pattern && typeof pattern === 'object' && pattern instanceof Array) {
1607
+ if (Array.isArray(pattern)) {
1523
1608
  for (let p = 0; p < pattern.length; p++) {
1524
1609
  this.unsubscribe(socket, 'objectChange', pattern[p]);
1525
1610
  }
@@ -277,26 +277,18 @@ class SocketCommandsAdmin extends SocketCommands {
277
277
  this.eventsThreshold.timeActivated = 0;
278
278
  this.adapter.log.info('Subscribe to all states again');
279
279
 
280
- setTimeout(() => {
281
- /*
282
- if (readAll) {
283
- this.adapter.getForeignStates('*', (error, res) => {
284
- this.adapter.log.info('received all states');
285
- for (const id in res) {
286
- if (res.hasOwnProperty(id) && JSON.stringify(states[id]) !== JSON.stringify(res[id])) {
287
- this.server.sockets.emit('stateChange', id, res[id]);
288
- states[id] = res[id];
289
- }
290
- }
291
- });
292
- }
293
- */
294
-
280
+ setTimeout(async() => {
295
281
  this.onThresholdChanged && this.onThresholdChanged(false);
296
282
  this.adapter.unsubscribeForeignStates('system.adapter.*');
297
283
 
298
- Object.keys(this.subscribes.stateChange).forEach(pattern =>
299
- this.adapter.subscribeForeignStates(pattern));
284
+ for (const pattern of Object.keys(this.subscribes.stateChange)) {
285
+ try {
286
+ await this.adapter.subscribeForeignStatesAsync(pattern)
287
+ } catch (e) {
288
+ this.adapter.log.error(`Invalid pattern on subscribe: ${e.message}`)
289
+ return
290
+ }
291
+ }
300
292
  }, 50);
301
293
  }
302
294
  }
@@ -305,15 +297,21 @@ class SocketCommandsAdmin extends SocketCommands {
305
297
  if (!this.eventsThreshold.active) {
306
298
  this.eventsThreshold.active = true;
307
299
 
308
- setTimeout(() => {
300
+ setTimeout(async () => {
309
301
  this.adapter.log.info(`Unsubscribe from all states, except system's, because over ${this.eventsThreshold.repeatSeconds} seconds the number of events is over ${this.eventsThreshold.value} (in last second ${this.eventsThreshold.count})`);
310
302
  this.eventsThreshold.timeActivated = Date.now();
311
303
 
312
304
  this.onThresholdChanged && this.onThresholdChanged(true);
313
305
  //this.server && this.server.sockets && this.server.sockets.emit('eventsThreshold', true);
314
306
 
315
- Object.keys(this.subscribes.stateChange).forEach(pattern =>
316
- this.adapter.unsubscribeForeignStates(pattern));
307
+ for (const pattern of Object.keys(this.subscribes.stateChange)) {
308
+ try {
309
+ await this.adapter.unsubscribeForeignStatesAsync(pattern)
310
+ } catch (e) {
311
+ this.adapter.log.error(`Invalid pattern on unsubscribe: ${e.message}`)
312
+ return
313
+ }
314
+ }
317
315
 
318
316
  this.adapter.subscribeForeignStates('system.adapter.*');
319
317
  }, 100);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iobroker/socket-classes",
3
- "version": "1.3.3",
3
+ "version": "1.4.0",
4
4
  "description": "ioBroker server-side web sockets",
5
5
  "author": {
6
6
  "name": "bluefox",