From c03a4b07924c429003990bfb3b0a6e59666eed69 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 23 Feb 2024 11:48:10 +0000 Subject: [PATCH] Addressing PR comments. --- packages/server/src/api/controllers/script.ts | 6 ++---- packages/server/src/jsRunner/index.ts | 1 - .../src/jsRunner/tests/isolatedVM.spec.ts | 19 +++++++++++++++---- .../src/jsRunner/tests/jsRunner.spec.ts | 2 +- .../tests/{marked.txt => largeJSExample.txt} | 0 packages/server/src/jsRunner/utilities.ts | 3 +++ .../server/src/jsRunner/vm/isolated-vm.ts | 11 ++++++----- packages/server/src/threads/query.ts | 3 ++- 8 files changed, 29 insertions(+), 16 deletions(-) rename packages/server/src/jsRunner/tests/{marked.txt => largeJSExample.txt} (100%) create mode 100644 packages/server/src/jsRunner/utilities.ts diff --git a/packages/server/src/api/controllers/script.ts b/packages/server/src/api/controllers/script.ts index 93e1ad7df9..b69fc430a6 100644 --- a/packages/server/src/api/controllers/script.ts +++ b/packages/server/src/api/controllers/script.ts @@ -1,13 +1,11 @@ import { Ctx } from "@budibase/types" import { IsolatedVM } from "../../jsRunner/vm" +import { iifeWrapper } from "../../jsRunner/utilities" export async function execute(ctx: Ctx) { const { script, context } = ctx.request.body const vm = new IsolatedVM() - const result = vm.withContext(context, () => - vm.execute(`(function(){\n${script}\n})();`) - ) - ctx.body = result + ctx.body = vm.withContext(context, () => vm.execute(iifeWrapper(script))) } export async function save(ctx: Ctx) { diff --git a/packages/server/src/jsRunner/index.ts b/packages/server/src/jsRunner/index.ts index 0fbfed3b66..0c9f5d9f01 100644 --- a/packages/server/src/jsRunner/index.ts +++ b/packages/server/src/jsRunner/index.ts @@ -7,7 +7,6 @@ import { } from "@budibase/string-templates" import { context, logging } from "@budibase/backend-core" import tracer from "dd-trace" - import { IsolatedVM } from "./vm" export function init() { diff --git a/packages/server/src/jsRunner/tests/isolatedVM.spec.ts b/packages/server/src/jsRunner/tests/isolatedVM.spec.ts index a396fa895e..5296598ef1 100644 --- a/packages/server/src/jsRunner/tests/isolatedVM.spec.ts +++ b/packages/server/src/jsRunner/tests/isolatedVM.spec.ts @@ -1,17 +1,21 @@ import fs from "fs" import path from "path" import { IsolatedVM } from "../vm" +import { iifeWrapper } from "../utilities" -function runJSWithIsolatedVM(script: string, context: any) { +function runJSWithIsolatedVM(script: string, context: Record) { const runner = new IsolatedVM() return runner.withContext(context, () => { - return runner.execute(`(function(){\n${script}\n})();`) + return runner.execute(iifeWrapper(script)) }) } describe("Test isolated vm directly", () => { it("should handle a very large file", () => { - const marked = fs.readFileSync(path.join(__dirname, "marked.txt"), "utf-8") + const marked = fs.readFileSync( + path.join(__dirname, "largeJSExample.txt"), + "utf-8" + ) const result = runJSWithIsolatedVM(marked, { trigger: { row: { Message: "dddd" } }, }) @@ -69,7 +73,14 @@ describe("Test isolated vm directly", () => { ) expect(result).toBeDefined() expect(result.length).toBe(1) - expect(result[0].imageLinks.length).toBe(6) + expect(result[0].imageLinks).toEqual([ + "https://budibase.com", + "_S/", + "https://budibase.com", + "https://budibase.com", + "https://budibase.com", + "https://budibase.com", + ]) }) it("should handle automation script example", () => { diff --git a/packages/server/src/jsRunner/tests/jsRunner.spec.ts b/packages/server/src/jsRunner/tests/jsRunner.spec.ts index ebe57b6ea3..54983aa470 100644 --- a/packages/server/src/jsRunner/tests/jsRunner.spec.ts +++ b/packages/server/src/jsRunner/tests/jsRunner.spec.ts @@ -74,7 +74,7 @@ describe("jsRunner (using isolated-vm)", () => { }) // the test cases here were extracted from templates/real world examples of JS in Budibase - describe("should real world tests of JS", () => { + describe("real test cases from Budicloud", () => { const context = { "Unit Value": 2, Quantity: 1, diff --git a/packages/server/src/jsRunner/tests/marked.txt b/packages/server/src/jsRunner/tests/largeJSExample.txt similarity index 100% rename from packages/server/src/jsRunner/tests/marked.txt rename to packages/server/src/jsRunner/tests/largeJSExample.txt diff --git a/packages/server/src/jsRunner/utilities.ts b/packages/server/src/jsRunner/utilities.ts new file mode 100644 index 0000000000..fa398ec239 --- /dev/null +++ b/packages/server/src/jsRunner/utilities.ts @@ -0,0 +1,3 @@ +export function iifeWrapper(script: string) { + return `(function(){\n${script}\n})();` +} diff --git a/packages/server/src/jsRunner/vm/isolated-vm.ts b/packages/server/src/jsRunner/vm/isolated-vm.ts index e5c431666d..b0692f0fd1 100644 --- a/packages/server/src/jsRunner/vm/isolated-vm.ts +++ b/packages/server/src/jsRunner/vm/isolated-vm.ts @@ -7,6 +7,7 @@ import querystring from "querystring" import { BundleType, loadBundle } from "../bundles" import { VM } from "@budibase/types" +import { iifeWrapper } from "../utilities" import environment from "../../environment" class ExecutionTimeoutError extends Error { @@ -118,11 +119,11 @@ export class IsolatedVM implements VM { // 3. Process script // 4. Stringify the result in order to convert the result from BSON to json this.codeWrapper = code => - `(function(){ - const data = bson.deserialize(bsonData, { validation: { utf8: false } }).data; - const result = ${code} - return bson.toJson(result); - })();` + iifeWrapper(` + const data = bson.deserialize(bsonData, { validation: { utf8: false } }).data; + const result = ${code} + return bson.toJson(result); + `) const bsonSource = loadBundle(BundleType.BSON) diff --git a/packages/server/src/threads/query.ts b/packages/server/src/threads/query.ts index 6cdccc7868..9d7b7062a5 100644 --- a/packages/server/src/threads/query.ts +++ b/packages/server/src/threads/query.ts @@ -8,6 +8,7 @@ import { QueryResponse, } from "./definitions" import { IsolatedVM } from "../jsRunner/vm" +import { iifeWrapper } from "../jsRunner/utilities" import { getIntegration } from "../integrations" import { processStringSync } from "@budibase/string-templates" import { context, cache, auth } from "@budibase/backend-core" @@ -127,7 +128,7 @@ class QueryRunner { // transform as required if (transformer) { - transformer = `(function(){\n${transformer}\n})();` + transformer = iifeWrapper(transformer) let vm = new IsolatedVM() if (datasource.source === SourceName.MONGODB) { vm = vm.withParsingBson(rows)