| |
| |
| |
| |
| @@ -3,6 +3,7 @@ const http = require('http'); |
| const https = require('https'); |
| const xml2js = require('xml2js'); |
| const url = require('url'); |
| +const zlib = require('zlib'); |
| |
| const fields = require('./fields'); |
| const utils = require('./utils'); |
| @@ -88,14 +89,27 @@ class Parser { |
| return reject(new Error("Status code " + res.statusCode)) |
| } |
| let encoding = utils.getEncodingFromContentType(res.headers['content-type']); |
| - res.setEncoding(encoding); |
| - res.on('data', (chunk) => { |
| - xml += chunk; |
| - }); |
| - res.on('end', () => { |
| - return this.parseString(xml).then(resolve, reject); |
| - }); |
| - }) |
| + const contentEncoding = (res.headers['content-encoding'] || '').toLowerCase(); |
| + if (contentEncoding === 'gzip') { |
| + const gunzip = zlib.createGunzip(); |
| + const chunks = []; |
| + gunzip.on('data', (chunk) => chunks.push(chunk)); |
| + gunzip.on('end', () => { |
| + const decompressed = Buffer.concat(chunks).toString(encoding); |
| + return this.parseString(decompressed).then(resolve, reject); |
| + }); |
| + gunzip.on('error', reject); |
| + res.pipe(gunzip); |
| + } else { |
| + res.setEncoding(encoding); |
| + res.on('data', (chunk) => { |
| + xml += chunk; |
| + }); |
| + res.on('end', () => { |
| + return this.parseString(xml).then(resolve, reject); |
| + }); |
| + } |
| + }); |
| req.on('error', reject); |
| timeout = setTimeout(() => { |
| return reject(new Error("Request timed out after " + this.options.timeout + "ms")); |
|
|