@giteeteam/apps-runtime-vm
Advanced tools
Comparing version 0.1.9 to 0.1.10
@@ -15,3 +15,3 @@ import LRU from 'lru-cache'; | ||
* @param {string} executeParams.fileDir 插件的代码文件夹 | ||
* @param {string} executeParams.functionName 执行的function,例如:index.render | ||
* @param {string} executeParams.handler 执行的function,例如:index.render | ||
* @param {Params} executeParams.params 执行function的参数 | ||
@@ -23,3 +23,3 @@ * @param {RuntimeApis} executeParams.runtimeApis vm运行时使用的api | ||
*/ | ||
execute({ fileDir, functionName, params, runtimeApis, timeout, }: ExecuteParams): Promise<Record<string, unknown> | undefined | string>; | ||
execute({ fileDir, handler, params, runtimeApis, timeout, }: ExecuteParams): Promise<Record<string, unknown> | undefined | string>; | ||
/** | ||
@@ -26,0 +26,0 @@ * 执行入口函数 |
@@ -22,3 +22,3 @@ "use strict"; | ||
* @param {string} executeParams.fileDir 插件的代码文件夹 | ||
* @param {string} executeParams.functionName 执行的function,例如:index.render | ||
* @param {string} executeParams.handler 执行的function,例如:index.render | ||
* @param {Params} executeParams.params 执行function的参数 | ||
@@ -30,8 +30,7 @@ * @param {RuntimeApis} executeParams.runtimeApis vm运行时使用的api | ||
*/ | ||
async execute({ fileDir, functionName, params, runtimeApis, timeout, }) { | ||
async execute({ fileDir, handler, params, runtimeApis, timeout, }) { | ||
const isolate = new isolated_vm_1.default.Isolate({ memoryLimit: this.memoryLimit }); | ||
try { | ||
const names = functionName.split('.'); | ||
// 获取入口文件名称 | ||
const indexFilename = names[0]; | ||
// 获取入口文件名称和执行函数 | ||
const [indexFilename, functionName] = handler.split('.'); | ||
const filename = path_1.default.join(fileDir, `${indexFilename}.js`); | ||
@@ -45,3 +44,3 @@ const [context, appModule] = await Promise.all([ | ||
// 构建入口模块 | ||
const mainModule = await this.createMainModule(isolate, context, appModule, indexFilename, functionName); | ||
const mainModule = await this.createMainModule(isolate, context, appModule, functionName); | ||
// 执行函数并返回结果 | ||
@@ -114,4 +113,4 @@ return await this.runMainCode(context, mainModule, filename, timeout); | ||
*/ | ||
async createMainModule(isolate, context, appModule, indexFilename, functionName) { | ||
const mainModule = await isolate.compileModule(this.buildMainScript(indexFilename, functionName)); | ||
async createMainModule(isolate, context, appModule, functionName) { | ||
const mainModule = await isolate.compileModule(this.buildMainScript(functionName)); | ||
await (mainModule === null || mainModule === void 0 ? void 0 : mainModule.instantiate(context, async (specifier) => { | ||
@@ -134,7 +133,7 @@ if (specifier === 'app') { | ||
*/ | ||
buildMainScript(indexFilename, functionName) { | ||
buildMainScript(functionName) { | ||
// 使用了Promise.race来保证超时时间,因为isolated-vm自带的timeout只是执行时间的timeout,不包括idle时间 | ||
// https://github.com/laverdet/isolated-vm/issues/318 | ||
return ` | ||
import * as ${indexFilename} from 'app'; | ||
import * as app from 'app'; | ||
@@ -145,3 +144,3 @@ export async function main() { | ||
res = await Promise.race([ | ||
${functionName}(global.params), | ||
app.${functionName}(global.params), | ||
new Promise((resolve,reject) => setTimeout(() => reject('Script execution timed out.'), timeout)) | ||
@@ -148,0 +147,0 @@ ]) |
@@ -14,3 +14,3 @@ export declare type Params = Record<string, any>; | ||
fileDir: string; | ||
functionName: string; | ||
handler: string; | ||
params?: Params; | ||
@@ -17,0 +17,0 @@ runtimeApis: RuntimeApis; |
{ | ||
"name": "@giteeteam/apps-runtime-vm", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "Giteeteam Apps Runtime VM", | ||
@@ -41,3 +41,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "09a6b545da63b26d19524d653bf78a79c72de270" | ||
"gitHead": "58a417a70eced4f82ef5cece3c184a6acbbf3b87" | ||
} |
17550
439