1.0.2 • Published 8 years ago

protobuf-compiler v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

protobuf-compiler

Compile ProtoBuf .proto files to JavaScript .js classes.

Build Status

Installation

sudo npm install -g protobuf-compiler

Usage

First compile protos with the command line interface:

protobuf-compile example.proto # exports exampleProto.js.
protobuf-compile exampleFolder # compiles all protoes in the folder.

Then import the generated js files and use:

/* 
 * Suppose we have the following in example.proto:
 * message Request {
 *   enum Type {
 *     GET = 1;
 *     POST = 2;
 *   }
 *   optional string url = 1;
 *   optional Type type = 2;
 *   repeated string cookie = 3;
 * }
 */
var Request = require('./exampleProto').Request;
var request =
    Request.newBuilder()
        .setUrl('test.com')
        .setType(Request.Type.GET)
        .addCookie('test')
        .build();
console.log(request.getUrl()); // 'test.com'
console.log(request.getCookieList()); // ['test']

var json = JSON.stringify(request); // ["test.com", 1, "test"]
var requestFromJSON = Request.newBuilder().fromJSON(json).build();

Check tests folder for more examples.

Can also be used directly at frontend:

<script src="basicProto.js"></script>
<script>
var author = Author.newBuilder().setName('My Name').build();
</script>