Skip to content

Commit bf7d12a

Browse files
author
codershiba
authored
🤖 Merge PR DefinitelyTyped#68264 [@types/node] Update v8 type definitions by @codershiba
1 parent a343d9e commit bf7d12a

File tree

12 files changed

+1314
-96
lines changed

12 files changed

+1314
-96
lines changed

‎types/node/test/v8.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import * as assert from "node:assert";
12
import { Readable } from "node:stream";
23
import * as v8 from "node:v8";
4+
import * as zlib from "node:zlib";
35

46
const heapStats = v8.getHeapStatistics();
57
const numOfDetached = heapStats.number_of_detached_contexts;
@@ -34,3 +36,39 @@ const stopInit = v8.promiseHooks.onInit((promise, parent) => {});
3436
const stopBefore = v8.promiseHooks.onBefore(promise => {});
3537
const stopAfter = v8.promiseHooks.onAfter(promise => {});
3638
const stopSettled = v8.promiseHooks.onSettled(promise => {});
39+
40+
class BookShelf {
41+
storage: Map<string, Buffer | string> = new Map();
42+
43+
constructor(bookNames: string[]) {
44+
for (const bookName of bookNames) {
45+
this.storage.set(bookName, bookName);
46+
}
47+
}
48+
49+
static compressAll(shelf: BookShelf): void {
50+
for (const [book, content] of shelf.storage) {
51+
shelf.storage.set(book, zlib.gzipSync(content));
52+
}
53+
}
54+
55+
static decompressAll(shelf: BookShelf): void {
56+
for (const [book, content] of shelf.storage) {
57+
shelf.storage.set(book, zlib.gunzipSync(content));
58+
}
59+
}
60+
}
61+
62+
const bookNames = ["book1.en_US", "book1.es_ES", "book2.zh_CN"];
63+
64+
const shelf = new BookShelf(bookNames);
65+
66+
assert(v8.startupSnapshot.isBuildingSnapshot());
67+
v8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf);
68+
v8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf);
69+
v8.startupSnapshot.setDeserializeMainFunction((shelf: BookShelf): void => {
70+
const lang = "en_US";
71+
const book = process.argv[1];
72+
const name = `${book}.${lang}`;
73+
console.log(shelf.storage.get(name));
74+
}, shelf);

‎types/node/ts4.8/test/v8.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import * as assert from "node:assert";
12
import { Readable } from "node:stream";
23
import * as v8 from "node:v8";
4+
import * as zlib from "node:zlib";
35

46
const heapStats = v8.getHeapStatistics();
57
const numOfDetached = heapStats.number_of_detached_contexts;
@@ -34,3 +36,39 @@ const stopInit = v8.promiseHooks.onInit((promise, parent) => {});
3436
const stopBefore = v8.promiseHooks.onBefore(promise => {});
3537
const stopAfter = v8.promiseHooks.onAfter(promise => {});
3638
const stopSettled = v8.promiseHooks.onSettled(promise => {});
39+
40+
class BookShelf {
41+
storage: Map<string, Buffer | string> = new Map();
42+
43+
constructor(bookNames: string[]) {
44+
for (const bookName of bookNames) {
45+
this.storage.set(bookName, bookName);
46+
}
47+
}
48+
49+
static compressAll(shelf: BookShelf): void {
50+
for (const [book, content] of shelf.storage) {
51+
shelf.storage.set(book, zlib.gzipSync(content));
52+
}
53+
}
54+
55+
static decompressAll(shelf: BookShelf): void {
56+
for (const [book, content] of shelf.storage) {
57+
shelf.storage.set(book, zlib.gunzipSync(content));
58+
}
59+
}
60+
}
61+
62+
const bookNames = ["book1.en_US", "book1.es_ES", "book2.zh_CN"];
63+
64+
const shelf = new BookShelf(bookNames);
65+
66+
assert(v8.startupSnapshot.isBuildingSnapshot());
67+
v8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf);
68+
v8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf);
69+
v8.startupSnapshot.setDeserializeMainFunction((shelf: BookShelf): void => {
70+
const lang = "en_US";
71+
const book = process.argv[1];
72+
const name = `${book}.${lang}`;
73+
console.log(shelf.storage.get(name));
74+
}, shelf);

‎types/node/ts4.8/v8.d.ts

