mirror of
https://github.com/tenrok/axios.git
synced 2026-05-21 13:24:11 +03:00
uncompress the response body transparently if required
This commit is contained in:
+18
-2
@@ -7,6 +7,7 @@ var transformData = require('./../helpers/transformData');
|
||||
var http = require('http');
|
||||
var https = require('https');
|
||||
var url = require('url');
|
||||
var zlib = require('zlib');
|
||||
var pkg = require('./../../package.json');
|
||||
var Buffer = require('buffer').Buffer;
|
||||
|
||||
@@ -59,12 +60,27 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
||||
// Create the request
|
||||
var transport = parsed.protocol === 'https:' ? https : http;
|
||||
var req = transport.request(options, function (res) {
|
||||
|
||||
// uncompress the response body transparently if required
|
||||
var stream = res;
|
||||
switch(res.headers['content-encoding']) {
|
||||
case 'gzip':
|
||||
case 'compress':
|
||||
case 'deflate': {
|
||||
// add the unzipper to the body stream processing pipeline
|
||||
stream = stream.pipe(zlib.createUnzip());
|
||||
|
||||
// remove the content-encoding in order to not confuse downstream operations
|
||||
delete res.headers['content-encoding'];
|
||||
}
|
||||
}
|
||||
|
||||
var responseBuffer = [];
|
||||
res.on('data', function (chunk) {
|
||||
stream.on('data', function (chunk) {
|
||||
responseBuffer.push(chunk);
|
||||
});
|
||||
|
||||
res.on('end', function () {
|
||||
stream.on('end', function () {
|
||||
var data = Buffer.concat(responseBuffer);
|
||||
if (config.responseType !== 'arraybuffer') {
|
||||
data = data.toString('utf8');
|
||||
|
||||
Reference in New Issue
Block a user