From f2c718712f4b8f0af0a0672dc73ecfd624d498c1 Mon Sep 17 00:00:00 2001 From: Joyal Date: Tue, 19 Jun 2018 12:08:32 +0200 Subject: [PATCH] Fixed emiting "End" when stopped - fixed __total type from string to number - added validation to emit progress when is fully downloaded aswell - fixed stop function when is called and is not started yet --- dist/index.js | 38 +++++++++++++++++++++++++------------- package.json | 2 +- src/index.js | 39 ++++++++++++++++++++++++++------------- 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/dist/index.js b/dist/index.js index 0fe10e2..9f177d0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -102,7 +102,7 @@ var DownloaderHelper = exports.DownloaderHelper = function (_EventEmitter) { _this2.__request = _this2.__protocol.request(_this2.__options, function (response) { //Stats if (!_this2.__isResumed) { - _this2.__total = response.headers['content-length']; + _this2.__total = parseInt(response.headers['content-length']); _this2.__downloaded = 0; _this2.__progress = 0; } @@ -147,7 +147,7 @@ var DownloaderHelper = exports.DownloaderHelper = function (_EventEmitter) { }); _this2.__fileStream.on('finish', function () { - if (_this2.state !== _this2.__states.PAUSED) { + if (_this2.state !== _this2.__states.PAUSED && _this2.state !== _this2.__states.STOPPED) { _this2.__setState(_this2.__states.FINISHED); _this2.emit('end'); } @@ -209,17 +209,29 @@ var DownloaderHelper = exports.DownloaderHelper = function (_EventEmitter) { var _this4 = this; this.__setState(this.__states.STOPPED); - this.__request.abort(); - this.__fileStream.close(); + if (this.__request) { + this.__request.abort(); + } + if (this.__fileStream) { + this.__fileStream.close(); + } return new Promise(function (resolve, reject) { - fs.unlink(_this4.__filePath, function (_err) { - if (_err) { - _this4.__setState(_this4.__states.FAILED); - _this4.emit('error', _err); - return reject(_err); + fs.access(_this4.__filePath, function (_accessErr) { + // if can't access, probably is not created yet + if (_accessErr) { + _this4.emit('stop'); + return resolve(true); } - _this4.emit('stop'); - resolve(true); + + fs.unlink(_this4.__filePath, function (_err) { + if (_err) { + _this4.__setState(_this4.__states.FAILED); + _this4.emit('error', _err); + return reject(_err); + } + _this4.emit('stop'); + resolve(true); + }); }); }); } @@ -232,8 +244,8 @@ var DownloaderHelper = exports.DownloaderHelper = function (_EventEmitter) { this.__downloaded += receivedBytes; this.__progress = this.__downloaded / this.__total * 100; - // emit the progress every second - if (elaspsedTime > 1000) { + // emit the progress every second or if finished + if (this.__downloaded === this.__total || elaspsedTime > 1000) { // Calculate the speed this.__statsEstimate.time = currentTime; this.__statsEstimate.bytes = this.__downloaded - this.__statsEstimate.prevBytes; diff --git a/package.json b/package.json index 938d022..9bc3c73 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-downloader-helper", - "version": "1.0.4", + "version": "1.0.5", "description": "A simple http file downloader for node.js", "main": "./dist/index.js", "scripts": { diff --git a/src/index.js b/src/index.js index 75bddf3..47a29fa 100644 --- a/src/index.js +++ b/src/index.js @@ -64,7 +64,7 @@ export class DownloaderHelper extends EventEmitter { this.__request = this.__protocol.request(this.__options, response => { //Stats if (!this.__isResumed) { - this.__total = response.headers['content-length']; + this.__total = parseInt(response.headers['content-length']); this.__downloaded = 0; this.__progress = 0; } @@ -109,7 +109,8 @@ export class DownloaderHelper extends EventEmitter { response.on('data', chunk => this.__calculateStats(chunk.length)); this.__fileStream.on('finish', () => { - if (this.state !== this.__states.PAUSED) { + if (this.state !== this.__states.PAUSED && + this.state !== this.__states.STOPPED) { this.__setState(this.__states.FINISHED); this.emit('end'); } @@ -159,17 +160,29 @@ export class DownloaderHelper extends EventEmitter { stop() { this.__setState(this.__states.STOPPED); - this.__request.abort(); - this.__fileStream.close(); + if (this.__request) { + this.__request.abort(); + } + if (this.__fileStream) { + this.__fileStream.close(); + } return new Promise((resolve, reject) => { - fs.unlink(this.__filePath, _err => { - if (_err) { - this.__setState(this.__states.FAILED); - this.emit('error', _err); - return reject(_err); + fs.access(this.__filePath, _accessErr => { + // if can't access, probably is not created yet + if (_accessErr) { + this.emit('stop'); + return resolve(true); } - this.emit('stop'); - resolve(true); + + fs.unlink(this.__filePath, _err => { + if (_err) { + this.__setState(this.__states.FAILED); + this.emit('error', _err); + return reject(_err); + } + this.emit('stop'); + resolve(true); + }); }); }); } @@ -181,8 +194,8 @@ export class DownloaderHelper extends EventEmitter { this.__downloaded += receivedBytes; this.__progress = (this.__downloaded / this.__total) * 100; - // emit the progress every second - if (elaspsedTime > 1000) { + // emit the progress every second or if finished + if (this.__downloaded === this.__total || elaspsedTime > 1000) { // Calculate the speed this.__statsEstimate.time = currentTime; this.__statsEstimate.bytes = this.__downloaded - this.__statsEstimate.prevBytes;