-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(core): Add progress bar to file downloads (#1038)
- Loading branch information
1 parent
a406e3a
commit 1102170
Showing
7 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export function bytesToMegabytes(bytes: number) { | ||
const megabytes = bytes / 1024 / 1024 | ||
return Math.round(megabytes * 10) / 10 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { AxiosProgressEvent } from 'axios' | ||
import ProgressBar from 'progress' | ||
import { bytesToMegabytes } from './bytesToMegabytes' | ||
|
||
export function onDownloadProgress(url: string, fileName: string) { | ||
let progressBar: ProgressBar | ||
let lastDownloadedBytes = 0 | ||
|
||
return ({ loaded: downloadedBytes, total: totalDownloadBytes }: AxiosProgressEvent) => { | ||
if (!totalDownloadBytes) { | ||
throw new Error( | ||
`[DownloadFile] Request returned total download bytes as 0. This should never happen, and it means that the target file is empty. URL: ${url}` | ||
) | ||
} | ||
|
||
if (!progressBar) { | ||
progressBar = getDownloadProgressBar(url, fileName, totalDownloadBytes) | ||
} else { | ||
const delta = downloadedBytes - lastDownloadedBytes | ||
lastDownloadedBytes = downloadedBytes | ||
progressBar.tick(delta) | ||
} | ||
} | ||
} | ||
|
||
function getDownloadProgressBar(url: string, fileName: string, totalBytes: number) { | ||
// TODO: This header should be based on the wallet config. | ||
const barHeader = url.startsWith('https://github.com/MetaMask/metamask-extension/releases/download/') | ||
? '🦊 MetaMask' | ||
: fileName | ||
|
||
const downloadSize = `${bytesToMegabytes(totalBytes)} MB` | ||
|
||
return new ProgressBar(`${barHeader} (${downloadSize}) [:bar] :percent :etas`, { | ||
width: 20, | ||
complete: '=', | ||
incomplete: ' ', | ||
total: totalBytes | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { bytesToMegabytes } from '../../src/utils/bytesToMegabytes' | ||
|
||
describe('bytesToMegabytes', () => { | ||
it('converts bytes to megabytes and rounds the result', async () => { | ||
const result = bytesToMegabytes(21_260_893) | ||
|
||
expect(result).to.equal(20.3) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.