@abtnode/core 1.7.8 → 1.7.9

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/team.js CHANGED
@@ -104,6 +104,19 @@ class TeamAPI extends EventEmitter {
104
104
  return state.getUser(user.did);
105
105
  }
106
106
 
107
+ async getOwner({ teamDid }) {
108
+ const owner = await this.teamManager.getOwner(teamDid);
109
+
110
+ if (!owner) {
111
+ return null;
112
+ }
113
+
114
+ const state = await this.getUserState(teamDid);
115
+
116
+ const full = await state.getUser(owner.did);
117
+ return full || owner;
118
+ }
119
+
107
120
  async updateUser({ teamDid, user }) {
108
121
  const state = await this.getUserState(teamDid);
109
122
 
@@ -114,6 +114,7 @@ const {
114
114
  shouldUpdateBlockletStatus,
115
115
  getBlockletMeta,
116
116
  validateBlockletMeta,
117
+ validateOwner,
117
118
  } = util;
118
119
 
119
120
  const preDownloadLock = new Lock('pre-download-lock');
@@ -256,7 +257,7 @@ class BlockletManager extends BaseBlockletManager {
256
257
  }
257
258
 
258
259
  const blocklet = await states.blocklet.getBlocklet(meta.did);
259
- const isRunning = blocklet?.status === BlockletStatus.running;
260
+ const isRunning = blocklet ? blocklet.status === BlockletStatus.running : false;
260
261
 
261
262
  return { meta, isFree, isInstalled: !!blocklet, isRunning };
262
263
  }
@@ -1147,9 +1148,14 @@ class BlockletManager extends BaseBlockletManager {
1147
1148
  return blocklet;
1148
1149
  }
1149
1150
 
