From e3d89b196c10c72e3b08aa5a04fc1f52c169a7e9 Mon Sep 17 00:00:00 2001 From: maslow Date: Sat, 24 Jul 2021 19:43:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E4=BA=91=E5=87=BD?= =?UTF-8?q?=E6=95=B0=20SDK=20=E5=8D=95=E7=8B=AC=E4=BE=9D=E8=B5=96=E5=8C=85?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/cloud-sdk/index.ts | 48 +++++++++++++++++++++++++++++++++++++ src/lib/cloud-sdk/invoke.ts | 37 ++++++++++++++++++++++++++++ src/lib/faas/engine.ts | 3 +++ src/lib/faas/function.ts | 10 +------- 4 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 src/lib/cloud-sdk/index.ts create mode 100644 src/lib/cloud-sdk/invoke.ts diff --git a/src/lib/cloud-sdk/index.ts b/src/lib/cloud-sdk/index.ts new file mode 100644 index 0000000000..4df032df52 --- /dev/null +++ b/src/lib/cloud-sdk/index.ts @@ -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 +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 + 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 \ No newline at end of file diff --git a/src/lib/cloud-sdk/invoke.ts b/src/lib/cloud-sdk/invoke.ts new file mode 100644 index 0000000000..507a403ffa --- /dev/null +++ b/src/lib/cloud-sdk/invoke.ts @@ -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 +} \ No newline at end of file diff --git a/src/lib/faas/engine.ts b/src/lib/faas/engine.ts index d98abc2244..19a6eb3ffd 100644 --- a/src/lib/faas/engine.ts +++ b/src/lib/faas/engine.ts @@ -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 } diff --git a/src/lib/faas/function.ts b/src/lib/faas/function.ts index 5315959a20..ad9f3353b4 100644 --- a/src/lib/faas/function.ts +++ b/src/lib/faas/function.ts @@ -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, @@ -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 }