Skip to content

Commit

Permalink
Be a better stream.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed Mar 27, 2011
1 parent 1c00080 commit 4c355d1
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ Request.prototype.request = function () {

var clientErrorHandler = function (error) {
if (setHost) delete options.headers.host;
if (options.onResponse) options.onResponse(error);
if (options.callback) options.callback(error);
options.emit('error', error);
};
if (options.onResponse) options.on('error', function (e) {options.onResponse(e)});
if (options.callback) options.on('error', function (e) {options.callback(e)});


if (options.uri.auth && !options.headers.authorization) {
options.headers.authorization = "Basic " + toBase64(options.uri.auth.split(':').map(qs.unescape).join(':'));
Expand Down Expand Up @@ -226,6 +228,9 @@ Request.prototype.request = function () {
return; // Ignore the rest of the response
} else {
options._redirectsFollowed = 0;
// Be a good stream and emit end when the response is finished.
// Hack to emit end on close becuase of a core bug that never fires end
response.on('close', function () {options.emit('end')})

if (options.encoding) {
if (options.dests.length !== 0) {
Expand All @@ -234,12 +239,14 @@ Request.prototype.request = function () {
response.setEncoding(options.encoding);
}
}

if (options.dests.length !== 0) {
options.dests.forEach(function (dest) {
response.pipe(dest);
})
if (options.onResponse) options.onResponse(null, response);
if (options.callback) options.callback(null, response, options.responseBodyStream);

} else {
if (options.onResponse) {
options.onResponse(null, response);
Expand Down

0 comments on commit 4c355d1

Please sign in to comment.