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

Avoid useless Buffer transformation #1733

Merged
merged 1 commit into from Aug 18, 2015
Merged
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
17 changes: 11 additions & 6 deletions request.js
Expand Up @@ -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 {
Expand Down