Virtual machine package for script database
npm install @scriptdb/vmVirtual Machine package for executing TypeScript and JavaScript code in a secure sandboxed environment.
``bash`
npm install @scriptdb/vmor
yarn add @scriptdb/vmor
bun add @scriptdb/vm
`typescript
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!"
`
`typescript`
new VM(options?)
Creates a new VM instance with the specified options.
#### Options
`typescript`
interface VMOptions {
language?: 'ts' | 'js'; // Code language (default: 'ts')
registerModules?: { [key: string]: any }; // Modules to register in the VM context
}
#### run(code, options?)
Executes code in the VM context.
`typescript`
await vm.run(code: string, options?: vm.RunningCodeOptions | string): Promise
- 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.
`typescript`
vm.register(context: { [key: string]: any }): void
- context (object): Object with keys that will be available in the VM context
`bashInstall dependencies
bun install