From 5f206ae5d749f46a6a44933ab08780a4ce7be124 Mon Sep 17 00:00:00 2001 From: Jeremy Fowler Date: Sun, 19 Mar 2017 00:09:11 -0500 Subject: [PATCH] support node buffers less than 8192 bytes --- lib/adapters/http.js | 6 ++++-- lib/defaults.js | 1 + lib/utils.js | 11 +++++++++++ test/unit/adapters/http.js | 24 ++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/adapters/http.js b/lib/adapters/http.js index 8a2fc57dac..23c599baaa 100644 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -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 )); } diff --git a/lib/defaults.js b/lib/defaults.js index 69fbad12da..9587b28b68 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -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) diff --git a/lib/utils.js b/lib/utils.js index 180820facc..df604da860 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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 * @@ -281,6 +291,7 @@ function extend(a, b, thisArg) { module.exports = { isArray: isArray, isArrayBuffer: isArrayBuffer, + isBuffer: isBuffer, isFormData: isFormData, isArrayBufferView: isArrayBufferView, isString: isString, diff --git a/test/unit/adapters/http.js b/test/unit/adapters/http.js index 131bce0032..829e2887d6 100644 --- a/test/unit/adapters/http.js +++ b/test/unit/adapters/http.js @@ -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');