4

I am using node.js to do some interactions with an API that returns gzipped data. I browsed through the package manager and the wiki for a good compression library but couldn't find one that hadn't been abandoned / didn't work at all. Any idea how I can either deflate the compressed data using javascript or node? (Or how to avoid the data all together?)

Here is what I have with comments:

app.get('/', function(req, res){
    // rest is a restler instance
    rest.get('http://api.stackoverflow.com/1.1/questions?type=jsontext', {
            headers: {"Accept-Encoding": 'deflate'}, 
            //tried deflate, gzip, etc. No changes
    }).on('complete', function(data) {
             // If I do: sys.puts(data); I get an exception
             // Maybe I could do something like this:
             /*
             var child = exec("gunzip " + data,
                    function(error, stdout, stderr) {
                            console.log('stdout: ' + stdout);
                            console.log('stderr: ' + stderr);
                             if (error !== null) {
                                    console.log('exec error: ' + error);
                            }
                     }); 
             */
    });

});

Swift
  • 13,118
  • 5
  • 56
  • 80
  • Related: http://stackoverflow.com/questions/4594654/node-js-proxy-dealing-with-gzip-decompression – j.w.r Jun 20 '11 at 20:08

1 Answers1

4

I used this one with success:

https://github.com/waveto/node-compress

this.get = function(options, cb){
  http.get({host: 'api.stackoverflow.com', path:'/1.1/questions?' + querystring.stringify(vectorize(options || {}))}, function(res){
    var body = [];
    var gunzip = new compress.Gunzip();
    gunzip.init();

    res.setEncoding('binary');
    res
      .on('data', function(chunk){
        body.push(gunzip.inflate(chunk, 'binary'));
      })
      .on('end', function(){
        console.log(res.headers);
        gunzip.end();
        cb(null, JSON.parse(body.join('')));
      });
  }).on('error', function(e){
    cb(e);
  })
}
Geoff Chappell
  • 2,432
  • 1
  • 16
  • 12
  • Using what version of node? That project still references posix (now fs) which changed back in February... – Swift Jun 20 '11 at 21:40
  • I guess only the samples and docs reference posix. The module itself is just a wrapper around zlib. I'm using node v0.4.8 – Geoff Chappell Jun 20 '11 at 22:08