From cbe5987f9d007a862a95fcb08eedab29eaa9183c Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 31 Oct 2022 15:40:00 +0100 Subject: [PATCH] fixup! lib: add options to the heap snapshot APIs --- doc/api/v8.md | 16 ++++++++++------ doc/api/worker_threads.md | 8 ++++++-- lib/internal/heap_utils.js | 16 +++++----------- lib/internal/worker.js | 2 +- test/common/heap.js | 2 +- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/doc/api/v8.md b/doc/api/v8.md index ae8602a5470ec5..cfdab87336d7d7 100644 --- a/doc/api/v8.md +++ b/doc/api/v8.md @@ -67,16 +67,17 @@ following properties: added: v11.13.0 changes: - version: REPLACEME - pr-url: REPLACEME + pr-url: https://github.com/nodejs/node/pull/44989 description: Support options to configure the heap snapshot. --> * `options` {Object} * `exposeInternals` {boolean} If true, expose internals in the heap snapshot. + **Default:** `false`. * `exposeNumericValues` {boolean} If true, expose numeric values in - artificial fields. + artificial fields. **Default:** `false`. -* Returns: {stream.Readable} A Readable Stream containing the V8 heap snapshot +* Returns: {stream.Readable} A Readable containing the V8 heap snapshot. Generates a snapshot of the current V8 heap and returns a Readable Stream that may be used to read the JSON serialized representation. @@ -310,7 +311,7 @@ changes: pr-url: https://github.com/nodejs/node/pull/42577 description: Make the returned error codes consistent across all platforms. - version: REPLACEME - pr-url: REPLACEME + pr-url: https://github.com/nodejs/node/pull/44989 description: Support options to configure the heap snapshot. --> @@ -320,7 +321,11 @@ changes: 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 worker thread. -* `options` {Object} See [`v8.getHeapSnapshot()`][] for more details. +* `options` {Object} + * `exposeInternals` {boolean} If true, expose internals in the heap snapshot. + **Default:** `false`. + * `exposeNumericValues` {boolean} If true, expose numeric values in + artificial fields. **Default:** `false`. * Returns: {string} The filename where the snapshot was saved. Generates a snapshot of the current V8 heap and writes it to a JSON @@ -1075,7 +1080,6 @@ Returns true if the Node.js instance is run to build a snapshot. [`serializer.transferArrayBuffer()`]: #serializertransferarraybufferid-arraybuffer [`serializer.writeRawBytes()`]: #serializerwriterawbytesbuffer [`settled` callback]: #settledpromise -[`v8.getHeapSnapshot()`]: #v8getheapsnapshot [`v8.stopCoverage()`]: #v8stopcoverage [`v8.takeCoverage()`]: #v8takecoverage [`vm.Script`]: vm.md#new-vmscriptcode-options diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 8df051fb7b40a9..717a857f99727c 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -1075,11 +1075,15 @@ added: - v12.17.0 changes: - version: REPLACEME - pr-url: REPLACEME + pr-url: https://github.com/nodejs/node/pull/44989 description: Support options to configure the heap snapshot. --> -* `options` {Object} See [`v8.getHeapSnapshot()`][] for more details. +* `options` {Object} + * `exposeInternals` {boolean} If true, expose internals in the heap snapshot. + **Default:** `false`. + * `exposeNumericValues` {boolean} If true, expose numeric values in + artificial fields. **Default:** `false`. * Returns: {Promise} A promise for a Readable Stream containing a V8 heap snapshot diff --git a/lib/internal/heap_utils.js b/lib/internal/heap_utils.js index 4c666088b3d7a6..2b59ce5d787f87 100644 --- a/lib/internal/heap_utils.js +++ b/lib/internal/heap_utils.js @@ -10,25 +10,19 @@ const { const { owner_symbol } = require('internal/async_hooks').symbols; const { Readable } = require('stream'); const { validateObject, validateBoolean } = require('internal/validators'); +const { kEmptyObject } = require('internal/util'); const kHandle = Symbol('kHandle'); -function getHeapSnapshotOptions(options = { - exposeInternals: false, - exposeNumericValues: false -}) { +function getHeapSnapshotOptions(options = kEmptyObject) { validateObject(options, 'options'); - if (options.exposeInternals === undefined) { - options.exposeInternals = false; - } - if (options.exposeNumericValues === undefined) { - options.exposeNumericValues = false; - } + options.exposeInternals ??= false; + options.exposeNumbericValues ??= false; validateBoolean(options.exposeInternals, 'options.exposeInternals'); validateBoolean(options.exposeNumericValues, 'options.exposeNumericValues'); return new Uint8Array([ +options.exposeInternals, - +options.exposeNumericValues + +options.exposeNumericValues, ]); } diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 05846a9f230841..3cc589c996703c 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -422,7 +422,7 @@ class Worker extends EventEmitter { getHeapSnapshotOptions } = require('internal/heap_utils'); const optionsArray = getHeapSnapshotOptions(options); - const heapSnapshotTaker = this[kHandle] && this[kHandle].takeHeapSnapshot(optionsArray); + const heapSnapshotTaker = this[kHandle]?.takeHeapSnapshot(optionsArray); return new Promise((resolve, reject) => { if (!heapSnapshotTaker) return reject(new ERR_WORKER_NOT_RUNNING()); heapSnapshotTaker.ondone = (handle) => { diff --git a/test/common/heap.js b/test/common/heap.js index 3856ad6f683cfe..1c22c274af125a 100644 --- a/test/common/heap.js +++ b/test/common/heap.js @@ -239,7 +239,7 @@ function getHeapSnapshotOptionTests() { snapshot.validateSnapshot('Klass', expected, { loose: true }); }, cases, - } + }; } module.exports = {