@nocobase/cli-v1 2.4.0-alpha.8 → 2.4.0-alpha.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/cli-v1",
3
- "version": "2.4.0-alpha.8",
3
+ "version": "2.4.0-alpha.9",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./src/index.js",
@@ -9,9 +9,9 @@
9
9
  "nocobase-v1": "./bin/index.js"
10
10
  },
11
11
  "dependencies": {
12
- "@nocobase/cli": "2.4.0-alpha.8",
12
+ "@nocobase/cli": "2.4.0-alpha.9",
13
13
  "@nocobase/license-kit": "^0.3.8",
14
- "@nocobase/utils": "2.4.0-alpha.8",
14
+ "@nocobase/utils": "2.4.0-alpha.9",
15
15
  "chalk": "^4.1.1",
16
16
  "commander": "^9.2.0",
17
17
  "deepmerge": "^4.3.1",
@@ -20,7 +20,7 @@
20
20
  "fast-glob": "^3.3.1",
21
21
  "fs-extra": "^11.1.1",
22
22
  "p-all": "3.0.0",
23
- "pm2": "^6.0.5",
23
+ "pm2": "^7.0.0",
24
24
  "portfinder": "^1.0.28",
25
25
  "tar": "^7.4.3",
26
26
  "tree-kill": "^1.2.2"
@@ -33,5 +33,5 @@
33
33
  "url": "git+https://github.com/nocobase/nocobase.git",
34
34
  "directory": "packages/core/cli"
35
35
  },
36
- "gitHead": "e8ec2bff07f4b405e08f984c9e92b44491bde462"
36
+ "gitHead": "d383bdb224c62d1ba21c3dffd682165f3385a11b"
37
37
  }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ /* eslint-env jest */
11
+ /* global vi */
12
+
13
+ const { putWithRetry, OSS_TIMEOUT, OSS_MAX_ATTEMPTS } = require('../commands/client')._test;
14
+
15
+ describe('cli-v1 client:upload', () => {
16
+ let warn;
17
+
18
+ beforeEach(() => {
19
+ warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
20
+ });
21
+
22
+ afterEach(() => {
23
+ warn.mockRestore();
24
+ });
25
+
26
+ it('should raise the ali-oss response timeout above its 60s default', () => {
27
+ expect(OSS_TIMEOUT).toBeGreaterThan(60000);
28
+ });
29
+
30
+ it('should return the result without retrying when the upload succeeds', async () => {
31
+ const put = vi.fn().mockResolvedValue({ name: 'a.js' });
32
+
33
+ await expect(putWithRetry({ put }, 'a.js', '/tmp/a.js')).resolves.toEqual({ name: 'a.js' });
34
+
35
+ expect(put).toHaveBeenCalledTimes(1);
36
+ expect(put).toHaveBeenCalledWith('a.js', '/tmp/a.js');
37
+ expect(warn).not.toHaveBeenCalled();
38
+ });
39
+
40
+ it('should retry a transient timeout and resolve', async () => {
41
+ const put = vi
42
+ .fn()
43
+ .mockRejectedValueOnce(new Error('Response timeout for 60000ms'))
44
+ .mockResolvedValue({ name: 'a.js' });
45
+
46
+ await expect(putWithRetry({ put }, 'a.js', '/tmp/a.js')).resolves.toEqual({ name: 'a.js' });
47
+
48
+ expect(put).toHaveBeenCalledTimes(2);
49
+ expect(warn).toHaveBeenCalledTimes(1);
50
+ });
51
+
52
+ it('should give up and rethrow after the attempt limit', async () => {
53
+ const error = new Error('Response timeout for 60000ms');
54
+ const put = vi.fn().mockRejectedValue(error);
55
+
56
+ await expect(putWithRetry({ put }, 'a.js', '/tmp/a.js')).rejects.toThrow(error);
57
+
58
+ expect(put).toHaveBeenCalledTimes(OSS_MAX_ATTEMPTS);
59
+ });
60
+ });
@@ -156,6 +156,31 @@ async function writeActiveVersion(version) {
156
156
  return activeVersionFile;
157
157
  }
158
158
 
159
+ // 客户端资源里有体积较大的 bundle,ali-oss 默认的 60s 响应超时在 CI 上不够用。
160
+ const OSS_TIMEOUT = 300000;
161
+ // 单个文件偶发超时不应该让整次上传前功尽弃,做有限重试。
162
+ const OSS_MAX_ATTEMPTS = 3;
163
+
164
+ /**
165
+ * 上传单个文件,对偶发失败重试
166
+ * @param {Client} client - OSS 客户端实例
167
+ * @param {string} ossKey - OSS 对象名
168
+ * @param {string} filePath - 本地文件路径
169
+ */
170
+ async function putWithRetry(client, ossKey, filePath) {
171
+ for (let attempt = 1; ; attempt++) {
172
+ try {
173
+ return await client.put(ossKey, filePath);
174
+ } catch (error) {
175
+ if (attempt >= OSS_MAX_ATTEMPTS) {
176
+ throw error;
177
+ }
178
+ console.warn(chalk.yellow(`Retrying ${ossKey} (${attempt}/${OSS_MAX_ATTEMPTS - 1}): ${error.message}`));
179
+ await new Promise((_resolve) => setTimeout(_resolve, attempt * 2000));
180
+ }
181
+ }
182
+ }
183
+
159
184
  /**
160
185
  * 递归上传目录到 OSS
161
186
  * @param {Client} client - OSS 客户端实例
@@ -189,7 +214,7 @@ async function uploadDirectoryToOSS(client, localDir, ossPrefix = '') {
189
214
  // 上传文件
190
215
  const ossKey = ossPrefix ? `${ossPrefix}/${file}` : file;
191
216
  try {
192
- await client.put(ossKey, filePath);
217
+ await putWithRetry(client, ossKey, filePath);
193
218
  // console.log(chalk.green(`Uploaded: ${ossKey}`));
194
219
  uploadedCount++;
195
220
  } catch (error) {
@@ -273,6 +298,7 @@ module.exports = (cli) => {
273
298
  accessKeySecret: process.env.CDN_ALI_OSS_ACCESS_KEY_SECRET,
274
299
  bucket: process.env.CDN_ALI_OSS_BUCKET,
275
300
  region: process.env.CDN_ALI_OSS_REGION,
301
+ timeout: OSS_TIMEOUT,
276
302
  });
277
303
 
278
304
  if (!(await fs.exists(target))) {
@@ -294,3 +320,9 @@ module.exports = (cli) => {
294
320
  };
295
321
 
296
322
  module.exports.renderExtractedClientIndexHtml = renderExtractedClientIndexHtml;
323
+
324
+ module.exports._test = {
325
+ putWithRetry,
326
+ OSS_TIMEOUT,
327
+ OSS_MAX_ATTEMPTS,
328
+ };