Decouple http file stream logic from temp dir logic

This commit is contained in:
Veetaha 2020-06-22 21:36:56 +03:00
parent ceb69203b5
commit 0514d817db
2 changed files with 57 additions and 44 deletions

View file

@ -178,7 +178,11 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi
assert(!!artifact, `Bad release: ${JSON.stringify(release)}`); assert(!!artifact, `Bad release: ${JSON.stringify(release)}`);
const dest = path.join(config.globalStoragePath, "rust-analyzer.vsix"); const dest = path.join(config.globalStoragePath, "rust-analyzer.vsix");
await download(artifact.browser_download_url, dest, "Downloading rust-analyzer extension"); await download({
url: artifact.browser_download_url,
dest,
progressTitle: "Downloading rust-analyzer extension",
});
await vscode.commands.executeCommand("workbench.extensions.installExtension", vscode.Uri.file(dest)); await vscode.commands.executeCommand("workbench.extensions.installExtension", vscode.Uri.file(dest));
await fs.unlink(dest); await fs.unlink(dest);
@ -299,7 +303,12 @@ async function getServer(config: Config, state: PersistentState): Promise<string
if (err.code !== "ENOENT") throw err; if (err.code !== "ENOENT") throw err;
}); });
await download(artifact.browser_download_url, dest, "Downloading rust-analyzer server", { mode: 0o755 }); await download({
url: artifact.browser_download_url,
dest,
progressTitle: "Downloading rust-analyzer server",
mode: 0o755
});
// Patching executable if that's NixOS. // Patching executable if that's NixOS.
if (await fs.stat("/etc/nixos").then(_ => true).catch(_ => false)) { if (await fs.stat("/etc/nixos").then(_ => true).catch(_ => false)) {

View file

@ -60,32 +60,40 @@ export interface GithubRelease {
}>; }>;
} }
interface DownloadOpts {
progressTitle: string;
url: string;
dest: string;
mode?: number;
}
export async function download( export async function download(opts: DownloadOpts) {
downloadUrl: string, // Put the artifact into a temporary folder to prevent partially downloaded files when user kills vscode
destinationPath: string, await withTempDir(async tempDir => {
progressTitle: string, const tempFile = path.join(tempDir, path.basename(opts.dest));
{ mode }: { mode?: number } = {},
) { await vscode.window.withProgress(
await vscode.window.withProgress( {
{ location: vscode.ProgressLocation.Notification,
location: vscode.ProgressLocation.Notification, cancellable: false,
cancellable: false, title: opts.progressTitle
title: progressTitle },
}, async (progress, _cancellationToken) => {
async (progress, _cancellationToken) => { let lastPercentage = 0;
let lastPercentage = 0; await downloadFile(opts.url, tempFile, opts.mode, (readBytes, totalBytes) => {
await downloadFile(downloadUrl, destinationPath, mode, (readBytes, totalBytes) => { const newPercentage = (readBytes / totalBytes) * 100;
const newPercentage = (readBytes / totalBytes) * 100; progress.report({
progress.report({ message: newPercentage.toFixed(0) + "%",
message: newPercentage.toFixed(0) + "%", increment: newPercentage - lastPercentage
increment: newPercentage - lastPercentage });
lastPercentage = newPercentage;
}); });
}
);
lastPercentage = newPercentage; await moveFile(tempFile, opts.dest);
}); });
}
);
} }
/** /**
@ -114,28 +122,23 @@ async function downloadFile(
log.debug("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); log.debug("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath);
// Put the artifact into a temporary folder to prevent partially downloaded files when user kills vscode let readBytes = 0;
await withTempFile(async tempFilePath => { res.body.on("data", (chunk: Buffer) => {
const destFileStream = fs.createWriteStream(tempFilePath, { mode }); readBytes += chunk.length;
onProgress(readBytes, totalBytes);
});
let readBytes = 0; const destFileStream = fs.createWriteStream(destFilePath, { mode });
res.body.on("data", (chunk: Buffer) => { await pipeline(res.body, destFileStream);
readBytes += chunk.length; await new Promise<void>(resolve => {
onProgress(readBytes, totalBytes); destFileStream.on("close", resolve);
}); destFileStream.destroy();
// This workaround is awaiting to be removed when vscode moves to newer nodejs version:
await pipeline(res.body, destFileStream); // https://github.com/rust-analyzer/rust-analyzer/issues/3167
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
});
await moveFile(tempFilePath, destFilePath);
}); });
} }
async function withTempFile(scope: (tempFilePath: string) => Promise<void>) { async function withTempDir(scope: (tempDirPath: string) => Promise<void>) {
// Based on the great article: https://advancedweb.hu/secure-tempfiles-in-nodejs-without-dependencies/ // Based on the great article: https://advancedweb.hu/secure-tempfiles-in-nodejs-without-dependencies/
// `.realpath()` should handle the cases where os.tmpdir() contains symlinks // `.realpath()` should handle the cases where os.tmpdir() contains symlinks
@ -144,7 +147,7 @@ async function withTempFile(scope: (tempFilePath: string) => Promise<void>) {
const tempDir = await fs.promises.mkdtemp(path.join(osTempDir, "rust-analyzer")); const tempDir = await fs.promises.mkdtemp(path.join(osTempDir, "rust-analyzer"));
try { try {
return await scope(path.join(tempDir, "file")); return await scope(tempDir);
} finally { } finally {
// We are good citizens :D // We are good citizens :D
void fs.promises.rmdir(tempDir, { recursive: true }).catch(log.error); void fs.promises.rmdir(tempDir, { recursive: true }).catch(log.error);
@ -161,6 +164,7 @@ async function moveFile(src: fs.PathLike, dest: fs.PathLike) {
await fs.promises.unlink(src); await fs.promises.unlink(src);
} else { } else {
log.error(`Failed to rename the file ${src} -> ${dest}`, err); log.error(`Failed to rename the file ${src} -> ${dest}`, err);
throw err;
} }
} }
} }