Addressing PR comments.
This commit is contained in:
parent
6d49295524
commit
c03a4b0792
|
@ -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) {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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<string, any>) {
|
||||
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", () => {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
export function iifeWrapper(script: string) {
|
||||
return `(function(){\n${script}\n})();`
|
||||
}
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue