-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket-data-parser.js
49 lines (39 loc) · 1.23 KB
/
socket-data-parser.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
/*
SocketDataParser Transform Stream
Reads the socket input (buffer), and outputs javascript objects.
usage:
//...
var net = require('net')
, SocketDataParser = require('tools/socket-data-parser')
, socket = net.connect({host: host, port: port});
socket.pipe(new SocketDataParser()).on('data', function (object) {
//Handle object
});
//...
*/
module.exports = AccommodationsParser;
var Transform = require('stream').Transform;
var util = require('util');
var utils = require('./utils');
util.inherits(AccommodationsParser, Transform);
function AccommodationsParser() {
Transform.call(this, { objectMode: true });
this._buffer = '';
this._pendSignal = '>>>PEND<<<';
};
AccommodationsParser.prototype._transform = function(chunk, encoding, callback) {
this._buffer += chunk;
var pedLastIndex = this._buffer.lastIndexOf(this._pendSignal);
if (pedLastIndex != -1) {
var packages = this._buffer.substring(0, pedLastIndex).split(this._pendSignal);
this._buffer = this._buffer.substring(pedLastIndex, this._buffer.length);
for (var i = 0; i < packages.length; i++) {
try {
if (packages[i]) this.push(JSON.parse(packages[i]));
} catch (e) {
return callback(e);
}
};
};
callback();
};