Skip to content

Commit

Permalink
Fixed emiting "End" when stopped
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
hgouveia committed Jun 19, 2018
1 parent d977812 commit f2c7187
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
38 changes: 25 additions & 13 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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);
});
});
});
}
Expand All @@ -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;
Expand Down
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": "1.0.4",
"version": "1.0.5",
"description": "A simple http file downloader for node.js",
"main": "./dist/index.js",
"scripts": {
Expand Down
39 changes: 26 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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);
});
});
});
}
Expand All @@ -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;
Expand Down

0 comments on commit f2c7187

Please sign in to comment.