1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 | const fs = require('fs');
|
17 | const stream = require('stream');
|
18 | const liner = require('./liner.js');
|
19 |
|
20 | const onLine = function(onCommand, batches) {
|
21 | const change = new stream.Transform({ objectMode: true });
|
22 | change._transform = function(line, encoding, done) {
|
23 | if (line && line[0] === ':') {
|
24 | const obj = {
|
25 | command: null,
|
26 | batch: null,
|
27 | docs: []
|
28 | };
|
29 |
|
30 | let matches;
|
31 |
|
32 |
|
33 | matches = line.match(/^:([a-z_]+) ?/);
|
34 | if (matches) {
|
35 | obj.command = matches[1];
|
36 | }
|
37 |
|
38 |
|
39 | matches = line.match(/ batch([0-9]+)/);
|
40 | if (matches) {
|
41 | obj.batch = parseInt(matches[1]);
|
42 | }
|
43 |
|
44 |
|
45 | if (obj.command === 't' && batches.indexOf(obj.batch) > -1) {
|
46 | const json = line.replace(/^.* batch[0-9]+ /, '').trim();
|
47 | obj.docs = JSON.parse(json);
|
48 | onCommand(obj);
|
49 | }
|
50 | }
|
51 | done();
|
52 | };
|
53 | return change;
|
54 | };
|
55 |
|
56 | module.exports = function(log, batches, callback) {
|
57 |
|
58 | const retval = { };
|
59 |
|
60 |
|
61 | const onCommand = function(obj) {
|
62 | retval[obj.batch] = obj;
|
63 | };
|
64 |
|
65 |
|
66 | fs.createReadStream(log)
|
67 | .pipe(liner())
|
68 | .pipe(onLine(onCommand, batches))
|
69 | .on('error', function(err) {
|
70 | callback(err);
|
71 | })
|
72 | .on('finish', function() {
|
73 | callback(null, retval);
|
74 | });
|
75 | };
|