1
0
Fork 0
mirror of synced 2024-10-05 12:34:50 +13:00

Use wrapper for scripts

This commit is contained in:
Adria Navarro 2024-02-08 00:54:03 +01:00
parent e4285e30f1
commit 008b39abf4
2 changed files with 12 additions and 6 deletions

View file

@ -104,6 +104,11 @@ export class IsolatedVM implements VM {
return this
}
withContext(context: Record<string, any>) {
this.#addToContext(context)
return this
}
execute(code: string): string {
const perRequestLimit = this.#perRequestLimit

View file

@ -4,17 +4,18 @@ import { IsolatedVM } from "../jsRunner/vm"
const JS_TIMEOUT_MS = 1000
class ScriptRunner {
#code
#vm
constructor(script: string, context: any) {
this.#code = `let fn = () => {\n${script}\n}; results.out = fn();`
this.#code = `(() => {${script}})();`
this.#vm = new IsolatedVM({
memoryLimit: env.JS_RUNNER_MEMORY_LIMIT,
timeout: JS_TIMEOUT_MS,
}).withContext(context)
}
execute() {
const vm = new IsolatedVM({
memoryLimit: env.JS_RUNNER_MEMORY_LIMIT,
timeout: JS_TIMEOUT_MS,
}).withHelpers()
const result = vm.execute(this.#code)
const result = this.#vm.execute(this.#code)
return result
}
}