@@ -252,24 +252,32 @@ export function createClient(configuration: ClientConfiguration, options?: Creat
252252 if ( options ?. useHttp2 !== true ) {
253253 return fetch ( url , grabOptions ) ;
254254 }
255+ const closeAndDeleteClient = ( origin : string ) => {
256+ const clientObj = clients . get ( origin ) ;
257+ if ( clientObj ) {
258+ clientObj . client . close ( ) ;
259+ clients . delete ( origin ) ;
260+ }
261+ } ;
262+
255263 const resetIdleTimeout = ( origin : string ) => {
256264 const clientObj = clients . get ( origin ) ;
257- if ( clientObj . idleTimeout ) {
265+ if ( clientObj && clientObj . idleTimeout ) {
258266 clearTimeout ( clientObj . idleTimeout ) ;
259267 }
260268 clientObj . idleTimeout = setTimeout ( ( ) => {
261- clientObj . client . close ( ) ;
262- clients . delete ( origin ) ;
269+ closeAndDeleteClient ( origin ) ;
263270 } , IDLE_TIMEOUT ) ;
264271 } ;
272+
265273 const getClient = ( origin : string ) : http2 . ClientHttp2Session => {
266- if ( ! clients . has ( origin ) ) {
274+ if ( ! clients . has ( origin ) || clients . get ( origin ) . client . closed ) {
275+ closeAndDeleteClient ( origin ) ;
267276 const client = http2 . connect ( origin ) ;
268277 client . on ( 'error' , ( ) => {
269- client . close ( ) ;
270- clients . delete ( origin ) ;
278+ closeAndDeleteClient ( origin ) ;
271279 } ) ;
272- clients . set ( origin , { client, lastUsed : Date . now ( ) , idleTimeout : null } ) ;
280+ clients . set ( origin , { client, idleTimeout : null } ) ;
273281 resetIdleTimeout ( origin ) ;
274282 }
275283 return clients . get ( origin ) . client ;
0 commit comments