From 2867425874c3903ddc6240e71007cc831d1b2f46 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 11 Dec 2018 22:42:34 +0100 Subject: [PATCH 1/2] lib: don't use `util.inspect()` internals This makes sure the internal `stylize` function is not used to render anything and instead just uses the regular inspect function in case of reaching the maximum depth level. --- lib/internal/encoding.js | 4 ++-- lib/internal/url.js | 2 +- test/parallel/test-whatwg-encoding-custom-textdecoder.js | 2 +- test/parallel/test-whatwg-url-custom-inspect.js | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/internal/encoding.js b/lib/internal/encoding.js index 0084c19b904a..3fbbbe1a2fef 100644 --- a/lib/internal/encoding.js +++ b/lib/internal/encoding.js @@ -320,7 +320,7 @@ class TextEncoder { [inspect](depth, opts) { validateEncoder(this); if (typeof depth === 'number' && depth < 0) - return opts.stylize('[Object]', 'special'); + return this; var ctor = getConstructorOf(this); var obj = Object.create({ constructor: ctor === null ? TextEncoder : ctor @@ -517,7 +517,7 @@ function makeTextDecoderJS() { [inspect](depth, opts) { validateDecoder(this); if (typeof depth === 'number' && depth < 0) - return opts.stylize('[Object]', 'special'); + return this; var ctor = getConstructorOf(this); var obj = Object.create({ constructor: ctor === null ? TextDecoder : ctor diff --git a/lib/internal/url.js b/lib/internal/url.js index 6ad7122b8643..816715ab5e6c 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -343,7 +343,7 @@ class URL { } if (typeof depth === 'number' && depth < 0) - return opts.stylize('[Object]', 'special'); + return this; var ctor = getConstructorOf(this); diff --git a/test/parallel/test-whatwg-encoding-custom-textdecoder.js b/test/parallel/test-whatwg-encoding-custom-textdecoder.js index ceee5b9cbbde..63abd2506a57 100644 --- a/test/parallel/test-whatwg-encoding-custom-textdecoder.js +++ b/test/parallel/test-whatwg-encoding-custom-textdecoder.js @@ -134,7 +134,7 @@ if (common.hasIntl) { // Test TextDecoder inspect with negative depth { const dec = new TextDecoder(); - assert.strictEqual(util.inspect(dec, { depth: -1 }), '[Object]'); + assert.strictEqual(util.inspect(dec, { depth: -1 }), '[TextDecoder]'); } { diff --git a/test/parallel/test-whatwg-url-custom-inspect.js b/test/parallel/test-whatwg-url-custom-inspect.js index 1083866d86f5..21c13582254e 100644 --- a/test/parallel/test-whatwg-url-custom-inspect.js +++ b/test/parallel/test-whatwg-url-custom-inspect.js @@ -63,7 +63,7 @@ assert.strictEqual( assert.strictEqual( util.inspect({ a: url }, { depth: 0 }), - '{ a: [Object] }'); + '{ a: [URL] }'); class MyURL extends URL {} assert(util.inspect(new MyURL(url.href)).startsWith('MyURL {')); From dd1a712079df8eca134c334d02ec668212f64c60 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 11 Dec 2018 21:28:45 +0100 Subject: [PATCH 2/2] util: prevent leaking internal properties This prevents leaking of the internal `inspect()` properties when using a custom inspect function. It also aligns the indentation to the way it was in v8.0.0 since that changed unintentionally. All strings returned by the custom inspect function will now be indented appropriately to the current depth. --- doc/api/util.md | 4 ++++ lib/internal/util/inspect.js | 8 ++++++-- test/parallel/test-util-inspect.js | 20 ++++++++++++++++---- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index 9db263632e0a..b1d4854997f0 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -388,6 +388,10 @@ stream.write('With ES6');