Skip to content

Commit 4436a3d

Browse files
BridgeARtargos
authored andcommitted
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. PR-URL: #23108 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent a2c1ce2 commit 4436a3d

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
@@ -16,6 +16,8 @@ const {
1616
} = require('internal/errors').codes;
1717
const { validateNumber } = require('internal/validators');
1818

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

@@ -80,6 +82,17 @@ function Timeout(callback, after, args, isRepeat, isUnrefed) {
8082
initAsyncResource(this, 'Timeout');
8183
}
8284

85+
// Make sure the linked list only shows the minimal necessary information.
86+
Timeout.prototype[inspect.custom] = function(_, options) {
87+
return inspect(this, {
88+
...options,
89+
// Only inspect one level.
90+
depth: 0,
91+
// It should not recurse.
92+
customInspect: false
93+
});
94+
};
95+
8396
Timeout.prototype.refresh = function() {
8497
if (this._handle) {
8598
// Would be more ideal with uv_timer_again(), however that API does not

lib/timers.js

+11
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ function TimersList(msecs, unrefed) {
206206
timer.start(msecs);
207207
}
208208

209+
// Make sure the linked list only shows the minimal necessary information.
210+
TimersList.prototype[util.inspect.custom] = function(_, options) {
211+
return util.inspect(this, {
212+
...options,
213+
// Only inspect one level.
214+
depth: 0,
215+
// It should not recurse.
216+
customInspect: false
217+
});
218+
};
219+
209220
function processTimers(now) {
210221
if (this[owner_symbol])
211222
return unrefdHandle(this[owner_symbol], 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)