UNPKG

15.2 kBTypeScriptView Raw
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:
3 *
4 * ```js
5 * const v8 = require('v8');
6 * ```
7 * @see [source](https://github.com/nodejs/node/blob/v16.4.2/lib/v8.js)
8 */
9declare module 'v8' {
10 import { Readable } from 'node:stream';
11 interface HeapSpaceInfo {
12 space_name: string;
13 space_size: number;
14 space_used_size: number;
15 space_available_size: number;
16 physical_space_size: number;
17 }
18 // ** Signifies if the --zap_code_space option is enabled or not. 1 == enabled, 0 == disabled. */
19 type DoesZapCodeSpaceFlag = 0 | 1;
20 interface HeapInfo {
21 total_heap_size: number;
22 total_heap_size_executable: number;
23 total_physical_size: number;
24 total_available_size: number;
25 used_heap_size: number;
26 heap_size_limit: number;
27 malloced_memory: number;
28 peak_malloced_memory: number;
29 does_zap_garbage: DoesZapCodeSpaceFlag;
30 number_of_native_contexts: number;
31 number_of_detached_contexts: number;
32 }
33 interface HeapCodeStatistics {
34 code_and_metadata_size: number;
35 bytecode_and_metadata_size: number;
36 external_script_source_size: number;
37 }
38 /**
39 * Returns an integer representing a version tag derived from the V8 version,
40 * command-line flags, and detected CPU features. This is useful for determining
41 * whether a `vm.Script` `cachedData` buffer is compatible with this instance
42 * of V8.
43 *
44 * ```js
45 * console.log(v8.cachedDataVersionTag()); // 3947234607
46 * // The value returned by v8.cachedDataVersionTag() is derived from the V8
47 * // version, command-line flags, and detected CPU features. Test that the value
48 * // does indeed update when flags are toggled.
49 * v8.setFlagsFromString('--allow_natives_syntax');
50 * console.log(v8.cachedDataVersionTag()); // 183726201
51 * ```
52 * @since v8.0.0
53 */
54 function cachedDataVersionTag(): number;
55 /**
56 * Returns an object with the following properties:
57 *
58 * `does_zap_garbage` is a 0/1 boolean, which signifies whether the`--zap_code_space` option is enabled or not. This makes V8 overwrite heap
59 * garbage with a bit pattern. The RSS footprint (resident set size) gets bigger
60 * because it continuously touches all heap pages and that makes them less likely
61 * to get swapped out by the operating system.
62 *
63 * `number_of_native_contexts` The value of native\_context is the number of the
64 * top-level contexts currently active. Increase of this number over time indicates
65 * a memory leak.
66 *
67 * `number_of_detached_contexts` The value of detached\_context is the number
68 * of contexts that were detached and not yet garbage collected. This number
69 * being non-zero indicates a potential memory leak.
70 *
71 * ```js
72 * {
73 * total_heap_size: 7326976,
74 * total_heap_size_executable: 4194304,
75 * total_physical_size: 7326976,
76 * total_available_size: 1152656,
77 * used_heap_size: 3476208,
78 * heap_size_limit: 1535115264,
79 * malloced_memory: 16384,
80 * peak_malloced_memory: 1127496,
81 * does_zap_garbage: 0,
82 * number_of_native_contexts: 1,
83 * number_of_detached_contexts: 0
84 * }
85 * ```
86 * @since v1.0.0
87 */
88 function getHeapStatistics(): HeapInfo;
89 /**
90 * Returns statistics about the V8 heap spaces, i.e. the segments which make up
91 * the V8 heap. Neither the ordering of heap spaces, nor the availability of a
92 * 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
94 * next.
95 *
96 * The value returned is an array of objects containing the following properties:
97 *
98 * ```json
99 * [
100 * {
101 * "space_name": "new_space",
102 * "space_size": 2063872,
103 * "space_used_size": 951112,
104 * "space_available_size": 80824,
105 * "physical_space_size": 2063872
106 * },
107 * {
108 * "space_name": "old_space",
109 * "space_size": 3090560,
110 * "space_used_size": 2493792,
111 * "space_available_size": 0,
112 * "physical_space_size": 3090560
113 * },
114 * {
115 * "space_name": "code_space",
116 * "space_size": 1260160,
117 * "space_used_size": 644256,
118 * "space_available_size": 960,
119 * "physical_space_size": 1260160
120 * },
121 * {
122 * "space_name": "map_space",
123 * "space_size": 1094160,
124 * "space_used_size": 201608,
125 * "space_available_size": 0,
126 * "physical_space_size": 1094160
127 * },
128 * {
129 * "space_name": "large_object_space",
130 * "space_size": 0,
131 * "space_used_size": 0,
132 * "space_available_size": 1490980608,
133 * "physical_space_size": 0
134 * }
135 * ]
136 * ```
137 * @since v6.0.0
138 */
139 function getHeapSpaceStatistics(): HeapSpaceInfo[];
140 /**
141 * The `v8.setFlagsFromString()` method can be used to programmatically set
142 * V8 command-line flags. This method should be used with care. Changing settings
143 * after the VM has started may result in unpredictable behavior, including
144 * crashes and data loss; or it may simply do nothing.
145 *
146 * The V8 options available for a version of Node.js may be determined by running`node --v8-options`.
147 *
148 * Usage:
149 *
150 * ```js
151 * // Print GC events to stdout for one minute.
152 * const v8 = require('v8');
153 * v8.setFlagsFromString('--trace_gc');
154 * setTimeout(() => { v8.setFlagsFromString('--notrace_gc'); }, 60e3);
155 * ```
156 * @since v1.0.0
157 */
158 function setFlagsFromString(flags: string): void;
159 /**
160 * Generates a snapshot of the current V8 heap and returns a Readable
161 * Stream that may be used to read the JSON serialized representation.
162 * This JSON stream format is intended to be used with tools such as
163 * Chrome DevTools. The JSON schema is undocumented and specific to the
164 * V8 engine. Therefore, the schema may change from one version of V8 to the next.
165 *
166 * ```js
167 * // Print heap snapshot to the console
168 * const v8 = require('v8');
169 * const stream = v8.getHeapSnapshot();
170 * stream.pipe(process.stdout);
171 * ```
172 * @since v11.13.0
173 * @return A Readable Stream containing the V8 heap snapshot
174 */
175 function getHeapSnapshot(): Readable;
176 /**
177 * Generates a snapshot of the current V8 heap and writes it to a JSON
178 * file. This file is intended to be used with tools such as Chrome
179 * DevTools. The JSON schema is undocumented and specific to the V8
180 * engine, and may change from one version of V8 to the next.
181 *
182 * A heap snapshot is specific to a single V8 isolate. When using `worker threads`, a heap snapshot generated from the main thread will
183 * not contain any information about the workers, and vice versa.
184 *
185 * ```js
186 * const { writeHeapSnapshot } = require('v8');
187 * const {
188 * Worker,
189 * isMainThread,
190 * parentPort
191 * } = require('worker_threads');
192 *
193 * if (isMainThread) {
194 * const worker = new Worker(__filename);
195 *
196 * worker.once('message', (filename) => {
197 * console.log(`worker heapdump: ${filename}`);
198 * // Now get a heapdump for the main thread.
199 * console.log(`main thread heapdump: ${writeHeapSnapshot()}`);
200 * });
201 *
202 * // Tell the worker to create a heapdump.
203 * worker.postMessage('heapdump');
204 * } else {
205 * parentPort.once('message', (message) => {
206 * if (message === 'heapdump') {
207 * // Generate a heapdump for the worker
208 * // and return the filename to the parent.
209 * parentPort.postMessage(writeHeapSnapshot());
210 * }
211 * });
212 * }
213 * ```
214 * @since v11.13.0
215 * @param filename The file path where the V8 heap snapshot is to be saved. If not specified, a file name with the pattern `'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'` will be
216 * generated, where `{pid}` will be the PID of the Node.js process, `{thread_id}` will be `0` when `writeHeapSnapshot()` is called from the main Node.js thread or the id of a
217 * worker thread.
218 * @return The filename where the snapshot was saved.
219 */
220 function writeHeapSnapshot(filename?: string): string;
221 /**
222 * Returns an object with the following properties:
223 *
224 * ```js
225 * {
226 * code_and_metadata_size: 212208,
227 * bytecode_and_metadata_size: 161368,
228 * external_script_source_size: 1410794
229 * }
230 * ```
231 * @since v12.8.0
232 */
233 function getHeapCodeStatistics(): HeapCodeStatistics;
234 /**
235 * @since v8.0.0
236 */
237 class Serializer {
238 /**
239 * Writes out a header, which includes the serialization format version.
240 */
241 writeHeader(): void;
242 /**
243 * Serializes a JavaScript value and adds the serialized representation to the
244 * internal buffer.
245 *
246 * This throws an error if `value` cannot be serialized.
247 */
248 writeValue(val: any): boolean;
249 /**
250 * Returns the stored internal buffer. This serializer should not be used once
251 * the buffer is released. Calling this method results in undefined behavior
252 * if a previous write has failed.
253 */
254 releaseBuffer(): Buffer;
255 /**
256 * Marks an `ArrayBuffer` as having its contents transferred out of band.
257 * Pass the corresponding `ArrayBuffer` in the deserializing context to `deserializer.transferArrayBuffer()`.
258 * @param id A 32-bit unsigned integer.
259 * @param arrayBuffer An `ArrayBuffer` instance.
260 */
261 transferArrayBuffer(id: number, arrayBuffer: ArrayBuffer): void;
262 /**
263 * Write a raw 32-bit unsigned integer.
264 * For use inside of a custom `serializer._writeHostObject()`.
265 */
266 writeUint32(value: number): void;
267 /**
268 * Write a raw 64-bit unsigned integer, split into high and low 32-bit parts.
269 * For use inside of a custom `serializer._writeHostObject()`.
270 */
271 writeUint64(hi: number, lo: number): void;
272 /**
273 * Write a JS `number` value.
274 * For use inside of a custom `serializer._writeHostObject()`.
275 */
276 writeDouble(value: number): void;
277 /**
278 * Write raw bytes into the serializer’s internal buffer. The deserializer
279 * will require a way to compute the length of the buffer.
280 * For use inside of a custom `serializer._writeHostObject()`.
281 */
282 writeRawBytes(buffer: NodeJS.TypedArray): void;
283 }
284 /**
285 * A subclass of `Serializer` that serializes `TypedArray`(in particular `Buffer`) and `DataView` objects as host objects, and only
286 * stores the part of their underlying `ArrayBuffer`s that they are referring to.
287 * @since v8.0.0
288 */
289 class DefaultSerializer extends Serializer {}
290 /**
291 * @since v8.0.0
292 */
293 class Deserializer {
294 constructor(data: NodeJS.TypedArray);
295 /**
296 * Reads and validates a header (including the format version).
297 * May, for example, reject an invalid or unsupported wire format. In that case,
298 * an `Error` is thrown.
299 */
300 readHeader(): boolean;
301 /**
302 * Deserializes a JavaScript value from the buffer and returns it.
303 */
304 readValue(): any;
305 /**
306 * Marks an `ArrayBuffer` as having its contents transferred out of band.
307 * Pass the corresponding `ArrayBuffer` in the serializing context to `serializer.transferArrayBuffer()` (or return the `id` from `serializer._getSharedArrayBufferId()` in the case of
308 * `SharedArrayBuffer`s).
309 * @param id A 32-bit unsigned integer.
310 * @param arrayBuffer An `ArrayBuffer` instance.
311 */
312 transferArrayBuffer(id: number, arrayBuffer: ArrayBuffer): void;
313 /**
314 * Reads the underlying wire format version. Likely mostly to be useful to
315 * legacy code reading old wire format versions. May not be called before`.readHeader()`.
316 */
317 getWireFormatVersion(): number;
318 /**
319 * Read a raw 32-bit unsigned integer and return it.
320 * For use inside of a custom `deserializer._readHostObject()`.
321 */
322 readUint32(): number;
323 /**
324 * Read a raw 64-bit unsigned integer and return it as an array `[hi, lo]`with two 32-bit unsigned integer entries.
325 * For use inside of a custom `deserializer._readHostObject()`.
326 */
327 readUint64(): [number, number];
328 /**
329 * Read a JS `number` value.
330 * For use inside of a custom `deserializer._readHostObject()`.
331 */
332 readDouble(): number;
333 /**
334 * Read raw bytes from the deserializer’s internal buffer. The `length` parameter
335 * must correspond to the length of the buffer that was passed to `serializer.writeRawBytes()`.
336 * For use inside of a custom `deserializer._readHostObject()`.
337 */
338 readRawBytes(length: number): Buffer;
339 }
340 /**
341 * A subclass of `Deserializer` corresponding to the format written by `DefaultSerializer`.
342 * @since v8.0.0
343 */
344 class DefaultDeserializer extends Deserializer {}
345 /**
346 * Uses a `DefaultSerializer` to serialize `value` into a buffer.
347 * @since v8.0.0
348 */
349 function serialize(value: any): Buffer;
350 /**
351 * Uses a `DefaultDeserializer` with default options to read a JS value
352 * from a buffer.
353 * @since v8.0.0
354 * @param buffer A buffer returned by {@link serialize}.
355 */
356 function deserialize(buffer: NodeJS.TypedArray): any;
357 /**
358 * The `v8.takeCoverage()` method allows the user to write the coverage started by `NODE_V8_COVERAGE` to disk on demand. This method can be invoked multiple
359 * times during the lifetime of the process. Each time the execution counter will
360 * be reset and a new coverage report will be written to the directory specified
361 * by `NODE_V8_COVERAGE`.
362 *
363 * When the process is about to exit, one last coverage will still be written to
364 * disk unless {@link stopCoverage} is invoked before the process exits.
365 * @since v15.1.0, v12.22.0
366 */
367 function takeCoverage(): void;
368 /**
369 * The `v8.stopCoverage()` method allows the user to stop the coverage collection
370 * started by `NODE_V8_COVERAGE`, so that V8 can release the execution count
371 * 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
373 */
374 function stopCoverage(): void;
375}
376declare module 'node:v8' {
377 export * from 'v8';
378}