pbjs
A minimal implementation of Google Protocol Buffers for JavaScript
Last updated 10 months ago by evanw .
Original npm · Tarball · package.json
$ cnpm install pbjs 
SYNC missed versions from official npm registry.

Protocol Buffers for JavaScript

This is a minimal implementation of Google Protocol Buffers for JavaScript and TypeScript. It generates completely self-contained code without any dependencies. Install it using npm:

npm install pbjs

Unlike other JavaScript implementations, this library doesn't write out default values. This makes it possible to tell if a field has been written at all or not, which allows for efficient encoding of maps. For example, it's possible to distinguish between a missing list and a list that is present but empty.

Command-Line Examples

  • Generate ES5 JavaScript:

    pbjs wire-format.proto --es5 wire-format.js
    

    See test.proto.es5.js for an example of the generated code.

  • Generate ES6 JavaScript:

    pbjs wire-format.proto --es6 wire-format.js
    

    See test.proto.es6.js for an example of the generated code.

  • Generate TypeScript:

    pbjs wire-format.proto --ts wire-format.ts
    

    See test.proto.ts for an example of the generated code.

  • Convert to JSON:

    pbjs wire-format.proto --decode MessageType < wire-format.bin > wire-format.json
    
  • Convert to Binary:

    pbjs wire-format.proto --encode MessageType < wire-format.json > wire-format.bin
    

API Example

const pbjs = require('pbjs');

const schema = pbjs.parseSchema(`
  message Demo {
    optional int32 x = 1;
    optional float y = 2;
  }
`).compile();

const buffer = schema.encodeDemo({x: 1, y: 2});
console.log(buffer);

const message = schema.decodeDemo(buffer);
console.log(message);

Running the above code should output this:

<Buffer 08 01 15 00 00 00 40>
{ x: 1, y: 2 }

Current Tags

  • 0.0.14                                ...           latest (10 months ago)

14 Versions

  • 0.0.14                                ...           10 months ago
  • 0.0.13                                ...           10 months ago
  • 0.0.12                                ...           10 months ago
  • 0.0.11                                ...           10 months ago
  • 0.0.10                                ...           10 months ago
  • 0.0.9                                ...           10 months ago
  • 0.0.8                                ...           10 months ago
  • 0.0.7                                ...           10 months ago
  • 0.0.6                                ...           10 months ago
  • 0.0.5                                ...           3 years ago
  • 0.0.4                                ...           3 years ago
  • 0.0.3                                ...           4 years ago
  • 0.0.2                                ...           4 years ago
  • 0.0.1                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 9
This Month 94
Last Day 0
Last Week 18
Last Month 105
Dependencies (2)
Dev Dependencies (5)

Copyright 2014 - 2016 © taobao.org |