Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

feat(js-sandbox): expose `atob` & `btoa` functions for Node.js (#3724)

Florian Metz0028f6e87877
diff --git a/packages/hoppscotch-common/src/helpers/terndoc/ecma.json b/packages/hoppscotch-common/src/helpers/terndoc/ecma.json
index 9262de7d6..a916755ba 100644
--- a/packages/hoppscotch-common/src/helpers/terndoc/ecma.json
+++ b/packages/hoppscotch-common/src/helpers/terndoc/ecma.json
@@ -1256,5 +1256,11 @@
"!type": "fn(value: ?) -> bool"
}
}
+ },
+ "btoa": {
+ "!type": "fn(data: string) -> string"
+ },
+ "atob": {
+ "!type": "fn(data: string) -> string"
}
}
diff --git a/packages/hoppscotch-js-sandbox/src/__tests__/testing/base64-helper-functions.spec.ts b/packages/hoppscotch-js-sandbox/src/__tests__/testing/base64-helper-functions.spec.ts
new file mode 100644
index 000000000..8e54af0d9
--- /dev/null
+++ b/packages/hoppscotch-js-sandbox/src/__tests__/testing/base64-helper-functions.spec.ts
@@ -0,0 +1,85 @@
+import * as TE from "fp-ts/TaskEither"
+import { pipe } from "fp-ts/function"
+
+import { runPreRequestScript } from "~/pre-request/node-vm"
+import { runTestScript } from "~/test-runner/node-vm"
+import { TestResponse, TestResult } from "~/types"
+
+describe("Base64 helper functions", () => {
+ const scriptExpectations = {
+ atob: {
+ script: `pw.env.set("atob", atob("SGVsbG8gV29ybGQ="))`,
+ environment: {
+ selected: [{ key: "atob", value: "Hello World" }],
+ },
+ },
+ btoa: {
+ script: `pw.env.set("btoa", btoa("Hello World"))`,
+ environment: {
+ selected: [{ key: "btoa", value: "SGVsbG8gV29ybGQ=" }],
+ },
+ },
+ }
+
+ describe("Pre-request script", () => {
+ describe("atob", () => {
+ test("successfully decodes the input string", () => {
+ return expect(
+ runPreRequestScript(scriptExpectations.atob.script, {
+ global: [],
+ selected: [],
+ })()
+ ).resolves.toEqualRight(
+ expect.objectContaining(scriptExpectations.atob.environment)
+ )
+ })
+ })
+
+ describe("btoa", () => {
+ test("successfully encodes the input string", () => {
+ return expect(
+ runPreRequestScript(scriptExpectations.btoa.script, {
+ global: [],
+ selected: [],
+ })()
+ ).resolves.toEqualRight(
+ expect.objectContaining(scriptExpectations.btoa.environment)
+ )
+ })
+ })
+ })
+
+ describe("Test script", () => {
+ const fakeResponse: TestResponse = {
+ status: 200,
+ body: "hoi",
+ headers: [],
+ }
+
+ const func = (script: string, envs: TestResult["envs"]) =>
+ pipe(
+ runTestScript(script, envs, fakeResponse),
+ TE.map((x) => x.envs)
+ )
+
+ describe("atob", () => {
+ test("successfully decodes the input string", () => {
+ return expect(
+ func(scriptExpectations.atob.script, { global: [], selected: [] })()
+ ).resolves.toEqualRight(
+ expect.objectContaining(scriptExpectations.atob.environment)
+ )
+ })
+ })
+
+ describe("btoa", () => {
+ test("successfully encodes the input string", () => {
+ return expect(
+ func(scriptExpectations.btoa.script, { global: [], selected: [] })()
+ ).resolves.toEqualRight(
+ expect.objectContaining(scriptExpectations.btoa.environment)
+ )
+ })
+ })
+ })
+})
diff --git a/packages/hoppscotch-js-sandbox/src/pre-request/node-vm/index.ts b/packages/hoppscotch-js-sandbox/src/pre-request/node-vm/index.ts
index f37b610cd..220a18d77 100644
--- a/packages/hoppscotch-js-sandbox/src/pre-request/node-vm/index.ts
+++ b/packages/hoppscotch-js-sandbox/src/pre-request/node-vm/index.ts
@@ -24,6 +24,8 @@ export const runPreRequestScript = (
// Expose pw to the context
context.pw = pw
+ context.atob = atob
+ context.btoa = btoa
// Run the pre-request script in the provided context
runInContext(preRequestScript, context)
diff --git a/packages/hoppscotch-js-sandbox/src/test-runner/node-vm/index.ts b/packages/hoppscotch-js-sandbox/src/test-runner/node-vm/index.ts
index ce96acb13..89d5a8f68 100644
--- a/packages/hoppscotch-js-sandbox/src/test-runner/node-vm/index.ts
+++ b/packages/hoppscotch-js-sandbox/src/test-runner/node-vm/index.ts
@@ -43,6 +43,8 @@ const executeScriptInContext = (
// Expose pw to the context
context.pw = { ...pw, response: responseObjHandle.right }
+ context.atob = atob
+ context.btoa = btoa
// Run the test script in the provided context
runInContext(testScript, context)