Swift
Swift

Reputation: 13188

Gunzip an API stream using javascript / node?

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);
                            }
                     }); 
             */
    });

});

Upvotes: 4

Views: 1235

Answers (1)

Geoff Chappell
Geoff Chappell

Reputation: 2442

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);
  })
}

Upvotes: 4

Related Questions