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

support node buffers less than 8192 bytes #773

Merged
merged 1 commit into from Apr 8, 2017
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
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