Skip to content

Commit

Permalink
Merge pull request #8 from leodutra/ClientRequest-error-guarding
Browse files Browse the repository at this point in the history
Implements stream.finished() for better error handling.
  • Loading branch information
minhnhut authored Nov 11, 2019
2 parents 02e26a3 + 20572e2 commit b613c9b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
7 changes: 5 additions & 2 deletions lib/AbstractDownloadSession.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const EventEmitter = require("events")
const fjs = require("functional.js")
const Utils = require('./Utils')
const { finished } = require('stream')

module.exports = class extends EventEmitter {
constructor(url, savePath) {
Expand Down Expand Up @@ -97,7 +98,9 @@ module.exports = class extends EventEmitter {
this._emitProgress();
}
});
res.on('end', () => this._emitEnd());
res.on('error', (e) => this._emitError(e));
finished(res, e => e
? this._emitError(e)
: this._emitEnd()
);
}
};
18 changes: 11 additions & 7 deletions lib/DownloadSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const http = require("http")
const https = require("https")
const fs = require("fs")
const url = require("url")
const { finished } = require('stream')
const AbstractDownloadSession = require("./AbstractDownloadSession");

module.exports = class extends AbstractDownloadSession {
Expand Down Expand Up @@ -55,13 +56,16 @@ module.exports = class extends AbstractDownloadSession {
}
requestOptions.timeout = 3000;
const httpLibrary = requestOptions.protocol.indexOf("https") === 0 ? https : http;
httpLibrary.get(requestOptions, res => {
this.lastSecondDownloadedBytes = 0;
this.downloadedBytes = 0;
this.bytesPerSecond = 0;
res.pipe(file);
this._handleResponse(res);
}).on('error', e => this._emitError(e));
finished(
httpLibrary.get(requestOptions, res => {
this.lastSecondDownloadedBytes = 0;
this.downloadedBytes = 0;
this.bytesPerSecond = 0;
res.pipe(file);
this._handleResponse(res);
}),
e => e && this._emitError(e)
);
this.status = "downloading";
};
};
8 changes: 7 additions & 1 deletion lib/DownloadWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const https = require("https")
const url = require("url")
const Utils = require('./Utils')
const path = require("path")
const { finished } = require('stream')


module.exports = class extends EventEmitter {
Expand Down Expand Up @@ -49,7 +50,12 @@ module.exports = class extends EventEmitter {
this.emit("error", {"code": "ERROR_BAD_STATUS_CODE", "message": "Target is not downloadable, server didn't response with 200 code."});
}
});
request.on('error', err => this.emit("error", {"code": "ERROR_CONNECT_TO_SERVER", "message": "Can not connect to target server."}));
finished(request, e =>
e && this.emit("error", {
"code": "ERROR_CONNECT_TO_SERVER",
"message": "Can not connect to target server."
})
);
request.end();
}

Expand Down
4 changes: 3 additions & 1 deletion lib/PartialDownloadSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const http = require("http")
const https = require("https")
const fs = require("fs")
const url = require("url")
const { finished } = require('stream')
const AbstractDownloadSession = require("./AbstractDownloadSession");

module.exports = class extends AbstractDownloadSession {
Expand Down Expand Up @@ -32,7 +33,8 @@ module.exports = class extends AbstractDownloadSession {
this.bytesPerSecond = 0;
res.pipe(file);
this._handleResponse(res);
}).on('error', e => this._emitError(e));
})
finished(res, e => e && this._emitError(e));
this.status = "downloading";
};
};
20 changes: 12 additions & 8 deletions lib/SimpleDownloadSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const http = require("http")
const https = require("https")
const fs = require("fs")
const url = require("url")
const { finished } = require('stream')
const AbstractDownloadSession = require("./AbstractDownloadSession");

module.exports = class extends AbstractDownloadSession {
Expand All @@ -13,14 +14,17 @@ module.exports = class extends AbstractDownloadSession {
const requestOptions = url.parse(downloadUrl);
requestOptions.timeout = 3000;
const httpLibrary = requestOptions.protocol.indexOf("https") === 0 ? https : http;
httpLibrary.get(requestOptions, res => {
this.lastSecondDownloadedBytes = 0;
this.downloadedBytes = 0;
this.totalBytes = res.headers["content-length"];
this.bytesPerSecond = 0;
res.pipe(file);
this._handleResponse(res);
}).on('error', e => this._emitError(e));
finished(
httpLibrary.get(requestOptions, res => {
this.lastSecondDownloadedBytes = 0;
this.downloadedBytes = 0;
this.totalBytes = res.headers["content-length"];
this.bytesPerSecond = 0;
res.pipe(file);
this._handleResponse(res);
}),
e => e && this._emitError(e)
);
this.status = "downloading"
};
};

0 comments on commit b613c9b

Please sign in to comment.