@iobroker/socket-classes 0.5.4 → 1.0.1

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
@@ -85,8 +85,8 @@ io.close();
85
85
  * [unsubscribeObjects](#unsubscribeobjects_w)
86
86
  * [getObjectView](#getobjectview_w)
87
87
  * [setObject](#setobject_w)
88
- * [getStates](#getstates_w)
89
88
  * [delObject](#delobject_w)
89
+ * [getStates](#getstates_w)
90
90
  * [getState](#getstate_w)
91
91
  * [setState](#setstate_w)
92
92
  * [getBinaryState](#getbinarystate_w)
@@ -210,17 +210,17 @@ Set object.
210
210
  * obj *(object)*: object itself
211
211
  * callback *(function)*: `function (error)`
212
212
 
213
- ### <a name="getstates_w"></a>getStates(pattern, callback)
214
- Read states by pattern
215
- * pattern *(string)*: optional pattern, like `system.adapter.*` or array of state IDs
216
- * callback *(function)*: `function (error, states)`, where `states` is an object like `{'system.adapter.history.0': {_id: 'system.adapter.history.0', common: {name: 'history', ...}, native: {...}, 'system.adapter.history.1': {...}}}`
217
-
218
213
  ### <a name="delobject_w"></a>delObject(id, options, callback)
219
214
  Delete object. Only deletion of flot objects is allowed
220
215
  * id *(string)*: Object ID like, 'flot.0.myChart'
221
216
  * options *(string)*: ignored
222
217
  * callback *(function)*: `function (error)`
223
218
 
219
+ ### <a name="getstates_w"></a>getStates(pattern, callback)
220
+ Read states by pattern
221
+ * pattern *(string)*: optional pattern, like `system.adapter.*` or array of state IDs
222
+ * callback *(function)*: `function (error, states)`, where `states` is an object like `{'system.adapter.history.0': {_id: 'system.adapter.history.0', common: {name: 'history', ...}, native: {...}, 'system.adapter.history.1': {...}}}`
223
+
224
224
  ### <a name="getstate_w"></a>getState(id, callback)
225
225
  Read one state.
226
226
  * id *(string)*: State ID like, 'system.adapter.admin.0.memRss'
@@ -424,10 +424,10 @@ Read all instances of the given adapter, or all instances of all adapters if ada
424
424
  * [unsubscribeObjects](#unsubscribeobjects_a)
425
425
  * [getObjectView](#getobjectview_a)
426
426
  * [setObject](#setobject_a)
427
+ * [delObject](#delobject_a)
427
428
  * [getAllObjects](#getallobjects_a)
428
429
  * [extendObject](#extendobject_a)
429
430
  * [getForeignObjects](#getforeignobjects_a)
430
- * [delObject](#delobject_a)
431
431
  * [delObjects](#delobjects_a)
432
432
  * [getStates](#getstates_a)
433
433
  * [getState](#getstate_a)
@@ -684,6 +684,12 @@ Set object.
684
684
  * obj *(object)*: object itself
685
685
  * callback *(function)*: `function (error)`
686
686
 
687
+ ### <a name="delobject_a"></a>delObject(id, options, callback)
688
+ Delete object or objects recursively. Objects with `dontDelete` cannot be deleted.
689
+ * id *(string)*: Object ID like, 'adapterName.0.channel'
690
+ * options *(string)*: `{recursive: true}`
691
+ * callback *(function)*: `function (error)`
692
+
687
693
  ### <a name="getallobjects_a"></a>getAllObjects(callback)
688
694
  Read absolutely all objects
689
695
  * callback *(function)*: `function (error, objects)`, where `objects` is an object like `{'system.adapter.admin.0': {...}, 'system.adapter.web.0': {...}}`
@@ -700,12 +706,6 @@ Read objects by pattern
700
706
  * type *(string)*: type of objects to delete, like `state`, `channel`, `device`, `host`, `adapter`. Default - `state`
701
707
  * callback *(function)*: `function (error, objects)`, where `objects` is an object like `{'system.adapter.admin.0': {...}, 'system.adapter.web.0': {...}}`
702
708
 
703
- ### <a name="delobject_a"></a>delObject(id, options, callback)
704
- Delete object. Only deletion of flot objects is allowed
705
- * id *(string)*: Object ID like, 'flot.0.myChart'
706
- * options *(string)*: ignored
707
- * callback *(function)*: `function (error)`
708
-
709
709
  ### <a name="delobjects_a"></a>delObjects(id, options, callback)
710
710
  Delete objects recursively. Objects with `dontDelete` cannot be deleted. Same as `delObject` but with `recursive: true`.
711
711
  * id *(string)*: Object ID like, 'adapterName.0.channel'
@@ -760,9 +760,8 @@ Unsubscribe from state changes by pattern. Same as `unsubscribe`.
760
760
  * callback *(function)*: `function (error)`
761
761
 
762
762
  ### <a name="getforeignstates_a"></a>getForeignStates(pattern, callback)
763
- Change user password
764
- * user *(string)*: user name, like 'benjamin`
765
- * pass *(string)*: new password
763
+ Read all states (which might not belong to this adapter) which match the given pattern
764
+ * pattern *(string)*: pattern like
766
765
  * callback *(function)*: `function (error)`
767
766
 
768
767
  ### <a name="readfile_a"></a>readFile(_adapter, fileName, callback)
@@ -883,6 +882,12 @@ Read all instances of the given adapter, or all instances of all adapters if ada
883
882
  -->
884
883
 
885
884
  ## Changelog
885
+ ### 1.0.1 (2022-10-10)
886
+ * (bluefox) Fixed error with delObject
887
+
888
+ ### 0.5.5 (2022-10-09)
889
+ * (Apollon77) Prepare for future js-controller versions
890
+
886
891
  ### 0.5.4 (2022-09-23)
887
892
  * (bluefox) Fixed error in `delObjects` method
888
893
 
@@ -1,21 +1,7 @@
1
+ const utils = require('@iobroker/adapter-core'); // Get common adapter utils
2
+ const pattern2RegEx = utils.commonTools.pattern2RegEx;
1
3
  let axios = null;
2
- let zipFiles;
3
-
4
- // taken from here: https://github.com/ioBroker/ioBroker.js-controller/blob/master/packages/common/src/lib/common/tools.ts#L2386
5
- /**
6
- * Converts ioB pattern into regex.
7
- * @param pattern - Regex string to use it in new RegExp(pattern)
8
- */
9
- function pattern2RegEx(pattern) {
10
- pattern = (pattern || '').toString();
11
-
12
- const startsWithWildcard = pattern[0] === '*';
13
- const endsWithWildcard = pattern[pattern.length - 1] === '*';
14
-
15
- pattern = pattern.replace(/[-/\\^$+?.()|[\]{}]/g, '\\$&').replace(/\*/g, '.*');
16
-
17
- return (startsWithWildcard ? '' : '^') + pattern + (endsWithWildcard ? '' : '$');
18
- }
4
+ let zipFiles = null;
19
5
 
20
6
  class SocketCommands {
21
7
  static ERROR_PERMISSION = 'permissionError';
@@ -717,10 +703,6 @@ class SocketCommands {
717
703
  if (this._checkPermissions(socket, protectedCommands.includes(command) ? 'cmdExec' : 'sendToHost', callback, command)) {
718
704
  // Try to decode this file locally as redis has a limitation for files bigger than 20MB
719
705
  if (command === 'writeDirAsZip' && message && message.data.length > 1024 * 1024) {
720
- if (!zipFiles) {
721
- const utils = require('@iobroker/adapter-core'); // Get common adapter utils
722
- zipFiles = require(utils.controllerDir + '/lib/zipFiles.js');
723
- }
724
706
  let buffer;
725
707
  try {
726
708
  buffer = Buffer.from(message.data, 'base64');
@@ -729,6 +711,8 @@ class SocketCommands {
729
711
  return callback && callback({error: `Cannot convert data: ${error.toString()}`});
730
712
  }
731
713
 
714
+ zipFiles = zipFiles || utils.commonTools.zipFiles;
715
+
732
716
  zipFiles
733
717
  .writeDirAsZip(
734
718
  this.adapter, // normally we have to pass here the internal "objects" object, but as
@@ -1210,27 +1194,6 @@ class SocketCommands {
1210
1194
  }
1211
1195
  };
1212
1196
 
1213
- // this function is overloaded in admin
1214
- this.commands['delObject'] = (socket, id, options, callback) => {
1215
- // Delete object. Only deletion of flot objects is allowed
1216
- // @param {string} id - Object ID like, 'flot.0.myChart'
1217
- // @param {string} options - ignored
1218
- // @param {function} callback - `function (error)`
1219
- if (id.startsWith('flot.')) {
1220
- if (this._checkPermissions(socket, 'delObject', callback, id)) {
1221
- try {
1222
- this.adapter.delForeignObject(id, {user: socket._acl.user}, (error, ...args) =>
1223
- SocketCommands._fixCallback(callback, error, ...args));
1224
- } catch (error) {
1225
- this.adapter.log.error('[delObject] ERROR: ' + error.toString());
1226
- SocketCommands._fixCallback(callback, error);
1227
- }
1228
- }
1229
- } else {
1230
- SocketCommands._fixCallback(callback, SocketCommands.ERROR_PERMISSION);
1231
- }
1232
- };
1233
-
1234
1197
  this.commands['getState'] = (socket, id, callback) => {
1235
1198
  // Read one state.
1236
1199
  // @param {string} id - State ID like, 'system.adapter.admin.0.memRss'
@@ -1356,26 +1319,28 @@ class SocketCommands {
1356
1319
  // @param {string} pattern - pattern like 'system.adapter.*' or array of states like ['system.adapter.admin.0.memRss', 'system.adapter.admin.0.memHeapTotal']
1357
1320
  // @param {function} callback - `function (error)`
1358
1321
  return this._subscribeStates(socket, pattern, callback);
1359
- }
1322
+ };
1323
+
1360
1324
  this.commands['subscribeStates'] = (socket, pattern, callback) => {
1361
1325
  // Subscribe on state changes by pattern. Same as `subscribe`. The events will come as 'stateChange' events to the socket.
1362
1326
  // @param {string} pattern - pattern like 'system.adapter.*' or array of states like ['system.adapter.admin.0.memRss', 'system.adapter.admin.0.memHeapTotal']
1363
1327
  // @param {function} callback - `function (error)`
1364
1328
  return this._subscribeStates(socket, pattern, callback);
1365
- }
1329
+ };
1366
1330
 
1367
1331
  this.commands['unsubscribe'] = (socket, pattern, callback) => {
1368
1332
  // Unsubscribe from state changes by pattern.
1369
1333
  // @param {string} pattern - pattern like 'system.adapter.*' or array of states like ['system.adapter.admin.0.memRss', 'system.adapter.admin.0.memHeapTotal']
1370
1334
  // @param {function} callback - `function (error)`
1371
1335
  return this._unsubscribeStates(socket, pattern, callback);
1372
- }
1336
+ };
1337
+
1373
1338
  this.commands['unsubscribeStates'] = (socket, pattern, callback) => {
1374
1339
  // Unsubscribe from state changes by pattern. Same as `unsubscribe`.
1375
1340
  // @param {string} pattern - pattern like 'system.adapter.*' or array of states like ['system.adapter.admin.0.memRss', 'system.adapter.admin.0.memHeapTotal']
1376
1341
  // @param {function} callback - `function (error)`
1377
1342
  return this._unsubscribeStates(socket, pattern, callback);
1378
- }
1343
+ };
1379
1344
  }
1380
1345
 
1381
1346
  __initCommandsObjects() {
@@ -1400,6 +1365,7 @@ class SocketCommands {
1400
1365
  };
1401
1366
 
1402
1367
  // not admin version of "all objects"
1368
+ // this function is overloaded in admin
1403
1369
  this.commands['getObjects'] = (socket, callback) => {
1404
1370
  // Get all objects that are relevant for web: all states and enums with rooms
1405
1371
  // @param {string} id - object ID.
@@ -1508,6 +1474,27 @@ class SocketCommands {
1508
1474
  }
1509
1475
  }
1510
1476
  };
1477
+
1478
+ // this function is overloaded in admin
1479
+ this.commands['delObject'] = (socket, id, options, callback) => {
1480
+ // Delete object. Only deletion of flot objects is allowed
1481
+ // @param {string} id - Object ID like, 'flot.0.myChart'
1482
+ // @param {string} options - ignored
1483
+ // @param {function} callback - `function (error)`
1484
+ if (id.startsWith('flot.')) {
1485
+ if (this._checkPermissions(socket, 'delObject', callback, id)) {
1486
+ try {
1487
+ this.adapter.delForeignObject(id, {user: socket._acl.user}, (error, ...args) =>
1488
+ SocketCommands._fixCallback(callback, error, ...args));
1489
+ } catch (error) {
1490
+ this.adapter.log.error('[delObject] ERROR: ' + error.toString());
1491
+ SocketCommands._fixCallback(callback, error);
1492
+ }
1493
+ }
1494
+ } else {
1495
+ SocketCommands._fixCallback(callback, SocketCommands.ERROR_PERMISSION);
1496
+ }
1497
+ };
1511
1498
  }
1512
1499
 
1513
1500
  _initCommands() {
@@ -1162,9 +1162,8 @@ class SocketCommandsAdmin extends SocketCommands {
1162
1162
  super.__initCommandsStates();
1163
1163
 
1164
1164
  this.commands['getForeignStates'] = (socket, pattern, callback) => {
1165
- // Change user password
1166
- // @param {string} user - user name, like 'benjamin`
1167
- // @param {string} pass - new password
1165
+ // Read all states (which might not belong to this adapter) which match the given pattern
1166
+ // @param {string} pattern - pattern like
1168
1167
  // @param {function} callback - `function (error)`
1169
1168
  if (this._checkPermissions(socket, 'getStates', callback)) {
1170
1169
  if (typeof callback === 'function') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iobroker/socket-classes",
3
- "version": "0.5.4",
3
+ "version": "1.0.1",
4
4
  "description": "ioBroker server-side web sockets",
5
5
  "author": {
6
6
  "name": "bluefox",
@@ -21,6 +21,7 @@
21
21
  "url": "https://github.com/ioBroker/ioBroker.socket-classes"
22
22
  },
23
23
  "dependencies": {
24
+ "@iobroker/adapter-core": "^2.6.7",
24
25
  "passport": "^0.6.0",
25
26
  "cookie-parser": "^1.4.6",
26
27
  "axios": "^0.27.2"
@@ -28,6 +29,7 @@
28
29
  "devDependencies": {
29
30
  "@alcalzone/release-script": "^3.5.9",
30
31
  "@alcalzone/release-script-plugin-license": "^3.5.9",
32
+ "iobroker.js-controller": "dev",
31
33
  "gulp": "^4.0.2",
32
34
  "chai": "^4.3.6",
33
35
  "mocha": "^10.0.0"