Skip to content

Commit

Permalink
fix(core): Fixed handling of prematurely closed streams
Browse files Browse the repository at this point in the history
The API changed since the early experimental versions of Node's 'http2', although this fix shouldn't
break anything since fetch-h2 depends on v10

Closes #18
  • Loading branch information
grantila committed Jan 9, 2019
1 parent 7a5259a commit eee97c0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,13 @@ async function fetchImpl(
} )
);

stream.on( "streamClosed", guard( ( errorCode: number ) =>
stream.on( "close", guard( ( ) =>
{
// We'll get an 'error' event if there actually is an
// error, but not if we got NGHTTP2_NO_ERROR.
// In case of an error, the 'error' event will be awaited
// instead, to get (and propagate) the error object.
if ( errorCode === NGHTTP2_NO_ERROR )
if ( stream.rstCode === NGHTTP2_NO_ERROR )
reject(
new AbortError( "Stream prematurely closed" ) );
} ) );
Expand Down
24 changes: 23 additions & 1 deletion test/fetch-h2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,6 @@ describe( "goaway", ( ) =>

describe( "integrity", ( ) =>
{

it( "handle and succeed on valid integrity", async ( ) =>
{
const { server, port } = await makeServer( );
Expand Down Expand Up @@ -735,3 +734,26 @@ describe( "integrity", ( ) =>
await server.shutdown( );
} );
} );

describe( "premature stream close", ( ) =>
{
it( "handle and reject fetch operation", async ( ) =>
{
const { server, port } = await makeServer( );

const url = `http://localhost:${port}/prem-close`;

try
{
await fetch( url );
expect( false ).to.equal( true );
}
catch ( err )
{
expect( err.message ).to.contain( "Stream prematurely closed" );
}

await disconnectAll( );
await server.shutdown( );
} );
} );
4 changes: 4 additions & 0 deletions test/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ export class Server
ignoreError( ( ) => stream.write( "fghij" ) );
ignoreError( ( ) => stream.end( ) );
}
else if ( path.startsWith( "/prem-close" ) )
{
stream.close( );
}
else
{
const matched = ( this._opts.matchers || [ ] )
Expand Down

0 comments on commit eee97c0

Please sign in to comment.