Browse Source

use new protobuf

v4
Mathias Buus 7 years ago
parent
commit
ad86f68798
  1. 282
      messages.js
  2. 6
      package.json
  3. 30
      schema.proto

282
messages.js

@ -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))
}

6
package.json

@ -9,13 +9,14 @@
"inherits": "^2.0.3",
"ipv4-peers": "^1.1.1",
"k-bucket": "^3.2.1",
"protocol-buffers": "^3.1.6",
"protocol-buffers-encodings": "^1.1.0",
"readable-stream": "^2.1.5",
"stream-collector": "^1.0.1",
"udp-request": "^1.3.0",
"xor-distance": "^1.0.0"
},
"devDependencies": {
"protocol-buffers": "^3.2.1",
"standard": "^8.6.0",
"tape": "^4.6.3"
},
@ -24,7 +25,8 @@
"url": "https://github.com/mafintosh/dht-rpc.git"
},
"scripts": {
"test": "standard && tape test.js"
"test": "standard && tape test.js",
"protobuf": "protocol-buffers schema.proto -o messages.js"
},
"author": "Mathias Buus (@mafintosh)",
"license": "MIT",

30
schema.proto

@ -1,16 +1,16 @@
message Request {
required string command = 1;
optional bytes id = 2;
optional bytes target = 3;
optional bytes forwardRequest = 4;
optional bytes forwardResponse = 5;
optional bytes roundtripToken = 6;
optional bytes value = 7;
}
message Request {
required string command = 1;
optional bytes id = 2;
optional bytes target = 3;
optional bytes forwardRequest = 4;
optional bytes forwardResponse = 5;
optional bytes roundtripToken = 6;
optional bytes value = 7;
}
message Response {
optional bytes id = 1;
optional bytes nodes = 2;
optional bytes value = 3;
optional bytes roundtripToken = 4;
}
message Response {
optional bytes id = 1;
optional bytes nodes = 2;
optional bytes value = 3;
optional bytes roundtripToken = 4;
}

Loading…
Cancel
Save