|
4 | 4 | * ```js
|
5 | 5 | * const v8 = require('node:v8');
|
6 | 6 | * ```
|
7 |
| - * @see [source](https://github.com/nodejs/node/blob/v20.2.0/lib/v8.js) |
| 7 | + * @see [source](https://github.com/nodejs/node/blob/v20.11.0/lib/v8.js) |
8 | 8 | */
|
9 | 9 | declare module "v8" {
|
10 | 10 | import { Readable } from "node:stream";
|
@@ -38,6 +38,18 @@ declare module "v8" {
|
38 | 38 | bytecode_and_metadata_size: number;
|
39 | 39 | external_script_source_size: number;
|
40 | 40 | }
|
| 41 | + interface HeapSnapshotOptions { |
| 42 | + /** |
| 43 | + * If true, expose internals in the heap snapshot. |
| 44 | + * @default false |
| 45 | + */ |
| 46 | + exposeInternals?: boolean; |
| 47 | + /** |
| 48 | + * If true, expose numeric values in artificial fields. |
| 49 | + * @default false |
| 50 | + */ |
| 51 | + exposeNumericValues?: boolean; |
| 52 | + } |
41 | 53 | /**
|
42 | 54 | * Returns an integer representing a version tag derived from the V8 version,
|
43 | 55 | * command-line flags, and detected CPU features. This is useful for determining
|
@@ -105,7 +117,7 @@ declare module "v8" {
|
105 | 117 | * Returns statistics about the V8 heap spaces, i.e. the segments which make up
|
106 | 118 | * the V8 heap. Neither the ordering of heap spaces, nor the availability of a
|
107 | 119 | * heap space can be guaranteed as the statistics are provided via 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 |
| 120 | + * 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 |
109 | 121 | * next.
|
110 | 122 | *
|
111 | 123 | * The value returned is an array of objects containing the following properties:
|
@@ -194,7 +206,7 @@ declare module "v8" {
|
194 | 206 | * @since v11.13.0
|
195 | 207 | * @return A Readable containing the V8 heap snapshot.
|
196 | 208 | */
|
197 |
| - function getHeapSnapshot(): Readable; |
| 209 | + function getHeapSnapshot(options?: HeapSnapshotOptions): Readable; |
198 | 210 | /**
|
199 | 211 | * Generates a snapshot of the current V8 heap and writes it to a JSON
|
200 | 212 | * file. This file is intended to be used with tools such as Chrome
|
@@ -246,10 +258,10 @@ declare module "v8" {
|
246 | 258 | * worker thread.
|
247 | 259 | * @return The filename where the snapshot was saved.
|
248 | 260 | */
|
249 |
| - function writeHeapSnapshot(filename?: string): string; |
| 261 | + function writeHeapSnapshot(filename?: string, options?: HeapSnapshotOptions): string; |
250 | 262 | /**
|
251 | 263 | * 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 |
| 264 | + * V8 [`GetHeapCodeAndMetadataStatistics`](https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#a6079122af17612ef54ef3348ce170866) API. Returns an object with the |
253 | 265 | * following properties:
|
254 | 266 | *
|
255 | 267 | * ```js
|
@@ -408,6 +420,13 @@ declare module "v8" {
|
408 | 420 | * @since v15.1.0, v14.18.0, v12.22.0
|
409 | 421 | */
|
410 | 422 | function stopCoverage(): void;
|
| 423 | + /** |
| 424 | + * 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. |
| 425 | + * `limit` must be a positive integer. See [`--heapsnapshot-near-heap-limit`](https://nodejs.org/docs/latest-v20.x/api/cli.html#--heapsnapshot-near-heap-limitmax_count) for more information. |
| 426 | + * @experimental |
| 427 | + * @since v18.10.0, v16.18.0 |
| 428 | + */ |
| 429 | + function setHeapSnapshotNearHeapLimit(limit: number): void; |
411 | 430 | /**
|
412 | 431 | * This API collects GC data in current thread.
|
413 | 432 | * @since v19.6.0, v18.15.0
|
@@ -629,6 +648,116 @@ declare module "v8" {
|
629 | 648 | * @since v17.1.0, v16.14.0
|
630 | 649 | */
|
631 | 650 | const promiseHooks: PromiseHooks;
|
| 651 | + type StartupSnapshotCallbackFn = (args: any) => any; |
| 652 | + interface StartupSnapshot { |
| 653 | + /** |
| 654 | + * Add a callback that will be called when the Node.js instance is about to get serialized into a snapshot and exit. |
| 655 | + * 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. |
| 656 | + * @since v18.6.0, v16.17.0 |
| 657 | + */ |
| 658 | + addSerializeCallback(callback: StartupSnapshotCallbackFn, data?: any): void; |
| 659 | + /** |
| 660 | + * Add a callback that will be called when the Node.js instance is deserialized from a snapshot. |
| 661 | + * 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 |
| 662 | + * to re-acquire resources that the application needs when the application is restarted from the snapshot. |
| 663 | + * @since v18.6.0, v16.17.0 |
| 664 | + */ |
| 665 | + addDeserializeCallback(callback: StartupSnapshotCallbackFn, data?: any): void; |
| 666 | + /** |
| 667 | + * 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. |
| 668 | + * 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 |
| 669 | + * data (if provided), otherwise an entry point script still needs to be provided to the deserialized application. |
| 670 | + * @since v18.6.0, v16.17.0 |
| 671 | + */ |
| 672 | + setDeserializeMainFunction(callback: StartupSnapshotCallbackFn, data?: any): void; |
| 673 | + /** |
| 674 | + * Returns true if the Node.js instance is run to build a snapshot. |
| 675 | + * @since v18.6.0, v16.17.0 |
| 676 | + */ |
| 677 | + isBuildingSnapshot(): boolean; |
| 678 | + } |
| 679 | + /** |
| 680 | + * The `v8.startupSnapshot` interface can be used to add serialization and deserialization hooks for custom startup snapshots. |
| 681 | + * |
| 682 | + * ```bash |
| 683 | + * $ node --snapshot-blob snapshot.blob --build-snapshot entry.js |
| 684 | + * # This launches a process with the snapshot |
| 685 | + * $ node --snapshot-blob snapshot.blob |
| 686 | + * ``` |
| 687 | + * |
| 688 | + * In the example above, `entry.js` can use methods from the `v8.startupSnapshot` interface to specify how to save information for custom objects |
| 689 | + * in the snapshot during serialization and how the information can be used to synchronize these objects during deserialization of the snapshot. |
| 690 | + * For example, if the `entry.js` contains the following script: |
| 691 | + * |
| 692 | + * ```js |
| 693 | + * 'use strict'; |
| 694 | + * |
| 695 | + * const fs = require('node:fs'); |
| 696 | + * const zlib = require('node:zlib'); |
| 697 | + * const path = require('node:path'); |
| 698 | + * const assert = require('node:assert'); |
| 699 | + * |
| 700 | + * const v8 = require('node:v8'); |
| 701 | + * |
| 702 | + * class BookShelf { |
| 703 | + * storage = new Map(); |
| 704 | + * |
| 705 | + * // Reading a series of files from directory and store them into storage. |
| 706 | + * constructor(directory, books) { |
| 707 | + * for (const book of books) { |
| 708 | + * this.storage.set(book, fs.readFileSync(path.join(directory, book))); |
| 709 | + * } |
| 710 | + * } |
| 711 | + * |
| 712 | + * static compressAll(shelf) { |
| 713 | + * for (const [ book, content ] of shelf.storage) { |
| 714 | + * shelf.storage.set(book, zlib.gzipSync(content)); |
| 715 | + * } |
| 716 | + * } |
| 717 | + * |
| 718 | + * static decompressAll(shelf) { |
| 719 | + * for (const [ book, content ] of shelf.storage) { |
| 720 | + * shelf.storage.set(book, zlib.gunzipSync(content)); |
| 721 | + * } |
| 722 | + * } |
| 723 | + * } |
| 724 | + * |
| 725 | + * // __dirname here is where the snapshot script is placed |
| 726 | + * // during snapshot building time. |
| 727 | + * const shelf = new BookShelf(__dirname, [ |
| 728 | + * 'book1.en_US.txt', |
| 729 | + * 'book1.es_ES.txt', |
| 730 | + * 'book2.zh_CN.txt', |
| 731 | + * ]); |
| 732 | + * |
| 733 | + * assert(v8.startupSnapshot.isBuildingSnapshot()); |
| 734 | + * // On snapshot serialization, compress the books to reduce size. |
| 735 | + * v8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf); |
| 736 | + * // On snapshot deserialization, decompress the books. |
| 737 | + * v8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf); |
| 738 | + * v8.startupSnapshot.setDeserializeMainFunction((shelf) => { |
| 739 | + * // process.env and process.argv are refreshed during snapshot |
| 740 | + * // deserialization. |
| 741 | + * const lang = process.env.BOOK_LANG || 'en_US'; |
| 742 | + * const book = process.argv[1]; |
| 743 | + * const name = `${book}.${lang}.txt`; |
| 744 | + * console.log(shelf.storage.get(name)); |
| 745 | + * }, shelf); |
| 746 | + * ``` |
| 747 | + * |
| 748 | + * 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: |
| 749 | + * |
| 750 | + * ```bash |
| 751 | + * $ BOOK_LANG=es_ES node --snapshot-blob snapshot.blob book1 |
| 752 | + * # Prints content of book1.es_ES.txt deserialized from the snapshot. |
| 753 | + * ``` |
| 754 | + * |
| 755 | + * 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. |
| 756 | + * |
| 757 | + * @experimental |
| 758 | + * @since v18.6.0, v16.17.0 |
| 759 | + */ |
| 760 | + const startupSnapshot: StartupSnapshot; |
632 | 761 | }
|
633 | 762 | declare module "node:v8" {
|
634 | 763 | export * from "v8";
|
|
0 commit comments