11'use strict'
22const crypto = require ( 'crypto' )
3- const axios = require ( 'axios' )
4- const MemoryStream = require ( 'memory-stream' )
3+ const { Readable } = require ( 'node:stream' )
54const zlib = require ( 'zlib' )
65const tar = require ( 'tar' )
76const fs = require ( 'fs' )
@@ -19,15 +18,18 @@ class Downloader {
1918 let length = 0
2019 let done = 0
2120 let lastPercent = 0
22- axios
23- . get ( url , { responseType : 'stream' } )
21+ fetch ( url )
2422 . then ( function ( response ) {
25- length = parseInt ( response . headers [ 'content-length' ] )
26- if ( typeof length !== 'number' ) {
23+ if ( ! response . ok ) {
24+ throw new Error ( response . statusText || 'Request failed' )
25+ }
26+ length = parseInt ( response . headers . get ( 'content-length' ) )
27+ if ( Number . isNaN ( length ) ) {
2728 length = 0
2829 }
2930
30- response . data . on ( 'data' , function ( chunk ) {
31+ const readable = Readable . fromWeb ( response . body )
32+ readable . on ( 'data' , function ( chunk ) {
3133 if ( shasum ) {
3234 shasum . update ( chunk )
3335 }
@@ -42,7 +44,10 @@ class Downloader {
4244 }
4345 } )
4446
45- response . data . pipe ( stream )
47+ readable . pipe ( stream )
48+ readable . on ( 'error' , function ( err ) {
49+ reject ( err )
50+ } )
4651 } )
4752 . catch ( function ( err ) {
4853 reject ( err )
@@ -58,9 +63,11 @@ class Downloader {
5863 } )
5964 }
6065 async downloadString ( url ) {
61- const result = new MemoryStream ( )
62- await this . downloadToStream ( url , result )
63- return result . toString ( )
66+ const response = await fetch ( url )
67+ if ( ! response . ok ) {
68+ throw new Error ( response . statusText || 'Request failed' )
69+ }
70+ return response . text ( )
6471 }
6572 async downloadFile ( url , options ) {
6673 if ( typeof options === 'string' ) {
0 commit comments