@@ -176,6 +176,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
176
176
private _triggerRead : boolean ;
177
177
declare private _jobs : Array < ( ) => void > ;
178
178
private _cancelTimeouts : ( ) => void ;
179
+ private readonly _removeListeners : ( ) => void ;
179
180
private _nativeResponse ?: IncomingMessageWithTimings ;
180
181
private _flushed : boolean ;
181
182
private _aborted : boolean ;
@@ -199,6 +200,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
199
200
this . _unproxyEvents = noop ;
200
201
this . _triggerRead = false ;
201
202
this . _cancelTimeouts = noop ;
203
+ this . _removeListeners = noop ;
202
204
this . _jobs = [ ] ;
203
205
this . _flushed = false ;
204
206
this . _requestInitialized = false ;
@@ -247,13 +249,20 @@ export default class Request extends Duplex implements RequestEvents<Request> {
247
249
return ;
248
250
}
249
251
250
- if ( this . options . signal ?. aborted ) {
251
- this . destroy ( new AbortError ( this ) ) ;
252
- }
252
+ if ( this . options . signal ) {
253
+ const abort = ( ) => {
254
+ this . destroy ( new AbortError ( this ) ) ;
255
+ } ;
253
256
254
- this . options . signal ?. addEventListener ( 'abort' , ( ) => {
255
- this . destroy ( new AbortError ( this ) ) ;
256
- } ) ;
257
+ if ( this . options . signal . aborted ) {
258
+ abort ( ) ;
259
+ }
260
+
261
+ this . options . signal . addEventListener ( 'abort' , abort ) ;
262
+ this . _removeListeners = ( ) => {
263
+ this . options . signal . removeEventListener ( 'abort' , abort ) ;
264
+ } ;
265
+ }
257
266
258
267
// Important! If you replace `body` in a handler with another stream, make sure it's readable first.
259
268
// The below is run only once.
@@ -508,6 +517,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
508
517
// Prevent further retries
509
518
this . _stopRetry ( ) ;
510
519
this . _cancelTimeouts ( ) ;
520
+ this . _removeListeners ( ) ;
511
521
512
522
if ( this . options ) {
513
523
const { body} = this . options ;
0 commit comments