@types/node 16.18.79 → 16.18.80

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 v16.18/README.md CHANGED
@@ -8,7 +8,7 @@ This package contains type definitions for node (https://nodejs.org/).
8
8
  Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node/v16.
9
9
 
10
10
  ### Additional Details
11
- * Last updated: Thu, 01 Feb 2024 17:35:23 GMT
11
+ * Last updated: Thu, 08 Feb 2024 20:35:44 GMT
12
12
  * Dependencies: none
13
13
 
14
14
  # Credits
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@types/node",
3
- "version": "16.18.79",
3
+ "version": "16.18.80",
4
4
  "description": "TypeScript definitions for node",
5
5
  "homepage": "https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node",
6
6
  "license": "MIT",
@@ -222,6 +222,6 @@
222
222
  },
223
223
  "scripts": {},
224
224
  "dependencies": {},
225
- "typesPublisherContentHash": "052d62dae6ffb01bc2ee31005dcedbade3b7287f6e0ea97a6302c5d2fd41aec9",
225
+ "typesPublisherContentHash": "0747ef1ff42367a2c659aab5fc827387e23d727f290cd39647ba32a7618902a6",
226
226
  "typeScriptVersion": "4.6"
227
227
  }
@@ -1,10 +1,10 @@
1
1
  /**
2
- * The `v8` module exposes APIs that are specific to the version of [V8](https://developers.google.com/v8/) built into the Node.js binary. It can be accessed using:
2
+ * The `node:v8` module exposes APIs that are specific to the version of [V8](https://developers.google.com/v8/) built into the Node.js binary. It can be accessed using:
3
3
  *
4
4
  * ```js
5
- * const v8 = require('v8');
5
+ * const v8 = require('node:v8');
6
6
  * ```
7
- * @see [source](https://github.com/nodejs/node/blob/v16.9.0/lib/v8.js)
7
+ * @see [source](https://github.com/nodejs/node/blob/v16.20.2/lib/v8.js)
8
8
  */
