1 | // Copyright © 2017 IBM Corp. All rights reserved.
|
2 | //
|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
4 | // you may not use this file except in compliance with the License.
|
5 | // You may obtain a copy of the License at
|
6 | //
|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
8 | //
|
9 | // Unless required by applicable law or agreed to in writing, software
|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | // See the License for the specific language governing permissions and
|
13 | // limitations under the License.
|
14 | ;
|
15 |
|
16 | // stolen from http://strongloop.com/strongblog/practical-examples-of-the-new-node-js-streams-api/
|
17 | var stream = require('stream');
|
18 |
|
19 | module.exports = function(onChange) {
|
20 | var change = new stream.Transform({ objectMode: true });
|
21 |
|
22 | change._transform = function(line, encoding, done) {
|
23 | var obj = null;
|
24 |
|
25 | // one change per line - remove the trailing comma
|
26 | line = line.trim().replace(/,$/, '');
|
27 |
|
28 | // extract thee last_seq at the end of the changes feed
|
29 | if (line.match(/^"last_seq":/)) {
|
30 | line = '{' + line;
|
31 | }
|
32 | try {
|
33 | obj = JSON.parse(line);
|
34 | } catch (e) {
|
35 | }
|
36 | onChange(obj);
|
37 | done();
|
38 | };
|
39 |
|
40 | return change;
|
41 | };
|