@@ -337,30 +337,31 @@ export class RequestBuilder implements PromiseLike<RequestResponse> {
337
337
}
338
338
}
339
339
340
- interface Timeout {
341
- signal : AbortSignal ;
342
- clear ( ) : void ;
340
+ interface RequestAbortController {
341
+ controller : AbortController ;
342
+ /** Clears the timeout that may be set if there's a delay */
343
+ clearTimeout ( ) : void ;
343
344
}
344
345
345
346
/** Response of making a request where the body can be read. */
346
347
export class RequestResponse {
347
348
#response: Response ;
348
349
#downloadResponse: Response ;
349
350
#originalUrl: string ;
350
- #timeout: Timeout | undefined ;
351
+ #abortController: RequestAbortController ;
351
352
352
353
/** @internal */
353
354
constructor ( opts : {
354
355
response : Response ;
355
356
originalUrl : string ;
356
357
progressBar : ProgressBar | undefined ;
357
- timeout : Timeout | undefined ;
358
+ abortController : RequestAbortController ;
358
359
} ) {
359
360
this . #originalUrl = opts . originalUrl ;
360
361
this . #response = opts . response ;
361
- this . #timeout = opts . timeout ;
362
+ this . #abortController = opts . abortController ;
362
363
if ( opts . response . body == null ) {
363
- this . #timeout ?. clear ( ) ;
364
+ opts . abortController . clearTimeout ( ) ;
364
365
}
365
366
366
367
if ( opts . progressBar != null ) {
@@ -381,8 +382,9 @@ export class RequestResponse {
381
382
pb . increment ( value . byteLength ) ;
382
383
controller . enqueue ( value ) ;
383
384
}
384
- if ( opts . timeout ?. signal ?. aborted ) {
385
- controller . error ( opts . timeout . signal . reason ) ;
385
+ const signal = opts . abortController . controller . signal ;
386
+ if ( signal . aborted ) {
387
+ controller . error ( signal . reason ) ;
386
388
} else {
387
389
controller . close ( ) ;
388
390
}
@@ -418,9 +420,10 @@ export class RequestResponse {
418
420
return this . #response. redirected ;
419
421
}
420
422
421
- /** The underlying `AbortSignal` if a delay or signal was specified. */
422
- get signal ( ) : AbortSignal | undefined {
423
- return this . #timeout?. signal ;
423
+ /** The underlying `AbortSignal` used to abort the request body
424
+ * when a timeout is reached or when the `.abort()` method is called. */
425
+ get signal ( ) : AbortSignal {
426
+ return this . #abortController. controller . signal ;
424
427
}
425
428
426
429
/** Status code of the response. */
@@ -438,6 +441,11 @@ export class RequestResponse {
438
441
return this . #response. url ;
439
442
}
440
443
444
+ /** Aborts */
445
+ abort ( reason ?: unknown ) {
446
+ this . #abortController?. controller . abort ( reason ) ;
447
+ }
448
+
441
449
/**
442
450
* Throws if the response doesn't have a 2xx code.
443
451
*
@@ -466,7 +474,7 @@ export class RequestResponse {
466
474
}
467
475
return this . #downloadResponse. arrayBuffer ( ) ;
468
476
} finally {
469
- this . #timeout ?. clear ( ) ;
477
+ this . #abortController ?. clearTimeout ( ) ;
470
478
}
471
479
}
472
480
@@ -483,7 +491,7 @@ export class RequestResponse {
483
491
}
484
492
return await this . #downloadResponse. blob ( ) ;
485
493
} finally {
486
- this . #timeout ?. clear ( ) ;
494
+ this . #abortController ?. clearTimeout ( ) ;
487
495
}
488
496
}
489
497
@@ -500,7 +508,7 @@ export class RequestResponse {
500
508
}
501
509
return await this . #downloadResponse. formData ( ) ;
502
510
} finally {
503
- this . #timeout ?. clear ( ) ;
511
+ this . #abortController ?. clearTimeout ( ) ;
504
512
}
505
513
}
506
514
@@ -517,7 +525,7 @@ export class RequestResponse {
517
525
}
518
526
return await this . #downloadResponse. json ( ) ;
519
527
} finally {
520
- this . #timeout ?. clear ( ) ;
528
+ this . #abortController ?. clearTimeout ( ) ;
521
529
}
522
530
}
523
531
@@ -537,7 +545,7 @@ export class RequestResponse {
537
545
}
538
546
return await this . #downloadResponse. text ( ) ;
539
547
} finally {
540
- this . #timeout ?. clear ( ) ;
548
+ this . #abortController ?. clearTimeout ( ) ;
541
549
}
542
550
}
543
551
@@ -546,7 +554,7 @@ export class RequestResponse {
546
554
try {
547
555
await this . readable . pipeTo ( dest , options ) ;
548
556
} finally {
549
- this . #timeout ?. clear ( ) ;
557
+ this . #abortController ?. clearTimeout ( ) ;
550
558
}
551
559
}
552
560
@@ -602,7 +610,7 @@ export class RequestResponse {
602
610
} catch {
603
611
// do nothing
604
612
}
605
- this . #timeout ?. clear ( ) ;
613
+ this . #abortController ?. clearTimeout ( ) ;
606
614
}
607
615
} catch ( err ) {
608
616
await this . #response. body ?. cancel ( ) ;
@@ -633,7 +641,12 @@ export async function makeRequest(state: RequestBuilderState) {
633
641
if ( state . url == null ) {
634
642
throw new Error ( "You must specify a URL before fetching." ) ;
635
643
}
636
- const timeout = getTimeout ( ) ;
644
+ const abortController = getTimeoutAbortController ( ) ?? {
645
+ controller : new AbortController ( ) ,
646
+ clearTimeout ( ) {
647
+ // do nothing
648
+ } ,
649
+ } ;
637
650
const response = await fetch ( state . url , {
638
651
body : state . body ,
639
652
cache : state . cache ,
@@ -645,13 +658,13 @@ export async function makeRequest(state: RequestBuilderState) {
645
658
redirect : state . redirect ,
646
659
referrer : state . referrer ,
647
660
referrerPolicy : state . referrerPolicy ,
648
- signal : timeout ? .signal ,
661
+ signal : abortController . controller . signal ,
649
662
} ) ;
650
663
const result = new RequestResponse ( {
651
664
response,
652
665
originalUrl : state . url . toString ( ) ,
653
666
progressBar : getProgressBar ( ) ,
654
- timeout ,
667
+ abortController ,
655
668
} ) ;
656
669
if ( ! state . noThrow ) {
657
670
result . throwIfNotOk ( ) ;
@@ -681,7 +694,7 @@ export async function makeRequest(state: RequestBuilderState) {
681
694
}
682
695
}
683
696
684
- function getTimeout ( ) : Timeout | undefined {
697
+ function getTimeoutAbortController ( ) : RequestAbortController | undefined {
685
698
if ( state . timeout == null ) {
686
699
return undefined ;
687
700
}
@@ -692,8 +705,8 @@ export async function makeRequest(state: RequestBuilderState) {
692
705
timeout ,
693
706
) ;
694
707
return {
695
- signal : controller . signal ,
696
- clear ( ) {
708
+ controller,
709
+ clearTimeout ( ) {
697
710
clearTimeout ( timeoutId ) ;
698
711
} ,
699
712
} ;
0 commit comments