1- import type { Response } from "node-fetch" ;
2- import fetch , { AbortError } from "node-fetch" ;
31import type { InputBoxOptions } from "vscode" ;
42import { Uri , window } from "vscode" ;
53import type { CodeQLCliServer } from "../codeql-cli/cli" ;
@@ -536,8 +534,8 @@ export class DatabaseFetcher {
536534 } catch ( e ) {
537535 disposeTimeout ( ) ;
538536
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." ) ;
541539 thrownError . stack = e . stack ;
542540 throw thrownError ;
543541 }
@@ -556,25 +554,50 @@ export class DatabaseFetcher {
556554 const totalNumBytes = contentLength
557555 ? parseInt ( contentLength , 10 )
558556 : undefined ;
559- reportStreamProgress ( body , "Downloading database" , totalNumBytes , progress ) ;
560557
561- body . on ( "data" , onData ) ;
558+ const reportProgress = reportStreamProgress (
559+ "Downloading database" ,
560+ totalNumBytes ,
561+ progress ,
562+ ) ;
562563
563564 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+ }
566584
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+ } ) ;
569592 } ) ;
570593 } catch ( e ) {
571594 // Close and remove the file if an error occurs
572595 archiveFileStream . close ( ( ) => {
573596 void remove ( archivePath ) ;
574597 } ) ;
575598
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." ) ;
578601 thrownError . stack = e . stack ;
579602 throw thrownError ;
580603 }
0 commit comments