Remove vm2 wrapper
This commit is contained in:
parent
40aff20af9
commit
b9afe1b926
|
@ -1,3 +1,2 @@
|
||||||
export * from "./isolated-vm"
|
export * from "./isolated-vm"
|
||||||
export * from "./builtin-vm"
|
export * from "./builtin-vm"
|
||||||
export * from "./vm2"
|
|
||||||
|
|
|
@ -1,36 +0,0 @@
|
||||||
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() {
|
|
||||||
this.vm = new vm2.VM({
|
|
||||||
timeout: JS_TIMEOUT_MS,
|
|
||||||
})
|
|
||||||
this.results = { out: "" }
|
|
||||||
this.vm.setGlobal("fetch", fetch)
|
|
||||||
this.vm.setGlobal("results", this.results)
|
|
||||||
}
|
|
||||||
|
|
||||||
withContext<T>(context: Record<string, any>, executeWithContext: () => T): T {
|
|
||||||
this.vm.setGlobals(context)
|
|
||||||
try {
|
|
||||||
return executeWithContext()
|
|
||||||
} finally {
|
|
||||||
for (const key in context) {
|
|
||||||
this.vm.setGlobal(key, undefined)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -7,20 +7,18 @@ import {
|
||||||
QueryVariable,
|
QueryVariable,
|
||||||
QueryResponse,
|
QueryResponse,
|
||||||
} from "./definitions"
|
} from "./definitions"
|
||||||
import { IsolatedVM, VM2 } from "../jsRunner/vm"
|
import { IsolatedVM } 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"
|
||||||
import { getGlobalIDFromUserMetadataID } from "../db/utils"
|
import { getGlobalIDFromUserMetadataID } from "../db/utils"
|
||||||
import sdk from "../sdk"
|
import sdk from "../sdk"
|
||||||
import { cloneDeep } from "lodash/fp"
|
import { cloneDeep } from "lodash/fp"
|
||||||
import { Datasource, Query, SourceName, VM } from "@budibase/types"
|
import { Datasource, Query, SourceName } from "@budibase/types"
|
||||||
|
|
||||||
import { isSQL } from "../integrations/utils"
|
import { isSQL } from "../integrations/utils"
|
||||||
import { interpolateSQL } from "../integrations/queries/sql"
|
import { interpolateSQL } from "../integrations/queries/sql"
|
||||||
|
|
||||||
const USE_ISOLATED_VM = true
|
|
||||||
|
|
||||||
class QueryRunner {
|
class QueryRunner {
|
||||||
datasource: Datasource
|
datasource: Datasource
|
||||||
queryVerb: string
|
queryVerb: string
|
||||||
|
@ -129,23 +127,17 @@ class QueryRunner {
|
||||||
|
|
||||||
// transform as required
|
// transform as required
|
||||||
if (transformer) {
|
if (transformer) {
|
||||||
let runner: VM
|
transformer = `(function(){\n${transformer}\n})();`
|
||||||
if (!USE_ISOLATED_VM) {
|
let vm = new IsolatedVM()
|
||||||
runner = new VM2()
|
if (datasource.source === SourceName.MONGODB) {
|
||||||
} else {
|
vm = vm.withParsingBson(rows)
|
||||||
transformer = `(function(){\n${transformer}\n})();`
|
|
||||||
let vm = new IsolatedVM()
|
|
||||||
if (datasource.source === SourceName.MONGODB) {
|
|
||||||
vm = vm.withParsingBson(rows)
|
|
||||||
}
|
|
||||||
runner = vm
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ctx = {
|
const ctx = {
|
||||||
data: rows,
|
data: rows,
|
||||||
params: enrichedParameters,
|
params: enrichedParameters,
|
||||||
}
|
}
|
||||||
rows = runner.withContext(ctx, () => runner.execute(transformer))
|
rows = vm.withContext(ctx, () => vm.execute(transformer))
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the request fails we retry once, invalidating the cached value
|
// if the request fails we retry once, invalidating the cached value
|
||||||
|
|
Loading…
Reference in New Issue