You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**Note:** Dynamically generated runtime message classes cannot be typed, technically, so you must either access its fields using `message["awesomeField"]` notation or you can utilize [typings of its static counterpart](https://github.com/dcodeIO/protobuf.js/tree/master/cli#pbts) for full typings support.
480
+
**Note:** Dynamically generated runtime message classes cannot be typed, technically, so you must either access its fields using `message["awesomeField"]` notation or you can utilize [typings of its static counterpart](#pbts-for-typescript) for full typings support.
469
481
470
482
If you generated static code to `bundle.js` using the CLI and its type definitions to `bundle.d.ts` instead, then you can just do:
471
483
@@ -486,25 +498,177 @@ var buffer = AwesomeMessage.encode(message).finish();
For production environments it is recommended to bundle all your .proto files to a single .json file, which minimizes the number of network requests and avoids any parser overhead (hint: works with just the **light** library):
Picking up on the example above, the following not just generates static code to a CommonJS module `compiled.js` but also its respective TypeScript definitions to `compiled.d.ts`:
Additionally, TypeScript definitions of static modules are compatible with their reflection-based counterparts (i.e. as exported by JSON modules), as long as the following conditions are met:
625
+
626
+
1. Instead of using `new SomeMessage(...)`, always use `SomeMessage.create(...)` because reflection objects do not provide a constructor.
627
+
2. Types, services and enums must start with an uppercase letter to become available as properties of the reflected types as well (i.e. to be able to use `MyMessage.MyEnum` instead of `root.lookup("MyMessage.MyEnum")`).
628
+
629
+
For example, the following generates a JSON module `bundle.js` and a `bundle.d.ts`, but no static code:
While using .proto files directly requires the full library respectively pure reflection/JSON the light library, pretty much all code but the relatively short descriptors is shared.
639
+
640
+
Static code, on the other hand, requires just the minimal library, but generates additional, albeit editable, source code without any reflection features.
641
+
642
+
There is no significant difference performance-wise as the code generated statically is pretty much the same as generated at runtime and both are largely interchangeable as seen in the previous section.
643
+
644
+
### Command line API
645
+
646
+
Both utilities can be used programmatically by providing command line arguments and a callback to their respective `main` functions:
647
+
648
+
```js
649
+
var pbjs =require("protobufjs/cli/pbjs"); // or require("protobufjs/cli").pbjs / .pbts
*[Frequently asked questions](https://github.com/dcodeIO/protobuf.js/wiki) on our wiki
500
668
501
669
#### Community
502
670
*[Questions and answers](http://stackoverflow.com/search?tab=newest&q=protobuf.js) on StackOverflow
503
671
504
-
Command line
505
-
------------
506
-
Command line usage has moved to the (soon to be decoupled) [CLI package](./cli/README.md)
507
-
508
672
Performance
509
673
-----------
510
674
The package includes a benchmark that tries to compare performance to native JSON as far as this is possible. On an i7-2600K running node 6.9.1 it yields:
0 commit comments