diff --git a/packages/server/src/jsRunner/vm/index.ts b/packages/server/src/jsRunner/vm/index.ts index c05d9c03f2..3cdf05b873 100644 --- a/packages/server/src/jsRunner/vm/index.ts +++ b/packages/server/src/jsRunner/vm/index.ts @@ -104,6 +104,11 @@ export class IsolatedVM implements VM { return this } + withContext(context: Record) { + this.#addToContext(context) + return this + } + execute(code: string): string { const perRequestLimit = this.#perRequestLimit diff --git a/packages/server/src/utilities/scriptRunner.ts b/packages/server/src/utilities/scriptRunner.ts index c89d266b7e..597c4269ac 100644 --- a/packages/server/src/utilities/scriptRunner.ts +++ b/packages/server/src/utilities/scriptRunner.ts @@ -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 } }