-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
144 lines (122 loc) · 2.86 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
var msgpack = require('msgpack-lite');
var options = {
codec: msgpack.createCodec({
binarraybuffer: true,
preset: false
})
};
var compressPublishPacket = function (object) {
if (object.event != '#publish' || object.data == null) {
return;
}
var pubArray = [object.data.channel, object.data.data];
if (object.cid != null) {
pubArray.push(object.cid);
}
object.p = pubArray;
delete object.event;
delete object.data;
delete object.cid;
};
var decompressPublishPacket = function (object) {
if (object.p == null) {
return;
}
object.event = '#publish';
object.data = {
channel: object.p[0],
data: object.p[1]
};
if (object.p[2] != null) {
object.cid = object.p[2];
}
delete object.p;
};
var compressEmitPacket = function (object) {
if (object.event == null) {
return;
}
object.e = [object.event, object.data];
if (object.cid != null) {
object.e.push(object.cid);
}
delete object.event;
delete object.data;
delete object.cid;
};
var decompressEmitPacket = function (object) {
if (object.e == null) {
return;
}
object.event = object.e[0];
object.data = object.e[1];
if (object.e[2] != null) {
object.cid = object.e[2];
}
delete object.e;
};
var compressResponsePacket = function (object) {
if (object.rid == null) {
return;
}
object.r = [object.rid, object.error, object.data];
delete object.rid;
delete object.error;
delete object.data;
};
var decompressResponsePacket = function (object) {
if (object.r == null) {
return;
}
object.rid = object.r[0];
object.error = object.r[1];
object.data = object.r[2];
delete object.r;
};
var clonePacket = function (object) {
var clone = {};
for (var i in object) {
if (object.hasOwnProperty(i)) {
clone[i] = object[i];
}
}
return clone;
};
var compressSinglePacket = function (object) {
object = clonePacket(object);
compressPublishPacket(object);
compressEmitPacket(object);
compressResponsePacket(object);
return object;
};
var decompressSinglePacket = function (object) {
decompressEmitPacket(object);
decompressPublishPacket(object);
decompressResponsePacket(object);
};
module.exports.encode = function (object) {
if (object) {
if (Array.isArray(object)) {
var len = object.length;
for (var i = 0; i < len; i++) {
object[i] = compressSinglePacket(object[i]);
}
} else if (object.event != null || object.rid != null) {
object = compressSinglePacket(object);
}
}
return msgpack.encode(object, options);
};
module.exports.decode = function (str) {
str = new Uint8Array(str);
var object = msgpack.decode(str, options);
if (Array.isArray(object)) {
var len = object.length;
for (var i = 0; i < len; i++) {
decompressSinglePacket(object[i]);
}
} else {
decompressSinglePacket(object);
}
return object;
};