-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (41 loc) · 1.17 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
var bun = require('bun');
var tstream = require('tstream');
var delimiter_frame = require('./lib/delimiter-frame');
var json_stream = {
Parse: tstream(function(chunk, encoding, callback){
try {
var data = JSON.parse(chunk.toString());
this.push(data);
} catch(err) {
this.emit('warn', err, chunk.toString());
} finally {
callback();
}
}),
Stringify: tstream(function(obj, encoding, done){
this.push(JSON.stringify(obj));
done();
})
};
module.exports = {
Parse: function(deliminiter){
var frame_receive = new delimiter_frame.Receive(deliminiter, {objectMode:true});
var json_parse = new json_stream.Parse({objectMode:true});
var pipeline = bun([frame_receive, json_parse]);
event_forward('warn', json_parse, pipeline);
return pipeline;
},
Stringify: function(deliminiter) {
return bun([
new json_stream.Stringify({objectMode:true}),
new delimiter_frame.Send(deliminiter)
]);
}
};
function event_forward(event, ee_from, ee_to){
ee_from.on(event, function(){
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(event);
ee_to.emit.apply(ee_to, args);
});
}