UNPKG

3.44 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 /** @deprecated in favor of `script.createCachedData()` */
27 produceCachedData?: boolean;
28 }
29 interface RunningScriptOptions extends BaseOptions {
30 /**
31 * When `true`, if an `Error` occurs while compiling the `code`, the line of code causing the error is attached to the stack trace.
32 * Default: `true`.
33 */
34 displayErrors?: boolean;
35 /**
36 * Specifies the number of milliseconds to execute code before terminating execution.
37 * If execution is terminated, an `Error` will be thrown. This value must be a strictly positive integer.
38 */
39 timeout?: number;
40 /**
41 * If `true`, the execution will be terminated when `SIGINT` (Ctrl+C) is received.
42 * 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.
43 * If execution is terminated, an `Error` will be thrown.
44 * Default: `false`.
45 */
46 breakOnSigint?: boolean;
47 }
48 interface CompileFunctionOptions extends BaseOptions {
49 /**
50 * Provides an optional data with V8's code cache data for the supplied source.
51 */
52 cachedData?: Buffer;
53 /**
54 * Specifies whether to produce new cache data.
55 * Default: `false`,
56 */
57 produceCachedData?: boolean;
58 /**
59 * The sandbox/context in which the said function should be compiled in.
60 */
61 parsingContext?: Context;
62
63 /**
64 * An array containing a collection of context extensions (objects wrapping the current scope) to be applied while compiling
65 */
66 contextExtensions?: Object[];
67 }
68 class Script {
69 constructor(code: string, options?: ScriptOptions);
70 runInContext(contextifiedSandbox: Context, options?: RunningScriptOptions): any;
71 runInNewContext(sandbox?: Context, options?: RunningScriptOptions): any;
72 runInThisContext(options?: RunningScriptOptions): any;
73 cachedDataRejected?: boolean;
74 }
75 function createContext(sandbox?: Context): Context;
76 function isContext(sandbox: Context): boolean;
77 function runInContext(code: string, contextifiedSandbox: Context, options?: RunningScriptOptions | string): any;
78 function runInNewContext(code: string, sandbox?: Context, options?: RunningScriptOptions | string): any;
79 function runInThisContext(code: string, options?: RunningScriptOptions | string): any;
80 function compileFunction(code: string, params?: ReadonlyArray<string>, options?: CompileFunctionOptions): Function;
81}