diff options
Diffstat (limited to 'editors/code')
-rw-r--r-- | editors/code/src/installation/download_file.ts | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts index b51602ef9..8a0766c66 100644 --- a/editors/code/src/installation/download_file.ts +++ b/editors/code/src/installation/download_file.ts | |||
@@ -13,22 +13,28 @@ export async function downloadFile( | |||
13 | destFilePath: fs.PathLike, | 13 | destFilePath: fs.PathLike, |
14 | onProgress: (readBytes: number, totalBytes: number) => void | 14 | onProgress: (readBytes: number, totalBytes: number) => void |
15 | ): Promise<void> { | 15 | ): Promise<void> { |
16 | const response = await fetch(url); | 16 | const res = await fetch(url); |
17 | 17 | ||
18 | const totalBytes = Number(response.headers.get('content-length')); | 18 | if (!res.ok) { |
19 | console.log("Error", res.status, "while downloading file from", url); | ||
20 | console.dir({ body: await res.text(), headers: res.headers }, { depth: 3 }); | ||
21 | |||
22 | throw new Error(`Got response ${res.status} when trying to download a file`); | ||
23 | } | ||
24 | |||
25 | const totalBytes = Number(res.headers.get('content-length')); | ||
19 | assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol"); | 26 | assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol"); |
20 | 27 | ||
21 | let readBytes = 0; | 28 | let readBytes = 0; |
22 | 29 | ||
23 | console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); | 30 | console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); |
24 | 31 | ||
25 | return new Promise<void>((resolve, reject) => response.body | 32 | return new Promise<void>((resolve, reject) => res.body |
26 | .on("data", (chunk: Buffer) => { | 33 | .on("data", (chunk: Buffer) => { |
27 | readBytes += chunk.length; | 34 | readBytes += chunk.length; |
28 | onProgress(readBytes, totalBytes); | 35 | onProgress(readBytes, totalBytes); |
29 | }) | 36 | }) |
30 | .on("end", resolve) | ||
31 | .on("error", reject) | 37 | .on("error", reject) |
32 | .pipe(fs.createWriteStream(destFilePath)) | 38 | .pipe(fs.createWriteStream(destFilePath).on("close", resolve)) |
33 | ); | 39 | ); |
34 | } | 40 | } |