-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.js
407 lines (354 loc) · 10.8 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
var Readable = require('readable-stream').Readable;
var StringDecoder = require('string_decoder').StringDecoder;
var url = require('url');
var util = require('util');
var qs = require('querystring');
var debug = require('debug')('changes-stream');
var http = require('http-https');
var back = require('back');
var extend = util._extend;
var DEFAULT_HEARTBEAT = 30 * 1000;
module.exports = ChangesStream;
util.inherits(ChangesStream, Readable);
//
// @ChangeStream
// ## Constructor to initialize the changes stream
//
function ChangesStream (options) {
if (!(this instanceof ChangesStream)) { return new ChangesStream(options) }
options = options || {};
var hwm = options.highWaterMark || 16;
Readable.call(this, { objectMode: true, highWaterMark: hwm });
//
// PARSE ALL THE OPTIONS OMG
//
this._feedParams = [
'heartbeat', 'feed', 'filter', 'include_docs', 'view', 'style', 'since',
'timeout', 'limit'
];
// Bit of a buffer for aggregating data
this._buffer = '';
this._decoder = new StringDecoder('utf8');
this.requestTimeout = options.requestTimeout || 2 * 60 * 1000;
// Time to wait for a new change before we jsut retry a brand new request
this.inactivity_ms = options.inactivity_ms || 60 * 60 * 1000;
this.reconnect = options.reconnect || { minDelay: 100, maxDelay: 30 * 1000, retries: 5 };
// Patch min and max delay using heartbeat
var minDelay = Math.max(this.reconnect.minDelay, (this.heartbeat || DEFAULT_HEARTBEAT) + 5000)
var maxDelay = Math.max(minDelay + (this.reconnect.maxDelay - this.reconnect.minDelay), this.reconnect.maxDelay)
this.reconnect.minDelay = minDelay
this.reconnect.maxDelay = maxDelay
this.db = typeof options === 'string'
? options
: options.db;
if (!this.db) throw new TypeError('you must specify a db');
if (this.db[this.db.length - 1] != '/') {
this.db = this.db + '/';
}
// http option
this.rejectUnauthorized = options.strictSSL || options.rejectUnauthorized || true;
this.agent = options.agent;
if (!this.db) {
throw new Error('DB is required');
}
// Setup all query options and defaults
this.feed = options.feed || 'continuous';
this.since = options.since || 0;
// Allow couch heartbeat to be used but we can just manage that timeout
// If passed heartbeat is a number, use the explicitly, if it's a boolean
// and true, use the default heartbeat, disable it otherwise.
if (typeof options.heartbeat === 'number')
this.heartbeat = options.heartbeat;
else if (typeof options.heartbeat === 'boolean')
this.heartbeat = options.heartbeat ? DEFAULT_HEARTBEAT : false;
else
this.heartbeat = DEFAULT_HEARTBEAT;
this.style = options.style || 'main_only';
this.query_params = options.query_params || {};
this.timeout = options.timeout
this.limit = options.limit;
this.filterIds = Array.isArray(options.filter)
? options.filter
: false;
this.filter = !this.filterIds
? (options.filter || false)
: '_doc_ids';
this.clientFilter = typeof this.filter === 'function';
// If we are doing a client side filter we need the actual document
this.include_docs = !this.clientFilter
? (options.include_docs || false)
: true;
this.use_post = this.filterIds
? false
: (options.use_post || false);
this.paused = false;
this.destroying = false;
this.request();
}
//
// Setup all the _changes query options
//
ChangesStream.prototype.preRequest = function () {
// We want to actually reform this every time in case something has changed
this.query = this._feedParams.reduce(function (acc, key) {
if (typeof this[key] !== 'undefined' && this[key] !== false) {
acc[key] = this[key];
}
return acc;
}.bind(this), JDUP(this.query_params));
// Remove filter from query parameters since we have confirmed it as
// a function
if (this.clientFilter) {
delete this.query.filter;
}
};
//
// Make the changes request and start listening on the feed
//
ChangesStream.prototype.request = function () {
// Setup possible query string options
this.preRequest();
var changes_url = url.resolve(this.db, '_changes');
var opts = url.parse(this.use_post
? changes_url
: url.resolve(changes_url, '?' + qs.stringify(this.query))
);
var payload;
//
// Handle both cases of POST and GET
//
opts.method = (this.filterIds || this.use_post) ? 'POST' : 'GET';
opts.timeout = this.requestTimeout;
opts.rejectUnauthorized = this.rejectUnauthorized;
opts.headers = {
'accept': 'application/json'
};
opts.agent = this.agent;
//
// When we are a post we need to create a payload;
//
if (this.filterIds || this.use_post) {
opts.headers['content-type'] = 'application/json';
payload = new Buffer(JSON.stringify(this.filterIds || this.query), 'utf8');
}
//
// Set a timer for the initial request with some extra magic number
//
this.timer = setTimeout(this.onTimeout.bind(this), (this.heartbeat || DEFAULT_HEARTBEAT) + 5000)
this.req = http.request(opts);
this.req.setSocketKeepAlive(true);
this.req.once('error', this._onError.bind(this));
this.req.once('response', this._onResponse.bind(this));
if (payload) {
this.req.write(payload);
}
this.req.end();
};
//
// Handle the response from a new request
// Remark: Should we use on('data') and just self buffer any events we get
// when a proper pause is called? This may be more intuitive behavior that is
// compatible with how streams3 will work anyway. This just makes the _read
// function essentially useless as it is on most cases
//
ChangesStream.prototype._onResponse = function (res) {
clearTimeout(this.timer);
this.timer = null;
if (res.statusCode !== 200) {
var err = new Error('Received a ' + res.statusCode + ' from couch');
err.statusCode = res.statusCode;
return this.emit('error', err);
}
this.source = res;
//
// Set a timer so that we know we are actually getting some changes from the
// socket
//
this.timer = setTimeout(this.onTimeout.bind(this), this.inactivity_ms);
this.source.on('data', this._readData.bind(this));
this.source.on('end', this._onEnd.bind(this));
};
//
// Little wrapper around retry for our self set timeouts
//
ChangesStream.prototype.onTimeout = function () {
clearTimeout(this.timer);
this.timer = null
debug('request timed out or is inactive, lets retry');
this.retry();
};
//
// Parse and read the data that we get from _changes
//
ChangesStream.prototype._readData = function (data) {
debug('data event fired from the underlying _changes response');
this.attempt = null;
var text = this._decoder.write(data);
var lines = text.split('\n')
if (lines.length > 1) {
this._buffer += lines.shift()
lines.unshift(this._buffer)
this._buffer = lines.pop()
for (var i=0; i<lines.length; i++) {
var line = lines[i];
try { line = JSON.parse(line) }
catch (ex) { return; }
//
// Process each change
//
this._onChange(line);
}
} else {
this._buffer += text
}
};
//
// Process each change request
//
ChangesStream.prototype._onChange = function (change) {
var query, doc;
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
this.timer = setTimeout(this.onTimeout.bind(this), this.inactivity_ms);
}
if (change === '') {
return this.emit('heartbeat');
}
//
// Update the since value internally as we will need that to
// be up to date for proper retries
//
this.since = change.seq || change.last_seq || this.since;
//
// This is ugly but replicates the correct behavior
// for running a client side filter function
//
if (this.clientFilter) {
doc = JDUP(change.doc);
query = JDUP({ query: this.query });
if (!this.filter(doc, query)) {
return;
}
}
//
// If we are ever going to have backpressure issues
// we would want to see if push returned false/null
// and then stop reading from underlying source.
//
if (!this.push(change)) {
debug('paused feed due to highWatermark and backpressure purposes');
this.pause();
}
//
// End the stream if we are on teh last change. Start destroying ourselves
// (`#destroy()` calls `#push(null)`).
//
if (change.last_seq) this.destroy();
};
//
// On error be set for retrying the underlying request
//
ChangesStream.prototype._onError = function (err) {
this.attempt = this.attempt || extend({}, this.reconnect);
return back(function (fail, opts) {
if (fail) {
this.attempt = null;
return this.emit('error', err);
}
debug('retry # %d', opts.attempt);
this.retry();
}.bind(this), this.attempt);
};
//
// When response ends (for example. CouchDB shuts down gracefully), create an
// artificial error to let the user know what happened.
//
ChangesStream.prototype._onEnd = function () {
var err = new Error('CouchDB disconnected gracefully');
err.code = 'ECOUCHDBDISCONNECTEDGRACEFULLY'
this._onError(err)
};
//
// Cleanup and flush any data and retry the request
//
ChangesStream.prototype.retry = function () {
debug('retry request');
if (this._destroying) return;
this.emit('retry');
this.cleanup();
this.request();
};
//
// Pause the underlying socket if we want manually handle that backpressure
// and buffering
//
ChangesStream.prototype.pause = function () {
if (!this.paused) {
debug('paused the source request');
this.emit('pause');
this.source && this.source.pause();
this.paused = true;
}
};
//
// Resume the underlying socket so we continue to push changes onto the
// internal buffer
//
ChangesStream.prototype.resume = function () {
if (this.paused) {
debug('resumed the source request');
this.emit('resume');
this.source.resume();
this.paused = false;
}
};
ChangesStream.prototype.preCleanup = function () {
var rem = this._buffer.trim();
debug('precleanup: do we have remaining data?')
if (rem) {
debug('attempting to parse remaining data');
try { rem = JSON.parse(rem) }
catch (ex) { return }
this.push(rem);
}
};
//
// Cleanup the valuable internals, great for before a retry
//
ChangesStream.prototype.cleanup = function () {
debug('cleanup: flushing any possible buffer and killing underlying request');
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
if (this.req && this.req.socket) {
this.req.abort();
this.req = null;
}
this.preCleanup();
if (this.source && this.source.socket) {
this.source.destroy();
this.source = null
}
};
//
// Complete destroy the internals and end the stream
//
ChangesStream.prototype.destroy = function () {
debug('destroy the instance and end the stream')
this._destroying = true;
this.cleanup();
this._decoder.end();
this._decoder = null;
this.push(null);
};
//
// Lol @_read
//
ChangesStream.prototype._read = function (n) {
this.resume();
};
function JDUP (obj) {
return JSON.parse(JSON.stringify(obj));
}