Skip to content

Commit

Permalink
fix(HttpClient): attach readable event after data to keep response st…
Browse files Browse the repository at this point in the history
…ream flow (#175)
  • Loading branch information
hekike committed Jun 7, 2018
1 parent 1edd6da commit b9b82f5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/HttpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,6 @@ function rawRequest(opts, cb) {

req.removeAllListeners('socket');

res.once('readable', function onReadable () {
eventTimes.firstByteAt = process.hrtime();
});

// End event is not emitted when stream is not consumed fully
// in our case we consume it see: res.on('data')
res.once('end', function onEnd () {
Expand All @@ -223,6 +219,15 @@ function rawRequest(opts, cb) {
});

emitResult((err || null), req, res);

// This has to be after res.on('data') in Node >= v10:
// Adding a 'readable' listener stay in non-flowing mode even after
// a 'data' event listener is added to the same stream:
// https://github.com/nodejs/node/commit/
// cf5f9867ff3e700dfd72519e7bdeb701e254317f
res.once('readable', function onReadable () {
eventTimes.firstByteAt = process.hrtime();
});
});
req.log = log;

Expand Down
16 changes: 16 additions & 0 deletions test/StringClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ describe('StringClient', function () {
SERVER.close(done);
});

it('should make a request',
function (done) {
SERVER.get('/ping', function (req, res, next) {
res.send('pong');
return next();
});

CLIENT.get({
path: '/ping'
}, function (err, req, res, data) {
assert.ifError(err);
assert.equal(data, 'pong');
return done();
});
});

it('should support decoding gzipped utf8 multibyte responses',
function (done) {
var payload = fs.readFileSync(path.join(
Expand Down

0 comments on commit b9b82f5

Please sign in to comment.