@cloudbase/manager-node 4.1.0 → 4.1.1

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