Skip to content

Commit a19f52f

Browse files
bors[bot]Veetaha
and
Veetaha
authored
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]>
2 parents 1f897d1 + 574dc11 commit a19f52f

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import fetch from "node-fetch";
22
import * as fs from "fs";
3+
import * as stream from "stream";
4+
import * as util from "util";
35
import { strict as assert } from "assert";
46

7+
const pipeline = util.promisify(stream.pipeline);
8+
59
/**
610
* Downloads file from `url` and stores it at `destFilePath` with `destFilePermissions`.
711
* `onProgress` callback is called on recieveing each chunk of bytes
@@ -20,25 +24,28 @@ export async function downloadFile(
2024
console.log("Error", res.status, "while downloading file from", url);
2125
console.dir({ body: await res.text(), headers: res.headers }, { depth: 3 });
2226

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.`);
2428
}
2529

2630
const totalBytes = Number(res.headers.get('content-length'));
2731
assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol");
2832

33+
console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath);
34+
2935
let readBytes = 0;
36+
res.body.on("data", (chunk: Buffer) => {
37+
readBytes += chunk.length;
38+
onProgress(readBytes, totalBytes);
39+
});
3040

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();
3247

33-
return new Promise<void>((resolve, reject) => res.body
34-
.on("data", (chunk: Buffer) => {
35-
readBytes += chunk.length;
36-
onProgress(readBytes, totalBytes);
37-
})
38-
.on("error", reject)
39-
.pipe(fs
40-
.createWriteStream(destFilePath, { mode: destFilePermissions })
41-
.on("close", resolve)
42-
)
43-
);
48+
// Details on workaround: https://github.com/rust-analyzer/rust-analyzer/pull/3092#discussion_r378191131
49+
// Issue at nodejs repo: https://github.com/nodejs/node/issues/31776
50+
});
4451
}

editors/code/src/installation/language_server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ export async function ensureLanguageServerBinary(
104104
`GitHub repository: ${err.message}`
105105
);
106106

107+
console.error(err);
108+
107109
dns.resolve('example.com').then(
108110
addrs => console.log("DNS resolution for example.com was successful", addrs),
109111
err => {

0 commit comments

Comments
 (0)