mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
vscode: add docs to installation module interfaces and sanity check to donloadFile()
This commit is contained in:
parent
6ef912f925
commit
4e85254444
2 changed files with 39 additions and 4 deletions
|
@ -1,17 +1,25 @@
|
||||||
import fetch from "node-fetch";
|
import fetch from "node-fetch";
|
||||||
import { throttle } from "throttle-debounce";
|
import { throttle } from "throttle-debounce";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
|
import { strict as assert } from "assert";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Downloads file from `url` and stores it at `destFilePath`.
|
||||||
|
* `onProgress` callback is periodically called to track the progress of downloading,
|
||||||
|
* it gets the already read and total amount of bytes to read as its parameters.
|
||||||
|
*/
|
||||||
export async function downloadFile(
|
export async function downloadFile(
|
||||||
url: string,
|
url: string,
|
||||||
destFilePath: fs.PathLike,
|
destFilePath: fs.PathLike,
|
||||||
onProgress: (readBytes: number, totalBytes: number) => void
|
onProgress: (readBytes: number, totalBytes: number) => void
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
onProgress = throttle(1000, /* noTrailing: */ true, onProgress);
|
onProgress = throttle(500, /* noTrailing: */ true, onProgress);
|
||||||
|
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
|
|
||||||
const totalBytes = Number(response.headers.get('content-length'));
|
const totalBytes = Number(response.headers.get('content-length'));
|
||||||
|
assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol");
|
||||||
|
|
||||||
let readBytes = 0;
|
let readBytes = 0;
|
||||||
|
|
||||||
return new Promise<void>((resolve, reject) => response.body
|
return new Promise<void>((resolve, reject) => response.body
|
||||||
|
|
|
@ -3,24 +3,51 @@ export interface GithubRepo {
|
||||||
owner: string;
|
owner: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metadata about particular artifact retrieved from GitHub releases.
|
||||||
|
*/
|
||||||
export interface ArtifactMetadata {
|
export interface ArtifactMetadata {
|
||||||
releaseName: string;
|
releaseName: string;
|
||||||
downloadUrl: string;
|
downloadUrl: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type tag for `BinarySource` discriminated union.
|
||||||
|
*/
|
||||||
export enum BinarySourceType { ExplicitPath, GithubBinary }
|
export enum BinarySourceType { ExplicitPath, GithubBinary }
|
||||||
|
|
||||||
export type BinarySource = EplicitPathSource | GithubBinarySource;
|
/**
|
||||||
|
* Represents the source of a binary artifact which is either specified by the user
|
||||||
|
* explicitly, or bundled by this extension from GitHub releases.
|
||||||
|
*/
|
||||||
|
export type BinarySource = ExplicitPathSource | GithubBinarySource;
|
||||||
|
|
||||||
export interface EplicitPathSource {
|
|
||||||
|
export interface ExplicitPathSource {
|
||||||
type: BinarySourceType.ExplicitPath;
|
type: BinarySourceType.ExplicitPath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filesystem path to the binary specified by the user explicitly.
|
||||||
|
*/
|
||||||
path: string;
|
path: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface GithubBinarySource {
|
export interface GithubBinarySource {
|
||||||
type: BinarySourceType.GithubBinary;
|
type: BinarySourceType.GithubBinary;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repository where the binary is stored.
|
||||||
|
*/
|
||||||
repo: GithubRepo;
|
repo: GithubRepo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Directory on the filesystem where the bundled binary is stored.
|
||||||
|
*/
|
||||||
dir: string;
|
dir: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the binary file. It is stored under the same name on GitHub releases
|
||||||
|
* and in local `.dir`.
|
||||||
|
*/
|
||||||
file: string;
|
file: string;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue