Skip to content

Commit 1883344

Browse files
jerfowlerrubennorte
authored andcommitted
support node buffers less than 8192 bytes (axios#773)
1 parent bbfbeff commit 1883344

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

lib/adapters/http.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ module.exports = function httpAdapter(config) {
3030
}
3131

3232
if (data && !utils.isStream(data)) {
33-
if (utils.isArrayBuffer(data)) {
33+
if (utils.isBuffer(data)) {
34+
// Nothing to do...
35+
} else if (utils.isArrayBuffer(data)) {
3436
data = new Buffer(new Uint8Array(data));
3537
} else if (utils.isString(data)) {
3638
data = new Buffer(data, 'utf-8');
3739
} else {
3840
return reject(createError(
39-
'Data after transformation must be a string, an ArrayBuffer, or a Stream',
41+
'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
4042
config
4143
));
4244
}

lib/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var defaults = {
3232
normalizeHeaderName(headers, 'Content-Type');
3333
if (utils.isFormData(data) ||
3434
utils.isArrayBuffer(data) ||
35+
utils.isBuffer(data) ||
3536
utils.isStream(data) ||
3637
utils.isFile(data) ||
3738
utils.isBlob(data)

lib/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ function isArray(val) {
1818
return toString.call(val) === '[object Array]';
1919
}
2020

21+
/**
22+
* Determine if a value is a Node Buffer
23+
*
24+
* @param {Object} val The value to test
25+
* @returns {boolean} True if value is a Node Buffer, otherwise false
26+
*/
27+
function isBuffer(val) {
28+
return ((typeof Buffer !== 'undefined') && (Buffer.isBuffer) && (Buffer.isBuffer(val)));
29+
}
30+
2131
/**
2232
* Determine if a value is an ArrayBuffer
2333
*
@@ -281,6 +291,7 @@ function extend(a, b, thisArg) {
281291
module.exports = {
282292
isArray: isArray,
283293
isArrayBuffer: isArrayBuffer,
294+
isBuffer: isBuffer,
284295
isFormData: isFormData,
285296
isArrayBufferView: isArrayBufferView,
286297
isString: isString,

test/unit/adapters/http.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,30 @@ module.exports = {
248248
});
249249
},
250250

251+
testBuffer: function(test) {
252+
var buf = new Buffer(1024); // Unsafe buffer < Buffer.poolSize (8192 bytes)
253+
buf.fill('x');
254+
server = http.createServer(function (req, res) {
255+
test.equal(req.headers['content-length'], buf.length.toString());
256+
req.pipe(res);
257+
}).listen(4444, function () {
258+
axios.post('http://localhost:4444/',
259+
buf, {
260+
responseType: 'stream'
261+
}).then(function (res) {
262+
var stream = res.data;
263+
var string = '';
264+
stream.on('data', function (chunk) {
265+
string += chunk.toString('utf8');
266+
});
267+
stream.on('end', function () {
268+
test.equal(string, buf.toString());
269+
test.done();
270+
});
271+
});
272+
});
273+
},
274+
251275
testHTTPProxy: function(test) {
252276
server = http.createServer(function(req, res) {
253277
res.setHeader('Content-Type', 'text/html; charset=UTF-8');

0 commit comments

Comments
 (0)