UNPKG

4.54 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
68 interface CreateContextOptions {
69 /**
70 * Human-readable name of the newly created context.
71 * @default 'VM Context i' Where i is an ascending numerical index of the created context.
72 */
73 name?: string;
74 /**
75 * Corresponds to the newly created context for display purposes.
76 * The origin should be formatted like a `URL`, but with only the scheme, host, and port (if necessary),
77 * like the value of the `url.origin` property of a URL object.
78 * Most notably, this string should omit the trailing slash, as that denotes a path.
79 * @default ''
80 */
81 origin?: string;
82 codeGeneration?: {
83 /**
84 * If set to false any calls to eval or function constructors (Function, GeneratorFunction, etc)
85 * will throw an EvalError.
86 * @default true
87 */
88 strings?: boolean;
89 /**
90 * If set to false any attempt to compile a WebAssembly module will throw a WebAssembly.CompileError.
91 * @default true
92 */
93 wasm?: boolean;
94 };
95 }
96
97 class Script {
98 constructor(code: string, options?: ScriptOptions);
99 runInContext(contextifiedSandbox: Context, options?: RunningScriptOptions): any;
100 runInNewContext(sandbox?: Context, options?: RunningScriptOptions): any;
101 runInThisContext(options?: RunningScriptOptions): any;
102 createCachedData(): Buffer;
103 }
104 function createContext(sandbox?: Context, options?: CreateContextOptions): Context;
105 function isContext(sandbox: Context): boolean;
106 function runInContext(code: string, contextifiedSandbox: Context, options?: RunningScriptOptions | string): any;
107 function runInNewContext(code: string, sandbox?: Context, options?: RunningScriptOptions | string): any;
108 function runInThisContext(code: string, options?: RunningScriptOptions | string): any;
109 function compileFunction(code: string, params: string[], options: CompileFunctionOptions): Function;
110}