From 1367cf36360da1ac8dd9f202be54ebfc956f0250 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 19 Feb 2024 16:17:19 +0100 Subject: [PATCH] Use wrappers --- packages/server/src/api/controllers/script.ts | 7 +++-- packages/server/src/threads/query.ts | 8 ++--- packages/server/src/utilities/scriptRunner.ts | 29 ------------------- 3 files changed, 8 insertions(+), 36 deletions(-) delete mode 100644 packages/server/src/utilities/scriptRunner.ts diff --git a/packages/server/src/api/controllers/script.ts b/packages/server/src/api/controllers/script.ts index d5b99d0733..f00383615e 100644 --- a/packages/server/src/api/controllers/script.ts +++ b/packages/server/src/api/controllers/script.ts @@ -1,10 +1,11 @@ -import ScriptRunner from "../../utilities/scriptRunner" import { Ctx } from "@budibase/types" +import { VM2 } from "../../jsRunner/vm" export async function execute(ctx: Ctx) { const { script, context } = ctx.request.body - const runner = new ScriptRunner(script, context) - ctx.body = runner.execute() + const runner = new VM2(context) + const result = runner.execute(script) + ctx.body = result } export async function save(ctx: Ctx) { diff --git a/packages/server/src/threads/query.ts b/packages/server/src/threads/query.ts index 6b11ce4759..429d058ef7 100644 --- a/packages/server/src/threads/query.ts +++ b/packages/server/src/threads/query.ts @@ -7,7 +7,7 @@ import { QueryVariable, QueryResponse, } from "./definitions" -import ScriptRunner from "../utilities/scriptRunner" +import { VM2 } from "../jsRunner/vm" import { getIntegration } from "../integrations" import { processStringSync } from "@budibase/string-templates" import { context, cache, auth } from "@budibase/backend-core" @@ -26,7 +26,7 @@ class QueryRunner { fields: any parameters: any pagination: any - transformer: any + transformer: string cachedVariables: any[] ctx: any queryResponse: any @@ -127,11 +127,11 @@ class QueryRunner { // transform as required if (transformer) { - const runner = new ScriptRunner(transformer, { + const runner = new VM2({ data: rows, params: enrichedParameters, }) - rows = runner.execute() + rows = runner.execute(transformer) } // if the request fails we retry once, invalidating the cached value diff --git a/packages/server/src/utilities/scriptRunner.ts b/packages/server/src/utilities/scriptRunner.ts deleted file mode 100644 index fee0215d2e..0000000000 --- a/packages/server/src/utilities/scriptRunner.ts +++ /dev/null @@ -1,29 +0,0 @@ -import fetch from "node-fetch" -import { VM, VMScript } from "vm2" - -const JS_TIMEOUT_MS = 1000 - -class ScriptRunner { - vm: VM - results: { out: string } - script: VMScript - - constructor(script: string, context: any) { - const code = `let fn = () => {\n${script}\n}; results.out = fn();` - this.vm = new VM({ - timeout: JS_TIMEOUT_MS, - }) - this.results = { out: "" } - this.vm.setGlobals(context) - this.vm.setGlobal("fetch", fetch) - this.vm.setGlobal("results", this.results) - this.script = new VMScript(code) - } - - execute() { - this.vm.run(this.script) - return this.results.out - } -} - -export default ScriptRunner