-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (122 loc) · 4.03 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
/**
* Created by paulo.simao on 23/12/2015.
*/
var EventEmitter = require('events');
var uuid = require('node-uuid');
//var Buffer = require('buffer');
module.exports.CMD_CONNECT = 'CONNECT';
module.exports.CMD_STOMP = 'STOMP';
module.exports.CMD_CONNECTED = 'CONNECTED';
module.exports.CMD_SEND = 'SEND';
module.exports.CMD_SUBSCRIBE = 'SUBSCRIBE';
module.exports.CMD_UNSUBSCRIBE = 'UNSUBSCRIBE';
module.exports.CMD_ACK = 'ACK';
module.exports.CMD_NACK = 'NACK';
module.exports.CMD_BEGIN = 'BEGIN';
module.exports.CMD_COMMIT = 'COMMIT';
module.exports.CMD_ABORT = 'ABORT';
module.exports.CMD_DISCONNECT = 'DISCONNECT';
module.exports.CMD_MESSAGE = 'MESSAGE';
module.exports.CMD_RECEIPT = 'RECEIPT';
module.exports.CMD_ERROR = 'ERROR';
module.exports.createparser = function (sock) {
var ret = new EventEmitter();
ret.buffer = '';
ret.sock = sock;
ret.chunckcount = 0;
sock.on('data', function (chunck) {
//console.log(chunck.toString());
ret.addchunck(chunck.toString());
});
ret.addchunck = function (chunk) {
ret.buffer += chunk;
if (ret.buffer.indexOf('\0') > -1) {
var msgs = ret.buffer.split('\0');
for (var i = 0; i < msgs.length - 1; i++) {
var msg = msgs[i];
if (msg.length > 0) {
//console.log('\n=======\n' + msg + '\n===============\n')
var parsedmsg = this.parsemsg(msg);
ret.emit(parsedmsg.cmd, parsedmsg);
ret.buffer = ret.buffer.replace(msg + '\0', '');
}
}
if (ret.buffer.endsWith('\0')) {
var msg = ret.buffer.split('\0')[0];
if (msg.length > 0) {
//console.log('\n=======\n' + msg + '\n===============\n')
var parsedmsg = this.parsemsg(msg);
ret.buffer = ret.buffer.replace(msg + '\0', '');
ret.emit(parsedmsg.cmd, parsedmsg);
}
}
}
};
ret.parsemsg = function (rawmsg) {
var parts = rawmsg.split('\n');
var parsedmsg = {
cmd: parts[0],
headers: {},
body: '',
torawmsg: function () {
var msgasstr = this.cmd + '\n';
for (h in parsedmsg.headers) {
msgasstr += h + ':' + parsedmsg.headers[h] + '\n';
}
msgasstr += '\n' + parsedmsg.body + '\0';
return msgasstr;
},
tosinglelinestr: function () {
var msg = parsedmsg.torawmsg();
return msg.buffer.replace(/\n/g, ' ').replace(/\0/g, '\n');
}
};
var i = 1;
for (; i < parts.length && parts[i].length > 0; i++) {
var header = parts[i].split(':');
parsedmsg.headers[header[0]] = header[1];
}
i++;
var body = '';
for (; i < parts.length && parts[i].length > 0; i++) {
body += parts[i];
}
parsedmsg.body = body;
return parsedmsg;
};
return ret;
};
module.exports.addmethodstomsg = function (msg) {
msg.torawmsg = function () {
var msgasstr = msg.cmd + '\n';
for (h in this.headers) {
msgasstr += h + ':' + this.headers[h] + '\n';
}
msgasstr += '\n' + this.body + '\0';
return msgasstr;
};
msg.addheader = function (h, v) {
if (ret.headers[h]) {
throw new Error('Header:' + h + ' already added to this message');
}
ret.headers[h] = v;
};
return msg;
}
module.exports.createmsg = function (cmd) {
ret = {
cmd: cmd,
headers: {},
body: ''
};
ret.headers.createdat = new Date().getTime();
if (cmd === module.exports.CMD_MESSAGE) {
ret.headers['message-id'] = new uuid.v4();
}
ret = module.exports.addmethodstomsg(ret)
ret.addheader('receipt', uuid.v4());
return ret;
};
module.exports.uuid = function () {
return uuid.v4();
};