Skip to content

Commit

Permalink
Docs: Added notes on how to use pbjs and pbts programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 20, 2016
1 parent 3544576 commit 24f2c03
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 37 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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.
Expand Down
13 changes: 4 additions & 9 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 3 additions & 8 deletions src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/data/ambiguous-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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())
Expand Down
8 changes: 4 additions & 4 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]=[]
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions tests/data/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions tests/data/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion types/protobuf.js.d.ts
Original file line number Diff line number Diff line change
@@ -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" {

/**
Expand Down

0 comments on commit 24f2c03

Please sign in to comment.