@types/node 18.19.13 → 18.19.15

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 v18.19/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/v18.
9
9
 
10
10
  ### Additional Details
11
- * Last updated: Thu, 01 Feb 2024 09:07:23 GMT
11
+ * Last updated: Thu, 08 Feb 2024 20:35:44 GMT
12
12
  * Dependencies: [undici-types](https://npmjs.com/package/undici-types)
13
13
 
14
14
  # Credits
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@types/node",
3
- "version": "18.19.13",
3
+ "version": "18.19.15",
4
4
  "description": "TypeScript definitions for node",
5
5
  "homepage": "https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node",
6
6
  "license": "MIT",
@@ -229,6 +229,6 @@
229
229
  "dependencies": {
230
230
  "undici-types": "~5.26.4"
231
231
  },
232
- "typesPublisherContentHash": "f5779be3e9e21495114131cf9b6f2e3d214d5a79f005223dcb824f7899cc8a86",
232
+ "typesPublisherContentHash": "ad9df99b7321a505717fdcd00214972319bf28852142b759e0020d0db2061bfb",
233
233
  "typeScriptVersion": "4.6"
234
234
  }
@@ -3531,12 +3531,13 @@ declare module "crypto" {
3531
3531
  */
3532
3532
  disableEntropyCache?: boolean | undefined;
3533
3533
  }
3534
+ type UUID = `${string}-${string}-${string}-${string}-${string}`;
3534
3535
  /**
3535
3536
  * Generates a random [RFC 4122](https://www.rfc-editor.org/rfc/rfc4122.txt) version 4 UUID. The UUID is generated using a
3536
3537
  * cryptographic pseudorandom number generator.
3537
3538
  * @since v15.6.0, v14.17.0
3538
3539
  */
3539
- function randomUUID(options?: RandomUUIDOptions): string;
3540
+ function randomUUID(options?: RandomUUIDOptions): UUID;
3540
3541
  interface X509CheckOptions {
3541
3542
  /**
3542
3543
  * @default 'always'
@@ -4072,7 +4073,7 @@ declare module "crypto" {
4072
4073
  * The UUID is generated using a cryptographic pseudorandom number generator.
4073
4074
  * @since v16.7.0
4074
4075
  */
4075
- randomUUID(): string;
4076
+ randomUUID(): UUID;
4076
4077
  CryptoKey: CryptoKeyConstructor;
4077
4078
  }
4078
4079
  // This constructor throws ILLEGAL_CONSTRUCTOR so it should not be newable.
