codekingpro/portable-devtools
114k
1'use strict'
2const diagnosticsChannel = require('node:diagnostics_channel')
3const util = require('node:util')
4
5const undiciDebugLog = util.debuglog('undici')
6const fetchDebuglog = util.debuglog('fetch')
7const websocketDebuglog = util.debuglog('websocket')
8let isClientSet = false
9const channels = {
10 // Client
11 beforeConnect: diagnosticsChannel.channel('undici:client:beforeConnect'),
12 connected: diagnosticsChannel.channel('undici:client:connected'),
13 connectError: diagnosticsChannel.channel('undici:client:connectError'),
14 sendHeaders: diagnosticsChannel.channel('undici:client:sendHeaders'),
15 // Request
16 create: diagnosticsChannel.channel('undici:request:create'),
17 bodySent: diagnosticsChannel.channel('undici:request:bodySent'),
18 headers: diagnosticsChannel.channel('undici:request:headers'),
19 trailers: diagnosticsChannel.channel('undici:request:trailers'),
20 error: diagnosticsChannel.channel('undici:request:error'),
21 // WebSocket
22 open: diagnosticsChannel.channel('undici:websocket:open'),
23 close: diagnosticsChannel.channel('undici:websocket:close'),
24 socketError: diagnosticsChannel.channel('undici:websocket:socket_error'),
25 ping: diagnosticsChannel.channel('undici:websocket:ping'),
26 pong: diagnosticsChannel.channel('undici:websocket:pong')
27}
28
29if (undiciDebugLog.enabled || fetchDebuglog.enabled) {
30 const debuglog = fetchDebuglog.enabled ? fetchDebuglog : undiciDebugLog
31
32 // Track all Client events
33 diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(evt => {
34 const {
35 connectParams: { version, protocol, port, host }
36 } = evt
37 debuglog(
38 'connecting to %s using %s%s',
39 `${host}${port ? `:${port}` : ''}`,
40 protocol,
41 version
42 )
43 })
44
45 diagnosticsChannel.channel('undici:client:connected').subscribe(evt => {
46 const {
47 connectParams: { version, protocol, port, host }
48 } = evt
49 debuglog(
50 'connected to %s using %s%s',
51 `${host}${port ? `:${port}` : ''}`,
52 protocol,
53 version
54 )
55 })
56
57 diagnosticsChannel.channel('undici:client:connectError').subscribe(evt => {
58 const {
59 connectParams: { version, protocol, port, host },
60 error
61 } = evt
62 debuglog(
63 'connection to %s using %s%s errored - %s',
64 `${host}${port ? `:${port}` : ''}`,
65 protocol,
66 version,
67 error.message
68 )
69 })
70
71 diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(evt => {
72 const {
73 request: { method, path, origin }
74 } = evt
75 debuglog('sending request to %s %s/%s', method, origin, path)
76 })
77
78 // Track Request events
79 diagnosticsChannel.channel('undici:request:headers').subscribe(evt => {
80 const {
81 request: { method, path, origin },
82 response: { statusCode }
83 } = evt
84 debuglog(
85 'received response to %s %s/%s - HTTP %d',
86 method,
87 origin,
88 path,
89 statusCode
90 )
91 })
92
93 diagnosticsChannel.channel('undici:request:trailers').subscribe(evt => {
94 const {
95 request: { method, path, origin }
96 } = evt
97 debuglog('trailers received from %s %s/%s', method, origin, path)
98 })
99
100 diagnosticsChannel.channel('undici:request:error').subscribe(evt => {
101 const {
102 request: { method, path, origin },
103 error
104 } = evt
105 debuglog(
106 'request to %s %s/%s errored - %s',
107 method,
108 origin,
109 path,
110 error.message
111 )
112 })
113
114 isClientSet = true
115}
116
117if (websocketDebuglog.enabled) {
118 if (!isClientSet) {
119 const debuglog = undiciDebugLog.enabled ? undiciDebugLog : websocketDebuglog
120 diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(evt => {
121 const {
122 connectParams: { version, protocol, port, host }
123 } = evt
124 debuglog(
125 'connecting to %s%s using %s%s',
126 host,
127 port ? `:${port}` : '',
128 protocol,
129 version
130 )
131 })
132
133 diagnosticsChannel.channel('undici:client:connected').subscribe(evt => {
134 const {
135 connectParams: { version, protocol, port, host }
136 } = evt
137 debuglog(
138 'connected to %s%s using %s%s',
139 host,
140 port ? `:${port}` : '',
141 protocol,
142 version
143 )
144 })
145
146 diagnosticsChannel.channel('undici:client:connectError').subscribe(evt => {
147 const {
148 connectParams: { version, protocol, port, host },
149 error
150 } = evt
151 debuglog(
152 'connection to %s%s using %s%s errored - %s',
153 host,
154 port ? `:${port}` : '',
155 protocol,
156 version,
157 error.message
158 )
159 })
160
161 diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(evt => {
162 const {
163 request: { method, path, origin }
164 } = evt
165 debuglog('sending request to %s %s/%s', method, origin, path)
166 })
167 }
168
169 // Track all WebSocket events
170 diagnosticsChannel.channel('undici:websocket:open').subscribe(evt => {
171 const {
172 address: { address, port }
173 } = evt
174 websocketDebuglog('connection opened %s%s', address, port ? `:${port}` : '')
175 })
176
177 diagnosticsChannel.channel('undici:websocket:close').subscribe(evt => {
178 const { websocket, code, reason } = evt
179 websocketDebuglog(
180 'closed connection to %s - %s %s',
181 websocket.url,
182 code,
183 reason
184 )
185 })
186
187 diagnosticsChannel.channel('undici:websocket:socket_error').subscribe(err => {
188 websocketDebuglog('connection errored - %s', err.message)
189 })
190
191 diagnosticsChannel.channel('undici:websocket:ping').subscribe(evt => {
192 websocketDebuglog('ping received')
193 })
194
195 diagnosticsChannel.channel('undici:websocket:pong').subscribe(evt => {
196 websocketDebuglog('pong received')
197 })
198}
199
200module.exports = {
201 channels
202}
203 