@cloudbase/manager-node 4.2.4 → 4.2.6

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.
Files changed (85) hide show
  1. package/lib/access/index.js +141 -0
  2. package/lib/access/types.js +2 -0
  3. package/lib/billing/index.js +36 -0
  4. package/lib/cam/index.js +77 -0
  5. package/lib/cloudBaseRun/index.js +36 -0
  6. package/lib/cloudBaseRun/types.js +2 -0
  7. package/lib/common/index.js +39 -0
  8. package/lib/constant.js +55 -0
  9. package/lib/context.js +14 -0
  10. package/lib/database/index.js +244 -0
  11. package/lib/debug.js +34 -0
  12. package/lib/env/index.js +288 -0
  13. package/lib/environment.js +124 -0
  14. package/lib/environmentManager.js +44 -0
  15. package/lib/error.js +16 -0
  16. package/lib/function/index.js +1019 -0
  17. package/lib/function/packer.js +129 -0
  18. package/lib/function/types.js +2 -0
  19. package/lib/hosting/index.js +461 -0
  20. package/lib/index.js +83 -0
  21. package/lib/interfaces/base.interface.js +2 -0
  22. package/lib/interfaces/billing.interface.js +2 -0
  23. package/lib/interfaces/cam.interface.js +2 -0
  24. package/lib/interfaces/flexdb.interface.js +2 -0
  25. package/lib/interfaces/function.interface.js +2 -0
  26. package/lib/interfaces/index.js +19 -0
  27. package/lib/interfaces/storage.interface.js +2 -0
  28. package/lib/interfaces/tcb.interface.js +2 -0
  29. package/lib/storage/index.js +1051 -0
  30. package/lib/third/index.js +18 -0
  31. package/lib/user/index.js +136 -0
  32. package/lib/user/types.js +2 -0
  33. package/lib/utils/auth.js +97 -0
  34. package/lib/utils/cloud-api-request.js +212 -0
  35. package/lib/utils/cloudbase-request.js +69 -0
  36. package/lib/utils/envLazy.js +18 -0
  37. package/lib/utils/fs.js +64 -0
  38. package/lib/utils/http-request.js +44 -0
  39. package/lib/utils/index.js +103 -0
  40. package/lib/utils/parallel.js +69 -0
  41. package/lib/utils/runenv.js +8 -0
  42. package/lib/utils/uuid.js +18 -0
  43. package/package.json +1 -1
  44. package/types/access/index.d.ts +38 -0
  45. package/types/access/types.d.ts +42 -0
  46. package/types/billing/index.d.ts +21 -0
  47. package/types/cam/index.d.ts +63 -0
  48. package/types/cloudBaseRun/index.d.ts +12 -0
  49. package/types/cloudBaseRun/types.d.ts +21 -0
  50. package/types/common/index.d.ts +18 -0
  51. package/types/constant.d.ts +44 -0
  52. package/types/context.d.ts +17 -0
  53. package/types/database/index.d.ts +66 -0
  54. package/types/debug.d.ts +1 -0
  55. package/types/env/index.d.ts +127 -0
  56. package/types/environment.d.ts +51 -0
  57. package/types/environmentManager.d.ts +13 -0
  58. package/types/error.d.ts +18 -0
  59. package/types/function/index.d.ts +379 -0
  60. package/types/function/packer.d.ts +37 -0
  61. package/types/function/types.d.ts +154 -0
  62. package/types/hosting/index.d.ts +253 -0
  63. package/types/index.d.ts +52 -0
  64. package/types/interfaces/base.interface.d.ts +7 -0
  65. package/types/interfaces/billing.interface.d.ts +18 -0
  66. package/types/interfaces/cam.interface.d.ts +24 -0
  67. package/types/interfaces/flexdb.interface.d.ts +67 -0
  68. package/types/interfaces/function.interface.d.ts +65 -0
  69. package/types/interfaces/index.d.ts +7 -0
  70. package/types/interfaces/storage.interface.d.ts +26 -0
  71. package/types/interfaces/tcb.interface.d.ts +305 -0
  72. package/types/storage/index.d.ts +324 -0
  73. package/types/third/index.d.ts +11 -0
  74. package/types/user/index.d.ts +52 -0
  75. package/types/user/types.d.ts +20 -0
  76. package/types/utils/auth.d.ts +8 -0
  77. package/types/utils/cloud-api-request.d.ts +21 -0
  78. package/types/utils/cloudbase-request.d.ts +14 -0
  79. package/types/utils/envLazy.d.ts +1 -0
  80. package/types/utils/fs.d.ts +7 -0
  81. package/types/utils/http-request.d.ts +2 -0
  82. package/types/utils/index.d.ts +20 -0
  83. package/types/utils/parallel.d.ts +17 -0
  84. package/types/utils/runenv.d.ts +1 -0
  85. package/types/utils/uuid.d.ts +2 -0
