Permalink
Browse files

Using sys.pump

  • Loading branch information...
1 parent 7a11f69 commit 186e9cf692511d768f8016d311609a0a0a315af6 @mikeal mikeal committed Jul 18, 2010
Showing with 11 additions and 15 deletions.
  1. +11 −15 lib/main.js
View
@@ -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);
@@ -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);
})
@@ -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();
}

0 comments on commit 186e9cf

Please sign in to comment.