Permalink
Browse files

Adding stream support for request and response bodies.

  • Loading branch information...
1 parent a0536a4 commit 9ccaad7dce05e5dcc3eacaf1500404622a0d8067 @mikeal mikeal committed May 29, 2010
Showing with 21 additions and 6 deletions.
  1. +2 −1 README.md
  2. +19 −5 lib/main.js
View
3 README.md
@@ -20,7 +20,8 @@ The first argument is an options object. The only required option is uri, all ot
* `'body'` - entity body for POST and PUT requests
* `'client'` - existing http client object (when undefined a new one will be created and assigned to this property so you can keep around a reference to it if you would like use keep-alive on later request)
* '`followRedirect` - follow HTTP 3xx responses as redirects. defaults to true.
-* '`bodyStream` - Stream to write body chunks to. When set this option will be passed as the last argument to the callback instead of the entire body.
+* '`requestBodyStream` - Stream to read request body chunks from.
+* '`responseBodyStream` - Stream to write body chunks to. When set this option will be passed as the last argument to the callback instead of the entire body.
The callback argument gets 3 arguments. The first is an error when applicable (usually from the http.Client option not the http.ClientRequest object). The second in an http.ClientResponse object. The third is the response body buffer.
View
24 lib/main.js
@@ -35,6 +35,11 @@ function request (options, callback) {
var secure = false;
}
+ if (options.bodyStream) {
+ sys.error('options.bodyStream is deprecated. use options.reponseBodyStream instead.');
+ options.reponseBodyStream = options.bodyStream;
+ }
+
options.client = options.client ? options.client : http.createClient(options.uri.port, options.uri.hostname, secure);
var clientErrorHandler = function (error) {
@@ -53,8 +58,8 @@ function request (options, callback) {
options.request.addListener("response", function (response) {
var buffer = '';
response.addListener("data", function (chunk) {
- if (options.bodyStream) {
- options.bodyStream.write(chunk)
+ if (options.responseBodyStream) {
+ options.responseBodyStream.write(chunk)
} else { buffer += chunk; }
})
response.addListener("end", function () {
@@ -68,17 +73,26 @@ function request (options, callback) {
return;
}
options.client.removeListener("error", clientErrorHandler);
- if (options.bodyStream) {
- buffer = options.bodyStream;
+ if (options.responseBodyStream) {
+ buffer = options.responseBodyStream;
}
callback(null, response, buffer);
})
})
if (options.body) {
options.request.write(options.body, 'binary');
+ options.request.end();
+ } else if (options.requestBodyStream) {
+ options.requestBodyStream.addListener('data', function (chunk) {
+ options.requestBodyStream.write(chunk);
+ })
+ options.requestBodyStream.addListener('end', function () {
+ options.request.end();
+ })
+ } else {
+ options.request.end();
}
- options.request.end();
}
module.exports = request;

0 comments on commit 9ccaad7

Please sign in to comment.