-
Notifications
You must be signed in to change notification settings - Fork 1
/
messages.js
288 lines (269 loc) · 8.17 KB
/
messages.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// This file is auto generated by the protocol-buffers compiler
/* eslint-disable quotes */
/* eslint-disable indent */
/* eslint-disable no-redeclare */
/* eslint-disable camelcase */
// Remember to `npm install --save protocol-buffers-encodings`
var encodings = require('protocol-buffers-encodings')
var varint = encodings.varint
var skip = encodings.skip
var PubSubMessage = exports.PubSubMessage = {
buffer: true,
encodingLength: null,
encode: null,
decode: null
}
var PexMessage = exports.PexMessage = {
buffer: true,
encodingLength: null,
encode: null,
decode: null
}
var PexBatch = exports.PexBatch = {
buffer: true,
encodingLength: null,
encode: null,
decode: null
}
definePubSubMessage()
definePexMessage()
definePexBatch()
function definePubSubMessage () {
PubSubMessage.encodingLength = encodingLength
PubSubMessage.encode = encode
PubSubMessage.decode = decode
function encodingLength (obj) {
var length = 0
if (!defined(obj.topic)) throw new Error("topic is required")
var len = encodings.string.encodingLength(obj.topic)
length += 1 + len
if (!defined(obj.type)) throw new Error("type is required")
var len = encodings.varint.encodingLength(obj.type)
length += 1 + len
if (defined(obj.application)) {
var len = encodings.string.encodingLength(obj.application)
length += 1 + len
}
if (defined(obj.data)) {
var len = encodings.bytes.encodingLength(obj.data)
length += 1 + len
}
return length
}
function encode (obj, buf, offset) {
if (!offset) offset = 0
if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj))
var oldOffset = offset
if (!defined(obj.topic)) throw new Error("topic is required")
buf[offset++] = 10
encodings.string.encode(obj.topic, buf, offset)
offset += encodings.string.encode.bytes
if (!defined(obj.type)) throw new Error("type is required")
buf[offset++] = 16
encodings.varint.encode(obj.type, buf, offset)
offset += encodings.varint.encode.bytes
if (defined(obj.application)) {
buf[offset++] = 26
encodings.string.encode(obj.application, buf, offset)
offset += encodings.string.encode.bytes
}
if (defined(obj.data)) {
buf[offset++] = 34
encodings.bytes.encode(obj.data, buf, offset)
offset += encodings.bytes.encode.bytes
}
encode.bytes = offset - oldOffset
return buf
}
function decode (buf, offset, end) {
if (!offset) offset = 0
if (!end) end = buf.length
if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid")
var oldOffset = offset
var obj = {
topic: "",
type: 0,
application: "",
data: null
}
var found0 = false
var found1 = false
while (true) {
if (end <= offset) {
if (!found0 || !found1) throw new Error("Decoded message is not valid")
decode.bytes = offset - oldOffset
return obj
}
var prefix = varint.decode(buf, offset)
offset += varint.decode.bytes
var tag = prefix >> 3
switch (tag) {
case 1:
obj.topic = encodings.string.decode(buf, offset)
offset += encodings.string.decode.bytes
found0 = true
break
case 2:
obj.type = encodings.varint.decode(buf, offset)
offset += encodings.varint.decode.bytes
found1 = true
break
case 3:
obj.application = encodings.string.decode(buf, offset)
offset += encodings.string.decode.bytes
break
case 4:
obj.data = encodings.bytes.decode(buf, offset)
offset += encodings.bytes.decode.bytes
break
default:
offset = skip(prefix & 7, buf, offset)
}
}
}
}
function definePexMessage () {
PexMessage.encodingLength = encodingLength
PexMessage.encode = encode
PexMessage.decode = decode
function encodingLength (obj) {
var length = 0
if (!defined(obj.discoveryKey)) throw new Error("discoveryKey is required")
var len = encodings.bytes.encodingLength(obj.discoveryKey)
length += 1 + len
if (defined(obj.address)) {
var len = encodings.string.encodingLength(obj.address)
length += 1 + len
}
if (defined(obj.publicKey)) {
var len = encodings.bytes.encodingLength(obj.publicKey)
length += 1 + len
}
return length
}
function encode (obj, buf, offset) {
if (!offset) offset = 0
if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj))
var oldOffset = offset
if (!defined(obj.discoveryKey)) throw new Error("discoveryKey is required")
buf[offset++] = 10
encodings.bytes.encode(obj.discoveryKey, buf, offset)
offset += encodings.bytes.encode.bytes
if (defined(obj.address)) {
buf[offset++] = 18
encodings.string.encode(obj.address, buf, offset)
offset += encodings.string.encode.bytes
}
if (defined(obj.publicKey)) {
buf[offset++] = 26
encodings.bytes.encode(obj.publicKey, buf, offset)
offset += encodings.bytes.encode.bytes
}
encode.bytes = offset - oldOffset
return buf
}
function decode (buf, offset, end) {
if (!offset) offset = 0
if (!end) end = buf.length
if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid")
var oldOffset = offset
var obj = {
discoveryKey: null,
address: "",
publicKey: null
}
var found0 = false
while (true) {
if (end <= offset) {
if (!found0) throw new Error("Decoded message is not valid")
decode.bytes = offset - oldOffset
return obj
}
var prefix = varint.decode(buf, offset)
offset += varint.decode.bytes
var tag = prefix >> 3
switch (tag) {
case 1:
obj.discoveryKey = encodings.bytes.decode(buf, offset)
offset += encodings.bytes.decode.bytes
found0 = true
break
case 2:
obj.address = encodings.string.decode(buf, offset)
offset += encodings.string.decode.bytes
break
case 3:
obj.publicKey = encodings.bytes.decode(buf, offset)
offset += encodings.bytes.decode.bytes
break
default:
offset = skip(prefix & 7, buf, offset)
}
}
}
}
function definePexBatch () {
PexBatch.encodingLength = encodingLength
PexBatch.encode = encode
PexBatch.decode = decode
function encodingLength (obj) {
var length = 0
if (defined(obj.messages)) {
for (var i = 0; i < obj.messages.length; i++) {
if (!defined(obj.messages[i])) continue
var len = PexMessage.encodingLength(obj.messages[i])
length += varint.encodingLength(len)
length += 1 + len
}
}
return length
}
function encode (obj, buf, offset) {
if (!offset) offset = 0
if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj))
var oldOffset = offset
if (defined(obj.messages)) {
for (var i = 0; i < obj.messages.length; i++) {
if (!defined(obj.messages[i])) continue
buf[offset++] = 10
varint.encode(PexMessage.encodingLength(obj.messages[i]), buf, offset)
offset += varint.encode.bytes
PexMessage.encode(obj.messages[i], buf, offset)
offset += PexMessage.encode.bytes
}
}
encode.bytes = offset - oldOffset
return buf
}
function decode (buf, offset, end) {
if (!offset) offset = 0
if (!end) end = buf.length
if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid")
var oldOffset = offset
var obj = {
messages: []
}
while (true) {
if (end <= offset) {
decode.bytes = offset - oldOffset
return obj
}
var prefix = varint.decode(buf, offset)
offset += varint.decode.bytes
var tag = prefix >> 3
switch (tag) {
case 1:
var len = varint.decode(buf, offset)
offset += varint.decode.bytes
obj.messages.push(PexMessage.decode(buf, offset, offset + len))
offset += PexMessage.decode.bytes
break
default:
offset = skip(prefix & 7, buf, offset)
}
}
}
}
function defined (val) {
return val !== null && val !== undefined && (typeof val !== 'number' || !isNaN(val))
}