@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.
- package/lib/api/node.js +20 -0
- package/lib/api/team.js +40 -5
- package/lib/blocklet/extras.js +1 -5
- package/lib/blocklet/hooks.js +9 -9
- package/lib/blocklet/manager/disk.js +284 -215
- package/lib/blocklet/migration.js +1 -1
- package/lib/index.js +5 -2
- package/lib/migrations/1.7.20-blocklet-component.js +41 -0
- package/lib/router/helper.js +1 -1
- package/lib/router/index.js +1 -1
- package/lib/router/manager.js +24 -21
- package/lib/states/audit-log.js +26 -4
- package/lib/states/blocklet-extras.js +66 -159
- package/lib/states/blocklet.js +65 -62
- package/lib/states/node.js +23 -13
- package/lib/util/blocklet.js +160 -127
- package/lib/util/index.js +29 -0
- package/lib/validators/router.js +1 -2
- package/package.json +23 -22
package/lib/states/blocklet.js
CHANGED
|
@@ -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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
42
|
+
b.children = b.children || [];
|
|
43
|
+
|
|
44
|
+
if (!b.environments || !b.meta || !dek) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
45
47
|
|
|
46
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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<
|
|
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
|
-
|
|
439
|
+
forEachChildSync(doc, (child, { id }) => {
|
|
444
440
|
if (children === 'all') {
|
|
445
441
|
child.status = status;
|
|
446
|
-
return
|
|
442
|
+
return;
|
|
447
443
|
}
|
|
448
444
|
|
|
449
445
|
if (!children) {
|
|
450
|
-
if (
|
|
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
|
|
457
|
+
return;
|
|
455
458
|
}
|
|
456
459
|
|
|
457
|
-
|
|
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
|
-
|
|
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
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
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);
|
package/lib/states/node.js
CHANGED
|
@@ -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
|
|
219
|
-
if (!validateOwner(
|
|
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
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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
|
-
|
|
230
|
+
const entities = { nodeOwner, initialized, initializedAt: initialized ? new Date() : null };
|
|
231
|
+
if (ownerNft) {
|
|
232
|
+
entities.ownerNft = ownerNft;
|
|
231
233
|
}
|
|
232
234
|
|
|
233
|
-
|
|
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
|
|
237
|
-
if (!
|
|
238
|
-
throw new Error('
|
|
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.
|
|
251
|
+
return this.update(doc._id, {
|
|
252
|
+
$set: { 'ownerNft.holder': holder },
|
|
253
|
+
});
|
|
244
254
|
}
|
|
245
255
|
|
|
246
256
|
async enterMode(mode) {
|