refactor(scripting-revamp): migrate js-sandbox to web worker/Node vm based implementation (#3619)
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { execPreRequestScript } from "../preRequest"
|
||||
import { runPreRequestScript } from "~/pre-request/node-vm"
|
||||
import "@relmify/jest-fp-ts"
|
||||
|
||||
describe("execPreRequestScript", () => {
|
||||
test("returns the updated envirionment properly", () => {
|
||||
return expect(
|
||||
execPreRequestScript(
|
||||
runPreRequestScript(
|
||||
`
|
||||
pw.env.set("bob", "newbob")
|
||||
`,
|
||||
@@ -27,7 +27,7 @@ describe("execPreRequestScript", () => {
|
||||
|
||||
test("fails if the key is not a string", () => {
|
||||
return expect(
|
||||
execPreRequestScript(
|
||||
runPreRequestScript(
|
||||
`
|
||||
pw.env.set(10, "newbob")
|
||||
`,
|
||||
@@ -44,7 +44,7 @@ describe("execPreRequestScript", () => {
|
||||
|
||||
test("fails if the value is not a string", () => {
|
||||
return expect(
|
||||
execPreRequestScript(
|
||||
runPreRequestScript(
|
||||
`
|
||||
pw.env.set("bob", 10)
|
||||
`,
|
||||
@@ -61,7 +61,7 @@ describe("execPreRequestScript", () => {
|
||||
|
||||
test("fails for invalid syntax", () => {
|
||||
return expect(
|
||||
execPreRequestScript(
|
||||
runPreRequestScript(
|
||||
`
|
||||
pw.env.set("bob",
|
||||
`,
|
||||
@@ -78,7 +78,7 @@ describe("execPreRequestScript", () => {
|
||||
|
||||
test("creates new env variable if doesn't exist", () => {
|
||||
return expect(
|
||||
execPreRequestScript(
|
||||
runPreRequestScript(
|
||||
`
|
||||
pw.env.set("foo", "bar")
|
||||
`,
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import "@relmify/jest-fp-ts"
|
||||
import * as TE from "fp-ts/TaskEither"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import { execTestScript, TestResponse, TestResult } from "../../../test-runner"
|
||||
|
||||
import "@relmify/jest-fp-ts"
|
||||
import { runTestScript } from "~/test-runner/node-vm"
|
||||
import { TestResponse, TestResult } from "~/types"
|
||||
|
||||
const fakeResponse: TestResponse = {
|
||||
status: 200,
|
||||
@@ -12,7 +13,7 @@ const fakeResponse: TestResponse = {
|
||||
|
||||
const func = (script: string, envs: TestResult["envs"]) =>
|
||||
pipe(
|
||||
execTestScript(script, envs, fakeResponse),
|
||||
runTestScript(script, envs, fakeResponse),
|
||||
TE.map((x) => x.tests)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import "@relmify/jest-fp-ts"
|
||||
import * as TE from "fp-ts/TaskEither"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import { execTestScript, TestResponse, TestResult } from "../../../test-runner"
|
||||
|
||||
import "@relmify/jest-fp-ts"
|
||||
import { runTestScript } from "~/test-runner/node-vm"
|
||||
import { TestResponse, TestResult } from "~/types"
|
||||
|
||||
const fakeResponse: TestResponse = {
|
||||
status: 200,
|
||||
@@ -12,7 +13,7 @@ const fakeResponse: TestResponse = {
|
||||
|
||||
const func = (script: string, envs: TestResult["envs"]) =>
|
||||
pipe(
|
||||
execTestScript(script, envs, fakeResponse),
|
||||
runTestScript(script, envs, fakeResponse),
|
||||
TE.map((x) => x.tests)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { pipe } from "fp-ts/function"
|
||||
import * as TE from "fp-ts/TaskEither"
|
||||
import { execTestScript, TestResponse, TestResult } from "../../../test-runner"
|
||||
import { pipe } from "fp-ts/function"
|
||||
|
||||
import { runTestScript } from "~/test-runner/node-vm"
|
||||
import { TestResponse, TestResult } from "~/types"
|
||||
|
||||
const fakeResponse: TestResponse = {
|
||||
status: 200,
|
||||
@@ -10,7 +12,7 @@ const fakeResponse: TestResponse = {
|
||||
|
||||
const func = (script: string, envs: TestResult["envs"]) =>
|
||||
pipe(
|
||||
execTestScript(script, envs, fakeResponse),
|
||||
runTestScript(script, envs, fakeResponse),
|
||||
TE.map((x) => x.tests)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import * as TE from "fp-ts/TaskEither"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import { execTestScript, TestResponse, TestResult } from "../../../test-runner"
|
||||
|
||||
import { runTestScript } from "~/test-runner/node-vm"
|
||||
import { TestResponse, TestResult } from "~/types"
|
||||
|
||||
const fakeResponse: TestResponse = {
|
||||
status: 200,
|
||||
@@ -10,13 +12,13 @@ const fakeResponse: TestResponse = {
|
||||
|
||||
const func = (script: string, envs: TestResult["envs"]) =>
|
||||
pipe(
|
||||
execTestScript(script, envs, fakeResponse),
|
||||
runTestScript(script, envs, fakeResponse),
|
||||
TE.map((x) => x.envs)
|
||||
)
|
||||
|
||||
const funcTest = (script: string, envs: TestResult["envs"]) =>
|
||||
pipe(
|
||||
execTestScript(script, envs, fakeResponse),
|
||||
runTestScript(script, envs, fakeResponse),
|
||||
TE.map((x) => x.tests)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import "@relmify/jest-fp-ts"
|
||||
import * as TE from "fp-ts/TaskEither"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import { execTestScript, TestResponse } from "../../../test-runner"
|
||||
import "@relmify/jest-fp-ts"
|
||||
|
||||
import { runTestScript } from "~/test-runner/node-vm"
|
||||
import { TestResponse } from "~/types"
|
||||
|
||||
const fakeResponse: TestResponse = {
|
||||
status: 200,
|
||||
@@ -11,7 +13,7 @@ const fakeResponse: TestResponse = {
|
||||
|
||||
const func = (script: string, res: TestResponse) =>
|
||||
pipe(
|
||||
execTestScript(script, { global: [], selected: [] }, res),
|
||||
runTestScript(script, { global: [], selected: [] }, res),
|
||||
TE.map((x) => x.tests)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import "@relmify/jest-fp-ts"
|
||||
import * as TE from "fp-ts/TaskEither"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import { execTestScript, TestResponse } from "../../../test-runner"
|
||||
import "@relmify/jest-fp-ts"
|
||||
|
||||
import { runTestScript } from "~/test-runner/node-vm"
|
||||
import { TestResponse } from "~/types"
|
||||
|
||||
const fakeResponse: TestResponse = {
|
||||
status: 200,
|
||||
@@ -11,7 +13,7 @@ const fakeResponse: TestResponse = {
|
||||
|
||||
const func = (script: string, res: TestResponse) =>
|
||||
pipe(
|
||||
execTestScript(script, { global: [], selected: [] }, res),
|
||||
runTestScript(script, { global: [], selected: [] }, res),
|
||||
TE.map((x) => x.tests)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import * as TE from "fp-ts/TaskEither"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import { execTestScript, TestResponse } from "../../../test-runner"
|
||||
|
||||
import { runTestScript } from "~/test-runner/node-vm"
|
||||
import { TestResponse } from "~/types"
|
||||
|
||||
const fakeResponse: TestResponse = {
|
||||
status: 200,
|
||||
@@ -10,7 +12,7 @@ const fakeResponse: TestResponse = {
|
||||
|
||||
const func = (script: string, res: TestResponse) =>
|
||||
pipe(
|
||||
execTestScript(script, { global: [], selected: [] }, res),
|
||||
runTestScript(script, { global: [], selected: [] }, res),
|
||||
TE.map((x) => x.tests)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import * as TE from "fp-ts/TaskEither"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import { execTestScript, TestResponse } from "../../../test-runner"
|
||||
|
||||
import { runTestScript } from "~/test-runner/node-vm"
|
||||
import { TestResponse } from "~/types"
|
||||
|
||||
const fakeResponse: TestResponse = {
|
||||
status: 200,
|
||||
@@ -10,7 +12,7 @@ const fakeResponse: TestResponse = {
|
||||
|
||||
const func = (script: string, res: TestResponse) =>
|
||||
pipe(
|
||||
execTestScript(script, { global: [], selected: [] }, res),
|
||||
runTestScript(script, { global: [], selected: [] }, res),
|
||||
TE.map((x) => x.tests)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import * as TE from "fp-ts/TaskEither"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import { execTestScript, TestResponse } from "../../../test-runner"
|
||||
|
||||
import { runTestScript } from "~/test-runner/node-vm"
|
||||
import { TestResponse } from "~/types"
|
||||
|
||||
const fakeResponse: TestResponse = {
|
||||
status: 200,
|
||||
@@ -10,7 +12,7 @@ const fakeResponse: TestResponse = {
|
||||
|
||||
const func = (script: string, res: TestResponse) =>
|
||||
pipe(
|
||||
execTestScript(script, { global: [], selected: [] }, res),
|
||||
runTestScript(script, { global: [], selected: [] }, res),
|
||||
TE.map((x) => x.tests)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import * as TE from "fp-ts/TaskEither"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import { execTestScript, TestResponse } from "../../test-runner"
|
||||
|
||||
import { runTestScript } from "~/test-runner/node-vm"
|
||||
import { TestResponse } from "~/types"
|
||||
|
||||
const fakeResponse: TestResponse = {
|
||||
status: 200,
|
||||
@@ -10,7 +12,7 @@ const fakeResponse: TestResponse = {
|
||||
|
||||
const func = (script: string, res: TestResponse) =>
|
||||
pipe(
|
||||
execTestScript(script, { global: [], selected: [] }, res),
|
||||
runTestScript(script, { global: [], selected: [] }, res),
|
||||
TE.map((x) => x.tests)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,40 +1,15 @@
|
||||
import { match } from "fp-ts/lib/Either"
|
||||
import { pipe } from "fp-ts/lib/function"
|
||||
import * as QuickJS from "quickjs-emscripten"
|
||||
import { marshalObjectToVM } from "../utils"
|
||||
import { preventCyclicObjects } from "~/utils"
|
||||
|
||||
let vm: QuickJS.QuickJSVm
|
||||
|
||||
beforeAll(async () => {
|
||||
const qjs = await QuickJS.getQuickJS()
|
||||
vm = qjs.createVm()
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
vm.dispose()
|
||||
})
|
||||
|
||||
describe("marshalObjectToVM", () => {
|
||||
test("successfully marshals simple object into the vm", () => {
|
||||
describe("preventCyclicObjects", () => {
|
||||
test("succeeds with a simple object", () => {
|
||||
const testObj = {
|
||||
a: 1,
|
||||
}
|
||||
|
||||
const objVMHandle: QuickJS.QuickJSHandle | null = pipe(
|
||||
marshalObjectToVM(vm, testObj),
|
||||
match(
|
||||
() => null,
|
||||
(result) => result
|
||||
)
|
||||
)
|
||||
|
||||
expect(objVMHandle).not.toBeNull()
|
||||
expect(vm.dump(objVMHandle!)).toEqual(testObj)
|
||||
|
||||
objVMHandle!.dispose()
|
||||
expect(preventCyclicObjects(testObj)).toBeRight()
|
||||
})
|
||||
|
||||
test("fails marshalling cyclic object into vm", () => {
|
||||
test("fails with a cyclic object", () => {
|
||||
const testObj = {
|
||||
a: 1,
|
||||
b: null as any,
|
||||
@@ -42,14 +17,6 @@ describe("marshalObjectToVM", () => {
|
||||
|
||||
testObj.b = testObj
|
||||
|
||||
const objVMHandle: QuickJS.QuickJSHandle | null = pipe(
|
||||
marshalObjectToVM(vm, testObj),
|
||||
match(
|
||||
() => null,
|
||||
(result) => result
|
||||
)
|
||||
)
|
||||
|
||||
expect(objVMHandle).toBeNull()
|
||||
expect(preventCyclicObjects(testObj)).toBeLeft()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user