Skip to content

Commit

Permalink
fix(node): fixed https connection issue on Node.js 12 and 13
Browse files Browse the repository at this point in the history
by applying the workaround as in nodejs/node#33343

fix #104
  • Loading branch information
grantila committed Aug 20, 2020
1 parent bec7c32 commit f313c55
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
10 changes: 10 additions & 0 deletions lib/context-https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { connect, ConnectionOptions, TLSSocket } from "tls";
import { HttpProtocols } from "./core";
import { AltNameMatch, parseOrigin } from "./san";


const needsSocketHack = [ "12", "13" ]
.includes( process.versions.node.split( '.' )[ 0 ] );

const alpnProtocols =
{
http1: Buffer.from( "\x08http/1.1" ),
Expand Down Expand Up @@ -84,6 +88,12 @@ export function connectTLS(
resolve( { socket, protocol, altNameMatch } );
} );

if ( needsSocketHack )
socket.once( 'secureConnect', ( ) =>
{
( socket as any ).secureConnecting = false;
} );

socket.once( "error", reject );
} );
}
3 changes: 1 addition & 2 deletions lib/fetch-http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@ async function fetchImpl(
}
) );

stream.on( "response", guard(
( headers: IncomingHttp2Headers ) =>
stream.on( "response", guard( ( headers: IncomingHttp2Headers ) =>
{
const {
signal: bodySignal = void 0,
Expand Down
4 changes: 2 additions & 2 deletions test/fetch-h2/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe( `context (${version} over ${proto.replace( ":", "" )})`, ( ) =>
).toBeTruthy( );
}

disconnectAll( );
await disconnectAll( );

await server.shutdown( );
} );
Expand All @@ -154,7 +154,7 @@ describe( `context (${version} over ${proto.replace( ":", "" )})`, ( ) =>
const res = await response.json( );
expect( res[ "user-agent" ] ).toBe( "foobar" );

disconnectAll( );
await disconnectAll( );

await server.shutdown( );
} );
Expand Down
4 changes: 2 additions & 2 deletions test/fetch-h2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,8 @@ describe( `premature stream close (${protoVersion})`, ( ) =>
const expected =
version === "http1"
? "socket hang up"
: "Stream prematurely closed";
expect( err.message ).toContain( expected );
: [ "Stream prematurely closed", "Connection closed" ];
expect( expected ).toContain( err.message );
}

await disconnectAll( );
Expand Down

0 comments on commit f313c55

Please sign in to comment.