From 73d39836ab480deb555ca162268625665224d69a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 20 Feb 2024 12:10:49 +0100 Subject: [PATCH] Remove vm wrapper --- packages/server/src/jsRunner/index.ts | 9 +-- packages/server/src/jsRunner/vm/builtin-vm.ts | 76 ------------------- packages/server/src/jsRunner/vm/index.ts | 1 - 3 files changed, 1 insertion(+), 85 deletions(-) delete mode 100644 packages/server/src/jsRunner/vm/builtin-vm.ts diff --git a/packages/server/src/jsRunner/index.ts b/packages/server/src/jsRunner/index.ts index 3b6b464dae..0fbfed3b66 100644 --- a/packages/server/src/jsRunner/index.ts +++ b/packages/server/src/jsRunner/index.ts @@ -8,18 +8,11 @@ import { import { context, logging } from "@budibase/backend-core" import tracer from "dd-trace" -import { BuiltInVM, IsolatedVM } from "./vm" - -const USE_ISOLATED_VM = true +import { IsolatedVM } from "./vm" export function init() { setJSRunner((js: string, ctx: Record) => { return tracer.trace("runJS", {}, span => { - if (!USE_ISOLATED_VM) { - const vm = new BuiltInVM(ctx, span) - return vm.execute(js) - } - try { const bbCtx = context.getCurrentContext() diff --git a/packages/server/src/jsRunner/vm/builtin-vm.ts b/packages/server/src/jsRunner/vm/builtin-vm.ts deleted file mode 100644 index 849abdd6f2..0000000000 --- a/packages/server/src/jsRunner/vm/builtin-vm.ts +++ /dev/null @@ -1,76 +0,0 @@ -import vm from "vm" -import env from "../../environment" -import { context, timers } from "@budibase/backend-core" -import tracer, { Span } from "dd-trace" -import { VM } from "@budibase/types" - -type TrackerFn = (f: () => T) => T - -export class BuiltInVM implements VM { - private ctx: vm.Context - private span?: Span - - constructor(ctx: vm.Context, span?: Span) { - this.ctx = ctx - this.span = span - } - - withContext(context: Record, executeWithContext: () => T): T { - this.ctx = vm.createContext(context) - try { - return executeWithContext() - } finally { - for (const key in context) { - delete this.ctx[key] - } - } - } - - execute(code: string) { - const perRequestLimit = env.JS_PER_REQUEST_TIMEOUT_MS - let track: TrackerFn = f => f() - if (perRequestLimit) { - const bbCtx = tracer.trace("runJS.getCurrentContext", {}, span => - context.getCurrentContext() - ) - if (bbCtx) { - if (!bbCtx.jsExecutionTracker) { - this.span?.addTags({ - createdExecutionTracker: true, - }) - bbCtx.jsExecutionTracker = tracer.trace( - "runJS.createExecutionTimeTracker", - {}, - span => timers.ExecutionTimeTracker.withLimit(perRequestLimit) - ) - } - this.span?.addTags({ - js: { - limitMS: bbCtx.jsExecutionTracker.limitMs, - elapsedMS: bbCtx.jsExecutionTracker.elapsedMS, - }, - }) - // We call checkLimit() here to prevent paying the cost of creating - // a new VM context below when we don't need to. - tracer.trace("runJS.checkLimitAndBind", {}, span => { - bbCtx.jsExecutionTracker!.checkLimit() - track = bbCtx.jsExecutionTracker!.track.bind(bbCtx.jsExecutionTracker) - }) - } - } - - this.ctx = { - ...this.ctx, - alert: undefined, - setInterval: undefined, - setTimeout: undefined, - } - - vm.createContext(this.ctx) - return track(() => - vm.runInNewContext(code, this.ctx, { - timeout: env.JS_PER_INVOCATION_TIMEOUT_MS, - }) - ) - } -} diff --git a/packages/server/src/jsRunner/vm/index.ts b/packages/server/src/jsRunner/vm/index.ts index cc50a5eeaa..286a277cfb 100644 --- a/packages/server/src/jsRunner/vm/index.ts +++ b/packages/server/src/jsRunner/vm/index.ts @@ -1,2 +1 @@ export * from "./isolated-vm" -export * from "./builtin-vm"