Skip to content

Commit

Permalink
Add event listener to data before piping, add percentage to logs
Browse files Browse the repository at this point in the history
  • Loading branch information
philmcmahon committed Jan 9, 2025
1 parent ed1ff8e commit 95867c1
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/backend-common/src/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,16 @@ const downloadS3Data = async (
key: string,
contentLength?: number,
) => {
const stream = data.pipe(createWriteStream(destinationPath));
let downloadedBytes = 0;
data.on('data', (chunk) => {
downloadedBytes += chunk.length;
logger.info(
`Downloaded ${downloadedBytes} of ${contentLength} bytes so far for ${key}`,
);
if (downloadedBytes > 0 && contentLength && contentLength > 0) {
logger.info(
`Downloaded ${downloadedBytes} of ${contentLength} bytes so far ${contentLength ? `(${Math.floor(downloadedBytes / contentLength)}%)` : ''} for ${key}`,
);
}
});
const stream = data.pipe(createWriteStream(destinationPath));
await new Promise<void>((resolve, reject) => {
stream
.on('finish', () => {
Expand Down

0 comments on commit 95867c1

Please sign in to comment.