iobroker.javascript 7.10.2 → 7.11.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/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 = {
@@ -1198,44 +1198,8 @@ function sandBox(script, name, verbose, debug, context) {
1198
1198
  }
1199
1199
 
1200
1200
  const config = {
1201
+ ...getHttpRequestConfig(url, options),
1201
1202
  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
1203
  };
1240
1204
 
1241
1205
  sandbox.verbose && sandbox.log(`httpGet(config=${JSON.stringify(config)})`, 'info');
@@ -1250,34 +1214,30 @@ function sandBox(script, name, verbose, debug, context) {
1250
1214
  headers: response.headers,
1251
1215
  });
1252
1216
  } catch (e) {
1253
- errorInCallback(e); //adapter.log.error('Error in callback: ' + e)
1217
+ errorInCallback(e);
1254
1218
  }
1255
1219
  }
1256
1220
  })
1257
1221
  .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
- }
1222
+ if (typeof callback === 'function') {
1223
+ let result = {
1224
+ statusCode: null,
1225
+ data: null,
1226
+ headers: {},
1227
+ };
1228
+
1229
+ if (error.response) {
1230
+ result = {
1231
+ statusCode: error.response.status,
1232
+ data: error.response.data,
1233
+ headers: error.response.headers,
1234
+ };
1269
1235
  }
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
- }
1236
+
1237
+ try {
1238
+ callback.call(sandbox, error.message, result);
1239
+ } catch (e) {
1240
+ errorInCallback(e);
1281
1241
  }
1282
1242
  }
1283
1243
  });
@@ -1289,45 +1249,8 @@ function sandBox(script, name, verbose, debug, context) {
1289
1249
  }
1290
1250
 
1291
1251
  const config = {
1252
+ ...getHttpRequestConfig(url, options),
1292
1253
  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
1254
  };
1332
1255
 
1333
1256
  sandbox.verbose && sandbox.log(`httpPost(config=${JSON.stringify(config)}, data=${data})`, 'info');
@@ -1342,34 +1265,30 @@ function sandBox(script, name, verbose, debug, context) {
1342
1265
  headers: response.headers,
1343
1266
  });
1344
1267
  } catch (e) {
1345
- errorInCallback(e); //adapter.log.error('Error in callback: ' + e)
1268
+ errorInCallback(e);
1346
1269
  }
1347
1270
  }
1348
1271
  })
1349
1272
  .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
- }
1273
+ if (typeof callback === 'function') {
1274
+ let result = {
1275
+ statusCode: null,
1276
+ data: null,
1277
+ headers: {},
1278
+ };
1279
+
1280
+ if (error.response) {
1281
+ result = {
1282
+ statusCode: error.response.status,
1283
+ data: error.response.data,
1284
+ headers: error.response.headers,
1285
+ };
1361
1286
  }
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
- }
1287
+
1288
+ try {
1289
+ callback.call(sandbox, error.message, result);
1290
+ } catch (e) {
1291
+ errorInCallback(e);
1373
1292
  }
1374
1293
  }
1375
1294
  });
@@ -3417,9 +3336,9 @@ function sandBox(script, name, verbose, debug, context) {
3417
3336
  if (format.match(/[WНOО]+/)) {
3418
3337
  let text = adapter.formatDate(date, format);
3419
3338
  if (!language || !consts.dayOfWeeksFull[language]) {
3420
- language = adapter.language || (objects['system.config'] && objects['system.config'].common && objects['system.config'].common.language) || 'de';
3339
+ language = adapter.language || (objects['system.config'] && objects['system.config'].common && objects['system.config'].common.language) || 'en';
3421
3340
  if (!consts.dayOfWeeksFull[language]) {
3422
- language = 'de';
3341
+ language = 'en';
3423
3342
  }
3424
3343
  }
3425
3344
  if (typeof date === 'number' || typeof date === 'string') {
@@ -3569,7 +3488,15 @@ function sandBox(script, name, verbose, debug, context) {
3569
3488
  _adapter = _adapter || '0_userdata.0';
3570
3489
  sandbox.verbose && sandbox.log(`readFile(adapter=${_adapter}, fileName=${fileName})`, 'info');
3571
3490
 
3572
- adapter.readFile(_adapter, fileName, callback);
3491
+ adapter.fileExists(_adapter, fileName, (error, result) => {
3492
+ if (error) {
3493
+ callback(error);
3494
+ } else if (!result) {
3495
+ callback('Not exists');
3496
+ } else {
3497
+ adapter.readFile(_adapter, fileName, callback);
3498
+ }
3499
+ });
3573
3500
  },
3574
3501
  unlink: function (_adapter, fileName, callback) {
3575
3502
  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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iobroker.javascript",
3
- "version": "7.10.2",
3
+ "version": "7.11.0",
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",