This repository has been archived by the owner on Feb 22, 2019. It is now read-only.
/
serializers.js
191 lines (168 loc) · 4.25 KB
/
serializers.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var UUID = require('../uuid');
/**
* Serializers for various types
* @class
*/
var Serializers = {};
/**
* Encodes an N length Integer
* @param {Number} bits The number of bits to encode to
* @param {Number} num The number to encode
* @returns {Buffer} A buffer containing the value bytes
* @static
*/
Serializers.encodeInteger = function(bits, num){
var buf = new Buffer(bits/8), func;
if(typeof num !== 'number'){
num = parseInt(num, 10);
}
switch(bits){
case 32: buf.writeUInt32BE(num, 0); break;
case 64:
var hex = num.toString(16);
hex = new Array(17 - hex.length).join('0') + hex;
buf.writeUInt32BE(parseInt(hex.slice( 0, 8), 16), 0);
buf.writeUInt32BE(parseInt(hex.slice( 8, 16), 16), 4);
}
return buf;
};
/**
* Does binary encoding
* @param {String} val The binary string to serialize
* @returns {Buffer} A buffer containing the value bytes
* @static
*/
Serializers.encodeBinary = function(val){
if(Buffer.isBuffer(val)){
return val;
} else {
return new Buffer(val.toString(), 'binary');
}
};
/**
* Encodes a Long (UInt64)
* @param {Number} val The number to serialize into a long
* @returns {Buffer} A buffer containing the value bytes
* @static
*/
Serializers.encodeLong = function(val){
return Serializers.encodeInteger(64, val);
};
/**
* Encodes a 32bit Unsinged Integer
* @param {Number} val The number to serialize into a 32-bit integer
* @returns {Buffer} A buffer containing the value bytes
* @static
*/
Serializers.encodeInt32 = function(val){
return Serializers.encodeInteger(32, val);
};
/**
* Encodes for UTF8
* @param {String} val The utf8 string value to serialize
* @returns {Buffer} A buffer containing the value bytes
* @static
*/
Serializers.encodeUTF8 = function(val){
if(Buffer.isBuffer(val)){
return val;
}
if(val === undefined || val === null){
val = '';
}
return new Buffer(val.toString(), 'utf8');
};
/**
* Encodes for Ascii
* @param {String} val The ascii string value to serialize
* @returns {Buffer} A buffer containing the value bytes
* @static
*/
Serializers.encodeAscii = function(val){
if(Buffer.isBuffer(val)){
return val;
}
if(val === undefined || val === null){
val = '';
}
return new Buffer(val.toString(), 'ascii');
};
/**
* Encode a Double Precision Floating Point Type
* @param {Number} val The number to serialize into a Double Precision Floating Point
* @returns {Buffer} A buffer containing the value bytes
* @static
*/
Serializers.encodeDouble = function(val){
var buf = new Buffer(8);
buf.writeDoubleBE(val, 0);
return buf;
};
/**
* Encode a Double Precision Floating Point Type
* @param {Number} val The number to serialize into a Single Precision Floating Point
* @returns {Buffer} A buffer containing the value bytes
* @static
*/
Serializers.encodeFloat = function(val){
var buf = new Buffer(4);
buf.writeFloatBE(val, 0);
return buf;
};
/**
* Encodes a boolean type
* @param {Boolean} val true or false, will be serialized into the proper boolean value
* @returns {Buffer} A buffer containing the value bytes
* @static
*/
Serializers.encodeBoolean = function(val){
if(val){
return new Buffer([0x01]);
} else {
return new Buffer([0x00]);
}
};
/**
* Encodes a Date object
* @param {Date} val The date to serialize
* @returns {Buffer} A buffer containing the value bytes
* @static
*/
Serializers.encodeDate = function(val){
var t;
if(val instanceof Date){
t = val.getTime();
} else {
t = new Date(val).getTime();
}
return Serializers.encodeLong(t);
};
/**
* Encodes a UUID Object
* @param {UUID} val The uuid object to serialize
* @returns {Buffer} A buffer containing the value bytes
* @static
*/
Serializers.encodeUUID = function(val){
if (val instanceof UUID.UUID) {
return val.toBuffer();
} else {
var uuid = new UUID.UUID(val);
return uuid.toBuffer();
}
};
/**
* Encodes a TimeUUID Object
* @param {TimeUUID} val The uuid object to serialize
* @returns {Buffer} A buffer containing the value bytes
* @static
*/
Serializers.encodeTimeUUID = function(val){
if (val instanceof UUID.TimeUUID) {
return val.toBuffer();
} else {
var timeUUID = new UUID.TimeUUID(val);
return timeUUID.toBuffer();
}
};
module.exports = Serializers;