@giteeteam/apps-runtime-vm
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -6,8 +6,13 @@ import LRU from 'lru-cache'; | ||
lruOptions: LRU.Options<string, string>; | ||
maxIsolate?: number; | ||
logger?: any; | ||
} | ||
declare class IsolateSandbox { | ||
private memoryLimit; | ||
private codeCache; | ||
private isolate; | ||
constructor({ memoryLimit, lruOptions }: IsolateSandboxOptions); | ||
private jobs; | ||
private maxIsolate; | ||
private isProcessing; | ||
private logger; | ||
constructor({ memoryLimit, lruOptions, maxIsolate, logger }: IsolateSandboxOptions); | ||
/** | ||
@@ -24,4 +29,17 @@ * 插件运行 | ||
*/ | ||
execute({ fileDir, handler, params, runtimeApis, timeout, }: ExecuteParams): Promise<Record<string, unknown> | undefined | string>; | ||
execute(params: ExecuteParams): Promise<Record<string, unknown> | undefined | string>; | ||
/** | ||
* 执行插件代码 | ||
* @param {ExecuteParams} executeParams | ||
* @param {string} executeParams.fileDir 插件的代码文件夹 | ||
* @param {string} executeParams.handler 执行的function,例如:index.render | ||
* @param {Params} executeParams.params 执行function的参数 | ||
* @param {RuntimeApis} executeParams.runtimeApis vm运行时使用的api | ||
* @param {number} executeParams.timeout 执行的超时时间,单位毫秒 | ||
* @returns {(Promise<Record<string, unknown> | undefined | string>)} | ||
* @memberof IsolateSandbox | ||
*/ | ||
executeCode({ fileDir, handler, params, runtimeApis, timeout, }: ExecuteParams): Promise<Record<string, unknown> | undefined | string>; | ||
private check; | ||
/** | ||
* 执行入口函数 | ||
@@ -28,0 +46,0 @@ * @private |
@@ -15,6 +15,9 @@ "use strict"; | ||
class IsolateSandbox { | ||
constructor({ memoryLimit, lruOptions }) { | ||
this.memoryLimit = memoryLimit; | ||
constructor({ memoryLimit, lruOptions, maxIsolate, logger }) { | ||
this.codeCache = new lru_cache_1.default(lruOptions); | ||
this.isolate = new isolated_vm_1.default.Isolate({ memoryLimit: this.memoryLimit }); | ||
this.isolate = new isolated_vm_1.default.Isolate({ memoryLimit }); | ||
this.jobs = []; | ||
this.isProcessing = 0; | ||
this.maxIsolate = maxIsolate !== null && maxIsolate !== void 0 ? maxIsolate : 30; | ||
this.logger = logger !== null && logger !== void 0 ? logger : console.info; | ||
} | ||
@@ -32,4 +35,26 @@ /** | ||
*/ | ||
async execute({ fileDir, handler, params, runtimeApis, timeout, }) { | ||
async execute(params) { | ||
return new Promise((resolve, reject) => { | ||
this.jobs.push({ | ||
params, | ||
resolve, | ||
reject, | ||
}); | ||
this.check(); | ||
}); | ||
} | ||
/** | ||
* 执行插件代码 | ||
* @param {ExecuteParams} executeParams | ||
* @param {string} executeParams.fileDir 插件的代码文件夹 | ||
* @param {string} executeParams.handler 执行的function,例如:index.render | ||
* @param {Params} executeParams.params 执行function的参数 | ||
* @param {RuntimeApis} executeParams.runtimeApis vm运行时使用的api | ||
* @param {number} executeParams.timeout 执行的超时时间,单位毫秒 | ||
* @returns {(Promise<Record<string, unknown> | undefined | string>)} | ||
* @memberof IsolateSandbox | ||
*/ | ||
async executeCode({ fileDir, handler, params, runtimeApis, timeout, }) { | ||
try { | ||
this.isProcessing++; | ||
// 获取入口文件名称和执行函数 | ||
@@ -73,3 +98,24 @@ const [indexFilename, functionName] = handler.split('.'); | ||
} | ||
finally { | ||
this.isProcessing--; | ||
this.check(); | ||
} | ||
} | ||
async check() { | ||
this.logger('====check==== current Jobs', this.jobs.length, 'isProcessing', this.isProcessing); | ||
// 没有达到线程上限,可以执行 | ||
if (this.isProcessing < this.maxIsolate) { | ||
const job = this.jobs.shift(); | ||
if (!job) | ||
return; | ||
const { params, resolve, reject } = job; | ||
try { | ||
const result = await this.executeCode(params); | ||
resolve(result); | ||
} | ||
catch (e) { | ||
reject(e); | ||
} | ||
} | ||
} | ||
/** | ||
@@ -76,0 +122,0 @@ * 执行入口函数 |
{ | ||
"name": "@giteeteam/apps-runtime-vm", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Giteeteam Apps Runtime VM", | ||
@@ -41,3 +41,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "c95bb2d6eb738419f6f4f4f85b0d2419b20c4f4f" | ||
"gitHead": "d0e3fd6edd7390f59a1fd0c27ac593fef5af3625" | ||
} |
20584
518