Addressing PR comments.
This commit is contained in:
parent
6d49295524
commit
c03a4b0792
|
@ -1,13 +1,11 @@
|
||||||
import { Ctx } from "@budibase/types"
|
import { Ctx } from "@budibase/types"
|
||||||
import { IsolatedVM } from "../../jsRunner/vm"
|
import { IsolatedVM } from "../../jsRunner/vm"
|
||||||
|
import { iifeWrapper } from "../../jsRunner/utilities"
|
||||||
|
|
||||||
export async function execute(ctx: Ctx) {
|
export async function execute(ctx: Ctx) {
|
||||||
const { script, context } = ctx.request.body
|
const { script, context } = ctx.request.body
|
||||||
const vm = new IsolatedVM()
|
const vm = new IsolatedVM()
|
||||||
const result = vm.withContext(context, () =>
|
ctx.body = vm.withContext(context, () => vm.execute(iifeWrapper(script)))
|
||||||
vm.execute(`(function(){\n${script}\n})();`)
|
|
||||||
)
|
|
||||||
ctx.body = result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function save(ctx: Ctx) {
|
export async function save(ctx: Ctx) {
|
||||||
|
|
|
@ -7,7 +7,6 @@ import {
|
||||||
} from "@budibase/string-templates"
|
} from "@budibase/string-templates"
|
||||||
import { context, logging } from "@budibase/backend-core"
|
import { context, logging } from "@budibase/backend-core"
|
||||||
import tracer from "dd-trace"
|
import tracer from "dd-trace"
|
||||||
|
|
||||||
import { IsolatedVM } from "./vm"
|
import { IsolatedVM } from "./vm"
|
||||||
|
|
||||||
export function init() {
|
export function init() {
|
||||||
|
|
|
@ -1,17 +1,21 @@
|
||||||
import fs from "fs"
|
import fs from "fs"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import { IsolatedVM } from "../vm"
|
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()
|
const runner = new IsolatedVM()
|
||||||
return runner.withContext(context, () => {
|
return runner.withContext(context, () => {
|
||||||
return runner.execute(`(function(){\n${script}\n})();`)
|
return runner.execute(iifeWrapper(script))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("Test isolated vm directly", () => {
|
describe("Test isolated vm directly", () => {
|
||||||
it("should handle a very large file", () => {
|
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, {
|
const result = runJSWithIsolatedVM(marked, {
|
||||||
trigger: { row: { Message: "dddd" } },
|
trigger: { row: { Message: "dddd" } },
|
||||||
})
|
})
|
||||||
|
@ -69,7 +73,14 @@ describe("Test isolated vm directly", () => {
|
||||||
)
|
)
|
||||||
expect(result).toBeDefined()
|
expect(result).toBeDefined()
|
||||||
expect(result.length).toBe(1)
|
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", () => {
|
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
|
// 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 = {
|
const context = {
|
||||||
"Unit Value": 2,
|
"Unit Value": 2,
|
||||||
Quantity: 1,
|
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 { BundleType, loadBundle } from "../bundles"
|
||||||
import { VM } from "@budibase/types"
|
import { VM } from "@budibase/types"
|
||||||
|
import { iifeWrapper } from "../utilities"
|
||||||
import environment from "../../environment"
|
import environment from "../../environment"
|
||||||
|
|
||||||
class ExecutionTimeoutError extends Error {
|
class ExecutionTimeoutError extends Error {
|
||||||
|
@ -118,11 +119,11 @@ export class IsolatedVM implements VM {
|
||||||
// 3. Process script
|
// 3. Process script
|
||||||
// 4. Stringify the result in order to convert the result from BSON to json
|
// 4. Stringify the result in order to convert the result from BSON to json
|
||||||
this.codeWrapper = code =>
|
this.codeWrapper = code =>
|
||||||
`(function(){
|
iifeWrapper(`
|
||||||
const data = bson.deserialize(bsonData, { validation: { utf8: false } }).data;
|
const data = bson.deserialize(bsonData, { validation: { utf8: false } }).data;
|
||||||
const result = ${code}
|
const result = ${code}
|
||||||
return bson.toJson(result);
|
return bson.toJson(result);
|
||||||
})();`
|
`)
|
||||||
|
|
||||||
const bsonSource = loadBundle(BundleType.BSON)
|
const bsonSource = loadBundle(BundleType.BSON)
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import {
|
||||||
QueryResponse,
|
QueryResponse,
|
||||||
} from "./definitions"
|
} from "./definitions"
|
||||||
import { IsolatedVM } from "../jsRunner/vm"
|
import { IsolatedVM } from "../jsRunner/vm"
|
||||||
|
import { iifeWrapper } from "../jsRunner/utilities"
|
||||||
import { getIntegration } from "../integrations"
|
import { getIntegration } from "../integrations"
|
||||||
import { processStringSync } from "@budibase/string-templates"
|
import { processStringSync } from "@budibase/string-templates"
|
||||||
import { context, cache, auth } from "@budibase/backend-core"
|
import { context, cache, auth } from "@budibase/backend-core"
|
||||||
|
@ -127,7 +128,7 @@ class QueryRunner {
|
||||||
|
|
||||||
// transform as required
|
// transform as required
|
||||||
if (transformer) {
|
if (transformer) {
|
||||||
transformer = `(function(){\n${transformer}\n})();`
|
transformer = iifeWrapper(transformer)
|
||||||
let vm = new IsolatedVM()
|
let vm = new IsolatedVM()
|
||||||
if (datasource.source === SourceName.MONGODB) {
|
if (datasource.source === SourceName.MONGODB) {
|
||||||
vm = vm.withParsingBson(rows)
|
vm = vm.withParsingBson(rows)
|
||||||
|
|
Loading…
Reference in New Issue