Skip to content

Commit ec1864f

Browse files
committed
refactor(cloud-function): simplize cloud-function types/class api;
1 parent 1cba956 commit ec1864f

File tree

14 files changed

+120
-57
lines changed

14 files changed

+120
-57
lines changed

packages/app-service/src/api/function-log.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-07-30 10:30:29
4-
* @LastEditTime: 2021-11-03 16:45:06
4+
* @LastEditTime: 2021-11-05 12:06:42
55
* @Description:
66
*/
7+
import { ObjectId } from "bson"
78
import { Constants } from "../constants"
89
import { DatabaseAgent } from "../lib/database"
910

1011

12+
export interface CloudFunctionLogStruct {
13+
requestId: string
14+
method: string
15+
func_id: ObjectId
16+
func_name: string
17+
logs: string[]
18+
time_usage: number
19+
data: any
20+
error: Error
21+
debug?: boolean
22+
created_at?: Date
23+
created_by?: any
24+
}
25+
1126
/**
1227
* Add function execution log
1328
* @param data
1429
* @returns
1530
*/
16-
export async function addFunctionLog(data: any): Promise<any> {
31+
export async function addFunctionLog(data: CloudFunctionLogStruct): Promise<any> {
1732
const db = DatabaseAgent.db
1833

1934
if (!data) return null

packages/app-service/src/api/function.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-07-30 10:30:29
4-
* @LastEditTime: 2021-10-06 19:25:59
4+
* @LastEditTime: 2021-11-05 13:38:09
55
* @Description:
66
*/
77

packages/app-service/src/cloud-sdk/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AxiosStatic } from "axios"
22
import { Db, getDb } from "database-proxy"
3-
import { FunctionContext, CloudFunction } from "cloud-function-engine"
3+
import { FunctionContext } from "cloud-function-engine"
44
import { FileStorageInterface } from "../lib/storage/interface"
55
import * as mongodb from "mongodb"
66
import { DatabaseAgent } from "../lib/database"
@@ -9,6 +9,7 @@ import { SchedulerInstance } from "../lib/scheduler"
99
import { getToken, parseToken } from "../lib/utils/token"
1010
import { invokeInFunction } from "./invoke"
1111
import { createFileStorage } from "../lib/storage"
12+
import { CloudFunction } from "../lib/function"
1213

1314

1415
export type InvokeFunctionType = (name: string, param: FunctionContext) => Promise<any>

packages/app-service/src/cloud-sdk/invoke.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getFunctionByName } from "../api/function"
2-
import { CloudFunction, FunctionContext } from "cloud-function-engine"
2+
import { FunctionContext } from "cloud-function-engine"
33
import { addFunctionLog } from "../api/function-log"
4+
import { CloudFunction } from "../lib/function"
45

56

67
/**
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
3+
import * as engine from "cloud-function-engine"
4+
import { ObjectId } from "mongodb"
5+
6+
export enum FunctionStatus {
7+
DISABLED = 0,
8+
ENABLED = 1
9+
}
10+
11+
/**
12+
* Extended function struct
13+
*/
14+
export interface CloudFunctionStruct extends engine.CloudFunctionStruct {
15+
_id: ObjectId
16+
description: string
17+
tags: string[]
18+
label: string
19+
triggers: any[]
20+
version: number
21+
hash: string
22+
status: FunctionStatus
23+
enableHTTP: boolean
24+
appid: string
25+
debugParams: string
26+
created_at: number
27+
updated_at: number
28+
created_by: any
29+
}
30+
31+
32+
export class CloudFunction extends engine.CloudFunction {
33+
/**
34+
* Custom require function in cloud function
35+
* @see CloudFunction.require_func
36+
* @param module the module id. ex. `path`, `lodash`
37+
* @returns
38+
*/
39+
40+
static require_func = (module: string): any => {
41+
if (module === '@/cloud-sdk') {
42+
return require('../../cloud-sdk')
43+
}
44+
45+
return require(module) as any
46+
}
47+
48+
/**
49+
* Function data
50+
*/
51+
protected _data: CloudFunctionStruct
52+
53+
get id() {
54+
return this._data._id
55+
}
56+
57+
/**
58+
* Http enabled status
59+
*/
60+
get enableHTTP() {
61+
return this._data.enableHTTP
62+
}
63+
64+
/**
65+
* Function status which control if the function could be invoked or not
66+
*/
67+
get status() {
68+
return this._data.status
69+
}
70+
}

packages/app-service/src/lib/policy-agent/policy-agent.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-08-04 00:31:53
4-
* @LastEditTime: 2021-10-06 21:12:34
4+
* @LastEditTime: 2021-11-05 13:52:52
55
* @Description:
66
*/
77

88
import assert = require("assert")
99
import { AccessorInterface, Params, Policy } from "database-proxy"
10-
import { CloudFunction } from "cloud-function-engine"
1110
import { getFunctionByName } from "../../api/function"
1211
import { InjectionGetter, PolicyAgentInterface, PolicyComposition, PolicyDataStruct } from "./types"
1312
import { logger } from "../logger"
13+
import { CloudFunction } from "../function"
1414

1515

1616
/**

packages/app-service/src/router/function/debug.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-07-30 10:30:29
4-
* @LastEditTime: 2021-11-03 19:41:03
4+
* @LastEditTime: 2021-11-05 13:49:11
55
* @Description:
66
*/
77

