diff --git a/lib/internal/timers.js b/lib/internal/timers.js index 093284a80d4..be86153d5df 100644 --- a/lib/internal/timers.js +++ b/lib/internal/timers.js @@ -17,6 +17,8 @@ const { } = require('internal/errors').codes; const { validateNumber } = require('internal/validators'); +const { inspect } = require('util'); + // Timeout values > TIMEOUT_MAX are set to 1. const TIMEOUT_MAX = 2 ** 31 - 1; @@ -82,6 +84,17 @@ function Timeout(callback, after, args, isRepeat) { initAsyncResource(this, 'Timeout'); } +// Make sure the linked list only shows the minimal necessary information. +Timeout.prototype[inspect.custom] = function(_, options) { + return inspect(this, { + ...options, + // Only inspect one level. + depth: 0, + // It should not recurse. + customInspect: false + }); +}; + Timeout.prototype.refresh = function() { if (this[kRefed]) getTimers().active(this); diff --git a/lib/timers.js b/lib/timers.js index dc9b875ec37..0c5d7b07b2c 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -239,6 +239,17 @@ function TimersList(expiry, msecs) { this.priorityQueuePosition = null; } +// Make sure the linked list only shows the minimal necessary information. +TimersList.prototype[util.inspect.custom] = function(_, options) { + return util.inspect(this, { + ...options, + // Only inspect one level. + depth: 0, + // It should not recurse. + customInspect: false + }); +}; + const { _tickCallback: runNextTicks } = process; function processTimers(now) { debug('process timer lists %d', now); diff --git a/test/parallel/test-http2-socket-proxy.js b/test/parallel/test-http2-socket-proxy.js index 64daeb62bae..17f641835de 100644 --- a/test/parallel/test-http2-socket-proxy.js +++ b/test/parallel/test-http2-socket-proxy.js @@ -8,6 +8,7 @@ if (!common.hasCrypto) const assert = require('assert'); const h2 = require('http2'); const net = require('net'); +const util = require('util'); const { kTimeout } = require('internal/timers'); @@ -35,6 +36,22 @@ server.on('stream', common.mustCall(function(stream, headers) { socket.setTimeout(987); assert.strictEqual(session[kTimeout]._idleTimeout, 987); + // The indentation is corrected depending on the depth. + let inspectedTimeout = util.inspect(session[kTimeout]); + assert(inspectedTimeout.includes(' _idlePrev: [TimersList]')); + assert(inspectedTimeout.includes(' _idleNext: [TimersList]')); + assert(!inspectedTimeout.includes(' _idleNext: [TimersList]')); + + inspectedTimeout = util.inspect([ session[kTimeout] ]); + assert(inspectedTimeout.includes(' _idlePrev: [TimersList]')); + assert(inspectedTimeout.includes(' _idleNext: [TimersList]')); + assert(!inspectedTimeout.includes(' _idleNext: [TimersList]')); + + const inspectedTimersList = util.inspect([[ session[kTimeout]._idlePrev ]]); + assert(inspectedTimersList.includes(' _idlePrev: [Timeout]')); + assert(inspectedTimersList.includes(' _idleNext: [Timeout]')); + assert(!inspectedTimersList.includes(' _idleNext: [Timeout]')); + common.expectsError(() => socket.destroy, errMsg); common.expectsError(() => socket.emit, errMsg); common.expectsError(() => socket.end, errMsg);