@abtnode/core 1.16.15-beta-ed0db59e → 1.16.15-beta-d464647a

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.
@@ -2172,7 +2172,7 @@ class DiskBlockletManager extends BaseBlockletManager {
2172
2172
  async _onBackupToSpaces({ blocklet, context, backup }) {
2173
2173
  const {
2174
2174
  referrer,
2175
- user: { did: userDid },
2175
+ user: { did: userDid, locale },
2176
2176
  } = context;
2177
2177
  const {
2178
2178
  appDid,
@@ -2180,7 +2180,7 @@ class DiskBlockletManager extends BaseBlockletManager {
2180
2180
  } = blocklet;
2181
2181
 
2182
2182
  try {
2183
- const spacesBackup = new SpacesBackup({ appDid, appPid, event: this, userDid, referrer, locale: 'en', backup });
2183
+ const spacesBackup = new SpacesBackup({ appDid, appPid, event: this, userDid, referrer, locale, backup });
2184
2184
  this.emit(BlockletEvents.backupProgress, {
2185
2185
  appDid,
2186
2186
  meta: { did: appPid },
@@ -27,6 +27,8 @@ const { getAppName, getAppDescription } = require('@blocklet/meta/lib/util');
27
27
 
28
28
  const logger = require('@abtnode/logger')('@abtnode/core:storage:backup');
29
29
 
30
+ const { default: axios } = require('axios');
31
+ const xbytes = require('xbytes');
30
32
  const states = require('../../../states');
31
33
  const { BaseBackup } = require('./base');
32
34
  const { AuditLogBackup } = require('./audit-log');
@@ -36,6 +38,7 @@ const { BlockletsBackup } = require('./blocklets');
36
38
  const { DataBackup } = require('./data');
37
39
  const { RoutingRuleBackup } = require('./routing-rule');
38
40
  const { translate } = require('../../../locales');
41
+ const { getFolderSize } = require('../utils/disk');
39
42
 
40
43
  class SpacesBackup extends BaseBackup {
41
44
  /**
@@ -126,6 +129,7 @@ class SpacesBackup extends BaseBackup {
126
129
  try {
127
130
  await this.initialize();
128
131
  await this.export();
132
+ await this.verifySpace();
129
133
  await this.syncToSpaces();
130
134
  } catch (error) {
131
135
  console.error(error);
@@ -183,6 +187,35 @@ class SpacesBackup extends BaseBackup {
183
187
  });
184
188
  }
185
189
 
190
+ /**
191
+ * @description 验证 Space 的空间是否足够备份所需
192
+ * @memberof SpacesBackup
193
+ */
194
+ async verifySpace() {
195
+ const { headers } = await axios.head(`${this.spaceBackupEndpoint}?withExtras=true`, {
196
+ // @FIXME: 等到 space 的存储优化专项完成后,这里可以给出更小的超时时间
197
+ timeout: 1000 * 120,
198
+ });
199
+
200
+ const [spaceIsFull, spaceFreeCapacity] = [headers['x-space-is-full'] === 'true', +headers['x-space-free-capacity']];
201
+ const backupFolderSize = getFolderSize(this.backupDir);
202
+
203
+ logger.info('verifySpace', {
204
+ spaceIsFull,
205
+ spaceFreeCapacity: xbytes(spaceFreeCapacity),
206
+ backupFolderSize: xbytes(backupFolderSize),
207
+ backupDir: this.backupDir,
208
+ });
209
+
210
+ if (spaceIsFull) {
211
+ throw new Error(translate(this.input.locale, 'backup.space.isFull'));
212
+ }
213
+
214
+ if (spaceFreeCapacity <= backupFolderSize) {
215
+ throw new Error(translate(this.input.locale, 'backup.space.lackOfSpace'));
216
+ }
217
+ }
218
+
186
219
  async syncToSpaces() {
187
220
  /**
188
221
  * @type {import('@abtnode/client').NodeState}
@@ -225,7 +258,7 @@ class SpacesBackup extends BaseBackup {
225
258
  const percent = (data.completed * 100) / data.total;
226
259
  // 0.8 是因为上传文件到 spaces 占进度的 80%,+ 20 是因为需要累加之前的进度
227
260
  const progress = +Math.floor(percent * 0.8).toFixed(2) + 20;
228
- const progressMessage = `Uploading file ${basename(data.key)} (${data.completed}/${data.total})`;
261
+ const progressMessage = `(${data.completed}/${data.total}) Uploading file ${basename(data.key)}`;
229
262
 
230
263
  await states.backup.progress(this.input.backup.id, {
231
264
  progress,
@@ -6,6 +6,11 @@ const logger = require('@abtnode/logger')('@abtnode/core:storage:utils:disk');
6
6
  const backupDirName = '_abtnode/backup';
7
7
  const restoreDirName = 'tmp/restore-disk';
8
8
 
9
+ /**
10
+ * @description
11
+ * @param {string} dataDir
12
+ * @return {any[]}
13
+ */
9
14
  const getBackupList = (dataDir) => {
10
15
  const baseBackupDir = path.join(dataDir, backupDirName);
11
16
  const backupList = [];
@@ -28,6 +33,12 @@ const getBackupList = (dataDir) => {
28
33
  return backupList;
29
34
  };
30
35
 
36
+ /**
37
+ *
38
+ * @param {string} dataDir
39
+ * @param {string} appDid
40
+ * @returns {Promise<boolean>}
41
+ */
31
42
  const removeBackup = async (dataDir, appDid) => {
32
43
  try {
33
44
  const baseBackupDir = path.join(dataDir, backupDirName);
@@ -54,8 +65,36 @@ const getBackupDirs = (serverDir, appDid) => {
54
65
  };
55
66
  };
56
67
 
68
+ /**
69
+ * @description
70
+ * @param {string} folderPath
71
+ * @return {number}
72
+ */
73
+ function getFolderSize(folderPath) {
74
+ let totalSize = 0;
75
+ const stack = [folderPath];
76
+
77
+ while (stack.length) {
78
+ const currentPath = stack.pop();
79
+ const stats = fs.statSync(currentPath);
80
+
81
+ if (stats.isFile()) {
82
+ totalSize += stats.size;
83
+ } else if (stats.isDirectory()) {
84
+ const files = fs.readdirSync(currentPath);
85
+ files.forEach((file) => {
86
+ const filePath = path.join(currentPath, file);
87
+ stack.push(filePath);
88
+ });
89
+ }
90
+ }
91
+
92
+ return totalSize;
93
+ }
94
+
57
95
  module.exports = {
58
96
  getBackupList,
59
97
  removeBackup,
60
98
  getBackupDirs,
99
+ getFolderSize,
61
100
  };
package/lib/locales/en.js CHANGED
@@ -11,6 +11,9 @@ module.exports = flat({
11
11
  forbidden:
12
12
  'You do not have permission to perform the backup, try restoring the application license on DID Spaces or reconnect to DID Spaces and try again',
13
13
  },
14
+ isFull: 'The current Space storage space is full, please expand the space and back up again',
15
+ lackOfSpace:
16
+ 'The current available space in the storage is insufficient. Please expand the space and perform the backup again',
14
17
  },
15
18
  },
16
19
  });
package/lib/locales/zh.js CHANGED
@@ -10,6 +10,8 @@ module.exports = flat({
10
10
  title: '备份到 Spaces 发生错误',
11
11
  forbidden: '你还没有权限执行备份操作,请尝试在 DID Spaces 上恢复应用授权或者重新连接 DID Spaces 后重试',
12
12
  },
13
+ isFull: '当前的空间存储空间已满,请扩展空间并再次备份',
14
+ lackOfSpace: '当前存储空间的可用空间不足。请扩展空间后再次进行备份',
13
15
  },
14
16
  },
15
17
  });
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.15-beta-ed0db59e",
6
+ "version": "1.16.15-beta-d464647a",
7
7
  "description": "",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,19 +19,19 @@
19
19
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
20
20
  "license": "Apache-2.0",
21
21
  "dependencies": {
22
- "@abtnode/analytics": "1.16.15-beta-ed0db59e",
23
- "@abtnode/auth": "1.16.15-beta-ed0db59e",
24
- "@abtnode/certificate-manager": "1.16.15-beta-ed0db59e",
25
- "@abtnode/constant": "1.16.15-beta-ed0db59e",
26
- "@abtnode/cron": "1.16.15-beta-ed0db59e",
27
- "@abtnode/logger": "1.16.15-beta-ed0db59e",
28
- "@abtnode/models": "1.16.15-beta-ed0db59e",
29
- "@abtnode/queue": "1.16.15-beta-ed0db59e",
30
- "@abtnode/rbac": "1.16.15-beta-ed0db59e",
31
- "@abtnode/router-provider": "1.16.15-beta-ed0db59e",
32
- "@abtnode/static-server": "1.16.15-beta-ed0db59e",
33
- "@abtnode/timemachine": "1.16.15-beta-ed0db59e",
34
- "@abtnode/util": "1.16.15-beta-ed0db59e",
22
+ "@abtnode/analytics": "1.16.15-beta-d464647a",
23
+ "@abtnode/auth": "1.16.15-beta-d464647a",
24
+ "@abtnode/certificate-manager": "1.16.15-beta-d464647a",
25
+ "@abtnode/constant": "1.16.15-beta-d464647a",
26
+ "@abtnode/cron": "1.16.15-beta-d464647a",
27
+ "@abtnode/logger": "1.16.15-beta-d464647a",
28
+ "@abtnode/models": "1.16.15-beta-d464647a",
29
+ "@abtnode/queue": "1.16.15-beta-d464647a",
30
+ "@abtnode/rbac": "1.16.15-beta-d464647a",
31
+ "@abtnode/router-provider": "1.16.15-beta-d464647a",
32
+ "@abtnode/static-server": "1.16.15-beta-d464647a",
33
+ "@abtnode/timemachine": "1.16.15-beta-d464647a",
34
+ "@abtnode/util": "1.16.15-beta-d464647a",
35
35
  "@arcblock/did": "1.18.89",
36
36
  "@arcblock/did-auth": "1.18.89",
37
37
  "@arcblock/did-ext": "^1.18.89",
@@ -42,12 +42,12 @@
42
42
  "@arcblock/pm2-events": "^0.0.5",
43
43
  "@arcblock/validator": "^1.18.89",
44
44
  "@arcblock/vc": "1.18.89",
45
- "@blocklet/constant": "1.16.15-beta-ed0db59e",
46
- "@blocklet/env": "1.16.15-beta-ed0db59e",
47
- "@blocklet/meta": "1.16.15-beta-ed0db59e",
48
- "@blocklet/resolver": "1.16.15-beta-ed0db59e",
49
- "@blocklet/sdk": "1.16.15-beta-ed0db59e",
50
- "@did-space/client": "^0.2.163",
45
+ "@blocklet/constant": "1.16.15-beta-d464647a",
46
+ "@blocklet/env": "1.16.15-beta-d464647a",
47
+ "@blocklet/meta": "1.16.15-beta-d464647a",
48
+ "@blocklet/resolver": "1.16.15-beta-d464647a",
49
+ "@blocklet/sdk": "1.16.15-beta-d464647a",
50
+ "@did-space/client": "^0.2.165",
51
51
  "@fidm/x509": "^1.2.1",
52
52
  "@ocap/mcrypto": "1.18.89",
53
53
  "@ocap/util": "1.18.89",
@@ -91,7 +91,8 @@
91
91
  "ua-parser-js": "^1.0.2",
92
92
  "url-join": "^4.0.1",
93
93
  "uuid": "^8.3.2",
94
- "valid-url": "^1.0.9"
94
+ "valid-url": "^1.0.9",
95
+ "xbytes": "^1.8.0"
95
96
  },
96
97
  "devDependencies": {
97
98
  "expand-tilde": "^2.0.2",
@@ -99,5 +100,5 @@
99
100
  "jest": "^27.5.1",
100
101
  "unzipper": "^0.10.11"
101
102
  },
102
- "gitHead": "647ebe45f6214ea5c284c9234f1319b6ff72f915"
103
+ "gitHead": "117b29484cbc36434ebdd635f100eb02a5cf7732"
103
104
  }