From c4aca9fb35202da41e9febc0496cf7194c30bbe1 Mon Sep 17 00:00:00 2001 From: legendecas Date: Sun, 16 Feb 2020 17:20:57 +0800 Subject: [PATCH] util: add private fields preview support `util.previewValue` may reveal private fields of an object. --- doc/api/errors.md | 7 + doc/api/util.md | 43 +++++ lib/internal/errors.js | 1 + lib/internal/util/inspect.js | 201 ++++++++++++++++++++++- lib/util.js | 4 +- test/parallel/test-util-preview-value.js | 94 +++++++++++ 6 files changed, 348 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-util-preview-value.js diff --git a/doc/api/errors.md b/doc/api/errors.md index 8f3af6fcb6a9d6..f03ba215b0b620 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1689,6 +1689,13 @@ object. An attempt was made to `require()` an [ES Module][]. + +### `ERR_PREVIEW_FAILURE` + +> Stability: 1 - Experimental + +An attempt of previewing a JavaScript value was failed. + ### `ERR_SCRIPT_EXECUTION_INTERRUPTED` diff --git a/doc/api/util.md b/doc/api/util.md index b702d5adba3a2c..319afaf8782f26 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -885,6 +885,49 @@ Otherwise, returns `false`. See [`assert.deepStrictEqual()`][] for more information about deep strict equality. +### `util.previewValue(value[, opts])` + + +* `object` {any} Any JavaScript primitive or `Object`. +* `options` {Object} + * `breakLength` {integer} The length at which input values are split across + multiple lines. Set to `Infinity` to format the input as a single line + (in combination with `compact` set to `true` or any number >= `1`). + **Default:** `80`. + * `colors` {boolean} If `true`, the output is styled with ANSI color + codes. Colors are customizable. See [Customizing `util.inspect` colors][]. + **Default:** `false`. + * `compact` {boolean|integer} Setting this to `false` causes each object key + to be displayed on a new line. It will also add new lines to text that is + longer than `breakLength`. If set to a number, the most `n` inner elements + are united on a single line as long as all properties fit into + `breakLength`. Short array elements are also grouped together. No + text will be reduced below 16 characters, no matter the `breakLength` size. + For more information, see the example below. **Default:** `3`. +* Returns: {Promise} A promise of the representation of `object`. + +> Stability: 1 - Experimental + +The `util.previewValue` method returns promise of a string representation +of `object` that is intended for debugging. The output of `util.previewValue` +may change at any time and should not be depended upon programmatically. +Additional `options` may be passed that alter the result. `util.previewValue()` +will use the constructor's name make an identifiable tag for an inspected value. + +The difference of this method with `util.inspect` is `util.previewValue` can +reveal target own private fields. + +```javascript +class Foo { #bar = 'baz' } + +util.previewValue(new Foo()) + .then((preview) => { + console.log(preview); // 'Foo { #bar: "baz" }' + }); +``` + ## `util.promisify(original)`