Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #177 from lbdremy/implements-stream-api-response

Implement ReadableStream in the Response prototype.
  • Loading branch information...
commit b6ec0302e8c7322eccd878456b14841d94214d01 2 parents 67b435e + cc7cf3b
@tj tj authored
Showing with 58 additions and 0 deletions.
  1. +21 −0 lib/node/response.js
  2. +37 −0 test/node/response-readable-stream.js
View
21 lib/node/response.js
@@ -21,6 +21,9 @@ module.exports = Response;
* @param {ClientRequest} req
* @param {IncomingMessage} res
* @param {Object} options
+ * @constructor
+ * @extends {Stream}
+ * @implements {ReadableStream}
* @api private
*/
@@ -39,6 +42,8 @@ function Response(req, res, options) {
this.setEncoding = res.setEncoding.bind(res);
res.on('data', this.emit.bind(this, 'data'));
res.on('end', this.emit.bind(this, 'end'));
+ res.on('close', this.emit.bind(this, 'close'));
+ res.on('error', this.emit.bind(this, 'error'));
}
/**
@@ -48,6 +53,22 @@ function Response(req, res, options) {
Response.prototype.__proto__ = Stream.prototype;
/**
+ * Implements methods of a `ReadableStream`
+ */
+
+Response.prototype.destroy = function(err){
+ this.res.destroy(err);
+};
+
+Response.prototype.pause = function(){
+ this.res.pause();
+};
+
+Response.prototype.resume = function(){
+ this.res.resume();
+};
+
+/**
* Return an `Error` representative of this response.
*
* @return {Error}
View
37 test/node/response-readable-stream.js
@@ -0,0 +1,37 @@
+
+var request = require('../../')
+ , express = require('express')
+ , app = express()
+ , fs = require('fs');
+
+app.get('/', function(req, res){
+ fs.createReadStream('test/node/fixtures/user.json').pipe(res);
+});
+
+app.listen(3025);
+
+describe('response', function(){
+ it('should act as a readable stream', function(done){
+ var req = request
+ .get('http://localhost:3025')
+ .buffer(false);
+
+ req.end(function(err,res){
+ if(err) return done(err);
+ var trackEndEvent = 0;
+ var trackCloseEvent = 0;
+ res.on('end',function(){
+ trackEndEvent++;
+ });
+ res.on('close',function(){
+ trackCloseEvent++;
+ trackEndEvent.should.equal(1);
+ trackCloseEvent.should.equal(1);
+ done();
+ });
+ (function(){res.pause();}).should.not.throw();
+ (function(){res.resume();}).should.not.throw();
+ (function(){res.destroy();}).should.not.throw();
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.