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

Convert typed arrays into regular buffers #1905

Merged
merged 1 commit into from Nov 17, 2015
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: 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