Skip to content

Commit d565da2

Browse files
authored
fix: adpater http/2 agent on diagnosticsChannel (#511)
#510
1 parent 00e196a commit d565da2

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

.github/workflows/nodejs.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ name: CI
33
on:
44
push:
55
branches: [ master ]
6-
76
pull_request:
87
branches: [ master ]
98

@@ -13,5 +12,6 @@ jobs:
1312
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1413
with:
1514
os: 'ubuntu-latest, macos-latest, windows-latest'
16-
version: '14.19.3, 14, 16, 18, 20, 21'
17-
install: 'npx npminstall'
15+
version: '14.19.3, 14, 16, 18, 20, 22'
16+
secrets:
17+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/release.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@ jobs:
1111
secrets:
1212
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
1313
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
14-
with:
15-
install: 'npx npminstall'

src/diagnosticsChannel.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,15 @@ export function initDiagnosticsChannel() {
184184

185185
// get socket from opaque
186186
const socket = opaque[symbols.kRequestSocket];
187-
socket[symbols.kHandledResponses]++;
188-
debug('[%s] Request#%d get %s response headers on Socket#%d (handled %d responses, sock: %o)',
189-
name, opaque[symbols.kRequestId], response.statusCode, socket[symbols.kSocketId], socket[symbols.kHandledResponses],
190-
formatSocket(socket));
187+
if (socket) {
188+
socket[symbols.kHandledResponses]++;
189+
debug('[%s] Request#%d get %s response headers on Socket#%d (handled %d responses, sock: %o)',
190+
name, opaque[symbols.kRequestId], response.statusCode, socket[symbols.kSocketId], socket[symbols.kHandledResponses],
191+
formatSocket(socket));
192+
} else {
193+
debug('[%s] Request#%d get %s response headers on Unknown Socket',
194+
name, opaque[symbols.kRequestId], response.statusCode);
195+
}
191196

192197
if (!opaque[symbols.kEnableRequestTiming]) return;
193198
opaque[symbols.kRequestTiming].waiting = performanceTime(opaque[symbols.kRequestStartTime]);
@@ -197,7 +202,9 @@ export function initDiagnosticsChannel() {
197202
subscribe('undici:request:trailers', (message, name) => {
198203
const { request } = message as DiagnosticsChannel.RequestTrailersMessage;
199204
const opaque = getRequestOpaque(request, kHandler);
200-
if (!opaque || !opaque[symbols.kRequestId]) return;
205+
if (!opaque || !opaque[symbols.kRequestId]) {
206+
return;
207+
}
201208

202209
debug('[%s] Request#%d get response body and trailers', name, opaque[symbols.kRequestId]);
203210

test/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('index.test.ts', () => {
3232
assert.equal(response.url, `${_url}html`);
3333
assert(!response.redirected);
3434
assert.equal(getDefaultHttpClient(), getDefaultHttpClient());
35-
console.log('stats %o', getDefaultHttpClient().getDispatcherPoolStats());
35+
// console.log('stats %o', getDefaultHttpClient().getDispatcherPoolStats());
3636
});
3737
});
3838

@@ -76,7 +76,7 @@ describe('index.test.ts', () => {
7676
dataType: 'json',
7777
});
7878
assert.equal(response.status, 200);
79-
console.log(response.data.hello);
79+
// console.log(response.data.hello);
8080
});
8181

8282
it('should curl alias to request', async () => {

test/keep-alive-header.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('keep-alive-header.test.ts', () => {
3131
count++;
3232
try {
3333
const task = httpClient.request(_url);
34-
console.log('after request stats: %o', httpClient.getDispatcherPoolStats());
34+
// console.log('after request stats: %o', httpClient.getDispatcherPoolStats());
3535
if (httpClient.getDispatcherPoolStats()[origin]) {
3636
if (!(nodeMajorVersion() === 14 || isWindows())) {
3737
// ignore node = 14 & windows
@@ -40,7 +40,7 @@ describe('keep-alive-header.test.ts', () => {
4040
}
4141
}
4242
let response = await task;
43-
console.log('after response stats: %o', httpClient.getDispatcherPoolStats());
43+
// console.log('after response stats: %o', httpClient.getDispatcherPoolStats());
4444
assert.equal(httpClient.getDispatcherPoolStats()[origin].pending, 0);
4545
assert.equal(httpClient.getDispatcherPoolStats()[origin].connected, 1);
4646
// console.log(response.res.socket);
@@ -129,12 +129,12 @@ describe('keep-alive-header.test.ts', () => {
129129
assert.equal(response.headers.connection, 'keep-alive');
130130
assert.equal(response.headers['keep-alive'], 'timeout=2');
131131
assert(parseInt(response.headers['x-requests-persocket'] as string) > 1);
132-
console.log('before sleep stats: %o', httpClient.getDispatcherPoolStats());
132+
// console.log('before sleep stats: %o', httpClient.getDispatcherPoolStats());
133133
// { connected: 2, free: 1, pending: 0, queued: 0, running: 0, size: 0 }
134134
assert.equal(httpClient.getDispatcherPoolStats()[origin].connected, 2);
135135
assert.equal(httpClient.getDispatcherPoolStats()[origin].free, 1);
136136
await sleep(keepAliveTimeout);
137-
console.log('after sleep stats: %o', httpClient.getDispatcherPoolStats());
137+
// console.log('after sleep stats: %o', httpClient.getDispatcherPoolStats());
138138
// clients maybe all gone => after sleep stats: {}
139139
// { connected: 0, free: 0, pending: 0, queued: 0, running: 0, size: 0 }
140140
// { connected: 1, free: 1, pending: 0, queued: 0, running: 0, size: 0 }

test/options.dispatcher.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { strict as assert } from 'node:assert';
22
import { describe, it, beforeAll, afterAll } from 'vitest';
33
import setup from 'proxy';
4-
import { request, ProxyAgent, getGlobalDispatcher, setGlobalDispatcher } from '../src';
4+
import { request, ProxyAgent, getGlobalDispatcher, setGlobalDispatcher, Agent } from '../src';
55
import { startServer } from './fixtures/server';
66

77
describe('options.dispatcher.test.ts', () => {
@@ -62,4 +62,19 @@ describe('options.dispatcher.test.ts', () => {
6262
assert.equal(response.data, '<h1>hello</h1>');
6363
setGlobalDispatcher(agent);
6464
});
65+
66+
it('should work with http/2 dispatcher', async () => {
67+
// https://github.com/nodejs/undici/issues/2750#issuecomment-1941009554
68+
const agent = new Agent({
69+
allowH2: true,
70+
});
71+
assert(agent);
72+
const response = await request('https://registry.npmmirror.com', {
73+
dataType: 'json',
74+
timing: true,
75+
dispatcher: agent,
76+
});
77+
assert.equal(response.status, 200);
78+
assert.equal(response.headers['content-type'], 'application/json; charset=utf-8');
79+
});
6580
});

0 commit comments

Comments
 (0)