This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencodings.js
99 lines (89 loc) · 2.63 KB
/
encodings.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
// This file is auto generated by the protocol-buffers cli tool
/* 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 EphemeralMessage = exports.EphemeralMessage = {
buffer: true,
encodingLength: null,
encode: null,
decode: null
}
defineEphemeralMessage()
function defineEphemeralMessage () {
var enc = [
encodings.string,
encodings.bytes
]
EphemeralMessage.encodingLength = encodingLength
EphemeralMessage.encode = encode
EphemeralMessage.decode = decode
function encodingLength (obj) {
var length = 0
if (defined(obj.contentType)) {
var len = enc[0].encodingLength(obj.contentType)
length += 1 + len
}
if (!defined(obj.payload)) throw new Error("payload is required")
var len = enc[1].encodingLength(obj.payload)
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.contentType)) {
buf[offset++] = 10
enc[0].encode(obj.contentType, buf, offset)
offset += enc[0].encode.bytes
}
if (!defined(obj.payload)) throw new Error("payload is required")
buf[offset++] = 18
enc[1].encode(obj.payload, buf, offset)
offset += enc[1].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 = {
contentType: "",
payload: null
}
var found1 = false
while (true) {
if (end <= offset) {
if (!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.contentType = enc[0].decode(buf, offset)
offset += enc[0].decode.bytes
break
case 2:
obj.payload = enc[1].decode(buf, offset)
offset += enc[1].decode.bytes
found1 = true
break
default:
offset = skip(prefix & 7, buf, offset)
}
}
}
}
function defined (val) {
return val !== null && val !== undefined && (typeof val !== 'number' || !isNaN(val))
}