mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40:40 +03:00
support node buffers less than 8192 bytes (#773)
This commit is contained in:
committed by
Rubén Norte
parent
bbfbeff4bc
commit
188334439f
@@ -30,13 +30,15 @@ module.exports = function httpAdapter(config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (data && !utils.isStream(data)) {
|
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));
|
data = new Buffer(new Uint8Array(data));
|
||||||
} else if (utils.isString(data)) {
|
} else if (utils.isString(data)) {
|
||||||
data = new Buffer(data, 'utf-8');
|
data = new Buffer(data, 'utf-8');
|
||||||
} else {
|
} else {
|
||||||
return reject(createError(
|
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
|
config
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ var defaults = {
|
|||||||
normalizeHeaderName(headers, 'Content-Type');
|
normalizeHeaderName(headers, 'Content-Type');
|
||||||
if (utils.isFormData(data) ||
|
if (utils.isFormData(data) ||
|
||||||
utils.isArrayBuffer(data) ||
|
utils.isArrayBuffer(data) ||
|
||||||
|
utils.isBuffer(data) ||
|
||||||
utils.isStream(data) ||
|
utils.isStream(data) ||
|
||||||
utils.isFile(data) ||
|
utils.isFile(data) ||
|
||||||
utils.isBlob(data)
|
utils.isBlob(data)
|
||||||
|
|||||||
@@ -18,6 +18,16 @@ function isArray(val) {
|
|||||||
return toString.call(val) === '[object Array]';
|
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
|
* Determine if a value is an ArrayBuffer
|
||||||
*
|
*
|
||||||
@@ -281,6 +291,7 @@ function extend(a, b, thisArg) {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
isArray: isArray,
|
isArray: isArray,
|
||||||
isArrayBuffer: isArrayBuffer,
|
isArrayBuffer: isArrayBuffer,
|
||||||
|
isBuffer: isBuffer,
|
||||||
isFormData: isFormData,
|
isFormData: isFormData,
|
||||||
isArrayBufferView: isArrayBufferView,
|
isArrayBufferView: isArrayBufferView,
|
||||||
isString: isString,
|
isString: isString,
|
||||||
|
|||||||
@@ -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) {
|
testHTTPProxy: function(test) {
|
||||||
server = http.createServer(function(req, res) {
|
server = http.createServer(function(req, res) {
|
||||||
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
|
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
|
||||||
|
|||||||
Reference in New Issue
Block a user