@@ -0,0 +1,288 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.EnvService = void 0;
13
+ const cos_nodejs_sdk_v5_1 = __importDefault(require("cos-nodejs-sdk-v5"));
14
+ const util_1 = __importDefault(require("util"));
15
+ const error_1 = require("../error");
16
+ const utils_1 = require("../utils");
17
+ const constant_1 = require("../constant");
18
+ class EnvService {
19
+ constructor(environment) {
20
+ this.environment = environment;
21
+ this.envId = environment.getEnvId();
22
+ this.envType = environment.getEnvType();
23
+ this.cloudService = new utils_1.CloudService(environment.cloudBaseContext, 'tcb', '2018-06-08');
24
+ }
25
+ /**
26
+ * 列出所有环境
27
+ * @returns {Promise<IListEnvRes>}
28
+ */
29
+ async listEnvs() {
30
+ return this.cloudService.request('DescribeEnvs');
31
+ }
32
+ /**
33
+ * 拉取安全域名列表
34
+ * @returns {Promise<IAuthDomainsRes>}
35
+ */
36
+ async getEnvAuthDomains() {
37
+ return this.cloudService.request('DescribeAuthDomains', {
38
+ EnvId: this.envId
39
+ });
40
+ }
41
+ /**
42
+ * 添加环境安全域名
43
+ * @param {string[]} domains 域名字符串数组
44
+ * @returns {Promise<IResponseInfo>}
45
+ */
46
+ async createEnvDomain(domains) {
47
+ const res = await this.cloudService.request('CreateAuthDomain', {
48
+ EnvId: this.envId,
49
+ Domains: domains
50
+ });
51
+ // 添加 COS CORS 域名
52
+ const promises = domains.map(async (domain) => {
53
+ this.modifyCosCorsDomain(domain);
54
+ });
55
+ await Promise.all(promises);
56
+ return res;
57
+ }
58
+ /**
59
+ * 删除环境安全域名
60
+ * @param {string[]} domainIds 域名字符串数组
61
+ * @returns {Promise<IDeleteDomainRes>}
62
+ */
63
+ async deleteEnvDomain(domains) {
64
+ // 根据域名获取域名 Id
65
+ const { Domains } = await this.getEnvAuthDomains();
66
+ const domainIds = Domains.filter(item => domains.includes(item.Domain)).map(item => item.Id);
67
+ const res = await this.cloudService.request('DeleteAuthDomain', {
68
+ EnvId: this.envId,
69
+ DomainIds: domainIds
70
+ });
71
+ // 删除 COS CORS 域名
72
+ const promises = domains.map(async (domain) => {
73
+ this.modifyCosCorsDomain(domain, true);
74
+ });
75
+ await Promise.all(promises);
76
+ return res;
77
+ }
78
+ /**
79
+ * 检查tcb服务是否开通
80
+ * @returns {Promise<ICheckTcbServiceRes>}
81
+ * @memberof CamService
82
+ */
83
+ async checkTcbService() {
84
+ return this.cloudService.request('CheckTcbService', {});
85
+ }
86
+ /**
87
+ * 初始化TCB
88
+ * @returns {Promise<IResponseInfo>}
89
+ * @memberof EnvService
90
+ */
91
+ async initTcb(param) {
92
+ let initParam = {};
93
+ if (param) {
94
+ initParam = Object.assign({}, param);
95
+ }
96
+ return this.cloudService.request('InitTcb', initParam);
97
+ }
98
+ /**
99
+ * 开通后付费套餐
100
+ * @param {string} envId
101
+ * @param {SOURCE} [source]
102
+ * @returns {Promise<ICreatePostpayRes>}
103
+ * @memberof EnvService
104
+ */
105
+ async CreatePostpayPackage(envId, source) {
106
+ const realSource = source ? source : 'qcloud';
107
+ return this.cloudService.request('CreatePostpayPackage', {
108
+ EnvId: envId,
109
+ Source: realSource
110
+ });
111
+ }
112
+ /**
113
+ * 销毁环境
114
+ * @param {string} envId
115
+ * @returns {Promise<IResponseInfo>}
116
+ * @memberof EnvService
117
+ */
118
+ async destroyEnv(envId) {
119
+ return this.cloudService.request('DestroyEnv', {
120
+ EnvId: envId
121
+ });
122
+ }
123
+ /**
124
+ * 获取环境信息
125
+ * @returns {Promise<IEnvInfoRes>}
126
+ */
127
+ async getEnvInfo() {
128
+ // NOTE: DescribeEnv 接口废弃,需要使用 DescribeEnvs 接口
129
+ const params = {
130
+ EnvId: this.envId
131
+ };
132
+ if (this.envType === 'run') {
133
+ params.EnvType = 'run';
134
+ }
135
+ const { EnvList, RequestId } = await this.cloudService.request('DescribeEnvs', params);
136
+ return {
137
+ EnvInfo: (EnvList === null || EnvList === void 0 ? void 0 : EnvList.length) ? EnvList[0] : {},
138
+ RequestId
139
+ };
140
+ }
141
+ /**
142
+ * 修改环境名称
143
+ * @param {string} alias 环境名称
144
+ * @returns {Promise<IResponseInfo>}
145
+ */
146
+ async updateEnvInfo(alias) {
147
+ return this.cloudService.request('ModifyEnv', {
148
+ EnvId: this.envId,
149
+ Alias: alias
150
+ });
151
+ }
152
+ /**
153
+ * 拉取登录配置列表
154
+ * @returns {Promise<IEnvLoginConfigRes>}
155
+ */
156
+ async getLoginConfigList() {
157
+ return this.cloudService.request('DescribeLoginConfigs', {
158
+ EnvId: this.envId
159
+ });
160
+ }
161
+ /**
162
+ * 创建登录方式
163
+ * 'WECHAT-OPEN':微信开放平台
164
+ * 'WECHAT-PUBLIC':微信公众平台
165
+ * @param {('WECHAT-OPEN' | 'WECHAT-PUBLIC')} platform 'WECHAT-OPEN' | 'WECHAT-PUBLIC'
166
+ * @param {string} appId 微信 appId
167
+ * @param {string} appSecret 微信 appSecret
168
+ * @returns {Promise<IResponseInfo>}
169
+ */
170
+ async createLoginConfig(platform, appId, appSecret) {
171
+ let finalAppSecret = appSecret;
172
+ if (platform === 'ANONYMOUS') {
173
+ finalAppSecret = 'anonymous';
174
+ }
175
+ return this.cloudService.request('CreateLoginConfig', {
176
+ EnvId: this.envId,
177
+ // 平台, “QQ" "WECHAT-OPEN" "WECHAT-PUBLIC"
178
+ Platform: platform,
179
+ PlatformId: appId,
180
+ PlatformSecret: finalAppSecret ? utils_1.rsaEncrypt(finalAppSecret) : undefined,
181
+ Status: 'ENABLE'
182
+ });
183
+ }
184
+ /**
185
+ * 更新登录方式配置
186
+ * @param {string} configId 配置 Id,从配置列表中获取
187
+ * @param {string} [status='ENABLE'] 是否启用 'ENABLE', 'DISABLE' ,可选
188
+ * @param {string} [appId=''] 微信 appId,可选
189
+ * @param {string} [appSecret=''] 微信 appSecret,可选
190
+ * @returns {Promise<IResponseInfo>}
191
+ */
192
+ /* eslint-disable-next-line */
193
+ async updateLoginConfig(configId, status = 'ENABLE', appId = '', appSecret = '') {
194
+ const validStatus = ['ENABLE', 'DISABLE'];
195
+ let finalAppSecret = appSecret;
196
+ if (!validStatus.includes(status)) {
197
+ throw new error_1.CloudBaseError(`Invalid status value: ${status}. Only support 'ENABLE', 'DISABLE'`);
198
+ }
199
+ const params = {
200
+ EnvId: this.envId,
201
+ ConfigId: configId,
202
+ Status: status
203
+ };
204
+ if (appId === 'anonymous') {
205
+ finalAppSecret = 'anonymous';
206
+ }
207
+ appId && (params.PlatformId = appId);
208
+ finalAppSecret && (params.PlatformSecret = utils_1.rsaEncrypt(finalAppSecret));
209
+ return this.cloudService.request('UpdateLoginConfig', params);
210
+ }
211
+ // 创建自定义登录私钥
212
+ async createCustomLoginKeys() {
213
+ return this.cloudService.request('CreateCustomLoginKeys', {
214
+ EnvId: this.envId
215
+ });
216
+ }
217
+ // 获取 COS CORS 域名
218
+ async getCOSDomains() {
219
+ const cos = this.getCos();
220
+ const getBucketCors = util_1.default.promisify(cos.getBucketCors).bind(cos);
221
+ const { bucket, region } = this.getStorageConfig();
222
+ const res = await getBucketCors({
223
+ Bucket: bucket,
224
+ Region: region
225
+ });
226
+ return res.CORSRules;
227
+ }
228
+ // 添加 COS CORS 域名,和 Web 端行为保持一致
229
+ async modifyCosCorsDomain(domain, deleted = false) {
230
+ const cos = this.getCos();
231
+ const putBucketCors = util_1.default.promisify(cos.putBucketCors).bind(cos);
232
+ const { bucket, region } = this.getStorageConfig();
233
+ // 去掉原有此域名CORS配置
234
+ let corsRules = await this.getCOSDomains();
235
+ corsRules = corsRules.filter(item => {
236
+ return !(item.AllowedOrigins &&
237
+ item.AllowedOrigins.length === 2 &&
238
+ item.AllowedOrigins[0] === `http://${domain}` &&
239
+ item.AllowedOrigins[1] === `https://${domain}`);
240
+ });
241
+ if (!deleted) {
242
+ corsRules.push({
243
+ AllowedOrigin: [`http://${domain}`, `https://${domain}`],
244
+ AllowedMethod: ['GET', 'POST', 'PUT', 'DELETE', 'HEAD'],
245
+ AllowedHeader: ['*'],
246
+ ExposeHeader: ['Etag', 'Date'],
247
+ MaxAgeSeconds: '5'
248
+ });
249
+ }
250
+ await putBucketCors({
251
+ Bucket: bucket,
252
+ Region: region,
253
+ CORSRules: corsRules
254
+ });
255
+ }
256
+ getCos() {
257
+ const { secretId, secretKey, token } = this.environment.getAuthConfig();
258
+ const cosConfig = {
259
+ SecretId: secretId,
260
+ SecretKey: secretKey,
261
+ SecurityToken: token,
262
+ Domain: constant_1.USE_INTERNAL_ENDPOINT ? "{Bucket}.cos-internal.{Region}.tencentcos.cn" /* INTERNAL */ : "{Bucket}.cos.{Region}.tencentcos.cn" /* PUBLIC */,
263
+ Protocol: constant_1.USE_INTERNAL_ENDPOINT
264
+ ? 'http:'
265
+ : (constant_1.COS_SDK_PROTOCOL === null || constant_1.COS_SDK_PROTOCOL === void 0 ? void 0 : constant_1.COS_SDK_PROTOCOL.endsWith(':')) ? constant_1.COS_SDK_PROTOCOL.toLowerCase()
266
+ : constant_1.COS_SDK_PROTOCOL.toLowerCase() + ':'
267
+ };
268
+ return new cos_nodejs_sdk_v5_1.default(cosConfig);
269
+ }
270
+ getStorageConfig() {
271
+ var _a;
272
+ const envConfig = this.environment.lazyEnvironmentConfig;
273
+ const storageConfig = (_a = envConfig === null || envConfig === void 0 ? void 0 : envConfig.Storages) === null || _a === void 0 ? void 0 : _a[0];
274
+ const { Region, Bucket } = storageConfig;
275
+ return {
276
+ env: envConfig.EnvId,
277
+ region: Region,
278
+ bucket: Bucket
279
+ };
280
+ }
281
+ }
282
+ __decorate([
283
+ utils_1.preLazy()
284
+ ], EnvService.prototype, "createEnvDomain", null);
285
+ __decorate([
286
+ utils_1.preLazy()
287
+ ], EnvService.prototype, "deleteEnvDomain", null);
288
+ exports.EnvService = EnvService;
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Environment = void 0;
4
+ const database_1 = require("./database");
5
+ const function_1 = require("./function");
6
+ const storage_1 = require("./storage");
7
+ const env_1 = require("./env");
8
+ const common_1 = require("./common");
9
+ const error_1 = require("./error");
10
+ const constant_1 = require("./constant");
11
+ const utils_1 = require("./utils");
12
+ const hosting_1 = require("./hosting");
13
+ const third_1 = require("./third");
14
+ const access_1 = require("./access");
15
+ const user_1 = require("./user");
16
+ const cloudBaseRun_1 = require("./cloudBaseRun");
17
+ class Environment {
18
+ constructor(context, envId) {
19
+ this.inited = false;
20
+ this.envId = envId;
21
+ this.cloudBaseContext = context;
22
+ this.envType = context.envType;
23
+ // 拉取当前环境 的环境信息 todo
24
+ this.functionService = new function_1.FunctionService(this);
25
+ this.databaseService = new database_1.DatabaseService(this);
26
+ this.storageService = new storage_1.StorageService(this);
27
+ this.envService = new env_1.EnvService(this);
28
+ this.hostingService = new hosting_1.HostingService(this);
29
+ this.thirdService = new third_1.ThirdService(this);
30
+ this.accessService = new access_1.AccessService(this);
31
+ this.userService = new user_1.UserService(this);
32
+ this.cloudBaseRunService = new cloudBaseRun_1.CloudBaseRunService(this);
33
+ }
34
+ async lazyInit() {
35
+ if (!this.inited) {
36
+ const envConfig = this.envService;
37
+ return envConfig.getEnvInfo().then(envInfo => {
38
+ this.lazyEnvironmentConfig = envInfo.EnvInfo;
39
+ if (!this.lazyEnvironmentConfig.EnvId) {
40
+ throw new error_1.CloudBaseError(`Environment ${this.envId} not found`);
41
+ }
42
+ this.inited = true;
43
+ return this.lazyEnvironmentConfig;
44
+ });
45
+ }
46
+ else {
47
+ return this.lazyEnvironmentConfig;
48
+ }
49
+ }
50
+ getEnvId() {
51
+ return this.envId;
52
+ }
53
+ getEnvType() {
54
+ return this.envType;
55
+ }
56
+ getStorageService() {
57
+ return this.storageService;
58
+ }
59
+ getDatabaseService() {
60
+ return this.databaseService;
61
+ }
62
+ getFunctionService() {
63
+ return this.functionService;
64
+ }
65
+ getEnvService() {
66
+ return this.envService;
67
+ }
68
+ getHostingService() {
69
+ return this.hostingService;
70
+ }
71
+ getThirdService() {
72
+ return this.thirdService;
73
+ }
74
+ getAccessService() {
75
+ return this.accessService;
76
+ }
77
+ getUserService() {
78
+ return this.userService;
79
+ }
80
+ getCloudBaseRunService() {
81
+ return this.cloudBaseRunService;
82
+ }
83
+ getCommonService(serviceType = 'tcb', serviceVersion) {
84
+ return new common_1.CommonService(this, serviceType, serviceVersion);
85
+ }
86
+ getServicesEnvInfo() {
87
+ const envConfig = this.envService;
88
+ return envConfig.getEnvInfo().then(envInfo => {
89
+ return envInfo.EnvInfo;
90
+ });
91
+ }
92
+ getAuthConfig() {
93
+ let { secretId, secretKey, token, proxy, region } = this.cloudBaseContext;
94
+ const envId = this.getEnvId();
95
+ if (!secretId || !secretKey) {
96
+ // 未主动传入密钥,从环境变量中读取
97
+ const envSecretId = utils_1.getEnvVar(constant_1.ENV_NAME.ENV_SECRETID);
98
+ const envSecretKey = utils_1.getEnvVar(constant_1.ENV_NAME.ENV_SECRETKEY);
99
+ const envToken = utils_1.getEnvVar(constant_1.ENV_NAME.ENV_SESSIONTOKEN);
100
+ if (!envSecretId || !envSecretKey) {
101
+ if (utils_1.getRuntime() === constant_1.RUN_ENV.SCF) {
102
+ throw new Error('missing authoration key, redeploy the function');
103
+ }
104
+ else {
105
+ throw new Error('missing secretId or secretKey of tencent cloud');
106
+ }
107
+ }
108
+ else {
109
+ secretId = envSecretId;
110
+ secretKey = envSecretKey;
111
+ token = envToken;
112
+ }
113
+ }
114
+ return {
115
+ envId,
116
+ secretId,
117
+ secretKey,
118
+ token,
119
+ proxy,
120
+ region
121
+ };
122
+ }
123
+ }
124
+ exports.Environment = Environment;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EnvironmentManager = void 0;
4
+ const environment_1 = require("./environment");
5
+ const constant_1 = require("./constant");
6
+ class EnvironmentManager {
7
+ constructor(context) {
8
+ this.envs = {};
9
+ this.currentEnv = null;
10
+ this.cloudBaseContext = context;
11
+ }
12
+ getCurrentEnv() {
13
+ if (!this.currentEnv) {
14
+ throw new Error(constant_1.ERROR.CURRENT_ENVIRONMENT_IS_NULL);
15
+ }
16
+ return this.currentEnv;
17
+ }
18
+ add(envId) {
19
+ if (!this.envs[envId]) {
20
+ this.envs[envId] = new environment_1.Environment(this.cloudBaseContext, envId);
21
+ }
22
+ if (!this.currentEnv) {
23
+ this.currentEnv = this.envs[envId];
24
+ }
25
+ }
26
+ remove(envId) {
27
+ this.envs[envId] = null;
28
+ delete this.envs[envId];
29
+ }
30
+ get(envId) {
31
+ return this.envs[envId] || null;
32
+ }
33
+ switchEnv(envId) {
34
+ const env = this.envs[envId];
35
+ if (env) {
36
+ this.currentEnv = env;
37
+ return true;
38
+ }
39
+ else {
40
+ return false;
41
+ }
42
+ }
43
+ }
44
+ exports.EnvironmentManager = EnvironmentManager;
package/lib/error.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CloudBaseError = void 0;
4
+ class CloudBaseError extends Error {
5
+ constructor(message, options = {}) {
6
+ super();
7
+ this.name = 'CloudBaseError';
8
+ const { code = '', action = '', original = null, requestId = '' } = options;
9
+ this.message = action ? `[${action}] ${message}` : message;
10
+ this.original = original;
11
+ this.code = code;
12
+ this.requestId = requestId;
13
+ this.action = action;
14
+ }
15
+ }
16
+ exports.CloudBaseError = CloudBaseError;