Skip to content

ChatFin-Labs/javascript-vm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

JavaScript VM

A secure, isolated JavaScript VM for Node.js, built with TypeScript. Developed and maintained as an open source project by ChatFinAI.

Find Us

Visit our official website:
πŸ‘‰ ChatFin – AI Finance Platform

Connect with us on LinkedIn:
πŸ‘‰ ChatFin LinkedIn

Explore our SuiteApp listing on NetSuite:
πŸ‘‰ ChatFin AI for NetSuite – SuiteApp

Read our latest press release:
πŸ‘‰ ChatFin Launches Next-Gen AI Releases to Revolutionize Finance

Book a Demo:
πŸ‘‰ Book Demo

Features

  • Run untrusted JavaScript code securely in an isolated VM
  • TypeScript-first development
  • Uses isolated-vm for sandboxing
  • WebSocket inspector support for debugging
  • Extensible sandbox API

Getting Started

Prerequisites

  • Node.js >= 18
  • npm >= 9

Installation

From npm (Recommended)

npm install @chatfinai/javascript-vm

From source

Clone the repository and install dependencies:

git clone https://github.com/ChatFinAI/javascript-vm.git
cd javascript-vm
npm install

Build

npm run build

Development

Run the sample calculator example:

npm run dev

Debugging

Node.js Debugger

You can debug the TypeScript code using VS Code or any Node.js debugger:

node --inspect-brk -r ts-node/register src/index.ts

Then attach your debugger to localhost:9229.

WebSocket Inspector (VM Debugging)

To enable the VM inspector, instantiate ExecSandbox with debug=true and specify a port (default: 3000):

const sandbox = new ExecSandbox({}, true, 3000);

This starts a WebSocket inspector server. You can connect Chrome DevTools to:

devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:3000

for live debugging of the isolated VM context.

Type Checking

npm run type-check

Usage Example

TypeScript

import { ExecSandbox, GenDictionary } from '@chatfinai/javascript-vm';

const calculatorCode = `
  function add(a, b) { return a + b; }
  function subtract(a, b) { return a - b; }
  function multiply(a, b) { return a * b; }
  function divide(a, b) { return a / b; }
  result = {
    add: add(5, 3),
    subtract: subtract(5, 3),
    multiply: multiply(5, 3),
    divide: divide(5, 3)
  };
`;

async function runCalculatorSandbox() {
  const sandbox = new ExecSandbox(false);
  try {
    const output = await sandbox.run(calculatorCode, {} as GenDictionary);
    console.log("Calculator Results:", output.result);
  } catch (err) {
    console.error("Sandbox error:", err);
  } finally {
    sandbox.dispose();
  }
}

runCalculatorSandbox();

JavaScript (CommonJS)

const { ExecSandbox } = require('@chatfinai/javascript-vm');

const calculatorCode = `
  function add(a, b) { return a + b; }
  function subtract(a, b) { return a - b; }
  function multiply(a, b) { return a * b; }
  function divide(a, b) { return a / b; }
  result = {
    add: add(5, 3),
    subtract: subtract(5, 3),
    multiply: multiply(5, 3),
    divide: divide(5, 3)
  };
`;

async function runCalculatorSandbox() {
  const sandbox = new ExecSandbox(false);
  try {
    const output = await sandbox.run(calculatorCode, {});
    console.log("Calculator Results:", output.result);
  } catch (err) {
    console.error("Sandbox error:", err);
  } finally {
    sandbox.dispose();
  }
}

runCalculatorSandbox();

Project Structure

  • src/VM/ β€” Core VM and sandbox classes
  • src/Models/ β€” Type definitions
  • src/index.ts β€” Main exports for npm package
  • examples/ β€” Calculator usage examples (JavaScript and TypeScript)

Examples

The examples/ directory contains calculator examples showing how to use the VM:

  • calculator.js β€” JavaScript (CommonJS) example
  • calculator.ts β€” TypeScript example with proper types

Running Examples

If you installed from npm:

npm install @chatfinai/javascript-vm

Then update the import statements in the examples:

  • In calculator.js: Change require('../dist/index.js') to require('@chatfinai/javascript-vm')
  • In calculator.ts: Change from '../src/index' to from '@chatfinai/javascript-vm'
# Run JavaScript example
node examples/calculator.js

# Run TypeScript example
npx ts-node examples/calculator.ts

If you're running from source:

# First, build the project
npm run build

# Run JavaScript example (uses dist/index.js)
node examples/calculator.js

# Run TypeScript example (uses src/index.ts)
npx ts-node examples/calculator.ts

License

This project is licensed under the MIT License. See the LICENSE file for details.

Maintainers

Support

For questions or support, open an issue on GitHub or contact us at support@chatfin.ai.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors