Skip to content

Commit 24f2c03

Browse files
committed
Docs: Added notes on how to use pbjs and pbts programmatically
1 parent 3544576 commit 24f2c03

16 files changed

+42
-37
lines changed

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ Documentation
313313

314314
* [Google's Developer Guide](https://developers.google.com/protocol-buffers/docs/overview)
315315

316-
* [protobuf.js API Documentation](http://dcode.io/protobuf.js/)
316+
* [protobuf.js API Documentation](http://dcode.io/protobuf.js/) and [CHANGELOG](https://github.com/dcodeIO/protobuf.js/blob/master/CHANGELOG.md)
317317

318318
* [Questions and answers on StackOverflow](http://stackoverflow.com/questions/tagged/protobuf.js)
319319

@@ -396,6 +396,20 @@ Generates TypeScript definitions from annotated JavaScript files.
396396
usage: pbts [options] file1.js file2.js ...
397397
```
398398

399+
### Using pbjs and pbts programmatically
400+
401+
Both utilities can be used programmatically by providing command line arguments and a callback to their respective `main` functions:
402+
403+
```js
404+
var pbjs = require("protobufjs/cli/pbjs");
405+
406+
pbjs.main([ "--target", "json-module", "path/to/myproto.proto" ], function(err, output) {
407+
if (err)
408+
throw err;
409+
// do something with output
410+
});
411+
```
412+
399413
### Descriptors vs. static modules
400414

401415
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.

dist/protobuf.js

+4-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

-5 Bytes
Binary file not shown.

dist/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.min.js.gz

0 Bytes
Binary file not shown.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"types": "node bin/pbts --name protobufjs --out types/protobuf.js.d.ts src && tsc types/test.ts --lib es2015 --noEmit",
3838
"zuul": "zuul --ui tape --no-coverage --concurrency 4 -- tests/*.js",
3939
"zuul-local": "zuul --ui tape --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",
40-
"make": "npm run lint && npm run test && npm run types && npm run build && npm run changelog"
40+
"make": "npm run lint && npm run test && npm run types && npm run build",
41+
"release": "npm run make && npm run changelog"
4142
},
4243
"dependencies": {
4344
"@protobufjs/aspromise": "^1.0.3",

src/message.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,16 @@ MessagePrototype.asJSON = function asJSON(options) {
4444
options = {};
4545
var fields = this.$type.fields,
4646
json = {};
47-
var keys;
48-
if (options.defaults) {
49-
keys = Object.keys(fields);
50-
} else
51-
keys = Object.keys(this);
47+
var keys = Object.keys(options.defaults ? fields : this);
5248
for (var i = 0, key; i < keys.length; ++i) {
5349
var field = fields[key = keys[i]],
5450
value = this[key];
5551
if (field) {
5652
if (field.repeated) {
5753
if (value && (value.length || options.defaults)) {
58-
var array = new Array(value.length);
54+
json[key] = [];
5955
for (var j = 0, l = value.length; j < l; ++j)
60-
array[j] = field.jsonConvert(value[j], options);
61-
json[key] = array;
56+
json[key].push(field.jsonConvert(value[j], options));
6257
}
6358
} else
6459
json[key] = field.jsonConvert(value, options);

tests/data/ambiguous-names.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ $root.A = (function() {
9090
r instanceof Reader||(r=Reader.create(r))
9191
var c=l===undefined?r.len:r.pos+l,m=new $root.A
9292
while(r.pos<c){
93-
var t=r.int32()
93+
var t=r.uint32()
9494
switch(t>>>3){
9595
case 1:
9696
m["whatever"]=r.string()
@@ -220,7 +220,7 @@ $root.B = (function() {
220220
r instanceof Reader||(r=Reader.create(r))
221221
var c=l===undefined?r.len:r.pos+l,m=new $root.B
222222
while(r.pos<c){
223-
var t=r.int32()
223+
var t=r.uint32()
224224
switch(t>>>3){
225225
case 1:
226226
m["A"]=types[0].decode(r,r.uint32())

tests/data/mapbox/vector_tile.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ $root.vector_tile = (function() {
100100
r instanceof Reader||(r=Reader.create(r))
101101
var c=l===undefined?r.len:r.pos+l,m=new $root.vector_tile.Tile
102102
while(r.pos<c){
103-
var t=r.int32()
103+
var t=r.uint32()
104104
switch(t>>>3){
105105
case 3:
106106
m["layers"]&&m["layers"].length?m["layers"]:m["layers"]=[]
@@ -300,7 +300,7 @@ $root.vector_tile = (function() {
300300
r instanceof Reader||(r=Reader.create(r))
301301
var c=l===undefined?r.len:r.pos+l,m=new $root.vector_tile.Tile.Value
302302
while(r.pos<c){
303-
var t=r.int32()
303+
var t=r.uint32()
304304
switch(t>>>3){
305305
case 1:
306306
m["stringValue"]=r.string()
@@ -507,7 +507,7 @@ $root.vector_tile = (function() {
507507
r instanceof Reader||(r=Reader.create(r))
508508
var c=l===undefined?r.len:r.pos+l,m=new $root.vector_tile.Tile.Feature
509509
while(r.pos<c){
510-
var t=r.int32()
510+
var t=r.uint32()
511511
switch(t>>>3){
512512
case 1:
513513
m["id"]=r.uint64()
@@ -731,7 +731,7 @@ $root.vector_tile = (function() {
731731
r instanceof Reader||(r=Reader.create(r))
732732
var c=l===undefined?r.len:r.pos+l,m=new $root.vector_tile.Tile.Layer
733733
while(r.pos<c){
734-
var t=r.int32()
734+
var t=r.uint32()
735735
switch(t>>>3){
736736
case 15:
737737
m["version"]=r.uint32()

tests/data/package.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ $root.Package = (function() {
251251
r instanceof Reader||(r=Reader.create(r))
252252
var c=l===undefined?r.len:r.pos+l,m=new $root.Package
253253
while(r.pos<c){
254-
var t=r.int32()
254+
var t=r.uint32()
255255
switch(t>>>3){
256256
case 1:
257257
m["name"]=r.string()
@@ -558,7 +558,7 @@ $root.Package = (function() {
558558
r instanceof Reader||(r=Reader.create(r))
559559
var c=l===undefined?r.len:r.pos+l,m=new $root.Package.Repository
560560
while(r.pos<c){
561-
var t=r.int32()
561+
var t=r.uint32()
562562
switch(t>>>3){
563563
case 1:
564564
m["type"]=r.string()

tests/data/rpc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ $root.MyRequest = (function() {
155155
r instanceof Reader||(r=Reader.create(r))
156156
var c=l===undefined?r.len:r.pos+l,m=new $root.MyRequest
157157
while(r.pos<c){
158-
var t=r.int32()
158+
var t=r.uint32()
159159
switch(t>>>3){
160160
case 1:
161161
m["path"]=r.string()
@@ -285,7 +285,7 @@ $root.MyResponse = (function() {
285285
r instanceof Reader||(r=Reader.create(r))
286286
var c=l===undefined?r.len:r.pos+l,m=new $root.MyResponse
287287
while(r.pos<c){
288-
var t=r.int32()
288+
var t=r.uint32()
289289
switch(t>>>3){
290290
case 2:
291291
m["status"]=r.int32()

types/protobuf.js.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// $> pbts --name protobufjs --out types/protobuf.js.d.ts src
2-
// Generated Tue, 20 Dec 2016 13:19:01 UTC
2+
// Generated Tue, 20 Dec 2016 16:43:56 UTC
33
declare module "protobufjs" {
44

55
/**

0 commit comments

Comments
 (0)