@types/node 16.3.3 → 16.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- node/README.md +2 -2
- node/assert/strict.d.ts +0 -1
- node/assert.d.ts +1352 -40
- node/async_hooks.d.ts +359 -90
- node/buffer.d.ts +1498 -73
- node/child_process.d.ts +1049 -227
- node/cluster.d.ts +317 -99
- node/console.d.ts +305 -32
- node/crypto.d.ts +3079 -704
- node/dgram.d.ts +446 -55
- node/diagnostics_channel.d.ts +83 -10
- node/dns/promises.d.ts +292 -36
- node/dns.d.ts +410 -97
- node/domain.d.ts +152 -8
- node/events.d.ts +361 -15
- node/fs/promises.d.ts +660 -271
- node/fs.d.ts +2240 -819
- node/http.d.ts +889 -81
- node/http2.d.ts +1520 -459
- node/https.d.ts +261 -11
- node/index.d.ts +26 -1
- node/inspector.d.ts +354 -661
- node/module.d.ts +49 -11
- node/net.d.ts +545 -137
- node/os.d.ts +236 -22
- node/package.json +7 -2
- node/path.d.ts +9 -5
- node/perf_hooks.d.ts +288 -90
- node/process.d.ts +1092 -153
- node/punycode.d.ts +65 -26
- node/querystring.d.ts +107 -8
- node/readline.d.ts +425 -79
- node/repl.d.ts +134 -109
- node/stream/promises.d.ts +15 -44
- node/stream.d.ts +924 -222
- node/string_decoder.d.ts +57 -1
- node/timers/promises.d.ts +97 -9
- node/timers.d.ts +30 -11
- node/tls.d.ts +440 -217
- node/trace_events.d.ts +107 -11
- node/tty.d.ts +159 -19
- node/url.d.ts +732 -18
- node/util.d.ts +1354 -70
- node/v8.d.ts +252 -76
- node/vm.d.ts +377 -29
- node/wasi.d.ts +107 -24
- node/worker_threads.d.ts +493 -130
- node/zlib.d.ts +215 -63
node/vm.d.ts
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `vm` module enables compiling and running code within V8 Virtual
|
|
3
|
+
* Machine contexts. **The `vm` module is not a security mechanism. Do**
|
|
4
|
+
* **not use it to run untrusted code**.
|
|
5
|
+
*
|
|
6
|
+
* JavaScript code can be compiled and run immediately or
|
|
7
|
+
* compiled, saved, and run later.
|
|
8
|
+
*
|
|
9
|
+
* A common use case is to run the code in a different V8 Context. This means
|
|
10
|
+
* invoked code has a different global object than the invoking code.
|
|
11
|
+
*
|
|
12
|
+
* One can provide the context by `contextifying` an
|
|
13
|
+
* object. The invoked code treats any property in the context like a
|
|
14
|
+
* global variable. Any changes to global variables caused by the invoked
|
|
15
|
+
* code are reflected in the context object.
|
|
16
|
+
*
|
|
17
|
+
* ```js
|
|
18
|
+
* const vm = require('vm');
|
|
19
|
+
*
|
|
20
|
+
* const x = 1;
|
|
21
|
+
*
|
|
22
|
+
* const context = { x: 2 };
|
|
23
|
+
* vm.createContext(context); // Contextify the object.
|
|
24
|
+
*
|
|
25
|
+
* const code = 'x += 40; var y = 17;';
|
|
26
|
+
* // `x` and `y` are global variables in the context.
|
|
27
|
+
* // Initially, x has the value 2 because that is the value of context.x.
|
|
28
|
+
* vm.runInContext(code, context);
|
|
29
|
+
*
|
|
30
|
+
* console.log(context.x); // 42
|
|
31
|
+
* console.log(context.y); // 17
|
|
32
|
+
*
|
|
33
|
+
* console.log(x); // 1; y is not defined.
|
|
34
|
+
* ```
|
|
35
|
+
* @see [source](https://github.com/nodejs/node/blob/v16.4.2/lib/vm.js)
|
|
36
|
+
*/
|
|
1
37
|
declare module 'vm' {
|
|
2
|
-
interface Context extends NodeJS.Dict<any> {
|
|
38
|
+
interface Context extends NodeJS.Dict<any> {}
|
|
3
39
|
interface BaseOptions {
|
|
4
40
|
/**
|
|
5
41
|
* Specifies the filename used in stack traces produced by this script.
|
|
@@ -61,13 +97,11 @@ declare module 'vm' {
|
|
|
61
97
|
* The sandbox/context in which the said function should be compiled in.
|
|
62
98
|
*/
|
|
63
99
|
parsingContext?: Context | undefined;
|
|
64
|
-
|
|
65
100
|
/**
|
|
66
101
|
* An array containing a collection of context extensions (objects wrapping the current scope) to be applied while compiling
|
|
67
102
|
*/
|
|
68
103
|
contextExtensions?: Object[] | undefined;
|
|
69
104
|
}
|
|
70
|
-
|
|
71
105
|
interface CreateContextOptions {
|
|
72
106
|
/**
|
|
73
107
|
* Human-readable name of the newly created context.
|
|
@@ -82,27 +116,27 @@ declare module 'vm' {
|
|
|
82
116
|
* @default ''
|
|
83
117
|
*/
|
|
84
118
|
origin?: string | undefined;
|
|
85
|
-
codeGeneration?:
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
119
|
+
codeGeneration?:
|
|
120
|
+
| {
|
|
121
|
+
/**
|
|
122
|
+
* If set to false any calls to eval or function constructors (Function, GeneratorFunction, etc)
|
|
123
|
+
* will throw an EvalError.
|
|
124
|
+
* @default true
|
|
125
|
+
*/
|
|
126
|
+
strings?: boolean | undefined;
|
|
127
|
+
/**
|
|
128
|
+
* If set to false any attempt to compile a WebAssembly module will throw a WebAssembly.CompileError.
|
|
129
|
+
* @default true
|
|
130
|
+
*/
|
|
131
|
+
wasm?: boolean | undefined;
|
|
132
|
+
}
|
|
133
|
+
| undefined;
|
|
98
134
|
/**
|
|
99
135
|
* If set to `afterEvaluate`, microtasks will be run immediately after the script has run.
|
|
100
136
|
*/
|
|
101
137
|
microtaskMode?: 'afterEvaluate' | undefined;
|
|
102
138
|
}
|
|
103
|
-
|
|
104
139
|
type MeasureMemoryMode = 'summary' | 'detailed';
|
|
105
|
-
|
|
106
140
|
interface MeasureMemoryOptions {
|
|
107
141
|
/**
|
|
108
142
|
* @default 'summary'
|
|
@@ -110,47 +144,361 @@ declare module 'vm' {
|
|
|
110
144
|
mode?: MeasureMemoryMode | undefined;
|
|
111
145
|
context?: Context | undefined;
|
|
112
146
|
}
|
|
113
|
-
|
|
114
147
|
interface MemoryMeasurement {
|
|
115
148
|
total: {
|
|
116
149
|
jsMemoryEstimate: number;
|
|
117
150
|
jsMemoryRange: [number, number];
|
|
118
151
|
};
|
|
119
152
|
}
|
|
120
|
-
|
|
153
|
+
/**
|
|
154
|
+
* Instances of the `vm.Script` class contain precompiled scripts that can be
|
|
155
|
+
* executed in specific contexts.
|
|
156
|
+
* @since v0.3.1
|
|
157
|
+
*/
|
|
121
158
|
class Script {
|
|
122
159
|
constructor(code: string, options?: ScriptOptions);
|
|
160
|
+
/**
|
|
161
|
+
* Runs the compiled code contained by the `vm.Script` object within the given`contextifiedObject` and returns the result. Running code does not have access
|
|
162
|
+
* to local scope.
|
|
163
|
+
*
|
|
164
|
+
* The following example compiles code that increments a global variable, sets
|
|
165
|
+
* the value of another global variable, then execute the code multiple times.
|
|
166
|
+
* The globals are contained in the `context` object.
|
|
167
|
+
*
|
|
168
|
+
* ```js
|
|
169
|
+
* const vm = require('vm');
|
|
170
|
+
*
|
|
171
|
+
* const context = {
|
|
172
|
+
* animal: 'cat',
|
|
173
|
+
* count: 2
|
|
174
|
+
* };
|
|
175
|
+
*
|
|
176
|
+
* const script = new vm.Script('count += 1; name = "kitty";');
|
|
177
|
+
*
|
|
178
|
+
* vm.createContext(context);
|
|
179
|
+
* for (let i = 0; i < 10; ++i) {
|
|
180
|
+
* script.runInContext(context);
|
|
181
|
+
* }
|
|
182
|
+
*
|
|
183
|
+
* console.log(context);
|
|
184
|
+
* // Prints: { animal: 'cat', count: 12, name: 'kitty' }
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* Using the `timeout` or `breakOnSigint` options will result in new event loops
|
|
188
|
+
* and corresponding threads being started, which have a non-zero performance
|
|
189
|
+
* overhead.
|
|
190
|
+
* @since v0.3.1
|
|
191
|
+
* @param contextifiedObject A `contextified` object as returned by the `vm.createContext()` method.
|
|
192
|
+
* @return the result of the very last statement executed in the script.
|
|
193
|
+
*/
|
|
123
194
|
runInContext(contextifiedSandbox: Context, options?: RunningScriptOptions): any;
|
|
195
|
+
/**
|
|
196
|
+
* First contextifies the given `contextObject`, runs the compiled code contained
|
|
197
|
+
* by the `vm.Script` object within the created context, and returns the result.
|
|
198
|
+
* Running code does not have access to local scope.
|
|
199
|
+
*
|
|
200
|
+
* The following example compiles code that sets a global variable, then executes
|
|
201
|
+
* the code multiple times in different contexts. The globals are set on and
|
|
202
|
+
* contained within each individual `context`.
|
|
203
|
+
*
|
|
204
|
+
* ```js
|
|
205
|
+
* const vm = require('vm');
|
|
206
|
+
*
|
|
207
|
+
* const script = new vm.Script('globalVar = "set"');
|
|
208
|
+
*
|
|
209
|
+
* const contexts = [{}, {}, {}];
|
|
210
|
+
* contexts.forEach((context) => {
|
|
211
|
+
* script.runInNewContext(context);
|
|
212
|
+
* });
|
|
213
|
+
*
|
|
214
|
+
* console.log(contexts);
|
|
215
|
+
* // Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
|
|
216
|
+
* ```
|
|
217
|
+
* @since v0.3.1
|
|
218
|
+
* @param contextObject An object that will be `contextified`. If `undefined`, a new object will be created.
|
|
219
|
+
* @return the result of the very last statement executed in the script.
|
|
220
|
+
*/
|
|
124
221
|
runInNewContext(sandbox?: Context, options?: RunningScriptOptions): any;
|
|
222
|
+
/**
|
|
223
|
+
* Runs the compiled code contained by the `vm.Script` within the context of the
|
|
224
|
+
* current `global` object. Running code does not have access to local scope, but_does_ have access to the current `global` object.
|
|
225
|
+
*
|
|
226
|
+
* The following example compiles code that increments a `global` variable then
|
|
227
|
+
* executes that code multiple times:
|
|
228
|
+
*
|
|
229
|
+
* ```js
|
|
230
|
+
* const vm = require('vm');
|
|
231
|
+
*
|
|
232
|
+
* global.globalVar = 0;
|
|
233
|
+
*
|
|
234
|
+
* const script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' });
|
|
235
|
+
*
|
|
236
|
+
* for (let i = 0; i < 1000; ++i) {
|
|
237
|
+
* script.runInThisContext();
|
|
238
|
+
* }
|
|
239
|
+
*
|
|
240
|
+
* console.log(globalVar);
|
|
241
|
+
*
|
|
242
|
+
* // 1000
|
|
243
|
+
* ```
|
|
244
|
+
* @since v0.3.1
|
|
245
|
+
* @return the result of the very last statement executed in the script.
|
|
246
|
+
*/
|
|
125
247
|
runInThisContext(options?: RunningScriptOptions): any;
|
|
248
|
+
/**
|
|
249
|
+
* Creates a code cache that can be used with the `Script` constructor's`cachedData` option. Returns a `Buffer`. This method may be called at any
|
|
250
|
+
* time and any number of times.
|
|
251
|
+
*
|
|
252
|
+
* ```js
|
|
253
|
+
* const script = new vm.Script(`
|
|
254
|
+
* function add(a, b) {
|
|
255
|
+
* return a + b;
|
|
256
|
+
* }
|
|
257
|
+
*
|
|
258
|
+
* const x = add(1, 2);
|
|
259
|
+
* `);
|
|
260
|
+
*
|
|
261
|
+
* const cacheWithoutX = script.createCachedData();
|
|
262
|
+
*
|
|
263
|
+
* script.runInThisContext();
|
|
264
|
+
*
|
|
265
|
+
* const cacheWithX = script.createCachedData();
|
|
266
|
+
* ```
|
|
267
|
+
* @since v10.6.0
|
|
268
|
+
*/
|
|
126
269
|
createCachedData(): Buffer;
|
|
127
270
|
cachedDataRejected?: boolean | undefined;
|
|
128
271
|
}
|
|
272
|
+
/**
|
|
273
|
+
* If given a `contextObject`, the `vm.createContext()` method will `prepare
|
|
274
|
+
* that object` so that it can be used in calls to {@link runInContext} or `script.runInContext()`. Inside such scripts,
|
|
275
|
+
* the `contextObject` will be the global object, retaining all of its existing
|
|
276
|
+
* properties but also having the built-in objects and functions any standard[global object](https://es5.github.io/#x15.1) has. Outside of scripts run by the vm module, global variables
|
|
277
|
+
* will remain unchanged.
|
|
278
|
+
*
|
|
279
|
+
* ```js
|
|
280
|
+
* const vm = require('vm');
|
|
281
|
+
*
|
|
282
|
+
* global.globalVar = 3;
|
|
283
|
+
*
|
|
284
|
+
* const context = { globalVar: 1 };
|
|
285
|
+
* vm.createContext(context);
|
|
286
|
+
*
|
|
287
|
+
* vm.runInContext('globalVar *= 2;', context);
|
|
288
|
+
*
|
|
289
|
+
* console.log(context);
|
|
290
|
+
* // Prints: { globalVar: 2 }
|
|
291
|
+
*
|
|
292
|
+
* console.log(global.globalVar);
|
|
293
|
+
* // Prints: 3
|
|
294
|
+
* ```
|
|
295
|
+
*
|
|
296
|
+
* If `contextObject` is omitted (or passed explicitly as `undefined`), a new,
|
|
297
|
+
* empty `contextified` object will be returned.
|
|
298
|
+
*
|
|
299
|
+
* The `vm.createContext()` method is primarily useful for creating a single
|
|
300
|
+
* context that can be used to run multiple scripts. For instance, if emulating a
|
|
301
|
+
* web browser, the method can be used to create a single context representing a
|
|
302
|
+
* window's global object, then run all `<script>` tags together within that
|
|
303
|
+
* context.
|
|
304
|
+
*
|
|
305
|
+
* The provided `name` and `origin` of the context are made visible through the
|
|
306
|
+
* Inspector API.
|
|
307
|
+
* @since v0.3.1
|
|
308
|
+
* @return contextified object.
|
|
309
|
+
*/
|
|
129
310
|
function createContext(sandbox?: Context, options?: CreateContextOptions): Context;
|
|
311
|
+
/**
|
|
312
|
+
* Returns `true` if the given `object` object has been `contextified` using {@link createContext}.
|
|
313
|
+
* @since v0.11.7
|
|
314
|
+
*/
|
|
130
315
|
function isContext(sandbox: Context): boolean;
|
|
316
|
+
/**
|
|
317
|
+
* The `vm.runInContext()` method compiles `code`, runs it within the context of
|
|
318
|
+
* the `contextifiedObject`, then returns the result. Running code does not have
|
|
319
|
+
* access to the local scope. The `contextifiedObject` object _must_ have been
|
|
320
|
+
* previously `contextified` using the {@link createContext} method.
|
|
321
|
+
*
|
|
322
|
+
* If `options` is a string, then it specifies the filename.
|
|
323
|
+
*
|
|
324
|
+
* The following example compiles and executes different scripts using a single `contextified` object:
|
|
325
|
+
*
|
|
326
|
+
* ```js
|
|
327
|
+
* const vm = require('vm');
|
|
328
|
+
*
|
|
329
|
+
* const contextObject = { globalVar: 1 };
|
|
330
|
+
* vm.createContext(contextObject);
|
|
331
|
+
*
|
|
332
|
+
* for (let i = 0; i < 10; ++i) {
|
|
333
|
+
* vm.runInContext('globalVar *= 2;', contextObject);
|
|
334
|
+
* }
|
|
335
|
+
* console.log(contextObject);
|
|
336
|
+
* // Prints: { globalVar: 1024 }
|
|
337
|
+
* ```
|
|
338
|
+
* @since v0.3.1
|
|
339
|
+
* @param code The JavaScript code to compile and run.
|
|
340
|
+
* @param contextifiedObject The `contextified` object that will be used as the `global` when the `code` is compiled and run.
|
|
341
|
+
* @return the result of the very last statement executed in the script.
|
|
342
|
+
*/
|
|
131
343
|
function runInContext(code: string, contextifiedSandbox: Context, options?: RunningScriptOptions | string): any;
|
|
344
|
+
/**
|
|
345
|
+
* The `vm.runInNewContext()` first contextifies the given `contextObject` (or
|
|
346
|
+
* creates a new `contextObject` if passed as `undefined`), compiles the `code`,
|
|
347
|
+
* runs it within the created context, then returns the result. Running code
|
|
348
|
+
* does not have access to the local scope.
|
|
349
|
+
*
|
|
350
|
+
* If `options` is a string, then it specifies the filename.
|
|
351
|
+
*
|
|
352
|
+
* The following example compiles and executes code that increments a global
|
|
353
|
+
* variable and sets a new one. These globals are contained in the `contextObject`.
|
|
354
|
+
*
|
|
355
|
+
* ```js
|
|
356
|
+
* const vm = require('vm');
|
|
357
|
+
*
|
|
358
|
+
* const contextObject = {
|
|
359
|
+
* animal: 'cat',
|
|
360
|
+
* count: 2
|
|
361
|
+
* };
|
|
362
|
+
*
|
|
363
|
+
* vm.runInNewContext('count += 1; name = "kitty"', contextObject);
|
|
364
|
+
* console.log(contextObject);
|
|
365
|
+
* // Prints: { animal: 'cat', count: 3, name: 'kitty' }
|
|
366
|
+
* ```
|
|
367
|
+
* @since v0.3.1
|
|
368
|
+
* @param code The JavaScript code to compile and run.
|
|
369
|
+
* @param contextObject An object that will be `contextified`. If `undefined`, a new object will be created.
|
|
370
|
+
* @return the result of the very last statement executed in the script.
|
|
371
|
+
*/
|
|
132
372
|
function runInNewContext(code: string, sandbox?: Context, options?: RunningScriptOptions | string): any;
|
|
373
|
+
/**
|
|
374
|
+
* `vm.runInThisContext()` compiles `code`, runs it within the context of the
|
|
375
|
+
* current `global` and returns the result. Running code does not have access to
|
|
376
|
+
* local scope, but does have access to the current `global` object.
|
|
377
|
+
*
|
|
378
|
+
* If `options` is a string, then it specifies the filename.
|
|
379
|
+
*
|
|
380
|
+
* The following example illustrates using both `vm.runInThisContext()` and
|
|
381
|
+
* the JavaScript [`eval()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) function to run the same code:
|
|
382
|
+
*
|
|
383
|
+
* ```js
|
|
384
|
+
* const vm = require('vm');
|
|
385
|
+
* let localVar = 'initial value';
|
|
386
|
+
*
|
|
387
|
+
* const vmResult = vm.runInThisContext('localVar = "vm";');
|
|
388
|
+
* console.log(`vmResult: '${vmResult}', localVar: '${localVar}'`);
|
|
389
|
+
* // Prints: vmResult: 'vm', localVar: 'initial value'
|
|
390
|
+
*
|
|
391
|
+
* const evalResult = eval('localVar = "eval";');
|
|
392
|
+
* console.log(`evalResult: '${evalResult}', localVar: '${localVar}'`);
|
|
393
|
+
* // Prints: evalResult: 'eval', localVar: 'eval'
|
|
394
|
+
* ```
|
|
395
|
+
*
|
|
396
|
+
* Because `vm.runInThisContext()` does not have access to the local scope,`localVar` is unchanged. In contrast,
|
|
397
|
+
* [`eval()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)_does_ have access to the
|
|
398
|
+
* local scope, so the value `localVar` is changed. In this way`vm.runInThisContext()` is much like an [indirect `eval()` call](https://es5.github.io/#x10.4.2), e.g.`(0,eval)('code')`.
|
|
399
|
+
*
|
|
400
|
+
* ## Example: Running an HTTP server within a VM
|
|
401
|
+
*
|
|
402
|
+
* When using either `script.runInThisContext()` or {@link runInThisContext}, the code is executed within the current V8 global
|
|
403
|
+
* context. The code passed to this VM context will have its own isolated scope.
|
|
404
|
+
*
|
|
405
|
+
* In order to run a simple web server using the `http` module the code passed to
|
|
406
|
+
* the context must either call `require('http')` on its own, or have a reference
|
|
407
|
+
* to the `http` module passed to it. For instance:
|
|
408
|
+
*
|
|
409
|
+
* ```js
|
|
410
|
+
* 'use strict';
|
|
411
|
+
* const vm = require('vm');
|
|
412
|
+
*
|
|
413
|
+
* const code = `
|
|
414
|
+
* ((require) => {
|
|
415
|
+
* const http = require('http');
|
|
416
|
+
*
|
|
417
|
+
* http.createServer((request, response) => {
|
|
418
|
+
* response.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
419
|
+
* response.end('Hello World\\n');
|
|
420
|
+
* }).listen(8124);
|
|
421
|
+
*
|
|
422
|
+
* console.log('Server running at http://127.0.0.1:8124/');
|
|
423
|
+
* })`;
|
|
424
|
+
*
|
|
425
|
+
* vm.runInThisContext(code)(require);
|
|
426
|
+
* ```
|
|
427
|
+
*
|
|
428
|
+
* The `require()` in the above case shares the state with the context it is
|
|
429
|
+
* passed from. This may introduce risks when untrusted code is executed, e.g.
|
|
430
|
+
* altering objects in the context in unwanted ways.
|
|
431
|
+
* @since v0.3.1
|
|
432
|
+
* @param code The JavaScript code to compile and run.
|
|
433
|
+
* @return the result of the very last statement executed in the script.
|
|
434
|
+
*/
|
|
133
435
|
function runInThisContext(code: string, options?: RunningScriptOptions | string): any;
|
|
436
|
+
/**
|
|
437
|
+
* Compiles the given code into the provided context (if no context is
|
|
438
|
+
* supplied, the current context is used), and returns it wrapped inside a
|
|
439
|
+
* function with the given `params`.
|
|
440
|
+
* @since v10.10.0
|
|
441
|
+
* @param code The body of the function to compile.
|
|
442
|
+
* @param params An array of strings containing all parameters for the function.
|
|
443
|
+
*/
|
|
134
444
|
function compileFunction(code: string, params?: ReadonlyArray<string>, options?: CompileFunctionOptions): Function;
|
|
135
|
-
|
|
136
445
|
/**
|
|
137
|
-
* Measure the memory known to V8 and used by
|
|
446
|
+
* Measure the memory known to V8 and used by all contexts known to the
|
|
447
|
+
* current V8 isolate, or the main context.
|
|
138
448
|
*
|
|
139
449
|
* The format of the object that the returned Promise may resolve with is
|
|
140
450
|
* specific to the V8 engine and may change from one version of V8 to the next.
|
|
141
451
|
*
|
|
142
|
-
* The returned result is different from the statistics returned by
|
|
143
|
-
*
|
|
144
|
-
* the
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
452
|
+
* The returned result is different from the statistics returned by`v8.getHeapSpaceStatistics()` in that `vm.measureMemory()` measure the
|
|
453
|
+
* memory reachable by each V8 specific contexts in the current instance of
|
|
454
|
+
* the V8 engine, while the result of `v8.getHeapSpaceStatistics()` measure
|
|
455
|
+
* the memory occupied by each heap space in the current V8 instance.
|
|
456
|
+
*
|
|
457
|
+
* ```js
|
|
458
|
+
* const vm = require('vm');
|
|
459
|
+
* // Measure the memory used by the main context.
|
|
460
|
+
* vm.measureMemory({ mode: 'summary' })
|
|
461
|
+
* // This is the same as vm.measureMemory()
|
|
462
|
+
* .then((result) => {
|
|
463
|
+
* // The current format is:
|
|
464
|
+
* // {
|
|
465
|
+
* // total: {
|
|
466
|
+
* // jsMemoryEstimate: 2418479, jsMemoryRange: [ 2418479, 2745799 ]
|
|
467
|
+
* // }
|
|
468
|
+
* // }
|
|
469
|
+
* console.log(result);
|
|
470
|
+
* });
|
|
148
471
|
*
|
|
472
|
+
* const context = vm.createContext({ a: 1 });
|
|
473
|
+
* vm.measureMemory({ mode: 'detailed', execution: 'eager' })
|
|
474
|
+
* .then((result) => {
|
|
475
|
+
* // Reference the context here so that it won't be GC'ed
|
|
476
|
+
* // until the measurement is complete.
|
|
477
|
+
* console.log(context.a);
|
|
478
|
+
* // {
|
|
479
|
+
* // total: {
|
|
480
|
+
* // jsMemoryEstimate: 2574732,
|
|
481
|
+
* // jsMemoryRange: [ 2574732, 2904372 ]
|
|
482
|
+
* // },
|
|
483
|
+
* // current: {
|
|
484
|
+
* // jsMemoryEstimate: 2438996,
|
|
485
|
+
* // jsMemoryRange: [ 2438996, 2768636 ]
|
|
486
|
+
* // },
|
|
487
|
+
* // other: [
|
|
488
|
+
* // {
|
|
489
|
+
* // jsMemoryEstimate: 135736,
|
|
490
|
+
* // jsMemoryRange: [ 135736, 465376 ]
|
|
491
|
+
* // }
|
|
492
|
+
* // ]
|
|
493
|
+
* // }
|
|
494
|
+
* console.log(result);
|
|
495
|
+
* });
|
|
496
|
+
* ```
|
|
497
|
+
* @since v13.10.0
|
|
149
498
|
* @experimental
|
|
150
499
|
*/
|
|
151
500
|
function measureMemory(options?: MeasureMemoryOptions): Promise<MemoryMeasurement>;
|
|
152
501
|
}
|
|
153
|
-
|
|
154
502
|
declare module 'node:vm' {
|
|
155
503
|
export * from 'vm';
|
|
156
504
|
}
|
node/wasi.d.ts
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The WASI API provides an implementation of the [WebAssembly System Interface](https://wasi.dev/)specification. WASI gives sandboxed WebAssembly applications access to the
|
|
3
|
+
* underlying operating system via a collection of POSIX-like functions.
|
|
4
|
+
*
|
|
5
|
+
* ```js
|
|
6
|
+
* import fs from 'fs';
|
|
7
|
+
* import { WASI } from 'wasi';
|
|
8
|
+
*
|
|
9
|
+
* const wasi = new WASI({
|
|
10
|
+
* args: process.argv,
|
|
11
|
+
* env: process.env,
|
|
12
|
+
* preopens: {
|
|
13
|
+
* '/sandbox': '/some/real/path/that/wasm/can/access'
|
|
14
|
+
* }
|
|
15
|
+
* });
|
|
16
|
+
* const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
|
|
17
|
+
*
|
|
18
|
+
* const wasm = await WebAssembly.compile(fs.readFileSync('./demo.wasm'));
|
|
19
|
+
* const instance = await WebAssembly.instantiate(wasm, importObject);
|
|
20
|
+
*
|
|
21
|
+
* wasi.start(instance);
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* ```js
|
|
25
|
+
* 'use strict';
|
|
26
|
+
* const fs = require('fs');
|
|
27
|
+
* const { WASI } = require('wasi');
|
|
28
|
+
* const wasi = new WASI({
|
|
29
|
+
* args: process.argv,
|
|
30
|
+
* env: process.env,
|
|
31
|
+
* preopens: {
|
|
32
|
+
* '/sandbox': '/some/real/path/that/wasm/can/access'
|
|
33
|
+
* }
|
|
34
|
+
* });
|
|
35
|
+
* const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
|
|
36
|
+
*
|
|
37
|
+
* (async () => {
|
|
38
|
+
* const wasm = await WebAssembly.compile(fs.readFileSync('./demo.wasm'));
|
|
39
|
+
* const instance = await WebAssembly.instantiate(wasm, importObject);
|
|
40
|
+
*
|
|
41
|
+
* wasi.start(instance);
|
|
42
|
+
* })();
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* To run the above example, create a new WebAssembly text format file named`demo.wat`:
|
|
46
|
+
*
|
|
47
|
+
* ```text
|
|
48
|
+
* (module
|
|
49
|
+
* ;; Import the required fd_write WASI function which will write the given io vectors to stdout
|
|
50
|
+
* ;; The function signature for fd_write is:
|
|
51
|
+
* ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
|
|
52
|
+
* (import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
|
|
53
|
+
*
|
|
54
|
+
* (memory 1)
|
|
55
|
+
* (export "memory" (memory 0))
|
|
56
|
+
*
|
|
57
|
+
* ;; Write 'hello world\n' to memory at an offset of 8 bytes
|
|
58
|
+
* ;; Note the trailing newline which is required for the text to appear
|
|
59
|
+
* (data (i32.const 8) "hello world\n")
|
|
60
|
+
*
|
|
61
|
+
* (func $main (export "_start")
|
|
62
|
+
* ;; Creating a new io vector within linear memory
|
|
63
|
+
* (i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
|
|
64
|
+
* (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string
|
|
65
|
+
*
|
|
66
|
+
* (call $fd_write
|
|
67
|
+
* (i32.const 1) ;; file_descriptor - 1 for stdout
|
|
68
|
+
* (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
|
|
69
|
+
* (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
|
|
70
|
+
* (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
|
|
71
|
+
* )
|
|
72
|
+
* drop ;; Discard the number of bytes written from the top of the stack
|
|
73
|
+
* )
|
|
74
|
+
* )
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* Use [wabt](https://github.com/WebAssembly/wabt) to compile `.wat` to `.wasm`
|
|
78
|
+
*
|
|
79
|
+
* ```console
|
|
80
|
+
* $ wat2wasm demo.wat
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* The `--experimental-wasi-unstable-preview1` CLI argument is needed for this
|
|
84
|
+
* example to run.
|
|
85
|
+
* @experimental
|
|
86
|
+
* @see [source](https://github.com/nodejs/node/blob/v16.4.2/lib/wasi.js)
|
|
87
|
+
*/
|
|
1
88
|
declare module 'wasi' {
|
|
2
89
|
interface WASIOptions {
|
|
3
90
|
/**
|
|
@@ -6,13 +93,11 @@ declare module 'wasi' {
|
|
|
6
93
|
* WASI command itself.
|
|
7
94
|
*/
|
|
8
95
|
args?: string[] | undefined;
|
|
9
|
-
|
|
10
96
|
/**
|
|
11
97
|
* An object similar to `process.env` that the WebAssembly
|
|
12
98
|
* application will see as its environment.
|
|
13
99
|
*/
|
|
14
100
|
env?: object | undefined;
|
|
15
|
-
|
|
16
101
|
/**
|
|
17
102
|
* This object represents the WebAssembly application's
|
|
18
103
|
* sandbox directory structure. The string keys of `preopens` are treated as
|
|
@@ -20,7 +105,6 @@ declare module 'wasi' {
|
|
|
20
105
|
* the real paths to those directories on the host machine.
|
|
21
106
|
*/
|
|
22
107
|
preopens?: NodeJS.Dict<string> | undefined;
|
|
23
|
-
|
|
24
108
|
/**
|
|
25
109
|
* By default, WASI applications terminate the Node.js
|
|
26
110
|
* process via the `__wasi_proc_exit()` function. Setting this option to `true`
|
|
@@ -29,62 +113,61 @@ declare module 'wasi' {
|
|
|
29
113
|
* @default false
|
|
30
114
|
*/
|
|
31
115
|
returnOnExit?: boolean | undefined;
|
|
32
|
-
|
|
33
116
|
/**
|
|
34
117
|
* The file descriptor used as standard input in the WebAssembly application.
|
|
35
118
|
* @default 0
|
|
36
119
|
*/
|
|
37
120
|
stdin?: number | undefined;
|
|
38
|
-
|
|
39
121
|
/**
|
|
40
122
|
* The file descriptor used as standard output in the WebAssembly application.
|
|
41
123
|
* @default 1
|
|
42
124
|
*/
|
|
43
125
|
stdout?: number | undefined;
|
|
44
|
-
|
|
45
126
|
/**
|
|
46
127
|
* The file descriptor used as standard error in the WebAssembly application.
|
|
47
128
|
* @default 2
|
|
48
129
|
*/
|
|
49
130
|
stderr?: number | undefined;
|
|
50
131
|
}
|
|
51
|
-
|
|
132
|
+
/**
|
|
133
|
+
* The `WASI` class provides the WASI system call API and additional convenience
|
|
134
|
+
* methods for working with WASI-based applications. Each `WASI` instance
|
|
135
|
+
* represents a distinct sandbox environment. For security purposes, each `WASI`instance must have its command-line arguments, environment variables, and
|
|
136
|
+
* sandbox directory structure configured explicitly.
|
|
137
|
+
* @since v13.3.0, v12.16.0
|
|
138
|
+
*/
|
|
52
139
|
class WASI {
|
|
53
140
|
constructor(options?: WASIOptions);
|
|
54
141
|
/**
|
|
142
|
+
* Attempt to begin execution of `instance` as a WASI command by invoking its`_start()` export. If `instance` does not contain a `_start()` export, or if`instance` contains an `_initialize()`
|
|
143
|
+
* export, then an exception is thrown.
|
|
55
144
|
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* invoke the `__wasi_unstable_reactor_start()` export. If neither of those exports
|
|
59
|
-
* is present on `instance`, then `start()` does nothing.
|
|
60
|
-
*
|
|
61
|
-
* `start()` requires that `instance` exports a `WebAssembly.Memory` named
|
|
62
|
-
* `memory`. If `instance` does not have a `memory` export an exception is thrown.
|
|
145
|
+
* `start()` requires that `instance` exports a [`WebAssembly.Memory`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory) named`memory`. If
|
|
146
|
+
* `instance` does not have a `memory` export an exception is thrown.
|
|
63
147
|
*
|
|
64
148
|
* If `start()` is called more than once, an exception is thrown.
|
|
149
|
+
* @since v13.3.0, v12.16.0
|
|
65
150
|
*/
|
|
66
151
|
start(instance: object): void; // TODO: avoid DOM dependency until WASM moved to own lib.
|
|
67
|
-
|
|
68
152
|
/**
|
|
69
|
-
* Attempt to initialize `instance` as a WASI reactor by invoking its
|
|
70
|
-
* If `instance` contains a `_start()` export, then an exception is thrown.
|
|
153
|
+
* Attempt to initialize `instance` as a WASI reactor by invoking its`_initialize()` export, if it is present. If `instance` contains a `_start()`export, then an exception is thrown.
|
|
71
154
|
*
|
|
72
|
-
* `
|
|
73
|
-
*
|
|
155
|
+
* `initialize()` requires that `instance` exports a [`WebAssembly.Memory`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory) named`memory`.
|
|
156
|
+
* If `instance` does not have a `memory` export an exception is thrown.
|
|
74
157
|
*
|
|
75
158
|
* If `initialize()` is called more than once, an exception is thrown.
|
|
159
|
+
* @since v14.6.0, v12.19.0
|
|
76
160
|
*/
|
|
77
161
|
initialize(instance: object): void; // TODO: avoid DOM dependency until WASM moved to own lib.
|
|
78
|
-
|
|
79
162
|
/**
|
|
80
|
-
*
|
|
81
|
-
* should be passed as the `wasi_snapshot_preview1` import during the instantiation
|
|
82
|
-
* `WebAssembly.Instance
|
|
163
|
+
* `wasiImport` is an object that implements the WASI system call API. This object
|
|
164
|
+
* should be passed as the `wasi_snapshot_preview1` import during the instantiation
|
|
165
|
+
* of a [`WebAssembly.Instance`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance).
|
|
166
|
+
* @since v13.3.0, v12.16.0
|
|
83
167
|
*/
|
|
84
168
|
readonly wasiImport: NodeJS.Dict<any>; // TODO: Narrow to DOM types
|
|
85
169
|
}
|
|
86
170
|
}
|
|
87
|
-
|
|
88
171
|
declare module 'node:wasi' {
|
|
89
172
|
export * from 'wasi';
|
|
90
173
|
}
|