@abtnode/core 1.6.18 → 1.6.19

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/api/node.js CHANGED
@@ -93,7 +93,7 @@ class NodeAPI {
93
93
  async updateNodeInfo(entity = {}, context) {
94
94
  await validateNodeInfo(entity, context);
95
95
 
96
- if (entity.autoUpgrade) {
96
+ if (entity.autoUpgrade && process.env.NODE_ENV !== 'development') {
97
97
  try {
98
98
  canPackageReadWrite(process.env.ABT_NODE_BINARY_NAME, process.env.ABT_NODE_PACKAGE_NAME);
99
99
  } catch (err) {
@@ -25,6 +25,7 @@ const { BLOCKLET_PURCHASE_NFT_TYPE } = require('@abtnode/constant');
25
25
  const getBlockletEngine = require('@blocklet/meta/lib/engine');
26
26
  const {
27
27
  isFreeBlocklet,
28
+ isComponentBlocklet,
28
29
  isDeletableBlocklet,
29
30
  getRequiredMissingConfigs,
30
31
  hasRunnableComponent,
@@ -88,6 +89,7 @@ const {
88
89
  checkDuplicateComponents,
89
90
  getDiffFiles,
90
91
  getBundleDir,
92
+ needBlockletDownload,
91
93
  } = require('../../util/blocklet');
92
94
  const states = require('../../states');
93
95
  const BlockletRegistry = require('../registry');
@@ -819,21 +821,23 @@ class BlockletManager extends BaseBlockletManager {
819
821
  };
820
822
  }
821
823
 
822
- async updateChildren({ updateId, did: inputDid, children: inputChildren }, context) {
824
+ async updateChildren({ updateId, did: inputDid, children: inputChildren, oldBlocklet: inputOldBlocklet }, context) {
823
825
  let did;
824
826
  let children;
827
+ let oldBlocklet;
825
828
  if (!updateId && inputDid && inputChildren) {
826
829
  did = inputDid;
827
830
  children = inputChildren;
831
+ oldBlocklet = inputOldBlocklet;
828
832
  } else {
829
833
  const sessionData = await states.session.end(updateId);
830
834
  did = sessionData.did;
831
835
  const { staticChildren = [], dynamicChildren = [] } = sessionData;
832
836
  children = [...staticChildren, ...dynamicChildren.map((x) => ({ ...x, dynamic: true }))];
837
+ oldBlocklet = await states.blocklet.getBlocklet(did);
833
838
  }
834
839
 
835
840
  // get old blocklet
836
- const oldBlocklet = await states.blocklet.getBlocklet(did);
837
841
  const { meta } = oldBlocklet;
838
842
  const { name, version } = meta;
839
843
 
@@ -1179,9 +1183,6 @@ class BlockletManager extends BaseBlockletManager {
1179
1183
  return;
1180
1184
  }
1181
1185
 
1182
- const blocklet1 = await states.blocklet.setBlockletStatus(did, BlockletStatus.downloading);
1183
- this.emit(BlockletEvents.statusChange, blocklet1);
1184
-
1185
1186
  preDownloadLock.release();
1186
1187
 
1187
1188
  const { isCancelled } = await this._downloadBlocklet(blocklet, oldBlocklet);
@@ -1434,6 +1435,10 @@ class BlockletManager extends BaseBlockletManager {
1434
1435
  throw new Error('Cannot add self as a component');
1435
1436
  }
1436
1437
 
1438
+ if (!isComponentBlocklet(meta)) {
1439
+ throw new Error('The blocklet cannot be a component');
1440
+ }
1441
+
1437
1442
  const newChildren = await parseChildren(blocklet.meta, {
1438
1443
  children: [
1439
1444
  {
@@ -1447,10 +1452,14 @@ class BlockletManager extends BaseBlockletManager {
1447
1452
 
1448
1453
  checkDuplicateComponents(blocklet.children, newChildren);
1449
1454
 
1455
+ // add component to db
1456
+ await states.blocklet.addChildren(rootDid, newChildren);
1457
+
1450
1458
  return this.updateChildren(
1451
1459
  {
1452
1460
  did: rootDid,
1453
1461
  children: [...blocklet.children, ...newChildren],
1462
+ oldBlocklet: blocklet,
1454
1463
  },
1455
1464
  context
1456
1465
  );
@@ -2272,7 +2281,7 @@ class BlockletManager extends BaseBlockletManager {
2272
2281
  } else {
2273
2282
  const status =
2274
2283
  oldBlocklet.status === BlockletStatus.installed ? BlockletStatus.installed : BlockletStatus.stopped;
2275
- await states.blocklet.setBlockletStatus(did, status);
2284
+ await states.blocklet.setBlockletStatus(did, status, { children: 'all' });
2276
2285
  }
2277
2286
 
2278
2287
  blocklet = await this.ensureBlocklet(did, context);
@@ -2496,10 +2505,7 @@ class BlockletManager extends BaseBlockletManager {
2496
2505
  } = blocklet;
2497
2506
 
2498
2507
  const metas = [];
2499
- if (
2500
- ![BlockletSource.upload, BlockletSource.local].includes(blocklet.source) &&
2501
- get(oldBlocklet, 'meta.dist.integrity') !== get(blocklet, 'meta.dist.integrity')
2502
- ) {
2508
+ if (needBlockletDownload(blocklet)) {
2503
2509
  metas.push(blocklet.meta);
2504
2510
  }
2505
2511
 
@@ -2510,16 +2516,17 @@ class BlockletManager extends BaseBlockletManager {
2510
2516
 
2511
2517
  for (const child of blocklet.children) {
2512
2518
  const oldChild = oldChildren[child.meta.did];
2513
- if (
2514
- !oldChild ||
2515
- (![BlockletSource.upload, BlockletSource.local].includes(child.source) &&
2516
- child.sourceUrl &&
2517
- get(oldChild, 'meta.dist.integrity') !== get(child, 'meta.dist.integrity'))
2518
- ) {
2519
+ if (needBlockletDownload(child, oldChild)) {
2519
2520
  metas.push(child.meta);
2520
2521
  }
2521
2522
  }
2522
2523
 
2524
+ // update children status
2525
+ const blocklet1 = await states.blocklet.setBlockletStatus(did, BlockletStatus.downloading, {
2526
+ children: metas.map((x) => ({ did: x.did })),
2527
+ });
2528
+ this.emit(BlockletEvents.statusChange, blocklet1);
2529
+
2523
2530
  try {
2524
2531
  logger.info('Download Blocklet', { name, did, bundles: metas.map((x) => get(x, 'dist.tarball')) });
2525
2532
  const tasks = [];
@@ -748,8 +748,7 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
748
748
 
749
749
  const existSite = await states.site.findOne({ domain: domainGroup });
750
750
  if (!existSite) {
751
- const ipEchoDnsDomain = getIpDnsDomainForBlocklet(blocklet, webInterface, nodeInfo.did);
752
- const domainAliases = [{ value: ipEchoDnsDomain, isProtected: true }];
751
+ const domainAliases = [];
753
752
 
754
753
  const didDomain = getDidDomainForBlocklet({
755
754
  name: blocklet.meta.name,
@@ -757,7 +756,10 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
757
756
  didDomain: nodeInfo.didDomain,
758
757
  });
759
758
 
760
- domainAliases.push({ value: didDomain, isProtected: true });
759
+ const ipEchoDnsDomain = getIpDnsDomainForBlocklet(blocklet, webInterface, nodeInfo.did);
760
+
761
+ // let didDomain in front of ipEchoDnsDomain
762
+ domainAliases.push({ value: didDomain, isProtected: true }, { value: ipEchoDnsDomain, isProtected: true });
761
763
 
762
764
  await routerManager.addRoutingSite(
763
765
  {
@@ -221,10 +221,10 @@ class RouterManager extends EventEmitter {
221
221
  }
222
222
  }
223
223
 
224
- const updateResult = await states.site.update(
225
- { _id: id },
226
- { $push: { domainAliases: { value: domainAlias, isProtected: false } } }
227
- );
224
+ // let custom domain in front of protected domain
225
+ const domainAliases = [{ value: domainAlias, isProtected: false }, ...dbSite.domainAliases];
226
+
227
+ const updateResult = await states.site.update({ _id: id }, { $set: { domainAliases } });
228
228
  logger.debug('add domain alias update result', { id, updateResult, domainAlias });
229
229
 
230
230
  const newSite = await states.site.findOne({ _id: id });
@@ -20,7 +20,7 @@ const {
20
20
  const logger = require('@abtnode/logger')('state-blocklet');
21
21
 
22
22
  const BaseState = require('./base');
23
- const { forEachBlocklet } = require('../util/blocklet');
23
+ const { forEachBlocklet, checkDuplicateComponents } = require('../util/blocklet');
24
24
  const { validateBlockletMeta } = require('../util');
25
25
 
26
26
  const lock = new Lock('blocklet-port-assign-lock');
@@ -391,13 +391,20 @@ class BlockletState extends BaseState {
391
391
  return result;
392
392
  }
393
393
 
394
- async setBlockletStatus(did, status) {
394
+ /**
395
+ * @param {String} did blocklet did
396
+ * @param {BlockletStatus} status blocklet status
397
+ *
398
+ * children status only different with parent before blocklet installation
399
+ * @param {Array<{did}>} children
400
+ */
401
+ async setBlockletStatus(did, status, { children } = {}) {
395
402
  if (typeof status === 'undefined') {
396
403
  throw new Error('Unsupported blocklet status');
397
404
  }
398
405
 
399
406
  const doc = await this.getBlocklet(did);
400
- if (doc.status === status) {
407
+ if (doc.status === status && !children) {
401
408
  return formatBlocklet(doc, 'onRead', this.options.dek);
402
409
  }
403
410
 
@@ -412,6 +419,28 @@ class BlockletState extends BaseState {
412
419
  updates.stoppedAt = new Date();
413
420
  }
414
421
 
422
+ // update children status
423
+ updates.children = doc.children.map((child) => {
424
+ if (children === 'all') {
425
+ child.status = status;
426
+ return child;
427
+ }
428
+
429
+ if (!children) {
430
+ if (![BlockletStatus.waiting, BlockletStatus.upgrading, BlockletStatus.installing].includes(status)) {
431
+ child.status = status;
432
+ }
433
+
434
+ return child;
435
+ }
436
+
437
+ const inputChild = children.find((x) => x.did === child.meta.did);
438
+ if (inputChild) {
439
+ child.status = status;
440
+ }
441
+ return child;
442
+ });
443
+
415
444
  await this.update(doc._id, { $set: updates });
416
445
  return formatBlocklet({ ...doc, ...updates }, 'onRead', this.options.dek);
417
446
  }
@@ -463,6 +492,48 @@ class BlockletState extends BaseState {
463
492
 
464
493
  return children;
465
494
  }
495
+
496
+ async addChildren(did, children) {
497
+ const parent = await this.getBlocklet(did);
498
+ if (!parent) {
499
+ throw new Error('Blocklet does not exist');
500
+ }
501
+
502
+ const oldChildren = parent.children || [];
503
+
504
+ const newChildren = [...oldChildren];
505
+ for (const child of children) {
506
+ const { meta, mountPoint, sourceUrl = '', source = '', deployedFrom = '' } = child;
507
+
508
+ if (!mountPoint) {
509
+ throw new Error(`mountPoint is required when adding component ${meta.title || meta.name}`);
510
+ }
511
+
512
+ if (meta.did === parent.meta.did) {
513
+ throw new Error('Cannot add self as a component');
514
+ }
515
+
516
+ checkDuplicateComponents([child], newChildren);
517
+
518
+ newChildren.push({
519
+ mountPoint,
520
+ meta,
521
+ sourceUrl,
522
+ source,
523
+ deployedFrom,
524
+ dynamic: true,
525
+ status: BlockletStatus.added,
526
+ });
527
+ }
528
+
529
+ // use upgradeBlocklet to assign ports to children and write new data to db
530
+ return this.upgradeBlocklet({
531
+ meta: parent.meta,
532
+ source: parent.source,
533
+ deployedFrom: parent.deployedFrom,
534
+ children: newChildren,
535
+ });
536
+ }
466
537
  }
467
538
 
468
539
  BlockletState.BlockletStatus = BlockletStatus;
@@ -1101,6 +1101,22 @@ const getDiffFiles = async (inputFiles, sourceDir) => {
1101
1101
 
1102
1102
  const getBundleDir = (installDir, bundle) => path.join(installDir, bundle.name, bundle.version);
1103
1103
 
1104
+ const needBlockletDownload = (blocklet, oldBlocklet) => {
1105
+ if ([BlockletSource.upload, BlockletSource.local, BlockletSource.custom].includes(blocklet.source)) {
1106
+ return false;
1107
+ }
1108
+
1109
+ if (!get(oldBlocklet, 'meta.dist.integrity')) {
1110
+ return true;
1111
+ }
1112
+
1113
+ if (get(oldBlocklet, 'meta.dist.integrity') === get(blocklet, 'meta.dist.integrity')) {
1114
+ return false;
1115
+ }
1116
+
1117
+ return true;
1118
+ };
1119
+
1104
1120
  module.exports = {
1105
1121
  forEachBlocklet,
1106
1122
  getBlockletMetaFromUrl,
@@ -1135,4 +1151,5 @@ module.exports = {
1135
1151
  checkDuplicateComponents,
1136
1152
  getDiffFiles,
1137
1153
  getBundleDir,
1154
+ needBlockletDownload,
1138
1155
  };
@@ -27,9 +27,6 @@ const waitUpdaterRpc = async (message) =>
27
27
  const checkNewVersion = async (params, context) => {
28
28
  try {
29
29
  const info = await states.node.read();
30
- if (!info.autoUpgrade) {
31
- return '';
32
- }
33
30
 
34
31
  if (!process.env.ABT_NODE_PACKAGE_NAME) {
35
32
  logger.error('ABT_NODE_PACKAGE_NAME name was not found in environment');
@@ -176,7 +173,14 @@ const getCron = () => ({
176
173
  name: 'check-update',
177
174
  time: '0 0 8 * * *', // check every day
178
175
  // time: '0 */5 * * * *', // check every 5 minutes
179
- fn: checkNewVersion,
176
+ fn: async () => {
177
+ const info = await states.node.read();
178
+ if (!info.autoUpgrade) {
179
+ return;
180
+ }
181
+
182
+ checkNewVersion();
183
+ },
180
184
  options: { runOnInit: false },
181
185
  });
182
186
 
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.6.18",
6
+ "version": "1.6.19",
7
7
  "description": "",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,30 +19,30 @@
19
19
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "@abtnode/certificate-manager": "1.6.18",
23
- "@abtnode/constant": "1.6.18",
24
- "@abtnode/cron": "1.6.18",
25
- "@abtnode/db": "1.6.18",
26
- "@abtnode/logger": "1.6.18",
27
- "@abtnode/queue": "1.6.18",
28
- "@abtnode/rbac": "1.6.18",
29
- "@abtnode/router-provider": "1.6.18",
30
- "@abtnode/static-server": "1.6.18",
31
- "@abtnode/timemachine": "1.6.18",
32
- "@abtnode/util": "1.6.18",
33
- "@arcblock/did": "^1.14.8",
34
- "@arcblock/event-hub": "1.14.8",
22
+ "@abtnode/certificate-manager": "1.6.19",
23
+ "@abtnode/constant": "1.6.19",
24
+ "@abtnode/cron": "1.6.19",
25
+ "@abtnode/db": "1.6.19",
26
+ "@abtnode/logger": "1.6.19",
27
+ "@abtnode/queue": "1.6.19",
28
+ "@abtnode/rbac": "1.6.19",
29
+ "@abtnode/router-provider": "1.6.19",
30
+ "@abtnode/static-server": "1.6.19",
31
+ "@abtnode/timemachine": "1.6.19",
32
+ "@abtnode/util": "1.6.19",
33
+ "@arcblock/did": "^1.14.11",
34
+ "@arcblock/event-hub": "1.14.11",
35
35
  "@arcblock/pm2-events": "^0.0.5",
36
- "@arcblock/vc": "^1.14.8",
37
- "@blocklet/meta": "1.6.18",
36
+ "@arcblock/vc": "^1.14.11",
37
+ "@blocklet/meta": "1.6.19",
38
38
  "@fidm/x509": "^1.2.1",
39
39
  "@nedb/core": "^1.2.2",
40
40
  "@nedb/multi": "^1.2.2",
41
- "@ocap/mcrypto": "^1.14.8",
42
- "@ocap/util": "^1.14.8",
43
- "@ocap/wallet": "^1.14.8",
41
+ "@ocap/mcrypto": "^1.14.11",
42
+ "@ocap/util": "^1.14.11",
43
+ "@ocap/wallet": "^1.14.11",
44
44
  "@slack/webhook": "^5.0.3",
45
- "axios": "^0.21.4",
45
+ "axios": "^0.25.0",
46
46
  "axon": "^2.0.3",
47
47
  "chalk": "^4.0.0",
48
48
  "deep-diff": "^1.0.2",
@@ -53,7 +53,7 @@
53
53
  "is-base64": "^1.1.0",
54
54
  "is-ip": "^3.1.0",
55
55
  "is-url": "^1.2.4",
56
- "joi": "^17.5.0",
56
+ "joi": "^17.6.0",
57
57
  "js-yaml": "^3.14.0",
58
58
  "lodash": "^4.17.21",
59
59
  "lru-cache": "^6.0.0",
@@ -75,5 +75,5 @@
75
75
  "express": "^4.17.1",
76
76
  "jest": "^27.4.5"
77
77
  },
78
- "gitHead": "2f02f166869d8ebedc0466068f6ed90ab3e07b87"
78
+ "gitHead": "42a1290f14fca261eccafb05561eecf8683ed66a"
79
79
  }