Open SrBrahma opened 3 years ago
Thanks! I was looking for a lightweight alternative. The size difference makes node-downloader-helper
a very good choice. It is 25 times smaller!
node-downloader-helper: 14.2 KB https://bundlephobia.com/result?p=node-downloader-helper@1.0.18
vs
download: 359.5 KB https://bundlephobia.com/result?p=download@8.0.0
Didn't know about it, good to know! Thanks for the info!
This function seems to work great for me:
const fs = require('fs')
const https = require('https')
// https://futurestud.io/tutorials/node-js-how-to-download-a-file
async function downloadFile (url, targetFile) {
return await new Promise((resolve, reject) => {
https.get(url, response => {
const code = response.statusCode ?? 0
if (code >= 400) {
return reject(new Error(response.statusMessage))
}
// handle redirects
if (code > 300 && code < 400 && !!response.headers.location) {
return downloadFile(response.headers.location, targetFile)
}
// save the file to disk
const fileWriter = fs
.createWriteStream(targetFile)
.on('finish', () => {
resolve({})
})
response.pipe(fileWriter)
}).on('error', error => {
reject(error)
})
})
}
I made an alternative package with zip/tar.gz extraction https://github.com/guoyunhe/downloader
I ain't the developer of it, but I think it's really good and a nice alternative for the
download
package.https://github.com/hgouveia/node-downloader-helper