Skip to content

Commit 511f8c1

Browse files
committed
quic: proper custom inspect for QuicEndpoint
PR-URL: #34247 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent fe11f6b commit 511f8c1

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

lib/internal/quic/core.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const {
2424
const { Buffer } = require('buffer');
2525
const { isArrayBufferView } = require('internal/util/types');
2626
const {
27+
customInspect,
2728
getAllowUnauthorized,
2829
getSocketType,
2930
lookup4,
@@ -638,14 +639,12 @@ class QuicEndpoint {
638639
socket[kHandle].addEndpoint(handle, preferred);
639640
}
640641

641-
[kInspect]() {
642-
// TODO(@jasnell): Proper custom inspect implementation
643-
const obj = {
642+
[kInspect](depth, options) {
643+
return customInspect(this, {
644644
address: this.address,
645-
fd: this[kInternalState].fd,
645+
fd: this.fd,
646646
type: this[kInternalState].type === AF_INET6 ? 'udp6' : 'udp4'
647-
};
648-
return `QuicEndpoint ${util.format(obj)}`;
647+
}, depth, options);
649648
}
650649

651650
// afterLookup is invoked when binding a QuicEndpoint. The first
@@ -741,7 +740,8 @@ class QuicEndpoint {
741740
}
742741

743742
get fd() {
744-
return this[kInternalState].fd;
743+
return this[kInternalState].fd >= 0 ?
744+
this[kInternalState].fd : undefined;
745745
}
746746

747747
// True if the QuicEndpoint has been destroyed and is

lib/internal/quic/util.js

+15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const {
2323
},
2424
} = require('internal/async_hooks');
2525

26+
const { inspect } = require('internal/util/inspect');
27+
2628
const { Readable } = require('stream');
2729
const {
2830
kHandle,
@@ -955,7 +957,20 @@ class QLogStream extends Readable {
955957
}
956958
}
957959

960+
function customInspect(self, obj, depth, options) {
961+
if (depth < 0)
962+
return self;
963+
964+
const opts = {
965+
...options,
966+
depth: options.depth == null ? null : options.depth - 1
967+
};
968+
969+
return `${self.constructor.name} ${inspect(obj, opts)}`;
970+
}
971+
958972
module.exports = {
973+
customInspect,
959974
getAllowUnauthorized,
960975
getSocketType,
961976
lookup4,

0 commit comments

Comments
 (0)