diff --git a/lib/twitter.js b/lib/twitter.js index 757ab18..0c784b4 100644 --- a/lib/twitter.js +++ b/lib/twitter.js @@ -2,6 +2,7 @@ var request = require('request') , split = require('split') , Writable = require('stream').Writable , util = require('util') + , zlib = require('zlib') function backoff (current, max, step, _value) { return function () { @@ -19,9 +20,9 @@ var FILTER_TYPE_TRACKING = 'tracking' , FILTER_TYPE_FOLLOW = 'follow' , FILTER_TYPE_LANGUAGE = 'language' -var Twitter = function (oauth) { +var Twitter = function (oauth, options) { if(!(this instanceof Twitter)) { - return new Twitter(oauth) + return new Twitter(oauth, options) } if (!oauth || !oauth.consumer_secret || !oauth.consumer_key || !oauth.token || !oauth.token_secret) { @@ -29,6 +30,9 @@ var Twitter = function (oauth) { } this.oauth = oauth + options.gzip = options.gzip ? options.gzip : false; + this.options = options; + this._filters = { tracking: {}, location: {}, @@ -209,6 +213,7 @@ Twitter.prototype.connect = function () { this.stream = request.post({ url: this.twitterUrl, oauth: this.oauth, + gzip: this.options.gzip, form: { track: Object.keys(this._filters[FILTER_TYPE_TRACKING]).join(','), locations: Object.keys(this._filters[FILTER_TYPE_LOCATION]).join(','), @@ -252,8 +257,18 @@ Twitter.prototype.connect = function () { } catch (e) {} }) - this.parser = res.pipe(this.parser, {end: false}) - this.parser.pipe(this) + var encoding = res.headers['content-encoding'] + if (encoding === 'gzip') { + console.log('initializing gunzip'); + this.parser = res.pipe(zlib.createGunzip()).pipe(this.parser, {end: false}) + this.parser.pipe(this) + } else if (encoding === 'deflate') { + this.parser = res.pipe(zlib.createInflate()).pipe(this.parser, {end: false}) + this.parser.pipe(this) + } else { + this.parser = res.pipe(this.parser, {end: false}) + this.parser.pipe(this) + } // Handle this: https://dev.twitter.com/docs/streaming-apis/connecting#Stalls // Abort the connection and reconnect if we haven't received an update for 90 seconds