1
- import type { Response } from "node-fetch" ;
2
- import fetch , { AbortError } from "node-fetch" ;
3
1
import type { InputBoxOptions } from "vscode" ;
4
2
import { Uri , window } from "vscode" ;
5
3
import type { CodeQLCliServer } from "../codeql-cli/cli" ;
@@ -536,8 +534,8 @@ export class DatabaseFetcher {
536
534
} catch ( e ) {
537
535
disposeTimeout ( ) ;
538
536
539
- if ( e instanceof AbortError ) {
540
- const thrownError = new AbortError ( "The request timed out." ) ;
537
+ if ( e instanceof DOMException && e . name === " AbortError" ) {
538
+ const thrownError = new Error ( "The request timed out." ) ;
541
539
thrownError . stack = e . stack ;
542
540
throw thrownError ;
543
541
}
@@ -556,25 +554,50 @@ export class DatabaseFetcher {
556
554
const totalNumBytes = contentLength
557
555
? parseInt ( contentLength , 10 )
558
556
: undefined ;
559
- reportStreamProgress ( body , "Downloading database" , totalNumBytes , progress ) ;
560
557
561
- body . on ( "data" , onData ) ;
558
+ const reportProgress = reportStreamProgress (
559
+ "Downloading database" ,
560
+ totalNumBytes ,
561
+ progress ,
562
+ ) ;
562
563
563
564
try {
564
- await new Promise ( ( resolve , reject ) => {
565
- body . pipe ( archiveFileStream ) . on ( "finish" , resolve ) . on ( "error" , reject ) ;
565
+ const reader = body . getReader ( ) ;
566
+ for ( ; ; ) {
567
+ const { done, value } = await reader . read ( ) ;
568
+ if ( done ) {
569
+ break ;
570
+ }
571
+
572
+ onData ( ) ;
573
+ reportProgress ( value ?. length ?? 0 ) ;
574
+
575
+ await new Promise ( ( resolve , reject ) => {
576
+ archiveFileStream . write ( value , ( err ) => {
577
+ if ( err ) {
578
+ reject ( err ) ;
579
+ }
580
+ resolve ( undefined ) ;
581
+ } ) ;
582
+ } ) ;
583
+ }
566
584
567
- // If an error occurs on the body, we also want to reject the promise (e.g. during a timeout error).
568
- body . on ( "error" , reject ) ;
585
+ await new Promise ( ( resolve , reject ) => {
586
+ archiveFileStream . close ( ( err ) => {
587
+ if ( err ) {
588
+ reject ( err ) ;
589
+ }
590
+ resolve ( undefined ) ;
591
+ } ) ;
569
592
} ) ;
570
593
} catch ( e ) {
571
594
// Close and remove the file if an error occurs
572
595
archiveFileStream . close ( ( ) => {
573
596
void remove ( archivePath ) ;
574
597
} ) ;
575
598
576
- if ( e instanceof AbortError ) {
577
- const thrownError = new AbortError ( "The download timed out." ) ;
599
+ if ( e instanceof DOMException && e . name === " AbortError" ) {
600
+ const thrownError = new Error ( "The download timed out." ) ;
578
601
thrownError . stack = e . stack ;
579
602
throw thrownError ;
580
603
}
0 commit comments