How to use the protobufjs.loadProto function in protobufjs

To help you get started, we’ve selected a few protobufjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ipfs / js-ipfs / submodules / ipfs-packet / index.js View on Github external
var fs = require('fs')
var inherits = require('inherits')
var multihash = require('multihashes')
// var protobuf = require('protocol-buffers')
// var proto2json = require('proto2json')
var protobuf = require('protobufjs')
var protobufStream = require('protobufjs-stream')
var mapv = require('map-values')

var pkt = module.exports = {}

// encoders/decoders
var protoFile = fs.readFileSync(__dirname + '/messages.proto', 'utf-8')
// pkt.protos = proto2json.parse(protoFile)
// pkt.schemas = mapv(pkt.protos.messages, function(v, k) { return protobuf(v) })
pkt.protos = protobuf.loadProto(protoFile).result.messages
pkt.schemas = mapv(pkt.protos, protobufStream)

// packet classes
pkt.Packet = require('./js/packet')
pkt.Frame = require('./js/frame')
pkt.PayloadFrame = require('./js/payload-frame')
pkt.NetworkFrame = require('./js/network-frame')
pkt.IntegrityFrame = require('./js/integrity-frame')
pkt.NonceFrame = require('./js/nonce-frame')
pkt.DataMessage = require('./js/data-message')

// utilities
pkt.peek = require('./js/peek')

// Register classes with PayloadFrame, so that it can
// instantiate them when decoding.
github agrande / lba2remake / src / renderer / utils / Cardboard.js View on Github external
import ProtoBuf from 'protobufjs';
import CardboardDeviceProto from './CardboardDevice.proto';

// to and from URL-safe variant of base64 encoding
function base64ToUrl(s) {
    return s.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
}

function base64FromUrl(s) {
    s += '==='.slice(0, [0, 3, 2, 1][s.length % 4]);
    return s.replace(/-/g, '+').replace(/_/g, '/');
}

const PARAMS_URI_PREFIX = 'http://google.com/cardboard/cfg?p=';

const DeviceParams = ProtoBuf.loadProto(CardboardDeviceProto).build().DeviceParams;

function paramsToUri(params) {
    const msg = new DeviceParams(params);
    return PARAMS_URI_PREFIX + base64ToUrl(msg.toBase64());
}

