mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Merge pull request #296 from nickuraltsev/stream
Adding support for Stream with HTTP adapter
This commit is contained in:
@@ -191,7 +191,7 @@ These are the available config options for making requests. Only the `url` is re
|
|||||||
|
|
||||||
// `transformRequest` allows changes to the request data before it is sent to the server
|
// `transformRequest` allows changes to the request data before it is sent to the server
|
||||||
// This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
// This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
||||||
// The last function in the array must return a string or an ArrayBuffer
|
// The last function in the array must return a string, an ArrayBuffer, or a Stream
|
||||||
transformRequest: [function (data) {
|
transformRequest: [function (data) {
|
||||||
// Do whatever you want to transform the data
|
// Do whatever you want to transform the data
|
||||||
|
|
||||||
@@ -222,7 +222,7 @@ These are the available config options for making requests. Only the `url` is re
|
|||||||
|
|
||||||
// `data` is the data to be sent as the request body
|
// `data` is the data to be sent as the request body
|
||||||
// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
||||||
// When no `transformRequest` is set, must be a string, an ArrayBuffer or a hash
|
// When no `transformRequest` is set, must be a string, an ArrayBuffer, a hash, or a Stream
|
||||||
data: {
|
data: {
|
||||||
firstName: 'Fred'
|
firstName: 'Fred'
|
||||||
},
|
},
|
||||||
@@ -250,7 +250,7 @@ These are the available config options for making requests. Only the `url` is re
|
|||||||
}
|
}
|
||||||
|
|
||||||
// `responseType` indicates the type of data that the server will respond with
|
// `responseType` indicates the type of data that the server will respond with
|
||||||
// options are 'arraybuffer', 'blob', 'document', 'json', 'text'
|
// options are 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
|
||||||
responseType: 'json', // default
|
responseType: 'json', // default
|
||||||
|
|
||||||
// `xsrfCookieName` is the name of the cookie to use as a value for xsrf token
|
// `xsrfCookieName` is the name of the cookie to use as a value for xsrf token
|
||||||
|
|||||||
+42
-33
@@ -10,6 +10,13 @@ var zlib = require('zlib');
|
|||||||
var pkg = require('./../../package.json');
|
var pkg = require('./../../package.json');
|
||||||
var Buffer = require('buffer').Buffer;
|
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*/
|
/*eslint consistent-return:0*/
|
||||||
module.exports = function httpAdapter(resolve, reject, config) {
|
module.exports = function httpAdapter(resolve, reject, config) {
|
||||||
var data = config.data;
|
var data = config.data;
|
||||||
@@ -24,13 +31,13 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
|||||||
headers['User-Agent'] = 'axios/' + pkg.version;
|
headers['User-Agent'] = 'axios/' + pkg.version;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data) {
|
if (data && !utils.isStream(data)) {
|
||||||
if (utils.isArrayBuffer(data)) {
|
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(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
|
// Add Content-Length header if data exists
|
||||||
@@ -93,39 +100,37 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var responseBuffer = [];
|
var response = {
|
||||||
stream.on('data', function handleStreamData(chunk) {
|
status: res.statusCode,
|
||||||
responseBuffer.push(chunk);
|
statusText: res.statusMessage,
|
||||||
|
headers: res.headers,
|
||||||
|
config: config,
|
||||||
|
request: req
|
||||||
|
};
|
||||||
|
|
||||||
// make sure the content length is not over the maxContentLength if specified
|
if (config.responseType === 'stream') {
|
||||||
if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
|
response.data = stream;
|
||||||
reject(new Error('maxContentLength size of ' + config.maxContentLength + ' exceeded'));
|
settle(resolve, reject, response);
|
||||||
}
|
} else {
|
||||||
});
|
var responseBuffer = [];
|
||||||
|
stream.on('data', function handleStreamData(chunk) {
|
||||||
|
responseBuffer.push(chunk);
|
||||||
|
|
||||||
stream.on('end', function handleStreamEnd() {
|
// make sure the content length is not over the maxContentLength if specified
|
||||||
var d = Buffer.concat(responseBuffer);
|
if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
|
||||||
if (config.responseType !== 'arraybuffer') {
|
reject(new Error('maxContentLength size of ' + config.maxContentLength + ' exceeded'));
|
||||||
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
|
|
||||||
};
|
|
||||||
|
|
||||||
// Resolve or reject the Promise based on the status
|
stream.on('end', function handleStreamEnd() {
|
||||||
(res.statusCode >= 200 && res.statusCode < 300 ?
|
var responseData = Buffer.concat(responseBuffer);
|
||||||
resolve :
|
if (config.responseType !== 'arraybuffer') {
|
||||||
reject)(response);
|
responseData = responseData.toString('utf8');
|
||||||
});
|
}
|
||||||
|
response.data = transformData(responseData, res.headers, config.transformResponse);
|
||||||
|
settle(resolve, reject, response);
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle errors
|
// Handle errors
|
||||||
@@ -147,5 +152,9 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send the request
|
// 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 = {
|
module.exports = {
|
||||||
transformRequest: [function transformRequestJSON(data, headers) {
|
transformRequest: [function transformRequest(data, headers) {
|
||||||
if (utils.isFormData(data)) {
|
if (utils.isFormData(data) || utils.isArrayBuffer(data) || utils.isStream(data)) {
|
||||||
return data;
|
|
||||||
}
|
|
||||||
if (utils.isArrayBuffer(data)) {
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
if (utils.isArrayBufferView(data)) {
|
if (utils.isArrayBufferView(data)) {
|
||||||
@@ -36,7 +33,7 @@ module.exports = {
|
|||||||
return data;
|
return data;
|
||||||
}],
|
}],
|
||||||
|
|
||||||
transformResponse: [function transformResponseJSON(data) {
|
transformResponse: [function transformResponse(data) {
|
||||||
/*eslint no-param-reassign:0*/
|
/*eslint no-param-reassign:0*/
|
||||||
if (typeof data === 'string') {
|
if (typeof data === 'string') {
|
||||||
data = data.replace(PROTECTION_PREFIX, '');
|
data = data.replace(PROTECTION_PREFIX, '');
|
||||||
|
|||||||
@@ -122,6 +122,26 @@ function isBlob(val) {
|
|||||||
return toString.call(val) === '[object Blob]';
|
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
|
* Trim excess whitespace off the beginning and end of a string
|
||||||
*
|
*
|
||||||
@@ -237,6 +257,8 @@ module.exports = {
|
|||||||
isDate: isDate,
|
isDate: isDate,
|
||||||
isFile: isFile,
|
isFile: isFile,
|
||||||
isBlob: isBlob,
|
isBlob: isBlob,
|
||||||
|
isFunction: isFunction,
|
||||||
|
isStream: isStream,
|
||||||
isStandardBrowserEnv: isStandardBrowserEnv,
|
isStandardBrowserEnv: isStandardBrowserEnv,
|
||||||
forEach: forEach,
|
forEach: forEach,
|
||||||
merge: merge,
|
merge: merge,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
var utils = require('../../../lib/utils');
|
var utils = require('../../../lib/utils');
|
||||||
|
var Stream = require('stream');
|
||||||
|
|
||||||
describe('utils::isX', function () {
|
describe('utils::isX', function () {
|
||||||
it('should validate Array', function () {
|
it('should validate Array', function () {
|
||||||
@@ -67,5 +68,14 @@ describe('utils::isX', function () {
|
|||||||
expect(utils.isDate(new Date())).toEqual(true);
|
expect(utils.isDate(new Date())).toEqual(true);
|
||||||
expect(utils.isDate(Date.now())).toEqual(false);
|
expect(utils.isDate(Date.now())).toEqual(false);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
|
it('should validate Function', function () {
|
||||||
|
expect(utils.isFunction(function () {})).toEqual(true);
|
||||||
|
expect(utils.isFunction('function')).toEqual(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should validate Stream', function () {
|
||||||
|
expect(utils.isStream(new Stream.Readable())).toEqual(true);
|
||||||
|
expect(utils.isStream({ foo: 'bar' })).toEqual(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ var axios = require('../../../index');
|
|||||||
var http = require('http');
|
var http = require('http');
|
||||||
var url = require('url');
|
var url = require('url');
|
||||||
var zlib = require('zlib');
|
var zlib = require('zlib');
|
||||||
|
var fs = require('fs');
|
||||||
var server;
|
var server;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@@ -127,7 +128,7 @@ module.exports = {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
testMaxContentLength: function(test) {
|
testMaxContentLength: function(test) {
|
||||||
var str = Array(100000).join('ж');
|
var str = Array(100000).join('ж');
|
||||||
|
|
||||||
@@ -145,7 +146,7 @@ module.exports = {
|
|||||||
error = res;
|
error = res;
|
||||||
failure = true;
|
failure = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
test.equal(success, false, 'request should not succeed');
|
test.equal(success, false, 'request should not succeed');
|
||||||
test.equal(failure, true, 'request should fail');
|
test.equal(failure, true, 'request should fail');
|
||||||
@@ -153,5 +154,26 @@ module.exports = {
|
|||||||
test.done();
|
test.done();
|
||||||
}, 100);
|
}, 100);
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
testStream: function(test) {
|
||||||
|
server = http.createServer(function (req, res) {
|
||||||
|
req.pipe(res);
|
||||||
|
}).listen(4444, function () {
|
||||||
|
axios.post('http://localhost:4444/',
|
||||||
|
fs.createReadStream(__filename), {
|
||||||
|
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, fs.readFileSync(__filename, 'utf8'));
|
||||||
|
test.done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user