UVM (Universal Virtual Machine) is a JavaScript library designed to provide a consistent API for executing code in isolated contexts, leveraging Node.js Worker Threads and Web Workers in browser environments. This abstraction simplifies cross-context communication via an event emitter-based bridge, enabling developers to run potentially untrusted or computationally intensive code without blocking the main thread. Currently at version 4.0.1 (last published 10 months ago, as of current date), the library appears to be actively maintained. Its key differentiator lies in offering a 'universal' approach to sandboxed code execution, providing a unified development experience across different JavaScript runtimes, making it suitable for applications requiring secure and efficient off-main-thread processing. The project originates from Postman Labs.
npm install uvmVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create an isolated UVM context, send data to it, receive data back using an event emitter bridge, and handle basic lifecycle events including disconnection for resource management. It showcases cross-context communication between the main process/thread and the UVM sandbox.
Review the official GitHub release notes for your target version. Update import statements (`require` to `import`) and ensure your Node.js environment meets the `engines.node` requirement (>=18 for v4.x). Verify data serialization for cross-context communication.
Ensure all data passed via `bridge.dispatch` or in `bootCode` options consists of primitive values, plain objects, arrays, or other structured clone algorithm compatible types. Avoid passing functions, DOM elements, or class instances directly.
Explicitly pass any necessary configuration, initial data, or environment variables to the UVM context via the `spawn` options or by dispatching events after the context has booted. Understand that functions and objects cannot be directly shared, only their serializable data representations.
For highly critical security requirements, consider additional layers of security like running worker processes with restrictive permissions, using containerization, or specialized sandboxing solutions (e.g., `vm2`, though it has also faced vulnerabilities). Do not solely rely on UVM for absolute security against untrusted code.
If your project is ESM, change `const uvm = require('uvm');` to `import uvm from 'uvm';`. Ensure your `package.json` has `"type": "module"` or use `.mjs` file extensions for ESM.Before dispatching data across the `bridge`, ensure that the data is structured clone algorithm compatible. Convert complex objects into plain JavaScript objects (POJOs), serialize functions to strings, or extract only necessary primitive values.
Ensure that `bridge.on` and `bridge.dispatch` calls are exclusively within the `bootCode` string provided to `uvm.spawn`, which defines the code to run inside the isolated VM/Worker. The main context interacts via `context.on` and `context.dispatch`.
No dependency data recorded yet.