Use wrapper for scripts
This commit is contained in:
parent
e4285e30f1
commit
008b39abf4
|
@ -104,6 +104,11 @@ export class IsolatedVM implements VM {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
withContext(context: Record<string, any>) {
|
||||||
|
this.#addToContext(context)
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
execute(code: string): string {
|
execute(code: string): string {
|
||||||
const perRequestLimit = this.#perRequestLimit
|
const perRequestLimit = this.#perRequestLimit
|
||||||
|
|
||||||
|
|
|
@ -4,17 +4,18 @@ import { IsolatedVM } from "../jsRunner/vm"
|
||||||
const JS_TIMEOUT_MS = 1000
|
const JS_TIMEOUT_MS = 1000
|
||||||
class ScriptRunner {
|
class ScriptRunner {
|
||||||
#code
|
#code
|
||||||
|
#vm
|
||||||
|
|
||||||
constructor(script: string, context: any) {
|
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() {
|
execute() {
|
||||||
const vm = new IsolatedVM({
|
const result = this.#vm.execute(this.#code)
|
||||||
memoryLimit: env.JS_RUNNER_MEMORY_LIMIT,
|
|
||||||
timeout: JS_TIMEOUT_MS,
|
|
||||||
}).withHelpers()
|
|
||||||
const result = vm.execute(this.#code)
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue