@novolobos/nodevm 3.10.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts ADDED
@@ -0,0 +1,318 @@
1
+ import { EventEmitter } from 'events';
2
+ import fs from 'fs';
3
+ import pa from 'path';
4
+
5
+ /**
6
+ * Interface for nodes fs module
7
+ */
8
+ export interface VMFS {
9
+ /** Implements fs.statSync */
10
+ statSync: typeof fs.statSync;
11
+ /** Implements fs.readFileSync */
12
+ readFileSync: typeof fs.readFileSync;
13
+ }
14
+
15
+ /**
16
+ * Interface for nodes path module
17
+ */
18
+ export interface VMPath {
19
+ /** Implements path.resolve */
20
+ resolve: typeof pa.resolve;
21
+ /** Implements path.isAbsolute */
22
+ isAbsolute: typeof pa.isAbsolute;
23
+ /** Implements path.join */
24
+ join: typeof pa.join;
25
+ /** Implements path.basename */
26
+ basename: typeof pa.basename;
27
+ /** Implements path.dirname */
28
+ dirname: typeof pa.dirname;
29
+ }
30
+
31
+ /**
32
+ * Custom file system which abstracts functions from node's fs and path modules.
33
+ */
34
+ export interface VMFileSystemInterface extends VMFS, VMPath {
35
+ /** Implements (sep) => sep === path.sep */
36
+ isSeparator(char: string): boolean;
37
+ }
38
+
39
+ /**
40
+ * Implementation of a default file system.
41
+ */
42
+ export class VMFileSystem implements VMFileSystemInterface {
43
+ constructor(options?: { fs?: VMFS, path?: VMPath });
44
+ /** Implements fs.statSync */
45
+ statSync: typeof fs.statSync;
46
+ /** Implements fs.readFileSync */
47
+ readFileSync: typeof fs.readFileSync;
48
+ /** Implements path.resolve */
49
+ resolve: typeof pa.resolve;
50
+ /** Implements path.isAbsolute */
51
+ isAbsolute: typeof pa.isAbsolute;
52
+ /** Implements path.join */
53
+ join: typeof pa.join;
54
+ /** Implements path.basename */
55
+ basename: typeof pa.basename;
56
+ /** Implements path.dirname */
57
+ dirname: typeof pa.dirname;
58
+ /** Implements (sep) => sep === path.sep */
59
+ isSeparator(char: string): boolean;
60
+ }
61
+
62
+ /**
63
+ * Function that will be called to load a built-in into a vm.
64
+ */
65
+ export type BuiltinLoad = (vm: NodeVM) => any;
66
+ /**
67
+ * Either a function that will be called to load a built-in into a vm or an object with a init method and a load method to load the built-in.
68
+ */
69
+ export type Builtin = BuiltinLoad | {init: (vm: NodeVM)=>void, load: BuiltinLoad};
70
+ /**
71
+ * Require method
72
+ */
73
+ export type HostRequire = (id: string) => any;
74
+
75
+ /**
76
+ * This callback will be called to specify the context to use "per" module. Defaults to 'sandbox' if no return value provided.
77
+ */
78
+ export type PathContextCallback = (modulePath: string, extensionType: string) => 'host' | 'sandbox';
79
+
80
+ /**
81
+ * Require options for a VM
82
+ */
83
+ export interface VMRequire {
84
+ /**
85
+ * Array of allowed built-in modules, accepts ["*"] for all. Using "*" increases the attack surface and potential
86
+ * new modules allow to escape the sandbox. (default: none)
87
+ */
88
+ builtin?: readonly string[];
89
+ /*
90
+ * `host` (default) to require modules in host and proxy them to sandbox. `sandbox` to load, compile and
91
+ * require modules in sandbox or a callback which chooses the context based on the filename.
92
+ * Built-in modules except `events` always required in host and proxied to sandbox
93
+ */
94
+ context?: "host" | "sandbox" | PathContextCallback;
95
+ /** `true`, an array of allowed external modules or an object with external options (default: `false`) */
96
+ external?: boolean | readonly string[] | { modules: readonly string[], transitive: boolean };
97
+ /** Array of modules to be loaded into NodeVM on start. */
98
+ import?: readonly string[];
99
+ /** Restricted path(s) where local modules can be required (default: every path). */
100
+ root?: string | readonly string[];
101
+ /** Collection of mock modules (both external or built-in). */
102
+ mock?: any;
103
+ /* An additional lookup function in case a module wasn't found in one of the traditional node lookup paths. */
104
+ resolve?: (moduleName: string, parentDirname: string) => string | { path: string, module?: string } | undefined;
105
+ /** Custom require to require host and built-in modules. */
106
+ customRequire?: HostRequire;
107
+ /** Load modules in strict mode. (default: true) */
108
+ strict?: boolean;
109
+ /** FileSystem to load files from */
110
+ fs?: VMFileSystemInterface;
111
+ }
112
+
113
+ /**
114
+ * A custom compiler function for all of the JS that comes
115
+ * into the VM
116
+ */
117
+ export type CompilerFunction = (code: string, filename: string) => string;
118
+
119
+ export abstract class Resolver {
120
+ private constructor(fs: VMFileSystemInterface, globalPaths: readonly string[], builtins: Map<string, Builtin>);
121
+ }
122
+
123
+ /**
124
+ * Create a resolver as normal `NodeVM` does given `VMRequire` options.
125
+ *
126
+ * @param options The options that would have been given to `NodeVM`.
127
+ * @param override Custom overrides for built-ins.
128
+ * @param compiler Compiler to be used for loaded modules.
129
+ */
130
+ export function makeResolverFromLegacyOptions(options: VMRequire, override?: {[key: string]: Builtin}, compiler?: CompilerFunction): Resolver;
131
+
132
+ /**
133
+ * Options for creating a VM
134
+ */
135
+ export interface VMOptions {
136
+ /**
137
+ * `javascript` (default), `typescript`, `coffeescript` or custom compiler function (which receives the code, and it's file path).
138
+ * The library expects you to have compiler pre-installed if the value is set to `typescript` or `coffeescript`.
139
+ */
140
+ compiler?: "javascript" | "typescript" | "coffeescript" | CompilerFunction;
141
+ /**
142
+ * Compiler options.
143
+ */
144
+ compilerOptions?: Record<string, any>;
145
+ /** VM's global object. */
146
+ sandbox?: any;
147
+ /**
148
+ * Script timeout in milliseconds. Timeout is only effective on code you run through `run`.
149
+ * Timeout is NOT effective on any method returned by VM.
150
+ */
151
+ timeout?: number;
152
+ /**
153
+ * If set to `false` any calls to eval or function constructors (`Function`, `GeneratorFunction`, etc.) will throw an
154
+ * `EvalError` (default: `true`).
155
+ */
156
+ eval?: boolean;
157
+ /**
158
+ * If set to `false` any attempt to compile a WebAssembly module will throw a `WebAssembly.CompileError` (default: `true`).
159
+ */
160
+ wasm?: boolean;
161
+ /**
162
+ * If set to `true` any attempt to run code using async will throw a `VMError` (default: `false`).
163
+ * @deprecated Use `allowAsync` instead.
164
+ */
165
+ fixAsync?: boolean;
166
+
167
+ /**
168
+ * If set to `false` any attempt to run code using async will throw a `VMError` (default: `true`).
169
+ */
170
+ allowAsync?: boolean;
171
+ }
172
+
173
+ /**
174
+ * Options for creating a NodeVM
175
+ */
176
+ export interface NodeVMOptions extends VMOptions {
177
+ /** `inherit` to enable console, `redirect` to redirect to events, `off` to disable console (default: `inherit`). */
178
+ console?: "inherit" | "redirect" | "off";
179
+ /** `true` or an object to enable `require` options (default: `false`). */
180
+ require?: boolean | VMRequire | Resolver;
181
+ /**
182
+ * **WARNING**: This should be disabled. It allows to create a NodeVM form within the sandbox which could return any host module.
183
+ * `true` to enable VMs nesting (default: `false`).
184
+ */
185
+ nesting?: boolean;
186
+ /** `commonjs` (default) to wrap script into CommonJS wrapper, `none` to retrieve value returned by the script. */
187
+ wrapper?: "commonjs" | "none";
188
+ /** File extensions that the internal module resolver should accept. */
189
+ sourceExtensions?: readonly string[];
190
+ /**
191
+ * Array of arguments passed to `process.argv`.
192
+ * This object will not be copied and the script can change this object.
193
+ */
194
+ argv?: string[];
195
+ /**
196
+ * Environment map passed to `process.env`.
197
+ * This object will not be copied and the script can change this object.
198
+ */
199
+ env?: any;
200
+ /** Run modules in strict mode. Required modules are always strict. */
201
+ strict?: boolean;
202
+ }
203
+
204
+ /**
205
+ * VM is a simple sandbox, without `require` feature, to synchronously run an untrusted code.
206
+ * Only JavaScript built-in objects + Buffer are available. Scheduling functions
207
+ * (`setInterval`, `setTimeout` and `setImmediate`) are not available by default.
208
+ */
209
+ export class VM {
210
+ constructor(options?: VMOptions);
211
+ /** Direct access to the global sandbox object */
212
+ readonly sandbox: any;
213
+ /** Timeout to use for the run methods */
214
+ timeout?: number;
215
+ /** Runs the code */
216
+ run(script: string | VMScript, options?: string | { filename?: string }): any;
217
+ /** Runs the code in the specific file */
218
+ runFile(filename: string): any;
219
+ /** Loads all the values into the global object with the same names */
220
+ setGlobals(values: any): this;
221
+ /** Make a object visible as a global with a specific name */
222
+ setGlobal(name: string, value: any): this;
223
+ /** Get the global object with the specific name */
224
+ getGlobal(name: string): any;
225
+ /** Freezes the object inside VM making it read-only. Not available for primitive values. */
226
+ freeze(object: any, name?: string): any;
227
+ /** Freezes the object inside VM making it read-only. Not available for primitive values. */
228
+ readonly(object: any): any;
229
+ /** Protects the object inside VM making impossible to set functions as it's properties. Not available for primitive values */
230
+ protect(object: any, name?: string): any;
231
+ }
232
+
233
+ /**
234
+ * A VM with behavior more similar to running inside Node.
235
+ */
236
+ export class NodeVM extends EventEmitter implements VM {
237
+ constructor(options?: NodeVMOptions);
238
+
239
+ /** Require a module in VM and return it's exports. */
240
+ require(module: string): any;
241
+
242
+ /**
243
+ * Create NodeVM and run code inside it.
244
+ *
245
+ * @param {string} script JavaScript code.
246
+ * @param {string} [filename] File name (used in stack traces only).
247
+ * @param {Object} [options] VM options.
248
+ */
249
+ static code(script: string, filename?: string, options?: NodeVMOptions): any;
250
+
251
+ /**
252
+ * Create NodeVM and run script from file inside it.
253
+ *
254
+ * @param {string} [filename] File name (used in stack traces only).
255
+ * @param {Object} [options] VM options.
256
+ */
257
+ static file(filename: string, options?: NodeVMOptions): any;
258
+
259
+ /** Direct access to the global sandbox object */
260
+ readonly sandbox: any;
261
+ /** Only here because of implements VM. Does nothing. */
262
+ timeout?: number;
263
+ /** The resolver used to resolve modules */
264
+ readonly resolver: Resolver;
265
+ /** Runs the code */
266
+ run(js: string | VMScript, options?: string | { filename?: string, wrapper?: "commonjs" | "none", strict?: boolean }): any;
267
+ /** Runs the code in the specific file */
268
+ runFile(filename: string): any;
269
+ /** Loads all the values into the global object with the same names */
270
+ setGlobals(values: any): this;
271
+ /** Make a object visible as a global with a specific name */
272
+ setGlobal(name: string, value: any): this;
273
+ /** Get the global object with the specific name */
274
+ getGlobal(name: string): any;
275
+ /** Freezes the object inside VM making it read-only. Not available for primitive values. */
276
+ freeze(object: any, name?: string): any;
277
+ /** Freezes the object inside VM making it read-only. Not available for primitive values. */
278
+ readonly(object: any): any;
279
+ /** Protects the object inside VM making impossible to set functions as it's properties. Not available for primitive values */
280
+ protect(object: any, name?: string): any;
281
+ }
282
+
283
+ /**
284
+ * You can increase performance by using pre-compiled scripts.
285
+ * The pre-compiled VMScript can be run later multiple times. It is important to note that the code is not bound
286
+ * to any VM (context); rather, it is bound before each run, just for that run.
287
+ */
288
+ export class VMScript {
289
+ constructor(code: string, path: string, options?: {
290
+ lineOffset?: number;
291
+ columnOffset?: number;
292
+ compiler?: "javascript" | "typescript" | "coffeescript" | CompilerFunction;
293
+ compilerOptions?: Record<string, any>;
294
+ });
295
+ constructor(code: string, options?: {
296
+ filename?: string,
297
+ lineOffset?: number;
298
+ columnOffset?: number;
299
+ compiler?: "javascript" | "typescript" | "coffeescript" | CompilerFunction;
300
+ compilerOptions?: Record<string, any>;
301
+ });
302
+ readonly code: string;
303
+ readonly filename: string;
304
+ readonly lineOffset: number;
305
+ readonly columnOffset: number;
306
+ readonly compiler: "javascript" | "typescript" | "coffeescript" | CompilerFunction;
307
+ readonly compilerOptions: Record<string, any> | undefined;
308
+ /**
309
+ * Wraps the code
310
+ * @deprecated
311
+ */
312
+ wrap(prefix: string, postfix: string): this;
313
+ /** Compiles the code. If called multiple times, the code is only compiled once. */
314
+ compile(): this;
315
+ }
316
+
317
+ /** Custom Error class */
318
+ export class VMError extends Error { }
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ if (parseInt(process.versions.node.split('.')[0]) < 6) throw new Error('vm2 requires Node.js version 6 or newer.');
2
+
3
+ module.exports = require('./lib/main');