1
0
Fork 0
mirror of synced 2024-07-15 11:15:59 +12:00

Use wrappers

This commit is contained in:
Adria Navarro 2024-02-19 16:17:19 +01:00
parent 4cabe612b1
commit 1367cf3636
3 changed files with 8 additions and 36 deletions

View file

@ -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) {

View file

@ -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

View file

@ -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