-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.js
94 lines (85 loc) · 2.59 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
// 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 Tombstone = exports.Tombstone = {
buffer: true,
encodingLength: null,
encode: null,
decode: null
}
defineTombstone()
function defineTombstone () {
Tombstone.encodingLength = encodingLength
Tombstone.encode = encode
Tombstone.decode = decode
function encodingLength (obj) {
var length = 0
if (!defined(obj.active)) throw new Error("active is required")
var len = encodings.bool.encodingLength(obj.active)
length += 1 + len
if (defined(obj.timestamp)) {
var len = encodings.varint.encodingLength(obj.timestamp)
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.active)) throw new Error("active is required")
buf[offset++] = 8
encodings.bool.encode(obj.active, buf, offset)
offset += encodings.bool.encode.bytes
if (defined(obj.timestamp)) {
buf[offset++] = 16
encodings.varint.encode(obj.timestamp, 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 = {
active: false,
timestamp: 0
}
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.active = encodings.bool.decode(buf, offset)
offset += encodings.bool.decode.bytes
found0 = true
break
case 2:
obj.timestamp = encodings.varint.decode(buf, offset)
offset += encodings.varint.decode.bytes
break
default:
offset = skip(prefix & 7, buf, offset)
}
}
}
}
function defined (val) {
return val !== null && val !== undefined && (typeof val !== 'number' || !isNaN(val))
}