Skip to content

Commit

Permalink
Using sys.pump
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed Jul 18, 2010
1 parent 7a11f69 commit 186e9cf
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions lib/main.js
Expand Up @@ -60,12 +60,16 @@ function request (options, callback) {
options.request = options.client.request(options.method, options.fullpath, options.headers);

options.request.addListener("response", function (response) {
var buffer = '';
response.addListener("data", function (chunk) {
if (options.responseBodyStream) {
options.responseBodyStream.write(chunk)
} else { buffer += chunk; }
})
var buffer;
if (options.responseBodyStream) {
buffer = options.responseBodyStream;
sys.pump(response, options.responseBodyStream);
}
else {
buffer = '';
response.addListener("data", function (chunk) { buffer += chunk; } )
}

response.addListener("end", function () {
options.client.removeListener("error", clientErrorHandler);

Expand All @@ -79,9 +83,6 @@ function request (options, callback) {
return;
}

if (options.responseBodyStream) {
buffer = options.responseBodyStream;
}
if (setHost) delete options.headers.host;
callback(null, response, buffer);
})
Expand All @@ -91,12 +92,7 @@ function request (options, callback) {
options.request.write(options.body, 'binary');
options.request.end();
} else if (options.requestBodyStream) {
options.requestBodyStream.addListener('data', function (chunk) {
options.request.write(chunk);
})
options.requestBodyStream.addListener('end', function () {
options.request.end();
})
sys.pump(options.requestBodyStream, options.request);
} else {
options.request.end();
}
Expand Down

0 comments on commit 186e9cf

Please sign in to comment.