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.ts30
1 files changed, 15 insertions, 15 deletions
diff --git a/editors/code/src/net.ts b/editors/code/src/net.ts
index 07ebc615c..747c02db9 100644
--- a/editors/code/src/net.ts
+++ b/editors/code/src/net.ts
@@ -73,14 +73,14 @@ export interface GithubRelease {
73 assets: Array<{ 73 assets: Array<{
74 name: string; 74 name: string;
75 // eslint-disable-next-line camelcase 75 // eslint-disable-next-line camelcase
76 browser_download_url: string; 76 browser_download_url: vscode.Uri;
77 }>; 77 }>;
78} 78}
79 79
80interface DownloadOpts { 80interface DownloadOpts {
81 progressTitle: string; 81 progressTitle: string;
82 url: string; 82 url: vscode.Uri;
83 dest: string; 83 dest: vscode.Uri;
84 mode?: number; 84 mode?: number;
85 gunzip?: boolean; 85 gunzip?: boolean;
86 httpProxy?: string; 86 httpProxy?: string;
@@ -90,9 +90,9 @@ export async function download(opts: DownloadOpts) {
90 // Put artifact into a temporary file (in the same dir for simplicity) 90 // Put artifact into a temporary file (in the same dir for simplicity)
91 // to prevent partially downloaded files when user kills vscode 91 // to prevent partially downloaded files when user kills vscode
92 // This also avoids overwriting running executables 92 // This also avoids overwriting running executables
93 const dest = path.parse(opts.dest);
94 const randomHex = crypto.randomBytes(5).toString("hex"); 93 const randomHex = crypto.randomBytes(5).toString("hex");
95 const tempFile = path.join(dest.dir, `${dest.name}${randomHex}`); 94 const rawDest = path.parse(opts.dest.path);
95 const tempFilePath = vscode.Uri.joinPath(vscode.Uri.file(rawDest.dir), `${rawDest.name}${randomHex}`);
96 96
97 await vscode.window.withProgress( 97 await vscode.window.withProgress(
98 { 98 {
@@ -102,7 +102,7 @@ export async function download(opts: DownloadOpts) {
102 }, 102 },
103 async (progress, _cancellationToken) => { 103 async (progress, _cancellationToken) => {
104 let lastPercentage = 0; 104 let lastPercentage = 0;
105 await downloadFile(opts.url, tempFile, opts.mode, !!opts.gunzip, opts.httpProxy, (readBytes, totalBytes) => { 105 await downloadFile(opts.url, tempFilePath, opts.mode, !!opts.gunzip, opts.httpProxy, (readBytes, totalBytes) => {
106 const newPercentage = Math.round((readBytes / totalBytes) * 100); 106 const newPercentage = Math.round((readBytes / totalBytes) * 100);
107 if (newPercentage !== lastPercentage) { 107 if (newPercentage !== lastPercentage) {
108 progress.report({ 108 progress.report({
@@ -116,12 +116,12 @@ export async function download(opts: DownloadOpts) {
116 } 116 }
117 ); 117 );
118 118
119 await fs.promises.rename(tempFile, opts.dest); 119 await vscode.workspace.fs.rename(tempFilePath, opts.dest);
120} 120}
121 121
122async function downloadFile( 122async function downloadFile(
123 url: string, 123 url: vscode.Uri,
124 destFilePath: fs.PathLike, 124 destFilePath: vscode.Uri,
125 mode: number | undefined, 125 mode: number | undefined,
126 gunzip: boolean, 126 gunzip: boolean,
127 httpProxy: string | null | undefined, 127 httpProxy: string | null | undefined,
@@ -129,15 +129,15 @@ async function downloadFile(
129): Promise<void> { 129): Promise<void> {
130 const res = await (() => { 130 const res = await (() => {
131 if (httpProxy) { 131 if (httpProxy) {
132 log.debug(`Downloading ${url} via proxy: ${httpProxy}`); 132 log.debug(`Downloading ${url.path} via proxy: ${httpProxy}`);
133 return fetch(url, { agent: new HttpsProxyAgent(httpProxy) }); 133 return fetch(url.path, { agent: new HttpsProxyAgent(httpProxy) });
134 } 134 }
135 135
136 return fetch(url); 136 return fetch(url.path);
137 })(); 137 })();
138 138
139 if (!res.ok) { 139 if (!res.ok) {
140 log.error("Error", res.status, "while downloading file from", url); 140 log.error("Error", res.status, "while downloading file from", url.path);
141 log.error({ body: await res.text(), headers: res.headers }); 141 log.error({ body: await res.text(), headers: res.headers });
142 142
143 throw new Error(`Got response ${res.status} when trying to download a file.`); 143 throw new Error(`Got response ${res.status} when trying to download a file.`);
@@ -146,7 +146,7 @@ async function downloadFile(
146 const totalBytes = Number(res.headers.get('content-length')); 146 const totalBytes = Number(res.headers.get('content-length'));
147 assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol"); 147 assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol");
148 148
149 log.debug("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); 149 log.debug("Downloading file of", totalBytes, "bytes size from", url.path, "to", destFilePath.path);
150 150
151 let readBytes = 0; 151 let readBytes = 0;
152 res.body.on("data", (chunk: Buffer) => { 152 res.body.on("data", (chunk: Buffer) => {
@@ -154,7 +154,7 @@ async function downloadFile(
154 onProgress(readBytes, totalBytes); 154 onProgress(readBytes, totalBytes);
155 }); 155 });
156 156
157 const destFileStream = fs.createWriteStream(destFilePath, { mode }); 157 const destFileStream = fs.createWriteStream(destFilePath.path, { mode });
158 const srcStream = gunzip ? res.body.pipe(zlib.createGunzip()) : res.body; 158 const srcStream = gunzip ? res.body.pipe(zlib.createGunzip()) : res.body;
159 159
160 await pipeline(srcStream, destFileStream); 160 await pipeline(srcStream, destFileStream);