aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/installation/download_file.ts
diff options
context:
space:
mode:
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}