diff options
Diffstat (limited to 'editors/code/src/net.ts')
-rw-r--r-- | editors/code/src/net.ts | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/editors/code/src/net.ts b/editors/code/src/net.ts index 747c02db9..722dab756 100644 --- a/editors/code/src/net.ts +++ b/editors/code/src/net.ts | |||
@@ -91,7 +91,7 @@ export async function download(opts: DownloadOpts) { | |||
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 randomHex = crypto.randomBytes(5).toString("hex"); | 93 | const randomHex = crypto.randomBytes(5).toString("hex"); |
94 | const rawDest = path.parse(opts.dest.path); | 94 | const rawDest = path.parse(opts.dest.fsPath); |
95 | const tempFilePath = vscode.Uri.joinPath(vscode.Uri.file(rawDest.dir), `${rawDest.name}${randomHex}`); | 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( |
@@ -116,7 +116,7 @@ export async function download(opts: DownloadOpts) { | |||
116 | } | 116 | } |
117 | ); | 117 | ); |
118 | 118 | ||
119 | await vscode.workspace.fs.rename(tempFilePath, opts.dest); | 119 | await vscode.workspace.fs.rename(tempFilePath, opts.dest, { overwrite: true }); |
120 | } | 120 | } |
121 | 121 | ||
122 | async function downloadFile( | 122 | async function downloadFile( |
@@ -127,17 +127,19 @@ async function downloadFile( | |||
127 | httpProxy: string | null | undefined, | 127 | httpProxy: string | null | undefined, |
128 | onProgress: (readBytes: number, totalBytes: number) => void | 128 | onProgress: (readBytes: number, totalBytes: number) => void |
129 | ): Promise<void> { | 129 | ): Promise<void> { |
130 | const urlString = url.toString(); | ||
131 | |||
130 | const res = await (() => { | 132 | const res = await (() => { |
131 | if (httpProxy) { | 133 | if (httpProxy) { |
132 | log.debug(`Downloading ${url.path} via proxy: ${httpProxy}`); | 134 | log.debug(`Downloading ${urlString} via proxy: ${httpProxy}`); |
133 | return fetch(url.path, { agent: new HttpsProxyAgent(httpProxy) }); | 135 | return fetch(urlString, { agent: new HttpsProxyAgent(httpProxy) }); |
134 | } | 136 | } |
135 | 137 | ||
136 | return fetch(url.path); | 138 | return fetch(urlString); |
137 | })(); | 139 | })(); |
138 | 140 | ||
139 | if (!res.ok) { | 141 | if (!res.ok) { |
140 | log.error("Error", res.status, "while downloading file from", url.path); | 142 | log.error("Error", res.status, "while downloading file from", urlString); |
141 | log.error({ body: await res.text(), headers: res.headers }); | 143 | log.error({ body: await res.text(), headers: res.headers }); |
142 | 144 | ||
143 | throw new Error(`Got response ${res.status} when trying to download a file.`); | 145 | throw new Error(`Got response ${res.status} when trying to download a file.`); |
@@ -146,7 +148,7 @@ async function downloadFile( | |||
146 | const totalBytes = Number(res.headers.get('content-length')); | 148 | const totalBytes = Number(res.headers.get('content-length')); |
147 | assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol"); | 149 | assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol"); |
148 | 150 | ||
149 | log.debug("Downloading file of", totalBytes, "bytes size from", url.path, "to", destFilePath.path); | 151 | log.debug("Downloading file of", totalBytes, "bytes size from", urlString, "to", destFilePath.fsPath); |
150 | 152 | ||
151 | let readBytes = 0; | 153 | let readBytes = 0; |
152 | res.body.on("data", (chunk: Buffer) => { | 154 | res.body.on("data", (chunk: Buffer) => { |
@@ -154,7 +156,7 @@ async function downloadFile( | |||
154 | onProgress(readBytes, totalBytes); | 156 | onProgress(readBytes, totalBytes); |
155 | }); | 157 | }); |
156 | 158 | ||
157 | const destFileStream = fs.createWriteStream(destFilePath.path, { mode }); | 159 | const destFileStream = fs.createWriteStream(destFilePath.fsPath, { mode }); |
158 | const srcStream = gunzip ? res.body.pipe(zlib.createGunzip()) : res.body; | 160 | const srcStream = gunzip ? res.body.pipe(zlib.createGunzip()) : res.body; |
159 | 161 | ||
160 | await pipeline(srcStream, destFileStream); | 162 | await pipeline(srcStream, destFileStream); |