UNPKG

baset-vm

Version:

VM package for BaseT project.

110 lines (109 loc) 4.3 kB
/// <reference types="node" /> import { EventEmitter } from 'events'; import { VMError } from './VMError'; import { VMScript } from './VMScript'; export { VMError, VMScript, }; /** * Require options for a VM */ export interface IVMRequire { /** Array of allowed builtin modules, accepts ["*"] for all (default: none) */ builtin?: string[] | { [index: string]: unknown; }; /** * `host` (default) to require modules in host and proxy them to sandbox. `sandbox` to load, compile and * require modules in sandbox. Builtin modules except `events` always required in host and proxied to sandbox */ context?: 'host' | 'sandbox'; /** `true` or an array of allowed external modules (default: `false`) */ external?: boolean | string[]; /** Array of modules to be loaded into NodeVM on start. */ import?: string[]; /** Restricted path where local modules can be required (default: every path). */ root?: string; /** Collection of mock modules (both external or builtin). */ mock?: { [index: string]: string; }; } export declare type CompilerFunction = (code: string, filename: string) => string; export declare type ResolverFunction = (request: string) => string | null; /** * Options specific for Node VM */ export interface INodeVMOptions { /** * `javascript` (default) or custom compiler function (which receives the code, and it's filepath). * The library expects you to have coffee-script pre-installed if the compiler is set to `coffeescript`. */ compiler?: 'javascript' | CompilerFunction; /** VM's global object. */ sandbox?: { [index: string]: unknown; }; /** * Script timeout in milliseconds. Timeout is only effective on code you run through `run`. * Timeout is NOT effective on any method returned by VM. */ timeout?: number; /** File extensions that the internal module resolver should accept. */ sourceExtensions?: string[]; resolveFilename?: false | ((original: ResolverFunction, request: string) => string | null); /** `inherit` to enable console, `redirect` to redirect to events, `off` to disable console (default: `inherit`). */ console?: 'inherit' | 'redirect'; /** `true` or an object to enable `require` options (default: `false`). */ require?: boolean | IVMRequire; /** `true` to enable VMs nesting (default: `false`). */ nesting?: boolean; /** `commonjs` (default) to wrap script into CommonJS wrapper, `none` to retrieve value returned by the script. */ wrapper?: 'commonjs' | 'none'; } /** * Class NodeVM. */ export declare class NodeVM extends EventEmitter { /** * VM options. */ options: Required<INodeVMOptions>; private context; private prepareRequire; /** * Create NodeVM instance. * Unlike VM, NodeVM lets you use require same way like in regular node. * @param options VM options. */ constructor(options?: INodeVMOptions); /** * Require a module in VM and return it's exports. * @param module Module name. * @returns Exported module. */ require<T = unknown>(module: string): T; /** * Run the code in NodeVM. * First time you run this method, code is executed same way like in node's regular `require` - * it's executed with `module`, `require`, `exports`, `__dirname`, `__filename` variables and expect result in `module.exports'. * @param code Code to run. * @param filename Filename that shows up in any stack traces produced from this script. * @returns Result of executed code. */ run<T = unknown>(codeInput: string, filenameInput?: string): T; /** * Create NodeVM and run code inside it. * @param script Javascript code. * @param filename File name (used in stack traces only). * @param options VM options. * @return VM. */ static code(script: string, options?: object): NodeVM; static code(script: string, filename: string, options?: object): NodeVM; /** * Create NodeVM and run script from file inside it. * @param filename File name (used in stack traces only). * @param options VM options. * @returns VM. */ static file(filename: string, options: object): NodeVM; }