Skip to content

Commit

Permalink
feat: 实现云函数 SDK 单独依赖包;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jul 24, 2021
1 parent 3fba6a9 commit e3d89b1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 9 deletions.
48 changes: 48 additions & 0 deletions src/lib/cloud-sdk/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { AxiosStatic } from "axios"
import { Db } from "less-api-database"
import { FunctionContext, FunctionResult } from "../faas/types"
import { FileStorageInterface } from "../storage/interface"
import * as mongodb from "mongodb"
import { Globals } from "../globals"
import { LocalFileStorage } from "../storage/local_file_storage"
import Config from "../../config"
import request from 'axios'
import { Scheduler } from "../scheduler"
import { CloudFunction } from "../faas"
import { getToken, parseToken } from "../utils/token"
import { invokeInFunction } from "./invoke"


export type RequireFuncType = (module: string) => any
export type InvokeFunctionType = (name: string, param: FunctionContext) => Promise<FunctionResult>
export type EmitFunctionType = (event: string, param: any) => void
export type GetTokenFunctionType = (payload: any) => string
export type ParseTokenFunctionType = (token: string) => any | null

export interface CloudSdkInterface {
fetch: AxiosStatic
storage(namespace: string): FileStorageInterface
database(): Db,
invoke: InvokeFunctionType
emit: EmitFunctionType
shared: Map<string, any>
getToken: GetTokenFunctionType
parseToken: ParseTokenFunctionType
mongodb: mongodb.Db
}


const cloud: CloudSdkInterface = {
database: () => Globals.createDb(),
storage: (namespace: string) => new LocalFileStorage(Config.LOCAL_STORAGE_ROOT_PATH, namespace),
fetch: request,
invoke: invokeInFunction,
emit: (event: string, param: any) => Scheduler.emit(event, param),
shared: CloudFunction._shared_preference,
getToken: getToken,
parseToken: parseToken,
mongodb: Globals.accessor.db
}


export default cloud
37 changes: 37 additions & 0 deletions src/lib/cloud-sdk/invoke.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getFunctionByName } from "../../api/function"
import { CloudFunction } from "../faas/function"
import { FunctionContext } from "../faas/types"


/**
* 在云函数中调用云函数,此函数运行于云函数中
* @param name 函数名
* @param param 函数运行参数
* @returns
*/
export async function invokeInFunction(name: string, param: FunctionContext) {
const data = await getFunctionByName(name)
const func = new CloudFunction(data)

if (!func) {
throw new Error(`invoke() failed to get function: ${name}`)
}

if(!func.compiledCode) {
func.compile2js()
}

param = param ?? {}

if(param.requestId) {
param.requestId = this.param.requestId
}

if (param.method) {
param.method = param.method ?? 'call'
}

const result = await func.invoke(param)

return result
}
3 changes: 3 additions & 0 deletions src/lib/faas/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { FunctionConsole } from './console'
import { CloudSdkInterface, FunctionContext, FunctionResult, RequireFuncType, RuntimeContext } from './types'

const require_func: RequireFuncType = (module): any => {
if(module === '@/cloud-sdk') {
return require('../cloud-sdk')
}
return require(module) as any
}

Expand Down
10 changes: 1 addition & 9 deletions src/lib/faas/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class CloudFunction {
database: () => Globals.createDb(),
storage: (namespace: string) => new LocalFileStorage(Config.LOCAL_STORAGE_ROOT_PATH, namespace),
fetch: request,
invoke: this.invokeInFunction.bind(this),
invoke: this.invokeInFunction,
emit: (event: string, param: any) => Scheduler.emit(event, param),
shared: CloudFunction._shared_preference,
getToken: getToken,
Expand Down Expand Up @@ -143,14 +143,6 @@ export class CloudFunction {
}

const result = await func.invoke(param)

// 将此执行日志并入父函数的日志中

const logs = result.logs
logs.unshift(`**** invoke function ${func.name} logs: ****`)
logs.push(`**** end invoke function ${func.name} ****`)
this.console.logs.push(...logs)

return result
}

Expand Down

0 comments on commit e3d89b1

Please sign in to comment.