@cloudbase/manager-node 4.2.0 → 4.2.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.
- package/CHANGELOG.md +0 -4
- package/lib/constant.js +1 -5
- package/lib/env/index.js +209 -10
- package/lib/function/index.js +5 -3
- package/lib/storage/index.js +3 -19
- package/lib/utils/cloud-api-request.js +0 -7
- package/lib/utils/http-request.js +3 -3
- package/package.json +3 -4
- package/src/access/index.ts +168 -0
- package/src/access/types.ts +55 -0
- package/src/billing/index.ts +43 -0
- package/src/cam/index.ts +106 -0
- package/src/cloudBaseRun/index.ts +40 -0
- package/src/cloudBaseRun/types.ts +24 -0
- package/src/common/index.ts +54 -0
- package/src/constant.ts +56 -0
- package/src/context.ts +18 -0
- package/src/database/index.ts +369 -0
- package/src/debug.ts +34 -0
- package/src/env/index.ts +614 -0
- package/src/environment.ts +156 -0
- package/src/environmentManager.ts +50 -0
- package/src/error.ts +27 -0
- package/src/function/index.ts +1362 -0
- package/src/function/packer.ts +164 -0
- package/src/function/types.ts +164 -0
- package/src/hosting/index.ts +698 -0
- package/src/index.ts +127 -0
- package/src/interfaces/base.interface.ts +8 -0
- package/src/interfaces/billing.interface.ts +21 -0
- package/src/interfaces/cam.interface.ts +28 -0
- package/src/interfaces/flexdb.interface.ts +104 -0
- package/src/interfaces/function.interface.ts +75 -0
- package/src/interfaces/index.ts +7 -0
- package/src/interfaces/storage.interface.ts +29 -0
- package/src/interfaces/tcb.interface.ts +636 -0
- package/src/storage/index.ts +1281 -0
- package/src/third/index.ts +24 -0
- package/src/user/index.ts +174 -0
- package/src/user/types.ts +21 -0
- package/src/utils/auth.ts +112 -0
- package/src/utils/cloud-api-request.ts +252 -0
- package/src/utils/cloudbase-request.ts +109 -0
- package/src/utils/envLazy.ts +15 -0
- package/src/utils/fs.ts +57 -0
- package/src/utils/http-request.ts +37 -0
- package/src/utils/index.ts +103 -0
- package/src/utils/parallel.ts +82 -0
- package/src/utils/uuid.ts +14 -0
- package/types/constant.d.ts +0 -7
- package/types/env/index.d.ts +17 -0
- package/types/function/index.d.ts +2 -1
- package/lib/utils/runenv.js +0 -8
- package/types/utils/runenv.d.ts +0 -1
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import del from 'del'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
import makeDir from 'make-dir'
|
|
5
|
+
import util from 'util'
|
|
6
|
+
import { compressToZip, checkFullAccess } from '../utils'
|
|
7
|
+
import { CloudBaseError } from '../error'
|
|
8
|
+
|
|
9
|
+
// 10 MB
|
|
10
|
+
export const BIG_FILE_SIZE = 10485760
|
|
11
|
+
export const API_MAX_SIZE = 52428800
|
|
12
|
+
|
|
13
|
+
export enum CodeType {
|
|
14
|
+
File,
|
|
15
|
+
JavaFile
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface IPackerOptions {
|
|
19
|
+
// 通过根目录和函数名指定函数路径
|
|
20
|
+
root?: string
|
|
21
|
+
name?: string
|
|
22
|
+
ignore: string | string[]
|
|
23
|
+
incrementalPath?: string
|
|
24
|
+
// 直接指定函数的路径
|
|
25
|
+
functionPath?: string
|
|
26
|
+
codeType: CodeType
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const TEMPDIR_NAME = '.cloudbase_temp'
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 将函数代码转换成 Base64 编码
|
|
33
|
+
* 普通文件:Node,PHP
|
|
34
|
+
* Java 文件:Jar,ZIP
|
|
35
|
+
*/
|
|
36
|
+
export class FunctionPacker {
|
|
37
|
+
// 函数名
|
|
38
|
+
name: string
|
|
39
|
+
// 代码文件类型
|
|
40
|
+
type: CodeType
|
|
41
|
+
funcPath: string
|
|
42
|
+
zipFilePath: string
|
|
43
|
+
// 存放打包文件的临时目录
|
|
44
|
+
tmpPath: string
|
|
45
|
+
// 忽略文件模式
|
|
46
|
+
ignore: string | string[]
|
|
47
|
+
// 指定增量文件路径
|
|
48
|
+
incrementalPath: string
|
|
49
|
+
// 代码类型: Java 和 其他
|
|
50
|
+
codeType: CodeType
|
|
51
|
+
|
|
52
|
+
constructor(options: IPackerOptions) {
|
|
53
|
+
const { root, name, codeType, ignore, incrementalPath, functionPath } = options
|
|
54
|
+
this.name = name
|
|
55
|
+
this.ignore = ignore
|
|
56
|
+
this.codeType = codeType
|
|
57
|
+
this.incrementalPath = incrementalPath
|
|
58
|
+
this.funcPath = functionPath ? functionPath : path.resolve(root, name)
|
|
59
|
+
// 每个函数采用不同的文件夹
|
|
60
|
+
this.tmpPath = root
|
|
61
|
+
? path.join(root, `${TEMPDIR_NAME}_${name}`)
|
|
62
|
+
: path.join(process.cwd(), `${TEMPDIR_NAME}_${name}`)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async compressFiles() {
|
|
66
|
+
checkFullAccess(this.funcPath, true)
|
|
67
|
+
// 清除原打包文件
|
|
68
|
+
this.clean()
|
|
69
|
+
// 确保目标路径存在
|
|
70
|
+
await makeDir(this.tmpPath)
|
|
71
|
+
// 生成 name.zip 文件
|
|
72
|
+
this.zipFilePath = path.resolve(this.tmpPath, `${this.name}.zip`)
|
|
73
|
+
|
|
74
|
+
const zipOption: any = {
|
|
75
|
+
dirPath: this.funcPath,
|
|
76
|
+
outputPath: this.zipFilePath,
|
|
77
|
+
ignore: this.ignore
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (this.incrementalPath) {
|
|
81
|
+
zipOption.pattern = this.incrementalPath
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
await compressToZip(zipOption)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 获取 Java 代码
|
|
88
|
+
getJavaFile() {
|
|
89
|
+
const { funcPath } = this
|
|
90
|
+
// funcPath 可能以 .jar 或 .zip 结尾
|
|
91
|
+
const filePath = funcPath.replace(/\.jar$|\.zip$/g, '')
|
|
92
|
+
// Java 代码为 jar 或 zip 包
|
|
93
|
+
const jarExist = checkFullAccess(`${filePath}.jar`)
|
|
94
|
+
const zipExist = checkFullAccess(`${filePath}.zip`)
|
|
95
|
+
if (!jarExist && !zipExist) {
|
|
96
|
+
throw new CloudBaseError('未找到部署函数的 Jar 或者 ZIP 格式文件!')
|
|
97
|
+
}
|
|
98
|
+
this.zipFilePath = jarExist ? `${filePath}.jar` : `${filePath}.zip`
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async build() {
|
|
102
|
+
if (this.codeType === CodeType.JavaFile) {
|
|
103
|
+
try {
|
|
104
|
+
await this.getJavaFile()
|
|
105
|
+
} catch (e) {
|
|
106
|
+
this.clean()
|
|
107
|
+
throw new CloudBaseError(`函数代码打包失败:${e.message}`, {
|
|
108
|
+
code: e.code
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (this.codeType === CodeType.File) {
|
|
114
|
+
try {
|
|
115
|
+
await this.compressFiles()
|
|
116
|
+
} catch (e) {
|
|
117
|
+
this.clean()
|
|
118
|
+
throw new CloudBaseError(`函数代码打包失败:${e.message}`, {
|
|
119
|
+
code: e.code
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 函数压缩后的代码大于 10M,建议使用 COS 上传(当前暂不支持)
|
|
126
|
+
async isBigFile() {
|
|
127
|
+
if (!this.zipFilePath) {
|
|
128
|
+
await this.build()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const promiseStat = util.promisify(fs.stat)
|
|
132
|
+
const fileStats = await promiseStat(this.zipFilePath)
|
|
133
|
+
|
|
134
|
+
return fileStats.size > BIG_FILE_SIZE
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// API 最大 50MB
|
|
138
|
+
async isReachMaxSize() {
|
|
139
|
+
if (!this.zipFilePath) {
|
|
140
|
+
await this.build()
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const promiseStat = util.promisify(fs.stat)
|
|
144
|
+
const fileStats = await promiseStat(this.zipFilePath)
|
|
145
|
+
|
|
146
|
+
return fileStats.size > API_MAX_SIZE
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async getBase64Code() {
|
|
150
|
+
// 将 zip 文件转换成 base64
|
|
151
|
+
const base64 = fs.readFileSync(this.zipFilePath).toString('base64')
|
|
152
|
+
// 非 Java 函数清除打包文件
|
|
153
|
+
if (this.codeType !== CodeType.JavaFile) {
|
|
154
|
+
await this.clean()
|
|
155
|
+
}
|
|
156
|
+
return base64
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async clean(): Promise<void> {
|
|
160
|
+
// allow deleting the current working directory and outside
|
|
161
|
+
this.tmpPath && del.sync([this.tmpPath], { force: true })
|
|
162
|
+
return
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
export interface ITag {
|
|
2
|
+
TagKey: string
|
|
3
|
+
TagValue: string[]
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* VPC
|
|
8
|
+
*/
|
|
9
|
+
export interface IVpc {
|
|
10
|
+
VpcId: string
|
|
11
|
+
VpcName: string
|
|
12
|
+
CidrBlock: string
|
|
13
|
+
Ipv6CidrBlock: string
|
|
14
|
+
IsDefault: false
|
|
15
|
+
EnableMulticast: false
|
|
16
|
+
CreatedTime: string
|
|
17
|
+
EnableDhcp: boolean
|
|
18
|
+
DhcpOptionsId: string
|
|
19
|
+
DnsServerSet: string[]
|
|
20
|
+
DomainName: string
|
|
21
|
+
TagSet: any[]
|
|
22
|
+
AssistantCidrSet: any[]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 子网
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
export interface ISubnet {
|
|
30
|
+
VpcId: string
|
|
31
|
+
SubnetId: string
|
|
32
|
+
SubnetName: string
|
|
33
|
+
CidrBlock: string
|
|
34
|
+
Ipv6CidrBlock: string
|
|
35
|
+
IsDefault: boolean
|
|
36
|
+
IsRemoteVpcSnat: boolean
|
|
37
|
+
EnableBroadcast: boolean
|
|
38
|
+
Zone: string
|
|
39
|
+
RouteTableId: string
|
|
40
|
+
NetworkAclId: string
|
|
41
|
+
TotalIpAddressCount: number
|
|
42
|
+
AvailableIpAddressCount: number
|
|
43
|
+
CreatedTime: string
|
|
44
|
+
TagSet: any[]
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 函数
|
|
48
|
+
*/
|
|
49
|
+
export interface IFunctionLog {
|
|
50
|
+
FunctionName: string
|
|
51
|
+
RetMsg: string
|
|
52
|
+
RequestId: string
|
|
53
|
+
StartTime?: string
|
|
54
|
+
RetCode?: number
|
|
55
|
+
InvokeFinished?: number
|
|
56
|
+
Duration?: number
|
|
57
|
+
BillDuration?: number
|
|
58
|
+
MemUsage: number
|
|
59
|
+
Log?: string
|
|
60
|
+
Level?: string
|
|
61
|
+
Source?: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface ILogFilter {
|
|
65
|
+
RetCode?: 'not0' | 'is0' | 'TimeLimitExceeded' | 'ResourceLimitExceeded' | 'UserCodeException'
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type IRegion = number
|
|
69
|
+
|
|
70
|
+
export interface IEnvVariable {
|
|
71
|
+
Key: string
|
|
72
|
+
Value: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface ITrigger {
|
|
76
|
+
Name: string
|
|
77
|
+
Type: string
|
|
78
|
+
Config: string
|
|
79
|
+
TriggerName: string
|
|
80
|
+
TriggerDesc: string
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface IFunctionInfo {
|
|
84
|
+
FunctionName: string
|
|
85
|
+
FunctionId: string
|
|
86
|
+
Runtime: string
|
|
87
|
+
Handler: string
|
|
88
|
+
CodeSize: number
|
|
89
|
+
Timeout: number
|
|
90
|
+
FunctionVersion: string
|
|
91
|
+
MemorySize: number
|
|
92
|
+
UseGpu: 'TRUE' | 'FALSE'
|
|
93
|
+
CodeInfo: string
|
|
94
|
+
CodeResult: string
|
|
95
|
+
CodeError: string
|
|
96
|
+
ErrNo: number
|
|
97
|
+
Role: string
|
|
98
|
+
InstallDependency: 'FALSE' | 'TRUE'
|
|
99
|
+
AddTime: string
|
|
100
|
+
ModTime: string
|
|
101
|
+
Namespace: string
|
|
102
|
+
Status: string
|
|
103
|
+
StatusDesc: string
|
|
104
|
+
Description: string
|
|
105
|
+
Tags: ITag[]
|
|
106
|
+
VpcConfig: {
|
|
107
|
+
VpcId?: string
|
|
108
|
+
SubnetId?: string
|
|
109
|
+
vpc?: string
|
|
110
|
+
subnet?: string
|
|
111
|
+
}
|
|
112
|
+
Environment: {
|
|
113
|
+
Variables: IEnvVariable[]
|
|
114
|
+
}
|
|
115
|
+
Type: string
|
|
116
|
+
|
|
117
|
+
EipConfig: {
|
|
118
|
+
EipFixed: 'FALSE' | 'TRUE'
|
|
119
|
+
Eips: string[]
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
PublicNetConfig: {
|
|
123
|
+
PublicNetStatus: 'ENABLE' | 'DISABLE'
|
|
124
|
+
EipConfig: {
|
|
125
|
+
EipStatus: 'ENABLE' | 'DISABLE'
|
|
126
|
+
EipAddress: string[]
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
Triggers: ITrigger[]
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface IFunctionCode {
|
|
134
|
+
// 对象存储桶名称
|
|
135
|
+
CosBucketName?: string
|
|
136
|
+
// 对象存储对象路径
|
|
137
|
+
CosObjectName?: string
|
|
138
|
+
// 包含函数代码文件及其依赖项的 zip 格式文件,使用该接口时要求将 zip 文件的内容转成 base64 编码,最大支持20M
|
|
139
|
+
ZipFile: string
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
export interface ILayerVersionItem {
|
|
144
|
+
LayerName: string
|
|
145
|
+
LayerVersion: number
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface IFunctionUpdateAttribute {
|
|
149
|
+
Code: IFunctionCode
|
|
150
|
+
Description: string
|
|
151
|
+
FunctionName: string
|
|
152
|
+
MemorySize: number
|
|
153
|
+
Timeout: number
|
|
154
|
+
UseGpu: 'FALSE' | 'TRUE'
|
|
155
|
+
Namespace: string
|
|
156
|
+
Environment: { Variables: IEnvVariable[] }
|
|
157
|
+
VpcConfig: { VpcId: string; SubnetId: string }
|
|
158
|
+
InstallDependency?: 'FALSE' | 'TRUE'
|
|
159
|
+
PublicNetConfig: {
|
|
160
|
+
PublicNetStatus: string
|
|
161
|
+
EipConfig: { EipStatus: string; EipAddress: string[] }
|
|
162
|
+
}
|
|
163
|
+
Layers?: ILayerVersionItem[]
|
|
164
|
+
}
|