download icon indicating copy to clipboard operation
download copied to clipboard

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

Open SrBrahma opened this issue 3 years ago • 4 comments

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

SrBrahma avatar May 06 '21 07:05 SrBrahma

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/[email protected]

vs

download: 359.5 KB https://bundlephobia.com/[email protected]

aminya avatar May 10 '21 04:05 aminya

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

SrBrahma avatar May 10 '21 07:05 SrBrahma

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)
      })
    })
  }

fawazahmed0 avatar Jul 27 '22 23:07 fawazahmed0

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

guoyunhe avatar Jan 29 '23 07:01 guoyunhe