@@ -433,7 +433,8 @@ class BulkDataClient extends events_1.EventEmitter {
433
433
const downloads = downloadJobs . map ( j => j . status ) ;
434
434
this . emit ( "allDownloadsComplete" , downloads ) ;
435
435
if ( this . options . saveManifest ) {
436
- this . writeToDestination ( "manifest.json" , stream_1 . Readable . from ( JSON . stringify ( manifest , null , 4 ) ) ) . then ( ( ) => {
436
+ const readable = stream_1 . Readable . from ( JSON . stringify ( manifest , null , 4 ) ) ;
437
+ ( 0 , promises_1 . pipeline ) ( readable , this . createDestinationStream ( "manifest.json" ) ) . then ( ( ) => {
437
438
resolve ( downloads ) ;
438
439
} ) ;
439
440
}
@@ -537,7 +538,7 @@ class BulkDataClient extends events_1.EventEmitter {
537
538
pdfToText : this . options . pdfToText ,
538
539
baseUrl : this . options . fhirUrl ,
539
540
save : ( name , stream , subFolder ) => {
540
- return this . writeToDestination ( name , stream , subFolder ) ;
541
+ return ( 0 , promises_1 . pipeline ) ( stream , this . createDestinationStream ( name , subFolder ) ) ;
541
542
} ,
542
543
} ) ;
543
544
docRefProcessor . on ( "attachment" , ( ) => _state . attachments += 1 ) ;
@@ -555,6 +556,10 @@ class BulkDataClient extends events_1.EventEmitter {
555
556
// ---------------------------------------------------------------------
556
557
// Write the file to the configured destination
557
558
// ---------------------------------------------------------------------
559
+ streams . push ( this . createDestinationStream ( fileName , subFolder ) ) ;
560
+ // ---------------------------------------------------------------------
561
+ // Run the pipeline
562
+ // ---------------------------------------------------------------------
558
563
try {
559
564
await ( 0 , promises_1 . pipeline ) ( streams ) ;
560
565
}
@@ -572,27 +577,21 @@ class BulkDataClient extends events_1.EventEmitter {
572
577
fileSize : _state . uncompressedBytes ,
573
578
resourceCount : _state . resources
574
579
} ) ;
575
- await this . writeToDestination ( fileName , stringify , subFolder ) ;
576
580
}
577
581
/**
578
- * Given a readable stream as input sends the data to the destination. The
579
- * actual actions taken are different depending on the destination:
582
+ * Creates and returns a writable stream to the destination.
580
583
* - For file system destination the files are written to the given location
581
584
* - For S3 destinations the files are uploaded to S3
582
585
* - For HTTP destinations the files are posted to the given URL
583
586
* - If the destination is "" or "none" no action is taken (files are discarded)
584
587
* @param fileName The desired fileName at destination
585
- * @param inputStream The input readable stream
586
- * @param subFolder
587
- * @returns
588
+ * @param subFolder Optional subfolder
588
589
*/
589
- writeToDestination ( fileName , inputStream , subFolder = "" ) {
590
+ createDestinationStream ( fileName , subFolder = "" ) {
590
591
const destination = String ( this . options . destination || "none" ) . trim ( ) ;
591
592
// No destination ------------------------------------------------------
592
593
if ( ! destination || destination . toLowerCase ( ) == "none" ) {
593
- return ( 0 , promises_1 . pipeline ) ( inputStream , new stream_1 . Writable ( {
594
- write ( chunk , encoding , cb ) { cb ( ) ; }
595
- } ) ) ;
594
+ return new stream_1 . Writable ( { write ( chunk , encoding , cb ) { cb ( ) ; } } ) ;
596
595
}
597
596
// S3 ------------------------------------------------------------------
598
597
if ( destination . startsWith ( "s3://" ) ) {
@@ -609,18 +608,20 @@ class BulkDataClient extends events_1.EventEmitter {
609
608
if ( subFolder ) {
610
609
bucket = ( 0 , path_1 . join ) ( bucket , subFolder ) ;
611
610
}
611
+ const stream = new stream_1 . PassThrough ( ) ;
612
612
const upload = new aws_sdk_1 . default . S3 . ManagedUpload ( {
613
613
params : {
614
614
Bucket : bucket ,
615
615
Key : fileName ,
616
- Body : inputStream
616
+ Body : stream
617
617
}
618
618
} ) ;
619
- return upload . promise ( ) ;
619
+ upload . promise ( ) . catch ( console . error ) ;
620
+ return stream ;
620
621
}
621
622
// HTTP ----------------------------------------------------------------
622
623
if ( destination . match ( / ^ h t t p s ? \: \/ \/ / ) ) {
623
- return ( 0 , promises_1 . pipeline ) ( inputStream , request_1 . default . stream . post ( ( 0 , path_1 . join ) ( destination , fileName ) + "?folder=" + subFolder ) , new stream_1 . PassThrough ( ) ) ;
624
+ return request_1 . default . stream . post ( ( 0 , path_1 . join ) ( destination , fileName ) + "?folder=" + subFolder ) ;
624
625
}
625
626
// local filesystem destinations ---------------------------------------
626
627
let path = destination . startsWith ( "file://" ) ?
@@ -636,7 +637,7 @@ class BulkDataClient extends events_1.EventEmitter {
636
637
( 0 , fs_1 . mkdirSync ) ( path ) ;
637
638
}
638
639
}
639
- return ( 0 , promises_1 . pipeline ) ( inputStream , fs_1 . default . createWriteStream ( ( 0 , path_1 . join ) ( path , fileName ) ) ) ;
640
+ return fs_1 . default . createWriteStream ( ( 0 , path_1 . join ) ( path , fileName ) ) ;
640
641
}
641
642
/**
642
643
* Given an URL query as URLSearchParams object, appends all the
0 commit comments