Skip to content

Commit

Permalink
Fixed 'resume' event triggered when stop() due the request abort
Browse files Browse the repository at this point in the history
  • Loading branch information
hgouveia committed Aug 23, 2022
1 parent 4d1f7d2 commit ebd091e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
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.3",
"version": "2.1.4",
"description": "A simple http file downloader for node.js",
"main": "./dist/index.js",
"types": "./types/index.d.ts",
Expand Down
16 changes: 11 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class DownloaderHelper extends EventEmitter {
this.__promise = null;
this.__request = null;
this.__response = null;
this.__isAborted = false;
this.__isResumed = false;
this.__isResumable = false;
this.__isRedirected = false;
Expand Down Expand Up @@ -413,6 +414,7 @@ export class DownloaderHelper extends EventEmitter {

// Start the Download
this.__response = null;
this.__isAborted = false;
this.__request = this.__downloadRequest(this.__promise.resolve, this.__promise.reject);

// Error Handling
Expand Down Expand Up @@ -563,11 +565,13 @@ export class DownloaderHelper extends EventEmitter {
* @memberof DownloaderHelper
*/
__hasFinished() {
return (this.state !== this.__states.PAUSED &&
this.state !== this.__states.STOPPED &&
this.state !== this.__states.RETRY &&
this.state !== this.__states.FAILED &&
this.state !== this.__states.RESUMED);
return !this.__isAborted && [
this.__states.PAUSED,
this.__states.STOPPED,
this.__states.RETRY,
this.__states.FAILED,
this.__states.RESUMED
].indexOf(this.state) === -1;
}


Expand Down Expand Up @@ -1123,6 +1127,8 @@ export class DownloaderHelper extends EventEmitter {
* @memberof DownloaderHelper
*/
__requestAbort() {
this.__isAborted = true;

if (this.__response) {
this.__response.destroy();
}
Expand Down

0 comments on commit ebd091e

Please sign in to comment.