1+ const nodeFetch = require ( 'node-fetch' ) ;
2+ const http2 = require ( "http2" ) ;
3+ const setCookie = require ( "set-cookie-parser" ) ;
4+
5+ const httpMainController = { } ;
6+
7+ // array of open http2 connections
8+ httpMainController . openHTTP2Connections = [ ] ;
9+
10+ httpMainController . openHTTPconnection = ( reqResObj , connectionArray ) => {
11+ // http2 is currentonly supported over https
12+ reqResObj . protocol === "https://"
13+ ? httpController . establishHTTP2Connection ( reqResObj , connectionArray )
14+ : httpController . establishHTTP1connection ( reqResObj , connectionArray ) ;
15+ } ;
16+
17+ httpMainController . establishHTTP2Connection = ( reqResObj , connectionArray ) => {
18+ /*
19+ Attempt to find an existing HTTP2 connection in openHTTP2Connections Array.
20+ If exists, use connection to initiate request
21+ If not, create connection, push to array, and then initiate request
22+ */
23+
24+ // find connection with same host as passed in reqResObj
25+ const foundHTTP2Connection = httpMainController . openHTTP2Connections . find (
26+ ( conn ) => conn . host === reqResObj . host
27+ ) ;
28+
29+ // EXISTING HTTP2 CONNECTION IS FOUND -----
30+
31+ if ( foundHTTP2Connection ) {
32+ const { client } = foundHTTP2Connection ;
33+
34+ // periodically check if the client is open or destroyed, and attach if conditions are met
35+ const interval = setInterval ( ( ) => {
36+ if ( foundHTTP2Connection . status === "connected" ) {
37+ this . attachRequestToHTTP2Client ( client , reqResObj , connectionArray ) ;
38+ clearInterval ( interval ) ;
39+ }
40+ // if failed, could because of protocol error. try HTTP1
41+ else if ( foundHTTP2Connection . status === "failed" || client . destroyed ) {
42+ httpController . establishHTTP1connection ( reqResObj , connectionArray ) ;
43+ clearInterval ( interval ) ;
44+ }
45+ } , 50 ) ;
46+
47+ // --------------------------------------------------
48+ // if hasnt changed in 10 seconds, mark as error
49+ // --------------------------------------------------
50+ setTimeout ( ( ) => {
51+ clearInterval ( interval ) ;
52+ if ( foundHTTP2Connection . status === "initialized" ) {
53+ reqResObj . connection = "error" ;
54+ store . default . dispatch ( actions . reqResUpdate ( reqResObj ) ) ;
55+ }
56+ } , 10000 ) ;
57+ } ;
58+ }
0 commit comments