@@ -674,12 +674,15 @@ class BulkDataClient extends EventEmitter
674
674
// Just "remember" the progress values but don't emit anything yet
675
675
download . on ( "progress" , state => Object . assign ( _state , state ) )
676
676
677
+ const streams : ( NodeJS . ReadableStream | NodeJS . WritableStream | NodeJS . ReadWriteStream ) [ ] = [ ] ;
678
+
677
679
// Start the download (the stream will be paused though)
678
- let processPipeline : Readable = await download . run ( {
680
+ let downloadStream : Readable = await download . run ( {
679
681
accessToken,
680
682
signal : this . abortController . signal ,
681
683
requestOptions : this . options . requests
682
- } ) . catch ( e => {
684
+ } )
685
+ . catch ( e => {
683
686
if ( e instanceof FileDownloadError ) {
684
687
this . emit ( "downloadError" , {
685
688
body : null , // Buffer
@@ -691,6 +694,8 @@ class BulkDataClient extends EventEmitter
691
694
throw e
692
695
} ) ;
693
696
697
+ streams . push ( downloadStream )
698
+
694
699
// ---------------------------------------------------------------------
695
700
// Create an NDJSON parser to verify that every single line is valid
696
701
// ---------------------------------------------------------------------
@@ -709,9 +714,8 @@ class BulkDataClient extends EventEmitter
709
714
expectedCount : exportType == "output" ? file . count || - 1 : - 1 ,
710
715
expectedResourceType
711
716
} )
712
-
713
- processPipeline = processPipeline . pipe ( parser ) ;
714
-
717
+
718
+ streams . push ( parser )
715
719
716
720
// ---------------------------------------------------------------------
717
721
// Download attachments
@@ -745,7 +749,7 @@ class BulkDataClient extends EventEmitter
745
749
746
750
docRefProcessor . on ( "attachment" , ( ) => _state . attachments ! += 1 )
747
751
748
- processPipeline = processPipeline . pipe ( docRefProcessor )
752
+ streams . push ( docRefProcessor )
749
753
}
750
754
751
755
@@ -757,19 +761,33 @@ class BulkDataClient extends EventEmitter
757
761
_state . resources ! += 1
758
762
onProgress ( _state )
759
763
} ) ;
760
- processPipeline = processPipeline . pipe ( stringify ) ;
764
+ streams . push ( stringify )
761
765
762
766
763
767
// ---------------------------------------------------------------------
764
768
// Write the file to the configured destination
765
769
// ---------------------------------------------------------------------
766
- await this . writeToDestination ( fileName , processPipeline , subFolder )
770
+ try {
771
+ await pipeline ( streams )
772
+
773
+ } catch ( e : any ) {
774
+ this . emit ( "downloadError" , {
775
+ body : null ,
776
+ code : e . code || null ,
777
+ fileUrl : e . fileUrl || file . url ,
778
+ message : String ( e . message || "Downloading failed" )
779
+ } )
767
780
781
+ throw e
782
+ }
783
+
768
784
this . emit ( "downloadComplete" , {
769
785
fileUrl : file . url ,
770
786
fileSize : _state . uncompressedBytes ,
771
787
resourceCount : _state . resources !
772
788
} )
789
+
790
+ await this . writeToDestination ( fileName , stringify , subFolder )
773
791
}
774
792
775
793
/**
0 commit comments