@cloudbase/cli 2.7.2 → 2.7.3
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/commands/functions/code-download.js +1 -2
- package/lib/commands/functions/layer/download.js +1 -2
- package/lib/function/code.js +3 -27
- package/lib/function/layer/download.js +3 -22
- package/lib/utils/tools/common.js +69 -0
- package/package.json +1 -1
- package/types/function/code.d.ts +0 -1
- package/types/function/layer/download.d.ts +0 -1
- package/types/utils/tools/common.d.ts +1 -0
|
@@ -102,8 +102,7 @@ let CodeDownload = class CodeDownload extends common_1.Command {
|
|
|
102
102
|
envId,
|
|
103
103
|
functionName: name,
|
|
104
104
|
destPath: destPath,
|
|
105
|
-
codeSecret: codeSecret
|
|
106
|
-
unzip: true
|
|
105
|
+
codeSecret: codeSecret
|
|
107
106
|
});
|
|
108
107
|
loading.succeed(`[${name}] 云函数代码下载成功!`);
|
|
109
108
|
});
|
package/lib/function/code.js
CHANGED
|
@@ -8,45 +8,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
12
|
exports.downloadFunctionCode = void 0;
|
|
16
|
-
const fs_1 = __importDefault(require("fs"));
|
|
17
|
-
const path_1 = __importDefault(require("path"));
|
|
18
|
-
const unzipper_1 = __importDefault(require("unzipper"));
|
|
19
13
|
const utils_1 = require("../utils");
|
|
14
|
+
const common_1 = require("../utils/tools/common");
|
|
20
15
|
const scfService = utils_1.CloudApiService.getInstance('scf');
|
|
21
16
|
function downloadFunctionCode(options) {
|
|
22
17
|
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
-
const { destPath, envId, functionName, codeSecret
|
|
18
|
+
const { destPath, envId, functionName, codeSecret } = options;
|
|
24
19
|
(0, utils_1.checkFullAccess)(destPath, true);
|
|
25
20
|
const { Url } = yield scfService.request('GetFunctionAddress', {
|
|
26
21
|
FunctionName: functionName,
|
|
27
22
|
Namespace: envId,
|
|
28
23
|
CodeSecret: codeSecret
|
|
29
24
|
});
|
|
30
|
-
|
|
31
|
-
const zipPath = path_1.default.join(destPath, `${functionName}.zip`);
|
|
32
|
-
const dest = fs_1.default.createWriteStream(zipPath);
|
|
33
|
-
res.body.pipe(dest);
|
|
34
|
-
return new Promise(resolve => {
|
|
35
|
-
dest.on('close', () => {
|
|
36
|
-
if (!unzip) {
|
|
37
|
-
resolve();
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
const unzipStream = unzipper_1.default.Extract({
|
|
41
|
-
path: destPath
|
|
42
|
-
});
|
|
43
|
-
fs_1.default.createReadStream(zipPath).pipe(unzipStream);
|
|
44
|
-
unzipStream.on('close', () => {
|
|
45
|
-
(0, utils_1.delSync)([zipPath]);
|
|
46
|
-
resolve();
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|
|
25
|
+
return (0, common_1.downloadAndExtractRemoteZip)(Url, destPath);
|
|
50
26
|
});
|
|
51
27
|
}
|
|
52
28
|
exports.downloadFunctionCode = downloadFunctionCode;
|
|
@@ -13,15 +13,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.downloadLayer = void 0;
|
|
16
|
-
const fs_1 = __importDefault(require("fs"));
|
|
17
16
|
const path_1 = __importDefault(require("path"));
|
|
18
|
-
const unzipper_1 = __importDefault(require("unzipper"));
|
|
19
17
|
const utils_1 = require("../../utils");
|
|
20
18
|
const error_1 = require("../../error");
|
|
19
|
+
const common_1 = require("../../utils/tools/common");
|
|
21
20
|
const scfService = new utils_1.CloudApiService('scf');
|
|
22
21
|
function downloadLayer(options) {
|
|
23
22
|
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
-
const { name, version, destPath
|
|
23
|
+
const { name, version, destPath } = options;
|
|
25
24
|
const res = yield scfService.request('GetLayerVersion', {
|
|
26
25
|
LayerName: name,
|
|
27
26
|
LayerVersion: version
|
|
@@ -31,25 +30,7 @@ function downloadLayer(options) {
|
|
|
31
30
|
if ((0, utils_1.checkFullAccess)(zipPath)) {
|
|
32
31
|
throw new error_1.CloudBaseError(`文件已存在:${zipPath}`);
|
|
33
32
|
}
|
|
34
|
-
|
|
35
|
-
const dest = fs_1.default.createWriteStream(zipPath);
|
|
36
|
-
streamRes.body.pipe(dest);
|
|
37
|
-
return new Promise(resolve => {
|
|
38
|
-
dest.on('close', () => {
|
|
39
|
-
if (!unzip) {
|
|
40
|
-
resolve();
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
const unzipStream = unzipper_1.default.Extract({
|
|
44
|
-
path: destPath
|
|
45
|
-
});
|
|
46
|
-
fs_1.default.createReadStream(zipPath).pipe(unzipStream);
|
|
47
|
-
unzipStream.on('close', () => {
|
|
48
|
-
(0, utils_1.delSync)([zipPath]);
|
|
49
|
-
resolve();
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
});
|
|
33
|
+
return (0, common_1.downloadAndExtractRemoteZip)(url, destPath);
|
|
53
34
|
});
|
|
54
35
|
}
|
|
55
36
|
exports.downloadLayer = downloadLayer;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.downloadAndExtractRemoteZip = void 0;
|
|
16
|
+
const toolbox_1 = require("@cloudbase/toolbox");
|
|
17
|
+
const path_1 = __importDefault(require("path"));
|
|
18
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
19
|
+
const unzipper_1 = __importDefault(require("unzipper"));
|
|
20
|
+
function downloadAndExtractRemoteZip(downloadUrl, targetPath) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const downloadResponse = yield (0, toolbox_1.fetchStream)(downloadUrl);
|
|
23
|
+
if (!downloadResponse.ok) {
|
|
24
|
+
throw new Error(`下载失败,状态码: ${downloadResponse.status}`);
|
|
25
|
+
}
|
|
26
|
+
if (!downloadResponse.body) {
|
|
27
|
+
throw new Error('下载响应中没有数据流');
|
|
28
|
+
}
|
|
29
|
+
const dirPath = path_1.default.resolve(targetPath);
|
|
30
|
+
yield fs_extra_1.default.ensureDir(dirPath);
|
|
31
|
+
yield new Promise((resolve, reject) => {
|
|
32
|
+
let fileCount = 0;
|
|
33
|
+
let extractedCount = 0;
|
|
34
|
+
downloadResponse
|
|
35
|
+
.body.pipe(unzipper_1.default.Parse())
|
|
36
|
+
.on('error', reject)
|
|
37
|
+
.on('entry', (entry) => __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
const filePath = path_1.default.join(dirPath, entry.path);
|
|
39
|
+
try {
|
|
40
|
+
yield fs_extra_1.default.ensureDir(path_1.default.dirname(filePath));
|
|
41
|
+
if (entry.type === 'Directory') {
|
|
42
|
+
yield fs_extra_1.default.ensureDir(filePath);
|
|
43
|
+
extractedCount++;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
fileCount++;
|
|
47
|
+
entry
|
|
48
|
+
.pipe(fs_extra_1.default.createWriteStream(filePath))
|
|
49
|
+
.on('error', reject)
|
|
50
|
+
.on('finish', () => {
|
|
51
|
+
extractedCount++;
|
|
52
|
+
checkCompletion();
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
reject(err);
|
|
58
|
+
}
|
|
59
|
+
}))
|
|
60
|
+
.on('close', checkCompletion);
|
|
61
|
+
function checkCompletion() {
|
|
62
|
+
if (fileCount > 0 && fileCount === extractedCount) {
|
|
63
|
+
resolve('');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
exports.downloadAndExtractRemoteZip = downloadAndExtractRemoteZip;
|
package/package.json
CHANGED
package/types/function/code.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function downloadAndExtractRemoteZip(downloadUrl: string, targetPath: string): Promise<void>;
|