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 <gerzoh1@gmail.com>
This commit is contained in:
commit
a19f52f9ae
@ -1,7 +1,11 @@
|
||||
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";
|
||||
|
||||
const pipeline = util.promisify(stream.pipeline);
|
||||
|
||||
/**
|
||||
* Downloads file from `url` and stores it at `destFilePath` with `destFilePermissions`.
|
||||
* `onProgress` callback is called on recieveing each chunk of bytes
|
||||
@ -20,25 +24,28 @@ export async function downloadFile(
|
||||
console.log("Error", res.status, "while downloading file from", url);
|
||||
console.dir({ body: await res.text(), headers: res.headers }, { depth: 3 });
|
||||
|
||||
throw new Error(`Got response ${res.status} when trying to download a file`);
|
||||
throw new Error(`Got response ${res.status} when trying to download a file.`);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return new Promise<void>((resolve, reject) => res.body
|
||||
.on("data", (chunk: Buffer) => {
|
||||
readBytes += chunk.length;
|
||||
onProgress(readBytes, totalBytes);
|
||||
})
|
||||
.on("error", reject)
|
||||
.pipe(fs
|
||||
.createWriteStream(destFilePath, { mode: destFilePermissions })
|
||||
.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);
|
||||
return new Promise<void>(resolve => {
|
||||
destFileStream.on("close", resolve);
|
||||
destFileStream.destroy();
|
||||
|
||||
// Details on workaround: https://github.com/rust-analyzer/rust-analyzer/pull/3092#discussion_r378191131
|
||||
// Issue at nodejs repo: https://github.com/nodejs/node/issues/31776
|
||||
});
|
||||
}
|
||||
|
@ -104,6 +104,8 @@ export async function ensureLanguageServerBinary(
|
||||
`GitHub repository: ${err.message}`
|
||||
);
|
||||
|
||||
console.error(err);
|
||||
|
||||
dns.resolve('example.com').then(
|
||||
addrs => console.log("DNS resolution for example.com was successful", addrs),
|
||||
err => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user