Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
util: show all nested proxies when inspecting
  • Loading branch information
BridgeAR committed Dec 20, 2025
commit 2ac85bdb7c5f129288470d63e072a0ac4da73ed6
13 changes: 9 additions & 4 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,7 @@ function formatValue(ctx, value, recurseTimes, typedArray) {

// Memorize the context for custom inspection on proxies.
const context = value;
let proxies = 0;
// Always check for proxies to prevent side effects and to prevent triggering
// any proxy handlers.
let proxy = getProxyDetails(value, !!ctx.showProxy);
Expand All @@ -1135,6 +1136,7 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
}
value = proxy;
proxy = getProxyDetails(value, false);
proxies += 1;
} while (proxy !== undefined);
}

Expand All @@ -1151,7 +1153,7 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
// a counter internally.
const depth = ctx.depth === null ? null : ctx.depth - recurseTimes;
const isCrossContext =
context !== value || !FunctionPrototypeSymbolHasInstance(Object, context);
proxies !== 0 || !FunctionPrototypeSymbolHasInstance(Object, context);
const ret = FunctionPrototypeCall(
maybeCustom,
context,
Expand Down Expand Up @@ -1187,10 +1189,13 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
return ctx.stylize(`[Circular *${index}]`, 'special');
}

const formatted = formatRaw(ctx, value, recurseTimes, typedArray);
let formatted = formatRaw(ctx, value, recurseTimes, typedArray);

if (proxy !== undefined) {
return `${ctx.stylize('Proxy(', 'special')}${formatted}${ctx.stylize(')', 'special')}`;
if (proxies !== 0) {
for (let i = 0; i < proxies; i++) {
formatted = `${ctx.stylize('Proxy(', 'special')}${formatted}${ctx.stylize(')', 'special')}`;
}
return formatted;
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
}

return formatted;
Expand Down
18 changes: 11 additions & 7 deletions test/parallel/test-util-inspect-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ const expected6 = 'Proxy [\n' +
' Proxy [ Proxy [Array], Proxy [Array] ]\n' +
' ]\n' +
']';
const expected2NoShowProxy = 'Proxy(Proxy({}))';
const expected3NoShowProxy = 'Proxy(Proxy(Proxy({})))';
const expected4NoShowProxy = 'Proxy(Proxy(Proxy(Proxy({}))))';
const expected5NoShowProxy = 'Proxy(Proxy(Proxy(Proxy(Proxy({})))))';
assert.strictEqual(
util.inspect(proxy1, { showProxy: 1, depth: null }),
expected1);
Expand All @@ -148,11 +152,11 @@ assert.strictEqual(util.inspect(proxy4, opts), expected4);
assert.strictEqual(util.inspect(proxy5, opts), expected5);
assert.strictEqual(util.inspect(proxy6, opts), expected6);
assert.strictEqual(util.inspect(proxy1), expected0);
assert.strictEqual(util.inspect(proxy2), expected0);
assert.strictEqual(util.inspect(proxy3), expected0);
assert.strictEqual(util.inspect(proxy4), expected0);
assert.strictEqual(util.inspect(proxy5), expected0);
assert.strictEqual(util.inspect(proxy6), expected0);
assert.strictEqual(util.inspect(proxy2), expected2NoShowProxy);
assert.strictEqual(util.inspect(proxy3), expected3NoShowProxy);
assert.strictEqual(util.inspect(proxy4), expected2NoShowProxy);
assert.strictEqual(util.inspect(proxy5), expected4NoShowProxy);
assert.strictEqual(util.inspect(proxy6), expected5NoShowProxy);

// Just for fun, let's create a Proxy using Arrays.
const proxy7 = new Proxy([], []);
Expand Down Expand Up @@ -197,8 +201,8 @@ assert.strictEqual(util.format('%s', proxy12), 'Proxy([ 1, 2, 3 ])');
// Nested proxies should not trigger any proxy handlers.
const nestedProxy = new Proxy(new Proxy(new Proxy({}, handler), {}), {});

util.inspect(nestedProxy, { showProxy: true });
util.inspect(nestedProxy, { showProxy: false });
assert.strictEqual(util.inspect(nestedProxy, { showProxy: true }), 'Proxy [ Proxy [ Proxy [ {}, [Object] ], {} ], {} ]');
assert.strictEqual(util.inspect(nestedProxy, { showProxy: false }), expected3NoShowProxy);
}

{
Expand Down