// TODO: support Cardboard v1 URI (i.e. default params)
function uriToParamsProto(uri) {
    uri = uri.replace('https://vr.google.com/cardboard/download/?p=', 'http://google.com/cardboard/cfg?p=');
    if (uri.substring(0, PARAMS_URI_PREFIX.length) !== PARAMS_URI_PREFIX) {
        return null;
    }
    const base64_msg = base64FromUrl(uri.substring(PARAMS_URI_PREFIX.length));
    // TODO: round numeric values
    return DeviceParams.decode64(base64_msg);
}
github gabrielgrant / grpc-bus-websocket-proxy-client / index.js View on Github external
GBC.prototype.connect = function() {
  var self = this;
  var ws = this.ws = new WebSocket(this.proxyURL);
  ws.binaryType = "arraybuffer";  // We are talking binary
  var wsPromise = new RSVP.Promise(function(resolve, reject) {
    ws.onopen = function(){
      return resolve(ws);
    }
    ws.onerror = reject;
  });
  //var gbTree = protobuf.loadJson(require('./grpc-bus.proto.json')).build().grpcbus;
  // When loaded from the compiled JSON, the "create service" call's
  // "result" field is being loaded as `null` rather than `0`, causing
  // the response to be treated as an error.
  // Workaround: load a JS-stringified version of grpc-bus.proto insead
  var gbTree = protobuf.loadProto(require('./grpc-bus.proto.js')).build().grpcbus;
  return RSVP.hash({
    protoFileContents: fetchProtoFilePromise(this.protoFile),
    ws: wsPromise
  }).then(function(results) {
    var protoFileExt = self.protoFile.substr(self.protoFile.lastIndexOf('.') + 1);
    if (protoFileExt === "json") {
      self.protoDefs = protobuf.loadJson(results.protoFileContents, null, self.protoFile);
    } else {
      self.protoDefs = protobuf.loadProto(results.protoFileContents, null, self.protoFile);
    }
    var initMessage = {
      filename: self.protoFile,
      contents: results.protoFileContents
    };
    ws.send(JSON.stringify(initMessage));
    var gbClient = new grpcBus.Client(self.protoDefs, function(msg) {
github gabrielgrant / grpc-bus-websocket-proxy-server / server.js View on Github external
ws.once('message', function incoming(data, flags) {
    var message = JSON.parse(data);
    console.log('connected with');
    console.dir(message, { depth: null });
    var protoFileExt = message.filename.substr(message.filename.lastIndexOf('.') + 1);
    if (protoFileExt === "json") {
      protoDefs = protobuf.loadJson(message.contents, null, message.filename);
    } else {
      protoDefs = protobuf.loadProto(message.contents, null, message.filename);
    }
    console.log('protoDefs');
    console.log(protoDefs);
    var gbServer = new grpcBus.Server(protoDefs, function(message) {
      console.log('sending (pre-stringify): %s')
      console.dir(message, { depth: null });
      console.log('sending (post-stringify): %s')
      console.dir(JSON.stringify(message));
      //ws.send(JSON.stringify(message));
      var pbMessage = new gbTree.GBServerMessage(message);
      console.log('sending (pbMessage message):', pbMessage);
      console.log('sending (raw message):', pbMessage.toBuffer());
      console.log('re-decoded message:', gbTree.GBServerMessage.decode(pbMessage.toBuffer()));
      if (ws.readyState === ws.OPEN) {
        ws.send(pbMessage.toBuffer());
      } else {
github aertslab / SCope / opt / scopeserver / bindserver / server.js View on Github external
ws.once('message', function incoming(data, flags) {
    var message = JSON.parse(data);
    var protoFileExt = message.filename.substr(message.filename.lastIndexOf('.') + 1);
    if (protoFileExt === "json") {
      protoDefs = protobuf.loadJson(message.contents, null, message.filename);
    } else {
      protoDefs = protobuf.loadProto(message.contents, null, message.filename);
    }
    var gbServer = new grpcBus.Server(protoDefs, function(message) {
      var pbMessage = new gbTree.GBServerMessage(message);
      if (ws.readyState === ws.OPEN) {
        ws.send(pbMessage.toBuffer());
      } else {
      }
    }, require('grpc'));

    ws.on('message', function incoming(data, flags) {

      var message = gbTree.GBClientMessage.decode(data);

      gbServer.handleMessage(message);
    });
  });
github openchain / openchain-js / lib / schema.js View on Github external
bytes version = 3;              \
}                                   \
                                    \
message Mutation {                  \
    bytes namespace = 1;            \
    repeated Record records = 2;    \
    bytes metadata = 3;             \
}                                   \
                                    \
message Transaction {               \
    bytes mutation = 1;             \
    int64 timestamp = 2;            \
    bytes transaction_metadata = 3; \
}";

var builder = ProtoBuf.loadProto(schema).build();

module.exports = {
    Record: builder.Openchain.Record,
    Mutation: builder.Openchain.Mutation,
    Transaction: builder.Openchain.Transaction
};
github gabrielgrant / grpc-bus-websocket-proxy-client / index.js View on Github external
}).then(function(results) {
    var protoFileExt = self.protoFile.substr(self.protoFile.lastIndexOf('.') + 1);
    if (protoFileExt === "json") {
      self.protoDefs = protobuf.loadJson(results.protoFileContents, null, self.protoFile);
    } else {
      self.protoDefs = protobuf.loadProto(results.protoFileContents, null, self.protoFile);
    }
    var initMessage = {
      filename: self.protoFile,
      contents: results.protoFileContents
    };
    ws.send(JSON.stringify(initMessage));
    var gbClient = new grpcBus.Client(self.protoDefs, function(msg) {
      var pbMessage = new gbTree.GBClientMessage(msg)
      var rawMessage = pbMessage.toBuffer();
      ws.send(rawMessage);
    });

    ws.onmessage = function (event) {
      var message = gbTree.GBServerMessage.decode(event.data);
      gbClient.handleMessage(message);
    }