@@ -575,7 +575,7 @@ export class Morphik {
575575 controller : AbortController ,
576576 ) : Promise < Response > {
577577 const { signal, method, ...options } = init || { } ;
578- const abort = controller . abort . bind ( controller ) ;
578+ const abort = this . _makeAbort ( controller ) ;
579579 if ( signal ) signal . addEventListener ( 'abort' , abort , { once : true } ) ;
580580
581581 const timeout = setTimeout ( abort , ms ) ;
@@ -601,6 +601,7 @@ export class Morphik {
601601 return await this . fetch . call ( undefined , url , fetchOptions ) ;
602602 } finally {
603603 clearTimeout ( timeout ) ;
604+ if ( signal ) signal . removeEventListener ( 'abort' , abort ) ;
604605 }
605606 }
606607
@@ -745,6 +746,12 @@ export class Morphik {
745746 return headers . values ;
746747 }
747748
749+ private _makeAbort ( controller : AbortController ) {
750+ // note: we can't just inline this method inside `fetchWithTimeout()` because then the closure
751+ // would capture all request options, and cause a memory leak.
752+ return ( ) => controller . abort ( ) ;
753+ }
754+
748755 private buildBody ( { options : { body, headers : rawHeaders } } : { options : FinalRequestOptions } ) : {
749756 bodyHeaders : HeadersLike ;
750757 body : BodyInit | undefined ;
0 commit comments