Skip to content

Commit

Permalink
Adding stream support for request and response bodies.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed May 29, 2010
1 parent a0536a4 commit 9ccaad7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
24 changes: 19 additions & 5 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 () {
Expand All @@ -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.