UNPKG

5.72 kBTypeScriptView Raw
1declare module "vm" {
2 interface Context extends NodeJS.Dict<any> { }
3 interface BaseOptions {
4 /**
5 * Specifies the filename used in stack traces produced by this script.
6 * Default: `''`.
7 */
8 filename?: string;
9 /**
10 * Specifies the line number offset that is displayed in stack traces produced by this script.
11 * Default: `0`.
12 */
13 lineOffset?: number;
14 /**
15 * Specifies the column number offset that is displayed in stack traces produced by this script.
16 * Default: `0`
17 */
18 columnOffset?: number;
19 }
20 interface ScriptOptions extends BaseOptions {
21 displayErrors?: boolean;
22 timeout?: number;
23 cachedData?: Buffer;
24 produceCachedData?: boolean;
25 }
26 interface RunningScriptOptions extends BaseOptions {
27 /**
28 * When `true`, if an `Error` occurs while compiling the `code`, the line of code causing the error is attached to the stack trace.
29 * Default: `true`.
30 */
31 displayErrors?: boolean;
32 /**
33 * Specifies the number of milliseconds to execute code before terminating execution.
34 * If execution is terminated, an `Error` will be thrown. This value must be a strictly positive integer.
35 */
36 timeout?: number;
37 /**
38 * If `true`, the execution will be terminated when `SIGINT` (Ctrl+C) is received.
39 * 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.
40 * If execution is terminated, an `Error` will be thrown.
41 * Default: `false`.
42 */
43 breakOnSigint?: boolean;
44 }
45 interface CompileFunctionOptions extends BaseOptions {
46 /**
47 * Provides an optional data with V8's code cache data for the supplied source.
48 */
49 cachedData?: Buffer;
50 /**
51 * Specifies whether to produce new cache data.
52 * Default: `false`,
53 */
54 produceCachedData?: boolean;
55 /**
56 * The sandbox/context in which the said function should be compiled in.
57 */
58 parsingContext?: Context;
59
60 /**
61 * An array containing a collection of context extensions (objects wrapping the current scope) to be applied while compiling
62 */
63 contextExtensions?: Object[];
64 }
65
66 interface CreateContextOptions {
67 /**
68 * Human-readable name of the newly created context.
69 * @default 'VM Context i' Where i is an ascending numerical index of the created context.
70 */
71 name?: string;
72 /**
73 * Corresponds to the newly created context for display purposes.
74 * The origin should be formatted like a `URL`, but with only the scheme, host, and port (if necessary),
75 * like the value of the `url.origin` property of a URL object.
76 * Most notably, this string should omit the trailing slash, as that denotes a path.
77 * @default ''
78 */
79 origin?: string;
80 codeGeneration?: {
81 /**
82 * If set to false any calls to eval or function constructors (Function, GeneratorFunction, etc)
83 * will throw an EvalError.
84 * @default true
85 */
86 strings?: boolean;
87 /**
88 * If set to false any attempt to compile a WebAssembly module will throw a WebAssembly.CompileError.
89 * @default true
90 */
91 wasm?: boolean;
92 };
93 }
94
95 type MeasureMemoryMode = 'summary' | 'detailed';
96
97 interface MeasureMemoryOptions {
98 /**
99 * @default 'summary'
100 */
101 mode?: MeasureMemoryMode;
102 context?: Context;
103 }
104
105 interface MemoryMeasurement {
106 total: {
107 jsMemoryEstimate: number;
108 jsMemoryRange: [number, number];
109 };
110 }
111
112 class Script {
113 constructor(code: string, options?: ScriptOptions);
114 runInContext(contextifiedSandbox: Context, options?: RunningScriptOptions): any;
115 runInNewContext(sandbox?: Context, options?: RunningScriptOptions): any;
116 runInThisContext(options?: RunningScriptOptions): any;
117 createCachedData(): Buffer;
118 }
119 function createContext(sandbox?: Context, options?: CreateContextOptions): Context;
120 function isContext(sandbox: Context): boolean;
121 function runInContext(code: string, contextifiedSandbox: Context, options?: RunningScriptOptions | string): any;
122 function runInNewContext(code: string, sandbox?: Context, options?: RunningScriptOptions | string): any;
123 function runInThisContext(code: string, options?: RunningScriptOptions | string): any;
124 function compileFunction(code: string, params?: string[], options?: CompileFunctionOptions): Function;
125
126 /**
127 * Measure the memory known to V8 and used by the current execution context or a specified context.
128 *
129 * The format of the object that the returned Promise may resolve with is
130 * specific to the V8 engine and may change from one version of V8 to the next.
131 *
132 * The returned result is different from the statistics returned by
133 * `v8.getHeapSpaceStatistics()` in that `vm.measureMemory()` measures
134 * the memory reachable by V8 from a specific context, while
135 * `v8.getHeapSpaceStatistics()` measures the memory used by an instance
136 * of V8 engine, which can switch among multiple contexts that reference
137 * objects in the heap of one engine.
138 *
139 * @experimental
140 */
141 function measureMemory(options?: MeasureMemoryOptions): Promise<MemoryMeasurement>;
142}