UNPKG

3.33 kBTypeScriptView Raw
1declare module "vm" {
2 interface Context {
3 [key: string]: any;
4 }
5 interface BaseOptions {
6 /**
7 * Specifies the filename used in stack traces produced by this script.
8 * Default: `''`.
9 */
10 filename?: string;
11 /**
12 * Specifies the line number offset that is displayed in stack traces produced by this script.
13 * Default: `0`.
14 */
15 lineOffset?: number;
16 /**
17 * Specifies the column number offset that is displayed in stack traces produced by this script.
18 * Default: `0`
19 */
20 columnOffset?: number;
21 }
22 interface ScriptOptions extends BaseOptions {
23 displayErrors?: boolean;
24 timeout?: number;
25 cachedData?: Buffer;
26 produceCachedData?: boolean;
27 }
28 interface RunningScriptOptions extends BaseOptions {
29 /**
30 * When `true`, if an `Error` occurs while compiling the `code`, the line of code causing the error is attached to the stack trace.
31 * Default: `true`.
32 */
33 displayErrors?: boolean;
34 /**
35 * Specifies the number of milliseconds to execute code before terminating execution.
36 * If execution is terminated, an `Error` will be thrown. This value must be a strictly positive integer.
37 */
38 timeout?: number;
39 /**
40 * If `true`, the execution will be terminated when `SIGINT` (Ctrl+C) is received.
41 * Existing handlers for the event that have been attached via `process.on('SIGINT')` will be disabled during script execution, but will continue to work after that.
42 * If execution is terminated, an `Error` will be thrown.
43 * Default: `false`.
44 */
45 breakOnSigint?: boolean;
46 }
47 interface CompileFunctionOptions extends BaseOptions {
48 /**
49 * Provides an optional data with V8's code cache data for the supplied source.
50 */
51 cachedData?: Buffer;
52 /**
53 * Specifies whether to produce new cache data.
54 * Default: `false`,
55 */
56 produceCachedData?: boolean;
57 /**
58 * The sandbox/context in which the said function should be compiled in.
59 */
60 parsingContext?: Context;
61
62 /**
63 * An array containing a collection of context extensions (objects wrapping the current scope) to be applied while compiling
64 */
65 contextExtensions?: Object[];
66 }
67 class Script {
68 constructor(code: string, options?: ScriptOptions);
69 runInContext(contextifiedSandbox: Context, options?: RunningScriptOptions): any;
70 runInNewContext(sandbox?: Context, options?: RunningScriptOptions): any;
71 runInThisContext(options?: RunningScriptOptions): any;
72 }
73 function createContext(sandbox?: Context): Context;
74 function isContext(sandbox: Context): boolean;
75 function runInContext(code: string, contextifiedSandbox: Context, options?: RunningScriptOptions | string): any;
76 function runInNewContext(code: string, sandbox?: Context, options?: RunningScriptOptions | string): any;
77 function runInThisContext(code: string, options?: RunningScriptOptions | string): any;
78 function compileFunction(code: string, params?: ReadonlyArray<string>, options?: CompileFunctionOptions): Function;
79}