-
Notifications
You must be signed in to change notification settings - Fork 1.9k
vscode: added error handling to download file streams #3116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
36dc3ed
a3febc1
da6ae3b
574dc11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
|
|
||
| console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); | ||
|
|
||
| let readBytes = 0; | ||
| res.body.on("data", (chunk: Buffer) => { | ||
| readBytes += chunk.length; | ||
| onProgress(readBytes, totalBytes); | ||
| }); | ||
|
|
||
| console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); | ||
| const destFileStream = fs.createWriteStream(destFilePath, { mode: destFilePermissions }); | ||
|
|
||
| await pipeline(res.body, destFileStream); | ||
| return new Promise<void>(resolve => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need a Promise here at all? I would imagine that the following would work (with some exception safety added just in case): try {
await pipeline(res.body, destFileStream);
return;
} finally {
destFileStream.destory()
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately it doesn't work on my laptop on windows ;( We still need to await the close event... Please see that discussion you originally referred to.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, sorry, I've missed the last bit of the discussion :( Yeah, I guess the current code is what we should do then.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the way, regarding
|
||
| destFileStream.on("close", resolve); | ||
| destFileStream.destroy(); | ||
|
|
||
| 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) | ||
| ) | ||
| ); | ||
| // 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 | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,6 +104,8 @@ export async function ensureLanguageServerBinary( | |
| `GitHub repository: ${err.message}` | ||
| ); | ||
|
|
||
| console.error(err); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not 100% sure we need to write the error out since the message is shown in vscode but I don't feel strongly about it. If you think it makes sense then that's fine by me.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kjeremy |
||
|
|
||
| dns.resolve('example.com').then( | ||
| addrs => console.log("DNS resolution for example.com was successful", addrs), | ||
| err => { | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove the extra line of white space.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do!