9
9
  declare module "v8" {
10
10
  import { Readable } from "node:stream";
@@ -29,6 +29,9 @@ declare module "v8" {
29
29
  does_zap_garbage: DoesZapCodeSpaceFlag;
30
30
  number_of_native_contexts: number;
31
31
  number_of_detached_contexts: number;
32
+ total_global_handles_size: number;
33
+ used_global_handles_size: number;
34
+ external_memory: number;
32
35
  }
33
36
  interface HeapCodeStatistics {
34
37
  code_and_metadata_size: number;
@@ -68,6 +71,15 @@ declare module "v8" {
68
71
  * of contexts that were detached and not yet garbage collected. This number
69
72
  * being non-zero indicates a potential memory leak.
70
73
  *
74
+ * `total_global_handles_size` The value of total\_global\_handles\_size is the
75
+ * total memory size of V8 global handles.
76
+ *
77
+ * `used_global_handles_size` The value of used\_global\_handles\_size is the
78
+ * used memory size of V8 global handles.
79
+ *
80
+ * `external_memory` The value of external\_memory is the memory size of array
81
+ * buffers and external strings.
82
+ *
71
83
  * ```js
72
84
  * {
73
85
  * total_heap_size: 7326976,
@@ -80,7 +92,10 @@ declare module "v8" {
80
92
  * peak_malloced_memory: 1127496,
81
93
  * does_zap_garbage: 0,
82
94
  * number_of_native_contexts: 1,
83
- * number_of_detached_contexts: 0
95
+ * number_of_detached_contexts: 0,
96
+ * total_global_handles_size: 8192,
97
+ * used_global_handles_size: 3296,
98
+ * external_memory: 318824
84
99
  * }
85
100
  * ```
86
101
  * @since v1.0.0
@@ -90,7 +105,7 @@ declare module "v8" {
90
105
  * Returns statistics about the V8 heap spaces, i.e. the segments which make up
91
106
  * the V8 heap. Neither the ordering of heap spaces, nor the availability of a
92
107
  * heap space can be guaranteed as the statistics are provided via the
93
- * V8[`GetHeapSpaceStatistics`](https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33378f8f57e1d13a4) function and may change from one V8 version to the
108
+ * V8 [`GetHeapSpaceStatistics`](https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33378f8f57e1d13a4) function and may change from one V8 version to the
94
109
  * next.
95
110
  *
96
111
  * The value returned is an array of objects containing the following properties:
@@ -149,7 +164,7 @@ declare module "v8" {
149
164
  *
150
165
  * ```js
151
166
  * // Print GC events to stdout for one minute.
152
- * const v8 = require('v8');
167
+ * const v8 = require('node:v8');
153
168
  * v8.setFlagsFromString('--trace_gc');
154
169
  * setTimeout(() => { v8.setFlagsFromString('--notrace_gc'); }, 60e3);
155
170
  * ```
@@ -163,9 +178,16 @@ declare module "v8" {
163
178
  * Chrome DevTools. The JSON schema is undocumented and specific to the
164
179
  * V8 engine. Therefore, the schema may change from one version of V8 to the next.
165
180
  *
181
+ * Creating a heap snapshot requires memory about twice the size of the heap at
182
+ * the time the snapshot is created. This results in the risk of OOM killers
183
+ * terminating the process.
184
+ *
185
+ * Generating a snapshot is a synchronous operation which blocks the event loop
186
+ * for a duration depending on the heap size.
187
+ *
166
188
  * ```js
167
189
  * // Print heap snapshot to the console
168
- * const v8 = require('v8');
190
+ * const v8 = require('node:v8');
169
191
  * const stream = v8.getHeapSnapshot();
170
192
  * stream.pipe(process.stdout);
171
193
  * ```
@@ -182,13 +204,20 @@ declare module "v8" {
182
204
  * A heap snapshot is specific to a single V8 isolate. When using `worker threads`, a heap snapshot generated from the main thread will
183
205
  * not contain any information about the workers, and vice versa.
184
206
  *
207
+ * Creating a heap snapshot requires memory about twice the size of the heap at
208
+ * the time the snapshot is created. This results in the risk of OOM killers
209
+ * terminating the process.
210
+ *
211
+ * Generating a snapshot is a synchronous operation which blocks the event loop
212
+ * for a duration depending on the heap size.
213
+ *
185
214
  * ```js
186
- * const { writeHeapSnapshot } = require('v8');
215
+ * const { writeHeapSnapshot } = require('node:v8');
187
216
  * const {
188
217
  * Worker,
189
218
  * isMainThread,
190
- * parentPort
191
- * } = require('worker_threads');
219
+ * parentPort,
220
+ * } = require('node:worker_threads');
192
221
  *
193
222
  * if (isMainThread) {
194
223
  * const worker = new Worker(__filename);
@@ -219,13 +248,16 @@ declare module "v8" {
219
248
  */
220
249
  function writeHeapSnapshot(filename?: string): string;
221
250
  /**
222
- * Returns an object with the following properties:
251
+ * Get statistics about code and its metadata in the heap, see
252
+ * V8 [`GetHeapCodeAndMetadataStatistics`](https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#a6079122af17612ef54ef3348ce170866) API. Returns an object with the
253
+ * following properties:
223
254
  *
224
255
  * ```js
225
256
  * {
226
257
  * code_and_metadata_size: 212208,
227
258
  * bytecode_and_metadata_size: 161368,
228
- * external_script_source_size: 1410794
259
+ * external_script_source_size: 1410794,
260
+ * cpu_profiler_metadata_size: 0,
229
261
  * }
230
262
  * ```
231
263
  * @since v12.8.0
@@ -275,7 +307,7 @@ declare module "v8" {
275
307
  */
276
308
  writeDouble(value: number): void;
277
309
  /**
278
- * Write raw bytes into the serializers internal buffer. The deserializer
310
+ * Write raw bytes into the serializer's internal buffer. The deserializer
279
311
  * will require a way to compute the length of the buffer.
280
312
  * For use inside of a custom `serializer._writeHostObject()`.
281
313
  */
@@ -331,7 +363,7 @@ declare module "v8" {
331
363
  */
332
364
  readDouble(): number;
333
365
  /**
334
- * Read raw bytes from the deserializers internal buffer. The `length` parameter
366
+ * Read raw bytes from the deserializer's internal buffer. The `length` parameter
335
367
  * must correspond to the length of the buffer that was passed to `serializer.writeRawBytes()`.
336
368
  * For use inside of a custom `deserializer._readHostObject()`.
337
369
  */
@@ -344,6 +376,10 @@ declare module "v8" {
344
376
  class DefaultDeserializer extends Deserializer {}
345
377
  /**
346
378
  * Uses a `DefaultSerializer` to serialize `value` into a buffer.
379
+ *
380
+ * `ERR_BUFFER_TOO_LARGE` will be thrown when trying to
381
+ * serialize a huge object which requires buffer
382
+ * larger than `buffer.constants.MAX_LENGTH`.
347
383
  * @since v8.0.0
348
384
  */
349
385
  function serialize(value: any): Buffer;
@@ -362,20 +398,27 @@ declare module "v8" {
362
398
  *
363
399
  * When the process is about to exit, one last coverage will still be written to
364
400
  * disk unless {@link stopCoverage} is invoked before the process exits.
365
- * @since v15.1.0, v12.22.0
401
+ * @since v15.1.0, v14.18.0, v12.22.0
366
402
  */
367
403
  function takeCoverage(): void;
368
404
  /**
369
405
  * The `v8.stopCoverage()` method allows the user to stop the coverage collection
370
406
  * started by `NODE_V8_COVERAGE`, so that V8 can release the execution count
371
407
  * records and optimize code. This can be used in conjunction with {@link takeCoverage} if the user wants to collect the coverage on demand.
372
- * @since v15.1.0, v12.22.0
408
+ * @since v15.1.0, v14.18.0, v12.22.0
373
409
  */
374
410
  function stopCoverage(): void;
411
+ /**
412
+ * The API is a no-op if `--heapsnapshot-near-heap-limit` is already set from the command line or the API is called more than once.
413
+ * `limit` must be a positive integer. See [`--heapsnapshot-near-heap-limit`](https://nodejs.org/docs/latest-v16.x/api/cli.html#--heapsnapshot-near-heap-limitmax_count) for more information.
414
+ * @experimental
415
+ * @since v16.18.0
416
+ */
417
+ function setHeapSnapshotNearHeapLimit(limit: number): void;
375
418
  /**
376
419
  * Called when a promise is constructed. This does not mean that corresponding before/after events will occur, only that the possibility exists. This will
377
420
  * happen if a promise is created without ever getting a continuation.
378
- * @since v17.1.0, v16.14.0
421
+ * @since v16.14.0
379
422
  * @param promise The promise being created.
380
423
  * @param parent The promise continued from, if applicable.
381
424
  */
@@ -387,14 +430,14 @@ declare module "v8" {
387
430
  *
388
431
  * The before callback will be called 0 to N times. The before callback will typically be called 0 times if no continuation was ever made for the promise.
389
432
  * The before callback may be called many times in the case where many continuations have been made from the same promise.
390
- * @since v17.1.0, v16.14.0
433
+ * @since v16.14.0
391
434
  */
392
435
  interface Before {
393
436
  (promise: Promise<unknown>): void;
394
437
  }
395
438
  /**
396
439
  * Called immediately after a promise continuation executes. This may be after a `then()`, `catch()`, or `finally()` handler or before an await after another await.
397
- * @since v17.1.0, v16.14.0
440
+ * @since v16.14.0
398
441
  */
399
442
  interface After {
400
443
  (promise: Promise<unknown>): void;
@@ -402,7 +445,7 @@ declare module "v8" {
402
445
  /**
403
446
  * Called when the promise receives a resolution or rejection value. This may occur synchronously in the case of {@link Promise.resolve()} or
404
447
  * {@link Promise.reject()}.
405
- * @since v17.1.0, v16.14.0
448
+ * @since v16.14.0
406
449
  */
407
450
  interface Settled {
408
451
  (promise: Promise<unknown>): void;
@@ -413,7 +456,7 @@ declare module "v8" {
413
456
  *
414
457
  * Because promises are asynchronous resources whose lifecycle is tracked via the promise hooks mechanism, the `init()`, `before()`, `after()`, and
415
458
  * `settled()` callbacks must not be async functions as they create more promises which would produce an infinite loop.
416
- * @since v17.1.0, v16.14.0
459
+ * @since v16.14.0
417
460
  */
418
461
  interface HookCallbacks {
419
462
  init?: Init;
@@ -424,28 +467,28 @@ declare module "v8" {
424
467
  interface PromiseHooks {
425
468
  /**
426
469
  * The `init` hook must be a plain function. Providing an async function will throw as it would produce an infinite microtask loop.
427
- * @since v17.1.0, v16.14.0
470
+ * @since v16.14.0
428
471
  * @param init The {@link Init | `init` callback} to call when a promise is created.
429
472
  * @return Call to stop the hook.
430
473
  */
431
474
  onInit: (init: Init) => Function;
432
475
  /**
433
476
  * The `settled` hook must be a plain function. Providing an async function will throw as it would produce an infinite microtask loop.
434
- * @since v17.1.0, v16.14.0
477
+ * @since v16.14.0
435
478
  * @param settled The {@link Settled | `settled` callback} to call when a promise is created.
436
479
  * @return Call to stop the hook.
437
480
  */
438
481
  onSettled: (settled: Settled) => Function;
439
482
  /**
440
483
  * The `before` hook must be a plain function. Providing an async function will throw as it would produce an infinite microtask loop.
441
- * @since v17.1.0, v16.14.0
484
+ * @since v16.14.0
442
485
  * @param before The {@link Before | `before` callback} to call before a promise continuation executes.
443
486
  * @return Call to stop the hook.
444
487
  */
445
488
  onBefore: (before: Before) => Function;
446
489
  /**
447
490
  * The `after` hook must be a plain function. Providing an async function will throw as it would produce an infinite microtask loop.
448
- * @since v17.1.0, v16.14.0
491
+ * @since v16.14.0
449
492
  * @param after The {@link After | `after` callback} to call after a promise continuation executes.
450
493
  * @return Call to stop the hook.
451
494
  */
@@ -455,7 +498,7 @@ declare module "v8" {
455
498
  * The callbacks `init()`/`before()`/`after()`/`settled()` are called for the respective events during a promise's lifetime.
456
499
  * All callbacks are optional. For example, if only promise creation needs to be tracked, then only the init callback needs to be passed.
457
500
  * The hook callbacks must be plain functions. Providing async functions will throw as it would produce an infinite microtask loop.
458
- * @since v17.1.0, v16.14.0
501
+ * @since v16.14.0
459
502
  * @param callbacks The {@link HookCallbacks | Hook Callbacks} to register
460
503
  * @return Used for disabling hooks
461
504
  */
@@ -463,9 +506,120 @@ declare module "v8" {
463
506
  }
464
507
  /**
465
508
  * The `promiseHooks` interface can be used to track promise lifecycle events.
466
- * @since v17.1.0, v16.14.0
509
+ * @since v16.14.0
467
510
  */
468
511
  const promiseHooks: PromiseHooks;
512
+ type StartupSnapshotCallbackFn = (args: any) => any;
513
+ interface StartupSnapshot {
514
+ /**
515
+ * Add a callback that will be called when the Node.js instance is about to get serialized into a snapshot and exit.
516
+ * This can be used to release resources that should not or cannot be serialized or to convert user data into a form more suitable for serialization.
517
+ * @since v16.17.0
518
+ */
519
+ addSerializeCallback(callback: StartupSnapshotCallbackFn, data?: any): void;
520
+ /**
521
+ * Add a callback that will be called when the Node.js instance is deserialized from a snapshot.
522
+ * The `callback` and the `data` (if provided) will be serialized into the snapshot, they can be used to re-initialize the state of the application or
523
+ * to re-acquire resources that the application needs when the application is restarted from the snapshot.
524
+ * @since v16.17.0
525
+ */
526
+ addDeserializeCallback(callback: StartupSnapshotCallbackFn, data?: any): void;
527
+ /**
528
+ * This sets the entry point of the Node.js application when it is deserialized from a snapshot. This can be called only once in the snapshot building script.
529
+ * If called, the deserialized application no longer needs an additional entry point script to start up and will simply invoke the callback along with the deserialized
530
+ * data (if provided), otherwise an entry point script still needs to be provided to the deserialized application.
531
+ * @since v16.17.0
532
+ */
533
+ setDeserializeMainFunction(callback: StartupSnapshotCallbackFn, data?: any): void;
534
+ /**
535
+ * Returns true if the Node.js instance is run to build a snapshot.
536
+ * @since v16.17.0
537
+ */
538
+ isBuildingSnapshot(): boolean;
539
+ }
540
+ /**
541
+ * The `v8.startupSnapshot` interface can be used to add serialization and deserialization hooks for custom startup snapshots.
542
+ *
543
+ * ```bash
544
+ * $ node --snapshot-blob snapshot.blob --build-snapshot entry.js
545
+ * # This launches a process with the snapshot
546
+ * $ node --snapshot-blob snapshot.blob
547
+ * ```
548
+ *
549
+ * In the example above, `entry.js` can use methods from the `v8.startupSnapshot` interface to specify how to save information for custom objects
550
+ * in the snapshot during serialization
551
+ * and how the information can be used to synchronize these objects during deserialization of the snapshot.
552
+ * For example, if the `entry.js` contains the following script:
553
+ *
554
+ * ```js
555
+ * 'use strict';
556
+ *
557
+ * const fs = require('node:fs');
558
+ * const zlib = require('node:zlib');
559
+ * const path = require('node:path');
560
+ * const assert = require('node:assert');
561
+ *
562
+ * const v8 = require('node:v8');
563
+ *
564
+ * class BookShelf {
565
+ * storage = new Map();
566
+ *
567
+ * // Reading a series of files from directory and store them into storage.
568
+ * constructor(directory, books) {
569
+ * for (const book of books) {
570
+ * this.storage.set(book, fs.readFileSync(path.join(directory, book)));
571
+ * }
572
+ * }
573
+ *
574
+ * static compressAll(shelf) {
575
+ * for (const [ book, content ] of shelf.storage) {
576
+ * shelf.storage.set(book, zlib.gzipSync(content));
577
+ * }
578
+ * }
579
+ *
580
+ * static decompressAll(shelf) {
581
+ * for (const [ book, content ] of shelf.storage) {
582
+ * shelf.storage.set(book, zlib.gunzipSync(content));
583
+ * }
584
+ * }
585
+ * }
586
+ *
587
+ * // __dirname here is where the snapshot script is placed
588
+ * // during snapshot building time.
589
+ * const shelf = new BookShelf(__dirname, [
590
+ * 'book1.en_US.txt',
591
+ * 'book1.es_ES.txt',
592
+ * 'book2.zh_CN.txt',
593
+ * ]);
594
+ *
595
+ * assert(v8.startupSnapshot.isBuildingSnapshot());
596
+ * // On snapshot serialization, compress the books to reduce size.
597
+ * v8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf);
598
+ * // On snapshot deserialization, decompress the books.
599
+ * v8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf);
600
+ * v8.startupSnapshot.setDeserializeMainFunction((shelf) => {
601
+ * // process.env and process.argv are refreshed during snapshot
602
+ * // deserialization.
603
+ * const lang = process.env.BOOK_LANG || 'en_US';
604
+ * const book = process.argv[1];
605
+ * const name = `${book}.${lang}.txt`;
606
+ * console.log(shelf.storage.get(name));
607
+ * }, shelf);
608
+ * ```
609
+ *
610
+ * The resulted binary will get print the data deserialized from the snapshot during start up, using the refreshed `process.env` and `process.argv` of the launched process:
611
+ *
612
+ * ```bash
613
+ * $ BOOK_LANG=es_ES node --snapshot-blob snapshot.blob book1
614
+ * # Prints content of book1.es_ES.txt deserialized from the snapshot.
615
+ * ```
616
+ *
617
+ * Currently the application deserialized from a user-land snapshot cannot be snapshotted again, so these APIs are only available to applications that are not deserialized from a user-land snapshot.
618
+ *
619
+ * @experimental
620
+ * @since v16.17.0
621
+ */
622
+ const startupSnapshot: StartupSnapshot;
469
623
  }
470
624
  declare module "node:v8" {
471
625
  export * from "v8";
node v16.18/v8.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  /**
2
- * The `v8` module exposes APIs that are specific to the version of [V8](https://developers.google.com/v8/) built into the Node.js binary. It can be accessed using:
2
+ * The `node:v8` module exposes APIs that are specific to the version of [V8](https://developers.google.com/v8/) built into the Node.js binary. It can be accessed using:
3
3
  *
4
4
  * ```js
5
- * const v8 = require('v8');
5
+ * const v8 = require('node:v8');
6
6
  * ```
7
- * @see [source](https://github.com/nodejs/node/blob/v16.9.0/lib/v8.js)
7
+ * @see [source](https://github.com/nodejs/node/blob/v16.20.2/lib/v8.js)
8
8
  */
9
9
  declare module "v8" {
10
10
  import { Readable } from "node:stream";
@@ -29,6 +29,9 @@ declare module "v8" {
29
29
  does_zap_garbage: DoesZapCodeSpaceFlag;
30
30
  number_of_native_contexts: number;
31
31
  number_of_detached_contexts: number;
32
+ total_global_handles_size: number;
33
+ used_global_handles_size: number;
34
+ external_memory: number;
32
35
  }
33
36
  interface HeapCodeStatistics {
34
37
  code_and_metadata_size: number;
@@ -68,6 +71,15 @@ declare module "v8" {
68
71
  * of contexts that were detached and not yet garbage collected. This number
69
72
  * being non-zero indicates a potential memory leak.
70
73
  *
74
+ * `total_global_handles_size` The value of total\_global\_handles\_size is the
75
+ * total memory size of V8 global handles.
76
+ *
77
+ * `used_global_handles_size` The value of used\_global\_handles\_size is the
78
+ * used memory size of V8 global handles.
79
+ *
80
+ * `external_memory` The value of external\_memory is the memory size of array
81
+ * buffers and external strings.
82
+ *
71
83
  * ```js
72
84
  * {
73
85
  * total_heap_size: 7326976,
@@ -80,7 +92,10 @@ declare module "v8" {
80
92
  * peak_malloced_memory: 1127496,
81
93
  * does_zap_garbage: 0,
82
94
  * number_of_native_contexts: 1,
83
- * number_of_detached_contexts: 0
95
+ * number_of_detached_contexts: 0,
96
+ * total_global_handles_size: 8192,
97
+ * used_global_handles_size: 3296,
98
+ * external_memory: 318824
84
99
  * }
85
100
  * ```
86
101
  * @since v1.0.0
@@ -90,7 +105,7 @@ declare module "v8" {
90
105
  * Returns statistics about the V8 heap spaces, i.e. the segments which make up
91
106
  * the V8 heap. Neither the ordering of heap spaces, nor the availability of a
92
107
  * heap space can be guaranteed as the statistics are provided via the
93
- * V8[`GetHeapSpaceStatistics`](https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33378f8f57e1d13a4) function and may change from one V8 version to the
108
+ * V8 [`GetHeapSpaceStatistics`](https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33378f8f57e1d13a4) function and may change from one V8 version to the
94
109
  * next.
95
110
  *
96
111
  * The value returned is an array of objects containing the following properties:
@@ -149,7 +164,7 @@ declare module "v8" {
149
164
  *
150
165
  * ```js
151
166
  * // Print GC events to stdout for one minute.
152
- * const v8 = require('v8');
167
+ * const v8 = require('node:v8');
153
168
  * v8.setFlagsFromString('--trace_gc');
154
169
  * setTimeout(() => { v8.setFlagsFromString('--notrace_gc'); }, 60e3);
155
170
  * ```
@@ -163,9 +178,16 @@ declare module "v8" {
163
178
  * Chrome DevTools. The JSON schema is undocumented and specific to the
164
179
  * V8 engine. Therefore, the schema may change from one version of V8 to the next.
165
180
  *
181
+ * Creating a heap snapshot requires memory about twice the size of the heap at
182
+ * the time the snapshot is created. This results in the risk of OOM killers
183
+ * terminating the process.
184
+ *
185
+ * Generating a snapshot is a synchronous operation which blocks the event loop
186
+ * for a duration depending on the heap size.
187
+ *
166
188
  * ```js
167
189
  * // Print heap snapshot to the console
168
- * const v8 = require('v8');
190
+ * const v8 = require('node:v8');
169
191
  * const stream = v8.getHeapSnapshot();
170
192
  * stream.pipe(process.stdout);
171
193
  * ```
@@ -182,13 +204,20 @@ declare module "v8" {
182
204
  * A heap snapshot is specific to a single V8 isolate. When using `worker threads`, a heap snapshot generated from the main thread will
183
205
  * not contain any information about the workers, and vice versa.
184
206
  *
207
+ * Creating a heap snapshot requires memory about twice the size of the heap at
208
+ * the time the snapshot is created. This results in the risk of OOM killers
209
+ * terminating the process.
210
+ *
211
+ * Generating a snapshot is a synchronous operation which blocks the event loop
212
+ * for a duration depending on the heap size.
213
+ *
185
214
  * ```js
186
- * const { writeHeapSnapshot } = require('v8');
215
+ * const { writeHeapSnapshot } = require('node:v8');
187
216
  * const {
188
217
  * Worker,
189
218
  * isMainThread,
190
- * parentPort
191
- * } = require('worker_threads');
219
+ * parentPort,
220
+ * } = require('node:worker_threads');
192
221
  *
193
222
  * if (isMainThread) {
194
223
  * const worker = new Worker(__filename);
@@ -219,13 +248,16 @@ declare module "v8" {
219
248
  */
220
249
  function writeHeapSnapshot(filename?: string): string;
221
250
  /**
222
- * Returns an object with the following properties:
251
+ * Get statistics about code and its metadata in the heap, see
252
+ * V8 [`GetHeapCodeAndMetadataStatistics`](https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#a6079122af17612ef54ef3348ce170866) API. Returns an object with the
253
+ * following properties:
223
254
  *
224
255
  * ```js
225
256
  * {
226
257
  * code_and_metadata_size: 212208,
227
258
  * bytecode_and_metadata_size: 161368,
228
- * external_script_source_size: 1410794
259
+ * external_script_source_size: 1410794,
260
+ * cpu_profiler_metadata_size: 0,
229
261
  * }
230
262
  * ```
231
263
  * @since v12.8.0
@@ -275,7 +307,7 @@ declare module "v8" {
275
307
  */
276
308
  writeDouble(value: number): void;
277
309
  /**
278
- * Write raw bytes into the serializers internal buffer. The deserializer
310
+ * Write raw bytes into the serializer's internal buffer. The deserializer
279
311
  * will require a way to compute the length of the buffer.
280
312
  * For use inside of a custom `serializer._writeHostObject()`.
281
313
  */
@@ -331,7 +363,7 @@ declare module "v8" {
331
363
  */
332
364
  readDouble(): number;
333
365
  /**
334
- * Read raw bytes from the deserializers internal buffer. The `length` parameter
366
+ * Read raw bytes from the deserializer's internal buffer. The `length` parameter
335
367
  * must correspond to the length of the buffer that was passed to `serializer.writeRawBytes()`.
336
368
  * For use inside of a custom `deserializer._readHostObject()`.
337
369
  */
@@ -344,6 +376,10 @@ declare module "v8" {
344
376
  class DefaultDeserializer extends Deserializer {}
345
377
  /**
346
378
  * Uses a `DefaultSerializer` to serialize `value` into a buffer.
379
+ *
380
+ * `ERR_BUFFER_TOO_LARGE` will be thrown when trying to
381
+ * serialize a huge object which requires buffer
382
+ * larger than `buffer.constants.MAX_LENGTH`.
347
383
  * @since v8.0.0
348
384
  */
349
385
  function serialize(value: any): Buffer;
@@ -362,20 +398,27 @@ declare module "v8" {
362
398
  *
363
399
  * When the process is about to exit, one last coverage will still be written to
364
400
  * disk unless {@link stopCoverage} is invoked before the process exits.
365
- * @since v15.1.0, v12.22.0
401
+ * @since v15.1.0, v14.18.0, v12.22.0
366
402
  */
367
403
  function takeCoverage(): void;
368
404
  /**
369
405
  * The `v8.stopCoverage()` method allows the user to stop the coverage collection
370
406
  * started by `NODE_V8_COVERAGE`, so that V8 can release the execution count
371
407
  * records and optimize code. This can be used in conjunction with {@link takeCoverage} if the user wants to collect the coverage on demand.
372
- * @since v15.1.0, v12.22.0
408
+ * @since v15.1.0, v14.18.0, v12.22.0
373
409
  */
374
410
  function stopCoverage(): void;
411
+ /**
412
+ * The API is a no-op if `--heapsnapshot-near-heap-limit` is already set from the command line or the API is called more than once.
413
+ * `limit` must be a positive integer. See [`--heapsnapshot-near-heap-limit`](https://nodejs.org/docs/latest-v16.x/api/cli.html#--heapsnapshot-near-heap-limitmax_count) for more information.
414
+ * @experimental
415
+ * @since v16.18.0
416
+ */
417
+ function setHeapSnapshotNearHeapLimit(limit: number): void;
375
418
  /**
376
419
  * Called when a promise is constructed. This does not mean that corresponding before/after events will occur, only that the possibility exists. This will
377
420
  * happen if a promise is created without ever getting a continuation.
378
- * @since v17.1.0, v16.14.0
421
+ * @since v16.14.0
379
422
  * @param promise The promise being created.
380
423
  * @param parent The promise continued from, if applicable.
381
424
  */
@@ -387,14 +430,14 @@ declare module "v8" {
387
430
  *
388
431
  * The before callback will be called 0 to N times. The before callback will typically be called 0 times if no continuation was ever made for the promise.
389
432
  * The before callback may be called many times in the case where many continuations have been made from the same promise.
390
- * @since v17.1.0, v16.14.0
433
+ * @since v16.14.0
391
434
  */
392
435
  interface Before {
393
436
  (promise: Promise<unknown>): void;
394
437
  }
395
438
  /**
396
439
  * Called immediately after a promise continuation executes. This may be after a `then()`, `catch()`, or `finally()` handler or before an await after another await.
397
- * @since v17.1.0, v16.14.0
440
+ * @since v16.14.0
398
441
  */
399
442
  interface After {
400
443
  (promise: Promise<unknown>): void;
@@ -402,7 +445,7 @@ declare module "v8" {
402
445
  /**
403
446
  * Called when the promise receives a resolution or rejection value. This may occur synchronously in the case of {@link Promise.resolve()} or
404
447
  * {@link Promise.reject()}.
405
- * @since v17.1.0, v16.14.0
448
+ * @since v16.14.0
406
449
  */
407
450
  interface Settled {
408
451
  (promise: Promise<unknown>): void;
@@ -413,7 +456,7 @@ declare module "v8" {
413
456
  *
414
457
  * Because promises are asynchronous resources whose lifecycle is tracked via the promise hooks mechanism, the `init()`, `before()`, `after()`, and
415
458
  * `settled()` callbacks must not be async functions as they create more promises which would produce an infinite loop.
416
- * @since v17.1.0, v16.14.0
459
+ * @since v16.14.0
417
460
  */
418
461
  interface HookCallbacks {
419
462
  init?: Init;
@@ -424,28 +467,28 @@ declare module "v8" {
424
467
  interface PromiseHooks {
425
468
  /**
426
469
  * The `init` hook must be a plain function. Providing an async function will throw as it would produce an infinite microtask loop.
427
- * @since v17.1.0, v16.14.0
470
+ * @since v16.14.0
428
471
  * @param init The {@link Init | `init` callback} to call when a promise is created.
429
472
  * @return Call to stop the hook.
430
473
  */
431
474
  onInit: (init: Init) => Function;
432
475
  /**
433
476
  * The `settled` hook must be a plain function. Providing an async function will throw as it would produce an infinite microtask loop.
434
- * @since v17.1.0, v16.14.0
477
+ * @since v16.14.0
435
478
  * @param settled The {@link Settled | `settled` callback} to call when a promise is created.
436
479
  * @return Call to stop the hook.
437
480
  */
438
481
  onSettled: (settled: Settled) => Function;
439
482
  /**
440
483
  * The `before` hook must be a plain function. Providing an async function will throw as it would produce an infinite microtask loop.
441
- * @since v17.1.0, v16.14.0
484
+ * @since v16.14.0
442
485
  * @param before The {@link Before | `before` callback} to call before a promise continuation executes.
443
486
  * @return Call to stop the hook.
444
487
  */
445
488
  onBefore: (before: Before) => Function;
446
489
  /**
447
490
  * The `after` hook must be a plain function. Providing an async function will throw as it would produce an infinite microtask loop.
448
- * @since v17.1.0, v16.14.0
491
+ * @since v16.14.0
449
492
  * @param after The {@link After | `after` callback} to call after a promise continuation executes.
450
493
  * @return Call to stop the hook.
451
494
  */
@@ -455,7 +498,7 @@ declare module "v8" {
455
498
  * The callbacks `init()`/`before()`/`after()`/`settled()` are called for the respective events during a promise's lifetime.
456
499
  * All callbacks are optional. For example, if only promise creation needs to be tracked, then only the init callback needs to be passed.
457
500
  * The hook callbacks must be plain functions. Providing async functions will throw as it would produce an infinite microtask loop.
458
- * @since v17.1.0, v16.14.0
501
+ * @since v16.14.0
459
502
  * @param callbacks The {@link HookCallbacks | Hook Callbacks} to register
460
503
  * @return Used for disabling hooks
461
504
  */
@@ -463,9 +506,120 @@ declare module "v8" {
463
506
  }
464
507
  /**
465
508
  * The `promiseHooks` interface can be used to track promise lifecycle events.
466
- * @since v17.1.0, v16.14.0
509
+ * @since v16.14.0
467
510
  */
468
511
  const promiseHooks: PromiseHooks;
512
+ type StartupSnapshotCallbackFn = (args: any) => any;
513
+ interface StartupSnapshot {
514
+ /**
515
+ * Add a callback that will be called when the Node.js instance is about to get serialized into a snapshot and exit.
516
+ * This can be used to release resources that should not or cannot be serialized or to convert user data into a form more suitable for serialization.
517
+ * @since v16.17.0
518
+ */
519
+ addSerializeCallback(callback: StartupSnapshotCallbackFn, data?: any): void;
520
+ /**
521
+ * Add a callback that will be called when the Node.js instance is deserialized from a snapshot.
522
+ * The `callback` and the `data` (if provided) will be serialized into the snapshot, they can be used to re-initialize the state of the application or
523
+ * to re-acquire resources that the application needs when the application is restarted from the snapshot.
524
+ * @since v16.17.0
525
+ */
526
+ addDeserializeCallback(callback: StartupSnapshotCallbackFn, data?: any): void;
527
+ /**
528
+ * This sets the entry point of the Node.js application when it is deserialized from a snapshot. This can be called only once in the snapshot building script.
529
+ * If called, the deserialized application no longer needs an additional entry point script to start up and will simply invoke the callback along with the deserialized
530
+ * data (if provided), otherwise an entry point script still needs to be provided to the deserialized application.
531
+ * @since v16.17.0
532
+ */
533
+ setDeserializeMainFunction(callback: StartupSnapshotCallbackFn, data?: any): void;
534
+ /**
535
+ * Returns true if the Node.js instance is run to build a snapshot.
536
+ * @since v16.17.0
537
+ */
538
+ isBuildingSnapshot(): boolean;
539
+ }
540
+ /**
541
+ * The `v8.startupSnapshot` interface can be used to add serialization and deserialization hooks for custom startup snapshots.
542
+ *
543
+ * ```bash
544
+ * $ node --snapshot-blob snapshot.blob --build-snapshot entry.js
545
+ * # This launches a process with the snapshot
546
+ * $ node --snapshot-blob snapshot.blob
547
+ * ```
548
+ *
549
+ * In the example above, `entry.js` can use methods from the `v8.startupSnapshot` interface to specify how to save information for custom objects
550
+ * in the snapshot during serialization
551
+ * and how the information can be used to synchronize these objects during deserialization of the snapshot.
552
+ * For example, if the `entry.js` contains the following script:
553
+ *
554
+ * ```js
555
+ * 'use strict';
556
+ *
557
+ * const fs = require('node:fs');
558
+ * const zlib = require('node:zlib');
559
+ * const path = require('node:path');
560
+ * const assert = require('node:assert');
561
+ *
562
+ * const v8 = require('node:v8');
563
+ *
564
+ * class BookShelf {
565
+ * storage = new Map();
566
+ *
567
+ * // Reading a series of files from directory and store them into storage.
568
+ * constructor(directory, books) {
569
+ * for (const book of books) {
570
+ * this.storage.set(book, fs.readFileSync(path.join(directory, book)));
571
+ * }
572
+ * }
573
+ *
574
+ * static compressAll(shelf) {
575
+ * for (const [ book, content ] of shelf.storage) {
576
+ * shelf.storage.set(book, zlib.gzipSync(content));
577
+ * }
578
+ * }
579
+ *
580
+ * static decompressAll(shelf) {
581
+ * for (const [ book, content ] of shelf.storage) {
582
+ * shelf.storage.set(book, zlib.gunzipSync(content));
583
+ * }
584
+ * }
585
+ * }
586
+ *
587
+ * // __dirname here is where the snapshot script is placed
588
+ * // during snapshot building time.
589
+ * const shelf = new BookShelf(__dirname, [
590
+ * 'book1.en_US.txt',
591
+ * 'book1.es_ES.txt',
592
+ * 'book2.zh_CN.txt',
593
+ * ]);
594
+ *
595
+ * assert(v8.startupSnapshot.isBuildingSnapshot());
596
+ * // On snapshot serialization, compress the books to reduce size.
597
+ * v8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf);
598
+ * // On snapshot deserialization, decompress the books.
599
+ * v8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf);
600
+ * v8.startupSnapshot.setDeserializeMainFunction((shelf) => {
601
+ * // process.env and process.argv are refreshed during snapshot
602
+ * // deserialization.
603
+ * const lang = process.env.BOOK_LANG || 'en_US';
604
+ * const book = process.argv[1];
605
+ * const name = `${book}.${lang}.txt`;
606
+ * console.log(shelf.storage.get(name));
607
+ * }, shelf);
608
+ * ```
609
+ *
610
+ * The resulted binary will get print the data deserialized from the snapshot during start up, using the refreshed `process.env` and `process.argv` of the launched process:
611
+ *
612
+ * ```bash
613
+ * $ BOOK_LANG=es_ES node --snapshot-blob snapshot.blob book1
614
+ * # Prints content of book1.es_ES.txt deserialized from the snapshot.
615
+ * ```
616
+ *
617
+ * Currently the application deserialized from a user-land snapshot cannot be snapshotted again, so these APIs are only available to applications that are not deserialized from a user-land snapshot.
618
+ *
619
+ * @experimental
620
+ * @since v16.17.0
621
+ */
622
+ const startupSnapshot: StartupSnapshot;
469
623
  }
470
624
  declare module "node:v8" {
471
625
  export * from "v8";