@@ -6,12 +6,10 @@ const fs = require('fs');
66const os = require ( 'os' ) ;
77const path = require ( 'path' ) ;
88const crypto = require ( 'crypto' ) ;
9- const zlib = require ( 'zlib' ) ;
109const stream = require ( 'stream' ) ;
1110const process = require ( 'process' ) ;
1211
13- const fetch = require ( 'node-fetch' ) ;
14- const HttpsProxyAgent = require ( 'https-proxy-agent' ) ;
12+ const { ProxyAgent, fetch } = require ( 'undici' ) ;
1513const ProgressBar = require ( 'progress' ) ;
1614const Proxy = require ( 'proxy-from-env' ) ;
1715const which = require ( 'which' ) ;
@@ -89,7 +87,7 @@ function getDownloadUrl(platform, arch) {
8987}
9088
9189function createProgressBar ( name , total ) {
92- const incorrectTotal = typeof total !== 'number' || Number . isNaN ( total ) ;
90+ const incorrectTotal = typeof total !== 'number' || Number . isNaN ( total ) || total <= 0 ;
9391
9492 if ( incorrectTotal || ! shouldRenderProgressBar ( ) ) {
9593 return {
@@ -220,7 +218,7 @@ async function downloadBinary() {
220218 }
221219
222220 const proxyUrl = Proxy . getProxyForUrl ( downloadUrl ) ;
223- const agent = proxyUrl ? new HttpsProxyAgent ( proxyUrl ) : null ;
221+ const dispatcher = proxyUrl ? new ProxyAgent ( proxyUrl ) : undefined ;
224222
225223 logger . log ( `Downloading from ${ downloadUrl } ` ) ;
226224
@@ -231,12 +229,8 @@ async function downloadBinary() {
231229 let response ;
232230 try {
233231 response = await fetch ( downloadUrl , {
234- agent,
235- compress : false ,
236- headers : {
237- 'accept-encoding' : 'gzip, deflate, br' ,
238- } ,
239232 redirect : 'follow' ,
233+ dispatcher,
240234 } ) ;
241235 } catch ( error ) {
242236 let errorMsg = `Unable to download sentry-cli binary from ${ downloadUrl } .\nError message: ${ error . message } ` ;
@@ -254,39 +248,38 @@ async function downloadBinary() {
254248 throw new Error ( errorMsg ) ;
255249 }
256250
257- const contentEncoding = response . headers . get ( 'content-encoding' ) ;
258- let decompressor ;
259- if ( / \b g z i p \b / . test ( contentEncoding ) ) {
260- decompressor = zlib . createGunzip ( ) ;
261- } else if ( / \b d e f l a t e \b / . test ( contentEncoding ) ) {
262- decompressor = zlib . createInflate ( ) ;
263- } else if ( / \b b r \b / . test ( contentEncoding ) ) {
264- decompressor = zlib . createBrotliDecompress ( ) ;
265- } else {
266- decompressor = new stream . PassThrough ( ) ;
267- }
268251 const name = downloadUrl . match ( / .* \/ ( .* ?) $ / ) [ 1 ] ;
269252 let downloadedBytes = 0 ;
270- const totalBytes = parseInt ( response . headers . get ( 'content-length' ) , 10 ) ;
253+
254+ // Note: content-length might not be available if response was compressed,
255+ // as native fetch decompresses transparently
256+ const contentLength = response . headers . get ( 'content-length' ) ;
257+ const totalBytes = contentLength ? parseInt ( contentLength , 10 ) : 0 ;
271258 const progressBar = createProgressBar ( name , totalBytes ) ;
272259 const tempPath = getTempFile ( cachedPath ) ;
273260 fs . mkdirSync ( path . dirname ( tempPath ) , { recursive : true } ) ;
274261
275262 await new Promise ( ( resolve , reject ) => {
276- response . body
263+ // Convert Web ReadableStream to Node.js stream
264+ const nodeStream = stream . Readable . fromWeb ( response . body ) ;
265+
266+ nodeStream
277267 . on ( 'error' , ( e ) => reject ( e ) )
278268 . on ( 'data' , ( chunk ) => {
279269 downloadedBytes += chunk . length ;
280- progressBar . tick ( chunk . length ) ;
270+
271+ if ( ! progressBar . complete ) {
272+ progressBar . tick ( chunk . length ) ;
273+ }
281274 } )
282- . pipe ( decompressor )
283275 . pipe ( fs . createWriteStream ( tempPath , { mode : '0755' } ) )
284276 . on ( 'error' , ( e ) => reject ( e ) )
285- . on ( 'close' , ( ) => {
286- if ( downloadedBytes >= totalBytes ) {
287- resolve ( ) ;
288- } else {
277+ . on ( 'finish' , ( ) => {
278+ // Check if we have a total size to validate against
279+ if ( totalBytes > 0 && downloadedBytes < totalBytes ) {
289280 reject ( new Error ( 'connection interrupted' ) ) ;
281+ } else {
282+ resolve ( ) ;
290283 }
291284 } ) ;
292285 } ) ;
0 commit comments