@@ -670,17 +670,7 @@ export class Filesystem {
670
670
gzipOptions : { level : 9 } ,
671
671
} ) ;
672
672
673
- // Use a more direct approach with streams
674
- const bufferChunks : Buffer [ ] = [ ] ;
675
-
676
- // Set up promise to track completion
677
- const archivePromise = new Promise < Buffer > ( ( resolve , reject ) => {
678
- archive . on ( "data" , ( chunk : Buffer ) => bufferChunks . push ( chunk ) ) ;
679
- archive . on ( "end" , ( ) => resolve ( Buffer . concat ( bufferChunks ) ) ) ;
680
- archive . on ( "error" , ( err : Error ) => reject ( err ) ) ;
681
- } ) ;
682
-
683
- // Recursively add files to archive
673
+ // Recursively add files to archive first
684
674
const addDirectory = async ( dir : string ) => {
685
675
const entries = await fs . readdir ( dir , { withFileTypes : true } ) ;
686
676
@@ -715,25 +705,45 @@ export class Filesystem {
715
705
// Process files
716
706
await addDirectory ( this . workDir ) ;
717
707
718
- // Finalize archive
719
- archive . finalize ( ) ;
720
-
708
+ // Handle saving to file and getting buffer
721
709
if ( saveAs ) {
710
+ // Save to file
722
711
const fullSavePath = this . resolvePath ( saveAs ) ;
723
712
const writeStream = fsSync . createWriteStream ( fullSavePath ) ;
713
+
724
714
archive . pipe ( writeStream ) ;
715
+ archive . finalize ( ) ;
716
+
725
717
await new Promise < void > ( ( resolve , reject ) => {
726
718
writeStream . on ( "finish" , ( ) => resolve ( ) ) ;
727
719
writeStream . on ( "error" , ( error ) => reject ( error ) ) ;
720
+ archive . on ( "error" , ( error ) => reject ( error ) ) ;
728
721
} ) ;
729
- }
730
722
731
- // Wait for archive to complete and return result
732
- const buffer = await archivePromise ;
733
- return {
734
- success : true ,
735
- data : { buffer } ,
736
- } ;
723
+ // Read the file back as buffer
724
+ const buffer = await fs . readFile ( fullSavePath ) ;
725
+ return {
726
+ success : true ,
727
+ data : { buffer } ,
728
+ } ;
729
+ } else {
730
+ // Just get buffer without saving to file
731
+ const bufferChunks : Buffer [ ] = [ ] ;
732
+
733
+ const archivePromise = new Promise < Buffer > ( ( resolve , reject ) => {
734
+ archive . on ( "data" , ( chunk : Buffer ) => bufferChunks . push ( chunk ) ) ;
735
+ archive . on ( "end" , ( ) => resolve ( Buffer . concat ( bufferChunks ) ) ) ;
736
+ archive . on ( "error" , ( err : Error ) => reject ( err ) ) ;
737
+ } ) ;
738
+
739
+ archive . finalize ( ) ;
740
+
741
+ const buffer = await archivePromise ;
742
+ return {
743
+ success : true ,
744
+ data : { buffer } ,
745
+ } ;
746
+ }
737
747
} catch ( error ) {
738
748
return {
739
749
success : false ,
0 commit comments