Skip to content

Commit

Permalink
support node buffers less than 8192 bytes (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerfowler authored and rubennorte committed Apr 8, 2017
1 parent bbfbeff commit 1883344
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/adapters/http.js
Expand Up @@ -30,13 +30,15 @@ module.exports = function httpAdapter(config) {
}

if (data && !utils.isStream(data)) {
if (utils.isArrayBuffer(data)) {
if (utils.isBuffer(data)) {
// Nothing to do...
} else if (utils.isArrayBuffer(data)) {
data = new Buffer(new Uint8Array(data));
} else if (utils.isString(data)) {
data = new Buffer(data, 'utf-8');
} else {
return reject(createError(
'Data after transformation must be a string, an ArrayBuffer, or a Stream',
'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
config
));
}
Expand Down
1 change: 1 addition & 0 deletions lib/defaults.js
Expand Up @@ -32,6 +32,7 @@ var defaults = {
normalizeHeaderName(headers, 'Content-Type');
if (utils.isFormData(data) ||
utils.isArrayBuffer(data) ||
utils.isBuffer(data) ||
utils.isStream(data) ||
utils.isFile(data) ||
utils.isBlob(data)
Expand Down
11 changes: 11 additions & 0 deletions lib/utils.js
Expand Up @@ -18,6 +18,16 @@ function isArray(val) {
return toString.call(val) === '[object Array]';
}

/**
* Determine if a value is a Node Buffer
*
* @param {Object} val The value to test
* @returns {boolean} True if value is a Node Buffer, otherwise false
*/
function isBuffer(val) {
return ((typeof Buffer !== 'undefined') && (Buffer.isBuffer) && (Buffer.isBuffer(val)));
}

/**
* Determine if a value is an ArrayBuffer
*
Expand Down Expand Up @@ -281,6 +291,7 @@ function extend(a, b, thisArg) {
module.exports = {
isArray: isArray,
isArrayBuffer: isArrayBuffer,
isBuffer: isBuffer,
isFormData: isFormData,
isArrayBufferView: isArrayBufferView,
isString: isString,
Expand Down
24 changes: 24 additions & 0 deletions test/unit/adapters/http.js
Expand Up @@ -248,6 +248,30 @@ module.exports = {
});
},

testBuffer: function(test) {
var buf = new Buffer(1024); // Unsafe buffer < Buffer.poolSize (8192 bytes)
buf.fill('x');
server = http.createServer(function (req, res) {
test.equal(req.headers['content-length'], buf.length.toString());
req.pipe(res);
}).listen(4444, function () {
axios.post('http://localhost:4444/',
buf, {
responseType: 'stream'
}).then(function (res) {
var stream = res.data;
var string = '';
stream.on('data', function (chunk) {
string += chunk.toString('utf8');
});
stream.on('end', function () {
test.equal(string, buf.toString());
test.done();
});
});
});
},

testHTTPProxy: function(test) {
server = http.createServer(function(req, res) {
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
Expand Down

0 comments on commit 1883344

Please sign in to comment.