0.0.3 • Published 7 months ago
sandbox-lite v0.0.3
Sandbox-Lite
This is a simple sandbox for running JavaScript in the Node.js environment, supporting the following features:
- Static security checks
- Dependency control (allow or disable dependencies)
- Global variable access control
- Syntax checking
- Support TypeScript
Support execution in the main thread or WebWorker
Usage
- Install the package:
npm install sandbox-lite
- execute the code:
Run in the main thread:
import { execute } from 'sandbox-lite';
const code = `
const a = 1;
const b = 2;
a + b;
`;
const result = await execute(code);
Run in the WebWorker:
import { executeInWorker } from 'sandbox-lite';
const code = `
const a = 1;
const b = 2;
a + b;
`;
const result = await executeInWorker(code);
Configuration
Run in the main thread:
timeout number
The maximum time in milliseconds that the code can run. Default is Infinity.
context object
The context object that will be available in the code. Default is an empty object.
allows
dependencies string[]
The dependencies that the code can use. Default is an empty array.