Skip to content

Commit 0b3a840

Browse files
committed
feat!: replace axios with fetch
1 parent c429d9e commit 0b3a840

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

lib/downloader.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22
const crypto = require('crypto')
3-
const axios = require('axios')
4-
const MemoryStream = require('memory-stream')
3+
const { Readable } = require('node:stream')
54
const zlib = require('zlib')
65
const tar = require('tar')
76
const 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') {

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,15 @@
4141
"node": "^20.17.0 || >=22.9.0"
4242
},
4343
"dependencies": {
44-
"axios": "^1.13.4",
4544
"debug": "^4",
4645
"fs-extra": "^11.3.3",
47-
"memory-stream": "^1.0.0",
4846
"node-api-headers": "^1.8.0",
4947
"npmlog": "^6.0.2",
5048
"rc": "^1.2.7",
5149
"semver": "^7.5.4",
5250
"tar": "^7.5.6",
5351
"url-join": "^4.0.1",
54-
"which": "^2.0.2",
52+
"which": "^6.0.0",
5553
"yargs": "^17.7.2"
5654
},
5755
"devDependencies": {

0 commit comments

Comments
 (0)