Lines changed: 134 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* ```js
55
* const v8 = require('node:v8');
66
* ```
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)
88
*/
99
declare module "v8" {
1010
import { Readable } from "node:stream";
@@ -38,6 +38,18 @@ declare module "v8" {
3838
bytecode_and_metadata_size: number;
3939
external_script_source_size: number;
4040
}
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+
}
4153
/**
4254
* Returns an integer representing a version tag derived from the V8 version,
4355
* command-line flags, and detected CPU features. This is useful for determining
@@ -105,7 +117,7 @@ declare module "v8" {
105117
* Returns statistics about the V8 heap spaces, i.e. the segments which make up
106118
* the V8 heap. Neither the ordering of heap spaces, nor the availability of a
107119
* 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
109121
* next.
110122
*
111123
* The value returned is an array of objects containing the following properties:
@@ -194,7 +206,7 @@ declare module "v8" {
194206
* @since v11.13.0
195207
* @return A Readable containing the V8 heap snapshot.
196208
*/
197-
function getHeapSnapshot(): Readable;
209+
function getHeapSnapshot(options?: HeapSnapshotOptions): Readable;
198210
/**
199211
* Generates a snapshot of the current V8 heap and writes it to a JSON
200212
* file. This file is intended to be used with tools such as Chrome
@@ -246,10 +258,10 @@ declare module "v8" {
246258
* worker thread.
247259
* @return The filename where the snapshot was saved.
248260
*/
249-
function writeHeapSnapshot(filename?: string): string;
261+
function writeHeapSnapshot(filename?: string, options?: HeapSnapshotOptions): string;
250262
/**
251263
* 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
253265
* following properties:
254266
*
255267
* ```js
@@ -408,6 +420,13 @@ declare module "v8" {
408420
* @since v15.1.0, v14.18.0, v12.22.0
409421
*/
410422
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;
411430
/**
412431
* This API collects GC data in current thread.
413432
* @since v19.6.0, v18.15.0
@@ -629,6 +648,116 @@ declare module "v8" {
629648
* @since v17.1.0, v16.14.0
630649
*/
631650
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;
632761
}
633762
declare module "node:v8" {
634763
export * from "v8";

‎types/node/v16/test/v8.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import * as assert from "node:assert";
12
import { Readable } from "node:stream";
23
import * as v8 from "node:v8";
4+
import * as zlib from "node:zlib";
35

46
const heapStats = v8.getHeapStatistics();
57
const numOfDetached = heapStats.number_of_detached_contexts;
@@ -26,3 +28,39 @@ const stopInit = v8.promiseHooks.onInit((promise, parent) => {});
2628
const stopBefore = v8.promiseHooks.onBefore(promise => {});
2729
const stopAfter = v8.promiseHooks.onAfter(promise => {});
2830
const stopSettled = v8.promiseHooks.onSettled(promise => {});
31+
32+
class BookShelf {
33+
storage: Map<string, Buffer | string> = new Map();
34+
35+
constructor(bookNames: string[]) {
36+
for (const bookName of bookNames) {
37+
this.storage.set(bookName, bookName);
38+
}
39+
}
40+
41+
static compressAll(shelf: BookShelf): void {
42+
for (const [book, content] of shelf.storage) {
43+
shelf.storage.set(book, zlib.gzipSync(content));
44+
}
45+
}
46+
47+
static decompressAll(shelf: BookShelf): void {
48+
for (const [book, content] of shelf.storage) {
49+
shelf.storage.set(book, zlib.gunzipSync(content));
50+
}
51+
}
52+
}
53+
54+
const bookNames = ["book1.en_US", "book1.es_ES", "book2.zh_CN"];
55+
56+
const shelf = new BookShelf(bookNames);
57+
58+
assert(v8.startupSnapshot.isBuildingSnapshot());
59+
v8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf);
60+
v8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf);
61+
v8.startupSnapshot.setDeserializeMainFunction((shelf: BookShelf): void => {
62+
const lang = "en_US";
63+
const book = process.argv[1];
64+
const name = `${book}.${lang}`;
65+
console.log(shelf.storage.get(name));
66+
}, shelf);

‎types/node/v16/ts4.8/test/v8.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import * as assert from "node:assert";
12
import { Readable } from "node:stream";
23
import * as v8 from "node:v8";
4+
import * as zlib from "node:zlib";
35

46
const heapStats = v8.getHeapStatistics();
57
const numOfDetached = heapStats.number_of_detached_contexts;
@@ -26,3 +28,39 @@ const stopInit = v8.promiseHooks.onInit((promise, parent) => {});
2628
const stopBefore = v8.promiseHooks.onBefore(promise => {});
2729
const stopAfter = v8.promiseHooks.onAfter(promise => {});
2830
const stopSettled = v8.promiseHooks.onSettled(promise => {});
31+
32+
class BookShelf {
33+
storage: Map<string, Buffer | string> = new Map();
34+
35+
constructor(bookNames: string[]) {
36+
for (const bookName of bookNames) {
37+
this.storage.set(bookName, bookName);
38+
}
39+
}
40+
41+
static compressAll(shelf: BookShelf): void {
42+
for (const [book, content] of shelf.storage) {
43+
shelf.storage.set(book, zlib.gzipSync(content));
44+
}
45+
}
46+
47+
static decompressAll(shelf: BookShelf): void {
48+
for (const [book, content] of shelf.storage) {
49+
shelf.storage.set(book, zlib.gunzipSync(content));
50+
}
51+
}
52+
}
53+
54+
const bookNames = ["book1.en_US", "book1.es_ES", "book2.zh_CN"];
55+
56+
const shelf = new BookShelf(bookNames);
57+
58+
assert(v8.startupSnapshot.isBuildingSnapshot());
59+
v8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf);
60+
v8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf);
61+
v8.startupSnapshot.setDeserializeMainFunction((shelf: BookShelf): void => {
62+
const lang = "en_US";
63+
const book = process.argv[1];
64+
const name = `${book}.${lang}`;
65+
console.log(shelf.storage.get(name));
66+
}, shelf);

0 commit comments

Comments
 (0)