Skip to content
This repository
Browse code

add buffer cancellation via .buffer(false)

stream proxy haCCCckkkkYYYY ... delegation suxxxorz
  • Loading branch information...
commit 3ccdbad7e9a477ba405c7a4df177c11f28b3aeab 1 parent 7a58c43
TJ Holowaychuk authored
2  Makefile
@@ -10,7 +10,7 @@ test:
10 10 @NODE_ENV=test ./node_modules/.bin/mocha \
11 11 --require should \
12 12 --reporter $(REPORTER) \
13   - --timeout 100 \
  13 + --timeout 200 \
14 14 --growl \
15 15 $(TESTS)
16 16
18 lib/node/index.js
@@ -375,21 +375,22 @@ Request.prototype.write = function(data, encoding){
375 375 */
376 376
377 377 Request.prototype.pipe = function(stream, options){
378   - this.preventBuffer();
  378 + this.buffer(false);
379 379 return this.end().req.on('response', function(res){
380 380 res.pipe(stream, options);
381 381 });
382 382 };
383 383
384 384 /**
385   - * Prevent buffering.
  385 + * Enable / disable buffering.
386 386 *
  387 + * @return {Boolean} val
387 388 * @return {Request} for chaining
388 389 * @api public
389 390 */
390 391
391   -Request.prototype.preventBuffer = function(){
392   - this._buffer = false;
  392 +Request.prototype.buffer = function(val){
  393 + this._buffer = val;
393 394 return this;
394 395 };
395 396
@@ -603,6 +604,15 @@ Request.prototype.end = function(fn){
603 604 });
604 605 }
605 606
  607 + // unbuffered
  608 + if (!buffer) {
  609 + self.res = res;
  610 + var response = new Response(self.req, self.res);
  611 + response.redirects = self._redirectList;
  612 + self.emit('response', response);
  613 + return;
  614 + }
  615 +
606 616 // end event
607 617 self.res = res;
608 618 res.on('end', function(){
12 lib/node/response.js
@@ -3,7 +3,8 @@
3 3 * Module dependencies.
4 4 */
5 5
6   -var utils = require('./utils');
  6 +var utils = require('./utils')
  7 + , Stream = require('stream');
7 8
8 9 /**
9 10 * Expose `Response`.
@@ -34,9 +35,18 @@ function Response(req, res, options) {
34 35 this.header = this.headers = res.headers;
35 36 this.setStatusProperties(res.statusCode);
36 37 this.setHeaderProperties(this.header);
  38 + this.setEncoding = res.setEncoding.bind(res);
  39 + res.on('data', this.emit.bind(this, 'data'));
  40 + res.on('end', this.emit.bind(this, 'end'));
37 41 }
38 42
39 43 /**
  44 + * Inherits from `Stream.prototype`.
  45 + */
  46 +
  47 +Response.prototype.__proto__ = Stream.prototype;
  48 +
  49 +/**
40 50 * Set header related properties:
41 51 *
42 52 * - `.type` the content type without params
22 test/node/basic.js
@@ -297,4 +297,26 @@ describe('request', function(){
297 297 });
298 298 })
299 299 })
  300 +
  301 + describe('.buffer(false)', function(){
  302 + it('should disable buffering', function(done){
  303 + request
  304 + .post('http://localhost:3000/echo')
  305 + .type('application/x-dog')
  306 + .send('hello this is dog')
  307 + .buffer(false)
  308 + .end(function(err, res){
  309 + assert(null == err);
  310 + assert(null == res.text);
  311 + res.body.should.eql({});
  312 + var buf = '';
  313 + res.setEncoding('utf8');
  314 + res.on('data', function(chunk){ buf += chunk });
  315 + res.on('end', function(){
  316 + buf.should.equal('hello this is dog');
  317 + done();
  318 + });
  319 + });
  320 + })
  321 + })
300 322 })

0 comments on commit 3ccdbad

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