@types/node 18.11.5 → 20.2.5
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.d.ts +86 -75
- node/async_hooks.d.ts +66 -37
- node/buffer.d.ts +193 -100
- node/child_process.d.ts +82 -56
- node/cluster.d.ts +12 -12
- node/console.d.ts +5 -5
- node/crypto.d.ts +233 -235
- node/dgram.d.ts +15 -15
- node/diagnostics_channel.d.ts +59 -21
- node/dns/promises.d.ts +50 -6
- node/dns.d.ts +26 -17
- node/domain.d.ts +4 -4
- node/events.d.ts +111 -65
- node/fs/promises.d.ts +99 -47
- node/fs.d.ts +244 -73
- node/globals.d.ts +6 -3
- node/http.d.ts +265 -148
- node/http2.d.ts +42 -47
- node/https.d.ts +53 -153
- node/index.d.ts +2 -1
- node/inspector.d.ts +10 -3
- node/module.d.ts +6 -4
- node/net.d.ts +32 -15
- node/os.d.ts +27 -16
- node/package.json +9 -4
- node/path.d.ts +4 -4
- node/perf_hooks.d.ts +28 -15
- node/process.d.ts +49 -46
- node/punycode.d.ts +1 -1
- node/querystring.d.ts +5 -5
- node/readline/promises.d.ts +65 -63
- node/readline.d.ts +32 -159
- node/repl.d.ts +20 -20
- node/stream/consumers.d.ts +1 -1
- node/stream.d.ts +950 -869
- node/string_decoder.d.ts +6 -6
- node/test.d.ts +877 -139
- node/timers/promises.d.ts +26 -1
- node/timers.d.ts +125 -4
- node/tls.d.ts +120 -29
- node/trace_events.d.ts +20 -9
- node/ts4.8/assert.d.ts +86 -75
- node/ts4.8/async_hooks.d.ts +66 -37
- node/ts4.8/buffer.d.ts +193 -101
- node/ts4.8/child_process.d.ts +82 -56
- node/ts4.8/cluster.d.ts +12 -12
- node/ts4.8/console.d.ts +5 -5
- node/ts4.8/crypto.d.ts +230 -233
- node/ts4.8/dgram.d.ts +15 -15
- node/ts4.8/diagnostics_channel.d.ts +59 -21
- node/ts4.8/dns/promises.d.ts +50 -6
- node/ts4.8/dns.d.ts +26 -17
- node/ts4.8/domain.d.ts +4 -4
- node/ts4.8/events.d.ts +111 -65
- node/ts4.8/fs/promises.d.ts +97 -46
- node/ts4.8/fs.d.ts +244 -73
- node/ts4.8/globals.d.ts +21 -12
- node/ts4.8/http.d.ts +265 -148
- node/ts4.8/http2.d.ts +42 -47
- node/ts4.8/https.d.ts +53 -153
- node/ts4.8/inspector.d.ts +10 -3
- node/ts4.8/module.d.ts +6 -4
- node/ts4.8/net.d.ts +33 -16
- node/ts4.8/os.d.ts +27 -16
- node/ts4.8/path.d.ts +4 -4
- node/ts4.8/perf_hooks.d.ts +28 -15
- node/ts4.8/process.d.ts +49 -46
- node/ts4.8/punycode.d.ts +1 -1
- node/ts4.8/querystring.d.ts +5 -5
- node/ts4.8/readline/promises.d.ts +65 -63
- node/ts4.8/readline.d.ts +32 -159
- node/ts4.8/repl.d.ts +20 -20
- node/ts4.8/stream/consumers.d.ts +1 -1
- node/ts4.8/stream.d.ts +192 -140
- node/ts4.8/string_decoder.d.ts +6 -6
- node/ts4.8/test.d.ts +877 -139
- node/ts4.8/timers/promises.d.ts +26 -1
- node/ts4.8/timers.d.ts +125 -4
- node/ts4.8/tls.d.ts +120 -29
- node/ts4.8/trace_events.d.ts +20 -9
- node/ts4.8/tty.d.ts +4 -5
- node/ts4.8/url.d.ts +46 -42
- node/ts4.8/util.d.ts +325 -123
- node/ts4.8/v8.d.ts +253 -14
- node/ts4.8/vm.d.ts +417 -32
- node/ts4.8/wasi.d.ts +13 -19
- node/ts4.8/worker_threads.d.ts +38 -34
- node/ts4.8/zlib.d.ts +11 -11
- node/tty.d.ts +4 -5
- node/url.d.ts +46 -42
- node/util.d.ts +325 -123
- node/v8.d.ts +253 -14
- node/vm.d.ts +417 -32
- node/wasi.d.ts +13 -19
- node/worker_threads.d.ts +38 -34
- node/zlib.d.ts +11 -11
node/ts4.8/vm.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The `vm` module enables compiling and running code within V8 Virtual
|
|
2
|
+
* The `node:vm` module enables compiling and running code within V8 Virtual
|
|
3
3
|
* Machine contexts.
|
|
4
4
|
*
|
|
5
|
-
* **The `vm` module is not a security**
|
|
5
|
+
* **The `node:vm` module is not a security**
|
|
6
6
|
* **mechanism. Do not use it to run untrusted code.**
|
|
7
7
|
*
|
|
8
8
|
* JavaScript code can be compiled and run immediately or
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* code are reflected in the context object.
|
|
18
18
|
*
|
|
19
19
|
* ```js
|
|
20
|
-
* const vm = require('vm');
|
|
20
|
+
* const vm = require('node:vm');
|
|
21
21
|
*
|
|
22
22
|
* const x = 1;
|
|
23
23
|
*
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
*
|
|
35
35
|
* console.log(x); // 1; y is not defined.
|
|
36
36
|
* ```
|
|
37
|
-
* @see [source](https://github.com/nodejs/node/blob/
|
|
37
|
+
* @see [source](https://github.com/nodejs/node/blob/v20.2.0/lib/vm.js)
|
|
38
38
|
*/
|
|
39
39
|
declare module 'vm' {
|
|
40
40
|
interface Context extends NodeJS.Dict<any> {}
|
|
@@ -56,11 +56,17 @@ declare module 'vm' {
|
|
|
56
56
|
columnOffset?: number | undefined;
|
|
57
57
|
}
|
|
58
58
|
interface ScriptOptions extends BaseOptions {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
/**
|
|
60
|
+
* V8's code cache data for the supplied source.
|
|
61
|
+
*/
|
|
62
|
+
cachedData?: Buffer | NodeJS.ArrayBufferView | undefined;
|
|
62
63
|
/** @deprecated in favor of `script.createCachedData()` */
|
|
63
64
|
produceCachedData?: boolean | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Called during evaluation of this module when `import()` is called.
|
|
67
|
+
* If this option is not specified, calls to `import()` will reject with `ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`.
|
|
68
|
+
*/
|
|
69
|
+
importModuleDynamically?: ((specifier: string, script: Script, importAssertions: Object) => Module) | undefined;
|
|
64
70
|
}
|
|
65
71
|
interface RunningScriptOptions extends BaseOptions {
|
|
66
72
|
/**
|
|
@@ -80,10 +86,31 @@ declare module 'vm' {
|
|
|
80
86
|
* Default: `false`.
|
|
81
87
|
*/
|
|
82
88
|
breakOnSigint?: boolean | undefined;
|
|
89
|
+
}
|
|
90
|
+
interface RunningScriptInNewContextOptions extends RunningScriptOptions {
|
|
91
|
+
/**
|
|
92
|
+
* Human-readable name of the newly created context.
|
|
93
|
+
*/
|
|
94
|
+
contextName?: CreateContextOptions['name'];
|
|
95
|
+
/**
|
|
96
|
+
* Origin corresponding to the newly created context for display purposes. The origin should be formatted like a URL,
|
|
97
|
+
* but with only the scheme, host, and port (if necessary), like the value of the `url.origin` property of a `URL` object.
|
|
98
|
+
* Most notably, this string should omit the trailing slash, as that denotes a path.
|
|
99
|
+
*/
|
|
100
|
+
contextOrigin?: CreateContextOptions['origin'];
|
|
101
|
+
contextCodeGeneration?: CreateContextOptions['codeGeneration'];
|
|
83
102
|
/**
|
|
84
103
|
* If set to `afterEvaluate`, microtasks will be run immediately after the script has run.
|
|
85
104
|
*/
|
|
86
|
-
microtaskMode?: '
|
|
105
|
+
microtaskMode?: CreateContextOptions['microtaskMode'];
|
|
106
|
+
}
|
|
107
|
+
interface RunningCodeOptions extends RunningScriptOptions {
|
|
108
|
+
cachedData?: ScriptOptions['cachedData'];
|
|
109
|
+
importModuleDynamically?: ScriptOptions['importModuleDynamically'];
|
|
110
|
+
}
|
|
111
|
+
interface RunningCodeInNewContextOptions extends RunningScriptInNewContextOptions {
|
|
112
|
+
cachedData?: ScriptOptions['cachedData'];
|
|
113
|
+
importModuleDynamically?: ScriptOptions['importModuleDynamically'];
|
|
87
114
|
}
|
|
88
115
|
interface CompileFunctionOptions extends BaseOptions {
|
|
89
116
|
/**
|
|
@@ -144,7 +171,10 @@ declare module 'vm' {
|
|
|
144
171
|
* @default 'summary'
|
|
145
172
|
*/
|
|
146
173
|
mode?: MeasureMemoryMode | undefined;
|
|
147
|
-
|
|
174
|
+
/**
|
|
175
|
+
* @default 'default'
|
|
176
|
+
*/
|
|
177
|
+
execution?: 'default' | 'eager' | undefined;
|
|
148
178
|
}
|
|
149
179
|
interface MemoryMeasurement {
|
|
150
180
|
total: {
|
|
@@ -158,7 +188,7 @@ declare module 'vm' {
|
|
|
158
188
|
* @since v0.3.1
|
|
159
189
|
*/
|
|
160
190
|
class Script {
|
|
161
|
-
constructor(code: string, options?: ScriptOptions);
|
|
191
|
+
constructor(code: string, options?: ScriptOptions | string);
|
|
162
192
|
/**
|
|
163
193
|
* Runs the compiled code contained by the `vm.Script` object within the given`contextifiedObject` and returns the result. Running code does not have access
|
|
164
194
|
* to local scope.
|
|
@@ -168,11 +198,11 @@ declare module 'vm' {
|
|
|
168
198
|
* The globals are contained in the `context` object.
|
|
169
199
|
*
|
|
170
200
|
* ```js
|
|
171
|
-
* const vm = require('vm');
|
|
201
|
+
* const vm = require('node:vm');
|
|
172
202
|
*
|
|
173
203
|
* const context = {
|
|
174
204
|
* animal: 'cat',
|
|
175
|
-
* count: 2
|
|
205
|
+
* count: 2,
|
|
176
206
|
* };
|
|
177
207
|
*
|
|
178
208
|
* const script = new vm.Script('count += 1; name = "kitty";');
|
|
@@ -204,7 +234,7 @@ declare module 'vm' {
|
|
|
204
234
|
* contained within each individual `context`.
|
|
205
235
|
*
|
|
206
236
|
* ```js
|
|
207
|
-
* const vm = require('vm');
|
|
237
|
+
* const vm = require('node:vm');
|
|
208
238
|
*
|
|
209
239
|
* const script = new vm.Script('globalVar = "set"');
|
|
210
240
|
*
|
|
@@ -220,7 +250,7 @@ declare module 'vm' {
|
|
|
220
250
|
* @param contextObject An object that will be `contextified`. If `undefined`, a new object will be created.
|
|
221
251
|
* @return the result of the very last statement executed in the script.
|
|
222
252
|
*/
|
|
223
|
-
runInNewContext(contextObject?: Context, options?:
|
|
253
|
+
runInNewContext(contextObject?: Context, options?: RunningScriptInNewContextOptions): any;
|
|
224
254
|
/**
|
|
225
255
|
* Runs the compiled code contained by the `vm.Script` within the context of the
|
|
226
256
|
* current `global` object. Running code does not have access to local scope, but _does_ have access to the current `global` object.
|
|
@@ -229,7 +259,7 @@ declare module 'vm' {
|
|
|
229
259
|
* executes that code multiple times:
|
|
230
260
|
*
|
|
231
261
|
* ```js
|
|
232
|
-
* const vm = require('vm');
|
|
262
|
+
* const vm = require('node:vm');
|
|
233
263
|
*
|
|
234
264
|
* global.globalVar = 0;
|
|
235
265
|
*
|
|
@@ -251,6 +281,16 @@ declare module 'vm' {
|
|
|
251
281
|
* 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
|
|
252
282
|
* time and any number of times.
|
|
253
283
|
*
|
|
284
|
+
* The code cache of the `Script` doesn't contain any JavaScript observable
|
|
285
|
+
* states. The code cache is safe to be saved along side the script source and
|
|
286
|
+
* used to construct new `Script` instances multiple times.
|
|
287
|
+
*
|
|
288
|
+
* Functions in the `Script` source can be marked as lazily compiled and they are
|
|
289
|
+
* not compiled at construction of the `Script`. These functions are going to be
|
|
290
|
+
* compiled when they are invoked the first time. The code cache serializes the
|
|
291
|
+
* metadata that V8 currently knows about the `Script` that it can use to speed up
|
|
292
|
+
* future compilations.
|
|
293
|
+
*
|
|
254
294
|
* ```js
|
|
255
295
|
* const script = new vm.Script(`
|
|
256
296
|
* function add(a, b) {
|
|
@@ -260,19 +300,46 @@ declare module 'vm' {
|
|
|
260
300
|
* const x = add(1, 2);
|
|
261
301
|
* `);
|
|
262
302
|
*
|
|
263
|
-
* const
|
|
303
|
+
* const cacheWithoutAdd = script.createCachedData();
|
|
304
|
+
* // In `cacheWithoutAdd` the function `add()` is marked for full compilation
|
|
305
|
+
* // upon invocation.
|
|
264
306
|
*
|
|
265
307
|
* script.runInThisContext();
|
|
266
308
|
*
|
|
267
|
-
* const
|
|
309
|
+
* const cacheWithAdd = script.createCachedData();
|
|
310
|
+
* // `cacheWithAdd` contains fully compiled function `add()`.
|
|
268
311
|
* ```
|
|
269
312
|
* @since v10.6.0
|
|
270
313
|
*/
|
|
271
314
|
createCachedData(): Buffer;
|
|
272
315
|
/** @deprecated in favor of `script.createCachedData()` */
|
|
273
316
|
cachedDataProduced?: boolean | undefined;
|
|
317
|
+
/**
|
|
318
|
+
* When `cachedData` is supplied to create the `vm.Script`, this value will be set
|
|
319
|
+
* to either `true` or `false` depending on acceptance of the data by V8\.
|
|
320
|
+
* Otherwise the value is `undefined`.
|
|
321
|
+
* @since v5.7.0
|
|
322
|
+
*/
|
|
274
323
|
cachedDataRejected?: boolean | undefined;
|
|
275
324
|
cachedData?: Buffer | undefined;
|
|
325
|
+
/**
|
|
326
|
+
* When the script is compiled from a source that contains a source map magic
|
|
327
|
+
* comment, this property will be set to the URL of the source map.
|
|
328
|
+
*
|
|
329
|
+
* ```js
|
|
330
|
+
* import vm from 'node:vm';
|
|
331
|
+
*
|
|
332
|
+
* const script = new vm.Script(`
|
|
333
|
+
* function myFunc() {}
|
|
334
|
+
* //# sourceMappingURL=sourcemap.json
|
|
335
|
+
* `);
|
|
336
|
+
*
|
|
337
|
+
* console.log(script.sourceMapURL);
|
|
338
|
+
* // Prints: sourcemap.json
|
|
339
|
+
* ```
|
|
340
|
+
* @since v19.1.0, v18.13.0
|
|
341
|
+
*/
|
|
342
|
+
sourceMapURL?: string | undefined;
|
|
276
343
|
}
|
|
277
344
|
/**
|
|
278
345
|
* If given a `contextObject`, the `vm.createContext()` method will `prepare
|
|
@@ -282,7 +349,7 @@ declare module 'vm' {
|
|
|
282
349
|
* will remain unchanged.
|
|
283
350
|
*
|
|
284
351
|
* ```js
|
|
285
|
-
* const vm = require('vm');
|
|
352
|
+
* const vm = require('node:vm');
|
|
286
353
|
*
|
|
287
354
|
* global.globalVar = 3;
|
|
288
355
|
*
|
|
@@ -329,7 +396,7 @@ declare module 'vm' {
|
|
|
329
396
|
* The following example compiles and executes different scripts using a single `contextified` object:
|
|
330
397
|
*
|
|
331
398
|
* ```js
|
|
332
|
-
* const vm = require('vm');
|
|
399
|
+
* const vm = require('node:vm');
|
|
333
400
|
*
|
|
334
401
|
* const contextObject = { globalVar: 1 };
|
|
335
402
|
* vm.createContext(contextObject);
|
|
@@ -345,7 +412,7 @@ declare module 'vm' {
|
|
|
345
412
|
* @param contextifiedObject The `contextified` object that will be used as the `global` when the `code` is compiled and run.
|
|
346
413
|
* @return the result of the very last statement executed in the script.
|
|
347
414
|
*/
|
|
348
|
-
function runInContext(code: string, contextifiedObject: Context, options?:
|
|
415
|
+
function runInContext(code: string, contextifiedObject: Context, options?: RunningCodeOptions | string): any;
|
|
349
416
|
/**
|
|
350
417
|
* The `vm.runInNewContext()` first contextifies the given `contextObject` (or
|
|
351
418
|
* creates a new `contextObject` if passed as `undefined`), compiles the `code`,
|
|
@@ -358,11 +425,11 @@ declare module 'vm' {
|
|
|
358
425
|
* variable and sets a new one. These globals are contained in the `contextObject`.
|
|
359
426
|
*
|
|
360
427
|
* ```js
|
|
361
|
-
* const vm = require('vm');
|
|
428
|
+
* const vm = require('node:vm');
|
|
362
429
|
*
|
|
363
430
|
* const contextObject = {
|
|
364
431
|
* animal: 'cat',
|
|
365
|
-
* count: 2
|
|
432
|
+
* count: 2,
|
|
366
433
|
* };
|
|
367
434
|
*
|
|
368
435
|
* vm.runInNewContext('count += 1; name = "kitty"', contextObject);
|
|
@@ -374,7 +441,7 @@ declare module 'vm' {
|
|
|
374
441
|
* @param contextObject An object that will be `contextified`. If `undefined`, a new object will be created.
|
|
375
442
|
* @return the result of the very last statement executed in the script.
|
|
376
443
|
*/
|
|
377
|
-
function runInNewContext(code: string, contextObject?: Context, options?:
|
|
444
|
+
function runInNewContext(code: string, contextObject?: Context, options?: RunningCodeInNewContextOptions | string): any;
|
|
378
445
|
/**
|
|
379
446
|
* `vm.runInThisContext()` compiles `code`, runs it within the context of the
|
|
380
447
|
* current `global` and returns the result. Running code does not have access to
|
|
@@ -386,7 +453,7 @@ declare module 'vm' {
|
|
|
386
453
|
* the JavaScript [`eval()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) function to run the same code:
|
|
387
454
|
*
|
|
388
455
|
* ```js
|
|
389
|
-
* const vm = require('vm');
|
|
456
|
+
* const vm = require('node:vm');
|
|
390
457
|
* let localVar = 'initial value';
|
|
391
458
|
*
|
|
392
459
|
* const vmResult = vm.runInThisContext('localVar = "vm";');
|
|
@@ -407,17 +474,17 @@ declare module 'vm' {
|
|
|
407
474
|
* When using either `script.runInThisContext()` or {@link runInThisContext}, the code is executed within the current V8 global
|
|
408
475
|
* context. The code passed to this VM context will have its own isolated scope.
|
|
409
476
|
*
|
|
410
|
-
* In order to run a simple web server using the `http` module the code passed
|
|
411
|
-
* the context must either call `require('http')` on its own, or have a
|
|
412
|
-
* to the `http` module passed to it. For instance:
|
|
477
|
+
* In order to run a simple web server using the `node:http` module the code passed
|
|
478
|
+
* to the context must either call `require('node:http')` on its own, or have a
|
|
479
|
+
* reference to the `node:http` module passed to it. For instance:
|
|
413
480
|
*
|
|
414
481
|
* ```js
|
|
415
482
|
* 'use strict';
|
|
416
|
-
* const vm = require('vm');
|
|
483
|
+
* const vm = require('node:vm');
|
|
417
484
|
*
|
|
418
485
|
* const code = `
|
|
419
486
|
* ((require) => {
|
|
420
|
-
* const http = require('http');
|
|
487
|
+
* const http = require('node:http');
|
|
421
488
|
*
|
|
422
489
|
* http.createServer((request, response) => {
|
|
423
490
|
* response.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
@@ -437,7 +504,7 @@ declare module 'vm' {
|
|
|
437
504
|
* @param code The JavaScript code to compile and run.
|
|
438
505
|
* @return the result of the very last statement executed in the script.
|
|
439
506
|
*/
|
|
440
|
-
function runInThisContext(code: string, options?:
|
|
507
|
+
function runInThisContext(code: string, options?: RunningCodeOptions | string): any;
|
|
441
508
|
/**
|
|
442
509
|
* Compiles the given code into the provided context (if no context is
|
|
443
510
|
* supplied, the current context is used), and returns it wrapped inside a
|
|
@@ -446,7 +513,15 @@ declare module 'vm' {
|
|
|
446
513
|
* @param code The body of the function to compile.
|
|
447
514
|
* @param params An array of strings containing all parameters for the function.
|
|
448
515
|
*/
|
|
449
|
-
function compileFunction(
|
|
516
|
+
function compileFunction(
|
|
517
|
+
code: string,
|
|
518
|
+
params?: ReadonlyArray<string>,
|
|
519
|
+
options?: CompileFunctionOptions
|
|
520
|
+
): Function & {
|
|
521
|
+
cachedData?: Script['cachedData'] | undefined;
|
|
522
|
+
cachedDataProduced?: Script['cachedDataProduced'] | undefined;
|
|
523
|
+
cachedDataRejected?: Script['cachedDataRejected'] | undefined;
|
|
524
|
+
};
|
|
450
525
|
/**
|
|
451
526
|
* Measure the memory known to V8 and used by all contexts known to the
|
|
452
527
|
* current V8 isolate, or the main context.
|
|
@@ -460,7 +535,7 @@ declare module 'vm' {
|
|
|
460
535
|
* the memory occupied by each heap space in the current V8 instance.
|
|
461
536
|
*
|
|
462
537
|
* ```js
|
|
463
|
-
* const vm = require('vm');
|
|
538
|
+
* const vm = require('node:vm');
|
|
464
539
|
* // Measure the memory used by the main context.
|
|
465
540
|
* vm.measureMemory({ mode: 'summary' })
|
|
466
541
|
* // This is the same as vm.measureMemory()
|
|
@@ -503,6 +578,316 @@ declare module 'vm' {
|
|
|
503
578
|
* @experimental
|
|
504
579
|
*/
|
|
505
580
|
function measureMemory(options?: MeasureMemoryOptions): Promise<MemoryMeasurement>;
|
|
581
|
+
interface ModuleEvaluateOptions {
|
|
582
|
+
timeout?: RunningScriptOptions['timeout'] | undefined;
|
|
583
|
+
breakOnSigint?: RunningScriptOptions['breakOnSigint'] | undefined;
|
|
584
|
+
}
|
|
585
|
+
type ModuleLinker = (
|
|
586
|
+
specifier: string,
|
|
587
|
+
referencingModule: Module,
|
|
588
|
+
extra: {
|
|
589
|
+
assert: Object;
|
|
590
|
+
}
|
|
591
|
+
) => Module | Promise<Module>;
|
|
592
|
+
type ModuleStatus = 'unlinked' | 'linking' | 'linked' | 'evaluating' | 'evaluated' | 'errored';
|
|
593
|
+
/**
|
|
594
|
+
* This feature is only available with the `--experimental-vm-modules` command
|
|
595
|
+
* flag enabled.
|
|
596
|
+
*
|
|
597
|
+
* The `vm.Module` class provides a low-level interface for using
|
|
598
|
+
* ECMAScript modules in VM contexts. It is the counterpart of the `vm.Script`class that closely mirrors [Module Record](https://www.ecma-international.org/ecma-262/#sec-abstract-module-records)
|
|
599
|
+
* s as defined in the ECMAScript
|
|
600
|
+
* specification.
|
|
601
|
+
*
|
|
602
|
+
* Unlike `vm.Script` however, every `vm.Module` object is bound to a context from
|
|
603
|
+
* its creation. Operations on `vm.Module` objects are intrinsically asynchronous,
|
|
604
|
+
* in contrast with the synchronous nature of `vm.Script` objects. The use of
|
|
605
|
+
* 'async' functions can help with manipulating `vm.Module` objects.
|
|
606
|
+
*
|
|
607
|
+
* Using a `vm.Module` object requires three distinct steps: creation/parsing,
|
|
608
|
+
* linking, and evaluation. These three steps are illustrated in the following
|
|
609
|
+
* example.
|
|
610
|
+
*
|
|
611
|
+
* This implementation lies at a lower level than the `ECMAScript Module
|
|
612
|
+
* loader`. There is also no way to interact with the Loader yet, though
|
|
613
|
+
* support is planned.
|
|
614
|
+
*
|
|
615
|
+
* ```js
|
|
616
|
+
* import vm from 'node:vm';
|
|
617
|
+
*
|
|
618
|
+
* const contextifiedObject = vm.createContext({
|
|
619
|
+
* secret: 42,
|
|
620
|
+
* print: console.log,
|
|
621
|
+
* });
|
|
622
|
+
*
|
|
623
|
+
* // Step 1
|
|
624
|
+
* //
|
|
625
|
+
* // Create a Module by constructing a new `vm.SourceTextModule` object. This
|
|
626
|
+
* // parses the provided source text, throwing a `SyntaxError` if anything goes
|
|
627
|
+
* // wrong. By default, a Module is created in the top context. But here, we
|
|
628
|
+
* // specify `contextifiedObject` as the context this Module belongs to.
|
|
629
|
+
* //
|
|
630
|
+
* // Here, we attempt to obtain the default export from the module "foo", and
|
|
631
|
+
* // put it into local binding "secret".
|
|
632
|
+
*
|
|
633
|
+
* const bar = new vm.SourceTextModule(`
|
|
634
|
+
* import s from 'foo';
|
|
635
|
+
* s;
|
|
636
|
+
* print(s);
|
|
637
|
+
* `, { context: contextifiedObject });
|
|
638
|
+
*
|
|
639
|
+
* // Step 2
|
|
640
|
+
* //
|
|
641
|
+
* // "Link" the imported dependencies of this Module to it.
|
|
642
|
+
* //
|
|
643
|
+
* // The provided linking callback (the "linker") accepts two arguments: the
|
|
644
|
+
* // parent module (`bar` in this case) and the string that is the specifier of
|
|
645
|
+
* // the imported module. The callback is expected to return a Module that
|
|
646
|
+
* // corresponds to the provided specifier, with certain requirements documented
|
|
647
|
+
* // in `module.link()`.
|
|
648
|
+
* //
|
|
649
|
+
* // If linking has not started for the returned Module, the same linker
|
|
650
|
+
* // callback will be called on the returned Module.
|
|
651
|
+
* //
|
|
652
|
+
* // Even top-level Modules without dependencies must be explicitly linked. The
|
|
653
|
+
* // callback provided would never be called, however.
|
|
654
|
+
* //
|
|
655
|
+
* // The link() method returns a Promise that will be resolved when all the
|
|
656
|
+
* // Promises returned by the linker resolve.
|
|
657
|
+
* //
|
|
658
|
+
* // Note: This is a contrived example in that the linker function creates a new
|
|
659
|
+
* // "foo" module every time it is called. In a full-fledged module system, a
|
|
660
|
+
* // cache would probably be used to avoid duplicated modules.
|
|
661
|
+
*
|
|
662
|
+
* async function linker(specifier, referencingModule) {
|
|
663
|
+
* if (specifier === 'foo') {
|
|
664
|
+
* return new vm.SourceTextModule(`
|
|
665
|
+
* // The "secret" variable refers to the global variable we added to
|
|
666
|
+
* // "contextifiedObject" when creating the context.
|
|
667
|
+
* export default secret;
|
|
668
|
+
* `, { context: referencingModule.context });
|
|
669
|
+
*
|
|
670
|
+
* // Using `contextifiedObject` instead of `referencingModule.context`
|
|
671
|
+
* // here would work as well.
|
|
672
|
+
* }
|
|
673
|
+
* throw new Error(`Unable to resolve dependency: ${specifier}`);
|
|
674
|
+
* }
|
|
675
|
+
* await bar.link(linker);
|
|
676
|
+
*
|
|
677
|
+
* // Step 3
|
|
678
|
+
* //
|
|
679
|
+
* // Evaluate the Module. The evaluate() method returns a promise which will
|
|
680
|
+
* // resolve after the module has finished evaluating.
|
|
681
|
+
*
|
|
682
|
+
* // Prints 42.
|
|
683
|
+
* await bar.evaluate();
|
|
684
|
+
* ```
|
|
685
|
+
* @since v13.0.0, v12.16.0
|
|
686
|
+
* @experimental
|
|
687
|
+
*/
|
|
688
|
+
class Module {
|
|
689
|
+
/**
|
|
690
|
+
* The specifiers of all dependencies of this module. The returned array is frozen
|
|
691
|
+
* to disallow any changes to it.
|
|
692
|
+
*
|
|
693
|
+
* Corresponds to the `[[RequestedModules]]` field of [Cyclic Module Record](https://tc39.es/ecma262/#sec-cyclic-module-records) s in
|
|
694
|
+
* the ECMAScript specification.
|
|
695
|
+
*/
|
|
696
|
+
dependencySpecifiers: readonly string[];
|
|
697
|
+
/**
|
|
698
|
+
* If the `module.status` is `'errored'`, this property contains the exception
|
|
699
|
+
* thrown by the module during evaluation. If the status is anything else,
|
|
700
|
+
* accessing this property will result in a thrown exception.
|
|
701
|
+
*
|
|
702
|
+
* The value `undefined` cannot be used for cases where there is not a thrown
|
|
703
|
+
* exception due to possible ambiguity with `throw undefined;`.
|
|
704
|
+
*
|
|
705
|
+
* Corresponds to the `[[EvaluationError]]` field of [Cyclic Module Record](https://tc39.es/ecma262/#sec-cyclic-module-records) s
|
|
706
|
+
* in the ECMAScript specification.
|
|
707
|
+
*/
|
|
708
|
+
error: any;
|
|
709
|
+
/**
|
|
710
|
+
* The identifier of the current module, as set in the constructor.
|
|
711
|
+
*/
|
|
712
|
+
identifier: string;
|
|
713
|
+
context: Context;
|
|
714
|
+
/**
|
|
715
|
+
* The namespace object of the module. This is only available after linking
|
|
716
|
+
* (`module.link()`) has completed.
|
|
717
|
+
*
|
|
718
|
+
* Corresponds to the [GetModuleNamespace](https://tc39.es/ecma262/#sec-getmodulenamespace) abstract operation in the ECMAScript
|
|
719
|
+
* specification.
|
|
720
|
+
*/
|
|
721
|
+
namespace: Object;
|
|
722
|
+
/**
|
|
723
|
+
* The current status of the module. Will be one of:
|
|
724
|
+
*
|
|
725
|
+
* * `'unlinked'`: `module.link()` has not yet been called.
|
|
726
|
+
* * `'linking'`: `module.link()` has been called, but not all Promises returned
|
|
727
|
+
* by the linker function have been resolved yet.
|
|
728
|
+
* * `'linked'`: The module has been linked successfully, and all of its
|
|
729
|
+
* dependencies are linked, but `module.evaluate()` has not yet been called.
|
|
730
|
+
* * `'evaluating'`: The module is being evaluated through a `module.evaluate()` on
|
|
731
|
+
* itself or a parent module.
|
|
732
|
+
* * `'evaluated'`: The module has been successfully evaluated.
|
|
733
|
+
* * `'errored'`: The module has been evaluated, but an exception was thrown.
|
|
734
|
+
*
|
|
735
|
+
* Other than `'errored'`, this status string corresponds to the specification's [Cyclic Module Record](https://tc39.es/ecma262/#sec-cyclic-module-records)'s `[[Status]]` field. `'errored'`
|
|
736
|
+
* corresponds to`'evaluated'` in the specification, but with `[[EvaluationError]]` set to a
|
|
737
|
+
* value that is not `undefined`.
|
|
738
|
+
*/
|
|
739
|
+
status: ModuleStatus;
|
|
740
|
+
/**
|
|
741
|
+
* Evaluate the module.
|
|
742
|
+
*
|
|
743
|
+
* This must be called after the module has been linked; otherwise it will reject.
|
|
744
|
+
* It could be called also when the module has already been evaluated, in which
|
|
745
|
+
* case it will either do nothing if the initial evaluation ended in success
|
|
746
|
+
* (`module.status` is `'evaluated'`) or it will re-throw the exception that the
|
|
747
|
+
* initial evaluation resulted in (`module.status` is `'errored'`).
|
|
748
|
+
*
|
|
749
|
+
* This method cannot be called while the module is being evaluated
|
|
750
|
+
* (`module.status` is `'evaluating'`).
|
|
751
|
+
*
|
|
752
|
+
* Corresponds to the [Evaluate() concrete method](https://tc39.es/ecma262/#sec-moduleevaluation) field of [Cyclic Module Record](https://tc39.es/ecma262/#sec-cyclic-module-records) s in the
|
|
753
|
+
* ECMAScript specification.
|
|
754
|
+
* @return Fulfills with `undefined` upon success.
|
|
755
|
+
*/
|
|
756
|
+
evaluate(options?: ModuleEvaluateOptions): Promise<void>;
|
|
757
|
+
/**
|
|
758
|
+
* Link module dependencies. This method must be called before evaluation, and
|
|
759
|
+
* can only be called once per module.
|
|
760
|
+
*
|
|
761
|
+
* The function is expected to return a `Module` object or a `Promise` that
|
|
762
|
+
* eventually resolves to a `Module` object. The returned `Module` must satisfy the
|
|
763
|
+
* following two invariants:
|
|
764
|
+
*
|
|
765
|
+
* * It must belong to the same context as the parent `Module`.
|
|
766
|
+
* * Its `status` must not be `'errored'`.
|
|
767
|
+
*
|
|
768
|
+
* If the returned `Module`'s `status` is `'unlinked'`, this method will be
|
|
769
|
+
* recursively called on the returned `Module` with the same provided `linker`function.
|
|
770
|
+
*
|
|
771
|
+
* `link()` returns a `Promise` that will either get resolved when all linking
|
|
772
|
+
* instances resolve to a valid `Module`, or rejected if the linker function either
|
|
773
|
+
* throws an exception or returns an invalid `Module`.
|
|
774
|
+
*
|
|
775
|
+
* The linker function roughly corresponds to the implementation-defined [HostResolveImportedModule](https://tc39.es/ecma262/#sec-hostresolveimportedmodule) abstract operation in the
|
|
776
|
+
* ECMAScript
|
|
777
|
+
* specification, with a few key differences:
|
|
778
|
+
*
|
|
779
|
+
* * The linker function is allowed to be asynchronous while [HostResolveImportedModule](https://tc39.es/ecma262/#sec-hostresolveimportedmodule) is synchronous.
|
|
780
|
+
*
|
|
781
|
+
* The actual [HostResolveImportedModule](https://tc39.es/ecma262/#sec-hostresolveimportedmodule) implementation used during module
|
|
782
|
+
* linking is one that returns the modules linked during linking. Since at
|
|
783
|
+
* that point all modules would have been fully linked already, the [HostResolveImportedModule](https://tc39.es/ecma262/#sec-hostresolveimportedmodule) implementation is fully synchronous per
|
|
784
|
+
* specification.
|
|
785
|
+
*
|
|
786
|
+
* Corresponds to the [Link() concrete method](https://tc39.es/ecma262/#sec-moduledeclarationlinking) field of [Cyclic Module Record](https://tc39.es/ecma262/#sec-cyclic-module-records) s in
|
|
787
|
+
* the ECMAScript specification.
|
|
788
|
+
*/
|
|
789
|
+
link(linker: ModuleLinker): Promise<void>;
|
|
790
|
+
}
|
|
791
|
+
interface SourceTextModuleOptions {
|
|
792
|
+
/**
|
|
793
|
+
* String used in stack traces.
|
|
794
|
+
* @default 'vm:module(i)' where i is a context-specific ascending index.
|
|
795
|
+
*/
|
|
796
|
+
identifier?: string | undefined;
|
|
797
|
+
cachedData?: ScriptOptions['cachedData'] | undefined;
|
|
798
|
+
context?: Context | undefined;
|
|
799
|
+
lineOffset?: BaseOptions['lineOffset'] | undefined;
|
|
800
|
+
columnOffset?: BaseOptions['columnOffset'] | undefined;
|
|
801
|
+
/**
|
|
802
|
+
* Called during evaluation of this module to initialize the `import.meta`.
|
|
803
|
+
*/
|
|
804
|
+
initializeImportMeta?: ((meta: ImportMeta, module: SourceTextModule) => void) | undefined;
|
|
805
|
+
importModuleDynamically?: ScriptOptions['importModuleDynamically'] | undefined;
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* This feature is only available with the `--experimental-vm-modules` command
|
|
809
|
+
* flag enabled.
|
|
810
|
+
*
|
|
811
|
+
*
|
|
812
|
+
*
|
|
813
|
+
* The `vm.SourceTextModule` class provides the [Source Text Module Record](https://tc39.es/ecma262/#sec-source-text-module-records) as
|
|
814
|
+
* defined in the ECMAScript specification.
|
|
815
|
+
* @since v9.6.0
|
|
816
|
+
* @experimental
|
|
817
|
+
*/
|
|
818
|
+
class SourceTextModule extends Module {
|
|
819
|
+
/**
|
|
820
|
+
* Creates a new `SourceTextModule` instance.
|
|
821
|
+
* @param code JavaScript Module code to parse
|
|
822
|
+
*/
|
|
823
|
+
constructor(code: string, options?: SourceTextModuleOptions);
|
|
824
|
+
}
|
|
825
|
+
interface SyntheticModuleOptions {
|
|
826
|
+
/**
|
|
827
|
+
* String used in stack traces.
|
|
828
|
+
* @default 'vm:module(i)' where i is a context-specific ascending index.
|
|
829
|
+
*/
|
|
830
|
+
identifier?: string | undefined;
|
|
831
|
+
/**
|
|
832
|
+
* The contextified object as returned by the `vm.createContext()` method, to compile and evaluate this module in.
|
|
833
|
+
*/
|
|
834
|
+
context?: Context | undefined;
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
837
|
+
* This feature is only available with the `--experimental-vm-modules` command
|
|
838
|
+
* flag enabled.
|
|
839
|
+
*
|
|
840
|
+
*
|
|
841
|
+
*
|
|
842
|
+
* The `vm.SyntheticModule` class provides the [Synthetic Module Record](https://heycam.github.io/webidl/#synthetic-module-records) as
|
|
843
|
+
* defined in the WebIDL specification. The purpose of synthetic modules is to
|
|
844
|
+
* provide a generic interface for exposing non-JavaScript sources to ECMAScript
|
|
845
|
+
* module graphs.
|
|
846
|
+
*
|
|
847
|
+
* ```js
|
|
848
|
+
* const vm = require('node:vm');
|
|
849
|
+
*
|
|
850
|
+
* const source = '{ "a": 1 }';
|
|
851
|
+
* const module = new vm.SyntheticModule(['default'], function() {
|
|
852
|
+
* const obj = JSON.parse(source);
|
|
853
|
+
* this.setExport('default', obj);
|
|
854
|
+
* });
|
|
855
|
+
*
|
|
856
|
+
* // Use `module` in linking...
|
|
857
|
+
* ```
|
|
858
|
+
* @since v13.0.0, v12.16.0
|
|
859
|
+
* @experimental
|
|
860
|
+
*/
|
|
861
|
+
class SyntheticModule extends Module {
|
|
862
|
+
/**
|
|
863
|
+
* Creates a new `SyntheticModule` instance.
|
|
864
|
+
* @param exportNames Array of names that will be exported from the module.
|
|
865
|
+
* @param evaluateCallback Called when the module is evaluated.
|
|
866
|
+
*/
|
|
867
|
+
constructor(exportNames: string[], evaluateCallback: (this: SyntheticModule) => void, options?: SyntheticModuleOptions);
|
|
868
|
+
/**
|
|
869
|
+
* This method is used after the module is linked to set the values of exports. If
|
|
870
|
+
* it is called before the module is linked, an `ERR_VM_MODULE_STATUS` error
|
|
871
|
+
* will be thrown.
|
|
872
|
+
*
|
|
873
|
+
* ```js
|
|
874
|
+
* import vm from 'node:vm';
|
|
875
|
+
*
|
|
876
|
+
* const m = new vm.SyntheticModule(['x'], () => {
|
|
877
|
+
* m.setExport('x', 1);
|
|
878
|
+
* });
|
|
879
|
+
*
|
|
880
|
+
* await m.link(() => {});
|
|
881
|
+
* await m.evaluate();
|
|
882
|
+
*
|
|
883
|
+
* assert.strictEqual(m.namespace.x, 1);
|
|
884
|
+
* ```
|
|
885
|
+
* @since v13.0.0, v12.16.0
|
|
886
|
+
* @param name Name of the export to set.
|
|
887
|
+
* @param value The value to set the export to.
|
|
888
|
+
*/
|
|
889
|
+
setExport(name: string, value: any): void;
|
|
890
|
+
}
|
|
506
891
|
}
|
|
507
892
|
declare module 'node:vm' {
|
|
508
893
|
export * from 'vm';
|