-
-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters