Permalink
Browse files

Avoid useless Buffer transformation

  • Loading branch information...
michelsalib committed Aug 17, 2015
1 parent e168bc6 commit 478e0c2766fab310b199c1b0b07d7a2197f070f1
Showing with 11 additions and 6 deletions.
  1. +11 −6 request.js
View
@@ -432,13 +432,18 @@ Request.prototype.init = function (options) {
}
function setContentLength () {
if (!Buffer.isBuffer(self.body) && !Array.isArray(self.body) && typeof self.body !== 'object') {
self.body = new Buffer(self.body)
}
if (!self.hasHeader('content-length')) {
var length = (Array.isArray(self.body))
? self.body.reduce(function (a, b) {return a + b.length}, 0)
: self.body.length
var length
if (typeof self.body === 'string') {
length = Buffer.byteLength(self.body)
}
else if (Array.isArray(self.body)) {
length = self.body.reduce(function (a, b) {return a + b.length}, 0)
}
else {
length = self.body.length
}
if (length) {
self.setHeader('content-length', length)
} else {

0 comments on commit 478e0c2

Please sign in to comment.