mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
Allow ArrayBuffer and related views as data
In order to push binary data under the form of ArrayBuffer and its related views (Int8Array, ...) one needs not to stringify those. For the XHR adapter there is nothing to do as it natively supports ArrayBuffer in req.send(). Node's http adapter supports only string or Buffer thus a transformation to Buffer is required before setting content length header.
This commit is contained in:
Vendored
+40
-4
@@ -161,10 +161,16 @@ var axios =
|
||||
|
||||
module.exports = {
|
||||
transformRequest: [function (data) {
|
||||
return utils.isObject(data) &&
|
||||
!utils.isFile(data) &&
|
||||
!utils.isBlob(data) ?
|
||||
JSON.stringify(data) : data;
|
||||
if (utils.isArrayBuffer(data)) {
|
||||
return data;
|
||||
}
|
||||
if (utils.isArrayBufferView(data)) {
|
||||
return data.buffer;
|
||||
}
|
||||
if (utils.isObject(data) && !utils.isFile(data) && !utils.isBlob(data)) {
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
return data;
|
||||
}],
|
||||
|
||||
transformResponse: [function (data) {
|
||||
@@ -208,6 +214,30 @@ var axios =
|
||||
return toString.call(val) === '[object Array]';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is an ArrayBuffer
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @returns {boolean} True if value is an ArrayBuffer, otherwise false
|
||||
*/
|
||||
function isArrayBuffer(val) {
|
||||
return toString.call(val) === '[object ArrayBuffer]';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is a view on an ArrayBuffer
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
|
||||
*/
|
||||
function isArrayBufferView(val) {
|
||||
if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) {
|
||||
return ArrayBuffer.isView(val);
|
||||
} else {
|
||||
return (val) && (val.buffer) && (val.buffer instanceof ArrayBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is a String
|
||||
*
|
||||
@@ -349,6 +379,8 @@ var axios =
|
||||
|
||||
module.exports = {
|
||||
isArray: isArray,
|
||||
isArrayBuffer: isArrayBuffer,
|
||||
isArrayBufferView: isArrayBufferView,
|
||||
isString: isString,
|
||||
isNumber: isNumber,
|
||||
isObject: isObject,
|
||||
@@ -483,6 +515,10 @@ var axios =
|
||||
}
|
||||
}
|
||||
|
||||
if (utils.isArrayBuffer(data)) {
|
||||
data = new DataView(data);
|
||||
}
|
||||
|
||||
// Send the request
|
||||
request.send(data);
|
||||
};
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user