diff options
author | Veetaha <[email protected]> | 2020-02-08 19:03:27 +0000 |
---|---|---|
committer | Veetaha <[email protected]> | 2020-02-08 19:03:27 +0000 |
commit | 4e85254444cfaf34fefc253ecd0b43b786e31dd8 (patch) | |
tree | 0862443f6b1df3811184ba546b0b32c3404584f9 /editors/code/src/installation/download_file.ts | |
parent | 6ef912f9259a78495fdba6a37bef7d78c4e0a4fd (diff) |
vscode: add docs to installation module interfaces and sanity check to donloadFile()
Diffstat (limited to 'editors/code/src/installation/download_file.ts')
-rw-r--r-- | editors/code/src/installation/download_file.ts | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts index 7b537e114..0cc5fc0cb 100644 --- a/editors/code/src/installation/download_file.ts +++ b/editors/code/src/installation/download_file.ts | |||
@@ -1,17 +1,25 @@ | |||
1 | import fetch from "node-fetch"; | 1 | import fetch from "node-fetch"; |
2 | import { throttle } from "throttle-debounce"; | 2 | import { throttle } from "throttle-debounce"; |
3 | import * as fs from "fs"; | 3 | import * as fs from "fs"; |
4 | import { strict as assert } from "assert"; | ||
4 | 5 | ||
6 | /** | ||
7 | * Downloads file from `url` and stores it at `destFilePath`. | ||
8 | * `onProgress` callback is periodically called to track the progress of downloading, | ||
9 | * it gets the already read and total amount of bytes to read as its parameters. | ||
10 | */ | ||
5 | export async function downloadFile( | 11 | export async function downloadFile( |
6 | url: string, | 12 | url: string, |
7 | destFilePath: fs.PathLike, | 13 | destFilePath: fs.PathLike, |
8 | onProgress: (readBytes: number, totalBytes: number) => void | 14 | onProgress: (readBytes: number, totalBytes: number) => void |
9 | ): Promise<void> { | 15 | ): Promise<void> { |
10 | onProgress = throttle(1000, /* noTrailing: */ true, onProgress); | 16 | onProgress = throttle(500, /* noTrailing: */ true, onProgress); |
11 | 17 | ||
12 | const response = await fetch(url); | 18 | const response = await fetch(url); |
13 | 19 | ||
14 | const totalBytes = Number(response.headers.get('content-length')); | 20 | const totalBytes = Number(response.headers.get('content-length')); |
21 | assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol"); | ||
22 | |||
15 | let readBytes = 0; | 23 | let readBytes = 0; |
16 | 24 | ||
17 | return new Promise<void>((resolve, reject) => response.body | 25 | return new Promise<void>((resolve, reject) => response.body |