Skip to content

Commit 305782f

Browse files
fix: bad propagation server constructor
1 parent 1c6fe0c commit 305782f

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

src/DigNetwork/PropagationServer.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -417,37 +417,32 @@ export class PropagationServer {
417417
const config: AxiosRequestConfig = {
418418
responseType: "arraybuffer", // To store the file content in memory
419419
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+
});
437429

438-
// Track progress of downloading the file
439-
response.data.on("data", (chunk: Buffer) => {
440-
downloadedBytes += chunk.length;
441430
progressBar.update(downloadedBytes, {
442431
percentage: Math.round((downloadedBytes / totalLength) * 100),
443432
});
444-
});
445433

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+
};
449441

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);
451446

452447
// Return the file contents as a Buffer
453448
return Buffer.from(response.data);
@@ -581,7 +576,8 @@ export class PropagationServer {
581576
await propagationServer.initializeWallet();
582577

583578
// Check if the store exists
584-
const { storeExists, rootHashExists} = await propagationServer.checkStoreExists(rootHash);
579+
const { storeExists, rootHashExists } =
580+
await propagationServer.checkStoreExists(rootHash);
585581
if (!storeExists || !rootHashExists) {
586582
throw new Error(`Store ${storeId} does not exist.`);
587583
}

0 commit comments

Comments
 (0)