diff --git a/lib/internal/timers.js b/lib/internal/timers.js index d33ad0c8ac8027..d5075582ec6270 100644 --- a/lib/internal/timers.js +++ b/lib/internal/timers.js @@ -16,6 +16,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; @@ -80,6 +82,17 @@ function Timeout(callback, after, args, isRepeat, isUnrefed) { 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._handle) { // Would be more ideal with uv_timer_again(), however that API does not diff --git a/lib/timers.js b/lib/timers.js index 55b6b53c5c8fd5..784a0051e8dbd4 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -206,6 +206,17 @@ function TimersList(msecs, unrefed) { timer.start(msecs); } +// 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 + }); +}; + function processTimers(now) { if (this[owner_symbol]) return unrefdHandle(this[owner_symbol], now); diff --git a/test/parallel/test-http2-socket-proxy.js b/test/parallel/test-http2-socket-proxy.js index 64daeb62baebf2..17f641835de696 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);