@@ -417,37 +417,32 @@ export class PropagationServer {
417
417
const config : AxiosRequestConfig = {
418
418
responseType : "arraybuffer" , // To store the file content in memory
419
419
httpsAgent : this . createHttpsAgent ( ) ,
420
- } ;
421
-
422
- const url = `https://${ this . ipAddress } :${ PropagationServer . port } /fetch/${ this . storeId } /${ dataPath } ` ;
423
-
424
- try {
425
- const response = await axios . get ( url , config ) ;
426
- const totalLength = parseInt ( response . headers [ "content-length" ] , 10 ) ;
427
-
428
- console . log ( cyan ( `Starting fetch for ${ dataPath } ...` ) ) ;
429
-
430
- // Create a progress bar for the download
431
- const progressBar = PropagationServer . multiBar . create ( totalLength , 0 , {
432
- dataPath : yellow ( dataPath ) ,
433
- percentage : 0 ,
434
- } ) ;
435
-
436
- let downloadedBytes = 0 ;
420
+ onDownloadProgress : ( progressEvent ) => {
421
+ const totalLength = progressEvent . total || 0 ;
422
+ const downloadedBytes = progressEvent . loaded ;
423
+
424
+ // Update progress bar
425
+ const progressBar = PropagationServer . multiBar . create ( totalLength , 0 , {
426
+ dataPath : yellow ( dataPath ) ,
427
+ percentage : 0 ,
428
+ } ) ;
437
429
438
- // Track progress of downloading the file
439
- response . data . on ( "data" , ( chunk : Buffer ) => {
440
- downloadedBytes += chunk . length ;
441
430
progressBar . update ( downloadedBytes , {
442
431
percentage : Math . round ( ( downloadedBytes / totalLength ) * 100 ) ,
443
432
} ) ;
444
- } ) ;
445
433
446
- // Complete the progress bar once done
447
- progressBar . update ( totalLength , { percentage : 100 } ) ;
448
- progressBar . stop ( ) ;
434
+ if ( downloadedBytes === totalLength ) {
435
+ progressBar . update ( totalLength , { percentage : 100 } ) ;
436
+ progressBar . stop ( ) ;
437
+ console . log ( green ( `✔ File ${ dataPath } fetched successfully.` ) ) ;
438
+ }
439
+ } ,
440
+ } ;
449
441
450
- console . log ( green ( `✔ File ${ dataPath } fetched successfully.` ) ) ;
442
+ const url = `https://${ this . ipAddress } :${ PropagationServer . port } /fetch/${ this . storeId } /${ dataPath } ` ;
443
+
444
+ try {
445
+ const response = await axios . get ( url , config ) ;
451
446
452
447
// Return the file contents as a Buffer
453
448
return Buffer . from ( response . data ) ;
@@ -581,7 +576,8 @@ export class PropagationServer {
581
576
await propagationServer . initializeWallet ( ) ;
582
577
583
578
// Check if the store exists
584
- const { storeExists, rootHashExists} = await propagationServer . checkStoreExists ( rootHash ) ;
579
+ const { storeExists, rootHashExists } =
580
+ await propagationServer . checkStoreExists ( rootHash ) ;
585
581
if ( ! storeExists || ! rootHashExists ) {
586
582
throw new Error ( `Store ${ storeId } does not exist.` ) ;
587
583
}
0 commit comments