mirror of
https://github.com/tenrok/axios.git
synced 2026-06-14 18:42:33 +03:00
Merge pull request #296 from nickuraltsev/stream
Adding support for Stream with HTTP adapter
This commit is contained in:
+42
-33
@@ -10,6 +10,13 @@ var zlib = require('zlib');
|
||||
var pkg = require('./../../package.json');
|
||||
var Buffer = require('buffer').Buffer;
|
||||
|
||||
// Resolve or reject the Promise based on the status
|
||||
function settle(resolve, reject, response) {
|
||||
(response.status >= 200 && response.status < 300 ?
|
||||
resolve :
|
||||
reject)(response);
|
||||
}
|
||||
|
||||
/*eslint consistent-return:0*/
|
||||
module.exports = function httpAdapter(resolve, reject, config) {
|
||||
var data = config.data;
|
||||
@@ -24,13 +31,13 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
||||
headers['User-Agent'] = 'axios/' + pkg.version;
|
||||
}
|
||||
|
||||
if (data) {
|
||||
if (data && !utils.isStream(data)) {
|
||||
if (utils.isArrayBuffer(data)) {
|
||||
data = new Buffer(new Uint8Array(data));
|
||||
} else if (utils.isString(data)) {
|
||||
data = new Buffer(data, 'utf-8');
|
||||
} else {
|
||||
return reject(new Error('Data after transformation must be a string or an ArrayBuffer'));
|
||||
return reject(new Error('Data after transformation must be a string, an ArrayBuffer, or a Stream'));
|
||||
}
|
||||
|
||||
// Add Content-Length header if data exists
|
||||
@@ -93,39 +100,37 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
||||
break;
|
||||
}
|
||||
|
||||
var responseBuffer = [];
|
||||
stream.on('data', function handleStreamData(chunk) {
|
||||
responseBuffer.push(chunk);
|
||||
var response = {
|
||||
status: res.statusCode,
|
||||
statusText: res.statusMessage,
|
||||
headers: res.headers,
|
||||
config: config,
|
||||
request: req
|
||||
};
|
||||
|
||||
// make sure the content length is not over the maxContentLength if specified
|
||||
if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
|
||||
reject(new Error('maxContentLength size of ' + config.maxContentLength + ' exceeded'));
|
||||
}
|
||||
});
|
||||
if (config.responseType === 'stream') {
|
||||
response.data = stream;
|
||||
settle(resolve, reject, response);
|
||||
} else {
|
||||
var responseBuffer = [];
|
||||
stream.on('data', function handleStreamData(chunk) {
|
||||
responseBuffer.push(chunk);
|
||||
|
||||
stream.on('end', function handleStreamEnd() {
|
||||
var d = Buffer.concat(responseBuffer);
|
||||
if (config.responseType !== 'arraybuffer') {
|
||||
d = d.toString('utf8');
|
||||
}
|
||||
var response = {
|
||||
data: transformData(
|
||||
d,
|
||||
res.headers,
|
||||
config.transformResponse
|
||||
),
|
||||
status: res.statusCode,
|
||||
statusText: res.statusMessage,
|
||||
headers: res.headers,
|
||||
config: config,
|
||||
request: req
|
||||
};
|
||||
// make sure the content length is not over the maxContentLength if specified
|
||||
if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
|
||||
reject(new Error('maxContentLength size of ' + config.maxContentLength + ' exceeded'));
|
||||
}
|
||||
});
|
||||
|
||||
// Resolve or reject the Promise based on the status
|
||||
(res.statusCode >= 200 && res.statusCode < 300 ?
|
||||
resolve :
|
||||
reject)(response);
|
||||
});
|
||||
stream.on('end', function handleStreamEnd() {
|
||||
var responseData = Buffer.concat(responseBuffer);
|
||||
if (config.responseType !== 'arraybuffer') {
|
||||
responseData = responseData.toString('utf8');
|
||||
}
|
||||
response.data = transformData(responseData, res.headers, config.transformResponse);
|
||||
settle(resolve, reject, response);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Handle errors
|
||||
@@ -147,5 +152,9 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
||||
}
|
||||
|
||||
// Send the request
|
||||
req.end(data);
|
||||
if (utils.isStream(data)) {
|
||||
data.pipe(req);
|
||||
} else {
|
||||
req.end(data);
|
||||
}
|
||||
};
|
||||
|
||||
+3
-6
@@ -8,11 +8,8 @@ var DEFAULT_CONTENT_TYPE = {
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
transformRequest: [function transformRequestJSON(data, headers) {
|
||||
if (utils.isFormData(data)) {
|
||||
return data;
|
||||
}
|
||||
if (utils.isArrayBuffer(data)) {
|
||||
transformRequest: [function transformRequest(data, headers) {
|
||||
if (utils.isFormData(data) || utils.isArrayBuffer(data) || utils.isStream(data)) {
|
||||
return data;
|
||||
}
|
||||
if (utils.isArrayBufferView(data)) {
|
||||
@@ -36,7 +33,7 @@ module.exports = {
|
||||
return data;
|
||||
}],
|
||||
|
||||
transformResponse: [function transformResponseJSON(data) {
|
||||
transformResponse: [function transformResponse(data) {
|
||||
/*eslint no-param-reassign:0*/
|
||||
if (typeof data === 'string') {
|
||||
data = data.replace(PROTECTION_PREFIX, '');
|
||||
|
||||
@@ -122,6 +122,26 @@ function isBlob(val) {
|
||||
return toString.call(val) === '[object Blob]';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is a Function
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @returns {boolean} True if value is a Function, otherwise false
|
||||
*/
|
||||
function isFunction(val) {
|
||||
return toString.call(val) === '[object Function]';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is a Stream
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @returns {boolean} True if value is a Stream, otherwise false
|
||||
*/
|
||||
function isStream(val) {
|
||||
return isObject(val) && isFunction(val.pipe);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim excess whitespace off the beginning and end of a string
|
||||
*
|
||||
@@ -237,6 +257,8 @@ module.exports = {
|
||||
isDate: isDate,
|
||||
isFile: isFile,
|
||||
isBlob: isBlob,
|
||||
isFunction: isFunction,
|
||||
isStream: isStream,
|
||||
isStandardBrowserEnv: isStandardBrowserEnv,
|
||||
forEach: forEach,
|
||||
merge: merge,
|
||||
|
||||
Reference in New Issue
Block a user