aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/installation/download_file.ts
diff options
context:
space:
mode:
authorVeetaha <[email protected]>2020-02-08 02:22:44 +0000
committerVeetaha <[email protected]>2020-02-08 02:34:11 +0000
commit5d88c1db38200896d2e4af7836fec95097adf509 (patch)
treee865f9b5446b1630acf554f353856334a8ef6007 /editors/code/src/installation/download_file.ts
parent3e0e4e90aeeff25db674f8db562c611bd8016482 (diff)
vscode: amended config to use binary from globalStoragePath, added ui for downloading
Diffstat (limited to 'editors/code/src/installation/download_file.ts')
-rw-r--r--editors/code/src/installation/download_file.ts26
1 files changed, 26 insertions, 0 deletions
diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts
new file mode 100644
index 000000000..7b537e114
--- /dev/null
+++ b/editors/code/src/installation/download_file.ts
@@ -0,0 +1,26 @@
1import fetch from "node-fetch";
2import { throttle } from "throttle-debounce";
3import * as fs from "fs";
4
5export async function downloadFile(
6 url: string,
7 destFilePath: fs.PathLike,
8 onProgress: (readBytes: number, totalBytes: number) => void
9): Promise<void> {
10 onProgress = throttle(1000, /* noTrailing: */ true, onProgress);
11
12 const response = await fetch(url);
13
14 const totalBytes = Number(response.headers.get('content-length'));
15 let readBytes = 0;
16
17 return new Promise<void>((resolve, reject) => response.body
18 .on("data", (chunk: Buffer) => {
19 readBytes += chunk.length;
20 onProgress(readBytes, totalBytes);
21 })
22 .on("end", resolve)
23 .on("error", reject)
24 .pipe(fs.createWriteStream(destFilePath))
25 );
26}