iobroker.javascript 7.10.2 → 7.11.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/lib/sandbox.js CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  'use strict';
6
6
 
7
- const { isObject, isArray, promisify } = require('./tools');
7
+ const { isObject, isArray, promisify, getHttpRequestConfig } = require('./tools');
8
8
  const utils = require('@iobroker/adapter-core');
9
9
  const pattern2RegEx = utils.commonTools.pattern2RegEx;
10
10
  // let context = {
@@ -23,7 +23,8 @@ const pattern2RegEx = utils.commonTools.pattern2RegEx;
23
23
  // enums,
24
24
  // channels,
25
25
  // devices,
26
- // isEnums
26
+ // isEnums,
27
+ // getAbsoluteDefaultDataDir,
27
28
  // };
28
29
 
29
30
  /**
@@ -517,6 +518,7 @@ function sandBox(script, name, verbose, debug, context) {
517
518
  name, // deprecated
518
519
  scriptName: name,
519
520
  instance: adapter.instance,
521
+ defaultDataDir: context.getAbsoluteDefaultDataDir(),
520
522
  verbose,
521
523
  exports: {}, // Polyfill for the export object in TypeScript modules
522
524
  require: function (md) {
@@ -1161,14 +1163,14 @@ function sandBox(script, name, verbose, debug, context) {
1161
1163
  if (!adapter.config.enableExec) {
1162
1164
  const error = 'exec is not available. Please enable "Enable Exec" option in instance settings';
1163
1165
  adapter.log.error(error);
1164
- sandbox.log(error);
1166
+ sandbox.log(error, 'warn');
1167
+
1165
1168
  if (typeof callback === 'function') {
1166
1169
  setImmediate(callback, error);
1167
1170
  }
1168
1171
  } else {
1169
- if (sandbox.verbose) {
1170
- sandbox.log(`exec: ${cmd}`, 'info');
1171
- }
1172
+ sandbox.verbose && sandbox.log(`exec(cmd=${cmd})`, 'info');
1173
+
1172
1174
  if (debug) {
1173
1175
  sandbox.log(words._('Command %s was not executed, while debug mode is active', cmd), 'warn');
1174
1176
  if (typeof callback === 'function') {
@@ -1177,18 +1179,26 @@ function sandBox(script, name, verbose, debug, context) {
1177
1179
  });
1178
1180
  }
1179
1181
  } else {
1180
- return mods.child_process.exec(cmd, options, callback);
1182
+ return mods.child_process.exec(cmd, options, (error, stdout, stderr) => {
1183
+ if (typeof callback === 'function') {
1184
+ try {
1185
+ callback.call(sandbox, error, stdout, stderr);
1186
+ } catch (e) {
1187
+ errorInCallback(e);
1188
+ }
1189
+ }
1190
+ });
1181
1191
  }
1182
1192
  }
1183
1193
  },
1184
1194
  email: function (msg) {
1185
1195
  sandbox.verbose && sandbox.log(`email(msg=${JSON.stringify(msg)})`, 'info');
1186
- sandbox.log(`email(msg=${JSON.stringify(msg)}) is deprecated. Please use sendTo instead!`, 'info');
1196
+ sandbox.log(`email(msg=${JSON.stringify(msg)}) is deprecated. Please use sendTo instead!`, 'warn');
1187
1197
  adapter.sendTo('email', msg);
1188
1198
  },
1189
1199
  pushover: function (msg) {
1190
1200
  sandbox.verbose && sandbox.log(`pushover(msg=${JSON.stringify(msg)})`, 'info');
1191
- sandbox.log(`pushover(msg=${JSON.stringify(msg)}) is deprecated. Please use sendTo instead!`, 'info');
1201
+ sandbox.log(`pushover(msg=${JSON.stringify(msg)}) is deprecated. Please use sendTo instead!`, 'warn');
1192
1202
  adapter.sendTo('pushover', msg);
1193
1203
  },
1194
1204
  httpGet: function(url, options, callback) {
@@ -1198,44 +1208,8 @@ function sandBox(script, name, verbose, debug, context) {
1198
1208
  }
1199
1209
 
1200
1210
  const config = {
1211
+ ...getHttpRequestConfig(url, options),
1201
1212
  method: 'get',
1202
- url,
1203
- validateStatus: (status) => status >= 200,
1204
- responseType: 'text',
1205
- responseEncoding: 'utf8',
1206
- };
1207
-
1208
- config.timeout = (options && options.timeout) ? options.timeout : 2000;
1209
-
1210
- if (!options.headers) {
1211
- options.headers = {};
1212
- }
1213
-
1214
- if (options && options.basicAuth) {
1215
- config.auth = {
1216
- username: options.basicAuth.user,
1217
- password: options.basicAuth.password,
1218
- };
1219
- } else if (options.bearerAuth) {
1220
- options.headers['Authorization'] = `Bearer ${options.bearerAuth}`;
1221
- } else {
1222
- const uri = new URL(url);
1223
- if (uri.username && uri.password) {
1224
- const username = decodeURIComponent(uri.username);
1225
- const password = decodeURIComponent(uri.password);
1226
-
1227
- config.url = config.url.replace(`${uri.protocol}//${username}:${password}@`, `${uri.protocol}//`);
1228
- config.auth = {
1229
- username,
1230
- password,
1231
- };
1232
- }
1233
- }
1234
-
1235
- // Set default headers
1236
- config.headers = {
1237
- 'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:109.0) Gecko/20100101 Firefox/121.0',
1238
- ...options.headers,
1239
1213
  };
1240
1214
 
1241
1215
  sandbox.verbose && sandbox.log(`httpGet(config=${JSON.stringify(config)})`, 'info');
@@ -1250,34 +1224,30 @@ function sandBox(script, name, verbose, debug, context) {
1250
1224
  headers: response.headers,
1251
1225
  });
1252
1226
  } catch (e) {
1253
- errorInCallback(e); //adapter.log.error('Error in callback: ' + e)
1227
+ errorInCallback(e);
1254
1228
  }
1255
1229
  }
1256
1230
  })
1257
1231
  .catch(error => {
1258
- if (error.response) {
1259
- if (typeof callback === 'function') {
1260
- try {
1261
- callback.call(sandbox, error.message, {
1262
- statusCode: error.response.status,
1263
- data: error.response.data,
1264
- headers: error.response.headers,
1265
- });
1266
- } catch (e) {
1267
- errorInCallback(e); //adapter.log.error('Error in callback: ' + e)
1268
- }
1232
+ if (typeof callback === 'function') {
1233
+ let result = {
1234
+ statusCode: null,
1235
+ data: null,
1236
+ headers: {},
1237
+ };
1238
+
1239
+ if (error.response) {
1240
+ result = {
1241
+ statusCode: error.response.status,
1242
+ data: error.response.data,
1243
+ headers: error.response.headers,
1244
+ };
1269
1245
  }
1270
- } else {
1271
- if (typeof callback === 'function') {
1272
- try {
1273
- callback.call(sandbox, error.message, {
1274
- statusCode: null,
1275
- data: null,
1276
- headers: {},
1277
- });
1278
- } catch (e) {
1279
- errorInCallback(e); //adapter.log.error('Error in callback: ' + e)
1280
- }
1246
+
1247
+ try {
1248
+ callback.call(sandbox, error.message, result);
1249
+ } catch (e) {
1250
+ errorInCallback(e);
1281
1251
  }
1282
1252
  }
1283
1253
  });
@@ -1289,45 +1259,8 @@ function sandBox(script, name, verbose, debug, context) {
1289
1259
  }
1290
1260
 
1291
1261
  const config = {
1262
+ ...getHttpRequestConfig(url, options),
1292
1263
  method: 'post',
1293
- url,
1294
- data,
1295
- validateStatus: (status) => status >= 200,
1296
- responseType: 'text',
1297
- responseEncoding: 'utf8',
1298
- };
1299
-
1300
- config.timeout = (options && options.timeout) ? options.timeout : 2000;
1301
-
1302
- if (!options.headers) {
1303
- options.headers = {};
1304
- }
1305
-
1306
- if (options && options.basicAuth) {
1307
- config.auth = {
1308
- username: options.basicAuth.user,
1309
- password: options.basicAuth.password,
1310
- };
1311
- } else if (options.bearerAuth) {
1312
- options.headers['Authorization'] = `Bearer ${options.bearerAuth}`;
1313
- } else {
1314
- const uri = new URL(url);
1315
- if (uri.username && uri.password) {
1316
- const username = decodeURIComponent(uri.username);
1317
- const password = decodeURIComponent(uri.password);
1318
-
1319
- config.url = config.url.replace(`${uri.protocol}//${username}:${password}@`, `${uri.protocol}//`);
1320
- config.auth = {
1321
- username,
1322
- password,
1323
- };
1324
- }
1325
- }
1326
-
1327
- // Set default headers
1328
- config.headers = {
1329
- 'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:109.0) Gecko/20100101 Firefox/121.0',
1330
- ...options.headers,
1331
1264
  };
1332
1265
 
1333
1266
  sandbox.verbose && sandbox.log(`httpPost(config=${JSON.stringify(config)}, data=${data})`, 'info');
@@ -1342,34 +1275,30 @@ function sandBox(script, name, verbose, debug, context) {
1342
1275
  headers: response.headers,
1343
1276
  });
1344
1277
  } catch (e) {
1345
- errorInCallback(e); //adapter.log.error('Error in callback: ' + e)
1278
+ errorInCallback(e);
1346
1279
  }
1347
1280
  }
1348
1281
  })
1349
1282
  .catch(error => {
1350
- if (error.response) {
1351
- if (typeof callback === 'function') {
1352
- try {
1353
- callback.call(sandbox, error.message, {
1354
- statusCode: error.response.status,
1355
- data: error.response.data,
1356
- headers: error.response.headers,
1357
- });
1358
- } catch (e) {
1359
- errorInCallback(e); //adapter.log.error('Error in callback: ' + e)
1360
- }
1283
+ if (typeof callback === 'function') {
1284
+ let result = {
1285
+ statusCode: null,
1286
+ data: null,
1287
+ headers: {},
1288
+ };
1289
+
1290
+ if (error.response) {
1291
+ result = {
1292
+ statusCode: error.response.status,
1293
+ data: error.response.data,
1294
+ headers: error.response.headers,
1295
+ };
1361
1296
  }
1362
- } else {
1363
- if (typeof callback === 'function') {
1364
- try {
1365
- callback.call(sandbox, error.message, {
1366
- statusCode: null,
1367
- data: null,
1368
- headers: {},
1369
- });
1370
- } catch (e) {
1371
- errorInCallback(e); //adapter.log.error('Error in callback: ' + e)
1372
- }
1297
+
1298
+ try {
1299
+ callback.call(sandbox, error.message, result);
1300
+ } catch (e) {
1301
+ errorInCallback(e);
1373
1302
  }
1374
1303
  }
1375
1304
  });
@@ -3417,9 +3346,9 @@ function sandBox(script, name, verbose, debug, context) {
3417
3346
  if (format.match(/[WНOО]+/)) {
3418
3347
  let text = adapter.formatDate(date, format);
3419
3348
  if (!language || !consts.dayOfWeeksFull[language]) {
3420
- language = adapter.language || (objects['system.config'] && objects['system.config'].common && objects['system.config'].common.language) || 'de';
3349
+ language = adapter.language || (objects['system.config'] && objects['system.config'].common && objects['system.config'].common.language) || 'en';
3421
3350
  if (!consts.dayOfWeeksFull[language]) {
3422
- language = 'de';
3351
+ language = 'en';
3423
3352
  }
3424
3353
  }
3425
3354
  if (typeof date === 'number' || typeof date === 'string') {
@@ -3569,7 +3498,15 @@ function sandBox(script, name, verbose, debug, context) {
3569
3498
  _adapter = _adapter || '0_userdata.0';
3570
3499
  sandbox.verbose && sandbox.log(`readFile(adapter=${_adapter}, fileName=${fileName})`, 'info');
3571
3500
 
3572
- adapter.readFile(_adapter, fileName, callback);
3501
+ adapter.fileExists(_adapter, fileName, (error, result) => {
3502
+ if (error) {
3503
+ callback(error);
3504
+ } else if (!result) {
3505
+ callback('Not exists');
3506
+ } else {
3507
+ adapter.readFile(_adapter, fileName, callback);
3508
+ }
3509
+ });
3573
3510
  },
3574
3511
  unlink: function (_adapter, fileName, callback) {
3575
3512
  if (typeof fileName === 'function') {
package/lib/tools.js CHANGED
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
- const fs = require('fs');
4
- const path = require('path');
5
- const crypto = require('crypto');
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+ const crypto = require('node:crypto');
6
6
 
7
7
  /**
8
8
  * Tests whether the given variable is a real object and not an Array
@@ -125,6 +125,50 @@ function hashSource(source) {
125
125
  return crypto.createHash('md5').update(source).digest('hex');
126
126
  }
127
127
 
128
+ function getHttpRequestConfig(url, options) {
129
+ const config = {
130
+ method: 'get',
131
+ url,
132
+ validateStatus: (status) => status >= 200,
133
+ responseType: (options && options.responseType) ? options.responseType : 'text',
134
+ responseEncoding: 'utf8',
135
+ timeout: (options && !isNaN(options.timeout)) ? options.timeout : 2000
136
+ };
137
+
138
+ if (!options.headers) {
139
+ options.headers = {};
140
+ }
141
+
142
+ if (options && options.basicAuth) {
143
+ config.auth = {
144
+ username: options.basicAuth.user,
145
+ password: options.basicAuth.password,
146
+ };
147
+ } else if (options.bearerAuth) {
148
+ options.headers['Authorization'] = `Bearer ${options.bearerAuth}`;
149
+ } else {
150
+ const uri = new URL(url);
151
+ if (uri.username && uri.password) {
152
+ const username = decodeURIComponent(uri.username);
153
+ const password = decodeURIComponent(uri.password);
154
+
155
+ config.url = config.url.replace(`${uri.protocol}//${username}:${password}@`, `${uri.protocol}//`);
156
+ config.auth = {
157
+ username,
158
+ password,
159
+ };
160
+ }
161
+ }
162
+
163
+ // Set default headers
164
+ config.headers = {
165
+ 'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:109.0) Gecko/20100101 Firefox/121.0',
166
+ ...options.headers,
167
+ };
168
+
169
+ return config;
170
+ }
171
+
128
172
  module.exports = {
129
173
  isArray,
130
174
  isObject,
@@ -133,4 +177,5 @@ module.exports = {
133
177
  promisify,
134
178
  promisifyNoError,
135
179
  hashSource,
180
+ getHttpRequestConfig,
136
181
  };
package/main.js CHANGED
@@ -59,7 +59,7 @@ const forbiddenMirrorLocations = [
59
59
  '../log'
60
60
  ];
61
61
 
62
- const utils = require('@iobroker/adapter-core'); // Get common adapter utils
62
+ const utils = require('@iobroker/adapter-core');
63
63
  const words = require('./lib/words');
64
64
  const sandBox = require('./lib/sandbox');
65
65
  const eventObj = require('./lib/eventObj');
@@ -288,6 +288,7 @@ const context = {
288
288
  leadingZeros: true
289
289
  },
290
290
  rulesOpened: null, //opened rules
291
+ getAbsoluteDefaultDataDir: utils.getAbsoluteDefaultDataDir,
291
292
  };
292
293
 
293
294
  const regExGlobalOld = /_global$/;
@@ -1036,7 +1037,8 @@ function main() {
1036
1037
  context.errorLogFunction = webstormDebug ? console : adapter.log;
1037
1038
  activeStr = `${adapter.namespace}.scriptEnabled.`;
1038
1039
 
1039
- mods.fs = new require('./lib/protectFs')(adapter.log);
1040
+ mods.fs = new require('./lib/protectFs')(adapter.log, utils.getAbsoluteDefaultDataDir());
1041
+ mods['fs/promises'] = mods.fs.promises; // to avoid require('fs/promises');
1040
1042
 
1041
1043
  // try to read TS declarations
1042
1044
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iobroker.javascript",
3
- "version": "7.10.2",
3
+ "version": "7.11.1",
4
4
  "description": "Rules Engine for ioBroker",
5
5
  "author": "bluefox <dogafox@gmail.com>",
6
6
  "contributors": [
@@ -41,7 +41,7 @@
41
41
  "@types/node": "should match the lowest MAJOR version of Node.js we support."
42
42
  },
43
43
  "dependencies": {
44
- "@iobroker/adapter-core": "^3.0.4",
44
+ "@iobroker/adapter-core": "^3.0.6",
45
45
  "@types/node": "^20.11.30",
46
46
  "@types/request": "^2.48.12",
47
47
  "axios": "^1.6.8",