Skip to content

Commit 8b3644e

Browse files
committed
refactor: adjust on-connect call
1 parent c0cf8bb commit 8b3644e

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

lib/client.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,19 +1665,6 @@ function writeH2 (client, session, request) {
16651665
return false
16661666
}
16671667

1668-
try {
1669-
// TODO(HTTP/2): Should we call onConnect immediately or on stream ready event?
1670-
request.onConnect((err) => {
1671-
if (request.aborted || request.completed) {
1672-
return
1673-
}
1674-
1675-
errorRequest(client, request, err || new RequestAbortedError())
1676-
})
1677-
} catch (err) {
1678-
errorRequest(client, request, err)
1679-
}
1680-
16811668
if (request.aborted) {
16821669
return false
16831670
}
@@ -1689,9 +1676,34 @@ function writeH2 (client, session, request) {
16891676
headers[HTTP2_HEADER_AUTHORITY] = host || client[kHost]
16901677
headers[HTTP2_HEADER_METHOD] = method
16911678

1679+
try {
1680+
// We are already connected, streams are pending.
1681+
// We can call on connect, and wait for abort
1682+
request.onConnect((err) => {
1683+
if (request.aborted || request.completed) {
1684+
return
1685+
}
1686+
1687+
err = err || new RequestAbortedError()
1688+
1689+
if (stream != null) {
1690+
util.destroy(stream, err)
1691+
1692+
h2State.openStreams -= 1
1693+
if (h2State.openStreams === 0) {
1694+
session.unref()
1695+
}
1696+
}
1697+
1698+
errorRequest(client, request, err)
1699+
})
1700+
} catch (err) {
1701+
errorRequest(client, request, err)
1702+
}
1703+
16921704
if (method === 'CONNECT') {
16931705
session.ref()
1694-
// we are already connected, streams are pending, first request
1706+
// We are already connected, streams are pending, first request
16951707
// will create a new stream. We trigger a request to create the stream and wait until
16961708
// `ready` event is triggered
16971709
// We disabled endStream to allow the user to write to the stream
@@ -1805,18 +1817,14 @@ function writeH2 (client, session, request) {
18051817
})
18061818

18071819
stream.on('data', (chunk) => {
1808-
// Aborting a request does not abort
1809-
// the stream, so we need to check and destroy it if request
1810-
// is aborted.
1811-
if (request.aborted) util.destroy(stream, new RequestAbortedError())
1812-
18131820
if (request.onData(chunk) === false) stream.pause()
18141821
})
18151822

18161823
stream.once('close', () => {
18171824
h2State.openStreams -= 1
1818-
// TODO(HTTP/2): unref only if current streams count is 0
1819-
if (h2State.openStreams === 0) session.unref()
1825+
if (h2State.openStreams === 0) {
1826+
session.unref()
1827+
}
18201828
})
18211829

18221830
stream.once('error', function (err) {

test/fetch/http2.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { Client, fetch, Headers } = require('../..')
1313

1414
const nodeVersion = Number(process.version.split('v')[1].split('.')[0])
1515

16-
plan(7)
16+
plan(8)
1717

1818
test('[Fetch] Issue#2311', async t => {
1919
const expectedBody = 'hello from client!'
@@ -438,7 +438,7 @@ test('Issue #2386', async t => {
438438
stream.end(JSON.stringify(expectedResponseBody))
439439
})
440440

441-
t.plan(3)
441+
t.plan(4)
442442

443443
server.listen(0)
444444
await once(server, 'listening')
@@ -470,6 +470,7 @@ test('Issue #2386', async t => {
470470
)
471471

472472
controller.abort()
473+
t.pass()
473474
} catch (error) {
474475
t.error(error)
475476
}

0 commit comments

Comments
 (0)