diff options
author | Veetaha <[email protected]> | 2020-02-07 01:11:24 +0000 |
---|---|---|
committer | Veetaha <[email protected]> | 2020-02-08 02:34:11 +0000 |
commit | 3e0e4e90aeeff25db674f8db562c611bd8016482 (patch) | |
tree | 638756f88bf4c370135b3984667c2618438f6d01 /editors/code/src/github/download_file.ts | |
parent | 1bdb78a89f6618527f2dc23f8f76e83ee77e3ea5 (diff) |
added fetchLatestArtifactMetadata() and downloadFile() functions
Diffstat (limited to 'editors/code/src/github/download_file.ts')
-rw-r--r-- | editors/code/src/github/download_file.ts | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/editors/code/src/github/download_file.ts b/editors/code/src/github/download_file.ts new file mode 100644 index 000000000..f40750be9 --- /dev/null +++ b/editors/code/src/github/download_file.ts | |||
@@ -0,0 +1,26 @@ | |||
1 | import fetch from "node-fetch"; | ||
2 | import { throttle } from "throttle-debounce"; | ||
3 | import * as fs from "fs"; | ||
4 | |||
5 | export async function downloadFile( | ||
6 | url: string, | ||
7 | destFilePath: fs.PathLike, | ||
8 | onProgress: (readBytes: number, totalBytes: number) => void | ||
9 | ): Promise<void> { | ||
10 | onProgress = throttle(100, /* 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 | } | ||