Socket
Socket
Sign inDemoInstall

nodejs-file-downloader

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nodejs-file-downloader

A file downloader for NodeJs


Version published
Weekly downloads
49K
increased by4.26%
Maintainers
1
Weekly downloads
 
Created
Source

nodejs-file-downloader is a simple utility for downloading files. It hides the complexity of dealing with streams, redirects, paths and duplicate file names. Can automatically repeat failed downloads.

If you encounter any bugs or have a question, please don't hesitate to open an issue.

Installation

$ npm install nodejs-file-downloader

Table of Contents

Examples

Basic

Download a large file with default configuration

const Downloader = require('nodejs-file-downloader');

(async () => {//Wrapping the code with an async function, just for the sake of example.

    const downloader = new Downloader({
      url: 'http://212.183.159.230/200MB.zip',//If the file name already exists, a new file with the name 200MB1.zip is created.     
      directory: "./downloads",//This folder will be created, if it doesn't exist.               
    })
    try {
      await downloader.download();//Downloader.download() returns a promise.

      console.log('All done');
    } catch (error) {//IMPORTANT: Handle a possible error. An error is thrown in case of network errors, or status codes of 400 and above.
      //Note that if the maxAttempts is set to higher than 1, the error is thrown only if all attempts fail.
      console.log('Download failed',error)
    }


})();    

 

Get the progress of a download
const Downloader = require('nodejs-file-downloader');

(async () => {

    const downloader = new Downloader({     
       url: 'http://212.183.159.230/200MB.zip',     
       directory: "./downloads/2020/May",//Sub directories will also be automatically created if they do not exist.  
       onProgress:function(percentage){//Gets called with each chunk.
            console.log('% ',percentage)   
       }         
     })    
    
    try {
      await downloader.download();   
    } catch (error) {
       console.log(error)
    }
     

})();  

 

Custom file name

Normally, nodejs-file-downloader "deduces" the file name, from the URL or the response headers. If you want to choose a custom file name, supply a config.fileName property.


  const downloader = new Downloader({     
      url: 'http://212.183.159.230/200MB.zip',     
      directory: "./downloads/2020/May", 
      fileName:'somename.zip'//This will be the file name.        
  }) 

 

Overwrite existing files

By default, nodejs-file-downloader uses config.cloneFiles = true, which means that files with an existing name, will have a number appended to them.


  const downloader = new Downloader({     
      url: 'http://212.183.159.230/200MB.zip',     
      directory: "./",  
      cloneFiles:false//This will cause the downloader to re-write an existing file.   
  }) 

 

Hook into response

If you need to get the underlying response, in order to decide whether the download should continue, or perform any other operations, use the onReponse hook.

//The response object is a node response(http.IncomingMessage)
function onResponse(response) {
  //Now you can do something with the response, like check the headers
  if (response.headers['content-length'] > 1000000) {
    console.log('File is too big!')
    return false;//If you return false, the download process is stopped, and downloader.download() is resolved.
  }

  //Returning any other value, including undefined, will tell the downloader to proceed as usual.
}

const downloader = new Downloader({
  url: 'http://212.183.159.230/200MB.zip',
  directory: "./",
  onResponse
})
try {
  await downloader.download()
} catch (error) {
  console.log(error)
}


 

Repeat failed downloads automatically

The program can repeat any failed downloads automatically. Only if the provided config.maxAttempts number is exceeded, an Error is thrown.


  const downloader = new Downloader({     
      url: 'http://212.183.159.230/200MB.zip',     
      directory: "./",
      maxAttempts:3,//Default is 1.
      onError:function(error){//You can also hook into each failed attempt.
        console.log('Error from attempt ',error)
      }        
  })   
  
  try {
    await downloader.download();
  } catch (error) {//If all attempts fail, the last error is thrown.
    console.log('Final fail',error)
  }



 

Use a proxy

You can pass a proxy string. Under the hood, this will create a custom httpsAgent. This feature wasn't tested extensively.


  const downloader = new Downloader({  
      proxy:"http://username:password@some-proxy.com:22225",   
      url: 'http://212.183.159.230/200MB.zip',     
      directory: "./",      
  })     


 

Error handling

downloader.download() should be put within a try-catch block. The program will throw an error, just like Axios, in case of network problems or an http status code of 400 or higher.

If the auto-repeat feature is enabled(by setting the maxAttempts to higher than 1), then only a failure of the final attempt will throw an error.

Keywords

FAQs

Package last updated on 09 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc