@abtnode/core 1.8.11 → 1.8.14

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.
@@ -533,7 +533,7 @@ class BlockletManager extends BaseBlockletManager {
533
533
  logger.info('blocklet reload successfully', { did });
534
534
 
535
535
  const res = await this.status(did);
536
- this.emit(BlockletEvents.statusUpdated, res);
536
+ this.emit(BlockletEvents.statusChange, res);
537
537
  return res;
538
538
  }
539
539
 
@@ -1370,7 +1370,9 @@ class BlockletManager extends BaseBlockletManager {
1370
1370
  return blocklet;
1371
1371
  }
1372
1372
 
1373
- return states.blocklet.setBlockletStatus(did, BlockletStatus.stopped);
1373
+ const res = await states.blocklet.setBlockletStatus(did, BlockletStatus.stopped);
1374
+ this.emit(BlockletEvents.statusChange, res);
1375
+ return res;
1374
1376
  };
1375
1377
 
1376
1378
  const blocklet = await this.ensureBlocklet(did);
@@ -1384,22 +1386,22 @@ class BlockletManager extends BaseBlockletManager {
1384
1386
  }
1385
1387
  }
1386
1388
 
1389
+ if (!shouldUpdateStatus) {
1390
+ return blocklet;
1391
+ }
1392
+
1387
1393
  try {
1388
1394
  const status = await getBlockletStatusFromProcess(blocklet);
1389
- if (shouldUpdateStatus) {
1390
- if (status === BlockletStatus.stopped) {
1391
- await states.blocklet.stopBlocklet(did);
1392
- }
1393
- return states.blocklet.setBlockletStatus(did, status);
1395
+ if (blocklet.status !== status) {
1396
+ const res = await states.blocklet.setBlockletStatus(did, status);
1397
+ this.emit(BlockletEvents.statusChange, res);
1398
+ return res;
1394
1399
  }
1400
+
1401
+ return blocklet;
1395
1402
  } catch (err) {
1396
- if (shouldUpdateStatus) {
1397
- return fastReturnOnForceSync(blocklet);
1398
- }
1399
- throw err;
1403
+ return fastReturnOnForceSync(blocklet);
1400
1404
  }
1401
-
1402
- return blocklet;
1403
1405
  }
1404
1406
 
