parse-message.js
3.93 KB
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
var Int64 = require('./int64').Int64;
var splitMessage = require('./split-message').splitMessage;
var BOOLEAN_TAG = 'boolean';
var BYTE_TAG = 'byte';
var SHORT_TAG = 'short';
var INT_TAG = 'integer';
var LONG_TAG = 'long';
var BINARY_TAG = 'binary';
var STRING_TAG = 'string';
var TIMESTAMP_TAG = 'timestamp';
var UUID_TAG = 'uuid';
/**
* @api private
*
* @param {Buffer} headers
*/
function parseHeaders(headers) {
var out = {};
var position = 0;
while (position < headers.length) {
var nameLength = headers.readUInt8(position++);
var name = headers.slice(position, position + nameLength).toString();
position += nameLength;
switch (headers.readUInt8(position++)) {
case 0 /* boolTrue */:
out[name] = {
type: BOOLEAN_TAG,
value: true
};
break;
case 1 /* boolFalse */:
out[name] = {
type: BOOLEAN_TAG,
value: false
};
break;
case 2 /* byte */:
out[name] = {
type: BYTE_TAG,
value: headers.readInt8(position++)
};
break;
case 3 /* short */:
out[name] = {
type: SHORT_TAG,
value: headers.readInt16BE(position)
};
position += 2;
break;
case 4 /* integer */:
out[name] = {
type: INT_TAG,
value: headers.readInt32BE(position)
};
position += 4;
break;
case 5 /* long */:
out[name] = {
type: LONG_TAG,
value: new Int64(headers.slice(position, position + 8))
};
position += 8;
break;
case 6 /* byteArray */:
var binaryLength = headers.readUInt16BE(position);
position += 2;
out[name] = {
type: BINARY_TAG,
value: headers.slice(position, position + binaryLength)
};
position += binaryLength;
break;
case 7 /* string */:
var stringLength = headers.readUInt16BE(position);
position += 2;
out[name] = {
type: STRING_TAG,
value: headers.slice(
position,
position + stringLength
).toString()
};
position += stringLength;
break;
case 8 /* timestamp */:
out[name] = {
type: TIMESTAMP_TAG,
value: new Date(
new Int64(headers.slice(position, position + 8))
.valueOf()
)
};
position += 8;
break;
case 9 /* uuid */:
var uuidChars = headers.slice(position, position + 16)
.toString('hex');
position += 16;
out[name] = {
type: UUID_TAG,
value: uuidChars.substr(0, 8) + '-' +
uuidChars.substr(8, 4) + '-' +
uuidChars.substr(12, 4) + '-' +
uuidChars.substr(16, 4) + '-' +
uuidChars.substr(20)
};
break;
default:
throw new Error('Unrecognized header type tag');
}
}
return out;
}
function parseMessage(message) {
var parsed = splitMessage(message);
return { headers: parseHeaders(parsed.headers), body: parsed.body };
}
/**
* @api private
*/
module.exports = {
parseMessage: parseMessage
};