Skip to content

Commit

Permalink
fixup! lib: add options to the heap snapshot APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Oct 31, 2022
1 parent df587a0 commit cbe5987
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
16 changes: 10 additions & 6 deletions doc/api/v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
-->

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 5 additions & 11 deletions lib/internal/heap_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion test/common/heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ function getHeapSnapshotOptionTests() {
snapshot.validateSnapshot('Klass', expected, { loose: true });
},
cases,
}
};
}

module.exports = {
Expand Down

0 comments on commit cbe5987

Please sign in to comment.