-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
231 lines (190 loc) · 6.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
var qs = require('querystring');
var path = require('path')
var hyperquest = require('hyperquest');
var xtend = require('xtend');
var concat = require('concat-stream');
var json = require('./lib/json');
var pkg = require('./package.json');
var through = require('through');
var repipe = require('repipe');
var split = require('split');
module.exports = function(config){
var config = config||{};
// token was passed directly.
if(typeof config == 'string'){
config = {token:config};
}
var api = {
session:config.session||{},
login:function(email,password,cb){
var z = this;
z.rest({method:'post',url:'/v1/login',data:{email:email,password:password}},function(err,data){
if(err) return cb(err);
z.session = data;
cb(false,data);
})
},
logout:function(cb){
if(!this.session.token) return process.nextTick(function(){
cb(false,false);
})
var z = this;
this.rest({method:'post',url:'/v1/logout'},function(err,data){
z.session = config.session = {};
cb(err,data);
})
},
register:function(data,cb){
var z = this;
// you should send email,password, firstname, lastname, username
this.rest({method:'post',url:'/v1/register',data:data},function(err,data){
if(err) return cb(err);
z.session = data;
cb(false,data);
})
},
rest:function(method,uri,data,cb){
// support the same rest style as browser.
// opts,cb < i prefer this way
if(typeof method == 'object' && method){
cb = uri;
uri = method.url
data = method.data
method = method.method || 'GET'
}
if(typeof data === 'function'){
cb = data;
data = {};
}
var uri = (config.api||'https://api.pinocc.io')+path.join('/',''+uri);
var data = data||{};
// allow argument supplied token.
if(!data.token && this.session.token) data.token = this.session.token;
var opts = {};
opts.method = (method||"GET").toUpperCase();
opts.headers = {'x-client-version':pkg.version};
if(opts.method == 'GET' || opts.method == 'DELETE'){
data = qs.stringify(data);
uri += '?'+data;
} else {
if(data.token) uri += '?token='+data.token;
data = JSON.stringify(data);
}
if(opts.method != 'GET' && opts.method != 'DELETE') {
opts.headers['content-length'] = data.length;
}
var req = hyperquest(uri,opts,function(err,res){
if(err) {
return cb(err);
}
res.on('error',function(err){
cb(err);
cb = function(){};
})
if(res.headers['x-stream']) {
cb(false,res);// return response stream.
} else {
res.pipe(concat(function(data){
var parsed = json(data)||{};
cb(parsed.error,parsed.data,data);
}))
}
});
if(opts.method != 'GET' && opts.method != 'DELETE') {
req.write(data);
req.end();
}
},
sync:function(opts){
opts = opts||{};
var z = this;
var start = opts.start||0;
var repiped = through();
var t = Date.now();
var begin = t;
repipe(repiped,function(err,last,done){
var elap = Date.now()-t;
t = Date.now();
repiped.emit('log','reconnect',err,last,opts,elap);
if(last) {
opts.start = start+0.001;// get the next event rather than the last event you already got.
// because sending start triggers stale mode.
// if we did not intend on getting stale events,
// we may get events that happend after the last start time when we reconnect that are not new.
// we will use the local begin timestamp to filter stale messages because the server timestamp at the start of the process is not available.
// this is not perfect. but as long as the drift is not greater than the streams life it will be accurate.
//
if(!opts.stale){
// this case is only true if there are no new events generated when the previous stream was open.
if(opts.start < begin) {
// override start with the time i started this process.
start = opts.start = begin;
}
}
}
done(false,make(opts));
});
function make(opts){
var s = through(function(data){
// catch empty strings throwing json parse errors.
if(!(data||'').trim().length) return repiped.emit('log','error','empty string event ignored!');
try{
var o = JSON.parse(data);
if(o.data){
var _t = (o.data.value?o.data.value.time||o.data.value._t:o.data.time);
if( _t && start < +_t) start = +_t;
// filter data events! this is an error in the api right now. it doesnt start from start correctly.
if(opts.start) {
if(_t < opts.start) return;
}
}
this.queue(o);
} catch(e) {
e.data = data;
this.emit('error',e);
}
});
// make end an error so repipe knows to reconnect!
s.on('end',function(){
this.emit('error','dont end bro!');
})
z.rest({url:"/v1/sync",data:opts},function(err,_s){
if(err) return s.emit('error',err);
if(!_s) return s.emit('error',new Error('no stream returned'));
_s.on('error',function(error){
s.emit('error',error);
});
_s.pipe(split()).pipe(s);
s.on('end',function(){
_s.socket.end();
});
});
return s;
}
return repiped;
},
stats:function(opts){
var s = through(function(data){
try{
this.queue(JSON.parse(data));
} catch(e) {
this.emit('error',e);
}
});
this.rest({url:"/v1/stats",data:opts},function(err,_s){
if(err) return s.emit('error',err);
if(!_s) return s.emit('error',new Error('no stream returned'));
_s.on('error',function(error){
s.emit('error',error);
})
_s.pipe(split()).pipe(s);
s.on('end',function(){
_s.socket.end();
})
})
return s;
}
};
api.session.token = config.token;
return api;
}