From a3febc1c574b9f99120b0e5bc860bf2458d0b919 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Wed, 12 Feb 2020 21:40:35 +0200 Subject: vscode: switched to stream.pipeline with .on(close) workaround --- editors/code/src/installation/download_file.ts | 36 ++++++++++++-------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'editors') diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts index 71700ec8a..b31d2a736 100644 --- a/editors/code/src/installation/download_file.ts +++ b/editors/code/src/installation/download_file.ts @@ -1,8 +1,12 @@ 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"; import { NestedError } from "ts-nested-error"; +const pipeline = util.promisify(stream.pipeline); + class DownloadFileError extends NestedError {} /** @@ -29,25 +33,19 @@ export async function downloadFile( 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); - // Here reject() may be called 2 times. As per ECMAScript standard, 2-d call is ignored - // https://tc39.es/ecma262/#sec-promise-reject-functions - - return new Promise((resolve, reject) => res.body - .on("data", (chunk: Buffer) => { - readBytes += chunk.length; - onProgress(readBytes, totalBytes); - }) - .on("error", err => reject( - new DownloadFileError(`Read-stream error, read bytes: ${readBytes}`, err) - )) - .pipe(fs.createWriteStream(destFilePath, { mode: destFilePermissions })) - .on("error", err => reject( - new DownloadFileError(`Write-stream error, read bytes: ${readBytes}`, err) - )) - .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).catch(DownloadFileError.rethrow("Piping file error")); + return new Promise(resolve => { + destFileStream.on("close", resolve); // details on workaround: https://github.com/rust-analyzer/rust-analyzer/pull/3092#discussion_r378191131 + destFileStream.destroy(); + }); } -- cgit v1.2.3