Permalink
Browse files

Docs: Added notes on how to use pbjs and pbts programmatically

  • Loading branch information...
dcodeIO committed Dec 20, 2016
1 parent 3544576 commit 24f2c03af9f13f5404259866fdc8fed33bfaae25
@@ -313,7 +313,7 @@ Documentation
* [Google's Developer Guide](https://developers.google.com/protocol-buffers/docs/overview)
* [protobuf.js API Documentation](http://dcode.io/protobuf.js/)
* [protobuf.js API Documentation](http://dcode.io/protobuf.js/) and [CHANGELOG](https://github.com/dcodeIO/protobuf.js/blob/master/CHANGELOG.md)
* [Questions and answers on StackOverflow](http://stackoverflow.com/questions/tagged/protobuf.js)
@@ -396,6 +396,20 @@ Generates TypeScript definitions from annotated JavaScript files.
usage: pbts [options] file1.js file2.js ...
```
### Using pbjs and pbts programmatically
Both utilities can be used programmatically by providing command line arguments and a callback to their respective `main` functions:
```js
var pbjs = require("protobufjs/cli/pbjs");
pbjs.main([ "--target", "json-module", "path/to/myproto.proto" ], function(err, output) {
if (err)
throw err;
// do something with output
});
```
### Descriptors vs. static modules
While .proto and JSON files require the full library (about 17.5kb gzipped), pretty much all code but the relatively short descriptors is shared and all features including reflection and the parser are available.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN -5 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
BIN +0 Bytes (100%) dist/runtime/protobuf.min.js.gz
Binary file not shown.
@@ -37,7 +37,8 @@
"types": "node bin/pbts --name protobufjs --out types/protobuf.js.d.ts src && tsc types/test.ts --lib es2015 --noEmit",
"zuul": "zuul --ui tape --no-coverage --concurrency 4 -- tests/*.js",
"zuul-local": "zuul --ui tape --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",
"make": "npm run lint && npm run test && npm run types && npm run build && npm run changelog"
"make": "npm run lint && npm run test && npm run types && npm run build",
"release": "npm run make && npm run changelog"
},
"dependencies": {
"@protobufjs/aspromise": "^1.0.3",
@@ -44,21 +44,16 @@ MessagePrototype.asJSON = function asJSON(options) {
options = {};
var fields = this.$type.fields,
json = {};
var keys;
if (options.defaults) {
keys = Object.keys(fields);
} else
keys = Object.keys(this);
var keys = Object.keys(options.defaults ? fields : this);
for (var i = 0, key; i < keys.length; ++i) {
var field = fields[key = keys[i]],
value = this[key];
if (field) {
if (field.repeated) {
if (value && (value.length || options.defaults)) {
var array = new Array(value.length);
json[key] = [];
for (var j = 0, l = value.length; j < l; ++j)
array[j] = field.jsonConvert(value[j], options);
json[key] = array;
json[key].push(field.jsonConvert(value[j], options));
}
} else
json[key] = field.jsonConvert(value, options);
@@ -90,7 +90,7 @@ $root.A = (function() {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.A
while(r.pos<c){
var t=r.int32()
var t=r.uint32()
switch(t>>>3){
case 1:
m["whatever"]=r.string()
@@ -220,7 +220,7 @@ $root.B = (function() {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.B
while(r.pos<c){
var t=r.int32()
var t=r.uint32()
switch(t>>>3){
case 1:
m["A"]=types[0].decode(r,r.uint32())
@@ -100,7 +100,7 @@ $root.vector_tile = (function() {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.vector_tile.Tile
while(r.pos<c){
var t=r.int32()
var t=r.uint32()
switch(t>>>3){
case 3:
m["layers"]&&m["layers"].length?m["layers"]:m["layers"]=[]
@@ -300,7 +300,7 @@ $root.vector_tile = (function() {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.vector_tile.Tile.Value
while(r.pos<c){
var t=r.int32()
var t=r.uint32()
switch(t>>>3){
case 1:
m["stringValue"]=r.string()
@@ -507,7 +507,7 @@ $root.vector_tile = (function() {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.vector_tile.Tile.Feature
while(r.pos<c){
var t=r.int32()
var t=r.uint32()
switch(t>>>3){
case 1:
m["id"]=r.uint64()
@@ -731,7 +731,7 @@ $root.vector_tile = (function() {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.vector_tile.Tile.Layer
while(r.pos<c){
var t=r.int32()
var t=r.uint32()
switch(t>>>3){
case 15:
m["version"]=r.uint32()
@@ -251,7 +251,7 @@ $root.Package = (function() {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.Package
while(r.pos<c){
var t=r.int32()
var t=r.uint32()
switch(t>>>3){
case 1:
m["name"]=r.string()
@@ -558,7 +558,7 @@ $root.Package = (function() {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.Package.Repository
while(r.pos<c){
var t=r.int32()
var t=r.uint32()
switch(t>>>3){
case 1:
m["type"]=r.string()
@@ -155,7 +155,7 @@ $root.MyRequest = (function() {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.MyRequest
while(r.pos<c){
var t=r.int32()
var t=r.uint32()
switch(t>>>3){
case 1:
m["path"]=r.string()
@@ -285,7 +285,7 @@ $root.MyResponse = (function() {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.MyResponse
while(r.pos<c){
var t=r.int32()
var t=r.uint32()
switch(t>>>3){
case 2:
m["status"]=r.int32()
@@ -1,5 +1,5 @@
// $> pbts --name protobufjs --out types/protobuf.js.d.ts src
// Generated Tue, 20 Dec 2016 13:19:01 UTC
// Generated Tue, 20 Dec 2016 16:43:56 UTC
declare module "protobufjs" {
/**

0 comments on commit 24f2c03

Please sign in to comment.