diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts index f1f9f4a256..d154f4816c 100644 --- a/editors/code/src/installation/download_file.ts +++ b/editors/code/src/installation/download_file.ts @@ -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((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(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 + }); } diff --git a/editors/code/src/installation/language_server.ts b/editors/code/src/installation/language_server.ts index 52c5cbe7d6..4797c3f01a 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( `GitHub repository: ${err.message}` ); + console.error(err); + dns.resolve('example.com').then( addrs => console.log("DNS resolution for example.com was successful", addrs), err => {