Skip to content

Commit

Permalink
fix: adpater http/2 agent on diagnosticsChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jun 1, 2024
1 parent 00e196a commit 1f266ce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/diagnosticsChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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]);

Expand Down
17 changes: 16 additions & 1 deletion test/options.dispatcher.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -62,4 +62,19 @@ describe('options.dispatcher.test.ts', () => {
assert.equal(response.data, '<h1>hello</h1>');
setGlobalDispatcher(agent);
});

it('should work with http/2 dispatcher', async () => {
// https://github.com/nodejs/undici/issues/2750#issuecomment-1941009554
const agent = new Agent({
allowH2: true,
});
assert(agent);
const response = await request('https://registry.npmmirror.com', {

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "https://registry.npmmirror.com" is used as
authorization header
.
dataType: 'json',
timing: true,
dispatcher: agent,
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-type'], 'application/json; charset=utf-8');
});
});

0 comments on commit 1f266ce

Please sign in to comment.