Skip to content

Commit

Permalink
Added custom metadata for the users, added getOptions, updated type d…
Browse files Browse the repository at this point in the history
…efinitions
  • Loading branch information
hgouveia committed Aug 12, 2022
1 parent d5340f8 commit 4d1f7d2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![NPM Version](https://img.shields.io/npm/v/node-downloader-helper.svg?style=flat-square "npm version")](https://www.npmjs.com/package/node-downloader-helper)
![npm](https://img.shields.io/npm/dw/node-downloader-helper?style=flat-square "npm download")
![GitHub Actions Build](https://github.com/github/docs/actions/workflows/test.yml/badge.svg "GitHub Actions Build")
![GitHub Actions Build](https://github.com/hgouveia/node-downloader-helper/actions/workflows/test.yml/badge.svg "GitHub Actions Build")
[![Build Status](https://img.shields.io/travis/hgouveia/node-downloader-helper/master.svg?style=flat-square "Build Status")](https://travis-ci.org/hgouveia/node-downloader-helper)
[![Windows Build Status](https://img.shields.io/appveyor/ci/hgouveia/node-downloader-helper/master.svg?label=windows&style=flat-square "Windows Build Status")](https://ci.appveyor.com/project/hgouveia/node-downloader-helper) [![Join the chat at https://gitter.im/node-downloader-helper/Lobby](https://badges.gitter.im/node-downloader-helper/Lobby.svg)](https://gitter.im/node-downloader-helper/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fhgouveia%2Fnode-downloader-helper.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fhgouveia%2Fnode-downloader-helper?ref=badge_shield)
Expand Down Expand Up @@ -65,6 +65,7 @@ these are the default values
method: 'GET', // Request Method Verb
headers: {}, // Custom HTTP Header ex: Authorization, User-Agent
timeout: -1, // Request timeout in milliseconds (-1 use default), is the equivalent of 'httpRequestOptions: { timeout: value }' (also applied to https)
metadata: {}, // custom metadata for the user retrieve later (default:null)
resumeOnIncomplete: true, // Resume download if the file is incomplete (set false if using any pipe that modifies the file)
resumeOnIncompleteMaxRetry: 5, // Max retry when resumeOnIncomplete is true
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
Expand Down
3 changes: 2 additions & 1 deletion example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const options = {
object: { skip: skip if already exists, skipSmaller: skip if smaller }
boolean: true to override file, false to append '(number)' to new file name
*/
metadata: { name: 'download-1' },
override: { skip: true, skipSmaller: true },
forceResume: false, // If the server does not return the "accept-ranges" header but it does support it
removeOnStop: true, // remove the file when is stopped (default:true)
Expand Down Expand Up @@ -86,6 +87,6 @@ dl
}
});

console.log('Downloading: ', url);
console.log(`Downloading [${dl.getMetadata().name}]: ${url}`);
dl.pipe(zlib.createGzip()); // Adding example of pipe to compress the file while downloading
dl.start().catch(err => { /* already listening on 'error' event but catch can be used too */ });
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-downloader-helper",
"version": "2.1.2",
"version": "2.1.3",
"description": "A simple http file downloader for node.js",
"main": "./dist/index.js",
"types": "./types/index.d.ts",
Expand Down
18 changes: 17 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class DownloaderHelper extends EventEmitter {
headers: {},
fileName: '',
timeout: -1, // -1 use default
metadata: null,
override: false, // { skip: false, skipSmaller: false }
forceResume: false,
removeOnStop: true,
Expand Down Expand Up @@ -262,7 +263,6 @@ export class DownloaderHelper extends EventEmitter {
return this.__isResumable;
}


/**
* Updates the options, can be use on pause/resume events
*
Expand All @@ -287,6 +287,22 @@ export class DownloaderHelper extends EventEmitter {
this.__initProtocol(this.url);
}

/**
*
* @returns {Object}
*/
getOptions() {
return this.__opts;
}

/**
*
* @returns {Object| null}
*/
getMetadata() {
return this.__opts.metadata;
}

/**
* Current download progress stats
*
Expand Down
21 changes: 15 additions & 6 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ interface DownloaderHelperOptions {
retry?: boolean | RetryOptions;
/* Request timeout in milliseconds (-1 use default), is the equivalent of 'httpRequestOptions: { timeout: value }' (also applied to https) */
timeout?: number;
/* custom metadata for the user retrieve later */
metadata?: object | null;
/** 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 */
resumeIfFileExists?: boolean;
/** If the server does not return the "accept-ranges" header, can be force if it does support it */
Expand All @@ -149,6 +151,10 @@ interface DownloaderHelperOptions {
httpRequestOptions?: object;
/** Override the https request options, ex: to add SSL Certs */
httpsRequestOptions?: object;
/** Resume download if the file is incomplete */
resumeOnIncomplete?: boolean;
/** Max retry when resumeOnIncomplete is true */
resumeOnIncompleteMaxRetry?: number;
}
export class DownloaderHelper extends EventEmitter {
/**
Expand Down Expand Up @@ -236,6 +242,9 @@ export class DownloaderHelper extends EventEmitter {
*/
updateOptions(options?: object): void;

getOptions(): object;
getMetadata(): object | null;

/**
* Current download progress stats
*
Expand All @@ -260,12 +269,12 @@ export class DownloaderHelper extends EventEmitter {
on<E extends keyof DownloadEvents>(event: E, callback: DownloadEvents[E]): any;

/**
* Get the state required to resume the download after restart. This state
* can be passed back to `resumeFromFile()` to resume a download
*
* @returns {IResumeState} Returns the state required to resume
* @memberof DownloaderHelper
*/
* Get the state required to resume the download after restart. This state
* can be passed back to `resumeFromFile()` to resume a download
*
* @returns {IResumeState} Returns the state required to resume
* @memberof DownloaderHelper
*/
getResumeState(): IResumeState;

/**
Expand Down

0 comments on commit 4d1f7d2

Please sign in to comment.