@iobroker/js-controller-common 4.0.5 → 4.0.8-alpha.0-20220211-7f8106a5

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.
@@ -61,7 +61,7 @@ const IoSeq =
61
61
  Seq &&
62
62
  class extends Seq {
63
63
  log(info, callback) {
64
- const ioInfo = info;
64
+ const ioInfo = deepClone(info);
65
65
  ioInfo.props = ioInfo.props || {};
66
66
 
67
67
  // map our log levels to Seq levels
@@ -10,7 +10,7 @@ const cpPromise = require('promisify-child-process');
10
10
  const jwt = require('jsonwebtoken');
11
11
  const { createInterface } = require('readline');
12
12
  const { PassThrough } = require('stream');
13
- const { detectPackageManager } = require('@alcalzone/pak');
13
+ const { detectPackageManager, packageManagers } = require('@alcalzone/pak');
14
14
  const EXIT_CODES = require('./exitCodes');
15
15
  const zlib = require('zlib');
16
16
 
@@ -215,11 +215,15 @@ async function isSingleHost(objects) {
215
215
  * @return Promise<boolean> true if one or more hosts running else false
216
216
  */
217
217
  async function isHostRunning(objects, states) {
218
- const res = await objects.getObjectViewAsync('system', 'host', { startkey: '', endkey: '\u9999' });
218
+ // do it without object view for now, can be reverted if no one downgrades to < 4 (redis-sets)
219
+ // const res = await objects.getObjectViewAsync('system', 'host', { startkey: '', endkey: '\u9999' });
220
+
221
+ const res = await objects.getObjectList({ startkey: 'system.host.', endkey: 'system.host.\u9999' });
222
+ res.rows = res.rows.filter(obj => obj.value && obj.value.type === 'host');
219
223
 
220
224
  for (const hostObj of res.rows) {
221
225
  const state = await states.getState(`${hostObj.id}.alive`);
222
- if (state.val) {
226
+ if (state && state.val) {
223
227
  return true;
224
228
  }
225
229
  }
@@ -1258,34 +1262,32 @@ function getRepositoryFile(urlOrPath, additionalInfo, callback) {
1258
1262
  */
1259
1263
  async function getRepositoryFileAsync(url, hash, force, _actualRepo) {
1260
1264
  let _hash;
1261
- if (_actualRepo && !force && hash && (url.startsWith('http://') || url.startsWith('https://'))) {
1262
- axios = axios || require('axios');
1263
- _hash = await axios({ url: url.replace(/\.json$/, '-hash.json'), timeout: 10000 });
1264
- if (_hash && _hash.data && hash === _hash.data.hash) {
1265
- return _actualRepo;
1266
- }
1267
- }
1268
-
1269
1265
  let data;
1270
1266
 
1271
1267
  if (url.startsWith('http://') || url.startsWith('https://')) {
1272
1268
  axios = axios || require('axios');
1273
- if (!_hash) {
1269
+ try {
1274
1270
  _hash = await axios({ url: url.replace(/\.json$/, '-hash.json'), timeout: 10000 });
1271
+ } catch {
1272
+ // ignore missing hash file
1275
1273
  }
1276
1274
 
1277
- if (_actualRepo && hash && _hash && _hash.data && _hash.data.hash === hash) {
1275
+ if (_actualRepo && !force && hash && _hash && _hash.data && _hash.data.hash === hash) {
1278
1276
  data = _actualRepo;
1279
1277
  } else {
1280
1278
  const agent = `${module.exports.appName}, RND: ${randomID}, Node:${process.version}, V:${
1281
1279
  require('../../package.json').version
1282
1280
  }`;
1283
- data = await axios({
1284
- url,
1285
- timeout: 10000,
1286
- headers: { 'User-Agent': agent }
1287
- });
1288
- data = data.data;
1281
+ try {
1282
+ data = await axios({
1283
+ url,
1284
+ timeout: 10000,
1285
+ headers: { 'User-Agent': agent }
1286
+ });
1287
+ data = data.data;
1288
+ } catch (e) {
1289
+ throw new Error(`Cannot download repository file from "${url}": ${e.message}`);
1290
+ }
1289
1291
  }
1290
1292
  } else {
1291
1293
  if (fs.existsSync(url)) {
@@ -1456,6 +1458,38 @@ function getSystemNpmVersion(callback) {
1456
1458
 
1457
1459
  const getSystemNpmVersionAsync = promisify(getSystemNpmVersion);
1458
1460
 
1461
+ /**
1462
+ * @private
1463
+ * Figure out which package manager is in charge, but with a fallback to npm.
1464
+ * @param {string} [cwd] Which directory to work in. If none is given, this defaults to ioBroker's root directory.
1465
+ * @returns {Promise<PackageManager>}
1466
+ */
1467
+ async function detectPackageManagerWithFallback(cwd) {
1468
+ try {
1469
+ // For the first attempt, use pak's default of requiring a lockfile. This makes sure we find ioBroker's root dir
1470
+ return await detectPackageManager(
1471
+ typeof cwd === 'string'
1472
+ ? // If a cwd was provided, use it
1473
+ { cwd }
1474
+ : // Otherwise try to find the ioBroker root dir
1475
+ {
1476
+ cwd: __dirname,
1477
+ setCwdToPackageRoot: true
1478
+ }
1479
+ );
1480
+ } catch {
1481
+ // Lockfile not found, use default to avoid picking up a wrong package manager
1482
+ // like a globally installed yarn
1483
+ }
1484
+
1485
+ // Since we have no lockfile to rely on, assume the root dir is 2 levels above js-controller
1486
+ const ioBrokerRootDir = path.join(getControllerDir(), '../..');
1487
+ // And fallback to npm
1488
+ const pak = new packageManagers.npm();
1489
+ pak.cwd = cwd || ioBrokerRootDir;
1490
+ return pak;
1491
+ }
1492
+
1459
1493
  /**
1460
1494
  * @typedef {object} InstallNodeModuleOptions
1461
1495
  * @property {boolean} [unsafePerm] Whether the `--unsafe-perm` flag should be used
@@ -1471,16 +1505,7 @@ const getSystemNpmVersionAsync = promisify(getSystemNpmVersion);
1471
1505
  */
1472
1506
  async function installNodeModule(npmUrl, options = {}) {
1473
1507
  // Figure out which package manager is in charge (probably npm at this point)
1474
- const pak = await detectPackageManager(
1475
- typeof options.cwd === 'string'
1476
- ? // If a cwd was provided, use it
1477
- { cwd: options.cwd }
1478
- : // Otherwise find the ioBroker root dir
1479
- {
1480
- cwd: __dirname,
1481
- setCwdToPackageRoot: true
1482
- }
1483
- );
1508
+ const pak = await detectPackageManagerWithFallback(options.cwd);
1484
1509
  // By default, don't print all the stuff the package manager spits out
1485
1510
  if (!options.debug) {
1486
1511
  pak.loglevel = 'error';
@@ -1520,16 +1545,7 @@ async function installNodeModule(npmUrl, options = {}) {
1520
1545
  */
1521
1546
  async function uninstallNodeModule(packageName, options = {}) {
1522
1547
  // Figure out which package manager is in charge (probably npm at this point)
1523
- const pak = await detectPackageManager(
1524
- typeof options.cwd === 'string'
1525
- ? // If a cwd was provided, use it
1526
- { cwd: options.cwd }
1527
- : // Otherwise find the ioBroker root dir
1528
- {
1529
- cwd: __dirname,
1530
- setCwdToPackageRoot: true
1531
- }
1532
- );
1548
+ const pak = await detectPackageManagerWithFallback(options.cwd);
1533
1549
  // By default, don't print all the stuff the package manager spits out
1534
1550
  if (!options.debug) {
1535
1551
  pak.loglevel = 'error';
@@ -1563,16 +1579,7 @@ async function uninstallNodeModule(packageName, options = {}) {
1563
1579
  */
1564
1580
  async function rebuildNodeModules(options = {}) {
1565
1581
  // Figure out which package manager is in charge (probably npm at this point)
1566
- const pak = await detectPackageManager(
1567
- typeof options.cwd === 'string'
1568
- ? // If a cwd was provided, use it
1569
- { cwd: options.cwd }
1570
- : // Otherwise find the ioBroker root dir
1571
- {
1572
- cwd: __dirname,
1573
- setCwdToPackageRoot: true
1574
- }
1575
- );
1582
+ const pak = await detectPackageManagerWithFallback(options.cwd);
1576
1583
  // By default, don't print all the stuff the package manager spits out
1577
1584
  if (!options.debug) {
1578
1585
  pak.loglevel = 'error';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iobroker/js-controller-common",
3
- "version": "4.0.5",
3
+ "version": "4.0.8-alpha.0-20220211-7f8106a5",
4
4
  "engines": {
5
5
  "node": ">=12.0.0"
6
6
  },
@@ -47,5 +47,5 @@
47
47
  "lib/",
48
48
  "index.js"
49
49
  ],
50
- "gitHead": "ba55e7d15a1e68e18d03254d0fa8030d7f785651"
50
+ "gitHead": "455a43ce329d9c140b817813948f95431493f597"
51
51
  }