diff options
author | Veetaha <[email protected]> | 2020-03-09 17:54:40 +0000 |
---|---|---|
committer | Veetaha <[email protected]> | 2020-03-14 00:01:46 +0000 |
commit | 8203828bb081faae4cd9d39c8abe6bc073138176 (patch) | |
tree | fb6f290c58677e3109e4ecbca424258f09d7fea7 /editors/code/src/installation/download_file.ts | |
parent | fab40db8bdfdac5cde5789f74d1ec28e60a8bb7e (diff) |
vscode-prerefactor: merge two files into downloads.ts
Diffstat (limited to 'editors/code/src/installation/download_file.ts')
-rw-r--r-- | editors/code/src/installation/download_file.ts | 51 |
1 files changed, 0 insertions, 51 deletions
diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts deleted file mode 100644 index ee8949d61..000000000 --- a/editors/code/src/installation/download_file.ts +++ /dev/null | |||
@@ -1,51 +0,0 @@ | |||
1 | import fetch from "node-fetch"; | ||
2 | import * as fs from "fs"; | ||
3 | import * as stream from "stream"; | ||
4 | import * as util from "util"; | ||
5 | import { log, assert } from "../util"; | ||
6 | |||
7 | const pipeline = util.promisify(stream.pipeline); | ||
8 | |||
9 | /** | ||
10 | * Downloads file from `url` and stores it at `destFilePath` with `destFilePermissions`. | ||
11 | * `onProgress` callback is called on recieveing each chunk of bytes | ||
12 | * to track the progress of downloading, it gets the already read and total | ||
13 | * amount of bytes to read as its parameters. | ||
14 | */ | ||
15 | export async function downloadFile( | ||
16 | url: string, | ||
17 | destFilePath: fs.PathLike, | ||
18 | destFilePermissions: number, | ||
19 | onProgress: (readBytes: number, totalBytes: number) => void | ||
20 | ): Promise<void> { | ||
21 | const res = await fetch(url); | ||
22 | |||
23 | if (!res.ok) { | ||
24 | log.error("Error", res.status, "while downloading file from", url); | ||
25 | log.error({ body: await res.text(), headers: res.headers }); | ||
26 | |||
27 | throw new Error(`Got response ${res.status} when trying to download a file.`); | ||
28 | } | ||
29 | |||
30 | const totalBytes = Number(res.headers.get('content-length')); | ||
31 | assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol"); | ||
32 | |||
33 | log.debug("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); | ||
34 | |||
35 | let readBytes = 0; | ||
36 | res.body.on("data", (chunk: Buffer) => { | ||
37 | readBytes += chunk.length; | ||
38 | onProgress(readBytes, totalBytes); | ||
39 | }); | ||
40 | |||
41 | const destFileStream = fs.createWriteStream(destFilePath, { mode: destFilePermissions }); | ||
42 | |||
43 | await pipeline(res.body, destFileStream); | ||
44 | return new Promise<void>(resolve => { | ||
45 | destFileStream.on("close", resolve); | ||
46 | destFileStream.destroy(); | ||
47 | |||
48 | // Details on workaround: https://github.com/rust-analyzer/rust-analyzer/pull/3092#discussion_r378191131 | ||
49 | // Issue at nodejs repo: https://github.com/nodejs/node/issues/31776 | ||
50 | }); | ||
51 | } | ||