/
changes.js
287 lines (254 loc) · 7.49 KB
/
changes.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
/*
* couchdb.js: Plugin for adding and removing Domains from the balancer based on a CouchDB changes feed.
*
* (C) 2011, Nodejitsu Inc.
*
*/
var events = require('events'),
util = require('util'),
async = require('async'),
qs = require('qs'),
request = require('request');
//
// ### function Changes (options)
// #### @options {Object} Options for this changes feed.
// #### @options.timeout {Object} Retry options on error.
// #### @options.timeout.max {number} Maximum timeout to wait between retries.
// #### @options.timeout.step {number} Step interval for timeout between retries.
// #### @options.views {Object} Metadata about any views to be queried.
// #### @options.filter {string} Name of the CouchDB filter for changes feed.
// #### @options.url {string} Fully qualified CouchDB URL (including auth).
// #### @options.updateSeq {number} update_seq to start changes from.
// Constructor function for the Changes object that is responsible for
// creating and maintaining a persisent `_changes` connection with CouchDB
// and optionally querying any views.
//
var Changes = module.exports = function (options) {
events.EventEmitter.call(this, options);
this.timeout = options.timeout || {};
this.timeout.max = this.timeout.max || 30000;
this.timeout.step = this.timeout.step || 2000;
this.views = options.views;
this.filter = options.filter;
this.url = options.url;
this.updateSeq = options.updateSeq || 0;
this.retry = {
enabled: true,
timeout: 0,
attempts: 0,
}
};
//
// Inherit from `events.EventEmitter`.
//
util.inherits(Changes, events.EventEmitter);
//
// ### function listen (updateSeq, callback)
// #### @updateSeq {number} **Optional** update_seq to start changes from.
// #### @callback {function} Continuation to respond once connected.
// Starts listening to the `_changes` feed at the optional `updateSeq`.
// Responds to the optional `callback` once a response from CouchDB is
// received.
//
Changes.prototype.listen = function (updateSeq, callback) {
if (!callback && typeof updateSeq === 'function') {
callback = updateSeq;
updateSeq = null;
}
updateSeq = updateSeq || this.updateSeq;
//
// Start the changes listener at `updateSeq` or the
// first update.
//
var self = this,
line = '',
responded,
changes,
feedUrl;
feedUrl = this.url + '/_changes?feed=continuous&include_docs=true&since=' +
(updateSeq || 0) +
(this.filter ? '&filter=' + encodeURIComponent(this.filter) : '');
changes = request({ url: feedUrl });
//
// Respond to an optional callback
//
function respond(err) {
if (!responded) {
responded = true;
if (callback) {
callback(err);
}
}
}
changes.on('response', function (res) {
respond();
changes.on('data', function (data) {
line += data;
var parts = line.split('\n');
for (var i = 0; i < parts.length - 1; i++) {
var piece = parts[i],
row;
if (piece.length === 0) continue;
try {
row = JSON.parse(piece);
if (!row) return;
updateSeq = row.last_seq || row.seq || updateSeq;
}
catch (e) {
// ignore this line, it will be removed at end
}
self.emit('change', row);
}
line = parts[parts.length - 1];
});
});
//
// If the changes end, attempt to parse
// the last data received and then restart
// the connection
//
changes.on('end', function () {
if (line.length !== 0) {
try {
var row = JSON.parse(line);
self.emit('change', row);
}
catch (e) {
//
// ignore this line, it will be removed at end
//
}
}
// Restart the connection.
self.listen(updateSeq);
});
//
// There are two important cases for errors:
// 1. _First attempt errors:_ This may indicate that CouchDB
// isn't running at the location provided. In that case
// attempt to respond to the optional callback with the error.
// 2. _Connection drops:_ This is most likely a hiccup in CouchDB
// so simply emit an error.
//
changes.on('error', function (err) {
if (!responded) {
respond(err);
}
else {
self.emit('error:changes', err);
}
//
// Retry with incremental backoff.
// Limit retry timeout to value of `this.timeout.max` config value.
//
var retryTimeout = self.retry.timeout,
maxTimeout = self.timeout.max,
step = self.timeout.step;
//
// Only attempt to retry if it is enabled. Since we respond
// to the callback or emit the error event before this block
// the caller has an opportunity to disable retrying.
//
if (self.retry.enabled) {
self.retry.attempts++;
setTimeout(function () {
self.listen(updateSeq);
}, retryTimeout);
if (!maxTimeout || (retryTimeout + step) < maxTimeout) {
self.retry.timeout += step;
}
else if (maxTimeout) {
self.retry.timeout = maxTimeout;
}
}
});
};
//
// ### function queryAndListen (callback)
// #### @callback {function} Continuation to respond to on _changes.
// Attempts to query all of `this.views`.
//
Changes.prototype.query = function (callback) {
var views = Object.keys(this.views),
url = this.url,
self = this,
updateSeq;
if (!views.length) {
return callback();
}
async.forEach(
views,
function queryOne(view, next) {
var path = self.views[view].path,
query = self.views[view].query || {};
query['update_seq'] = true;
request({
url: self.url + '/' + path + '?' + qs.stringify(query)
}, function (err, res, body) {
if (err) {
//
// Free up potentially large objects
//
body = null;
res = null;
return next(err);
}
var json;
try { json = JSON.parse(body) }
catch (ex) {
//
// Free up potentially large objects
//
err = null;
res = null;
body = null;
return next(ex);
}
//
// Set the updateSeq. The last query returned
// will win the race and ensure the latest value.
//
updateSeq = json.update_seq || updateSeq || 0;
//
// Emit the rows found when querying the view.
//
self.emit(['views', view].join(':'), json.rows);
//
// Free up potentially large objects
//
err = null;
res = null;
body = null;
next();
});
},
function (err) {
if (err) {
return !callback
? self.emit('error:views', err)
: callback(err);
}
self.updateSeq = updateSeq;
self.emit('views');
callback(null, updateSeq);
}
);
};
//
// ### function queryAndListen (callback)
// #### @callback {function} Continuation to respond to on _changes.
// Attempts to query all of `this.views` and then establish a connection
// to the CouchDB `_changes` feed. If `this.views` is set then this
// instance will emit `views:<name>` events for each document received.
//
Changes.prototype.queryAndListen = function (callback) {
var self = this;
this.query(function (err, updateSeq) {
if (err) {
return !callback
? self.emit('error:views', err)
: callback(err);
}
self.listen(updateSeq, callback);
});
};