Skip to content

Commit

Permalink
Merge pull request #2424 from zertosh/remove-bl
Browse files Browse the repository at this point in the history
Handle buffers directly instead of using "bl"
  • Loading branch information
simov committed Oct 25, 2016
2 parents 8c04a23 + 0d27170 commit fae254e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
30 changes: 15 additions & 15 deletions request.js
Expand Up @@ -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')
Expand Down Expand Up @@ -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 () {
Expand All @@ -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().
Expand Down

0 comments on commit fae254e

Please sign in to comment.