1 | import {EventEmitter} from 'events';
|
2 |
|
3 | /**
|
4 | * Require options for a VM
|
5 | */
|
6 | export interface VMRequire {
|
7 | /** Array of allowed builtin modules, accepts ["*"] for all (default: none) */
|
8 | builtin?: string[];
|
9 | /*
|
10 | * `host` (default) to require modules in host and proxy them to sandbox. `sandbox` to load, compile and
|
11 | * require modules in sandbox. Builtin modules except `events` always required in host and proxied to sandbox
|
12 | */
|
13 | context?: "host" | "sandbox";
|
14 | /** `true`, an array of allowed external modules or an object with external options (default: `false`) */
|
15 | external?: boolean | string[] | { modules: string[], transitive: boolean };
|
16 | /** Array of modules to be loaded into NodeVM on start. */
|
17 | import?: string[];
|
18 | /** Restricted path(s) where local modules can be required (default: every path). */
|
19 | root?: string | string[];
|
20 | /** Collection of mock modules (both external or builtin). */
|
21 | mock?: any;
|
22 | /* An additional lookup function in case a module wasn't found in one of the traditional node lookup paths. */
|
23 | resolve?: (moduleName: string, parentDirname: string) => string;
|
24 | }
|
25 |
|
26 | /**
|
27 | * A custom compiler function for all of the JS that comes
|
28 | * into the VM
|
29 | */
|
30 | type CompilerFunction = (code: string, filename: string) => string;
|
31 |
|
32 | /**
|
33 | * Options for creating a VM
|
34 | */
|
35 | export interface VMOptions {
|
36 | /**
|
37 | * `javascript` (default) or `coffeescript` or custom compiler function (which receives the code, and it's filepath).
|
38 | * The library expects you to have coffee-script pre-installed if the compiler is set to `coffeescript`.
|
39 | */
|
40 | compiler?: "javascript" | "coffeescript" | CompilerFunction;
|
41 | /** VM's global object. */
|
42 | sandbox?: any;
|
43 | /**
|
44 | * Script timeout in milliseconds. Timeout is only effective on code you run through `run`.
|
45 | * Timeout is NOT effective on any method returned by VM.
|
46 | */
|
47 | timeout?: number;
|
48 | /**
|
49 | * If set to `false` any calls to eval or function constructors (`Function`, `GeneratorFunction`, etc) will throw an
|
50 | * `EvalError` (default: `true`).
|
51 | */
|
52 | eval?: boolean;
|
53 | /**
|
54 | * If set to `false` any attempt to compile a WebAssembly module will throw a `WebAssembly.CompileError` (default: `true`).
|
55 | */
|
56 | wasm?: boolean;
|
57 | /**
|
58 | * If set to `true` any attempt to run code using async will throw a `VMError` (default: `false`).
|
59 | */
|
60 | fixAsync?: boolean;
|
61 | }
|
62 |
|
63 | /**
|
64 | * Options for creating a NodeVM
|
65 | */
|
66 | export interface NodeVMOptions extends VMOptions {
|
67 | /** `inherit` to enable console, `redirect` to redirect to events, `off` to disable console (default: `inherit`). */
|
68 | console?: "inherit" | "redirect" | "off";
|
69 | /** `true` or an object to enable `require` optionss (default: `false`). */
|
70 | require?: true | VMRequire;
|
71 | /** `true` to enable VMs nesting (default: `false`). */
|
72 | nesting?: boolean;
|
73 | /** `commonjs` (default) to wrap script into CommonJS wrapper, `none` to retrieve value returned by the script. */
|
74 | wrapper?: "commonjs" | "none";
|
75 | /** File extensions that the internal module resolver should accept. */
|
76 | sourceExtensions?: string[];
|
77 | /**
|
78 | * Array of arguments passed to `process.argv`.
|
79 | * This object will not be copied and the script can change this object.
|
80 | */
|
81 | argv?: string[];
|
82 | /**
|
83 | * Environment map passed to `process.env`.
|
84 | * This object will not be copied and the script can change this object.
|
85 | */
|
86 | env?: any;
|
87 | }
|
88 |
|
89 | /**
|
90 | * VM is a simple sandbox, without `require` feature, to synchronously run an untrusted code.
|
91 | * Only JavaScript built-in objects + Buffer are available. Scheduling functions
|
92 | * (`setInterval`, `setTimeout` and `setImmediate`) are not available by default.
|
93 | */
|
94 | export class VM {
|
95 | constructor(options?: VMOptions);
|
96 | /** Direct access to the global sandbox object */
|
97 | readonly sandbox: any;
|
98 | /** Timeout to use for the run methods */
|
99 | timeout?: number;
|
100 | /** Runs the code */
|
101 | run(js: string, path?: string): any;
|
102 | /** Runs the VMScript object */
|
103 | run(script: VMScript): any;
|
104 | /** Runs the code in the specific file */
|
105 | runFile(filename: string): any;
|
106 | /** Loads all the values into the global object with the same names */
|
107 | setGlobals(values: any): this;
|
108 | /** Make a object visible as a global with a specific name */
|
109 | setGlobal(name: string, value: any): this;
|
110 | /** Get the global object with the specific name */
|
111 | getGlobal(name: string): any;
|
112 | /** Freezes the object inside VM making it read-only. Not available for primitive values. */
|
113 | freeze(object: any, name?: string): any;
|
114 | /** Protects the object inside VM making impossible to set functions as it's properties. Not available for primitive values */
|
115 | protect(object: any, name?: string): any;
|
116 | }
|
117 |
|
118 | /**
|
119 | * A VM with behavior more similar to running inside Node.
|
120 | */
|
121 | export class NodeVM extends EventEmitter implements VM {
|
122 | constructor(options?: NodeVMOptions);
|
123 |
|
124 | /** Require a module in VM and return it's exports. */
|
125 | require(module: string): any;
|
126 |
|
127 | /**
|
128 | * Create NodeVM and run code inside it.
|
129 | *
|
130 | * @param {string} script Javascript code.
|
131 | * string} [filename] File name (used in stack traces only).
{ |
132 | * Object} [options] VM options.
{ |
133 | */
|
134 | static code(script: string, filename?: string, options?: NodeVMOptions): any;
|
135 |
|
136 | /**
|
137 | * Create NodeVM and run script from file inside it.
|
138 | *
|
139 | * @param {string} [filename] File name (used in stack traces only).
|
140 | * @param {Object} [options] VM options.
|
141 | */
|
142 | static file(filename: string, options?: NodeVMOptions): any;
|
143 |
|
144 | /** Direct access to the global sandbox object */
|
145 | readonly sandbox: any;
|
146 | /** Only here because of implements VM. Does nothing. */
|
147 | timeout?: number;
|
148 | /** Runs the code */
|
149 | run(js: string, path?: string): any;
|
150 | /** Runs the VMScript object */
|
151 | run(script: VMScript): any;
|
152 | /** Runs the code in the specific file */
|
153 | runFile(filename: string): any;
|
154 | /** Loads all the values into the global object with the same names */
|
155 | setGlobals(values: any): this;
|
156 | /** Make a object visible as a global with a specific name */
|
157 | setGlobal(name: string, value: any): this;
|
158 | /** Get the global object with the specific name */
|
159 | getGlobal(name: string): any;
|
160 | /** Freezes the object inside VM making it read-only. Not available for primitive values. */
|
161 | freeze(object: any, name?: string): any;
|
162 | /** Protects the object inside VM making impossible to set functions as it's properties. Not available for primitive values */
|
163 | protect(object: any, name?: string): any;
|
164 | }
|
165 |
|
166 | /**
|
167 | * You can increase performance by using pre-compiled scripts.
|
168 | * The pre-compiled VMScript can be run later multiple times. It is important to note that the code is not bound
|
169 | * to any VM (context); rather, it is bound before each run, just for that run.
|
170 | */
|
171 | export class VMScript {
|
172 | constructor(code: string, path: string, options?: {
|
173 | lineOffset?: number;
|
174 | columnOffset?: number;
|
175 | compiler?: "javascript" | "coffeescript" | CompilerFunction;
|
176 | });
|
177 | constructor(code: string, options?: {
|
178 | filename?: string,
|
179 | lineOffset?: number;
|
180 | columnOffset?: number;
|
181 | compiler?: "javascript" | "coffeescript" | CompilerFunction;
|
182 | });
|
183 | readonly code: string;
|
184 | readonly filename: string;
|
185 | readonly lineOffset: number;
|
186 | readonly columnOffset: number;
|
187 | readonly compiler: "javascript" | "coffeescript" | CompilerFunction;
|
188 | /**
|
189 | * Wraps the code
|
190 | * @deprecated
|
191 | */
|
192 | wrap(prefix: string, postfix: string): this;
|
193 | /** Compiles the code. If called multiple times, the code is only compiled once. */
|
194 | compile(): this;
|
195 | }
|
196 |
|
197 | /** Custom Error class */
|
198 | export class VMError extends Error {}
|