Use wrappers
This commit is contained in:
parent
4cabe612b1
commit
1367cf3636
3 changed files with 8 additions and 36 deletions
|
@ -1,10 +1,11 @@
|
||||||
import ScriptRunner from "../../utilities/scriptRunner"
|
|
||||||
import { Ctx } from "@budibase/types"
|
import { Ctx } from "@budibase/types"
|
||||||
|
import { VM2 } from "../../jsRunner/vm"
|
||||||
|
|
||||||
export async function execute(ctx: Ctx) {
|
export async function execute(ctx: Ctx) {
|
||||||
const { script, context } = ctx.request.body
|
const { script, context } = ctx.request.body
|
||||||
const runner = new ScriptRunner(script, context)
|
const runner = new VM2(context)
|
||||||
ctx.body = runner.execute()
|
const result = runner.execute(script)
|
||||||
|
ctx.body = result
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function save(ctx: Ctx) {
|
export async function save(ctx: Ctx) {
|
||||||
|
|
|
@ -7,7 +7,7 @@ import {
|
||||||
QueryVariable,
|
QueryVariable,
|
||||||
QueryResponse,
|
QueryResponse,
|
||||||
} from "./definitions"
|
} from "./definitions"
|
||||||
import ScriptRunner from "../utilities/scriptRunner"
|
import { VM2 } from "../jsRunner/vm"
|
||||||
import { getIntegration } from "../integrations"
|
import { getIntegration } from "../integrations"
|
||||||
import { processStringSync } from "@budibase/string-templates"
|
import { processStringSync } from "@budibase/string-templates"
|
||||||
import { context, cache, auth } from "@budibase/backend-core"
|
import { context, cache, auth } from "@budibase/backend-core"
|
||||||
|
@ -26,7 +26,7 @@ class QueryRunner {
|
||||||
fields: any
|
fields: any
|
||||||
parameters: any
|
parameters: any
|
||||||
pagination: any
|
pagination: any
|
||||||
transformer: any
|
transformer: string
|
||||||
cachedVariables: any[]
|
cachedVariables: any[]
|
||||||
ctx: any
|
ctx: any
|
||||||
queryResponse: any
|
queryResponse: any
|
||||||
|
@ -127,11 +127,11 @@ class QueryRunner {
|
||||||
|
|
||||||
// transform as required
|
// transform as required
|
||||||
if (transformer) {
|
if (transformer) {
|
||||||
const runner = new ScriptRunner(transformer, {
|
const runner = new VM2({
|
||||||
data: rows,
|
data: rows,
|
||||||
params: enrichedParameters,
|
params: enrichedParameters,
|
||||||
})
|
})
|
||||||
rows = runner.execute()
|
rows = runner.execute(transformer)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the request fails we retry once, invalidating the cached value
|
// if the request fails we retry once, invalidating the cached value
|
||||||
|
|
|
@ -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
|
|
Loading…
Reference in a new issue