-
Notifications
You must be signed in to change notification settings - Fork 1
/
messages.js
413 lines (389 loc) · 12.3 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// 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 IndexNode = exports.IndexNode = {
buffer: true,
encodingLength: null,
encode: null,
decode: null
}
var Block = exports.Block = {
buffer: true,
encodingLength: null,
encode: null,
decode: null
}
var TransactionMarker = exports.TransactionMarker = {
buffer: true,
encodingLength: null,
encode: null,
decode: null
}
var HypercoreHeader = exports.HypercoreHeader = {
buffer: true,
encodingLength: null,
encode: null,
decode: null
}
defineIndexNode()
defineBlock()
defineTransactionMarker()
defineHypercoreHeader()
function defineIndexNode () {
IndexNode.encodingLength = encodingLength
IndexNode.encode = encode
IndexNode.decode = decode
function encodingLength (obj) {
var length = 0
if (!defined(obj.id)) throw new Error("id is required")
var len = encodings.varint.encodingLength(obj.id)
length += 1 + len
if (defined(obj.children)) {
for (var i = 0; i < obj.children.length; i++) {
if (!defined(obj.children[i])) continue
var len = encodings.varint.encodingLength(obj.children[i])
length += 1 + len
}
}
if (defined(obj.content)) {
for (var i = 0; i < obj.content.length; i++) {
if (!defined(obj.content[i])) continue
var len = encodings.varint.encodingLength(obj.content[i])
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.id)) throw new Error("id is required")
buf[offset++] = 8
encodings.varint.encode(obj.id, buf, offset)
offset += encodings.varint.encode.bytes
if (defined(obj.children)) {
for (var i = 0; i < obj.children.length; i++) {
if (!defined(obj.children[i])) continue
buf[offset++] = 16
encodings.varint.encode(obj.children[i], buf, offset)
offset += encodings.varint.encode.bytes
}
}
if (defined(obj.content)) {
for (var i = 0; i < obj.content.length; i++) {
if (!defined(obj.content[i])) continue
buf[offset++] = 24
encodings.varint.encode(obj.content[i], buf, offset)
offset += encodings.varint.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 = {
id: 0,
children: [],
content: []
}
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.id = encodings.varint.decode(buf, offset)
offset += encodings.varint.decode.bytes
found0 = true
break
case 2:
obj.children.push(encodings.varint.decode(buf, offset))
offset += encodings.varint.decode.bytes
break
case 3:
obj.content.push(encodings.varint.decode(buf, offset))
offset += encodings.varint.decode.bytes
break
default:
offset = skip(prefix & 7, buf, offset)
}
}
}
}
function defineBlock () {
Block.encodingLength = encodingLength
Block.encode = encode
Block.decode = decode
function encodingLength (obj) {
var length = 0
if ((+defined(obj.marker) + +defined(obj.indexNode) + +defined(obj.dataBlock)) > 1) throw new Error("only one of the properties defined in oneof content can be set")
if (defined(obj.marker)) {
var len = TransactionMarker.encodingLength(obj.marker)
length += varint.encodingLength(len)
length += 1 + len
}
if (defined(obj.indexNode)) {
var len = IndexNode.encodingLength(obj.indexNode)
length += varint.encodingLength(len)
length += 1 + len
}
if (defined(obj.dataBlock)) {
var len = encodings.bytes.encodingLength(obj.dataBlock)
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.marker) + +defined(obj.indexNode) + +defined(obj.dataBlock)) > 1) throw new Error("only one of the properties defined in oneof content can be set")
if (defined(obj.marker)) {
buf[offset++] = 10
varint.encode(TransactionMarker.encodingLength(obj.marker), buf, offset)
offset += varint.encode.bytes
TransactionMarker.encode(obj.marker, buf, offset)
offset += TransactionMarker.encode.bytes
}
if (defined(obj.indexNode)) {
buf[offset++] = 18
varint.encode(IndexNode.encodingLength(obj.indexNode), buf, offset)
offset += varint.encode.bytes
IndexNode.encode(obj.indexNode, buf, offset)
offset += IndexNode.encode.bytes
}
if (defined(obj.dataBlock)) {
buf[offset++] = 26
encodings.bytes.encode(obj.dataBlock, 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 = {
marker: null,
indexNode: null,
dataBlock: null
}
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:
delete obj.indexNode
delete obj.dataBlock
var len = varint.decode(buf, offset)
offset += varint.decode.bytes
obj.marker = TransactionMarker.decode(buf, offset, offset + len)
offset += TransactionMarker.decode.bytes
break
case 2:
delete obj.marker
delete obj.dataBlock
var len = varint.decode(buf, offset)
offset += varint.decode.bytes
obj.indexNode = IndexNode.decode(buf, offset, offset + len)
offset += IndexNode.decode.bytes
break
case 3:
delete obj.marker
delete obj.indexNode
obj.dataBlock = encodings.bytes.decode(buf, offset)
offset += encodings.bytes.decode.bytes
break
default:
offset = skip(prefix & 7, buf, offset)
}
}
}
}
function defineTransactionMarker () {
TransactionMarker.encodingLength = encodingLength
TransactionMarker.encode = encode
TransactionMarker.decode = decode
function encodingLength (obj) {
var length = 0
if (!defined(obj.sequenceNr)) throw new Error("sequenceNr is required")
var len = encodings.varint.encodingLength(obj.sequenceNr)
length += 1 + len
if (!defined(obj.objectCtr)) throw new Error("objectCtr is required")
var len = encodings.varint.encodingLength(obj.objectCtr)
length += 1 + len
if (defined(obj.timestamp)) {
var len = encodings.varint.encodingLength(obj.timestamp)
length += 1 + len
}
if (defined(obj.previous)) {
var len = encodings.varint.encodingLength(obj.previous)
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.sequenceNr)) throw new Error("sequenceNr is required")
buf[offset++] = 8
encodings.varint.encode(obj.sequenceNr, buf, offset)
offset += encodings.varint.encode.bytes
if (!defined(obj.objectCtr)) throw new Error("objectCtr is required")
buf[offset++] = 16
encodings.varint.encode(obj.objectCtr, buf, offset)
offset += encodings.varint.encode.bytes
if (defined(obj.timestamp)) {
buf[offset++] = 24
encodings.varint.encode(obj.timestamp, buf, offset)
offset += encodings.varint.encode.bytes
}
if (defined(obj.previous)) {
buf[offset++] = 32
encodings.varint.encode(obj.previous, buf, offset)
offset += encodings.varint.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 = {
sequenceNr: 0,
objectCtr: 0,
timestamp: 0,
previous: 0
}
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.sequenceNr = encodings.varint.decode(buf, offset)
offset += encodings.varint.decode.bytes
found0 = true
break
case 2:
obj.objectCtr = encodings.varint.decode(buf, offset)
offset += encodings.varint.decode.bytes
found1 = true
break
case 3:
obj.timestamp = encodings.varint.decode(buf, offset)
offset += encodings.varint.decode.bytes
break
case 4:
obj.previous = encodings.varint.decode(buf, offset)
offset += encodings.varint.decode.bytes
break
default:
offset = skip(prefix & 7, buf, offset)
}
}
}
}
function defineHypercoreHeader () {
HypercoreHeader.encodingLength = encodingLength
HypercoreHeader.encode = encode
HypercoreHeader.decode = decode
function encodingLength (obj) {
var length = 0
if (!defined(obj.dataStructureType)) throw new Error("dataStructureType is required")
var len = encodings.string.encodingLength(obj.dataStructureType)
length += 1 + len
if (defined(obj.extension)) {
var len = encodings.bytes.encodingLength(obj.extension)
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.dataStructureType)) throw new Error("dataStructureType is required")
buf[offset++] = 10
encodings.string.encode(obj.dataStructureType, buf, offset)
offset += encodings.string.encode.bytes
if (defined(obj.extension)) {
buf[offset++] = 18
encodings.bytes.encode(obj.extension, 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 = {
dataStructureType: "",
extension: 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.dataStructureType = encodings.string.decode(buf, offset)
offset += encodings.string.decode.bytes
found0 = true
break
case 2:
obj.extension = encodings.bytes.decode(buf, offset)
offset += encodings.bytes.decode.bytes
break
default:
offset = skip(prefix & 7, buf, offset)
}
}
}
}
function defined (val) {
return val !== null && val !== undefined && (typeof val !== 'number' || !isNaN(val))
}