Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Implement ReadableStream in the Response prototype. #177

Merged
merged 2 commits into from

2 participants

@lbdremy

Hello @visionmedia,

I just added the methods and bound the relevant events to Response, so it behaves like a Readable Stream.

Cheers

@tj tj merged commit b6ec030 into visionmedia:master
@tj
Owner
tj commented

hmm im getting:

  ․․

  ✖ 2 of 1 test failed:

  1) response should act as a readable stream:
     Error: Parse Error
      at Socket.socketOnData (http.js:1447:20)
      at TCP.onread (net.js:404:27)

  2) response should act as a readable stream:
     Error: Parse Error
      at Socket.socketOnData (http.js:1447:20)
      at TCP.onread (net.js:404:27)
@tj
Owner
tj commented

nvm that was just shouldjs apparently

@lbdremy

how did you fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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();
+ });
+ });
+});
Something went wrong with that request. Please try again.