about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVeetaha <gerzoh1@gmail.com>2020-02-12 21:40:35 +0200
committerVeetaha <gerzoh1@gmail.com>2020-02-12 21:40:35 +0200
commita3febc1c574b9f99120b0e5bc860bf2458d0b919 (patch)
treef267e8562ffb50b28b4eee698d02d136a6ee6cfe
parent36dc3edb7a73e0d60ba2a3e589d1ae76c27f9d9d (diff)
downloadrust-a3febc1c574b9f99120b0e5bc860bf2458d0b919.tar.gz
rust-a3febc1c574b9f99120b0e5bc860bf2458d0b919.zip
vscode: switched to stream.pipeline with .on(close) workaround
-rw-r--r--editors/code/src/installation/download_file.ts36
1 files changed, 17 insertions, 19 deletions
diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts
index 71700ec8aec..b31d2a736d2 100644
--- a/editors/code/src/installation/download_file.ts
+++ b/editors/code/src/installation/download_file.ts
@@ -1,8 +1,12 @@
 import fetch from "node-fetch";
 import * as fs from "fs";
+import * as stream from "stream";
+import * as util from "util";
 import { strict as assert } from "assert";
 import { NestedError } from "ts-nested-error";
 
+const pipeline = util.promisify(stream.pipeline);
+
 class DownloadFileError extends NestedError {}
 
 /**
@@ -29,25 +33,19 @@ export async function downloadFile(
     const totalBytes = Number(res.headers.get('content-length'));
     assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol");
 
-    let readBytes = 0;
-
     console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath);
 
-    // Here reject() may be called 2 times. As per ECMAScript standard, 2-d call is ignored
-    // https://tc39.es/ecma262/#sec-promise-reject-functions
-
-    return new Promise<void>((resolve, reject) => res.body
-        .on("data", (chunk: Buffer) => {
-            readBytes += chunk.length;
-            onProgress(readBytes, totalBytes);
-        })
-        .on("error", err => reject(
-            new DownloadFileError(`Read-stream error, read bytes: ${readBytes}`, err)
-        ))
-        .pipe(fs.createWriteStream(destFilePath, { mode: destFilePermissions }))
-        .on("error", err => reject(
-            new DownloadFileError(`Write-stream error, read bytes: ${readBytes}`, err)
-        ))
-        .on("close", resolve)
-    );
+    let readBytes = 0;
+    res.body.on("data", (chunk: Buffer) => {
+        readBytes += chunk.length;
+        onProgress(readBytes, totalBytes);
+    });
+
+    const destFileStream = fs.createWriteStream(destFilePath, { mode: destFilePermissions });
+
+    await pipeline(res.body, destFileStream).catch(DownloadFileError.rethrow("Piping file error"));
+    return new Promise<void>(resolve => {
+        destFileStream.on("close", resolve); // details on workaround: https://github.com/rust-analyzer/rust-analyzer/pull/3092#discussion_r378191131
+        destFileStream.destroy();
+    });
 }