UNPKG

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