Skip to content

Commit 90a4390

Browse files
committed
repl: show proxies as Proxy objects
Before this commit they transparently invoked their magic methods but that sometimes throws confusing exceptions with misbehaving proxies. This change is not wholly uncontroversial but we can always change the default if necessary. Let's see how it goes. Fixes: #16483 PR-URL: #16485 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 6563e56 commit 90a4390

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

lib/repl.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ function hasOwnProperty(obj, prop) {
100100

101101
// Can overridden with custom print functions, such as `probe` or `eyes.js`.
102102
// This is the default "writer" value if none is passed in the REPL options.
103-
exports.writer = util.inspect;
103+
const writer = exports.writer = (obj) => util.inspect(obj, writer.options);
104+
writer.options =
105+
Object.assign(util.inspect.defaultOptions, { showProxy: true });
104106

105107
exports._builtinLibs = internalModule.builtinLibs;
106108

@@ -373,11 +375,10 @@ function REPLServer(prompt,
373375
}
374376
self.useColors = !!options.useColors;
375377

376-
if (self.useColors && self.writer === util.inspect) {
378+
if (self.useColors && self.writer === writer) {
377379
// Turn on ANSI coloring.
378-
self.writer = function(obj, showHidden, depth) {
379-
return util.inspect(obj, showHidden, depth, true);
380-
};
380+
self.writer = (obj) => util.inspect(obj, self.writer.options);
381+
self.writer.options = Object.assign(writer.options, { colors: true });
381382
}
382383

383384
function filterInternalStackFrames(error, structuredStack) {

test/parallel/test-repl.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,17 @@ function error_test() {
413413
expect: `${prompt_multiline}${prompt_multiline}undefined\n${prompt_unix}`
414414
},
415415

416+
// https://github.com/nodejs/node/issues/16483
417+
{
418+
client: client_unix, send: 'new Proxy({x:42}, {get(){throw null}});',
419+
expect: `Proxy [ { x: 42 }, { get: [Function: get] } ]\n${prompt_unix}`
420+
},
421+
{
422+
client: client_unix,
423+
send: 'repl.writer.options.showProxy = false, new Proxy({x:42}, {});',
424+
expect: `{ x: 42 }\n${prompt_unix}`
425+
},
426+
416427
// Newline within template string maintains whitespace.
417428
{ client: client_unix, send: '`foo \n`',
418429
expect: `${prompt_multiline}'foo \\n'\n${prompt_unix}` },

0 commit comments

Comments
 (0)