88
import { Request, Response } from 'express'
9-
import { FunctionContext, CloudFunction } from 'cloud-function-engine'
9+
import { FunctionContext } from 'cloud-function-engine'
1010
import { parseToken } from '../../lib/utils/token'
1111
import { logger } from '../../lib/logger'
1212
import { addFunctionLog } from '../../api/function-log'
1313
import { ObjectId } from 'bson'
14+
import { CloudFunction } from '../../lib/function'
1415

1516
/**
1617
* Handler of debugging cloud function

packages/app-service/src/router/function/index.ts

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,17 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-07-30 10:30:29
4-
* @LastEditTime: 2021-10-07 01:41:39
4+
* @LastEditTime: 2021-11-05 13:53:31
55
* @Description:
66
*/
77

88
import { Router } from 'express'
9-
import { CloudFunction } from 'cloud-function-engine'
109
import * as multer from 'multer'
1110
import * as path from 'path'
1211
import { handleDebugFunction } from './debug'
1312
import { handleInvokeFunction } from './invoke'
1413
import { generateUUID } from '../../lib/utils/rand'
1514

16-
/**
17-
* Custom require function in cloud function
18-
* @see CloudFunction.require_func
19-
* @param module the module id. ex. `path`, `lodash`
20-
* @returns
21-
*/
22-
CloudFunction.require_func = (module): any => {
23-
if (module === '@/cloud-sdk') {
24-
return require('../../cloud-sdk')
25-
}
26-
return require(module) as any
27-
}
28-
2915
export const FunctionRouter = Router()
3016

3117
/**

packages/app-service/src/router/function/invoke.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-07-30 10:30:29
4-
* @LastEditTime: 2021-11-03 18:02:23
4+
* @LastEditTime: 2021-11-05 13:45:36
55
* @Description:
66
*/
77

88
import { Request, Response } from 'express'
9-
import { FunctionContext, CloudFunction } from 'cloud-function-engine'
9+
import { FunctionContext } from 'cloud-function-engine'
1010
import { getFunctionByName } from '../../api/function'
1111
import Config from '../../config'
1212
import { logger } from '../../lib/logger'
1313
import { addFunctionLog } from '../../api/function-log'
14+
import { CloudFunction } from '../../lib/function'
1415

1516

1617
/**

packages/cloud-function/copy2app.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
rm -rf ../app-service/node_modules/cloud-function-engine/dist
2+
rm -rf ../app-service/node_modules/cloud-function-engine/src
3+
cp -r dist ../app-service/node_modules/cloud-function-engine/dist
4+
cp -r src ../app-service/node_modules/cloud-function-engine/src

packages/cloud-function/copy2sys.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
rm -rf ../system-server/node_modules/cloud-function-engine/dist
2+
rm -rf ../system-server/node_modules/cloud-function-engine/src
3+
cp -r dist ../system-server/node_modules/cloud-function-engine/dist
4+
cp -r src ../system-server/node_modules/cloud-function-engine/src

packages/cloud-function/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
"scripts": {
3131
"build": "npx tsc -p tsconfig.json",
3232
"watch": "npx tsc -p tsconfig.json -w",
33-
"prepublishOnly": "npm run build"
33+
"prepublishOnly": "npm run build",
34+
"copy2app": "sh copy2app.sh",
35+
"copy2sys": "sh copy2sys.sh",
36+
"copy4dev": "sh copy2app.sh && sh copy2sys.sh"
3437
},
3538
"bugs": {
3639
"url": "https://github.com/Maslow/less-framework/issues"

packages/cloud-function/src/function.ts

-21
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ export class CloudFunction {
3838
*/
3939
result: FunctionResult
4040

41-
/**
42-
* 函数ID
43-
*/
44-
get id() {
45-
return this._data._id
46-
}
47-
4841
/**
4942
* 函数显示名称
5043
*/
@@ -66,20 +59,6 @@ export class CloudFunction {
6659
return this._data.compiledCode
6760
}
6861

69-
/**
70-
* 是否允许 HTTP 访问
71-
*/
72-
get enableHTTP() {
73-
return this._data.enableHTTP
74-
}
75-
76-
/**
77-
* 启停状态
78-
*/
79-
get status() {
80-
return this._data.status
81-
}
82-
8362
constructor(data: CloudFunctionStruct) {
8463
assert.ok(data)
8564
this._data = data

packages/cloud-function/src/types.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,24 @@ export interface FunctionResult {
5151
error?: Error,
5252
time_usage: number
5353
}
54-
5554
/**
5655
* 云函数的存储结构
5756
*/
5857
export interface CloudFunctionStruct {
59-
_id: string,
60-
name: string,
58+
/**
59+
* 云函数名
60+
*/
61+
name: string
62+
6163
/**
6264
* 云函数源代码,通常是 ts
6365
*/
64-
code: string,
66+
code: string
67+
6568
/**
6669
* 云函数编译后的代码,通常是 js
6770
*/
6871
compiledCode: string
69-
enableHTTP: boolean,
70-
status: number,
71-
created_by: number,
72-
created_at: number
73-
updated_at: number
7472
}
7573

7674
/** Object containing file metadata and access information. */

0 commit comments

Comments
 (0)