From eee97c095368847e86c9ac3811dd2e04d3880458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustaf=20R=C3=A4ntil=C3=A4?= Date: Wed, 9 Jan 2019 01:05:30 +0100 Subject: [PATCH] fix(core): Fixed handling of prematurely closed streams 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 --- lib/fetch.ts | 4 ++-- test/fetch-h2/index.ts | 24 +++++++++++++++++++++++- test/lib/server.ts | 4 ++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/fetch.ts b/lib/fetch.ts index 6384756..3f7d472 100644 --- a/lib/fetch.ts +++ b/lib/fetch.ts @@ -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" ) ); } ) ); diff --git a/test/fetch-h2/index.ts b/test/fetch-h2/index.ts index 196c890..d950a04 100644 --- a/test/fetch-h2/index.ts +++ b/test/fetch-h2/index.ts @@ -690,7 +690,6 @@ describe( "goaway", ( ) => describe( "integrity", ( ) => { - it( "handle and succeed on valid integrity", async ( ) => { const { server, port } = await makeServer( ); @@ -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( ); + } ); +} ); diff --git a/test/lib/server.ts b/test/lib/server.ts index 8ea5cb1..ddcaa81 100644 --- a/test/lib/server.ts +++ b/test/lib/server.ts @@ -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 || [ ] )