Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import fetch from "node-fetch";
var HttpsProxyAgent = require('https-proxy-agent');

import * as vscode from "vscode";
import * as stream from "stream";
import * as crypto from "crypto";
import * as fs from "fs";
import * as zlib from "zlib";
import * as util from "util";
import * as path from "path";
import { log, assert } from "./util";
import * as https from "https";
import { ProxySettings } from "./config";

const pipeline = util.promisify(stream.pipeline);

const GITHUB_API_ENDPOINT_URL = "https://api.github.com";
const OWNER = "rust-analyzer";
const REPO = "rust-analyzer";

function makeHttpAgent(proxy: string | null | undefined, options?: https.AgentOptions) {
    if (proxy) {
        return new HttpsProxyAgent({ ...options, ...new URL(proxy) });
    } else {
        return new https.Agent(options);
    }
}

export async function fetchRelease(
    releaseTag: string,
    githubToken: string | null | undefined,
    proxySettings: ProxySettings,
): 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 headers: Record<string, string> = { Accept: "application/vnd.github.v3+json" };
    if (githubToken != null) {
        headers.Authorization = "token " + githubToken;
    }

    const response = await (() => {
        if (proxySettings.proxy) {
            log.debug(`Fetching release metadata via proxy: ${proxySettings.proxy}`);
        }
        const options: any = {};
        if (proxySettings.strictSSL) {
            options["rejectUnauthorized"] = false;
        }
        const agent = makeHttpAgent(proxySettings.proxy, options);
        return fetch(requestUrl, { headers: headers, agent: agent });
    })();

    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 `unknown` to `GithubRelease`)
    const release = await response.json() as GithubRelease;
    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: vscode.Uri;
    }>;
}

interface DownloadOpts {
    progressTitle: string;
    url: vscode.Uri;
    dest: vscode.Uri;
    mode?: number;
    gunzip?: boolean;
    proxySettings: ProxySettings;
}

export async function download(opts: DownloadOpts) {
    // Put artifact into a temporary file (in the same dir for simplicity)
    // to prevent partially downloaded files when user kills vscode
    // This also avoids overwriting running executables
    const randomHex = crypto.randomBytes(5).toString("hex");
    const rawDest = path.parse(opts.dest.fsPath);
    const oldServerPath = vscode.Uri.joinPath(vscode.Uri.file(rawDest.dir), `${rawDest.name}-stale-${randomHex}${rawDest.ext}`);
    const tempFilePath = vscode.Uri.joinPath(vscode.Uri.file(rawDest.dir), `${rawDest.name}${randomHex}`);

    await vscode.window.withProgress(
        {
            location: vscode.ProgressLocation.Notification,
            cancellable: false,
            title: opts.progressTitle
        },
        async (progress, _cancellationToken) => {
            let lastPercentage = 0;
            await downloadFile(opts.url, tempFilePath, opts.mode, !!opts.gunzip, opts.proxySettings, (readBytes, totalBytes) => {
                const newPercentage = Math.round((readBytes / totalBytes) * 100);
                if (newPercentage !== lastPercentage) {
                    progress.report({
                        message: `${newPercentage.toFixed(0)}%`,
                        increment: newPercentage - lastPercentage
                    });

                    lastPercentage = newPercentage;
                }
            });
        }
    );

    // Try to rename a running server to avoid EPERM on Windows
    // NB: this can lead to issues if a running Code instance tries to restart the server.
    try {
        await vscode.workspace.fs.rename(opts.dest, oldServerPath, { overwrite: true });
        log.info(`Renamed old server binary ${opts.dest.fsPath} to ${oldServerPath.fsPath}`);
    } catch (err) {
        const fsErr = err as vscode.FileSystemError;

        // This is supposed to return `FileNotFound` (spelled as `EntryNotFound`)
        // but instead `code` is `Unknown` and `name` is `EntryNotFound (FileSystemError) (FileSystemError)`.
        // https://github.com/rust-analyzer/rust-analyzer/pull/10222
        if (!fsErr.code || fsErr.code !== "EntryNotFound" && fsErr.name.indexOf("EntryNotFound") === -1) {
            log.error(`Cannot rename existing server instance: ${err}"`);
        }
    }
    try {
        await vscode.workspace.fs.rename(tempFilePath, opts.dest, { overwrite: true });
    } catch (err) {
        log.error(`Cannot update server binary: ${err}`);
    }

    // Now try to remove any stale server binaries
    const serverDir = vscode.Uri.file(rawDest.dir);
    try {
        const entries = await vscode.workspace.fs.readDirectory(serverDir);
        for (const [entry, _] of entries) {
            try {
                if (entry.includes(`${rawDest.name}-stale-`)) {
                    const uri = vscode.Uri.joinPath(serverDir, entry);
                    try {
                        await vscode.workspace.fs.delete(uri);
                        log.info(`Removed old server binary ${uri.fsPath}`);
                    } catch (err) {
                        log.error(`Unable to remove old server binary ${uri.fsPath}`);
                    }
                }
            } catch (err) {
                log.error(`Unable to parse ${entry}`);
            }
        }
    } catch (err) {
        log.error(`Unable to enumerate contents of ${serverDir.fsPath}`);
    }
}

async function downloadFile(
    url: vscode.Uri,
    destFilePath: vscode.Uri,
    mode: number | undefined,
    gunzip: boolean,
    proxySettings: ProxySettings,
    onProgress: (readBytes: number, totalBytes: number) => void
): Promise<void> {
    const urlString = url.toString();

    const res = await (() => {
        if (proxySettings.proxy) {
            log.debug(`Downloading ${urlString} via proxy: ${proxySettings.proxy}`);
        }
        const options: any = {};
        if (proxySettings.strictSSL) {
            options["rejectUnauthorized"] = false;
        }
        const agent = makeHttpAgent(proxySettings.proxy, options);
        return fetch(urlString, { agent: agent });
    })();

    if (!res.ok) {
        log.error("Error", res.status, "while downloading file from", urlString);
        log.error({ body: await res.text(), headers: res.headers });

        throw new Error(`Got response ${res.status} when trying to download a file.`);
    }

    if (!res.body) {
        log.error("Empty body while downloading file from", urlString);
        log.error({ headers: res.headers });
        throw new Error(`Got empty body when trying to download a file.`);
    }

    const totalBytes = Number(res.headers.get('content-length'));
    assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol");

    log.debug("Downloading file of", totalBytes, "bytes size from", urlString, "to", destFilePath.fsPath);

    let readBytes = 0;
    res.body.on("data", (chunk: Buffer) => {
        readBytes += chunk.length;
        onProgress(readBytes, totalBytes);
    });

    const destFileStream = fs.createWriteStream(destFilePath.fsPath, { mode });
    const srcStream = gunzip ? res.body.pipe(zlib.createGunzip()) : res.body;

    await pipeline(srcStream, destFileStream);
}