1405
1407
  async getBlockletInterfaces({ blocklet, nodeInfo, context }) {
@@ -2301,7 +2303,7 @@ class BlockletManager extends BaseBlockletManager {
2301
2303
  async _syncBlockletStatus() {
2302
2304
  const run = async (blocklet) => {
2303
2305
  try {
2304
- await this.status(blocklet.meta.did, { forceSync: true });
2306
+ await this.status(blocklet.meta.did);
2305
2307
  } catch (err) {
2306
2308
  logger.error('sync blocklet status failed', { error: err });
2307
2309
  }
@@ -39,7 +39,9 @@ const formatBlocklet = (blocklet, phase, dek) => {
39
39
  fixInterfaces(b.meta);
40
40
  }
41
41
 
42
- b.children = b.children || [];
42
+ if (phase === 'onRead') {
43
+ b.children = b.children || [];
44
+ }
43
45
 
44
46
  if (!b.environments || !b.meta || !dek) {
45
47
  return;
@@ -8,7 +8,7 @@ class WebhookState extends BaseState {
8
8
  super(baseDir, { filename: 'webhook.db', ...options });
9
9
  }
10
10
 
11
- async create(info) {
11
+ async create(info, { mock } = {}) {
12
12
  const { type, params } = info;
13
13
  const filterParams = params.map((item) => {
14
14
  const data = {
@@ -26,6 +26,10 @@ class WebhookState extends BaseState {
26
26
 
27
27
  await validateWebhook(data);
28
28
 
29
+ if (mock) {
30
+ return data;
31
+ }
32
+
29
33
  const webhook = await this.asyncDB.insert(data);
30
34
  return webhook;
31
35
  }
@@ -9,8 +9,6 @@ const streamToPromise = require('stream-to-promise');
9
9
  const { Throttle } = require('stream-throttle');
10
10
  const ssri = require('ssri');
11
11
  const diff = require('deep-diff');
12
- const any = require('promise.any');
13
- const joinUrl = require('url-join');
14
12
 
15
13
  const { toHex } = require('@ocap/util');
16
14
  const logger = require('@abtnode/logger')('@abtnode/core:util:blocklet');
@@ -22,7 +20,7 @@ const getFolderSize = require('@abtnode/util/lib/get-folder-size');
22
20
  const normalizePathPrefix = require('@abtnode/util/lib/normalize-path-prefix');
23
21
  const hashFiles = require('@abtnode/util/lib/hash-files');
24
22
  const isPathPrefixEqual = require('@abtnode/util/lib/is-path-prefix-equal');
25
- const { BLOCKLET_MAX_MEM_LIMIT_IN_MB, BLOCKLET_STORE_API_BLOCKLET_PREFIX } = require('@abtnode/constant');
23
+ const { BLOCKLET_MAX_MEM_LIMIT_IN_MB } = require('@abtnode/constant');
26
24
 
27
25
  const {
28
26
  BlockletStatus,
@@ -55,19 +53,17 @@ const {
55
53
  } = require('@blocklet/meta/lib/util');
56
54
  const toBlockletDid = require('@blocklet/meta/lib/did');
57
55
  const { titleSchema, descriptionSchema } = require('@blocklet/meta/lib/schema');
56
+ const {
57
+ getSourceUrlsFromConfig,
58
+ getBlockletMetaFromUrls,
59
+ getBlockletMetaFromUrl,
60
+ } = require('@blocklet/meta/lib/util-meta');
58
61
 
59
62
  const { validate: validateEngine, get: getEngine } = require('../blocklet/manager/engine');
60
63
 
61
64
  const isRequirementsSatisfied = require('./requirement');
62
65
  const { getDidDomainForBlocklet } = require('./get-domain-for-blocklet');
63
- const {
64
- isBeforeInstalled,
65
- expandBundle,
66
- getBlockletMetaByUrl,
67
- validateUrl,
68
- findInterfacePortByName,
69
- validateBlockletMeta,
70
- } = require('./index');
66
+ const { isBeforeInstalled, expandBundle, findInterfacePortByName, validateBlockletMeta } = require('./index');
71
67
 
72
68
  /**
73
69
  * get blocklet engine info, default is node
@@ -353,8 +349,8 @@ const getRuntimeEnvironments = (blocklet, nodeEnvironments, ancestors) => {
353
349
  for (const x of root.children || []) {
354
350
  mountPoints.push({
355
351
  title: x.meta.title,
356
- did: x.meta.did,
357
- name: x.meta.name,
352
+ did: x.meta.bundleDid,
353
+ name: x.meta.bundleName,
358
354
  mountPoint: x.mountPoint || '',
359
355
  });
360
356
  }
@@ -366,6 +362,7 @@ const getRuntimeEnvironments = (blocklet, nodeEnvironments, ancestors) => {
366
362
  ...devEnvironments,
367
363
  BLOCKLET_WEB_PORTS: JSON.stringify(ports),
368
364
  BLOCKLET_MOUNT_POINTS: JSON.stringify(mountPoints),
365
+ BLOCKLET_MODE: blocklet.mode || BLOCKLET_MODES.PRODUCTION,
369
366
  ...nodeEnvironments,
370
367
  ...safeNodeEnvironments,
371
368
  };
@@ -405,45 +402,6 @@ const getHealthyCheckTimeout = (blocklet, { checkHealthImmediately } = {}) => {
405
402
  };
406
403
  };
407
404
 
408
- const getBlockletMetaFromUrl = async (url) => {
409
- const meta = await getBlockletMetaByUrl(url);
410
- delete meta.htmlAst;
411
-
412
- validateBlockletMeta(meta, { ensureDist: true });
413
-
414
- try {
415
- const { href } = new URL(meta.dist.tarball, url);
416
- const tarball = decodeURIComponent(href);
417
-
418
- try {
419
- await validateUrl(tarball, ['application/octet-stream', 'application/x-gzip']);
420
- } catch (error) {
421
- if (!error.message.startsWith('Cannot get content-type')) {
422
- throw error;
423
- }
424
- }
425
- logger.info('resolve tarball url base on meta url', { meta: url, tarball });
426
-
427
- meta.dist.tarball = tarball;
428
- } catch (err) {
429
- const msg = `Invalid blocklet meta: dist.tarball is not a valid url ${err.message}`;
430
- logger.error(msg);
431
- throw new Error(msg);
432
- }
433
-
434
- return meta;
435
- };
436
-
437
- const getBlockletMetaFromUrls = async (urls) => {
438
- try {
439
- const meta = await any(urls.map(getBlockletMetaFromUrl));
440
- return meta;
441
- } catch (err) {
442
- logger.error('failed get blocklet meta', { urls, error: err });
443
- throw new Error('Failed get blocklet meta');
444
- }
445
- };
446
-
447
405
  /**
448
406
  * Start all precesses of a blocklet
449
407
  * @param {*} blocklet should contain env props
@@ -694,36 +652,6 @@ const parseChildren = (children, parentMeta = {}, { dynamic } = {}) => {
694
652
  return children;
695
653
  };
696
654
 
697
- /**
698
- * @param {*} config defined in childrenSchema in blocklet meta schema
699
- */
700
- const getSourceUrlsFromConfig = (config) => {
701
- if (config.source) {
702
- if (config.source.url) {
703
- return [config.source.url].flat();
704
- }
705
-
706
- const { store, version, name } = config.source;
707
- return [store]
708
- .flat()
709
- .map((x) =>
710
- joinUrl(
711
- x,
712
- BLOCKLET_STORE_API_BLOCKLET_PREFIX,
713
- toBlockletDid(name),
714
- !version || version === 'latest' ? '' : version,
715
- 'blocklet.json'
716
- )
717
- );
718
- }
719
-
720
- if (config.resolved) {
721
- return [config.resolved];
722
- }
723
-
724
- throw new Error('Invalid child config');
725
- };
726
-
727
655
  /**
728
656
  * this function has side effect on children
729
657
  */
@@ -751,7 +679,7 @@ const parseChildrenFromMeta = async (src, context = {}) => {
751
679
 
752
680
  let m;
753
681
  try {
754
- m = await getBlockletMetaFromUrls(urls);
682
+ m = await getBlockletMetaFromUrls(urls, { logger });
755
683
  } catch {
756
684
  throw new Error(`Failed get component meta: ${config.title || config.name}`);
757
685
  }
@@ -1297,7 +1225,7 @@ const ensureMeta = (meta, { name, did } = {}) => {
1297
1225
 
1298
1226
  module.exports = {
1299
1227
  forEachBlocklet,
1300
- getBlockletMetaFromUrl,
1228
+ getBlockletMetaFromUrl: (url) => getBlockletMetaFromUrl(url, { logger }),
1301
1229
  parseChildrenFromMeta,
1302
1230
  parseChildren,
1303
1231
  getComponentDirs,
@@ -1333,5 +1261,4 @@ module.exports = {
1333
1261
  needBlockletDownload,
1334
1262
  findAvailableDid,
1335
1263
  ensureMeta,
1336
- getSourceUrlsFromConfig,
1337
1264
  };
package/lib/util/index.js CHANGED
@@ -36,8 +36,6 @@ const DEFAULT_WELLKNOWN_PORT = 8088;
36
36
 
37
37
  const logger = require('@abtnode/logger')('@abtnode/core:util');
38
38
 
39
- const request = require('./request');
40
-
41
39
  const validateOwner = (owner) => {
42
40
  try {
43
41
  return owner && owner.did && owner.pk && isFromPublicKey(owner.did, owner.pk);
@@ -351,66 +349,6 @@ const getBaseUrls = async (node, ips) => {
351
349
  }));
352
350
  };
353
351
 
354
- const getBlockletMetaByUrl = async (url) => {
355
- const { protocol, pathname } = new URL(url);
356
-
357
- if (protocol.startsWith('file')) {
358
- const decoded = decodeURIComponent(pathname);
359
- if (!fs.existsSync(decoded)) {
360
- throw new Error(`File does not exist: ${decoded}`);
361
- }
362
- const d = await fs.promises.readFile(decoded);
363
- const meta = JSON.parse(d);
364
- return meta;
365
- }
366
-
367
- if (protocol.startsWith('http')) {
368
- const { data: meta } = await request({ url, method: 'GET', timeout: 1000 * 20 });
369
- if (Object.prototype.toString.call(meta) !== '[object Object]') {
370
- throw new Error('Url is not valid');
371
- }
372
- return meta;
373
- }
374
-
375
- throw new Error(`Invalid url protocol: ${protocol.replace(/:$/, '')}`);
376
- };
377
-
378
- const validateUrl = async (url, expectedHttpResTypes = ['application/json', 'text/plain']) => {
379
- const parsed = new URL(url);
380
- const { protocol, pathname } = parsed;
381
-
382
- // file
383
- if (protocol.startsWith('file')) {
384
- const decoded = decodeURIComponent(pathname);
385
- if (!fs.existsSync(decoded)) {
386
- throw new Error(`File does not exist: ${decoded}`);
387
- }
388
- return true;
389
- }
390
-
391
- // http(s)
392
- if (protocol.startsWith('http')) {
393
- let res;
394
-
395
- try {
396
- res = await request({ url, method: 'HEAD', timeout: 1000 * 10 });
397
- } catch (err) {
398
- throw new Error(`Cannot get content-type from ${url}: ${err.message}`);
399
- }
400
-
401
- if (
402
- res.headers['content-type'] &&
403
- expectedHttpResTypes.some((x) => res.headers['content-type'].includes(x)) === false
404
- ) {
405
- throw new Error(`Unexpected content-type from ${url}: ${res.headers['content-type']}`);
406
- }
407
-
408
- return true;
409
- }
410
-
411
- throw new Error(`Invalid url protocol: ${protocol.replace(/:$/, '')}`);
412
- };
413
-
414
352
  const expandBundle = (bundlePath, destDir) =>
415
353
  unzipper.Open.file(bundlePath).then((d) => d.extract({ path: destDir, concurrency: 20 }));
416
354
 
@@ -567,8 +505,6 @@ const lib = {
567
505
  fixAndValidateService(meta);
568
506
  return validateMeta(meta, opts);
569
507
  },
570
- getBlockletMetaByUrl,
571
- validateUrl,
572
508
  expandBundle,
573
509
  findInterfaceByName,
574
510
  findInterfacePortByName,
@@ -134,13 +134,14 @@ module.exports = ({ events, dataDirs, instance }) => {
134
134
 
135
135
  const createWebhook = async (webhook) => {
136
136
  try {
137
- const createRes = await webhookState.create(webhook);
138
- await sentTextMessage(createRes, `A ${createRes.type} integration is now *successfully added*`);
137
+ const mockCreateRes = await webhookState.create(webhook, { mock: true });
138
+ await sentTextMessage(mockCreateRes, `A ${mockCreateRes.type} integration is now *successfully added*`);
139
139
 
140
+ const createRes = await webhookState.create(webhook);
140
141
  return createRes;
141
142
  } catch (err) {
142
- logger.error('create webhook error', { err });
143
- throw new Error(err.message);
143
+ logger.error('Failed to create webhook', { err });
144
+ throw new Error(`Failed to create webhook: ${err.message}`);
144
145
  }
145
146
  };
146
147
 
@@ -29,7 +29,7 @@ class SlackSender extends BaseSender {
29
29
  type: 'section',
30
30
  text: {
31
31
  type: 'mrkdwn',
32
- text: `*${nodeInfo.name}*`,
32
+ text: `*${nodeInfo?.name}*`,
33
33
  },
34
34
  },
35
35
  {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.8.11",
6
+ "version": "1.8.14",
7
7
  "description": "",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,32 +19,32 @@
19
19
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "@abtnode/certificate-manager": "1.8.11",
23
- "@abtnode/constant": "1.8.11",
24
- "@abtnode/cron": "1.8.11",
25
- "@abtnode/db": "1.8.11",
26
- "@abtnode/logger": "1.8.11",
27
- "@abtnode/queue": "1.8.11",
28
- "@abtnode/rbac": "1.8.11",
29
- "@abtnode/router-provider": "1.8.11",
30
- "@abtnode/static-server": "1.8.11",
31
- "@abtnode/timemachine": "1.8.11",
32
- "@abtnode/util": "1.8.11",
33
- "@arcblock/did": "1.17.11",
22
+ "@abtnode/certificate-manager": "1.8.14",
23
+ "@abtnode/constant": "1.8.14",
24
+ "@abtnode/cron": "1.8.14",
25
+ "@abtnode/db": "1.8.14",
26
+ "@abtnode/logger": "1.8.14",
27
+ "@abtnode/queue": "1.8.14",
28
+ "@abtnode/rbac": "1.8.14",
29
+ "@abtnode/router-provider": "1.8.14",
30
+ "@abtnode/static-server": "1.8.14",
31
+ "@abtnode/timemachine": "1.8.14",
32
+ "@abtnode/util": "1.8.14",
33
+ "@arcblock/did": "1.17.17",
34
34
  "@arcblock/did-motif": "^1.1.10",
35
- "@arcblock/did-util": "1.17.11",
36
- "@arcblock/event-hub": "1.17.11",
37
- "@arcblock/jwt": "^1.17.11",
35
+ "@arcblock/did-util": "1.17.17",
36
+ "@arcblock/event-hub": "1.17.17",
37
+ "@arcblock/jwt": "^1.17.17",
38
38
  "@arcblock/pm2-events": "^0.0.5",
39
- "@arcblock/vc": "1.17.11",
40
- "@blocklet/meta": "1.8.11",
41
- "@blocklet/sdk": "1.8.11",
39
+ "@arcblock/vc": "1.17.17",
40
+ "@blocklet/meta": "1.8.14",
41
+ "@blocklet/sdk": "1.8.14",
42
42
  "@fidm/x509": "^1.2.1",
43
43
  "@nedb/core": "^1.3.4",
44
44
  "@nedb/multi": "^1.3.4",
45
- "@ocap/mcrypto": "1.17.11",
46
- "@ocap/util": "1.17.11",
47
- "@ocap/wallet": "1.17.11",
45
+ "@ocap/mcrypto": "1.17.17",
46
+ "@ocap/util": "1.17.17",
47
+ "@ocap/wallet": "1.17.17",
48
48
  "@slack/webhook": "^5.0.4",
49
49
  "axios": "^0.27.2",
50
50
  "axon": "^2.0.3",
@@ -62,7 +62,6 @@
62
62
  "lodash": "^4.17.21",
63
63
  "lru-cache": "^6.0.0",
64
64
  "pm2": "^5.2.0",
65
- "promise.any": "^2.0.4",
66
65
  "semver": "^7.3.7",
67
66
  "shelljs": "^0.8.5",
68
67
  "slugify": "^1.6.5",
@@ -82,5 +81,5 @@
82
81
  "express": "^4.18.1",
83
82
  "jest": "^27.5.1"
84
83
  },
85
- "gitHead": "389148562de1ce5b45097e764bc9d1718009e527"
84
+ "gitHead": "4b0cee41fd03d09301e2c35af46dd9e513473ef8"
86
85
  }