2020-02-07 01:11:24 +00:00
|
|
|
import fetch from "node-fetch";
|
2020-03-09 17:54:40 +00:00
|
|
|
import * as vscode from "vscode";
|
|
|
|
import * as path from "path";
|
2020-02-07 01:11:24 +00:00
|
|
|
import * as fs from "fs";
|
2020-02-12 19:40:35 +00:00
|
|
|
import * as stream from "stream";
|
|
|
|
import * as util from "util";
|
2020-02-28 21:56:17 +00:00
|
|
|
import { log, assert } from "../util";
|
2020-03-09 17:54:40 +00:00
|
|
|
import { ArtifactReleaseInfo } from "./interfaces";
|
2020-02-11 21:58:20 +00:00
|
|
|
|
2020-02-12 19:40:35 +00:00
|
|
|
const pipeline = util.promisify(stream.pipeline);
|
|
|
|
|
2020-02-08 19:03:27 +00:00
|
|
|
/**
|
2020-02-11 20:34:52 +00:00
|
|
|
* Downloads file from `url` and stores it at `destFilePath` with `destFilePermissions`.
|
2020-02-09 12:18:05 +00: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 19:03:27 +00:00
|
|
|
*/
|
2020-02-07 01:11:24 +00:00
|
|
|
export async function downloadFile(
|
|
|
|
url: string,
|
|
|
|
destFilePath: fs.PathLike,
|
2020-02-11 20:34:52 +00:00
|
|
|
destFilePermissions: number,
|
2020-02-07 01:11:24 +00:00
|
|
|
onProgress: (readBytes: number, totalBytes: number) => void
|
|
|
|
): Promise<void> {
|
2020-02-13 22:31:23 +00:00
|
|
|
const res = await fetch(url);
|
2020-02-07 01:11:24 +00:00
|
|
|
|
2020-02-11 00:14:04 +00:00
|
|
|
if (!res.ok) {
|
2020-02-21 14:59:46 +00:00
|
|
|
log.error("Error", res.status, "while downloading file from", url);
|
|
|
|
log.error({ body: await res.text(), headers: res.headers });
|
2020-02-11 00:14:04 +00:00
|
|
|
|
2020-02-13 22:31:23 +00:00
|
|
|
throw new Error(`Got response ${res.status} when trying to download a file.`);
|
2020-02-11 00:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const totalBytes = Number(res.headers.get('content-length'));
|
2020-02-08 19:03:27 +00:00
|
|
|
assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol");
|
|
|
|
|
2020-02-21 14:59:46 +00:00
|
|
|
log.debug("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath);
|
2020-02-09 13:01:00 +00:00
|
|
|
|
2020-02-12 19:40:35 +00: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 22:31:23 +00:00
|
|
|
await pipeline(res.body, destFileStream);
|
2020-02-12 19:40:35 +00:00
|
|
|
return new Promise<void>(resolve => {
|
2020-02-13 20:21:19 +00:00
|
|
|
destFileStream.on("close", resolve);
|
2020-02-12 19:40:35 +00:00
|
|
|
destFileStream.destroy();
|
2020-02-13 20:21:19 +00: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 19:40:35 +00:00
|
|
|
});
|
2020-02-07 01:11:24 +00:00
|
|
|
}
|
2020-03-09 17:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Downloads artifact from given `downloadUrl`.
|
|
|
|
* Creates `installationDir` if it is not yet created and puts the artifact under
|
|
|
|
* `artifactFileName`.
|
|
|
|
* Displays info about the download progress in an info message printing the name
|
|
|
|
* of the artifact as `displayName`.
|
|
|
|
*/
|
|
|
|
export async function downloadArtifactWithProgressUi(
|
|
|
|
{ downloadUrl, releaseName }: ArtifactReleaseInfo,
|
|
|
|
artifactFileName: string,
|
|
|
|
installationDir: string,
|
|
|
|
displayName: string,
|
|
|
|
) {
|
|
|
|
await fs.promises.mkdir(installationDir).catch(err => assert(
|
|
|
|
err?.code === "EEXIST",
|
|
|
|
`Couldn't create directory "${installationDir}" to download ` +
|
|
|
|
`${artifactFileName} artifact: ${err?.message}`
|
|
|
|
));
|
|
|
|
|
|
|
|
const installationPath = path.join(installationDir, artifactFileName);
|
|
|
|
|
|
|
|
await vscode.window.withProgress(
|
|
|
|
{
|
|
|
|
location: vscode.ProgressLocation.Notification,
|
|
|
|
cancellable: false, // FIXME: add support for canceling download?
|
|
|
|
title: `Downloading rust-analyzer ${displayName} (${releaseName})`
|
|
|
|
},
|
|
|
|
async (progress, _cancellationToken) => {
|
|
|
|
let lastPrecentage = 0;
|
|
|
|
const filePermissions = 0o755; // (rwx, r_x, r_x)
|
|
|
|
await downloadFile(downloadUrl, installationPath, filePermissions, (readBytes, totalBytes) => {
|
|
|
|
const newPercentage = (readBytes / totalBytes) * 100;
|
|
|
|
progress.report({
|
|
|
|
message: newPercentage.toFixed(0) + "%",
|
|
|
|
increment: newPercentage - lastPrecentage
|
|
|
|
});
|
|
|
|
|
|
|
|
lastPrecentage = newPercentage;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|