How to use the load function from protobufjs

Find comprehensive JavaScript protobufjs.load code examples handpicked from public code repositorys.

protobufjs.load is a function that loads a .proto file and generates corresponding JavaScript code.

2
3
4
5
6
7
8
9
10
11
var protobuf = require('protobufjs');

protobuf.parse.defaults.keepCase = true;

function run () {
    protobuf.load('./Thoth.UnitedMessage.proto')
            .then(loaded5) 
            .catch(function(e){
                console.log(e.message);
            });
fork icon224
star icon469
watch icon75

+ 5 other calls in file

59
60
61
62
63
64
65
66
67
loadProtoFile()
  .then(r => (root = r))
  .catch(console.error);

function loadProtoFile() {
  return protobuf.load(
    path.join(__dirname, '..', 'src', 'protos', 'checkin.proto')
  );
}
fork icon62
star icon0
watch icon2

How does protobufjs.load work?

protobufjs.load is a function in the Protobuf.js library that loads a protobuf file from a given file path or URL and creates a root object containing all the messages and enums defined in the file, which can then be used to encode and decode data according to the protocol buffer specification. When called, it sends a request to the specified path or URL to retrieve the contents of the protobuf file, parses it and returns a promise that resolves with the root object.

50
51
52
53
54
55
56
57
58
59
PeerInfo.create((err, peerInfo) => {
    if (err) 
        throw new Error(err)
    
    // Load your .proto file
    protobuf.load(path.join(__dirname, './protocol.proto')).then((root) => {
    
        // Create Node
        const node = new Node(peerInfo, root, config)
        
fork icon5
star icon14
watch icon3

6
7
8
9
10
11
12
13
14
    this.protoFile = protoFile
    this.typeName = typeName
}

async init () {
    this.protoRoot = await protobuf.load(path.join(__dirname,  '../proto/',  this.protoFile))
    this.type = this.protoRoot.lookupType(this.typeName)
    return this
}
fork icon2
star icon12
watch icon4

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const protobuf = require("protobufjs");

protobuf.load("myProtoFile.proto", function (err, root) {
  if (err) throw err;

  // Access the types defined in the proto file
  const MyMessage = root.lookupType("package.MyMessage");

  // Create a new message
  const message = MyMessage.create({ foo: "bar" });

  // Encode the message as bytes
  const buffer = MyMessage.encode(message).finish();

  console.log(buffer);
});

In this example, we use protobuf.load to load a Protocol Buffers schema from a file (myProtoFile.proto). Once the schema is loaded, we use the root object to access the message types defined in the schema. We then create a new instance of the MyMessage type, set a field value (foo) on the message, and encode the message as bytes using the MyMessage.encode method. Finally, we log the resulting buffer to the console.

20
21
22
23
24
25
26
27
28
29
    this.loadProtos();
}

async loadProtos(): Promise<any> {
    // alt protos for Android
    const load = await protobuf.load('protos/Alternate.Signature.proto');
    this.altProtos = load.POGOProtos;
}

async decodeRequest(session: string, requestId: string, force = false): Promise<any> {
fork icon1
star icon6
watch icon3

21
22
23
24
25
26
27
28
29
30

const protos = {}

const loadProtos = (filename, protoNames) => {
  const protoPath = path.resolve(__dirname, '../../protos', filename)
  return protobuf.load(protoPath)
    .then(root => {
      protoNames.forEach(name => {
        protos[name] = root.lookupType(name)
      })
fork icon211
star icon0
watch icon47

153
154
155
156
157
158
159
160
161
162
        setTimeout(polling, POLLING_INTERVAL);
    });
}

function startPolling() {
    protobuf.load("gtfs.proto", function(err, root) {
        if (err) {
            throw error;
        }
        FeedMessage = root.lookupType("transit_realtime.FeedMessage");
fork icon0
star icon3
watch icon1

+ 9 other calls in file

78
79
80
81
82
83
84
85
86
    curl.perform()
  })
}

async function main() {
  const root = await protobuf.load(protoPath)

  const RequestGreet = root.lookup('RequestGreet')
  const ResponseGreet = root.lookup('ResponseGreet')
fork icon97
star icon0
watch icon2

0
1
2
3
4
5
6
7
8
9
var fs = require('fs');
var path = require('path');
var protobuf = require('protobufjs');
var protoPath = path.join(__dirname, 'protos', 'rs.proto');

// var builder = protobuf.load(protoPath, function (err, root) {
//      console.log(err, root);
// });

var content = fs.readFileSync(protoPath, 'utf-8');
fork icon1
star icon0
watch icon2

+ 9 other calls in file

40
41
42
43
44
45
46
47
48
49
module.exports = class Parser extends EventEmitter {
  static async init() {
    if (proto) {
      return;
    }
    proto = await load(path.resolve(__dirname, 'mcs.proto'));
  }

  constructor(socket) {
    super();
fork icon61
star icon0
watch icon10

705
706
707
708
709
710
711
712
713
714
root.resolvePath = function (origin, file) {
    var str = this.options.root + "/" + file;
    // console.log(`Origin: "${origin}"; String: "${str}"`);
    return str;
};
pb.load([
    "tbui/tbcharacter.proto",
    "tbmatch/event.proto",
    "tbmatch/match.proto",
    "tbportal/portal.proto",
fork icon1
star icon0
watch icon0

+ 2 other calls in file

2
3
4
5
6
7
8
9
10
11
12
const imFile = "../public/IMClientParams.proto";
const imEnumFile = "../public/IMClientEnum.proto";


function start_websocket() {
    (async () => {
        const imContent = await protobuf.load(imFile);
        const imEnum = await protobuf.load(imEnumFile);
        
        // 生成解析器
        const imContentParser = imContent.lookupType("com.example.mylibrary.IMClientParams");
fork icon0
star icon1
watch icon1

+ 59 other calls in file

9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084


async function run() {


    const ws = new WebSocket("ws://127.0.0.1:8081");


    const root = await protobuf.load("protobuf/Messages.proto");
    const Req = root.lookupType("FoDoMustProto.RequestMessage");
    const Resp = root.lookupType("FoDoMustProto.ResponseMessage");


    var connection;
fork icon0
star icon1
watch icon0

+ 2 other calls in file

34
35
36
37
38
39
40
41
42
43
44
45


app.set('prod', process.env.prod);


/* encode the JSON message into protobuf
async function encodeMessage(payload, messageName) {
  const root = await protobuf.load("buyer.proto");
  const testMessage = root.lookupType(messageName);
  const message = testMessage.create(payload);
  return testMessage.encode(message).finish();
}*/
fork icon0
star icon0
watch icon1

+ 59 other calls in file

91
92
93
94
95
96
97
98
99
100
101
102


async function loadProtoFile() {
  if (root) {
    return;
  }
  root = await protobuf.load(path.join(process.cwd(), 'storage', 'gcm', 'checkin.proto'));
  return root;
}


function getCheckinRequest(androidId, securityToken) {
fork icon0
star icon0
watch icon0

+ 4 other calls in file

91
92
93
94
95
96
97
98
99
100
101
102


async function loadProtoFile() {
  if (root) {
    return;
  }
  root = await protobuf.load(path.join(__dirname, 'checkin.proto'));
  return root;
}


function getCheckinRequest(androidId, securityToken) {
fork icon0
star icon0
watch icon1

+ 18 other calls in file

91
92
93
94
95
96
97
98
99
100
let self = this;

return new Promise((resolve, reject) => {
  // TODO: Use new Protobuf.js API

  protobuf.load('node_modules/wire-webapp-protocol-messaging/proto/messages.proto')
    .then((root) => {
      self.protocolBuffer.GenericMessage = root.lookup('GenericMessage');
      self.protocolBuffer.Text = root.lookup('Text');
      return self.service.user.login();
fork icon0
star icon0
watch icon2