@@ -35,6 +35,8 @@
35
35
  * @see [source](https://github.com/nodejs/node/blob/v18.0.0/lib/events.js)
36
36
  */
37
37
  declare module "events" {
38
+ import { AsyncResource, AsyncResourceOptions } from "node:async_hooks";
39
+
38
40
  // NOTE: This class is in the docs but is **not actually exported** by Node.
39
41
  // If https://github.com/nodejs/node/issues/39903 gets resolved and Node
40
42
  // actually starts exporting the class, uncomment below.
@@ -109,6 +111,9 @@ declare module "events" {
109
111
  */
110
112
  class EventEmitter {
111
113
  constructor(options?: EventEmitterOptions);
114
+
115
+ [EventEmitter.captureRejectionSymbol]?(error: Error, event: string, ...args: any[]): void;
116
+
112
117
  /**
113
118
  * Creates a `Promise` that is fulfilled when the `EventEmitter` emits the given
114
119
  * event or that is rejected if the `EventEmitter` emits `'error'` while waiting.
@@ -415,10 +420,54 @@ declare module "events" {
415
420
  */
416
421
  signal?: AbortSignal | undefined;
417
422
  }
423
+
424
+ export interface EventEmitterReferencingAsyncResource extends AsyncResource {
425
+ readonly eventEmitter: EventEmitterAsyncResource;
426
+ }
427
+
428
+ export interface EventEmitterAsyncResourceOptions extends AsyncResourceOptions, EventEmitterOptions {
429
+ /**
430
+ * The type of async event, this is required when instantiating `EventEmitterAsyncResource`
431
+ * directly rather than as a child class.
432
+ * @default new.target.name if instantiated as a child class.
433
+ */
434
+ name?: string;
435
+ }
436
+
437
+ /**
438
+ * Integrates `EventEmitter` with `AsyncResource` for `EventEmitter`s that require
439
+ * manual async tracking. Specifically, all events emitted by instances of
440
+ * `EventEmitterAsyncResource` will run within its async context.
441
+ *
442
+ * The EventEmitterAsyncResource class has the same methods and takes the
443
+ * same options as EventEmitter and AsyncResource themselves.
444
+ * @throws if `options.name` is not provided when instantiated directly.
445
+ * @since v17.4.0, v16.14.0
446
+ */
447
+ export class EventEmitterAsyncResource extends EventEmitter {
448
+ /**
449
+ * @param options Only optional in child class.
450
+ */
451
+ constructor(options?: EventEmitterAsyncResourceOptions);
452
+ /**
453
+ * Call all destroy hooks. This should only ever be called once. An
454
+ * error will be thrown if it is called more than once. This must be
455
+ * manually called. If the resource is left to be collected by the GC then
456
+ * the destroy hooks will never be called.
457
+ */
458
+ emitDestroy(): void;
459
+ /** The unique asyncId assigned to the resource. */
460
+ readonly asyncId: number;
461
+ /** The same triggerAsyncId that is passed to the AsyncResource constructor. */
462
+ readonly triggerAsyncId: number;
463
+ /** The underlying AsyncResource */
464
+ readonly asyncResource: EventEmitterReferencingAsyncResource;
465
+ }
418
466
  }
419
467
  global {
420
468
  namespace NodeJS {
421
469
  interface EventEmitter {
470
+ [EventEmitter.captureRejectionSymbol]?(error: Error, event: string, ...args: any[]): void;
422
471
  /**
423
472
  * Alias for `emitter.on(eventName, listener)`.
424
473
  * @since v0.1.26
@@ -22,7 +22,7 @@
22
22
  * IN THE SOFTWARE.
23
23
  */
24
24
 
25
- // NOTE: These definitions support NodeJS and TypeScript 4.8 and earlier.
25
+ // NOTE: These definitions support NodeJS and TypeScript 4.9+.
26
26
 
27
27
  // Reference required types from the default lib:
28
28
  /// <reference lib="es2020" />
@@ -30,7 +30,7 @@
30
30
  */
31
31
  declare module "perf_hooks" {
32
32
  import { AsyncResource } from "node:async_hooks";
33
- type EntryType = "node" | "mark" | "measure" | "gc" | "function" | "http2" | "http";
33
+ type EntryType = "node" | "mark" | "measure" | "gc" | "function" | "http2" | "http" | "dns";
34
34
  interface NodeGCPerformanceDetail {
35
35
  /**
36
36
  * When `performanceEntry.entryType` is equal to 'gc', `the performance.kind` property identifies
@@ -1544,7 +1544,7 @@ declare module "util" {
1544
1544
  /**
1545
1545
  * Returns an iterator over each of the name-value pairs in the parameters.
1546
1546
  */
1547
- entries(): IterableIterator<[string, string]>;
1547
+ entries(): IterableIterator<[name: string, value: string]>;
1548
1548
  /**
1549
1549
  * Returns the value of the first name-value pair whose name is `name`.
1550
1550
  * If there are no such pairs, `null` is returned.
@@ -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/v18.0.0/lib/v8.js)
7
+ * @see [source](https://github.com/nodejs/node/blob/v18.19.0/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
  * ```
@@ -172,7 +187,7 @@ declare module "v8" {
172
187
  *
173
188
  * ```js
174
189
  * // Print heap snapshot to the console
175
- * const v8 = require('v8');
190
+ * const v8 = require('node:v8');
176
191
  * const stream = v8.getHeapSnapshot();
177
192
  * stream.pipe(process.stdout);
178
193
  * ```
@@ -197,12 +212,12 @@ declare module "v8" {
197
212
  * for a duration depending on the heap size.
198
213
  *
199
214
  * ```js
200
- * const { writeHeapSnapshot } = require('v8');
215
+ * const { writeHeapSnapshot } = require('node:v8');
201
216
  * const {
202
217
  * Worker,
203
218
  * isMainThread,
204
- * parentPort
205
- * } = require('worker_threads');
219
+ * parentPort,
220
+ * } = require('node:worker_threads');
206
221
  *
207
222
  * if (isMainThread) {
208
223
  * const worker = new Worker(__filename);
@@ -233,13 +248,16 @@ declare module "v8" {
233
248
  */
234
249
  function writeHeapSnapshot(filename?: string): string;
235
250
  /**
236
- * 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:
237
254
  *
238
255
  * ```js
239
256
  * {
240
257
  * code_and_metadata_size: 212208,
241
258
  * bytecode_and_metadata_size: 161368,
242
- * external_script_source_size: 1410794
259
+ * external_script_source_size: 1410794,
260
+ * cpu_profiler_metadata_size: 0,
243
261
  * }
244
262
  * ```
245
263
  * @since v12.8.0
@@ -289,7 +307,7 @@ declare module "v8" {
289
307
  */
290
308
  writeDouble(value: number): void;
291
309
  /**
292
- * Write raw bytes into the serializers internal buffer. The deserializer
310
+ * Write raw bytes into the serializer's internal buffer. The deserializer
293
311
  * will require a way to compute the length of the buffer.
294
312
  * For use inside of a custom `serializer._writeHostObject()`.
295
313
  */
@@ -345,7 +363,7 @@ declare module "v8" {
345
363
  */
346
364
  readDouble(): number;
347
365
  /**
348
- * Read raw bytes from the deserializers internal buffer. The `length` parameter
366
+ * Read raw bytes from the deserializer's internal buffer. The `length` parameter
349
367
  * must correspond to the length of the buffer that was passed to `serializer.writeRawBytes()`.
350
368
  * For use inside of a custom `deserializer._readHostObject()`.
351
369
  */
@@ -390,17 +408,100 @@ declare module "v8" {
390
408
  * @since v15.1.0, v14.18.0, v12.22.0
391
409
  */
392
410
  function stopCoverage(): void;
393
-
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-v18.x/api/cli.html#--heapsnapshot-near-heap-limitmax_count) for more information.
414
+ * @experimental
415
+ * @since v18.10.0, v16.18.0
416
+ */
417
+ function setHeapSnapshotNearHeapLimit(limit: number): void;
394
418
  /**
395
419
  * This API collects GC data in current thread.
420
+ * @since v18.15.0
396
421
  */
397
422
  class GCProfiler {
398
423
  /**
399
424
  * Start collecting GC data.
425
+ * @since v18.15.0
400
426
  */
401
427
  start(): void;
402
428
  /**
403
- * Stop collecting GC data and return a object.
429
+ * Stop collecting GC data and return an object.The content of object
430
+ * is as follows.
431
+ *
432
+ * ```json
433
+ * {
434
+ * "version": 1,
435
+ * "startTime": 1674059033862,
436
+ * "statistics": [
437
+ * {
438
+ * "gcType": "Scavenge",
439
+ * "beforeGC": {
440
+ * "heapStatistics": {
441
+ * "totalHeapSize": 5005312,
442
+ * "totalHeapSizeExecutable": 524288,
443
+ * "totalPhysicalSize": 5226496,
444
+ * "totalAvailableSize": 4341325216,
445
+ * "totalGlobalHandlesSize": 8192,
446
+ * "usedGlobalHandlesSize": 2112,
447
+ * "usedHeapSize": 4883840,
448
+ * "heapSizeLimit": 4345298944,
449
+ * "mallocedMemory": 254128,
450
+ * "externalMemory": 225138,
451
+ * "peakMallocedMemory": 181760
452
+ * },
453
+ * "heapSpaceStatistics": [
454
+ * {
455
+ * "spaceName": "read_only_space",
456
+ * "spaceSize": 0,
457
+ * "spaceUsedSize": 0,
458
+ * "spaceAvailableSize": 0,
459
+ * "physicalSpaceSize": 0
460
+ * }
461
+ * ]
462
+ * },
463
+ * "cost": 1574.14,
464
+ * "afterGC": {
465
+ * "heapStatistics": {
466
+ * "totalHeapSize": 6053888,
467
+ * "totalHeapSizeExecutable": 524288,
468
+ * "totalPhysicalSize": 5500928,
469
+ * "totalAvailableSize": 4341101384,
470
+ * "totalGlobalHandlesSize": 8192,
471
+ * "usedGlobalHandlesSize": 2112,
472
+ * "usedHeapSize": 4059096,
473
+ * "heapSizeLimit": 4345298944,
474
+ * "mallocedMemory": 254128,
475
+ * "externalMemory": 225138,
476
+ * "peakMallocedMemory": 181760
477
+ * },
478
+ * "heapSpaceStatistics": [
479
+ * {
480
+ * "spaceName": "read_only_space",
481
+ * "spaceSize": 0,
482
+ * "spaceUsedSize": 0,
483
+ * "spaceAvailableSize": 0,
484
+ * "physicalSpaceSize": 0
485
+ * }
486
+ * ]
487
+ * }
488
+ * }
489
+ * ],
490
+ * "endTime": 1674059036865
491
+ * }
492
+ * ```
493
+ *
494
+ * Here's an example.
495
+ *
496
+ * ```js
497
+ * const { GCProfiler } = require('v8');
498
+ * const profiler = new GCProfiler();
499
+ * profiler.start();
500
+ * setTimeout(() => {
501
+ * console.log(profiler.stop());
502
+ * }, 1000);
503
+ * ```
504
+ * @since v18.15.0
404
505
  */
405
506
  stop(): GCProfilerResult;
406
507
  }
@@ -535,6 +636,117 @@ declare module "v8" {
535
636
  * @since v17.1.0, v16.14.0
536
637
  */
537
638
  const promiseHooks: PromiseHooks;
639
+ type StartupSnapshotCallbackFn = (args: any) => any;
640
+ interface StartupSnapshot {
641
+ /**
642
+ * Add a callback that will be called when the Node.js instance is about to get serialized into a snapshot and exit.
643
+ * 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.
644
+ * @since v18.6.0, v16.17.0
645
+ */
646
+ addSerializeCallback(callback: StartupSnapshotCallbackFn, data?: any): void;
647
+ /**
648
+ * Add a callback that will be called when the Node.js instance is deserialized from a snapshot.
649
+ * 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
650
+ * to re-acquire resources that the application needs when the application is restarted from the snapshot.
651
+ * @since v18.6.0, v16.17.0
652
+ */
653
+ addDeserializeCallback(callback: StartupSnapshotCallbackFn, data?: any): void;
654
+ /**
655
+ * 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.
656
+ * 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
657
+ * data (if provided), otherwise an entry point script still needs to be provided to the deserialized application.
658
+ * @since v18.6.0, v16.17.0
659
+ */
660
+ setDeserializeMainFunction(callback: StartupSnapshotCallbackFn, data?: any): void;
661
+ /**
662
+ * Returns true if the Node.js instance is run to build a snapshot.
663
+ * @since v18.6.0, v16.17.0
664
+ */
665
+ isBuildingSnapshot(): boolean;
666
+ }
667
+ /**
668
+ * The `v8.startupSnapshot` interface can be used to add serialization and deserialization hooks for custom startup snapshots.
669
+ *
670
+ * ```bash
671
+ * $ node --snapshot-blob snapshot.blob --build-snapshot entry.js
672
+ * # This launches a process with the snapshot
673
+ * $ node --snapshot-blob snapshot.blob
674
+ * ```
675
+ *
676
+ * In the example above, `entry.js` can use methods from the `v8.startupSnapshot` interface to specify how to save information for custom objects
677
+ * in the snapshot during serialization
678
+ * and how the information can be used to synchronize these objects during deserialization of the snapshot.
679
+ * For example, if the `entry.js` contains the following script:
680
+ *
681
+ * ```js
682
+ * 'use strict';
683
+ *
684
+ * const fs = require('node:fs');
685
+ * const zlib = require('node:zlib');
686
+ * const path = require('node:path');
687
+ * const assert = require('node:assert');
688
+ *
689
+ * const v8 = require('node:v8');
690
+ *
691
+ * class BookShelf {
692
+ * storage = new Map();
693
+ *
694
+ * // Reading a series of files from directory and store them into storage.
695
+ * constructor(directory, books) {
696
+ * for (const book of books) {
697
+ * this.storage.set(book, fs.readFileSync(path.join(directory, book)));
698
+ * }
699
+ * }
700
+ *
701
+ * static compressAll(shelf) {
702
+ * for (const [ book, content ] of shelf.storage) {
703
+ * shelf.storage.set(book, zlib.gzipSync(content));
704
+ * }
705
+ * }
706
+ *
707
+ * static decompressAll(shelf) {
708
+ * for (const [ book, content ] of shelf.storage) {
709
+ * shelf.storage.set(book, zlib.gunzipSync(content));
710
+ * }
711
+ * }
712
+ * }
713
+ *
714
+ * // __dirname here is where the snapshot script is placed
715
+ * // during snapshot building time.
716
+ * const shelf = new BookShelf(__dirname, [
717
+ * 'book1.en_US.txt',
718
+ * 'book1.es_ES.txt',
719
+ * 'book2.zh_CN.txt',
720
+ * ]);
721
+ *
722
+ * assert(v8.startupSnapshot.isBuildingSnapshot());
723
+ * // On snapshot serialization, compress the books to reduce size.
724
+ * v8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf);
725
+ * // On snapshot deserialization, decompress the books.
726
+ * v8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf);
727
+ * v8.startupSnapshot.setDeserializeMainFunction((shelf) => {
728
+ * // process.env and process.argv are refreshed during snapshot
729
+ * // deserialization.
730
+ * const lang = process.env.BOOK_LANG || 'en_US';
731
+ * const book = process.argv[1];
732
+ * const name = `${book}.${lang}.txt`;
733
+ * console.log(shelf.storage.get(name));
734
+ * }, shelf);
735
+ * ```
736
+ *
737
+ * 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:
738
+ *
739
+ * ```bash
740
+ * $ BOOK_LANG=es_ES node --snapshot-blob snapshot.blob book1
741
+ * # Prints content of book1.es_ES.txt deserialized from the snapshot.
742
+ * ```
743
+ *
744
+ * 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.
745
+ *
746
+ * @experimental
747
+ * @since v18.6.0, v16.17.0
748
+ */
749
+ const startupSnapshot: StartupSnapshot;
538
750
  }
539
751
  declare module "node:v8" {
540
752
  export * from "v8";
node v18.19/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/v18.0.0/lib/v8.js)
7
+ * @see [source](https://github.com/nodejs/node/blob/v18.19.0/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
  * ```
@@ -172,7 +187,7 @@ declare module "v8" {
172
187
  *
173
188
  * ```js
174
189
  * // Print heap snapshot to the console
175
- * const v8 = require('v8');
190
+ * const v8 = require('node:v8');
176
191
  * const stream = v8.getHeapSnapshot();
177
192
  * stream.pipe(process.stdout);
178
193
  * ```
@@ -197,12 +212,12 @@ declare module "v8" {
197
212
  * for a duration depending on the heap size.
198
213
  *
199
214
  * ```js
200
- * const { writeHeapSnapshot } = require('v8');
215
+ * const { writeHeapSnapshot } = require('node:v8');
201
216
  * const {
202
217
  * Worker,
203
218
  * isMainThread,
204
- * parentPort
205
- * } = require('worker_threads');
219
+ * parentPort,
220
+ * } = require('node:worker_threads');
206
221
  *
207
222
  * if (isMainThread) {
208
223
  * const worker = new Worker(__filename);
@@ -233,13 +248,16 @@ declare module "v8" {
233
248
  */
234
249
  function writeHeapSnapshot(filename?: string): string;
235
250
  /**
236
- * 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:
237
254
  *
238
255
  * ```js
239
256
  * {
240
257
  * code_and_metadata_size: 212208,
241
258
  * bytecode_and_metadata_size: 161368,
242
- * external_script_source_size: 1410794
259
+ * external_script_source_size: 1410794,
260
+ * cpu_profiler_metadata_size: 0,
243
261
  * }
244
262
  * ```
245
263
  * @since v12.8.0
@@ -289,7 +307,7 @@ declare module "v8" {
289
307
  */
290
308
  writeDouble(value: number): void;
291
309
  /**
292
- * Write raw bytes into the serializers internal buffer. The deserializer
310
+ * Write raw bytes into the serializer's internal buffer. The deserializer
293
311
  * will require a way to compute the length of the buffer.
294
312
  * For use inside of a custom `serializer._writeHostObject()`.
295
313
  */
@@ -345,7 +363,7 @@ declare module "v8" {
345
363
  */
346
364
  readDouble(): number;
347
365
  /**
348
- * Read raw bytes from the deserializers internal buffer. The `length` parameter
366
+ * Read raw bytes from the deserializer's internal buffer. The `length` parameter
349
367
  * must correspond to the length of the buffer that was passed to `serializer.writeRawBytes()`.
350
368
  * For use inside of a custom `deserializer._readHostObject()`.
351
369
  */
@@ -390,17 +408,100 @@ declare module "v8" {
390
408
  * @since v15.1.0, v14.18.0, v12.22.0
391
409
  */
392
410
  function stopCoverage(): void;
393
-
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-v18.x/api/cli.html#--heapsnapshot-near-heap-limitmax_count) for more information.
414
+ * @experimental
415
+ * @since v18.10.0, v16.18.0
416
+ */
417
+ function setHeapSnapshotNearHeapLimit(limit: number): void;
394
418
  /**
395
419
  * This API collects GC data in current thread.
420
+ * @since v18.15.0
396
421
  */
397
422
  class GCProfiler {
398
423
  /**
399
424
  * Start collecting GC data.
425
+ * @since v18.15.0
400
426
  */
401
427
  start(): void;
402
428
  /**
403
- * Stop collecting GC data and return a object.
429
+ * Stop collecting GC data and return an object.The content of object
430
+ * is as follows.
431
+ *
432
+ * ```json
433
+ * {
434
+ * "version": 1,
435
+ * "startTime": 1674059033862,
436
+ * "statistics": [
437
+ * {
438
+ * "gcType": "Scavenge",
439
+ * "beforeGC": {
440
+ * "heapStatistics": {
441
+ * "totalHeapSize": 5005312,
442
+ * "totalHeapSizeExecutable": 524288,
443
+ * "totalPhysicalSize": 5226496,
444
+ * "totalAvailableSize": 4341325216,
445
+ * "totalGlobalHandlesSize": 8192,
446
+ * "usedGlobalHandlesSize": 2112,
447
+ * "usedHeapSize": 4883840,
448
+ * "heapSizeLimit": 4345298944,
449
+ * "mallocedMemory": 254128,
450
+ * "externalMemory": 225138,
451
+ * "peakMallocedMemory": 181760
452
+ * },
453
+ * "heapSpaceStatistics": [
454
+ * {
455
+ * "spaceName": "read_only_space",
456
+ * "spaceSize": 0,
457
+ * "spaceUsedSize": 0,
458
+ * "spaceAvailableSize": 0,
459
+ * "physicalSpaceSize": 0
460
+ * }
461
+ * ]
462
+ * },
463
+ * "cost": 1574.14,
464
+ * "afterGC": {
465
+ * "heapStatistics": {
466
+ * "totalHeapSize": 6053888,
467
+ * "totalHeapSizeExecutable": 524288,
468
+ * "totalPhysicalSize": 5500928,
469
+ * "totalAvailableSize": 4341101384,
470
+ * "totalGlobalHandlesSize": 8192,
471
+ * "usedGlobalHandlesSize": 2112,
472
+ * "usedHeapSize": 4059096,
473
+ * "heapSizeLimit": 4345298944,
474
+ * "mallocedMemory": 254128,
475
+ * "externalMemory": 225138,
476
+ * "peakMallocedMemory": 181760
477
+ * },
478
+ * "heapSpaceStatistics": [
479
+ * {
480
+ * "spaceName": "read_only_space",
481
+ * "spaceSize": 0,
482
+ * "spaceUsedSize": 0,
483
+ * "spaceAvailableSize": 0,
484
+ * "physicalSpaceSize": 0
485
+ * }
486
+ * ]
487
+ * }
488
+ * }
489
+ * ],
490
+ * "endTime": 1674059036865
491
+ * }
492
+ * ```
493
+ *
494
+ * Here's an example.
495
+ *
496
+ * ```js
497
+ * const { GCProfiler } = require('v8');
498
+ * const profiler = new GCProfiler();
499
+ * profiler.start();
500
+ * setTimeout(() => {
501
+ * console.log(profiler.stop());
502
+ * }, 1000);
503
+ * ```
504
+ * @since v18.15.0
404
505
  */
405
506
  stop(): GCProfilerResult;
406
507
  }
@@ -535,6 +636,117 @@ declare module "v8" {
535
636
  * @since v17.1.0, v16.14.0
536
637
  */
537
638
  const promiseHooks: PromiseHooks;
639
+ type StartupSnapshotCallbackFn = (args: any) => any;
640
+ interface StartupSnapshot {
641
+ /**
642
+ * Add a callback that will be called when the Node.js instance is about to get serialized into a snapshot and exit.
643
+ * 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.
644
+ * @since v18.6.0, v16.17.0
645
+ */
646
+ addSerializeCallback(callback: StartupSnapshotCallbackFn, data?: any): void;
647
+ /**
648
+ * Add a callback that will be called when the Node.js instance is deserialized from a snapshot.
649
+ * 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
650
+ * to re-acquire resources that the application needs when the application is restarted from the snapshot.
651
+ * @since v18.6.0, v16.17.0
652
+ */
653
+ addDeserializeCallback(callback: StartupSnapshotCallbackFn, data?: any): void;
654
+ /**
655
+ * 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.
656
+ * 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
657
+ * data (if provided), otherwise an entry point script still needs to be provided to the deserialized application.
658
+ * @since v18.6.0, v16.17.0
659
+ */
660
+ setDeserializeMainFunction(callback: StartupSnapshotCallbackFn, data?: any): void;
661
+ /**
662
+ * Returns true if the Node.js instance is run to build a snapshot.
663
+ * @since v18.6.0, v16.17.0
664
+ */
665
+ isBuildingSnapshot(): boolean;
666
+ }
667
+ /**
668
+ * The `v8.startupSnapshot` interface can be used to add serialization and deserialization hooks for custom startup snapshots.
669
+ *
670
+ * ```bash
671
+ * $ node --snapshot-blob snapshot.blob --build-snapshot entry.js
672
+ * # This launches a process with the snapshot
673
+ * $ node --snapshot-blob snapshot.blob
674
+ * ```
675
+ *
676
+ * In the example above, `entry.js` can use methods from the `v8.startupSnapshot` interface to specify how to save information for custom objects
677
+ * in the snapshot during serialization
678
+ * and how the information can be used to synchronize these objects during deserialization of the snapshot.
679
+ * For example, if the `entry.js` contains the following script:
680
+ *
681
+ * ```js
682
+ * 'use strict';
683
+ *
684
+ * const fs = require('node:fs');
685
+ * const zlib = require('node:zlib');
686
+ * const path = require('node:path');
687
+ * const assert = require('node:assert');
688
+ *
689
+ * const v8 = require('node:v8');
690
+ *
691
+ * class BookShelf {
692
+ * storage = new Map();
693
+ *
694
+ * // Reading a series of files from directory and store them into storage.
695
+ * constructor(directory, books) {
696
+ * for (const book of books) {
697
+ * this.storage.set(book, fs.readFileSync(path.join(directory, book)));
698
+ * }
699
+ * }
700
+ *
701
+ * static compressAll(shelf) {
702
+ * for (const [ book, content ] of shelf.storage) {
703
+ * shelf.storage.set(book, zlib.gzipSync(content));
704
+ * }
705
+ * }
706
+ *
707
+ * static decompressAll(shelf) {
708
+ * for (const [ book, content ] of shelf.storage) {
709
+ * shelf.storage.set(book, zlib.gunzipSync(content));
710
+ * }
711
+ * }
712
+ * }
713
+ *
714
+ * // __dirname here is where the snapshot script is placed
715
+ * // during snapshot building time.
716
+ * const shelf = new BookShelf(__dirname, [
717
+ * 'book1.en_US.txt',
718
+ * 'book1.es_ES.txt',
719
+ * 'book2.zh_CN.txt',
720
+ * ]);
721
+ *
722
+ * assert(v8.startupSnapshot.isBuildingSnapshot());
723
+ * // On snapshot serialization, compress the books to reduce size.
724
+ * v8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf);
725
+ * // On snapshot deserialization, decompress the books.
726
+ * v8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf);
727
+ * v8.startupSnapshot.setDeserializeMainFunction((shelf) => {
728
+ * // process.env and process.argv are refreshed during snapshot
729
+ * // deserialization.
730
+ * const lang = process.env.BOOK_LANG || 'en_US';
731
+ * const book = process.argv[1];
732
+ * const name = `${book}.${lang}.txt`;
733
+ * console.log(shelf.storage.get(name));
734
+ * }, shelf);
735
+ * ```
736
+ *
737
+ * 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:
738
+ *
739
+ * ```bash
740
+ * $ BOOK_LANG=es_ES node --snapshot-blob snapshot.blob book1
741
+ * # Prints content of book1.es_ES.txt deserialized from the snapshot.
742
+ * ```
743
+ *
744
+ * 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.
745
+ *
746
+ * @experimental
747
+ * @since v18.6.0, v16.17.0
748
+ */
749
+ const startupSnapshot: StartupSnapshot;
538
750
  }
539
751
  declare module "node:v8" {
540
752
  export * from "v8";