-
-
Notifications
You must be signed in to change notification settings - Fork 239
ref: Use undici to download binary #2993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,12 +6,10 @@ const fs = require('fs'); | |
| const os = require('os'); | ||
| const path = require('path'); | ||
| const crypto = require('crypto'); | ||
| const zlib = require('zlib'); | ||
| const stream = require('stream'); | ||
| const process = require('process'); | ||
|
|
||
| const fetch = require('node-fetch'); | ||
| const HttpsProxyAgent = require('https-proxy-agent'); | ||
| const { ProxyAgent, fetch } = require('undici'); | ||
| const ProgressBar = require('progress'); | ||
| const Proxy = require('proxy-from-env'); | ||
| const which = require('which'); | ||
|
|
@@ -89,7 +87,7 @@ function getDownloadUrl(platform, arch) { | |
| } | ||
|
|
||
| function createProgressBar(name, total) { | ||
| const incorrectTotal = typeof total !== 'number' || Number.isNaN(total); | ||
| const incorrectTotal = typeof total !== 'number' || Number.isNaN(total) || total <= 0; | ||
|
|
||
| if (incorrectTotal || !shouldRenderProgressBar()) { | ||
| return { | ||
|
|
@@ -220,7 +218,7 @@ async function downloadBinary() { | |
| } | ||
|
|
||
| const proxyUrl = Proxy.getProxyForUrl(downloadUrl); | ||
| const agent = proxyUrl ? new HttpsProxyAgent(proxyUrl) : null; | ||
| const dispatcher = proxyUrl ? new ProxyAgent(proxyUrl) : undefined; | ||
|
|
||
| logger.log(`Downloading from ${downloadUrl}`); | ||
|
|
||
|
|
@@ -231,12 +229,8 @@ async function downloadBinary() { | |
| let response; | ||
| try { | ||
| response = await fetch(downloadUrl, { | ||
| agent, | ||
| compress: false, | ||
| headers: { | ||
| 'accept-encoding': 'gzip, deflate, br', | ||
| }, | ||
| redirect: 'follow', | ||
| dispatcher, | ||
| }); | ||
| } catch (error) { | ||
| let errorMsg = `Unable to download sentry-cli binary from ${downloadUrl}.\nError message: ${error.message}`; | ||
|
|
@@ -254,39 +248,38 @@ async function downloadBinary() { | |
| throw new Error(errorMsg); | ||
| } | ||
|
|
||
| const contentEncoding = response.headers.get('content-encoding'); | ||
| let decompressor; | ||
| if (/\bgzip\b/.test(contentEncoding)) { | ||
| decompressor = zlib.createGunzip(); | ||
| } else if (/\bdeflate\b/.test(contentEncoding)) { | ||
| decompressor = zlib.createInflate(); | ||
| } else if (/\bbr\b/.test(contentEncoding)) { | ||
| decompressor = zlib.createBrotliDecompress(); | ||
| } else { | ||
| decompressor = new stream.PassThrough(); | ||
| } | ||
| const name = downloadUrl.match(/.*\/(.*?)$/)[1]; | ||
| let downloadedBytes = 0; | ||
| const totalBytes = parseInt(response.headers.get('content-length'), 10); | ||
|
|
||
| // Note: content-length might not be available if response was compressed, | ||
| // as native fetch decompresses transparently | ||
| const contentLength = response.headers.get('content-length'); | ||
| const totalBytes = contentLength ? parseInt(contentLength, 10) : 0; | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const progressBar = createProgressBar(name, totalBytes); | ||
| const tempPath = getTempFile(cachedPath); | ||
| fs.mkdirSync(path.dirname(tempPath), { recursive: true }); | ||
|
|
||
| await new Promise((resolve, reject) => { | ||
| response.body | ||
| // Convert Web ReadableStream to Node.js stream | ||
| const nodeStream = stream.Readable.fromWeb(response.body); | ||
|
|
||
| nodeStream | ||
| .on('error', (e) => reject(e)) | ||
| .on('data', (chunk) => { | ||
| downloadedBytes += chunk.length; | ||
| progressBar.tick(chunk.length); | ||
|
|
||
| if (!progressBar.complete) { | ||
| progressBar.tick(chunk.length); | ||
| } | ||
| }) | ||
| .pipe(decompressor) | ||
| .pipe(fs.createWriteStream(tempPath, { mode: '0755' })) | ||
| .on('error', (e) => reject(e)) | ||
| .on('close', () => { | ||
| if (downloadedBytes >= totalBytes) { | ||
| resolve(); | ||
| } else { | ||
| .on('finish', () => { | ||
| // Check if we have a total size to validate against | ||
| if (totalBytes > 0 && downloadedBytes < totalBytes) { | ||
| reject(new Error('connection interrupted')); | ||
| } else { | ||
| resolve(); | ||
| } | ||
|
Comment on lines
+278
to
283
This comment was marked as outdated.
Sorry, something went wrong.
Comment on lines
+277
to
283
This comment was marked as outdated.
Sorry, something went wrong.
Comment on lines
+277
to
283
This comment was marked as outdated.
Sorry, something went wrong. |
||
| }); | ||
|
Comment on lines
+277
to
284
This comment was marked as outdated.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Download integrity check unreliable with compressed responsesThe connection interrupted check compares Additional Locations (1) |
||
| }); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.