diff --git a/package.json b/package.json index fc1cfaf5a..a3214931d 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,6 @@ "dependencies": { "aws-sign2": "~0.6.0", "aws4": "^1.2.1", - "bl": "~1.1.2", "caseless": "~0.11.0", "combined-stream": "~1.0.5", "extend": "~3.0.0", diff --git a/request.js b/request.js index fe70a6ed2..8ed57b7f0 100644 --- a/request.js +++ b/request.js @@ -6,7 +6,6 @@ var http = require('http') , util = require('util') , stream = require('stream') , zlib = require('zlib') - , bl = require('bl') , hawk = require('hawk') , aws2 = require('aws-sign2') , aws4 = require('aws4') @@ -1005,14 +1004,16 @@ Request.prototype.onRequestResponse = function (response) { Request.prototype.readResponseBody = function (response) { var self = this debug('reading response\'s body') - var buffer = bl() + var buffers = [] + , bufferLength = 0 , strings = [] self.on('data', function (chunk) { - if (Buffer.isBuffer(chunk)) { - buffer.append(chunk) - } else { + if (!Buffer.isBuffer(chunk)) { strings.push(chunk) + } else if (chunk.length) { + bufferLength += chunk.length + buffers.push(chunk) } }) self.on('end', function () { @@ -1021,22 +1022,21 @@ Request.prototype.readResponseBody = function (response) { debug('aborted', self.uri.href) // `buffer` is defined in the parent scope and used in a closure it exists for the life of the request. // This can lead to leaky behavior if the user retains a reference to the request object. - buffer.destroy() + buffers = [] + bufferLength = 0 return } - if (buffer.length) { - debug('has body', self.uri.href, buffer.length) - if (self.encoding === null) { - // response.body = buffer - // can't move to this until https://github.com/rvagg/bl/issues/13 - response.body = buffer.slice() - } else { - response.body = buffer.toString(self.encoding) + if (bufferLength) { + debug('has body', self.uri.href, bufferLength) + response.body = Buffer.concat(buffers, bufferLength) + if (self.encoding !== null) { + response.body = response.body.toString(self.encoding) } // `buffer` is defined in the parent scope and used in a closure it exists for the life of the Request. // This can lead to leaky behavior if the user retains a reference to the request object. - buffer.destroy() + buffers = [] + bufferLength = 0 } else if (strings.length) { // The UTF8 BOM [0xEF,0xBB,0xBF] is converted to [0xFE,0xFF] in the JS UTC16/UCS2 representation. // Strip this value out when the encoding is set to 'utf8', as upstream consumers won't expect it and it breaks JSON.parse().