@@ -251,9 +251,8 @@ export default class Request extends Duplex implements RequestEvents<Request> {
251
251
this . destroy ( new AbortError ( this ) ) ;
252
252
}
253
253
254
- this . options . signal ?. addEventListener ( 'abort' , ( ) => {
255
- this . destroy ( new AbortError ( this ) ) ;
256
- } ) ;
254
+ this . abortHandler = this . abortHandler . bind ( this ) ;
255
+ this . options . signal ?. addEventListener ( 'abort' , this . abortHandler , { once : true } ) ;
257
256
258
257
// Important! If you replace `body` in a handler with another stream, make sure it's readable first.
259
258
// The below is run only once.
@@ -545,6 +544,10 @@ export default class Request extends Duplex implements RequestEvents<Request> {
545
544
return this ;
546
545
}
547
546
547
+ private abortHandler ( ) : void {
548
+ this . destroy ( new AbortError ( this ) ) ;
549
+ }
550
+
548
551
private async _finalizeBody ( ) : Promise < void > {
549
552
const { options} = this ;
550
553
const { headers} = options ;
@@ -664,6 +667,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
664
667
response . once ( 'end' , ( ) => {
665
668
this . _responseSize = this . _downloadedSize ;
666
669
this . emit ( 'downloadProgress' , this . downloadProgress ) ;
670
+ this . removeListener ( 'abort' , this . abortHandler ) ;
667
671
} ) ;
668
672
669
673
response . once ( 'error' , ( error : Error ) => {
@@ -674,6 +678,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
674
678
response . destroy ( ) ;
675
679
676
680
this . _beforeError ( new ReadError ( error , this ) ) ;
681
+ this . removeListener ( 'abort' , this . abortHandler ) ;
677
682
} ) ;
678
683
679
684
response . once ( 'aborted' , ( ) => {
@@ -684,6 +689,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
684
689
message : 'The server aborted pending request' ,
685
690
code : 'ECONNRESET' ,
686
691
} , this ) ) ;
692
+ this . removeListener ( 'abort' , this . abortHandler ) ;
687
693
} ) ;
688
694
689
695
this . emit ( 'downloadProgress' , this . downloadProgress ) ;
0 commit comments