kevva / download

Download and extract files
MIT License
1.28k stars 200 forks source link

[node-downloader-helper] A very good alternative to this dead package #213

Open SrBrahma opened 3 years ago

SrBrahma commented 3 years ago

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

aminya commented 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

SrBrahma commented 3 years ago

Didn't know about it, good to know! Thanks for the info!

fawazahmed0 commented 2 years ago

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)
      })
    })
  }
guoyunhe commented 1 year ago

I made an alternative package with zip/tar.gz extraction https://github.com/guoyunhe/downloader