How to use protobufjs

Comprehensive protobufjs code examples:

How to use protobufjs.PACKABLE_WIRE_TYPES:

23
24
25
26
27
28
29
30
31
32
protobuf.loadProtoFile('protos/Alternate.Signature.proto', builder);

function addPackedOption(ns) {
    if (ns instanceof protobuf.Reflect.Message) {
        ns.getChildren(protobuf.Reflect.Message.Field).forEach(field => {
            if (field.repeated && protobuf.PACKABLE_WIRE_TYPES.indexOf(field.type.wireType) !== -1) {
                field.options.packed = true;
            }
        });
        ns.getChildren(protobuf.Reflect.Message).forEach(addPackedOption);

How to use protobufjs.Protos:

637
638
639
640
641
642
643
644
645
646
},{"protobufjs":16}],2:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var routes_1 = require("./routes");
var pb = require("./pb-handlers");
exports.protos = new pb.Protos();
exports.protos.onProtosLoad = function () {
    run();
};
function run() {

How to use protobufjs.Reflect:

21
22
23
24
25
26
27
28
29
30
loadAltProtos(): any {
    let builder = protobuf.newBuilder();
    protobuf.loadProtoFile('protos/Alternate.Signature.proto', builder);

    function addPackedOption(ns) {
        if (ns instanceof protobuf.Reflect.Message) {
            ns.getChildren(protobuf.Reflect.Message.Field).forEach(field => {
                if (field.repeated && protobuf.PACKABLE_WIRE_TYPES.indexOf(field.type.wireType) !== -1) {
                    field.options.packed = true;
                }

How to use protobufjs.Util:

19
20
21
22
23
24
25
26
27
28
    builder = callback,
    callback = null;
else if (!callback || typeof callback !== 'function')
    callback = null;
if (callback)
    return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {
        if (contents === null) {
            callback(Error("Failed to fetch file"));
            return;
        }

How to use protobufjs.BufferWriter:

29
30
31
32
33
34
35
36
37
38
 * @method formatInputBool
 * @param {Boolean}
 * @returns {Buffer}
 */
var formatInputBool = function (value, fieldNumber) {
    var w = new protobuf.BufferWriter();
    // Tag
    w.uint32(fieldNumber << 3);
    // Data
    w.bool(value);

How to use protobufjs.loadJson:

13
14
15
16
17
18
19
20
21

var ProtoBuf = require('protobufjs');
var OrUtils = require('open-registry-utils');
var Promise = require('bluebird');

var builder = ProtoBuf.loadJson(require('../schemas/registrant.proto.json'));
var Registrant = builder.build('Registrant').Registrant;
builder = ProtoBuf.loadJson(require('../schemas/schema.proto.json'));
var Schema = builder.build('Schema').Schema;

How to use protobufjs.protoFromFile:

2
3
4
5
6
7
8
9
10
11
var protobuf = require('protobufjs');
var director = require('./director.js');
var converter = require('./converter.js');
var tile = require('./tile.js');

var proto = protobuf.protoFromFile("./../proto/vector_tile.proto");
var MapnikVector = proto.build("mapnik.vector");

parse = function (file) {
    var data = fs.readFileSync(file);

How to use protobufjs.Type:

5
6
7
8
9
10
11
12
13
14
const decoder = require('./lib/decoder');
const converter = require('./lib/converter');
const parse = require('./lib/parse');
const protobuf = require('protobufjs');
const common = protobuf.common;
const Type = protobuf.Type;
const Root = protobuf.Root;
const Reader = protobuf.Reader;
const Writer = protobuf.Writer;
const util = protobuf.util;

How to use protobufjs.wrappers:

11
12
13
14
15
16
17
18
19
20
21
const Root = protobuf.Root;
const Reader = protobuf.Reader;
const Writer = protobuf.Writer;
const util = protobuf.util;
const verifier = protobuf.verifier;
const wrappers = protobuf.wrappers;


const originalSetup = Type.prototype.setup;
const originGenerateConstructor = Type.generateConstructor;

How to use protobufjs.verifier:

10
11
12
13
14
15
16
17
18
19
20
const Type = protobuf.Type;
const Root = protobuf.Root;
const Reader = protobuf.Reader;
const Writer = protobuf.Writer;
const util = protobuf.util;
const verifier = protobuf.verifier;
const wrappers = protobuf.wrappers;


const originalSetup = Type.prototype.setup;
const originGenerateConstructor = Type.generateConstructor;

How to use protobufjs.loadProto:

60
61
62
63
64
65
66
67
68
69
            } catch (e) {
                callback(e);
            }
        });
    var contents = cc.loader.loadRes(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename); 
    return contents === null ? null : ProtoBuf.loadProto(contents, builder, filename);
};
```

---

How to use protobufjs.rpc:

308
309
310
311
312
313
314
315
316
317
const cachedService = this.serviceCache.get(serviceName);
if (cachedService) {
    return cachedService;
}
const rpcServiceConstructor = services[serviceName];
if (!(rpcServiceConstructor.prototype instanceof protobufjs_1.rpc.Service)) {
    throw new InvalidRPCServiceError(`Service with serviceName ${serviceName} does not support remote procedure calls`);
}
const rpcImplementation = this.getRpcImpl(serviceName);
const service = new rpcServiceConstructor(rpcImplementation);

How to use protobufjs.roots:

38
39
40
41
42
43
44
45
46
47
let meta_graph = null;
const identifier = context.identifier;
const extension = identifier.split('.').pop().toLowerCase();
if (extension === 'pbtxt' || identifier.toLowerCase().endsWith('.uff.txt')) {
    try {
        uff.proto = protobuf.roots.uff.uff;
        const reader = prototxt.TextReader.create(context.text);
        const field = reader.field;
        reader.field = (token, module) => {
            if (token === 'descriptors' || token === 'graphs' || token == 'referenced_data') {

How to use protobufjs.Reader:

7
8
9
10
11
12
13
14
15
16
const parse = require('./lib/parse');
const protobuf = require('protobufjs');
const common = protobuf.common;
const Type = protobuf.Type;
const Root = protobuf.Root;
const Reader = protobuf.Reader;
const Writer = protobuf.Writer;
const util = protobuf.util;
const verifier = protobuf.verifier;
const wrappers = protobuf.wrappers;

How to use protobufjs.common:

4
5
6
7
8
9
10
11
12
13
const encoder = require('./lib/encoder');
const decoder = require('./lib/decoder');
const converter = require('./lib/converter');
const parse = require('./lib/parse');
const protobuf = require('protobufjs');
const common = protobuf.common;
const Type = protobuf.Type;
const Root = protobuf.Root;
const Reader = protobuf.Reader;
const Writer = protobuf.Writer;

How to use protobufjs.Writer:

246
247
248
249
250
251
252
253
254
255
        null != t.stepInfoList && t.stepInfoList.length) for (var o = 0; o < t.stepInfoList.length; ++o)                c.protocol.MatchStepInfo.encode(t.stepInfoList[o], e.uint32(34).fork()).ldelim();
        return e;
}
*/
t=f
e = protobufjs.Writer.create()
e.uint32(8).int32(t.gameType)
for (var o = 0; o < t.stepInfoList.length; ++o) {
        MatchStepInfo_encode(f.stepInfoList[o], e.uint32(34).fork()).ldelim()
}

How to use protobufjs.BufferReader:

30
31
32
33
34
35
36
37
38
39
         *
         * @param {Buffer} firstChunk - first chunk (or whole) of serialized data
         * @return {{length: number, dataOffset: number}} length - data length, dataOffset - position of payload in chunk
         */
        static readMsgLength(firstChunk) {
            const buffReader = new BufferReader(firstChunk);
            return {length: buffReader.int32(), dataOffset: buffReader.pos};
        }
    };
};

How to use protobufjs.newBuilder:

54
55
56
57
58
59
60
61
62
                                }
                        ]
                }
        });

        this.protobuf = protobuf.newBuilder({}).import(definitions).build('pcov.proto');

        this.lists = [];
}

How to use protobufjs.parse:

44
45
46
47
48
49
50
51
52
53

describe('Protobuf encoding', function () {
    var expectedArray = [0x0a, 0x0d, 0x41, 0x77, 0x65, 0x73, 0x6f, 0x6d, 0x65, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67];
    var expectedBuffer = Buffer.from(expectedArray);
    it("Encodes a proto message", function () {
        root = protobuf.parse(myMsg).root;

        // Obtain a message type
        var AwesomeMessage = root.lookupType(".awesomepackage.AwesomeMessage");
        var payload = { awesomeField: "AwesomeString" };

How to use protobufjs.loadSync:

How to use protobufjs.loadProtoFile:

27
28
29
30
31
32
33
34
35
36
```js
var ProtoBuf = require('protobufjs')
var ProtoBufStream = require('protobufjs-stream')

// load the protos using the ProtoBuf library
var protos = ProtoBuf.loadProtoFile("nested.proto")
var SearchResponse = protos.result.SearchResponse

// make a stream-creating object for SearchResponse
var srStreamer = ProtoBufStream(SearchResponse)

How to use protobufjs.util:

16
17
18
19
20
21
22
23
24
25
module.exports = require('./bundle.js');

// Allow client code to translate the obscure uint64 field format to/from a number
module.exports.numberToUint64 = protobuf.util.longToHash;
module.exports.uint64ToNumber = (uint64Value) => {
    const value = protobuf.util.longFromHash(uint64Value, true);
    return typeof value === 'number' ? value : value.toInt();
}

// Build the services

How to use protobufjs.Root:

-2
-1

module.exports = protos.POGOProtos;

How to use protobufjs.load:

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