@abtnode/core 1.7.18 → 1.7.21

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.
@@ -9,7 +9,7 @@ const detectPort = require('detect-port');
9
9
  const Lock = require('@abtnode/util/lib/lock');
10
10
  const security = require('@abtnode/util/lib/security');
11
11
  const { fixPerson, fixInterfaces } = require('@blocklet/meta/lib/fix');
12
- const { getDisplayName, forEachBlocklet } = require('@blocklet/meta/lib/util');
12
+ const { getDisplayName, forEachBlocklet, forEachBlockletSync, forEachChildSync } = require('@blocklet/meta/lib/util');
13
13
  const {
14
14
  BlockletStatus,
15
15
  BlockletSource,
@@ -33,35 +33,31 @@ const getExternalPortsFromMeta = (meta) =>
33
33
  (meta.interfaces || []).map((x) => x.port && x.port.external).filter(Boolean);
34
34
 
35
35
  const formatBlocklet = (blocklet, phase, dek) => {
36
- forEachBlocklet(
37
- blocklet,
38
- (b) => {
39
- if (b.meta) {
40
- fixPerson(b.meta);
41
- fixInterfaces(b.meta);
42
- }
36
+ forEachBlockletSync(blocklet, (b) => {
37
+ if (b.meta) {
38
+ fixPerson(b.meta);
39
+ fixInterfaces(b.meta);
40
+ }
43
41
 
44
- b.children = b.children || [];
42
+ b.children = b.children || [];
43
+
44
+ if (!b.environments || !b.meta || !dek) {
45
+ return;
46
+ }
45
47
 
46
- if (!b.environments || !b.meta || !dek) {
48
+ ['BLOCKLET_APP_SK'].forEach((key) => {
49
+ const env = b.environments.find((x) => x.key === key);
50
+ if (!env) {
47
51
  return;
48
52
  }
49
-
50
- ['BLOCKLET_APP_SK'].forEach((key) => {
51
- const env = b.environments.find((x) => x.key === key);
52
- if (!env) {
53
- return;
54
- }
55
- if (phase === 'onUpdate' && isHex(env.value) === true) {
56
- env.value = security.encrypt(env.value, b.meta.did, dek);
57
- }
58
- if (phase === 'onRead' && isHex(env.value) === false) {
59
- env.value = security.decrypt(env.value, b.meta.did, dek);
60
- }
61
- });
62
- },
63
- { sync: true }
64
- );
53
+ if (phase === 'onUpdate' && isHex(env.value) === true) {
54
+ env.value = security.encrypt(env.value, b.meta.did, dek);
55
+ }
56
+ if (phase === 'onRead' && isHex(env.value) === false) {
57
+ env.value = security.decrypt(env.value, b.meta.did, dek);
58
+ }
59
+ });
60
+ });
65
61
 
66
62
  return blocklet;
67
63
  };
@@ -416,7 +412,7 @@ class BlockletState extends BaseState {
416
412
  * @param {BlockletStatus} status blocklet status
417
413
  *
418
414
  * children status only different with parent before blocklet installation
419
- * @param {Array<{did}>} children
415
+ * @param {Array<componentId>} children
420
416
  */
421
417
  async setBlockletStatus(did, status, { children } = {}) {
422
418
  if (typeof status === 'undefined') {
@@ -440,44 +436,45 @@ class BlockletState extends BaseState {
440
436
  }
441
437
 
442
438
  // update children status
443
- updates.children = doc.children.map((child) => {
439
+ forEachChildSync(doc, (child, { id }) => {
444
440
  if (children === 'all') {
445
441
  child.status = status;
446
- return child;
442
+ return;
447
443
  }
448
444
 
449
445
  if (!children) {
450
- if (![BlockletStatus.waiting, BlockletStatus.upgrading, BlockletStatus.installing].includes(status)) {
446
+ if (
447
+ ![
448
+ BlockletStatus.waiting,
449
+ BlockletStatus.upgrading,
450
+ BlockletStatus.installing,
451
+ BlockletStatus.starting,
452
+ ].includes(status)
453
+ ) {
451
454
  child.status = status;
452
455
  }
453
456
 
454
- return child;
457
+ return;
455
458
  }
456
459
 
457
- const inputChild = children.find((x) => x.did === child.meta.did);
458
- if (inputChild) {
460
+ if (children.includes(id)) {
459
461
  child.status = status;
460
462
  }
461
- return child;
462
463
  });
463
464
 
465
+ updates.children = doc.children;
464
466
  return this.updateBlocklet(did, updates);
465
467
  }
466
468
 
467
- async fillChildrenPorts(children, { defaultPort = 0, oldChildren } = {}) {
469
+ async fillChildrenPorts(children, { defaultPort = 0, oldChildren, returnMaxPort } = {}) {
468
470
  let _maxPort = defaultPort;
469
471
  for (const child of children || []) {
470
472
  // generate ports
471
473
  const childMeta = child.meta;
474
+ const oldChild = (oldChildren || []).find((x) => x.meta.did === child.meta.did);
472
475
 
473
476
  // get skipOccupiedCheckPorts
474
- let skipOccupiedCheckPorts = [];
475
- if (Array.isArray(oldChildren)) {
476
- const oldChild = oldChildren.find((x) => x.meta.did === child.meta.did);
477
- if (oldChild) {
478
- skipOccupiedCheckPorts = getExternalPortsFromMeta(oldChild.meta);
479
- }
480
- }
477
+ const skipOccupiedCheckPorts = oldChild ? getExternalPortsFromMeta(oldChild.meta) : [];
481
478
 
482
479
  const ports = await this.getBlockletPorts({
483
480
  interfaces: childMeta.interfaces || [],
@@ -486,22 +483,17 @@ class BlockletState extends BaseState {
486
483
  });
487
484
  _maxPort = getMaxPort(ports);
488
485
 
489
- // fill old child's port to new child
490
- if (Array.isArray(oldChildren)) {
491
- const oldChild = oldChildren.find((x) => x.meta.did === child.meta.did);
492
- if (oldChild && oldChild.ports) {
493
- logger.info('Merge the previous ports to child blocklet', {
494
- did: child.meta.did,
495
- name: child.meta.name,
496
- oldPorts: oldChild.ports,
497
- ports,
498
- });
499
- Object.keys(ports).forEach((p) => {
500
- ports[p] = oldChild.ports[p] || ports[p];
501
- });
502
- child.ports = ports;
503
- continue; // eslint-disable-line
504
- }
486
+ if (oldChild && oldChild.ports) {
487
+ // fill old child's port to new child
488
+ logger.info('Merge the previous ports to child blocklet', {
489
+ did: child.meta.did,
490
+ name: child.meta.name,
491
+ oldPorts: oldChild.ports,
492
+ ports,
493
+ });
494
+ Object.keys(ports).forEach((p) => {
495
+ ports[p] = oldChild.ports[p] || ports[p];
496
+ });
505
497
  }
506
498
 
507
499
  // assign a new port to child
@@ -509,6 +501,20 @@ class BlockletState extends BaseState {
509
501
  child.ports = ports;
510
502
  }
511
503
 
504
+ for (const child of children || []) {
505
+ const oldChild = (oldChildren || []).find((x) => x.meta.did === child.meta.did);
506
+
507
+ _maxPort = await this.fillChildrenPorts(child.children || [], {
508
+ defaultPort: _maxPort,
509
+ oldChildren: oldChild?.children,
510
+ returnMaxPort: true,
511
+ });
512
+ }
513
+
514
+ if (returnMaxPort) {
515
+ return _maxPort;
516
+ }
517
+
512
518
  return children;
513
519
  }
514
520
 
@@ -528,10 +534,6 @@ class BlockletState extends BaseState {
528
534
  throw new Error(`mountPoint is required when adding component ${getDisplayName(child, true)}`);
529
535
  }
530
536
 
531
- if (meta.did === parent.meta.did) {
532
- throw new Error('Cannot add self as a component');
533
- }
534
-
535
537
  checkDuplicateComponents([child, ...newChildren]);
536
538
 
537
539
  newChildren.push({
@@ -543,6 +545,7 @@ class BlockletState extends BaseState {
543
545
  mode,
544
546
  dynamic: true,
545
547
  status: BlockletStatus.added,
548
+ children: child.children,
546
549
  });
547
550
 
548
551
  fixChildren(newChildren);
@@ -1,7 +1,6 @@
1
1
  /* eslint-disable no-underscore-dangle */
2
2
  const semver = require('semver');
3
3
  const omit = require('lodash/omit');
4
- const isEqual = require('lodash/isEqual');
5
4
  const isEmpty = require('lodash/isEmpty');
6
5
  const security = require('@abtnode/util/lib/security');
7
6
  const { isFromPublicKey } = require('@arcblock/did');
@@ -215,32 +214,43 @@ class NodeState extends BaseState {
215
214
  return updateResult;
216
215
  }
217
216
 
218
- async addNodeOwner(owner) {
219
- if (!validateOwner(owner)) {
217
+ async updateNodeOwner({ nodeOwner, ownerNft }) {
218
+ if (!validateOwner(nodeOwner)) {
220
219
  throw new Error('Node owner is invalid');
221
220
  }
222
221
 
223
222
  const doc = await this.read();
224
223
 
225
- if (doc.nodeOwner) {
226
- if (isEqual(owner, doc.nodeOwner)) {
227
- return doc;
228
- }
224
+ const initialized = this.isInitialized({ nodeOwner });
225
+
226
+ if (this.notification && typeof this.notification.setDefaultReceiver === 'function') {
227
+ this.notification.setDefaultReceiver(nodeOwner.did);
228
+ }
229
229
 
230
- throw new Error('Cannot set owner because owner already exists');
230
+ const entities = { nodeOwner, initialized, initializedAt: initialized ? new Date() : null };
231
+ if (ownerNft) {
232
+ entities.ownerNft = ownerNft;
231
233
  }
232
234
 
233
- return this.addOwner(owner, doc);
235
+ const updateResult = await this.update(doc._id, {
236
+ $set: entities,
237
+ });
238
+
239
+ this.emit(EVENTS.NODE_ADDED_OWNER, updateResult);
240
+
241
+ return updateResult;
234
242
  }
235
243
 
236
- async updateNodeOwner(owner) {
237
- if (!validateOwner(owner)) {
238
- throw new Error('Node owner is invalid');
244
+ async updateNftHolder(holder) {
245
+ if (!holder) {
246
+ throw new Error('NFT holder can not be empty');
239
247
  }
240
248
 
241
249
  const doc = await this.read();
242
250
 
243
- return this.addOwner(owner, doc);
251
+ return this.update(doc._id, {
252
+ $set: { 'ownerNft.holder': holder },
253
+ });
244
254
  }
245
255
 
246
256
  async enterMode(mode) {