Skip to content

Commit 8d1e8d9

Browse files
authored
fix(pool): propagate useH2c to connector when connections > 1 (#5031)
1 parent 759602e commit 8d1e8d9

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

lib/dispatcher/pool.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Pool extends PoolBase {
3636
autoSelectFamily,
3737
autoSelectFamilyAttemptTimeout,
3838
allowH2,
39+
useH2c,
3940
clientTtl,
4041
...options
4142
} = {}) {
@@ -56,6 +57,7 @@ class Pool extends PoolBase {
5657
...tls,
5758
maxCachedSessions,
5859
allowH2,
60+
useH2c,
5961
socketPath,
6062
timeout: connectTimeout,
6163
...(typeof autoSelectFamily === 'boolean' ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : undefined),
@@ -67,7 +69,7 @@ class Pool extends PoolBase {
6769

6870
this[kConnections] = connections || null
6971
this[kUrl] = util.parseOrigin(origin)
70-
this[kOptions] = { ...util.deepClone(options), connect, allowH2, clientTtl, socketPath }
72+
this[kOptions] = { ...util.deepClone(options), connect, allowH2, useH2c, clientTtl, socketPath }
7173
this[kFactory] = factory
7274

7375
this.on('connect', (origin, targets) => {

test/h2c-client.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { test } = require('node:test')
77
const { tspl } = require('@matteo.collina/tspl')
88
const pem = require('@metcoder95/https-pem')
99

10-
const { H2CClient, Client } = require('..')
10+
const { H2CClient, Client, Agent, Pool, request } = require('..')
1111

1212
test('Should throw if no h2c origin', async t => {
1313
const planner = tspl(t, { plan: 1 })
@@ -180,3 +180,63 @@ test('Should throw if bad useH2c has been passed', async t => {
180180

181181
await t.completed
182182
})
183+
184+
test('Pool with useH2c and connections > 1 should not raise HTTPParserError', async t => {
185+
const planner = tspl(t, { plan: 6 })
186+
187+
const server = createServer((req, res) => {
188+
res.writeHead(200)
189+
res.end('Hello, world!')
190+
})
191+
192+
server.listen(0)
193+
await once(server, 'listening')
194+
const url = `http://localhost:${server.address().port}`
195+
const pool = new Pool(url, { useH2c: true, connections: 2 })
196+
197+
t.after(() => pool.close())
198+
t.after(() => server.close())
199+
200+
const responses = await Promise.all([
201+
pool.request({ path: '/test1', method: 'GET' }),
202+
pool.request({ path: '/test2', method: 'GET' }),
203+
pool.request({ path: '/test3', method: 'GET' })
204+
])
205+
206+
for (const response of responses) {
207+
planner.equal(response.statusCode, 200)
208+
planner.equal(await response.body.text(), 'Hello, world!')
209+
}
210+
211+
await planner.completed
212+
})
213+
214+
test('Agent with useH2c and connections > 1 should not raise HTTPParserError', async t => {
215+
const planner = tspl(t, { plan: 6 })
216+
217+
const server = createServer((req, res) => {
218+
res.writeHead(200)
219+
res.end('Hello, world!')
220+
})
221+
222+
server.listen(0)
223+
await once(server, 'listening')
224+
const port = server.address().port
225+
const agent = new Agent({ useH2c: true, connections: 2 })
226+
227+
t.after(() => agent.close())
228+
t.after(() => server.close())
229+
230+
const responses = await Promise.all([
231+
request(`http://localhost:${port}/test1`, { dispatcher: agent }),
232+
request(`http://localhost:${port}/test2`, { dispatcher: agent }),
233+
request(`http://localhost:${port}/test3`, { dispatcher: agent })
234+
])
235+
236+
for (const response of responses) {
237+
planner.equal(response.statusCode, 200)
238+
planner.equal(await response.body.text(), 'Hello, world!')
239+
}
240+
241+
await planner.completed
242+
})

0 commit comments

Comments
 (0)