Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding optional body buffer.

  • Loading branch information...
commit 1c9cf719c92b02ba85c4e47bd2b92a3303cbe1cf 1 parent 4ff3493
Mikeal Rogers mikeal authored
Showing with 19 additions and 1 deletion.
  1. +1 −0  README.md
  2. +6 −1 lib/main.js
  3. +12 −0 tests/couch.js
1  README.md
View
@@ -20,6 +20,7 @@ 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.
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.
7 lib/main.js
View
@@ -48,7 +48,9 @@ function request (options, callback) {
options.request.addListener("response", function (response) {
var buffer = '';
response.addListener("data", function (chunk) {
- buffer += chunk;
+ if (options.bodyStream) {
+ options.bodyStream.write(chunk)
+ } else { buffer += chunk; }
})
response.addListener("end", function () {
if (response.statusCode > 299 && response.statusCode < 400 && options.followRedirect && response.headers.location) {
@@ -61,6 +63,9 @@ function request (options, callback) {
return;
}
options.client.removeListener("error", clientErrorHandler);
+ if (options.bodyStream) {
+ buffer = options.bodyStream;
+ }
callback(null, response, buffer);
})
})
12 tests/couch.js
View
@@ -15,6 +15,18 @@ testports();
testports(80)
testports(5984)
+function testportsStream (port) {
+ var uri = port ? 'http://mikeal.couchone.com' + ":" + port : 'http://mikeal.couchone.com';
+ sys.puts(uri)
+ var body = ''
+ var bs = {write:function (chunk) {body += chunk}}
+ request({uri:uri}, function (error, response) {
+ if (error) {throw new Error(error)};
+ assert.equal(response.statusCode, 200);
+ assert.equal(body.slice(0, '{"couchdb":"Welcome",'.length), '{"couchdb":"Welcome",');
+ })
+}
+testports();
var randomnumber=Math.floor(Math.random()*100000000).toString();
request({uri:'http://mikeal.couchone.com/testjs', method:'POST', body:'{"_id":"'+randomnumber+'"}'},
function (error, response, body) {
Please sign in to comment.
Something went wrong with that request. Please try again.