Revert scriptRunner changes to use vm2
This commit is contained in:
parent
7ce9756d8c
commit
cc715b7722
|
@ -1,40 +1,28 @@
|
||||||
import tracer, { Span } from "dd-trace"
|
import fetch from "node-fetch"
|
||||||
import env from "../environment"
|
import { VM, VMScript } from "vm2"
|
||||||
import { IsolatedVM } from "../jsRunner/vm"
|
|
||||||
|
|
||||||
const JS_TIMEOUT_MS = 1000
|
const JS_TIMEOUT_MS = 1000
|
||||||
|
|
||||||
class ScriptRunner {
|
class ScriptRunner {
|
||||||
private code: string
|
vm: VM
|
||||||
private vm: IsolatedVM
|
results: { out: string }
|
||||||
|
script: VMScript
|
||||||
|
|
||||||
private tracerSpan: Span
|
constructor(script: string, context: any) {
|
||||||
|
const code = `let fn = () => {\n${script}\n}; results.out = fn();`
|
||||||
constructor(script: string, context: any, { parseBson = false } = {}) {
|
this.vm = new VM({
|
||||||
this.tracerSpan = tracer.startSpan("scriptRunner", { tags: { parseBson } })
|
timeout: JS_TIMEOUT_MS,
|
||||||
|
})
|
||||||
this.code = `(() => {${script}})();`
|
this.results = { out: "" }
|
||||||
this.vm = new IsolatedVM({
|
this.vm.setGlobals(context)
|
||||||
memoryLimit: env.JS_RUNNER_MEMORY_LIMIT,
|
this.vm.setGlobal("fetch", fetch)
|
||||||
invocationTimeout: JS_TIMEOUT_MS,
|
this.vm.setGlobal("results", this.results)
|
||||||
}).withContext(context)
|
this.script = new VMScript(code)
|
||||||
|
|
||||||
if (parseBson && context.data) {
|
|
||||||
this.vm = this.vm.withParsingBson(context.data)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
execute() {
|
execute() {
|
||||||
const result = tracer.trace(
|
this.vm.run(this.script)
|
||||||
"scriptRunner.execute",
|
return this.results.out
|
||||||
{ childOf: this.tracerSpan },
|
|
||||||
() => {
|
|
||||||
const result = this.vm.execute(this.code)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
)
|
|
||||||
this.tracerSpan.finish()
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue