Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
nodejs-file-downloader
Advanced tools
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.
$ npm install nodejs-file-downloader
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)
}
})();
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)
}
})();
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.
})
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.
})
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)
}
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)
}
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: "./",
})
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.
FAQs
A file downloader for NodeJs
The npm package nodejs-file-downloader receives a total of 46,667 weekly downloads. As such, nodejs-file-downloader popularity was classified as popular.
We found that nodejs-file-downloader demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.