aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-02-13 22:41:37 +0000
committerGitHub <[email protected]>2020-02-13 22:41:37 +0000
commita19f52f9ae1634fa2267c3bc7647a0d47b6014ac (patch)
treef0e39e825bc20d915c7169eaecf2fd2a74d50216
parent1f897d1c6eaa7f0251bb47a5d2d1894cd8e82d54 (diff)
parent574dc11a2fed943bc40e338b22c5b12bef66e768 (diff)
Merge #3116
3116: vscode: added error handling to download file streams r=matklad a=Veetaha As a followup for #3092 `ts-nested-error` is mine, it is just [one file worth nothing](https://github.com/Veetaha/ts-nested-error/blob/master/src/nested-error.ts), but let's us inspect original errors Co-authored-by: Veetaha <[email protected]>
-rw-r--r--editors/code/src/installation/download_file.ts33
-rw-r--r--editors/code/src/installation/language_server.ts2
2 files changed, 22 insertions, 13 deletions
diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts
index f1f9f4a25..d154f4816 100644
--- a/editors/code/src/installation/download_file.ts
+++ b/editors/code/src/installation/download_file.ts
@@ -1,7 +1,11 @@
1import fetch from "node-fetch"; 1import fetch from "node-fetch";
2import * as fs from "fs"; 2import * as fs from "fs";
3import * as stream from "stream";
4import * as util from "util";
3import { strict as assert } from "assert"; 5import { strict as assert } from "assert";
4 6
7const pipeline = util.promisify(stream.pipeline);
8
5/** 9/**
6 * Downloads file from `url` and stores it at `destFilePath` with `destFilePermissions`. 10 * Downloads file from `url` and stores it at `destFilePath` with `destFilePermissions`.
7 * `onProgress` callback is called on recieveing each chunk of bytes 11 * `onProgress` callback is called on recieveing each chunk of bytes
@@ -20,25 +24,28 @@ export async function downloadFile(
20 console.log("Error", res.status, "while downloading file from", url); 24 console.log("Error", res.status, "while downloading file from", url);
21 console.dir({ body: await res.text(), headers: res.headers }, { depth: 3 }); 25 console.dir({ body: await res.text(), headers: res.headers }, { depth: 3 });
22 26
23 throw new Error(`Got response ${res.status} when trying to download a file`); 27 throw new Error(`Got response ${res.status} when trying to download a file.`);
24 } 28 }
25 29
26 const totalBytes = Number(res.headers.get('content-length')); 30 const totalBytes = Number(res.headers.get('content-length'));
27 assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol"); 31 assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol");
28 32
33 console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath);
34
29 let readBytes = 0; 35 let readBytes = 0;
36 res.body.on("data", (chunk: Buffer) => {
37 readBytes += chunk.length;
38 onProgress(readBytes, totalBytes);
39 });
30 40
31 console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); 41 const destFileStream = fs.createWriteStream(destFilePath, { mode: destFilePermissions });
42
43 await pipeline(res.body, destFileStream);
44 return new Promise<void>(resolve => {
45 destFileStream.on("close", resolve);
46 destFileStream.destroy();
32 47
33 return new Promise<void>((resolve, reject) => res.body 48 // Details on workaround: https://github.com/rust-analyzer/rust-analyzer/pull/3092#discussion_r378191131
34 .on("data", (chunk: Buffer) => { 49 // Issue at nodejs repo: https://github.com/nodejs/node/issues/31776
35 readBytes += chunk.length; 50 });
36 onProgress(readBytes, totalBytes);
37 })
38 .on("error", reject)
39 .pipe(fs
40 .createWriteStream(destFilePath, { mode: destFilePermissions })
41 .on("close", resolve)
42 )
43 );
44} 51}
diff --git a/editors/code/src/installation/language_server.ts b/editors/code/src/installation/language_server.ts
index 52c5cbe7d..4797c3f01 100644
--- a/editors/code/src/installation/language_server.ts
+++ b/editors/code/src/installation/language_server.ts
@@ -104,6 +104,8 @@ export async function ensureLanguageServerBinary(
104 `GitHub repository: ${err.message}` 104 `GitHub repository: ${err.message}`
105 ); 105 );
106 106
107 console.error(err);
108
107 dns.resolve('example.com').then( 109 dns.resolve('example.com').then(
108 addrs => console.log("DNS resolution for example.com was successful", addrs), 110 addrs => console.log("DNS resolution for example.com was successful", addrs),
109 err => { 111 err => {