From 2d3138a99a98e8ffa76e7719dbab3ac4fbd3010a Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 12 Sep 2024 00:23:23 +0200 Subject: [PATCH] fix: DRY lib/core/diagnostics.js --- lib/core/diagnostics.js | 133 +++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 76 deletions(-) diff --git a/lib/core/diagnostics.js b/lib/core/diagnostics.js index e1af3db6112..35b825ec453 100644 --- a/lib/core/diagnostics.js +++ b/lib/core/diagnostics.js @@ -5,7 +5,7 @@ const util = require('node:util') const undiciDebugLog = util.debuglog('undici') const fetchDebuglog = util.debuglog('fetch') const websocketDebuglog = util.debuglog('websocket') -let isClientSet = false + const channels = { // Client beforeConnect: diagnosticsChannel.channel('undici:client:beforeConnect'), @@ -26,17 +26,23 @@ const channels = { pong: diagnosticsChannel.channel('undici:websocket:pong') } -if (undiciDebugLog.enabled || fetchDebuglog.enabled) { - const debuglog = fetchDebuglog.enabled ? fetchDebuglog : undiciDebugLog +let isTrackingClientEvents = false + +function trackClientEvents (debugLog = undiciDebugLog) { + if (isTrackingClientEvents) { + return + } + + isTrackingClientEvents = true - // Track all Client events diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(evt => { const { connectParams: { version, protocol, port, host } } = evt - debuglog( - 'connecting to %s using %s%s', - `${host}${port ? `:${port}` : ''}`, + debugLog( + 'connecting to %s%s using %s%s', + host, + port ? `:${port}` : '', protocol, version ) @@ -46,9 +52,10 @@ if (undiciDebugLog.enabled || fetchDebuglog.enabled) { const { connectParams: { version, protocol, port, host } } = evt - debuglog( - 'connected to %s using %s%s', - `${host}${port ? `:${port}` : ''}`, + debugLog( + 'connected to %s%s using %s%s', + host, + port ? `:${port}` : '', protocol, version ) @@ -59,9 +66,10 @@ if (undiciDebugLog.enabled || fetchDebuglog.enabled) { connectParams: { version, protocol, port, host }, error } = evt - debuglog( - 'connection to %s using %s%s errored - %s', - `${host}${port ? `:${port}` : ''}`, + debugLog( + 'connection to %s%s using %s%s errored - %s', + host, + port ? `:${port}` : '', protocol, version, error.message @@ -72,16 +80,25 @@ if (undiciDebugLog.enabled || fetchDebuglog.enabled) { const { request: { method, path, origin } } = evt - debuglog('sending request to %s %s/%s', method, origin, path) + debugLog('sending request to %s %s/%s', method, origin, path) }) +} + +let isTrackingRequestEvents = false + +function trackRequestEvents (debugLog = undiciDebugLog) { + if (isTrackingRequestEvents) { + return + } + + isTrackingRequestEvents = true - // Track Request events diagnosticsChannel.channel('undici:request:headers').subscribe(evt => { const { request: { method, path, origin }, response: { statusCode } } = evt - debuglog( + debugLog( 'received response to %s %s/%s - HTTP %d', method, origin, @@ -94,7 +111,7 @@ if (undiciDebugLog.enabled || fetchDebuglog.enabled) { const { request: { method, path, origin } } = evt - debuglog('trailers received from %s %s/%s', method, origin, path) + debugLog('trailers received from %s %s/%s', method, origin, path) }) diagnosticsChannel.channel('undici:request:error').subscribe(evt => { @@ -102,7 +119,7 @@ if (undiciDebugLog.enabled || fetchDebuglog.enabled) { request: { method, path, origin }, error } = evt - debuglog( + debugLog( 'request to %s %s/%s errored - %s', method, origin, @@ -110,73 +127,27 @@ if (undiciDebugLog.enabled || fetchDebuglog.enabled) { error.message ) }) - - isClientSet = true } -if (websocketDebuglog.enabled) { - if (!isClientSet) { - const debuglog = undiciDebugLog.enabled ? undiciDebugLog : websocketDebuglog - diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(evt => { - const { - connectParams: { version, protocol, port, host } - } = evt - debuglog( - 'connecting to %s%s using %s%s', - host, - port ? `:${port}` : '', - protocol, - version - ) - }) - - diagnosticsChannel.channel('undici:client:connected').subscribe(evt => { - const { - connectParams: { version, protocol, port, host } - } = evt - debuglog( - 'connected to %s%s using %s%s', - host, - port ? `:${port}` : '', - protocol, - version - ) - }) - - diagnosticsChannel.channel('undici:client:connectError').subscribe(evt => { - const { - connectParams: { version, protocol, port, host }, - error - } = evt - debuglog( - 'connection to %s%s using %s%s errored - %s', - host, - port ? `:${port}` : '', - protocol, - version, - error.message - ) - }) - - diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(evt => { - const { - request: { method, path, origin } - } = evt - debuglog('sending request to %s %s/%s', method, origin, path) - }) +let isTrackingWebSocketEvents = false + +function trackWebSocketEvents (debugLog = websocketDebuglog) { + if (isTrackingWebSocketEvents) { + return } - // Track all WebSocket events + isTrackingWebSocketEvents = true + diagnosticsChannel.channel('undici:websocket:open').subscribe(evt => { const { address: { address, port } } = evt - websocketDebuglog('connection opened %s%s', address, port ? `:${port}` : '') + debugLog('connection opened %s%s', address, port ? `:${port}` : '') }) diagnosticsChannel.channel('undici:websocket:close').subscribe(evt => { const { websocket, code, reason } = evt - websocketDebuglog( + debugLog( 'closed connection to %s - %s %s', websocket.url, code, @@ -185,18 +156,28 @@ if (websocketDebuglog.enabled) { }) diagnosticsChannel.channel('undici:websocket:socket_error').subscribe(err => { - websocketDebuglog('connection errored - %s', err.message) + debugLog('connection errored - %s', err.message) }) diagnosticsChannel.channel('undici:websocket:ping').subscribe(evt => { - websocketDebuglog('ping received') + debugLog('ping received') }) diagnosticsChannel.channel('undici:websocket:pong').subscribe(evt => { - websocketDebuglog('pong received') + debugLog('pong received') }) } +if (undiciDebugLog.enabled || fetchDebuglog.enabled) { + trackClientEvents(fetchDebuglog.enabled ? fetchDebuglog : undiciDebugLog) + trackRequestEvents(fetchDebuglog.enabled ? fetchDebuglog : undiciDebugLog) +} + +if (websocketDebuglog.enabled) { + trackClientEvents(undiciDebugLog.enabled ? undiciDebugLog : websocketDebuglog) + trackWebSocketEvents(websocketDebuglog) +} + module.exports = { channels }