Skip to content

Commit 1ad6994

Browse files
committed
timers: use custom inspection for linked lists
Inspecting linked lists is something that is not really useful. Instead, just use a custom inspection function and hide everything besides the first level.
1 parent d6a6df9 commit 1ad6994

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

lib/internal/timers.js

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const {
1717
} = require('internal/errors').codes;
1818
const { validateNumber } = require('internal/validators');
1919

20+
const { inspect } = require('util');
21+
2022
// Timeout values > TIMEOUT_MAX are set to 1.
2123
const TIMEOUT_MAX = 2 ** 31 - 1;
2224

@@ -82,6 +84,17 @@ function Timeout(callback, after, args, isRepeat) {
8284
initAsyncResource(this, 'Timeout');
8385
}
8486

87+
// Make sure the linked list only shows the minimal necessary information.
88+
Timeout.prototype[inspect.custom] = function(_, options) {
89+
return inspect(this, {
90+
...options,
91+
// Only inspect one level.
92+
depth: 0,
93+
// It should not recurse.
94+
customInspect: false
95+
});
96+
};
97+
8598
Timeout.prototype.refresh = function() {
8699
if (this[kRefed])
87100
getTimers().active(this);

lib/timers.js

+11
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,17 @@ function TimersList(expiry, msecs) {
239239
this.priorityQueuePosition = null;
240240
}
241241

242+
// Make sure the linked list only shows the minimal necessary information.
243+
TimersList.prototype[util.inspect.custom] = function(_, options) {
244+
return util.inspect(this, {
245+
...options,
246+
// Only inspect one level.
247+
depth: 0,
248+
// It should not recurse.
249+
customInspect: false
250+
});
251+
};
252+
242253
const { _tickCallback: runNextTicks } = process;
243254
function processTimers(now) {
244255
debug('process timer lists %d', now);

test/parallel/test-http2-socket-proxy.js

+17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ if (!common.hasCrypto)
88
const assert = require('assert');
99
const h2 = require('http2');
1010
const net = require('net');
11+
const util = require('util');
1112

1213
const { kTimeout } = require('internal/timers');
1314

@@ -35,6 +36,22 @@ server.on('stream', common.mustCall(function(stream, headers) {
3536
socket.setTimeout(987);
3637
assert.strictEqual(session[kTimeout]._idleTimeout, 987);
3738

39+
// The indentation is corrected depending on the depth.
40+
let inspectedTimeout = util.inspect(session[kTimeout]);
41+
assert(inspectedTimeout.includes(' _idlePrev: [TimersList]'));
42+
assert(inspectedTimeout.includes(' _idleNext: [TimersList]'));
43+
assert(!inspectedTimeout.includes(' _idleNext: [TimersList]'));
44+
45+
inspectedTimeout = util.inspect([ session[kTimeout] ]);
46+
assert(inspectedTimeout.includes(' _idlePrev: [TimersList]'));
47+
assert(inspectedTimeout.includes(' _idleNext: [TimersList]'));
48+
assert(!inspectedTimeout.includes(' _idleNext: [TimersList]'));
49+
50+
const inspectedTimersList = util.inspect([[ session[kTimeout]._idlePrev ]]);
51+
assert(inspectedTimersList.includes(' _idlePrev: [Timeout]'));
52+
assert(inspectedTimersList.includes(' _idleNext: [Timeout]'));
53+
assert(!inspectedTimersList.includes(' _idleNext: [Timeout]'));
54+
3855
common.expectsError(() => socket.destroy, errMsg);
3956
common.expectsError(() => socket.emit, errMsg);
4057
common.expectsError(() => socket.end, errMsg);

0 commit comments

Comments
 (0)