Gathering detailed insights and metrics for node-downloader-helper
Gathering detailed insights and metrics for node-downloader-helper
Gathering detailed insights and metrics for node-downloader-helper
Gathering detailed insights and metrics for node-downloader-helper
@expo/downloader
`@expo/downloader` is a small helper library used to download files.
cldr-data-downloader
Download tool for Unicode CLDR JSON data
bindings
Helper module for loading your native module's .node file
image-downloader
A Nodejs module for downloading image to disk from a given URL
npm install node-downloader-helper
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
254 Stars
100 Commits
55 Forks
8 Watching
3 Branches
19 Contributors
Updated on 14 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-7.3%
70,423
Compared to previous day
Last week
-0%
367,124
Compared to previous week
Last month
7.9%
1,551,595
Compared to previous month
Last year
39.2%
19,006,978
Compared to previous year
A simple http file downloader for node.js
Features:
$ npm install --save node-downloader-helper
For a more complete example check example folder
1const { DownloaderHelper } = require('node-downloader-helper'); 2const dl = new DownloaderHelper('https://proof.ovh.net/files/1Gb.dat', __dirname); 3 4dl.on('end', () => console.log('Download Completed')); 5dl.on('error', (err) => console.log('Download Failed', err)); 6dl.start().catch(err => console.error(err));
IMPORTANT NOTE: I highly recommend to use both .on('error')
and .start().catch
, although they do the same thing, if on('error')
is not defined, an error will be thrown when the error
event is emitted and not listing, this is because EventEmitter is designed to throw an unhandled error event
error if not been listened and is too late to change it now.
This can be used as standalone CLI downloader
Install npm i -g node-downloader-helper
Usage: ndh [folder] [url]
1$ ndh ./folder http://url
Download Helper constructor also allow a 3rd parameter to set some options constructor(url, destinationFolder, options)
,
these are the default values
1{ 2 body: null, // Request body, can be any, string, object, etc. 3 method: 'GET', // Request Method Verb 4 headers: {}, // Custom HTTP Header ex: Authorization, User-Agent 5 timeout: -1, // Request timeout in milliseconds (-1 use default), is the equivalent of 'httpRequestOptions: { timeout: value }' (also applied to https) 6 metadata: {}, // custom metadata for the user retrieve later (default:null) 7 resumeOnIncomplete: true, // Resume download if the file is incomplete (set false if using any pipe that modifies the file) 8 resumeOnIncompleteMaxRetry: 5, // Max retry when resumeOnIncomplete is true 9 resumeIfFileExists: false, // it will resume if a file already exists and is not completed, you might want to set removeOnStop and removeOnFail to false. If you used pipe for compression it will produce corrupted files 10 fileName: string|cb(fileName, filePath, contentType)|{name, ext}, // Custom filename when saved 11 retry: false, // { maxRetries: number, delay: number in ms } or false to disable (default) 12 forceResume: false, // If the server does not return the "accept-ranges" header, can be force if it does support it 13 removeOnStop: true, // remove the file when is stopped (default:true) 14 removeOnFail: true, // remove the file when fail (default:true) 15 progressThrottle: 1000, // interval time of the 'progress.throttled' event will be emitted 16 override: boolean|{skip, skipSmaller}, // Behavior when local file already exists 17 httpRequestOptions: {}, // Override the http request options 18 httpsRequestOptions: {}, // Override the https request options, ex: to add SSL Certs 19}
for body
you can provide any parameter accepted by http.request write function req.write(body)
https://nodejs.org/api/http.html, when using this, you might need to add the content-length
and content-type
header in addition with the http method POST
or PUT
ex:
1const data = JSON.stringify({ 2 todo: 'Buy the milk' 3}); 4const dl = new DownloaderHelper('my_url', __dirname, { 5method: 'POST', 6body: data, 7headers: { 8 'Content-Type': 'application/json', 9 'Content-Length': data.length 10} } );
for fileName
you can provide 3 types of parameter
(fileName) => 'PREFIX_' + fileName;
, contentType will be provided if availablename
attribute and an optional ext
attribute, the ext
attribute can be an string without dot(.
) or a boolean where true
use the name
as full file name (same as just giving an string to the fileName
parameter) or false (default) will only replace the name and keep the original extension, for example if the original name is myfile.zip
and the option is {name: 'somename'}
the output will be somename.zip
for override
you can provide 2 types of parameter
true
to override existing local file, false
to append '(number)' to new file nameskip
(boolean): whether to skip download if file exists, and skipSmaller
(boolean): whether to skip download if file exists but is smaller. Both default to false
, for the equivalent of override: true
.for httpRequestOptions
the available options are detailed in here https://nodejs.org/api/http.html#http_http_request_options_callback
for httpsRequestOptions
the available options are detailed in here https://nodejs.org/api/https.html#https_https_request_options_callback
Name | Description |
---|---|
start | starts the downloading |
pause | pause the downloading |
resume | resume the downloading if supported, if not it will start from the beginning |
stop | stop the downloading and remove the file |
pipe | readable.pipe(stream.Readable, options) : stream.Readable |
unpipe | (stream) if not stream is not specified, then all pipes are detached. |
updateOptions | (options, url) updates the options, can be use on pause/resume events |
getStats | returns stats from the current download, these are the same stats sent via progress event |
getTotalSize | gets the total file size from the server |
getDownloadPath | gets the full path where the file will be downloaded (available after the start phase) |
isResumable | return true/false if the download can be resumable (available after the start phase) |
getResumeState | Get the state required to resume the download after restart. This state can be passed back to resumeFromFile() to resume a download |
resumeFromFile | resumeFromFile(filePath?: string, state?: IResumeState) Resume the download from a previous file path, if the state is not provided it will try to fetch from the information the headers and filePath, @see resumeIfFileExists option |
usage of resumeFromFile
1const downloadDir = 'D:/TEMP'; 2const { DownloaderHelper } = require('node-downloader-helper'); 3const dl = new DownloaderHelper('https://proof.ovh.net/files/1Gb.dat', downloadDir); 4dl.on('end', () => console.log('Download Completed')); 5dl.on('error', (err) => console.log('Download Failed', err)); 6 7// option 1 8const prevFilePath = `${downloadDir}/1Gb.dat`; 9dl.resumeFromFile(prevFilePath).catch(err => console.error(err)); 10 11// option 2 12const prevState = dl.getResumeState(); // this should be stored in a file, localStorage, db, etc in a previous process for example on 'stop' 13dl.resumeFromFile(prevState.filePath, prevState).catch(err => console.error(err));
Name | Description |
---|---|
start | Emitted when the .start method is called |
skip | Emitted when the download is skipped because the file already exists |
download | Emitted when the download starts callback(downloadInfo) |
progress | Emitted every time gets data from the server callback(stats) |
progress.throttled | The same as progress but emits every 1 second while is downloading callback(stats) |
retry | Emitted when the download fails and retry is enabled callback(attempt, retryOpts, err) |
end | Emitted when the downloading has finished callback(downloadInfo) |
error | Emitted when there is any error callback(error) |
timeout | Emitted when the underlying socket times out from inactivity. |
pause | Emitted when the .pause method is called |
stop | Emitted when the .stop method is called |
resume | Emitted when the .resume method is called callback(isResume) |
renamed | Emitted when '(number)' is appended to the end of file, this requires override:false opt, callback(filePaths) |
redirected | Emitted when an url redirect happened callback(newUrl, oldUrl) NOTE: this will be triggered during getTotalSize() as well |
stateChanged | Emitted when the state changes callback(state) |
warning | Emitted when an error occurs that was not thrown intentionally callback(err: Error) |
event skip skipInfo
object
1{ 2 totalSize:, // total file size got from the server (will be set as 'null' if content-length header is not available) 3 fileName:, // original file name 4 filePath:, // original path name 5 downloadedSize:, // the downloaded amount 6}
event download downloadInfo
object
1{ 2 totalSize:, // total file size got from the server (will be set as 'null' if content-length header is not available) 3 fileName:, // assigned name 4 filePath:, // download path 5 isResumed:, // if the download is a resume, 6 downloadedSize:, // the downloaded amount (only if is resumed otherwise always 0) 7}
event progress or progress.throttled stats
object
1{ 2 name:, // file name 3 total:, // total size that needs to be downloaded in bytes, (will be set as 'null' if content-length header is not available) 4 downloaded:, // downloaded size in bytes 5 progress:, // progress porcentage 0-100%, (will be set as 0 if total is null) 6 speed: // download speed in bytes 7}
event end downloadInfo
object
1{ 2 fileName:, 3 filePath:, 4 totalSize:, // total file size got from the server, (will be set as 'null' if content-length header is not available) 5 incomplete:, // true/false if the download endend but still incomplete, set as 'false' if totalSize is null 6 onDiskSize, // total size of file on the disk 7 downloadedSize:, // the total size downloaded 8}
event renamed filePaths
object
1{ 2 path:, // modified path name 3 fileName:, // modified file name 4 prevPath:, // original path name 5 prevFileName:, // original file name 6}
event error error
object
1{ 2 message:, // Error message 3 status:, // Http status response if available 4 body:, // Http body response if available 5}
Name | Value |
---|---|
IDLE | 'IDLE' |
SKIPPED | 'SKIPPED' |
STARTED | 'STARTED' |
DOWNLOADING | 'DOWNLOADING' |
PAUSED | 'PAUSED' |
RESUMED | 'RESUMED' |
STOPPED | 'STOPPED' |
FINISHED | 'FINISHED' |
FAILED | 'FAILED' |
RETRY | 'RETRY' |
$ npm test
Read License for more licensing information.
Read here for more information.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 7/30 approved changesets -- score normalized to 2
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More