Skip to content

Commit

Permalink
Merge pull request #1905 from simov/fix-body-buffer
Browse files Browse the repository at this point in the history
Convert typed arrays into regular buffers
  • Loading branch information
simov committed Nov 17, 2015
2 parents 4ebc955 + cb5b7ac commit a7f7e75
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -40,6 +40,7 @@
"stringstream": "~0.0.4",
"combined-stream": "~1.0.5",
"isstream": "~0.1.2",
"is-typedarray": "~1.0.0",
"har-validator": "~2.0.2"
},
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions request.js
Expand Up @@ -15,6 +15,7 @@ var http = require('http')
, caseless = require('caseless')
, ForeverAgent = require('forever-agent')
, FormData = require('form-data')
, isTypedArray = require('is-typedarray').strict
, helpers = require('./lib/helpers')
, cookies = require('./lib/cookies')
, getProxyFromURI = require('./lib/getProxyFromURI')
Expand Down Expand Up @@ -427,6 +428,10 @@ Request.prototype.init = function (options) {
}

function setContentLength () {
if (isTypedArray(self.body)) {
self.body = new Buffer(self.body)
}

if (!self.hasHeader('content-length')) {
var length
if (typeof self.body === 'string') {
Expand Down
20 changes: 20 additions & 0 deletions tests/test-body.js
Expand Up @@ -3,6 +3,7 @@
var server = require('./server')
, request = require('../index')
, tape = require('tape')
, http = require('http')

var s = server.createServer()

Expand Down Expand Up @@ -144,6 +145,25 @@ addTest('testPutMultipartPostambleCRLF', {
]
})

tape('typed array', function (t) {
var server = http.createServer()
server.on('request', function (req, res) {
req.pipe(res)
})
server.listen(6768, function () {
var data = new Uint8Array([1, 2, 3])
request({
uri: 'http://localhost:6768',
method: 'POST',
body: data,
encoding: null
}, function (err, res, body) {
t.deepEqual(new Buffer(data), body)
server.close(t.end)
})
})
})

tape('cleanup', function(t) {
s.close(function() {
t.end()
Expand Down

0 comments on commit a7f7e75

Please sign in to comment.