-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·156 lines (141 loc) · 4.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
#!/usr/bin/env node
(function() {
var http = require('http');
var program = require('commander');
var chalk = require('chalk');
var path = require('path');
var fs = require('fs');
var bonjour = require('bonjour')();
var fs = require('fs');
var package = require('./package.json');
var FileStreamer = {
writeableStream: null,
fileName: null,
init: function(recordPath) {
if(!recordPath) return;
this.fileName = path.normalize(recordPath);
this.writeableStream = fs.createWriteStream(this.fileName);
console.log('Logs will be saved in ' + chalk.cyan(this.fileName));
},
write: function(msg) {
if(this.writeableStream) {
this.writeableStream.write(msg);
}
},
end: function() {
if(this.writeableStream) {
this.writeableStream.write(chalk.green('\nReceived SIGINT.'));
process.stdout.write('\nClosing ' + chalk.cyan(fileName));
this.writeableStream.end();
}
}
};
program
.version(package.version)
.option('-h, --hostname <hostname>', 'host name of the event emitter.')
.option('-f, --filters <filters>', 'events you don\'t want to log (separated with comma). Can not be used with "lookup" option.')
.option('-r, --record <filename>', 'filename to records events')
.parse(process.argv);
if(!program.hostname) {
console.log('You have to specify a host SSE you want to connect to.\nYou may find bellow a list of active SSE hosts :');
var browser = bonjour.find({ type: 'g7' }, function (service) {
console.log('The host "' + service.name + '" : [' + service.addresses[0] +'] is up ...');
});
setTimeout(function(){
console.log('Stop browsing network.')
browser.stop();
}, 12000);
} else {
var optionsGet = {
hostname: program.hostname,
path: '/stream',
method: 'GET',
headers: {
'Accept': 'text/event-stream'
}
};
FileStreamer.init(program.record);
function isFiltered(event) {
var match = false;
if(!program.filters)
return false;
program.filters.split(',').forEach(function(filter){
if(filter === event) {
match = true;
}
});
return match;
}
process.on('SIGINT', function() {
process.stdout.write(chalk.green('\nReceived SIGINT.'));
FileStreamer.end();
process.stdout.write('\nExit.\n');
process.exit(0);
});
var req = http.request(optionsGet, function(res){
console.log(chalk.red('Connected ...'));
var _event;
var _data;
var accChunk;
var multiLineSav = false;
res.on('data', function (chunk) {
// check if last two bytes are == '\n\n'
var multiLine = (chunk[chunk.length-1] !== 10 && chunk[chunk.length-2] !== 10);
// if not, it was a multi chunk payload
if(multiLineSav) {
// if last chunk for the payload, strip mast two '\n'
if(!multiLine) {
chunk = chunk.slice(0,chunk.length-2);
}
FileStreamer.write(chalk.cyan(chunk));
process.stdout.write(chalk.cyan(chunk));
multiLineSav = multiLine;
} else {
// handle multi sse in a single chunk
var lines = chunk.toString().split('\n');
var skipNextLine = false;
lines.forEach(function(line){
var field = line.split(': ')[0];
var payload = line.split(': ')[1];
if(field === 'event') {
skipNextLine=isFiltered(payload);
}
if(!skipNextLine && line !== '' && field !== 'id') {
var now = new Date();
if( field === 'event') {
_event = payload;
}
if(program.record && field === 'data') {
try{
_data = JSON.parse(payload);
}
catch(e) {
_data = payload;
}
}
var msg = '\n|' + chalk.grey(now.toJSON())+'| ' + chalk.green(field)+': ' + chalk.cyan(payload);
FileStreamer.write(msg);
process.stdout.write(msg);
}
if(field === 'data')
skipNextLine = false;
});
multiLineSav = multiLine;
}
});
res.on('end', function() {
var msg = '\nNo more data in response, or server closed connexion.\n' + chalk.red('--> exit !\n');
FileStreamer.write(msg);
process.stdout.write(msg);
process.exit(-1);
});
});
req.on('error', (e) => {
var msg = chalk.red('problem with request: ') + chalk.cyan(e.message) + chalk.red('\n--> exit !\n');
FileStreamer.write(msg);
process.stdout.write(msg);
process.exit(-1);
});
req.end();
}
})();