From 511f8c1138afad082bdad048a09ac345d8ec57a3 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 8 Jul 2020 09:40:23 -0700 Subject: [PATCH] quic: proper custom inspect for QuicEndpoint PR-URL: https://github.com/nodejs/node/pull/34247 Reviewed-By: Anna Henningsen --- lib/internal/quic/core.js | 14 +++++++------- lib/internal/quic/util.js | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/internal/quic/core.js b/lib/internal/quic/core.js index 9a1322a06db9d9..0b0629d37745e0 100644 --- a/lib/internal/quic/core.js +++ b/lib/internal/quic/core.js @@ -24,6 +24,7 @@ const { const { Buffer } = require('buffer'); const { isArrayBufferView } = require('internal/util/types'); const { + customInspect, getAllowUnauthorized, getSocketType, lookup4, @@ -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 @@ -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 diff --git a/lib/internal/quic/util.js b/lib/internal/quic/util.js index a7763c9bf3c15a..aff4f3cbc86afb 100644 --- a/lib/internal/quic/util.js +++ b/lib/internal/quic/util.js @@ -23,6 +23,8 @@ const { }, } = require('internal/async_hooks'); +const { inspect } = require('internal/util/inspect'); + const { Readable } = require('stream'); const { kHandle, @@ -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,