aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/installation/downloads.ts
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-03-17 11:44:31 +0000
committerAleksey Kladov <[email protected]>2020-03-19 08:04:59 +0000
commitfb6e655de8a44c65275ad45a27bf5bd684670ba0 (patch)
tree9c307ac69c8fc59465ee2fb6f9a8a619fc064167 /editors/code/src/installation/downloads.ts
parentf0a1b64d7ee3baa7ccf980b35b85f0a4a3b85b1a (diff)
Rewrite auto-update
Everything now happens in main.ts, in the bootstrap family of functions. The current flow is: * check everything only on extension installation. * if the user is on nightly channel, try to download the nightly extension and reload. * when we install nightly extension, we persist its release id, so that we can check if the current release is different. * if server binary was not downloaded by the current version of the extension, redownload it (we persist the version of ext that downloaded the server).
Diffstat (limited to 'editors/code/src/installation/downloads.ts')
-rw-r--r--editors/code/src/installation/downloads.ts97
1 files changed, 0 insertions, 97 deletions
diff --git a/editors/code/src/installation/downloads.ts b/editors/code/src/installation/downloads.ts
deleted file mode 100644
index 7ce2e2960..000000000
--- a/editors/code/src/installation/downloads.ts
+++ /dev/null
@@ -1,97 +0,0 @@
1import fetch from "node-fetch";
2import * as vscode from "vscode";
3import * as path from "path";
4import * as fs from "fs";
5import * as stream from "stream";
6import * as util from "util";
7import { log, assert } from "../util";
8import { ArtifactReleaseInfo } from "./interfaces";
9
10const pipeline = util.promisify(stream.pipeline);
11
12/**
13 * Downloads file from `url` and stores it at `destFilePath` with `destFilePermissions`.
14 * `onProgress` callback is called on recieveing each chunk of bytes
15 * to track the progress of downloading, it gets the already read and total
16 * amount of bytes to read as its parameters.
17 */
18export async function downloadFile(
19 url: string,
20 destFilePath: fs.PathLike,
21 destFilePermissions: number,
22 onProgress: (readBytes: number, totalBytes: number) => void
23): Promise<void> {
24 const res = await fetch(url);
25
26 if (!res.ok) {
27 log.error("Error", res.status, "while downloading file from", url);
28 log.error({ body: await res.text(), headers: res.headers });
29
30 throw new Error(`Got response ${res.status} when trying to download a file.`);
31 }
32
33 const totalBytes = Number(res.headers.get('content-length'));
34 assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol");
35
36 log.debug("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath);
37
38 let readBytes = 0;
39 res.body.on("data", (chunk: Buffer) => {
40 readBytes += chunk.length;
41 onProgress(readBytes, totalBytes);
42 });
43
44 const destFileStream = fs.createWriteStream(destFilePath, { mode: destFilePermissions });
45
46 await pipeline(res.body, destFileStream);
47 return new Promise<void>(resolve => {
48 destFileStream.on("close", resolve);
49 destFileStream.destroy();
50
51 // Details on workaround: https://github.com/rust-analyzer/rust-analyzer/pull/3092#discussion_r378191131
52 // Issue at nodejs repo: https://github.com/nodejs/node/issues/31776
53 });
54}
55
56/**
57 * Downloads artifact from given `downloadUrl`.
58 * Creates `installationDir` if it is not yet created and puts the artifact under
59 * `artifactFileName`.
60 * Displays info about the download progress in an info message printing the name
61 * of the artifact as `displayName`.
62 */
63export async function downloadArtifactWithProgressUi(
64 { downloadUrl, releaseName }: ArtifactReleaseInfo,
65 artifactFileName: string,
66 installationDir: string,
67 displayName: string,
68) {
69 await fs.promises.mkdir(installationDir).catch(err => assert(
70 err?.code === "EEXIST",
71 `Couldn't create directory "${installationDir}" to download ` +
72 `${artifactFileName} artifact: ${err?.message}`
73 ));
74
75 const installationPath = path.join(installationDir, artifactFileName);
76
77 await vscode.window.withProgress(
78 {
79 location: vscode.ProgressLocation.Notification,
80 cancellable: false, // FIXME: add support for canceling download?
81 title: `Downloading rust-analyzer ${displayName} (${releaseName})`
82 },
83 async (progress, _cancellationToken) => {
84 let lastPrecentage = 0;
85 const filePermissions = 0o755; // (rwx, r_x, r_x)
86 await downloadFile(downloadUrl, installationPath, filePermissions, (readBytes, totalBytes) => {
87 const newPercentage = (readBytes / totalBytes) * 100;
88 progress.report({
89 message: newPercentage.toFixed(0) + "%",
90 increment: newPercentage - lastPrecentage
91 });
92
93 lastPrecentage = newPercentage;
94 });
95 }
96 );
97}