2020-02-06 19:11:24 -06:00
|
|
|
import fetch from "node-fetch";
|
|
|
|
import * as fs from "fs";
|
2020-02-12 13:40:35 -06:00
|
|
|
import * as stream from "stream";
|
|
|
|
import * as util from "util";
|
2020-02-08 13:03:27 -06:00
|
|
|
import { strict as assert } from "assert";
|
2020-02-11 15:58:20 -06:00
|
|
|
|
2020-02-12 13:40:35 -06:00
|
|
|
const pipeline = util.promisify(stream.pipeline);
|
|
|
|
|
2020-02-08 13:03:27 -06:00
|
|
|
/**
|
2020-02-11 14:34:52 -06:00
|
|
|
* Downloads file from `url` and stores it at `destFilePath` with `destFilePermissions`.
|
2020-02-09 06:18:05 -06:00
|
|
|
* `onProgress` callback is called on recieveing each chunk of bytes
|
|
|
|
* to track the progress of downloading, it gets the already read and total
|
|
|
|
* amount of bytes to read as its parameters.
|
2020-02-08 13:03:27 -06:00
|
|
|
*/
|
2020-02-06 19:11:24 -06:00
|
|
|
export async function downloadFile(
|
|
|
|
url: string,
|
|
|
|
destFilePath: fs.PathLike,
|
2020-02-11 14:34:52 -06:00
|
|
|
destFilePermissions: number,
|
2020-02-06 19:11:24 -06:00
|
|
|
onProgress: (readBytes: number, totalBytes: number) => void
|
|
|
|
): Promise<void> {
|
2020-02-13 16:31:23 -06:00
|
|
|
const res = await fetch(url);
|
2020-02-06 19:11:24 -06:00
|
|
|
|
2020-02-10 18:14:04 -06:00
|
|
|
if (!res.ok) {
|
|
|
|
console.log("Error", res.status, "while downloading file from", url);
|
|
|
|
console.dir({ body: await res.text(), headers: res.headers }, { depth: 3 });
|
|
|
|
|
2020-02-13 16:31:23 -06:00
|
|
|
throw new Error(`Got response ${res.status} when trying to download a file.`);
|
2020-02-10 18:14:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const totalBytes = Number(res.headers.get('content-length'));
|
2020-02-08 13:03:27 -06:00
|
|
|
assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol");
|
|
|
|
|
2020-02-09 07:01:00 -06:00
|
|
|
console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath);
|
|
|
|
|
2020-02-12 13:40:35 -06:00
|
|
|
let readBytes = 0;
|
|
|
|
res.body.on("data", (chunk: Buffer) => {
|
|
|
|
readBytes += chunk.length;
|
|
|
|
onProgress(readBytes, totalBytes);
|
|
|
|
});
|
|
|
|
|
|
|
|
const destFileStream = fs.createWriteStream(destFilePath, { mode: destFilePermissions });
|
|
|
|
|
2020-02-13 16:31:23 -06:00
|
|
|
await pipeline(res.body, destFileStream);
|
2020-02-12 13:40:35 -06:00
|
|
|
return new Promise<void>(resolve => {
|
2020-02-13 14:21:19 -06:00
|
|
|
destFileStream.on("close", resolve);
|
2020-02-12 13:40:35 -06:00
|
|
|
destFileStream.destroy();
|
2020-02-13 14:21:19 -06:00
|
|
|
|
|
|
|
// Details on workaround: https://github.com/rust-analyzer/rust-analyzer/pull/3092#discussion_r378191131
|
|
|
|
// Issue at nodejs repo: https://github.com/nodejs/node/issues/31776
|
2020-02-12 13:40:35 -06:00
|
|
|
});
|
2020-02-06 19:11:24 -06:00
|
|
|
}
|