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

Moving to module instead of cutomer buffer concatenation. #1008

Merged
merged 4 commits into from
Aug 22, 2014
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
],
"main": "index.js",
"dependencies": {
"bl": "~0.9.0",
"caseless": "~0.6.0",
"forever-agent": "~0.5.0",
"qs": "~1.2.0",
Expand All @@ -41,5 +42,8 @@
},
"scripts": {
"test": "node tests/run.js"
},
"devDependencies": {
"rimraf": "~2.2.8"
}
}
34 changes: 16 additions & 18 deletions request.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var optional = require('./lib/optional')
, crypto = require('crypto')
, zlib = require('zlib')

, bl = require('bl')
, oauth = optional('oauth-sign')
, hawk = optional('hawk')
, aws = optional('aws-sign2')
Expand Down Expand Up @@ -1002,11 +1003,12 @@ Request.prototype.onResponse = function (response) {
dataStream.on("close", function () {self.emit("close")})

if (self.callback) {
var buffer = []
var bodyLen = 0
var buffer = bl()
, strings = []
;
self.on("data", function (chunk) {
buffer.push(chunk)
bodyLen += chunk.length
if (Buffer.isBuffer(chunk)) buffer.append(chunk)
else strings.push(chunk)
})
self.on("end", function () {
debug('end event', self.uri.href)
Expand All @@ -1015,26 +1017,22 @@ Request.prototype.onResponse = function (response) {
return
}

if (buffer.length && Buffer.isBuffer(buffer[0])) {
debug('has body', self.uri.href, bodyLen)
var body = new Buffer(bodyLen)
var i = 0
buffer.forEach(function (chunk) {
chunk.copy(body, i, 0, chunk.length)
i += chunk.length
})
if (buffer.length) {
debug('has body', self.uri.href)
Copy link
Contributor

Choose a reason for hiding this comment

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

is this debug message correct? (body != uri.href)

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually don't know when this originally came from.

Copy link

Choose a reason for hiding this comment

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

Thank you
On Aug 22, 2014 2:03 AM, "Fred K. Schott" notifications@github.com wrote:

In request.js:

@@ -1015,26 +1017,22 @@ Request.prototype.onResponse = function (response) {
return
}

  •    if (buffer.length && Buffer.isBuffer(buffer[0])) {
    
  •      debug('has body', self.uri.href, bodyLen)
    
  •      var body = new Buffer(bodyLen)
    
  •      var i = 0
    
  •      buffer.forEach(function (chunk) {
    
  •        chunk.copy(body, i, 0, chunk.length)
    
  •        i += chunk.length
    
  •      })
    
  •    if (buffer.length) {
    
  •      debug('has body', self.uri.href)
    

is this debug message correct? (body != uri.href)

Reply to this email directly or view it on GitHub
https://github.com/mikeal/request/pull/1008/files#r16574603.

if (self.encoding === null) {
response.body = body
// response.body = buffer
// can't move to this until https://github.com/rvagg/bl/issues/13
response.body = buffer.slice()
} else {
response.body = body.toString(self.encoding)
response.body = buffer.toString(self.encoding)
}
} else if (buffer.length) {
} 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().
if (self.encoding === 'utf8' && buffer[0].length > 0 && buffer[0][0] === "\uFEFF") {
buffer[0] = buffer[0].substring(1)
if (self.encoding === 'utf8' && strings[0].length > 0 && strings[0][0] === "\uFEFF") {
strings[0] = strings[0].substring(1)
}
response.body = buffer.join('')
response.body = strings.join('')
}

if (self._json) {
Expand Down
3 changes: 1 addition & 2 deletions tests/test-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ s.listen(s.port, function () {
request(test, function (err, resp, body) {
if (err) throw err
if (test.expectBody) {
assert.deepEqual(test.expectBody, body)
if (Buffer.isBuffer(test.expectBody)) assert.deepEqual(test.expectBody.toString(), body.toString())
}
counter = counter - 1;
if (counter === 0) {
Expand All @@ -119,4 +119,3 @@ s.listen(s.port, function () {
})()
}
})

2 changes: 1 addition & 1 deletion tests/test-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ s.listen(s.port, function () {
request(s.url + '/' + i, test, function (err, resp, body) {
if (err) throw err
if (test.expectBody) {
assert.deepEqual(test.expectBody, body)
if (Buffer.isBuffer(test.expectBody))assert.deepEqual(test.expectBody.toString(), body.toString())
}
counter = counter - 1;
if (counter === 0) {
Expand Down
11 changes: 7 additions & 4 deletions tests/test-unix.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ var assert = require('assert')
, request = require('../index')
, http = require('http')
, fs = require('fs')
, rimraf = require('rimraf')
;

var path = [null, 'test', 'path'].join('/');
var socket = [__dirname, 'tmp-socket'].join('/');
var body = 'connected';
var statusCode = 200;

rimraf.sync(socket)

var s = http.createServer(function(req, res) {
// Assert requested path is sent to server
assert.equal(req.url, path);
Expand All @@ -19,13 +22,13 @@ var s = http.createServer(function(req, res) {
request(['unix://', socket, path].join(''), function (error, response, response_body) {
// Assert no error in connection
assert.equal(error, null);
// Assert http success status code
// Assert http success status code
assert.equal(response.statusCode, statusCode);
// Assert expected response body is recieved
assert.equal(response_body, body);
// clean up
s.close();
fs.unlink(socket, function(){});
})
})

})