@cloudbase/manager-node 4.2.3 → 4.2.5
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 +4 -0
- package/lib/constant.js +5 -1
- package/lib/env/index.js +10 -209
- package/lib/function/index.js +0 -3
- package/lib/storage/index.js +24 -10
- package/lib/utils/cloud-api-request.js +7 -0
- package/lib/utils/http-request.js +3 -3
- package/lib/utils/runenv.js +8 -0
- package/package.json +4 -3
- package/types/constant.d.ts +7 -0
- package/types/env/index.d.ts +0 -17
- package/types/function/types.d.ts +0 -2
- package/types/utils/runenv.d.ts +1 -0
- package/src/access/index.ts +0 -168
- package/src/access/types.ts +0 -55
- package/src/billing/index.ts +0 -43
- package/src/cam/index.ts +0 -106
- package/src/cloudBaseRun/index.ts +0 -40
- package/src/cloudBaseRun/types.ts +0 -24
- package/src/common/index.ts +0 -54
- package/src/constant.ts +0 -56
- package/src/context.ts +0 -18
- package/src/database/index.ts +0 -369
- package/src/debug.ts +0 -34
- package/src/env/index.ts +0 -614
- package/src/environment.ts +0 -156
- package/src/environmentManager.ts +0 -50
- package/src/error.ts +0 -27
- package/src/function/index.ts +0 -1378
- package/src/function/packer.ts +0 -164
- package/src/function/types.ts +0 -165
- package/src/hosting/index.ts +0 -698
- package/src/index.ts +0 -127
- package/src/interfaces/base.interface.ts +0 -8
- package/src/interfaces/billing.interface.ts +0 -21
- package/src/interfaces/cam.interface.ts +0 -28
- package/src/interfaces/flexdb.interface.ts +0 -104
- package/src/interfaces/function.interface.ts +0 -75
- package/src/interfaces/index.ts +0 -7
- package/src/interfaces/storage.interface.ts +0 -29
- package/src/interfaces/tcb.interface.ts +0 -642
- package/src/storage/index.ts +0 -1290
- package/src/third/index.ts +0 -24
- package/src/user/index.ts +0 -174
- package/src/user/types.ts +0 -21
- package/src/utils/auth.ts +0 -112
- package/src/utils/cloud-api-request.ts +0 -252
- package/src/utils/cloudbase-request.ts +0 -109
- package/src/utils/envLazy.ts +0 -15
- package/src/utils/fs.ts +0 -57
- package/src/utils/http-request.ts +0 -37
- package/src/utils/index.ts +0 -103
- package/src/utils/parallel.ts +0 -82
- package/src/utils/uuid.ts +0 -14
package/src/utils/envLazy.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export function preLazy() {
|
|
2
|
-
return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
3
|
-
let oldFunc = descriptor.value
|
|
4
|
-
descriptor.value = async function() {
|
|
5
|
-
// 检查当前环境对象上是否已加载好环境信息
|
|
6
|
-
const currentEnvironment = this.environment
|
|
7
|
-
|
|
8
|
-
if (!currentEnvironment.inited) {
|
|
9
|
-
await currentEnvironment.lazyInit()
|
|
10
|
-
}
|
|
11
|
-
let result = await oldFunc.apply(this, arguments)
|
|
12
|
-
return result
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
package/src/utils/fs.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import { CloudBaseError } from '../error'
|
|
3
|
-
import del from 'del'
|
|
4
|
-
|
|
5
|
-
type SizeUnit = 'MB' | 'GB'
|
|
6
|
-
|
|
7
|
-
// 检查路径是否可以访问(读、写)
|
|
8
|
-
export function checkFullAccess(dest: string, throwError = false): boolean {
|
|
9
|
-
try {
|
|
10
|
-
// 可见、可写
|
|
11
|
-
fs.accessSync(dest, fs.constants.F_OK)
|
|
12
|
-
fs.accessSync(dest, fs.constants.W_OK)
|
|
13
|
-
fs.accessSync(dest, fs.constants.R_OK)
|
|
14
|
-
return true
|
|
15
|
-
} catch (e) {
|
|
16
|
-
if (throwError) {
|
|
17
|
-
throw new CloudBaseError(`路径不存在或无读写权限:${dest}`)
|
|
18
|
-
} else {
|
|
19
|
-
return false
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// 检查路径是否可以写
|
|
25
|
-
export function checkReadable(dest: string, throwError = false): boolean {
|
|
26
|
-
try {
|
|
27
|
-
// 可见、可读
|
|
28
|
-
fs.accessSync(dest, fs.constants.F_OK)
|
|
29
|
-
fs.accessSync(dest, fs.constants.R_OK)
|
|
30
|
-
return true
|
|
31
|
-
} catch (e) {
|
|
32
|
-
if (throwError) {
|
|
33
|
-
throw new CloudBaseError(`路径不存在或无读权限:${dest}`)
|
|
34
|
-
} else {
|
|
35
|
-
return false
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function isDirectory(dest: string) {
|
|
41
|
-
checkFullAccess(dest, true)
|
|
42
|
-
return fs.statSync(dest).isDirectory()
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function formateFileSize(size: number, unit: SizeUnit) {
|
|
46
|
-
const unitMap = {
|
|
47
|
-
KB: 1024,
|
|
48
|
-
MB: Math.pow(1024, 2),
|
|
49
|
-
GB: Math.pow(1024, 3)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return Number(size / unitMap[unit]).toFixed(2)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function delSync(patterns: string | readonly string[]) {
|
|
56
|
-
del.sync(patterns, { force: true })
|
|
57
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { URL } from 'url'
|
|
2
|
-
import _fetch from 'node-fetch'
|
|
3
|
-
import HttpsProxyAgent from 'https-proxy-agent'
|
|
4
|
-
import { CloudBaseError } from '../error'
|
|
5
|
-
|
|
6
|
-
// 使用 fetch + 代理
|
|
7
|
-
export async function fetch(url: string, config: Record<string, any> = {}, proxy) {
|
|
8
|
-
if (proxy || process.env.http_proxy) {
|
|
9
|
-
config.agent = new HttpsProxyAgent(proxy || process.env.http_proxy)
|
|
10
|
-
}
|
|
11
|
-
// 解决中文编码问题
|
|
12
|
-
const escapeUrl = new URL(url).toString()
|
|
13
|
-
let json
|
|
14
|
-
let text
|
|
15
|
-
try {
|
|
16
|
-
const res = await _fetch(escapeUrl, config)
|
|
17
|
-
text = await res.text()
|
|
18
|
-
json = JSON.parse(text)
|
|
19
|
-
} catch (e) {
|
|
20
|
-
// 某些情况下回返回 HTML 文本异常
|
|
21
|
-
// JSON 解析错误,抛出原响应文本
|
|
22
|
-
if (e.name === 'SyntaxError') {
|
|
23
|
-
throw new CloudBaseError(text)
|
|
24
|
-
}
|
|
25
|
-
throw new CloudBaseError(e)
|
|
26
|
-
}
|
|
27
|
-
return json
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export async function fetchStream(url: string, config: Record<string, any> = {}, proxy) {
|
|
31
|
-
if (proxy || process.env.http_proxy) {
|
|
32
|
-
config.agent = new HttpsProxyAgent(proxy || process.env.http_proxy)
|
|
33
|
-
}
|
|
34
|
-
// 解决中文编码问题
|
|
35
|
-
const escapeUrl = new URL(url).toString()
|
|
36
|
-
return _fetch(escapeUrl, config)
|
|
37
|
-
}
|
package/src/utils/index.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import archiver from 'archiver'
|
|
3
|
-
import crypto from 'crypto'
|
|
4
|
-
import { PUBLIC_RSA_KEY, ENV_NAME } from '../constant'
|
|
5
|
-
|
|
6
|
-
export { guid6 } from './uuid'
|
|
7
|
-
export * from './cloud-api-request'
|
|
8
|
-
export * from './auth'
|
|
9
|
-
export * from './cloudbase-request'
|
|
10
|
-
export * from './http-request'
|
|
11
|
-
export * from './envLazy'
|
|
12
|
-
export * from './fs'
|
|
13
|
-
|
|
14
|
-
interface IZipOption {
|
|
15
|
-
dirPath: string
|
|
16
|
-
outputPath: string
|
|
17
|
-
ignore?: string | string[]
|
|
18
|
-
pattern?: string
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export async function compressToZip(option: IZipOption) {
|
|
22
|
-
const { dirPath, outputPath, ignore, pattern = '**/*' } = option
|
|
23
|
-
|
|
24
|
-
return new Promise((resolve, reject) => {
|
|
25
|
-
const output = fs.createWriteStream(outputPath)
|
|
26
|
-
const archive = archiver('zip')
|
|
27
|
-
|
|
28
|
-
output.on('close', function () {
|
|
29
|
-
resolve({
|
|
30
|
-
zipPath: outputPath,
|
|
31
|
-
size: Math.ceil(archive.pointer() / 1024)
|
|
32
|
-
})
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
archive.on('error', function (err) {
|
|
36
|
-
reject(err)
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
archive.pipe(output)
|
|
40
|
-
// append files from a glob pattern
|
|
41
|
-
archive.glob(pattern, {
|
|
42
|
-
// 目标路径
|
|
43
|
-
cwd: dirPath,
|
|
44
|
-
ignore: ignore,
|
|
45
|
-
dot: true
|
|
46
|
-
})
|
|
47
|
-
archive.finalize()
|
|
48
|
-
})
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function getRuntime(): string {
|
|
52
|
-
return process.env[ENV_NAME.ENV_RUNENV]
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function getEnvVar(envName: string): string {
|
|
56
|
-
return process.env[envName]
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function rsaEncrypt(data: string): string {
|
|
60
|
-
const buffer = Buffer.from(data)
|
|
61
|
-
const encrypted = crypto.publicEncrypt(
|
|
62
|
-
{
|
|
63
|
-
key: PUBLIC_RSA_KEY,
|
|
64
|
-
padding: crypto.constants.RSA_PKCS1_PADDING
|
|
65
|
-
},
|
|
66
|
-
buffer
|
|
67
|
-
)
|
|
68
|
-
return encrypted.toString('base64')
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function sleep(time: number): Promise<void> {
|
|
72
|
-
return new Promise(resolve => {
|
|
73
|
-
setTimeout(() => {
|
|
74
|
-
resolve()
|
|
75
|
-
}, time)
|
|
76
|
-
})
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function upperCaseStringFisrt(str: string) {
|
|
80
|
-
return str.slice(0, 1).toUpperCase().concat(str.slice(1))
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export function upperCaseObjKey(object: any) {
|
|
84
|
-
const type = Object.prototype.toString.call(object).slice(8, -1)
|
|
85
|
-
if (type === 'Object') {
|
|
86
|
-
let newObj = {}
|
|
87
|
-
// eslint-disable-next-line guard-for-in
|
|
88
|
-
for (let key in object) {
|
|
89
|
-
newObj[upperCaseStringFisrt(key)] = upperCaseObjKey(object[key])
|
|
90
|
-
}
|
|
91
|
-
return newObj
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (type === 'Array') {
|
|
95
|
-
let newArr = []
|
|
96
|
-
for (let item of object) {
|
|
97
|
-
newArr.push(upperCaseObjKey(item))
|
|
98
|
-
}
|
|
99
|
-
return newArr
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return object
|
|
103
|
-
}
|
package/src/utils/parallel.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
export type AsyncTask = () => Promise<any>
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 异步任务并发控制器,以一定的并发数执行所有任务
|
|
5
|
-
* 不具备队列性质,异步任务随机执行
|
|
6
|
-
* 单个任务异常,错误会返回,单不会退出执行
|
|
7
|
-
* 所有任务执行
|
|
8
|
-
*/
|
|
9
|
-
export class AsyncTaskParallelController {
|
|
10
|
-
// 任务最大并发数
|
|
11
|
-
maxParallel: number
|
|
12
|
-
// 异步任务队列
|
|
13
|
-
tasks: AsyncTask[]
|
|
14
|
-
// 检查间隔时间,单位 ms
|
|
15
|
-
checkInterval: number
|
|
16
|
-
// 所有任务数量
|
|
17
|
-
totalTasks: number
|
|
18
|
-
|
|
19
|
-
constructor(maxParallel: number, checkInterval = 20) {
|
|
20
|
-
this.tasks = []
|
|
21
|
-
this.maxParallel = Number(maxParallel) || 20
|
|
22
|
-
this.checkInterval = checkInterval
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
loadTasks(tasks: AsyncTask[]) {
|
|
26
|
-
this.tasks.push(...tasks)
|
|
27
|
-
this.totalTasks = this.tasks.length
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
push(task: AsyncTask) {
|
|
31
|
-
this.tasks.push(task)
|
|
32
|
-
this.totalTasks = this.tasks.length
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// 开始执行任务
|
|
36
|
-
async run(): Promise<any[]> {
|
|
37
|
-
// 存储任务执行结果
|
|
38
|
-
const results = []
|
|
39
|
-
// 记录已经运行的任务
|
|
40
|
-
const taskHasRun = []
|
|
41
|
-
// 记录任务是否执行完成
|
|
42
|
-
const taskDone = []
|
|
43
|
-
// 当前正在运行的任务数量
|
|
44
|
-
let runningTask = 0
|
|
45
|
-
|
|
46
|
-
return new Promise(resolve => {
|
|
47
|
-
// 使用定时器,不阻塞线程
|
|
48
|
-
const timer = setInterval(() => {
|
|
49
|
-
// 全部任务运行完成
|
|
50
|
-
const taskDoneLength = taskDone.filter(item => item).length
|
|
51
|
-
if (runningTask === 0 && taskDoneLength === this.totalTasks) {
|
|
52
|
-
clearInterval(timer)
|
|
53
|
-
resolve(results)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// 当前运行任务数超过最大并发,不再执行新的任务
|
|
57
|
-
if (runningTask >= this.maxParallel) {
|
|
58
|
-
return
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// 遍历任务列表,开始执行还没有执行的任务
|
|
62
|
-
this.tasks.forEach((task, index) => {
|
|
63
|
-
if (!taskHasRun[index] && runningTask < this.maxParallel) {
|
|
64
|
-
runningTask++
|
|
65
|
-
taskHasRun[index] = 1
|
|
66
|
-
task()
|
|
67
|
-
.then(res => {
|
|
68
|
-
results[index] = res
|
|
69
|
-
})
|
|
70
|
-
.catch(err => {
|
|
71
|
-
results[index] = err
|
|
72
|
-
})
|
|
73
|
-
.then(() => {
|
|
74
|
-
runningTask--
|
|
75
|
-
taskDone[index] = 1
|
|
76
|
-
})
|
|
77
|
-
}
|
|
78
|
-
})
|
|
79
|
-
}, this.checkInterval)
|
|
80
|
-
})
|
|
81
|
-
}
|
|
82
|
-
}
|
package/src/utils/uuid.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// 环境 uuid
|
|
2
|
-
export function guid6() {
|
|
3
|
-
return Math.floor((1 + Math.random()) * 0x1000000)
|
|
4
|
-
.toString(16)
|
|
5
|
-
.substring(1)
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function uuidv4() {
|
|
9
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
10
|
-
const r = (Math.random() * 16) | 0
|
|
11
|
-
const v = c === 'x' ? r : (r & 0x3) | 0x8
|
|
12
|
-
return v.toString(16)
|
|
13
|
-
})
|
|
14
|
-
}
|