1150
- async setInitialized({ did }) {
1151
+ async setInitialized({ did, owner }) {
1152
+ if (!validateOwner(owner)) {
1153
+ throw new Error('Blocklet owner is invalid');
1154
+ }
1155
+
1151
1156
  const blocklet = await states.blocklet.getBlocklet(did);
1152
- await states.blockletExtras.setSettings(blocklet.meta.did, { initialized: true });
1157
+ await states.blockletExtras.setSettings(blocklet.meta.did, { initialized: true, owner });
1158
+ logger.info('Blocklet initialized', { did, owner });
1153
1159
 
1154
1160
  this.emit(BlockletEvents.updated, { meta: { did: blocklet.meta.did } });
1155
1161
 
@@ -21,6 +21,7 @@ async function runScripts({
21
21
  printSuccess,
22
22
  printError,
23
23
  }) {
24
+ // 获取所有待执行的脚本
24
25
  let scripts = [];
25
26
  try {
26
27
  scripts = await getScripts(scriptsDir);
@@ -37,13 +38,17 @@ async function runScripts({
37
38
  }
38
39
 
39
40
  printInfo('pending scripts', pendingScripts);
41
+ // 执行脚本前需要对数据库文件做一次备份
40
42
  try {
41
43
  await doBackup({ dbDir, backupDir, printInfo, printSuccess, printError });
42
44
  } catch (err) {
43
45
  printError(`Failed to backup state db due to ${err.message}, abort!`);
46
+ // 备份失败时,需要移除备份目录
47
+ fs.removeSync(backupDir);
44
48
  throw err;
45
49
  }
46
50
 
51
+ // 按照顺序依次执行 migration 脚本
47
52
  for (let i = 0; i < pendingScripts.length; i++) {
48
53
  const { script: scriptPath } = pendingScripts[i];
49
54
  try {
@@ -59,7 +64,10 @@ async function runScripts({
59
64
 
60
65
  try {
61
66
  await doRestore({ dbDir, backupDir, printInfo, printSuccess, printError });
67
+ // 恢复备份成功时,需要移除备份目录
68
+ fs.removeSync(backupDir);
62
69
  } catch (restoreErr) {
70
+ // 如果恢复备份失败,则保留备份目录,恢复数据
63
71
  printError(`Failed to restore state db due to: ${restoreErr.message}`);
64
72
  }
65
73
 
package/lib/event.js CHANGED
@@ -125,6 +125,14 @@ module.exports = ({
125
125
  await teamAPI.refreshBlockletInterfacePermissions(blocklet.meta);
126
126
  } catch (error) {
127
127
  logger.error('upgrade blocklet routing rules error', { event: name, error });
128
+ notificationState.create({
129
+ title: 'Blocklet URL Mapping Error',
130
+ // eslint-disable-next-line max-len
131
+ description: `Failed to upgrade URL mapping for blocklet ${blocklet.meta.name}@${blocklet.meta.version}, due to: ${error.message}`,
132
+ entityType: 'blocklet',
133
+ entityId: blocklet.meta.did,
134
+ severity: 'error',
135
+ });
128
136
  }
129
137
 
130
138
  try {
package/lib/index.js CHANGED
@@ -265,6 +265,7 @@ function ABTNode(options) {
265
265
  getUsers: teamAPI.getUsers.bind(teamAPI),
266
266
  getUsersCount: teamAPI.getUsersCount.bind(teamAPI),
267
267
  getUser: teamAPI.getUser.bind(teamAPI),
268
+ getOwner: teamAPI.getOwner.bind(teamAPI),
268
269
  getNodeUsers: () => teamAPI.getUsers({ teamDid: options.nodeDid }),
269
270
  getNodeUser: (user) => teamAPI.getUser({ teamDid: options.nodeDid, user }),
270
271
  addUser: teamAPI.addUser.bind(teamAPI),
@@ -863,88 +863,6 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
863
863
  return changes.some(Boolean);
864
864
  };
865
865
 
866
- /**
867
- * Add system routing rules for blocklet in dashboard site
868
- *
869
- * @returns {boolean} if routing changed
870
- * TODO: do we still need this?
871
- */
872
- const _ensureBlockletRulesForDashboardSite = async (blocklet, sites, nodeInfo, context = {}) => {
873
- // blocklet level duplication detection
874
- const findRulesByInterface = (site, value) =>
875
- site.rules.filter((x) => x.isProtected && x.to.did === blocklet.meta.did && get(x, 'to.interfaceName') === value);
876
-
877
- const ipSite = sites.find((x) => x.domain === DOMAIN_FOR_IP_SITE);
878
- if (!ipSite) {
879
- logger.warn(`Routing rule for dashboard not found, abort on ensure rules for blocklet ${blocklet.meta.did}`);
880
- return false;
881
- }
882
-
883
- // If we have rule for legacy path prefix, just return
884
- const pathPrefixLegacy = normalizePathPrefix(`/${nodeInfo.routing.adminPath}/${blocklet.meta.name}`);
885
- if (hasRuleByPrefix(ipSite, pathPrefixLegacy)) {
886
- return false;
887
- }
888
-
889
- const removedRuleIds = [];
890
- const changes = await Promise.all(
891
- blocklet.meta.interfaces.map(async (x) => {
892
- let changed = false; // if state db was mutated
893
- let pathPrefix = '';
894
- if (x.prefix === BLOCKLET_DYNAMIC_PATH_PREFIX) {
895
- // pathPrefix for dynamic path: `/{adminPath}/{blockletName}/{interfaceName}`
896
- pathPrefix = normalizePathPrefix(`/${pathPrefixLegacy}/${x.name}`);
897
- } else {
898
- // pathPrefix for fixed path: `/{interfacePrefix}`
899
- pathPrefix = normalizePathPrefix(x.prefix);
900
- }
901
-
902
- // Remove if we have rule with same interface
903
- const exists = findRulesByInterface(ipSite, x.name);
904
- if (exists.length) {
905
- // Note: we need to keep a list of removed rules since we might remove multiple rules in the loop
906
- exists.forEach((e) => removedRuleIds.push(e.id));
907
- await states.site.update(
908
- { _id: ipSite.id },
909
- { $set: { rules: ipSite.rules.filter((r) => removedRuleIds.includes(r.id) === false) } }
910
- );
911
- changed = true;
912
- } else if (hasRuleByPrefix(ipSite, pathPrefix)) {
913
- // Skip same pathPrefix rules
914
- return changed;
915
- }
916
-
917
- // 不在 dashboard site 中添加 path 和 prefix 都是根路径(/) 的路由
918
- if (x.path === '/' && x.prefix === '/') {
919
- return changed;
920
- }
921
-
922
- await routerManager.addRoutingRule(
923
- {
924
- id: ipSite.id,
925
- rule: {
926
- from: { pathPrefix },
927
- to: {
928
- type: ROUTING_RULE_TYPES.BLOCKLET,
929
- port: findInterfacePortByName(blocklet, x.name),
930
- did: blocklet.meta.did, // root blocklet did
931
- interfaceName: x.name, // root blocklet interface
932
- },
933
- isProtected: true,
934
- },
935
- skipCheckDynamicBlacklist: true,
936
- },
937
- context
938
- );
939
- changed = true;
940
-
941
- return changed;
942
- })
943
- );
944
-
945
- return changes.some(Boolean);
946
- };
947
-
948
866
  const _removeBlockletSites = async (blocklet, nodeInfo, context = {}) => {
949
867
  let changed = false;
950
868
 
@@ -980,14 +898,6 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
980
898
  _ensureBlockletSites(blocklet, sites, nodeInfo, context),
981
899
  ];
982
900
 
983
- if (nodeInfo.mode === NODE_MODES.DEBUG || ['e2e', 'test'].includes(process.env.NODE_ENV)) {
984
- logger.info('Add blocklet endpoint in dashboard site', {
985
- nodeMode: nodeInfo.mode,
986
- NODE_ENV: process.env.NODE_ENV,
987
- });
988
- tasks.push(_ensureBlockletRulesForDashboardSite(blocklet, sites, nodeInfo, context));
989
- }
990
-
991
901
  const changes = await Promise.all(tasks);
992
902
 
993
903
  return changes.some(Boolean);
@@ -267,6 +267,19 @@ class TeamManager extends EventEmitter {
267
267
  }
268
268
  }
269
269
 
270
+ async getOwner(did) {
271
+ let owner;
272
+ if (this.isNodeTeam(did)) {
273
+ const nodeInfo = await this.states.node.read();
274
+ owner = get(nodeInfo, 'nodeOwner');
275
+ } else {
276
+ const settings = await this.states.blockletExtras.getSettings(did);
277
+ owner = get(settings, 'owner');
278
+ }
279
+
280
+ return owner;
281
+ }
282
+
270
283
  // =======
271
284
  // Private
272
285
  // =======
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.7.8",
6
+ "version": "1.7.9",
7
7
  "description": "",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,29 +19,29 @@
19
19
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "@abtnode/certificate-manager": "1.7.8",
23
- "@abtnode/constant": "1.7.8",
24
- "@abtnode/cron": "1.7.8",
25
- "@abtnode/db": "1.7.8",
26
- "@abtnode/logger": "1.7.8",
27
- "@abtnode/queue": "1.7.8",
28
- "@abtnode/rbac": "1.7.8",
29
- "@abtnode/router-provider": "1.7.8",
30
- "@abtnode/static-server": "1.7.8",
31
- "@abtnode/timemachine": "1.7.8",
32
- "@abtnode/util": "1.7.8",
33
- "@arcblock/did": "^1.16.3",
22
+ "@abtnode/certificate-manager": "1.7.9",
23
+ "@abtnode/constant": "1.7.9",
24
+ "@abtnode/cron": "1.7.9",
25
+ "@abtnode/db": "1.7.9",
26
+ "@abtnode/logger": "1.7.9",
27
+ "@abtnode/queue": "1.7.9",
28
+ "@abtnode/rbac": "1.7.9",
29
+ "@abtnode/router-provider": "1.7.9",
30
+ "@abtnode/static-server": "1.7.9",
31
+ "@abtnode/timemachine": "1.7.9",
32
+ "@abtnode/util": "1.7.9",
33
+ "@arcblock/did": "^1.16.4",
34
34
  "@arcblock/did-motif": "^1.1.5",
35
- "@arcblock/event-hub": "1.16.3",
35
+ "@arcblock/event-hub": "1.16.4",
36
36
  "@arcblock/pm2-events": "^0.0.5",
37
- "@arcblock/vc": "^1.16.3",
38
- "@blocklet/meta": "1.7.8",
37
+ "@arcblock/vc": "^1.16.4",
38
+ "@blocklet/meta": "1.7.9",
39
39
  "@fidm/x509": "^1.2.1",
40
40
  "@nedb/core": "^1.2.2",
41
41
  "@nedb/multi": "^1.2.2",
42
- "@ocap/mcrypto": "^1.16.3",
43
- "@ocap/util": "^1.16.3",
44
- "@ocap/wallet": "^1.16.3",
42
+ "@ocap/mcrypto": "^1.16.4",
43
+ "@ocap/util": "^1.16.4",
44
+ "@ocap/wallet": "^1.16.4",
45
45
  "@slack/webhook": "^5.0.3",
46
46
  "ajv": "^7.0.3",
47
47
  "axios": "^0.26.1",
@@ -78,5 +78,5 @@
78
78
  "express": "^4.17.1",
79
79
  "jest": "^27.4.5"
80
80
  },
81
- "gitHead": "ae75dd20b4750a31dc53c88b75ed1d95c8f15397"
81
+ "gitHead": "285f4fedd41fcb8e1814ce5d8250ac10616e67e0"
82
82
  }