@giteeteam/apps-runtime-vm
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -38,3 +38,3 @@ "use strict"; | ||
// 获取函数执行的上下文:注入全局变量和api | ||
const context = await this.createContext(isolate, params, runtimeApis); | ||
const context = await this.createContext(isolate, { ...params, timeout }, runtimeApis); | ||
// 构建函数执行模块 | ||
@@ -129,2 +129,4 @@ const appModule = await this.createAppModule(isolate, filename); | ||
buildMainScript(indexFilename, functionName) { | ||
// 使用了Promise.race来保证超时时间,因为isolated-vm自带的timeout只是执行时间的timeout,不包括idle时间 | ||
// https://github.com/laverdet/isolated-vm/issues/318 | ||
return ` | ||
@@ -136,7 +138,11 @@ import * as ${indexFilename} from 'app'; | ||
try { | ||
res = await ${functionName}(global.params); | ||
res = await Promise.race([ | ||
${functionName}(global.params), | ||
new Promise((resolve,reject) => setTimeout(() => reject('Script execution timed out.'), timeout)) | ||
]) | ||
} catch (e) { | ||
res = { | ||
message: e.message, | ||
message: e.message ?? e, | ||
stack: e.stack, | ||
code: 500, | ||
} | ||
@@ -143,0 +149,0 @@ } |
@@ -13,3 +13,3 @@ export declare type Params = Record<string, any>; | ||
runtimeApis?: RuntimeApis; | ||
timeout?: number; | ||
timeout: number; | ||
} |
{ | ||
"name": "@giteeteam/apps-runtime-vm", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Giteeteam Apps Runtime VM", | ||
@@ -40,3 +40,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "b9c916cc5d6b3ebcc552c3ee63941d2a397af596" | ||
"gitHead": "63aa645e12ebcd0519c98fcf46e5ad6338a79c07" | ||
} |
19424
493