@@ -2,7 +2,7 @@ import http from "http";
22import  https  from  "https" ; 
33import  {  gunzipSync  }  from  "zlib" ; 
44
5- import  {  assert  }  from  "./errors.js" ; 
5+ import  {  assert ,   makeError  }  from  "./errors.js" ; 
66import  {  getBytes  }  from  "./data.js" ; 
77
88import  type  { 
@@ -15,6 +15,8 @@ import type {
1515export  function  createGetUrl ( options ?: Record < string ,  any > ) : FetchGetUrlFunc  { 
1616
1717    async  function  getUrl ( req : FetchRequest ,  signal ?: FetchCancelSignal ) : Promise < GetUrlResponse >  { 
18+         // Make sure we weren't cancelled before sending 
19+         assert ( signal  ==  null  ||  ! signal . cancelled ,  "request cancelled before sending" ,  "CANCELLED" ) ; 
1820
1921        const  protocol  =  req . url . split ( ":" ) [ 0 ] . toLowerCase ( ) ; 
2022
@@ -35,6 +37,13 @@ export function createGetUrl(options?: Record<string, any>): FetchGetUrlFunc {
3537            if  ( options . agent )  {  reqOptions . agent  =  options . agent ;  } 
3638        } 
3739
40+         // Create a Node-specific AbortController, if available 
41+         let  abort : null  |  AbortController  =  null ; 
42+         try  { 
43+             abort  =  new  AbortController ( ) ; 
44+             reqOptions . abort  =  abort . signal ; 
45+         }  catch  ( e )  {  console . log ( e ) ;  } 
46+ 
3847        const  request  =  ( ( protocol  ===  "http" )  ? http : https ) . request ( req . url ,  reqOptions ) ; 
3948
4049        request . setTimeout ( req . timeout ) ; 
@@ -45,8 +54,17 @@ export function createGetUrl(options?: Record<string, any>): FetchGetUrlFunc {
4554        request . end ( ) ; 
4655
4756        return  new  Promise ( ( resolve ,  reject )  =>  { 
48-             // @TODO : Node 15 added AbortSignal; once we drop support for 
49-             // Node14, we can add that in here too 
57+ 
58+             if  ( signal )  { 
59+                 signal . addListener ( ( )  =>  { 
60+                     if  ( abort )  {  abort . abort ( ) ;  } 
61+                     reject ( makeError ( "request cancelled" ,  "CANCELLED" ) ) ; 
62+                 } ) ; 
63+             } 
64+ 
65+             request . on ( "timeout" ,  ( )  =>  { 
66+                 reject ( makeError ( "request timeout" ,  "TIMEOUT" ) ) ; 
67+             } ) ; 
5068
5169            request . once ( "response" ,  ( resp : http . IncomingMessage )  =>  { 
5270                const  statusCode  =  resp . statusCode  ||  0 ; 
0 commit comments