From 1abfba525384590ccfd4d62e8875108816514589 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 8 Jan 2024 09:42:54 +0000 Subject: [PATCH] WIP --- packages/server/package.json | 1 + packages/server/src/jsRunner.ts | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/server/package.json b/packages/server/package.json index 0fef624964..26e34221b1 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -15,6 +15,7 @@ "check:types": "tsc -p tsconfig.json --noEmit --paths null", "build:dev": "yarn prebuild && tsc --build --watch --preserveWatchOutput", "debug": "yarn build && node --expose-gc --inspect=9222 dist/index.js", + "jest": "NODE_OPTIONS=\"--no-node-snapshot $NODE_OPTIONS\" jest", "test": "bash scripts/test.sh", "test:memory": "jest --maxWorkers=2 --logHeapUsage --forceExit", "test:watch": "jest --watch", diff --git a/packages/server/src/jsRunner.ts b/packages/server/src/jsRunner.ts index 58fceaec1c..b8870f2492 100644 --- a/packages/server/src/jsRunner.ts +++ b/packages/server/src/jsRunner.ts @@ -1,4 +1,3 @@ -import vm from "vm" import ivm from "isolated-vm" import env from "./environment" import { setJSRunner } from "@budibase/string-templates" @@ -8,7 +7,7 @@ import tracer from "dd-trace" type TrackerFn = (f: () => T) => T export function init() { - setJSRunner((js: string, ctx: vm.Context) => { + setJSRunner((js: string, ctx: Record) => { return tracer.trace("runJS", {}, span => { const perRequestLimit = env.JS_PER_REQUEST_TIME_LIMIT_MS let track: TrackerFn = f => f() @@ -44,10 +43,14 @@ export function init() { } else { value = ctx[key] } - jail.setSync( - key, - new ivm.ExternalCopy(value).copyInto({ release: true }) - ) + + if (typeof value === "function") { + js = value.toString() + "\n" + js + continue + } else { + value = new ivm.ExternalCopy(value).copyInto({ release: true }) + } + jail.setSync(key, value) } const script = isolate.compileScriptSync(js)