diff --git a/src/diagnosticsChannel.ts b/src/diagnosticsChannel.ts index 51fe618f..97780e00 100644 --- a/src/diagnosticsChannel.ts +++ b/src/diagnosticsChannel.ts @@ -184,10 +184,15 @@ export function initDiagnosticsChannel() { // get socket from opaque const socket = opaque[symbols.kRequestSocket]; - socket[symbols.kHandledResponses]++; - debug('[%s] Request#%d get %s response headers on Socket#%d (handled %d responses, sock: %o)', - name, opaque[symbols.kRequestId], response.statusCode, socket[symbols.kSocketId], socket[symbols.kHandledResponses], - formatSocket(socket)); + if (socket) { + socket[symbols.kHandledResponses]++; + debug('[%s] Request#%d get %s response headers on Socket#%d (handled %d responses, sock: %o)', + name, opaque[symbols.kRequestId], response.statusCode, socket[symbols.kSocketId], socket[symbols.kHandledResponses], + formatSocket(socket)); + } else { + debug('[%s] Request#%d get %s response headers on Unknown Socket', + name, opaque[symbols.kRequestId], response.statusCode); + } if (!opaque[symbols.kEnableRequestTiming]) return; opaque[symbols.kRequestTiming].waiting = performanceTime(opaque[symbols.kRequestStartTime]); @@ -197,7 +202,9 @@ export function initDiagnosticsChannel() { subscribe('undici:request:trailers', (message, name) => { const { request } = message as DiagnosticsChannel.RequestTrailersMessage; const opaque = getRequestOpaque(request, kHandler); - if (!opaque || !opaque[symbols.kRequestId]) return; + if (!opaque || !opaque[symbols.kRequestId]) { + return; + } debug('[%s] Request#%d get response body and trailers', name, opaque[symbols.kRequestId]); diff --git a/test/options.dispatcher.test.ts b/test/options.dispatcher.test.ts index 0571f870..bde1e00e 100644 --- a/test/options.dispatcher.test.ts +++ b/test/options.dispatcher.test.ts @@ -1,7 +1,7 @@ import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; import setup from 'proxy'; -import { request, ProxyAgent, getGlobalDispatcher, setGlobalDispatcher } from '../src'; +import { request, ProxyAgent, getGlobalDispatcher, setGlobalDispatcher, Agent } from '../src'; import { startServer } from './fixtures/server'; describe('options.dispatcher.test.ts', () => { @@ -62,4 +62,19 @@ describe('options.dispatcher.test.ts', () => { assert.equal(response.data, '