Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -19,16 +20,19 @@ 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) {
throw new Error('Oauth credentials required')
}
this.oauth = oauth

options.gzip = options.gzip ? options.gzip : false;
this.options = options;

this._filters = {
tracking: {},
location: {},
Expand Down Expand Up @@ -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(','),
Expand Down Expand Up @@ -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
Expand Down