Created
May 4, 2013 00:24
-
-
Save nickfishman/5515364 to your computer and use it in GitHub Desktop.
Third attempt at conditional gzip decoding based on the Content-Encoding response header with the mikeal/request library for Node.js. Based on Mikeal's code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'), | |
zlib = require('zlib'), | |
fs = require('fs'); | |
var headers = { | |
"accept-charset" : "ISO-8859-1,utf-8;q=0.7,*;q=0.3", | |
"accept-language" : "en-US,en;q=0.8", | |
"accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", | |
"user-agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2", | |
"accept-encoding" : "gzip,deflate", | |
} | |
var options = { | |
url: "http://google.com", | |
headers: headers | |
} | |
var compressedRequest = function(options, outStream) { | |
var req = request(options) | |
req.on('response', function (res) { | |
if (res.statusCode !== 200) throw new Error('Status not 200') | |
var encoding = res.headers['content-encoding'] | |
if (encoding == 'gzip') { | |
res.pipe(zlib.createGunzip()).pipe(outStream) | |
} else if (encoding == 'deflate') { | |
res.pipe(zlib.createInflate()).pipe(outStream) | |
} else { | |
res.pipe(outStream) | |
} | |
}) | |
req.on('error', function(err) { | |
throw err; | |
}) | |
} | |
// Dummy write stream. Substitute with any other writeable stream | |
var outStream = fs.createWriteStream('./sample.html') | |
compressedRequest(options, outStream) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment