2020-02-06 19:11:24 -06:00
|
|
|
import fetch from "node-fetch";
|
|
|
|
import * as fs from "fs";
|
2020-02-08 13:03:27 -06:00
|
|
|
import { strict as assert } from "assert";
|
2020-02-11 15:58:20 -06:00
|
|
|
import { NestedError } from "ts-nested-error";
|
|
|
|
|
|
|
|
class DownloadFileError extends NestedError {}
|
2020-02-06 19:11:24 -06:00
|
|
|
|
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-11 15:58:20 -06:00
|
|
|
const res = await fetch(url).catch(DownloadFileError.rethrow("Failed at initial fetch"));
|
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-11 15:58:20 -06:00
|
|
|
throw new DownloadFileError(`Got response ${res.status}`);
|
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-06 19:11:24 -06:00
|
|
|
let readBytes = 0;
|
|
|
|
|
2020-02-09 07:01:00 -06:00
|
|
|
console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath);
|
|
|
|
|
2020-02-11 15:58:20 -06:00
|
|
|
// Here reject() may be called 2 times. As per ECMAScript standard, 2-d call is ignored
|
|
|
|
// https://tc39.es/ecma262/#sec-promise-reject-functions
|
|
|
|
|
2020-02-10 18:14:04 -06:00
|
|
|
return new Promise<void>((resolve, reject) => res.body
|
2020-02-06 19:11:24 -06:00
|
|
|
.on("data", (chunk: Buffer) => {
|
|
|
|
readBytes += chunk.length;
|
|
|
|
onProgress(readBytes, totalBytes);
|
|
|
|
})
|
2020-02-11 15:58:20 -06:00
|
|
|
.on("error", err => reject(
|
|
|
|
new DownloadFileError(`Read-stream error, read bytes: ${readBytes}`, err)
|
|
|
|
))
|
|
|
|
.pipe(fs.createWriteStream(destFilePath, { mode: destFilePermissions }))
|
|
|
|
.on("error", err => reject(
|
|
|
|
new DownloadFileError(`Write-stream error, read bytes: ${readBytes}`, err)
|
|
|
|
))
|
|
|
|
.on("close", resolve)
|
2020-02-06 19:11:24 -06:00
|
|
|
);
|
|
|
|
}
|