Skip to content
Browse files

add buffer cancellation via .buffer(false)

stream proxy haCCCckkkkYYYY ... delegation suxxxorz
  • Loading branch information...
1 parent 7a58c43 commit 3ccdbad7e9a477ba405c7a4df177c11f28b3aeab @tj tj committed Aug 19, 2012
Showing with 48 additions and 6 deletions.
  1. +1 −1 Makefile
  2. +14 −4 lib/node/index.js
  3. +11 −1 lib/node/response.js
  4. +22 −0 test/node/basic.js
View
2 Makefile
@@ -10,7 +10,7 @@ test:
@NODE_ENV=test ./node_modules/.bin/mocha \
--require should \
--reporter $(REPORTER) \
- --timeout 100 \
+ --timeout 200 \
--growl \
$(TESTS)
View
18 lib/node/index.js
@@ -375,21 +375,22 @@ Request.prototype.write = function(data, encoding){
*/
Request.prototype.pipe = function(stream, options){
- this.preventBuffer();
+ this.buffer(false);
return this.end().req.on('response', function(res){
res.pipe(stream, options);
});
};
/**
- * Prevent buffering.
+ * Enable / disable buffering.
*
+ * @return {Boolean} val
* @return {Request} for chaining
* @api public
*/
-Request.prototype.preventBuffer = function(){
- this._buffer = false;
+Request.prototype.buffer = function(val){
+ this._buffer = val;
return this;
};
@@ -603,6 +604,15 @@ Request.prototype.end = function(fn){
});
}
+ // unbuffered
+ if (!buffer) {
+ self.res = res;
+ var response = new Response(self.req, self.res);
+ response.redirects = self._redirectList;
+ self.emit('response', response);
+ return;
+ }
+
// end event
self.res = res;
res.on('end', function(){
View
12 lib/node/response.js
@@ -3,7 +3,8 @@
* Module dependencies.
*/
-var utils = require('./utils');
+var utils = require('./utils')
+ , Stream = require('stream');
/**
* Expose `Response`.
@@ -34,9 +35,18 @@ function Response(req, res, options) {
this.header = this.headers = res.headers;
this.setStatusProperties(res.statusCode);
this.setHeaderProperties(this.header);
+ this.setEncoding = res.setEncoding.bind(res);
+ res.on('data', this.emit.bind(this, 'data'));
+ res.on('end', this.emit.bind(this, 'end'));
}
/**
+ * Inherits from `Stream.prototype`.
+ */
+
+Response.prototype.__proto__ = Stream.prototype;
+
+/**
* Set header related properties:
*
* - `.type` the content type without params
View
22 test/node/basic.js
@@ -297,4 +297,26 @@ describe('request', function(){
});
})
})
+
+ describe('.buffer(false)', function(){
+ it('should disable buffering', function(done){
+ request
+ .post('http://localhost:3000/echo')
+ .type('application/x-dog')
+ .send('hello this is dog')
+ .buffer(false)
+ .end(function(err, res){
+ assert(null == err);
+ assert(null == res.text);
+ res.body.should.eql({});
+ var buf = '';
+ res.setEncoding('utf8');
+ res.on('data', function(chunk){ buf += chunk });
+ res.on('end', function(){
+ buf.should.equal('hello this is dog');
+ done();
+ });
+ });
+ })
+ })
})

0 comments on commit 3ccdbad

Please sign in to comment.
Something went wrong with that request. Please try again.