Create vm2 wrapper

This commit is contained in:
Adria Navarro 2024-02-19 16:14:01 +01:00
parent 598ebccc2c
commit 4cabe612b1
2 changed files with 27 additions and 0 deletions

View File

@ -1,2 +1,3 @@
export * from "./isolated-vm"
export * from "./builtin-vm"
export * from "./vm2"

View File

@ -0,0 +1,26 @@
import vm2 from "vm2"
import { VM } from "@budibase/types"
const JS_TIMEOUT_MS = 1000
export class VM2 implements VM {
vm: vm2.VM
results: { out: string }
constructor(context: any) {
this.vm = new vm2.VM({
timeout: JS_TIMEOUT_MS,
})
this.results = { out: "" }
this.vm.setGlobals(context)
this.vm.setGlobal("fetch", fetch)
this.vm.setGlobal("results", this.results)
}
execute(script: string) {
const code = `let fn = () => {\n${script}\n}; results.out = fn();`
const vmScript = new vm2.VMScript(code)
this.vm.run(vmScript)
return this.results.out
}
}