Mathias Buus
7 years ago
3 changed files with 296 additions and 22 deletions
@ -1,6 +1,278 @@ |
|||
var fs = require('fs') |
|||
var path = require('path') |
|||
var protobuf = require('protocol-buffers') |
|||
var messages = protobuf(fs.readFileSync(path.join(__dirname, 'schema.proto'), 'utf-8')) |
|||
// This file is auto generated by the protocol-buffers cli tool
|
|||
|
|||
module.exports = messages |
|||
/* 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 Request = exports.Request = { |
|||
buffer: true, |
|||
encodingLength: null, |
|||
encode: null, |
|||
decode: null |
|||
} |
|||
|
|||
var Response = exports.Response = { |
|||
buffer: true, |
|||
encodingLength: null, |
|||
encode: null, |
|||
decode: null |
|||
} |
|||
|
|||
defineRequest() |
|||
defineResponse() |
|||
|
|||
function defineRequest () { |
|||
var enc = [ |
|||
encodings.string, |
|||
encodings.bytes |
|||
] |
|||
|
|||
Request.encodingLength = encodingLength |
|||
Request.encode = encode |
|||
Request.decode = decode |
|||
|
|||
function encodingLength (obj) { |
|||
var length = 0 |
|||
if (!defined(obj.command)) throw new Error("command is required") |
|||
var len = enc[0].encodingLength(obj.command) |
|||
length += 1 + len |
|||
if (defined(obj.id)) { |
|||
var len = enc[1].encodingLength(obj.id) |
|||
length += 1 + len |
|||
} |
|||
if (defined(obj.target)) { |
|||
var len = enc[1].encodingLength(obj.target) |
|||
length += 1 + len |
|||
} |
|||
if (defined(obj.forwardRequest)) { |
|||
var len = enc[1].encodingLength(obj.forwardRequest) |
|||
length += 1 + len |
|||
} |
|||
if (defined(obj.forwardResponse)) { |
|||
var len = enc[1].encodingLength(obj.forwardResponse) |
|||
length += 1 + len |
|||
} |
|||
if (defined(obj.roundtripToken)) { |
|||
var len = enc[1].encodingLength(obj.roundtripToken) |
|||
length += 1 + len |
|||
} |
|||
if (defined(obj.value)) { |
|||
var len = enc[1].encodingLength(obj.value) |
|||
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.command)) throw new Error("command is required") |
|||
buf[offset++] = 10 |
|||
enc[0].encode(obj.command, buf, offset) |
|||
offset += enc[0].encode.bytes |
|||
if (defined(obj.id)) { |
|||
buf[offset++] = 18 |
|||
enc[1].encode(obj.id, buf, offset) |
|||
offset += enc[1].encode.bytes |
|||
} |
|||
if (defined(obj.target)) { |
|||
buf[offset++] = 26 |
|||
enc[1].encode(obj.target, buf, offset) |
|||
offset += enc[1].encode.bytes |
|||
} |
|||
if (defined(obj.forwardRequest)) { |
|||
buf[offset++] = 34 |
|||
enc[1].encode(obj.forwardRequest, buf, offset) |
|||
offset += enc[1].encode.bytes |
|||
} |
|||
if (defined(obj.forwardResponse)) { |
|||
buf[offset++] = 42 |
|||
enc[1].encode(obj.forwardResponse, buf, offset) |
|||
offset += enc[1].encode.bytes |
|||
} |
|||
if (defined(obj.roundtripToken)) { |
|||
buf[offset++] = 50 |
|||
enc[1].encode(obj.roundtripToken, buf, offset) |
|||
offset += enc[1].encode.bytes |
|||
} |
|||
if (defined(obj.value)) { |
|||
buf[offset++] = 58 |
|||
enc[1].encode(obj.value, 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 = { |
|||
command: "", |
|||
id: null, |
|||
target: null, |
|||
forwardRequest: null, |
|||
forwardResponse: null, |
|||
roundtripToken: null, |
|||
value: 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.command = enc[0].decode(buf, offset) |
|||
offset += enc[0].decode.bytes |
|||
found0 = true |
|||
break |
|||
case 2: |
|||
obj.id = enc[1].decode(buf, offset) |
|||
offset += enc[1].decode.bytes |
|||
break |
|||
case 3: |
|||
obj.target = enc[1].decode(buf, offset) |
|||
offset += enc[1].decode.bytes |
|||
break |
|||
case 4: |
|||
obj.forwardRequest = enc[1].decode(buf, offset) |
|||
offset += enc[1].decode.bytes |
|||
break |
|||
case 5: |
|||
obj.forwardResponse = enc[1].decode(buf, offset) |
|||
offset += enc[1].decode.bytes |
|||
break |
|||
case 6: |
|||
obj.roundtripToken = enc[1].decode(buf, offset) |
|||
offset += enc[1].decode.bytes |
|||
break |
|||
case 7: |
|||
obj.value = enc[1].decode(buf, offset) |
|||
offset += enc[1].decode.bytes |
|||
break |
|||
default: |
|||
offset = skip(prefix & 7, buf, offset) |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
function defineResponse () { |
|||
var enc = [ |
|||
encodings.bytes |
|||
] |
|||
|
|||
Response.encodingLength = encodingLength |
|||
Response.encode = encode |
|||
Response.decode = decode |
|||
|
|||
function encodingLength (obj) { |
|||
var length = 0 |
|||
if (defined(obj.id)) { |
|||
var len = enc[0].encodingLength(obj.id) |
|||
length += 1 + len |
|||
} |
|||
if (defined(obj.nodes)) { |
|||
var len = enc[0].encodingLength(obj.nodes) |
|||
length += 1 + len |
|||
} |
|||
if (defined(obj.value)) { |
|||
var len = enc[0].encodingLength(obj.value) |
|||
length += 1 + len |
|||
} |
|||
if (defined(obj.roundtripToken)) { |
|||
var len = enc[0].encodingLength(obj.roundtripToken) |
|||
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)) { |
|||
buf[offset++] = 10 |
|||
enc[0].encode(obj.id, buf, offset) |
|||
offset += enc[0].encode.bytes |
|||
} |
|||
if (defined(obj.nodes)) { |
|||
buf[offset++] = 18 |
|||
enc[0].encode(obj.nodes, buf, offset) |
|||
offset += enc[0].encode.bytes |
|||
} |
|||
if (defined(obj.value)) { |
|||
buf[offset++] = 26 |
|||
enc[0].encode(obj.value, buf, offset) |
|||
offset += enc[0].encode.bytes |
|||
} |
|||
if (defined(obj.roundtripToken)) { |
|||
buf[offset++] = 34 |
|||
enc[0].encode(obj.roundtripToken, buf, offset) |
|||
offset += enc[0].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: null, |
|||
nodes: null, |
|||
value: null, |
|||
roundtripToken: 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: |
|||
obj.id = enc[0].decode(buf, offset) |
|||
offset += enc[0].decode.bytes |
|||
break |
|||
case 2: |
|||
obj.nodes = enc[0].decode(buf, offset) |
|||
offset += enc[0].decode.bytes |
|||
break |
|||
case 3: |
|||
obj.value = enc[0].decode(buf, offset) |
|||
offset += enc[0].decode.bytes |
|||
break |
|||
case 4: |
|||
obj.roundtripToken = enc[0].decode(buf, offset) |
|||
offset += enc[0].decode.bytes |
|||
break |
|||
default: |
|||
offset = skip(prefix & 7, buf, offset) |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
function defined (val) { |
|||
return val !== null && val !== undefined && (typeof val !== 'number' || !isNaN(val)) |
|||
} |
|||
|
Loading…
Reference in new issue