aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/net.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/net.ts')
-rw-r--r--editors/code/src/net.ts21
1 files changed, 12 insertions, 9 deletions
diff --git a/editors/code/src/net.ts b/editors/code/src/net.ts
index 866092882..681eaa9c9 100644
--- a/editors/code/src/net.ts
+++ b/editors/code/src/net.ts
@@ -1,8 +1,12 @@
1import fetch from "node-fetch"; 1// Replace with `import fetch from "node-fetch"` once this is fixed in rollup:
2// https://github.com/rollup/plugins/issues/491
3const fetch = require("node-fetch") as typeof import("node-fetch")["default"];
4
2import * as vscode from "vscode"; 5import * as vscode from "vscode";
3import * as stream from "stream"; 6import * as stream from "stream";
4import * as crypto from "crypto"; 7import * as crypto from "crypto";
5import * as fs from "fs"; 8import * as fs from "fs";
9import * as zlib from "zlib";
6import * as util from "util"; 10import * as util from "util";
7import * as path from "path"; 11import * as path from "path";
8import { log, assert } from "./util"; 12import { log, assert } from "./util";
@@ -65,6 +69,7 @@ interface DownloadOpts {
65 url: string; 69 url: string;
66 dest: string; 70 dest: string;
67 mode?: number; 71 mode?: number;
72 gunzip?: boolean;
68} 73}
69 74
70export async function download(opts: DownloadOpts) { 75export async function download(opts: DownloadOpts) {
@@ -82,7 +87,7 @@ export async function download(opts: DownloadOpts) {
82 }, 87 },
83 async (progress, _cancellationToken) => { 88 async (progress, _cancellationToken) => {
84 let lastPercentage = 0; 89 let lastPercentage = 0;
85 await downloadFile(opts.url, tempFile, opts.mode, (readBytes, totalBytes) => { 90 await downloadFile(opts.url, tempFile, opts.mode, !!opts.gunzip, (readBytes, totalBytes) => {
86 const newPercentage = (readBytes / totalBytes) * 100; 91 const newPercentage = (readBytes / totalBytes) * 100;
87 progress.report({ 92 progress.report({
88 message: newPercentage.toFixed(0) + "%", 93 message: newPercentage.toFixed(0) + "%",
@@ -97,16 +102,11 @@ export async function download(opts: DownloadOpts) {
97 await fs.promises.rename(tempFile, opts.dest); 102 await fs.promises.rename(tempFile, opts.dest);
98} 103}
99 104
100/**
101 * Downloads file from `url` and stores it at `destFilePath` with `mode` (unix permissions).
102 * `onProgress` callback is called on recieveing each chunk of bytes
103 * to track the progress of downloading, it gets the already read and total
104 * amount of bytes to read as its parameters.
105 */
106async function downloadFile( 105async function downloadFile(
107 url: string, 106 url: string,
108 destFilePath: fs.PathLike, 107 destFilePath: fs.PathLike,
109 mode: number | undefined, 108 mode: number | undefined,
109 gunzip: boolean,
110 onProgress: (readBytes: number, totalBytes: number) => void 110 onProgress: (readBytes: number, totalBytes: number) => void
111): Promise<void> { 111): Promise<void> {
112 const res = await fetch(url); 112 const res = await fetch(url);
@@ -130,7 +130,10 @@ async function downloadFile(
130 }); 130 });
131 131
132 const destFileStream = fs.createWriteStream(destFilePath, { mode }); 132 const destFileStream = fs.createWriteStream(destFilePath, { mode });
133 await pipeline(res.body, destFileStream); 133 const srcStream = gunzip ? res.body.pipe(zlib.createGunzip()) : res.body;
134
135 await pipeline(srcStream, destFileStream);
136
134 await new Promise<void>(resolve => { 137 await new Promise<void>(resolve => {
135 destFileStream.on("close", resolve); 138 destFileStream.on("close", resolve);
136 destFileStream.destroy(); 139 destFileStream.destroy();