Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
quic: proper custom inspect for QuicEndpoint
PR-URL: #34247
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
jasnell committed Jul 9, 2020
1 parent fe11f6b commit 511f8c1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/internal/quic/core.js
Expand Up @@ -24,6 +24,7 @@ const {
const { Buffer } = require('buffer');
const { isArrayBufferView } = require('internal/util/types');
const {
customInspect,
getAllowUnauthorized,
getSocketType,
lookup4,
Expand Down Expand Up @@ -638,14 +639,12 @@ class QuicEndpoint {
socket[kHandle].addEndpoint(handle, preferred);
}

[kInspect]() {
// TODO(@jasnell): Proper custom inspect implementation
const obj = {
[kInspect](depth, options) {
return customInspect(this, {
address: this.address,
fd: this[kInternalState].fd,
fd: this.fd,
type: this[kInternalState].type === AF_INET6 ? 'udp6' : 'udp4'
};
return `QuicEndpoint ${util.format(obj)}`;
}, depth, options);
}

// afterLookup is invoked when binding a QuicEndpoint. The first
Expand Down Expand Up @@ -741,7 +740,8 @@ class QuicEndpoint {
}

get fd() {
return this[kInternalState].fd;
return this[kInternalState].fd >= 0 ?
this[kInternalState].fd : undefined;
}

// True if the QuicEndpoint has been destroyed and is
Expand Down
15 changes: 15 additions & 0 deletions lib/internal/quic/util.js
Expand Up @@ -23,6 +23,8 @@ const {
},
} = require('internal/async_hooks');

const { inspect } = require('internal/util/inspect');

const { Readable } = require('stream');
const {
kHandle,
Expand Down Expand Up @@ -955,7 +957,20 @@ class QLogStream extends Readable {
}
}

function customInspect(self, obj, depth, options) {
if (depth < 0)
return self;

const opts = {
...options,
depth: options.depth == null ? null : options.depth - 1
};

return `${self.constructor.name} ${inspect(obj, opts)}`;
}

module.exports = {
customInspect,
getAllowUnauthorized,
getSocketType,
lookup4,
Expand Down

0 comments on commit 511f8c1

Please sign in to comment.