diff --git a/package.json b/package.json index ad416d9..eea98ee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-downloader-helper", - "version": "2.1.7", + "version": "2.1.8", "description": "A simple http/s 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 470b377..56d7727 100644 --- a/src/index.js +++ b/src/index.js @@ -126,7 +126,6 @@ export class DownloaderHelper extends EventEmitter { return Promise.resolve(true); } - if (this.__response) { this.__response.unpipe(); this.__pipes.forEach(pipe => pipe.stream.unpipe()); @@ -718,37 +717,39 @@ export class DownloaderHelper extends EventEmitter { if (!this.__opts.retry || typeof this.__opts.retry !== 'object') { return Promise.reject(err || new Error('wrong retry options')); } - + const { delay: retryDelay = 0, maxRetries = 999 } = this.__opts.retry; - + // reached the maximum retries if (this.__retryCount >= maxRetries) { return Promise.reject(err || new Error('reached the maximum retries')); } - + this.__retryCount++; this.__setState(this.__states.RETRY); this.emit('retry', this.__retryCount, this.__opts.retry, err); - + if (this.__response) { this.__response.unpipe(); this.__pipes.forEach(pipe => pipe.stream.unpipe()); } - + if (this.__fileStream) { this.__fileStream.removeAllListeners(); } - + this.__requestAbort(); - - return this.__closeFileStream().then(() => - new Promise((resolve) => - this.__retryTimeout = setTimeout(() => resolve(this.__downloaded > 0 ? - this.resume() : - this.__start()), - retryDelay) - ) - ); + + return this.__closeFileStream().then(() => + new Promise((resolve) => + this.__retryTimeout = setTimeout( + () => resolve(this.__downloaded > 0 ? + this.resume() : + this.__start()), + retryDelay + ) + ) + ); } /**