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";
|
2020-02-12 19:40:35 +00:00
|
|
|
import * as stream from "stream";
|
2020-06-24 22:00:30 +00:00
|
|
|
import * as crypto from "crypto";
|
2020-06-20 12:38:08 +00:00
|
|
|
import * as fs from "fs";
|
2020-02-12 19:40:35 +00:00
|
|
|
import * as util from "util";
|
2020-06-24 22:00:30 +00:00
|
|
|
import * as path from "path";
|
2020-03-17 11:44:31 +00:00
|
|
|
import { log, assert } from "./util";
|
2020-02-11 21:58:20 +00:00
|
|
|
|
2020-02-12 19:40:35 +00:00
|
|
|
const pipeline = util.promisify(stream.pipeline);
|
|
|
|
|
2020-03-17 11:44:31 +00:00
|
|
|
const GITHUB_API_ENDPOINT_URL = "https://api.github.com";
|
|
|
|
const OWNER = "rust-analyzer";
|
|
|
|
const REPO = "rust-analyzer";
|
|
|
|
|
|
|
|
export async function fetchRelease(
|
|
|
|
releaseTag: string
|
|
|
|
): Promise<GithubRelease> {
|
|
|
|
|
|
|
|
const apiEndpointPath = `/repos/${OWNER}/${REPO}/releases/tags/${releaseTag}`;
|
|
|
|
|
|
|
|
const requestUrl = GITHUB_API_ENDPOINT_URL + apiEndpointPath;
|
|
|
|
|
|
|
|
log.debug("Issuing request for released artifacts metadata to", requestUrl);
|
|
|
|
|
|
|
|
const response = await fetch(requestUrl, { headers: { Accept: "application/vnd.github.v3+json" } });
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
log.error("Error fetching artifact release info", {
|
|
|
|
requestUrl,
|
|
|
|
releaseTag,
|
|
|
|
response: {
|
|
|
|
headers: response.headers,
|
|
|
|
status: response.status,
|
|
|
|
body: await response.text(),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
throw new Error(
|
|
|
|
`Got response ${response.status} when trying to fetch ` +
|
|
|
|
`release info for ${releaseTag} release`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We skip runtime type checks for simplicity (here we cast from `any` to `GithubRelease`)
|
|
|
|
const release: GithubRelease = await response.json();
|
|
|
|
return release;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We omit declaration of tremendous amount of fields that we are not using here
|
|
|
|
export interface GithubRelease {
|
|
|
|
name: string;
|
|
|
|
id: number;
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
published_at: string;
|
|
|
|
assets: Array<{
|
|
|
|
name: string;
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
browser_download_url: string;
|
|
|
|
}>;
|
|
|
|
}
|
|
|
|
|
2020-06-22 18:36:56 +00:00
|
|
|
interface DownloadOpts {
|
|
|
|
progressTitle: string;
|
|
|
|
url: string;
|
|
|
|
dest: string;
|
|
|
|
mode?: number;
|
|
|
|
}
|
2020-03-17 11:44:31 +00:00
|
|
|
|
2020-06-22 18:36:56 +00:00
|
|
|
export async function download(opts: DownloadOpts) {
|
2020-06-24 10:19:14 +00:00
|
|
|
// Put artifact into a temporary file (in the same dir for simplicity)
|
|
|
|
// to prevent partially downloaded files when user kills vscode
|
2020-06-24 22:00:30 +00:00
|
|
|
const dest = path.parse(opts.dest);
|
|
|
|
const randomHex = crypto.randomBytes(5).toString("hex");
|
|
|
|
const tempFile = path.join(dest.dir, `${dest.name}${randomHex}`);
|
2020-06-24 10:19:14 +00:00
|
|
|
|
|
|
|
await vscode.window.withProgress(
|
|
|
|
{
|
|
|
|
location: vscode.ProgressLocation.Notification,
|
|
|
|
cancellable: false,
|
|
|
|
title: opts.progressTitle
|
|
|
|
},
|
|
|
|
async (progress, _cancellationToken) => {
|
|
|
|
let lastPercentage = 0;
|
|
|
|
await downloadFile(opts.url, tempFile, opts.mode, (readBytes, totalBytes) => {
|
|
|
|
const newPercentage = (readBytes / totalBytes) * 100;
|
|
|
|
progress.report({
|
|
|
|
message: newPercentage.toFixed(0) + "%",
|
|
|
|
increment: newPercentage - lastPercentage
|
2020-03-17 11:44:31 +00:00
|
|
|
});
|
|
|
|
|
2020-06-24 10:19:14 +00:00
|
|
|
lastPercentage = newPercentage;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
await fs.promises.rename(tempFile, opts.dest);
|
2020-03-17 11:44:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 19:03:27 +00:00
|
|
|
/**
|
2020-06-20 12:38:08 +00:00
|
|
|
* Downloads file from `url` and stores it at `destFilePath` with `mode` (unix permissions).
|
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-03-17 11:44:31 +00:00
|
|
|
async function downloadFile(
|
2020-02-07 01:11:24 +00:00
|
|
|
url: string,
|
|
|
|
destFilePath: fs.PathLike,
|
2020-03-17 11:44:31 +00:00
|
|
|
mode: number | undefined,
|
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-06-22 18:36:56 +00:00
|
|
|
let readBytes = 0;
|
|
|
|
res.body.on("data", (chunk: Buffer) => {
|
|
|
|
readBytes += chunk.length;
|
|
|
|
onProgress(readBytes, totalBytes);
|
|
|
|
});
|
2020-06-22 16:50:57 +00:00
|
|
|
|
2020-06-22 18:36:56 +00:00
|
|
|
const destFileStream = fs.createWriteStream(destFilePath, { mode });
|
|
|
|
await pipeline(res.body, destFileStream);
|
|
|
|
await new Promise<void>(resolve => {
|
|
|
|
destFileStream.on("close", resolve);
|
|
|
|
destFileStream.destroy();
|
|
|
|
// This workaround is awaiting to be removed when vscode moves to newer nodejs version:
|
|
|
|
// https://github.com/rust-analyzer/rust-analyzer/issues/3167
|
2020-02-12 19:40:35 +00:00
|
|
|
});
|
2020-02-07 01:11:24 +00:00
|
|
|
}
|