Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle buffers directly instead of using "bl" #2424

Merged
merged 1 commit into from Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: perhaps to be consistent with the changes down below, bufferLength = 0 should be added after this line as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

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