diff --git a/README.md b/README.md index 7d6ff92..6f18374 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 diff --git a/example/index.js b/example/index.js index cc0a18d..361a208 100644 --- a/example/index.js +++ b/example/index.js @@ -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) @@ -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 */ }); diff --git a/package.json b/package.json index 32bd7eb..5da7312 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index 038913d..d902098 100644 --- a/src/index.js +++ b/src/index.js @@ -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, @@ -262,7 +263,6 @@ export class DownloaderHelper extends EventEmitter { return this.__isResumable; } - /** * Updates the options, can be use on pause/resume events * @@ -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 * diff --git a/types/index.d.ts b/types/index.d.ts index 2870c45..9668ac7 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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 */ @@ -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 { /** @@ -236,6 +242,9 @@ export class DownloaderHelper extends EventEmitter { */ updateOptions(options?: object): void; + getOptions(): object; + getMetadata(): object | null; + /** * Current download progress stats * @@ -260,12 +269,12 @@ export class DownloaderHelper extends EventEmitter { on(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; /**