user-stream
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -64,3 +64,2 @@ var oauth = require('oauth'), | ||
//required params for lib | ||
params.delimited = 'length'; | ||
params.stall_warnings = 'true'; | ||
@@ -86,3 +85,3 @@ | ||
request.on('response', function(response) { | ||
// Any response code greater then 200 from steam API is an error | ||
@@ -95,16 +94,14 @@ if(response.statusCode > 200) { | ||
var buffer = '', | ||
next_data_length = 0, | ||
end = '\r\n'; | ||
//emit connected event | ||
stream.emit('connected'); | ||
//set chunk encoding | ||
response.setEncoding('utf8'); | ||
var data = ''; | ||
response.on('data', function(chunk) { | ||
//is heartbeat? | ||
if (chunk == end) { | ||
data += chunk.toString('utf8'); | ||
//is heartbeat? | ||
if (data == '\r\n') { | ||
stream.emit('heartbeat'); | ||
@@ -114,40 +111,17 @@ return; | ||
//check whether new incomming data set | ||
if (!buffer.length) { | ||
//get length of incomming data | ||
var line_end_pos = chunk.indexOf(end); | ||
next_data_length = parseInt(chunk.slice(0, line_end_pos)); | ||
//slice data length string from chunk | ||
chunk = chunk.slice(line_end_pos+end.length); | ||
} | ||
if (buffer.length != next_data_length) { | ||
//data set recieved | ||
//first remove end and append to buffer | ||
buffer+= chunk.slice(0, chunk.indexOf(end)); | ||
//parse json | ||
var parsed = false; | ||
try { | ||
//try parse & emit | ||
buffer = JSON.parse(buffer); | ||
parsed = true; | ||
} catch(e) { | ||
stream.emit('garbage', buffer); | ||
var index, json; | ||
while((index = data.indexOf('\r\n')) > -1) { | ||
json = data.slice(0, index); | ||
data = data.slice(index + 2); | ||
if(json.length > 0) { | ||
try { | ||
stream.emit('data', JSON.parse(json)); | ||
} catch(e) { | ||
stream.emit('garbage', data); | ||
} | ||
} | ||
//don't emit into "try" and emit only if data formatted | ||
if (parsed) { | ||
stream.emit('data', buffer); | ||
} | ||
//clean buffer | ||
buffer = ''; | ||
} else { | ||
//append to buffer | ||
buffer+=chunk; | ||
} | ||
}); | ||
}); | ||
response.on('error', function(error) { | ||
@@ -154,0 +128,0 @@ |
{ | ||
"name": "user-stream", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Simple twitter user stream client. Only one dependency (oauth).", | ||
@@ -5,0 +5,0 @@ "keywords": ["twitter","streaming"], |
user-stream | ||
============= | ||
### Version: 0.0.6 ### | ||
[![NPM module](https://badge.fury.io/js/user-stream.png)](https://badge.fury.io/js/user-stream) | ||
Simple Node.js Twitter (API 1.1) user stream client (https://dev.twitter.com/docs/streaming-apis/streams/user) | ||
[![NPM](https://nodei.co/npm/user-stream.png?stars&downloads)](https://nodei.co/npm/user-stream/) | ||
Install | ||
@@ -8,0 +10,0 @@ ------- |
58
6253
150