@scriptdb/vm
v1.1.3
Published
Virtual machine package for script database
Downloads
173
Readme
ScriptDB VM
Virtual Machine package for executing TypeScript and JavaScript code in a secure sandboxed environment.
Installation
npm install @scriptdb/vm
# or
yarn add @scriptdb/vm
# or
bun add @scriptdb/vmQuick Start
import { VM } from '@scriptdb/vm';
// Create a VM instance
const vm = new VM();
// Execute TypeScript code
const result = await vm.run(`
const greet = (name: string) => \`Hello, \${name}!\`;
greet("World");
`);
console.log(result); // "Hello, World!"API Reference
Constructor
new VM(options?)Creates a new VM instance with the specified options.
Options
interface VMOptions {
language?: 'ts' | 'js'; // Code language (default: 'ts')
registerModules?: { [key: string]: any }; // Modules to register in the VM context
}Methods
run(code, options?)
Executes code in the VM context.
await vm.run(code: string, options?: vm.RunningCodeOptions | string): Promise<any>code(string): The TypeScript or JavaScript code to executeoptions(optional): Execution options or filename
Returns a promise that resolves with the result of the code execution.
register(context)
Registers modules and values in the VM context.
vm.register(context: { [key: string]: any }): voidcontext(object): Object with keys that will be available in the VM context
Development
# Install dependencies
bun install
# Run in development mode
bun run dev
# Build the package
bun run build:all
# Run tests
bun run test
# Run linting
bun run lint
# Type checking
bun run typecheck