about summary refs log tree commit diff
path: root/editors/code/src
diff options
context:
space:
mode:
authorVeetaha <veetaha2@gmail.com>2020-06-22 21:36:56 +0300
committerVeetaha <veetaha2@gmail.com>2020-06-22 21:43:53 +0300
commit0514d817db84c683aaf250b823edc8b08bfee3da (patch)
tree56af0bdf3724d4bed6101c18b3a8579102e2b46c /editors/code/src
parentceb69203b55d83aeaf4e58bff4a58f2f17d4087d (diff)
downloadrust-0514d817db84c683aaf250b823edc8b08bfee3da.tar.gz
rust-0514d817db84c683aaf250b823edc8b08bfee3da.zip
Decouple http file stream logic from temp dir logic
Diffstat (limited to 'editors/code/src')
-rw-r--r--editors/code/src/main.ts13
-rw-r--r--editors/code/src/net.ts88
2 files changed, 57 insertions, 44 deletions
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 670f2ebfd62..7bae8bb3338 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -178,7 +178,11 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi
     assert(!!artifact, `Bad release: ${JSON.stringify(release)}`);
 
     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 fs.unlink(dest);
@@ -299,7 +303,12 @@ async function getServer(config: Config, state: PersistentState): Promise<string
         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.
     if (await fs.stat("/etc/nixos").then(_ => true).catch(_ => false)) {
diff --git a/editors/code/src/net.ts b/editors/code/src/net.ts
index 9debdc57bfa..e02fd6d4f47 100644
--- a/editors/code/src/net.ts
+++ b/editors/code/src/net.ts
@@ -60,32 +60,40 @@ export interface GithubRelease {
     }>;
 }
 
+interface DownloadOpts {
+    progressTitle: string;
+    url: string;
+    dest: string;
+    mode?: number;
+}
 
-export async function download(
-    downloadUrl: string,
-    destinationPath: string,
-    progressTitle: string,
-    { mode }: { mode?: number } = {},
-) {
-    await vscode.window.withProgress(
-        {
-            location: vscode.ProgressLocation.Notification,
-            cancellable: false,
-            title: progressTitle
-        },
-        async (progress, _cancellationToken) => {
-            let lastPercentage = 0;
-            await downloadFile(downloadUrl, destinationPath, mode, (readBytes, totalBytes) => {
-                const newPercentage = (readBytes / totalBytes) * 100;
-                progress.report({
-                    message: newPercentage.toFixed(0) + "%",
-                    increment: newPercentage - lastPercentage
+export async function download(opts: DownloadOpts) {
+    // Put the artifact into a temporary folder to prevent partially downloaded files when user kills vscode
+    await withTempDir(async tempDir => {
+        const tempFile = path.join(tempDir, path.basename(opts.dest));
+
+        await vscode.window.withProgress(
+            {
+                location: vscode.ProgressLocation.Notification,
+                cancellable: false,
+                title: opts.progressTitle
+            },
+            async (progress, _cancellationToken) => {
+                let lastPercentage = 0;
+                await downloadFile(opts.url, tempFile, opts.mode, (readBytes, totalBytes) => {
+                    const newPercentage = (readBytes / totalBytes) * 100;
+                    progress.report({
+                        message: newPercentage.toFixed(0) + "%",
+                        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);
 
-    // Put the artifact into a temporary folder to prevent partially downloaded files when user kills vscode
-    await withTempFile(async tempFilePath => {
-        const destFileStream = fs.createWriteStream(tempFilePath, { mode });
-
-        let readBytes = 0;
-        res.body.on("data", (chunk: Buffer) => {
-            readBytes += chunk.length;
-            onProgress(readBytes, totalBytes);
-        });
+    let readBytes = 0;
+    res.body.on("data", (chunk: Buffer) => {
+        readBytes += chunk.length;
+        onProgress(readBytes, totalBytes);
+    });
 
-        await pipeline(res.body, destFileStream);
-        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);
+    const destFileStream = fs.createWriteStream(destFilePath, { mode });
+    await pipeline(res.body, destFileStream);
+    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
     });
 }
 
-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/
 
     // `.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"));
 
     try {
-        return await scope(path.join(tempDir, "file"));
+        return await scope(tempDir);
     } finally {
         // We are good citizens :D
         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);
         } else {
             log.error(`Failed to rename the file ${src} -> ${dest}`, err);
+            throw err;
         }
     }
 }