Skip to content

Commit 09adea9

Browse files
robhoganmeta-codesync[bot]
authored andcommitted
Align Flow lib defs for Node.js v8 module with v24 (#55225)
Summary: Pull Request resolved: #55225 This is an AI-assisted change to align the Flow definitions for the `v8` module with the Node.js docs as at v24. **New v18+ Features:** 1. **Heap Snapshot Near Limit** (v18.10.0) - Automatic snapshot generation - Added `setHeapSnapshotNearHeapLimit(limit: number): void` function - Generates heap snapshots automatically when heap usage reaches specified limit - Useful for debugging memory issues and OOM scenarios - https://nodejs.org/api/v8.html#v8setheapsnapshotnearhea plimit 2. **GCProfiler Class** (v19.6.0, v18.15.0) - Garbage collection profiling - Added `GCProfiler` class for detailed GC statistics collection - `start()` method begins profiling session - `stop()` method returns `GCProfilerResult` with detailed statistics - Includes before/after heap statistics for each GC event - https://nodejs.org/api/v8.html#class-v8gcprofiler **New v20+ Features:** 3. **Query Objects** (v20.13.0) - Object instance tracking - Added `queryObjects(ctor, options?)` function with two overloads - Similar to Chrome DevTools console API - Searches for objects matching a constructor on prototype chain - Two formats: `'count'` (returns number) or `'summary'` (returns string array) - Useful for memory leak regression tests - https://nodejs.org/api/v8.html#v8queryobjectsctor-options **New v22+ Features:** 4. **C++ Heap Statistics** (v22.15.0) - cppgc heap monitoring - Added `getCppHeapStatistics(detailLevel?)` function - Returns C++ heap statistics from cppgc - Two detail levels: `'brief'` (top-level stats) or `'detailed'` (includes space/page breakdown) - https://nodejs.org/api/v8.html#v8getcppheapstatisticsdetaillevel 5. **String Representation Check** (v23.10.0, backported to v22.15.0) - Added `isStringOneByteRepresentation(content: string): boolean` function - Checks if string uses Latin-1/ISO-8859-1 encoding (single byte per character) - Useful for optimizing string serialization - https://nodejs.org/api/v8.html#v8isstringonebyterepresentationvalue **Type System Improvements:** 6. **Complete Type Definitions** - Added missing fields and proper types: - `DoesZapCodeSpaceFlag` type alias (0 | 1) for `does_zap_garbage` - `HeapCodeStatistics` - Added `cpu_profiler_metadata_size` field - `HeapInfo` (renamed from `HeapStatistics`) - Added 3 missing fields: - `total_global_handles_size` (v13.3.0) - `used_global_handles_size` (v13.3.0) - `external_memory` (v13.3.0) - `HeapSpaceInfo` (renamed from `HeapSpaceStatistics`) - Removed spread operator - `HeapSnapshotOptions` (new) - Options for heap snapshot generation: - `exposeInternals?: boolean` - Expose internals in snapshot - `exposeNumericValues?: boolean` - Expose numeric values - `HeapStatistics` (camelCase) - For GCProfiler, complete with all 11 fields - `HeapSpaceStatistics` (camelCase) - For GCProfiler, complete with all 5 fields - `GCProfilerResult` - Complete result type with version, timing, and statistics array - https://nodejs.org/api/v8.html#v8getheapstatistics 7. **Enhanced Function Signatures**: - `getHeapSnapshot(options?)` - Added optional `HeapSnapshotOptions` parameter - `writeHeapSnapshot(fileName?, options?)` - Added optional `HeapSnapshotOptions` parameter - `getHeapStatistics()` - Changed return type from `HeapStatistics` to `HeapInfo` - `getHeapSpaceStatistics()` - Changed return type to `Array<HeapSpaceInfo>` - https://nodejs.org/api/v8.html#v8getheapsnapshotoptions 8. **Coverage Functions** - For type profile analysis: - `takeCoverage(): void` (v15.1.0) - Starts capturing V8 type profile - `stopCoverage(): void` (v15.1.0) - Stops capturing V8 type profile - https://nodejs.org/api/v8.html#v8takecoverage 9. **Serialization Documentation**: - Added `since v8.0.0` tags to all serialization classes and functions - Improved class documentation for `Serializer`, `Deserializer`, `DefaultSerializer`, `DefaultDeserializer` - https://nodejs.org/api/v8.html#serialization-api **References:** - Node.js v8 module docs: https://nodejs.org/api/v8.html - Heap statistics: https://nodejs.org/api/v8.html#v8getheapstatistics - GC profiling: https://nodejs.org/api/v8.html#class-v8gcprofiler - Query objects: https://nodejs.org/api/v8.html#v8queryobjectsctor-options Changelog: [Internal] Reviewed By: vzaidman Differential Revision: D90762740 fbshipit-source-id: 41581da6af6e62198c94e336740241b7983d4486
1 parent 6f698ab commit 09adea9

1 file changed

Lines changed: 186 additions & 51 deletions

File tree

flow-typed/environment/node.js

Lines changed: 186 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4975,73 +4975,186 @@ declare module 'assert/strict' {
49754975
declare module.exports: $Exports<'assert'>['strict'];
49764976
}
49774977

4978-
type HeapCodeStatistics = {
4979-
code_and_metadata_size: number,
4980-
bytecode_and_metadata_size: number,
4981-
external_script_source_size: number,
4982-
...
4983-
};
4978+
declare module 'v8' {
4979+
declare export type DoesZapCodeSpaceFlag = 0 | 1;
49844980

4985-
type HeapStatistics = {
4986-
total_heap_size: number,
4987-
total_heap_size_executable: number,
4988-
total_physical_size: number,
4989-
total_available_size: number,
4990-
used_heap_size: number,
4991-
heap_size_limit: number,
4992-
malloced_memory: number,
4993-
peak_malloced_memory: number,
4994-
does_zap_garbage: 0 | 1,
4995-
number_of_native_contexts: number,
4996-
number_of_detached_contexts: number,
4997-
...
4998-
};
4981+
declare export type HeapCodeStatistics = {
4982+
code_and_metadata_size: number,
4983+
bytecode_and_metadata_size: number,
4984+
external_script_source_size: number,
4985+
cpu_profiler_metadata_size: number,
4986+
};
49994987

5000-
type HeapSpaceStatistics = {
5001-
space_name: string,
5002-
space_size: number,
5003-
space_used_size: number,
5004-
space_available_size: number,
5005-
physical_space_size: number,
5006-
...
5007-
};
4988+
declare export type HeapInfo = {
4989+
total_heap_size: number,
4990+
total_heap_size_executable: number,
4991+
total_physical_size: number,
4992+
total_available_size: number,
4993+
used_heap_size: number,
4994+
heap_size_limit: number,
4995+
malloced_memory: number,
4996+
peak_malloced_memory: number,
4997+
does_zap_garbage: DoesZapCodeSpaceFlag,
4998+
number_of_native_contexts: number,
4999+
number_of_detached_contexts: number,
5000+
total_global_handles_size: number,
5001+
used_global_handles_size: number,
5002+
external_memory: number,
5003+
};
5004+
5005+
declare export type HeapSpaceInfo = {
5006+
space_name: string,
5007+
space_size: number,
5008+
space_used_size: number,
5009+
space_available_size: number,
5010+
physical_space_size: number,
5011+
};
5012+
5013+
declare export type HeapSnapshotOptions = Readonly<{
5014+
exposeInternals?: boolean,
5015+
exposeNumericValues?: boolean,
5016+
}>;
5017+
5018+
// For GCProfiler - uses camelCase naming convention
5019+
declare export type HeapStatistics = {
5020+
totalHeapSize: number,
5021+
totalHeapSizeExecutable: number,
5022+
totalPhysicalSize: number,
5023+
totalAvailableSize: number,
5024+
totalGlobalHandlesSize: number,
5025+
usedGlobalHandlesSize: number,
5026+
usedHeapSize: number,
5027+
heapSizeLimit: number,
5028+
mallocedMemory: number,
5029+
externalMemory: number,
5030+
peakMallocedMemory: number,
5031+
};
5032+
5033+
declare export type HeapSpaceStatistics = {
5034+
spaceName: string,
5035+
spaceSize: number,
5036+
spaceUsedSize: number,
5037+
spaceAvailableSize: number,
5038+
physicalSpaceSize: number,
5039+
};
5040+
5041+
declare export type GCProfilerResult = {
5042+
version: number,
5043+
startTime: number,
5044+
endTime: number,
5045+
statistics: $ReadOnlyArray<{
5046+
gcType: string,
5047+
cost: number,
5048+
beforeGC: {
5049+
heapStatistics: HeapStatistics,
5050+
heapSpaceStatistics: $ReadOnlyArray<HeapSpaceStatistics>,
5051+
},
5052+
afterGC: {
5053+
heapStatistics: HeapStatistics,
5054+
heapSpaceStatistics: $ReadOnlyArray<HeapSpaceStatistics>,
5055+
},
5056+
}>,
5057+
};
50085058

5009-
// Adapted from DefinitelyTyped for Node v14:
5010-
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/dea4d99dc302a0b0a25270e46e72c1fe9b741a17/types/node/v14/v8.d.ts
5011-
declare module 'v8' {
50125059
/**
5013-
* Returns an integer representing a "version tag" derived from the V8 version, command line flags and detected CPU features.
5014-
* This is useful for determining whether a vm.Script cachedData buffer is compatible with this instance of V8.
5060+
* Returns an integer representing a "version tag" derived from the V8 version,
5061+
* command line flags and detected CPU features. This is useful for determining
5062+
* whether a vm.Script cachedData buffer is compatible with this instance of V8.
50155063
*/
50165064
declare function cachedDataVersionTag(): number;
50175065

50185066
/**
5019-
* Generates a snapshot of the current V8 heap and returns a Readable
5020-
* Stream that may be used to read the JSON serialized representation.
5021-
* This conversation was marked as resolved by joyeecheung
5022-
* This JSON stream format is intended to be used with tools such as
5023-
* Chrome DevTools. The JSON schema is undocumented and specific to the
5024-
* V8 engine, and may change from one version of V8 to the next.
5067+
* Returns statistics about code and its metadata in the heap.
50255068
*/
5026-
declare function getHeapSnapshot(): stream$Readable;
5069+
declare function getHeapCodeStatistics(): HeapCodeStatistics;
50275070

50285071
/**
5029-
*
5030-
* @param fileName The file path where the V8 heap snapshot is to be
5031-
* saved. If not specified, a file name with the pattern
5032-
* `'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'` will be
5033-
* generated, where `{pid}` will be the PID of the Node.js process,
5034-
* `{thread_id}` will be `0` when `writeHeapSnapshot()` is called from
5035-
* the main Node.js thread or the id of a worker thread.
5072+
* Returns an object with statistics about the V8 heap.
50365073
*/
5037-
declare function writeHeapSnapshot(fileName?: string): string;
5074+
declare function getHeapStatistics(): HeapInfo;
50385075

5039-
declare function getHeapCodeStatistics(): HeapCodeStatistics;
5076+
/**
5077+
* Returns statistics about the V8 heap spaces.
5078+
*/
5079+
declare function getHeapSpaceStatistics(): Array<HeapSpaceInfo>;
50405080

5041-
declare function getHeapStatistics(): HeapStatistics;
5042-
declare function getHeapSpaceStatistics(): Array<HeapSpaceStatistics>;
5081+
/**
5082+
* Generates a snapshot of the current V8 heap and returns a Readable Stream
5083+
* that may be used to read the JSON serialized representation. This JSON stream
5084+
* format is intended to be used with tools such as Chrome DevTools. The JSON
5085+
* schema is undocumented and specific to the V8 engine.
5086+
* @param options Optional settings for controlling snapshot detail
5087+
*/
5088+
declare function getHeapSnapshot(
5089+
options?: HeapSnapshotOptions,
5090+
): stream$Readable;
5091+
5092+
/**
5093+
* Generates a snapshot of the current V8 heap and writes it to a JSON file.
5094+
* @param fileName The file path where the snapshot will be saved. If not specified,
5095+
* a file name with the pattern 'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'
5096+
* will be generated.
5097+
* @param options Optional settings for controlling snapshot detail
5098+
* @returns The filename where the snapshot was saved
5099+
*/
5100+
declare function writeHeapSnapshot(
5101+
fileName?: string,
5102+
options?: HeapSnapshotOptions,
5103+
): string;
5104+
5105+
/**
5106+
* Sets V8 command-line flags. Use with care; changing settings after the VM has
5107+
* started may result in unpredictable behavior, crashes, or data loss.
5108+
*/
50435109
declare function setFlagsFromString(flags: string): void;
50445110

5111+
/**
5112+
* Generates a heap snapshot when the heap usage reaches the specified limit.
5113+
* @param limit The heap size limit that triggers snapshot generation
5114+
*/
5115+
declare function setHeapSnapshotNearHeapLimit(limit: number): void;
5116+
5117+
/**
5118+
* Searches for objects that match the given constructor on their prototype chain.
5119+
* Similar to Chrome DevTools' queryObjects() console API.
5120+
* @since v20.13.0
5121+
* @experimental
5122+
*/
5123+
declare function queryObjects(
5124+
ctor: Function,
5125+
options?: {format: 'count'},
5126+
): number;
5127+
declare function queryObjects(
5128+
ctor: Function,
5129+
options: {format: 'summary'},
5130+
): Array<string>;
5131+
5132+
/**
5133+
* Returns C++ heap statistics from the cppgc heap.
5134+
* @since v22.15.0
5135+
* @param detailLevel Level of detail: 'brief' for top-level stats, 'detailed' for space/page breakdown
5136+
*/
5137+
declare function getCppHeapStatistics(
5138+
detailLevel?: 'brief' | 'detailed',
5139+
): Object;
5140+
5141+
/**
5142+
* Checks if a string's internal representation uses one byte per character (Latin-1/ISO-8859-1 encoding).
5143+
* Useful for optimizing string serialization.
5144+
* @since v23.10.0, v22.15.0
5145+
*/
5146+
declare function isStringOneByteRepresentation(content: string): boolean;
5147+
5148+
/**
5149+
* Starts capturing V8 type profile for coverage analysis.
5150+
*/
5151+
declare function takeCoverage(): void;
5152+
5153+
/**
5154+
* Stops capturing V8 type profile for coverage analysis.
5155+
*/
5156+
declare function stopCoverage(): void;
5157+
50455158
declare class Serializer {
50465159
constructor(): void;
50475160

@@ -5163,6 +5276,23 @@ declare module 'v8' {
51635276
* Uses a `DefaultDeserializer` with default options to read a JS value from a buffer.
51645277
*/
51655278
declare function deserialize(data: Buffer | $TypedArray | DataView): any;
5279+
5280+
/**
5281+
* Starts a GC profiling session that collects detailed garbage collection statistics.
5282+
*/
5283+
declare class GCProfiler {
5284+
constructor(): void;
5285+
5286+
/**
5287+
* Starts the GC profiling session.
5288+
*/
5289+
start(): void;
5290+
5291+
/**
5292+
* Stops the GC profiling session and returns collected statistics.
5293+
*/
5294+
stop(): GCProfilerResult;
5295+
}
51665296
}
51675297

51685298
type repl$DefineCommandOptions = (...args: Array<any>) => void | {
@@ -5600,6 +5730,11 @@ declare module 'node:util' {
56005730
declare module.exports: $Exports<'util'>;
56015731
}
56025732

5733+
declare module 'node:v8' {
5734+
export type * from 'v8';
5735+
declare module.exports: $Exports<'v8'>;
5736+
}
5737+
56035738
declare module 'node:worker_threads' {
56045739
export type * from 'worker_threads';
56055740
declare module.exports: $Exports<'worker_threads'>;

0 commit comments

Comments
 (0)