@@ -1665,19 +1665,6 @@ function writeH2 (client, session, request) {
1665
1665
return false
1666
1666
}
1667
1667
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
-
1681
1668
if ( request . aborted ) {
1682
1669
return false
1683
1670
}
@@ -1689,9 +1676,34 @@ function writeH2 (client, session, request) {
1689
1676
headers [ HTTP2_HEADER_AUTHORITY ] = host || client [ kHost ]
1690
1677
headers [ HTTP2_HEADER_METHOD ] = method
1691
1678
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
+
1692
1704
if ( method === 'CONNECT' ) {
1693
1705
session . ref ( )
1694
- // we are already connected, streams are pending, first request
1706
+ // We are already connected, streams are pending, first request
1695
1707
// will create a new stream. We trigger a request to create the stream and wait until
1696
1708
// `ready` event is triggered
1697
1709
// We disabled endStream to allow the user to write to the stream
@@ -1805,18 +1817,14 @@ function writeH2 (client, session, request) {
1805
1817
} )
1806
1818
1807
1819
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
-
1813
1820
if ( request . onData ( chunk ) === false ) stream . pause ( )
1814
1821
} )
1815
1822
1816
1823
stream . once ( 'close' , ( ) => {
1817
1824
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
+ }
1820
1828
} )
1821
1829
1822
1830
stream . once ( 'error' , function ( err ) {
0 commit comments