[PR #3619] [MERGED] refactor(scripting-revamp): migrate js-sandbox to web worker/Node vm based implementation #4452

Closed
opened 2026-03-17 01:59:18 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/hoppscotch/hoppscotch/pull/3619
Author: @jamesgeorge007
Created: 12/3/2023
Status: Merged
Merged: 12/7/2023
Merged by: @AndrewBastin

Base: release/2023.12.0Head: refactor/scripting-revamp-dep-migration


📝 Commits (10+)

  • 9a1bfc3 refactor: node VM based implementation
  • fd5b196 refactor: get started with the web worker based implementation
  • c817394 refactor: change in directory hierarchy
  • aac90fe chore: conditionally export modules
  • 15600a8 refactor: web worker based implementation for pre-request script
  • a6ce496 chore: dynamically import environment specific dependencies
  • 6d6a2ff chore: inline worker script for pre request script
  • 62a393b chore: iterations
  • 6f10a89 chore: move js-sandbox to a vite based build system
  • 9296aab chore: move to multiple entrypoints with subpath exports

📊 Changes

43 files changed (+2708 additions, -3188 deletions)

View changed files

📝 packages/hoppscotch-cli/jest.config.ts (+1 -1)
📝 packages/hoppscotch-cli/package.json (+2 -1)
📝 packages/hoppscotch-cli/src/__tests__/commands/test.spec.ts (+2 -3)
📝 packages/hoppscotch-cli/src/__tests__/samples/env-flag-envs.json (+0 -1)
📝 packages/hoppscotch-cli/src/__tests__/samples/env-flag-tests.json (+1 -1)
📝 packages/hoppscotch-cli/src/utils/pre-request.ts (+10 -9)
📝 packages/hoppscotch-cli/src/utils/test.ts (+9 -7)
📝 packages/hoppscotch-common/src/helpers/RequestRunner.ts (+21 -23)
📝 packages/hoppscotch-common/src/helpers/preRequest.ts (+6 -2)
packages/hoppscotch-js-sandbox/index.d.ts (+2 -0)
📝 packages/hoppscotch-js-sandbox/jest.config.js (+5 -1)
packages/hoppscotch-js-sandbox/node.d.ts (+2 -0)
📝 packages/hoppscotch-js-sandbox/package.json (+22 -10)
📝 packages/hoppscotch-js-sandbox/src/__tests__/preRequest.spec.ts (+6 -6)
📝 packages/hoppscotch-js-sandbox/src/__tests__/testing/envs/get.spec.ts (+4 -3)
📝 packages/hoppscotch-js-sandbox/src/__tests__/testing/envs/getResolve.spec.ts (+4 -3)
📝 packages/hoppscotch-js-sandbox/src/__tests__/testing/envs/resolve.spec.ts (+5 -3)
📝 packages/hoppscotch-js-sandbox/src/__tests__/testing/envs/set.spec.ts (+5 -3)
📝 packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBe.spec.ts (+5 -3)
📝 packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBeLevelxxx.spec.ts (+5 -3)

...and 23 more files

📄 Description

Description

This PR marks the initial step for the efforts that will be made towards the scripting revamp front. We're moving from quickjs-emscripten to a Web worker/Node VM-based implementation for js-sandbox.

Closes HFE-318.

Changes

  • Separate implementation based on environments for js-sandbox with subpath-based exports. The functions to execute pre-request and test scripts are exposed under subpaths after the respective environment, while types are accessible from the package root.

      import { runPreRequestScript, runTestScript } from "@hoppscotch/js-sandbox/web"
    
      import { runPreRequestScript, runTestScript } from "@hoppscotch/js-sandbox/node"
    
      import { TestDescriptor } from "@hoppscotch/js-sandbox"
    
    • The web worker-based implementation {pre-request,test-runner}/web-worker/*.ts includes a script compiling the main thread logic for communicating with the worker thread via the exposed function. The received arguments (script, envs, etc) from the place of invocation are supplied to the worker script residing relative to the main thread source, where the script gets executed via the Function constructor, and the results are sent back to the main thread which in turn is returned to the system.

    • The Node VM-based implementation {pre-request,test-runner}/node-vm/*.ts creates a new VM context via the createContext method, executes the script in a new context via the runInContext method and returns the relevant results.

    • getPreRequestScriptMethods & getTestRunnerScriptMethods functions under utils compiles the methods to be exposed under the pw namespace.

  • Move the js-sandbox build system from tsup to vite.

    • There are two entry points corresponding to the environment.
      • src/web.ts
      • src/node.ts
    • vite-plugin-dts ensures the declaration files are emitted.
  • The marshlObjecToVm function under utils has been made to prevent cyclic objects under the name preventCyclicObjects since there isn't a requirement for marshaling.

  • Enable CLI e2e tests on CI.

Checks

  • My pull request adheres to the code style of this project
  • All the tests have passed

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/hoppscotch/hoppscotch/pull/3619 **Author:** [@jamesgeorge007](https://github.com/jamesgeorge007) **Created:** 12/3/2023 **Status:** ✅ Merged **Merged:** 12/7/2023 **Merged by:** [@AndrewBastin](https://github.com/AndrewBastin) **Base:** `release/2023.12.0` ← **Head:** `refactor/scripting-revamp-dep-migration` --- ### 📝 Commits (10+) - [`9a1bfc3`](https://github.com/hoppscotch/hoppscotch/commit/9a1bfc383a8f7f8bfca6959dc78dbd7223b509b6) refactor: node VM based implementation - [`fd5b196`](https://github.com/hoppscotch/hoppscotch/commit/fd5b1964785a302a7b7f8374c97e852b966abbab) refactor: get started with the web worker based implementation - [`c817394`](https://github.com/hoppscotch/hoppscotch/commit/c817394d1919530c9ad2a45b7619ac5c20946df7) refactor: change in directory hierarchy - [`aac90fe`](https://github.com/hoppscotch/hoppscotch/commit/aac90fe0e1972c646da6d5e7618163bca30bd370) chore: conditionally export modules - [`15600a8`](https://github.com/hoppscotch/hoppscotch/commit/15600a8594099f23d19562055e501057c684b744) refactor: web worker based implementation for pre-request script - [`a6ce496`](https://github.com/hoppscotch/hoppscotch/commit/a6ce49611044182c6f0aa71638ac17060fc5f4f6) chore: dynamically import environment specific dependencies - [`6d6a2ff`](https://github.com/hoppscotch/hoppscotch/commit/6d6a2ff32bddcfd6edb147ba72e1d4f6def7923a) chore: inline worker script for pre request script - [`62a393b`](https://github.com/hoppscotch/hoppscotch/commit/62a393b993094d1fed16048c5f23cc3a2e1df46a) chore: iterations - [`6f10a89`](https://github.com/hoppscotch/hoppscotch/commit/6f10a895e0d9c2e5997970572bd252e7a8111d3c) chore: move js-sandbox to a vite based build system - [`9296aab`](https://github.com/hoppscotch/hoppscotch/commit/9296aabd3a46f57451d203e1eed2cb81e6fc2a15) chore: move to multiple entrypoints with subpath exports ### 📊 Changes **43 files changed** (+2708 additions, -3188 deletions) <details> <summary>View changed files</summary> 📝 `packages/hoppscotch-cli/jest.config.ts` (+1 -1) 📝 `packages/hoppscotch-cli/package.json` (+2 -1) 📝 `packages/hoppscotch-cli/src/__tests__/commands/test.spec.ts` (+2 -3) 📝 `packages/hoppscotch-cli/src/__tests__/samples/env-flag-envs.json` (+0 -1) 📝 `packages/hoppscotch-cli/src/__tests__/samples/env-flag-tests.json` (+1 -1) 📝 `packages/hoppscotch-cli/src/utils/pre-request.ts` (+10 -9) 📝 `packages/hoppscotch-cli/src/utils/test.ts` (+9 -7) 📝 `packages/hoppscotch-common/src/helpers/RequestRunner.ts` (+21 -23) 📝 `packages/hoppscotch-common/src/helpers/preRequest.ts` (+6 -2) ➕ `packages/hoppscotch-js-sandbox/index.d.ts` (+2 -0) 📝 `packages/hoppscotch-js-sandbox/jest.config.js` (+5 -1) ➕ `packages/hoppscotch-js-sandbox/node.d.ts` (+2 -0) 📝 `packages/hoppscotch-js-sandbox/package.json` (+22 -10) 📝 `packages/hoppscotch-js-sandbox/src/__tests__/preRequest.spec.ts` (+6 -6) 📝 `packages/hoppscotch-js-sandbox/src/__tests__/testing/envs/get.spec.ts` (+4 -3) 📝 `packages/hoppscotch-js-sandbox/src/__tests__/testing/envs/getResolve.spec.ts` (+4 -3) 📝 `packages/hoppscotch-js-sandbox/src/__tests__/testing/envs/resolve.spec.ts` (+5 -3) 📝 `packages/hoppscotch-js-sandbox/src/__tests__/testing/envs/set.spec.ts` (+5 -3) 📝 `packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBe.spec.ts` (+5 -3) 📝 `packages/hoppscotch-js-sandbox/src/__tests__/testing/expect/toBeLevelxxx.spec.ts` (+5 -3) _...and 23 more files_ </details> ### 📄 Description ### Description This PR marks the initial step for the efforts that will be made towards the [scripting revamp](https://github.com/hoppscotch/hoppscotch/discussions/2292) front. We're moving from [quickjs-emscripten](https://github.com/justjake/quickjs-emscripten) to a Web worker/Node VM-based implementation for `js-sandbox`. Closes HFE-318. ### Changes - Separate implementation based on environments for `js-sandbox` with subpath-based exports. The functions to execute pre-request and test scripts are exposed under subpaths after the respective environment, while types are accessible from the package root. ```ts import { runPreRequestScript, runTestScript } from "@hoppscotch/js-sandbox/web" ``` ```ts import { runPreRequestScript, runTestScript } from "@hoppscotch/js-sandbox/node" ``` ```ts import { TestDescriptor } from "@hoppscotch/js-sandbox" ``` - The web worker-based implementation `{pre-request,test-runner}/web-worker/*.ts` includes a script compiling the main thread logic for communicating with the worker thread via the exposed function. The received arguments (script, envs, etc) from the place of invocation are supplied to the worker script residing relative to the main thread source, where the script gets executed via the [Function constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function), and the results are sent back to the main thread which in turn is returned to the system. - The Node VM-based implementation `{pre-request,test-runner}/node-vm/*.ts` creates a new VM context via the [createContext](https://nodejs.org/api/vm.html#vmcreatecontextcontextobject-options) method, executes the script in a new context via the [runInContext](https://nodejs.org/api/vm.html#scriptrunincontextcontextifiedobject-options) method and returns the relevant results. - `getPreRequestScriptMethods` & `getTestRunnerScriptMethods` functions under `utils` compiles the methods to be exposed under the `pw` namespace. - Move the `js-sandbox` build system from `tsup` to `vite`. - There are two entry points corresponding to the environment. - `src/web.ts` - `src/node.ts` - [vite-plugin-dts](https://github.com/qmhc/vite-plugin-dts) ensures the declaration files are emitted. - The `marshlObjecToVm` function under `utils` has been made to prevent cyclic objects under the name `preventCyclicObjects` since there isn't a requirement for marshaling. - Enable CLI e2e tests on CI. ### Checks - [x] My pull request adheres to the code style of this project - [x] All the tests have passed --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-17 01:59:18 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hoppscotch#4452
No description provided.