-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressor.js
More file actions
49 lines (41 loc) · 1.11 KB
/
Compressor.js
File metadata and controls
49 lines (41 loc) · 1.11 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
var config = require('./config/config.json');
function Compressor() {
this.BSON;
this.msgpack;
switch (config.data_transfer_protocol) {
case "BSON":
const bson = require('bson');
this.BSON = bson;
this.serialize = this.BSONserialize;
this.deserialize = this.BSONdeserialize;
break;
case "msgpack":
this.msgpack = require('msgpack5')();
this.serialize = this.MSGPACKserialize;
this.deserialize = this.MSGPACKdeserialize;
break;
case "protobuf":
break;
default:
break;
}
}
Compressor.prototype.BSONserialize = function(data) {
return this.BSON.serialize(data);
}
Compressor.prototype.BSONdeserialize = function(data) {
return this.BSON.deserialize(Buffer.isBuffer(data) ? data : Buffer.from(data));
}
Compressor.prototype.MSGPACKserialize = function(data) {
return this.msgpack.encode(data);
}
Compressor.prototype.MSGPACKdeserialize = function(data) {
return this.msgpack.decode(data);
}
Compressor.prototype.serialize = function(data) {
return JSON.stringify(data);
}
Compressor.prototype.deserialize = function(data) {
return JSON.parse(data);
}
module.exports = Compressor;