Skip to content

Commit

Permalink
Add a progress bar to the binary server download
Browse files Browse the repository at this point in the history
Closes redhat-developer#433

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Mar 9, 2021
1 parent 728c669 commit 6fd36a2
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/server/binary/binaryServerStarter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as https from 'https';
import * as os from 'os';
import * as path from 'path';
import { Readable } from 'stream';
import { ExtensionContext, extensions, window, WorkspaceConfiguration } from "vscode";
import { ExtensionContext, extensions, window, WorkspaceConfiguration, ProgressOptions, ProgressLocation, Progress, CancellationToken } from "vscode";
import { Executable } from "vscode-languageclient";
import * as yauzl from 'yauzl';
import { addTrustedHash, getTrustedHashes } from './binaryHashManager';
Expand Down Expand Up @@ -86,6 +86,7 @@ async function downloadBinary(): Promise<string> {
reject(`Server binary download failed: ${e.message}`);
});
} else if (statusCode === 200) {
showProgressForDownload(response);
if (/^application\/zip$/.test(response.headers['content-type'])) {
acceptZipDownloadResponse(response).then(resolve, reject);
} else {
Expand All @@ -101,7 +102,6 @@ async function downloadBinary(): Promise<string> {
reject(`Server binary download failed: ${e.message}`);
});
});
window.setStatusBarMessage('Downloading XML language server binary...', downloadPromise);
return downloadPromise;
}

Expand Down Expand Up @@ -217,6 +217,45 @@ function httpHttpsGet(url: string, callback: (response: http.IncomingMessage) =>
return http.get(url, callback);
}

/**
* Show a progress notification for the given get response
*
* Inspired by
* https://stackoverflow.com/questions/18323152/get-download-progress-in-node-js-with-request
*
* @param response the http GET response
*/
function showProgressForDownload(response: http.IncomingMessage) {
const progressOptions: ProgressOptions = {
location: ProgressLocation.Notification,
cancellable: false,
title: "Downloading the XML Language Server..."
};
window.withProgress(
progressOptions,
(progress, _cancelToken) => {

const size: number = parseInt(response.headers['content-length']);
let downloaded: number = 0;
let previousReportedDownloadedPercent: number = 0;
response.on("data", (chunk) => {
downloaded += chunk.length;
const incrementAmt = ((100.0 * downloaded / size) - previousReportedDownloadedPercent);
previousReportedDownloadedPercent += incrementAmt;
progress.report({
increment: incrementAmt
});
});
const downloadFinish: Promise<void> = new Promise((resolve, _reject) => {
response.on("close", () => {
resolve();
})
});

return downloadFinish;
});
}

/**
* Unzips the contents of the response and writes the contained file to a local file, marks the file as executable, and returns the path to the file
*
Expand Down

0 comments on commit 6fd36a2

Please sign in to comment.