Skip to content

Commit 412407d

Browse files
committed
Docs: Restructured README
1 parent bed5142 commit 412407d

File tree

2 files changed

+177
-194
lines changed

2 files changed

+177
-194
lines changed

README.md

Lines changed: 175 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,24 @@ Contents
1717
* [Examples](#examples)<br />
1818
A few examples to get you started.
1919

20-
* [Documentation](#documentation)<br />
21-
A list of available documentation resources.
20+
* [Using .proto files](#using-proto-files)
21+
* [Using JSON descriptors](#using-json-descriptors)
22+
* [Using reflection only](#using-reflection-only)
23+
* [Using custom classes](#using-custom-classes)
24+
* [Using services](#using-services)
25+
* [Usage with TypeScript](#usage-with-typescript)
2226

2327
* [Command line](#command-line)<br />
2428
How to use the command line utility.
2529

30+
* [pbjs for JavaScript](#pbjs-for-javascript)
31+
* [pbts for TypeScript](#pbts-for-typescript)
32+
* [Reflection vs. static code](#reflection-vs-static-code)
33+
* [Command line API](#command-line-api)
34+
35+
* [Additional documentation](#additional-documentation)<br />
36+
A list of available documentation resources.
37+
2638
* [Performance](#performance)<br />
2739
A few internals and a benchmark on performance.
2840

@@ -329,7 +341,7 @@ var root = new Root().define("awesomepackage").add(AwesomeMessage);
329341
...
330342
```
331343

332-
Detailed information on the reflection structure is available within the [documentation](#documentation).
344+
Detailed information on the reflection structure is available within the [API documentation](#additional-documentation).
333345

334346
### Using custom classes
335347

@@ -465,7 +477,7 @@ protobuf.load("awesome.proto", function(err, root) {
465477
});
466478
```
467479

468-
**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.
469481

470482
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:
471483

@@ -486,25 +498,177 @@ var buffer = AwesomeMessage.encode(message).finish();
486498
/// <reference path="./node_modules/protobufjs/stub-node.d.ts" />
487499
```
488500

489-
Documentation
490-
-------------
501+
Command line
502+
------------
503+
504+
**Note** that moving the CLI to [its own package](./cli) is a work in progress. At the moment, it's still part of the main package.
505+
506+
The command line interface (CLI) can be used to translate between file formats and to generate static code as well as TypeScript definitions.
507+
508+
### pbjs for JavaScript
509+
510+
```
511+
Translates between file formats and generates static code.
512+
513+
-t, --target Specifies the target format. Also accepts a path to require a custom target.
514+
515+
json JSON representation
516+
json-module JSON representation as a module
517+
proto2 Protocol Buffers, Version 2
518+
proto3 Protocol Buffers, Version 3
519+
static Static code without reflection (non-functional on its own)
520+
static-module Static code without reflection as a module
521+
522+
-p, --path Adds a directory to the include path.
523+
524+
-o, --out Saves to a file instead of writing to stdout.
525+
526+
--sparse Exports only those types referenced from a main file (experimental).
527+
528+
Module targets only:
529+
530+
-w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.
531+
532+
default Default wrapper supporting both CommonJS and AMD
533+
commonjs CommonJS wrapper
534+
amd AMD wrapper
535+
es6 ES6 wrapper (implies --es6)
536+
537+
-r, --root Specifies an alternative protobuf.roots name.
538+
539+
-l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:
540+
541+
eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins
542+
543+
--es6 Enables ES6 syntax (const/let instead of var)
544+
545+
Proto sources only:
546+
547+
--keep-case Keeps field casing instead of converting to camel case.
548+
549+
Static targets only:
550+
551+
--no-create Does not generate create functions used for reflection compatibility.
552+
--no-encode Does not generate encode functions.
553+
--no-decode Does not generate decode functions.
554+
--no-verify Does not generate verify functions.
555+
--no-convert Does not generate convert functions like from/toObject
556+
--no-delimited Does not generate delimited encode/decode functions.
557+
--no-beautify Does not beautify generated code.
558+
--no-comments Does not output any JSDoc comments.
559+
560+
--force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.
561+
--force-message Enfores the use of runtime messages instead of plain objects.
562+
563+
usage: pbjs [options] file1.proto file2.json ... (or) other | pbjs [options] -
564+
```
565+
566+
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):
567+
568+
```
569+
$> pbjs -t json file1.proto file2.proto > bundle.json
570+
```
571+
572+
Now, either include this file in your final bundle:
573+
574+
```js
575+
var root = protobuf.Root.fromJSON(require("./bundle.json"));
576+
```
577+
578+
or load it the usual way:
579+
580+
```js
581+
protobuf.load("bundle.json", function(err, root) {
582+
...
583+
});
584+
```
585+
586+
Generated static code, on the other hand, works with just the **minimal** library. For example
587+
588+
```
589+
$> pbjs -t static-module -w commonjs -o compiled.js file1.proto file2.proto
590+
```
591+
592+
will generate static code for definitions within `file1.proto` and `file2.proto` to a CommonJS module `compiled.js`.
593+
594+
**ProTip!** Documenting your .proto files with `/** ... */`-blocks or (trailing) `/// ...` lines translates to generated static code.
595+
596+
597+
### pbts for TypeScript
598+
599+
```
600+
Generates TypeScript definitions from annotated JavaScript files.
601+
602+
-o, --out Saves to a file instead of writing to stdout.
603+
604+
-g, --global Name of the global object in browser environments, if any.
605+
606+
--no-comments Does not output any JSDoc comments.
607+
608+
Internal flags:
609+
610+
-n, --name Wraps everything in a module of the specified name.
611+
612+
-m, --main Whether building the main library without any imports.
613+
614+
usage: pbts [options] file1.js file2.js ... (or) other | pbts [options] -
615+
```
616+
617+
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`:
618+
619+
```
620+
$> pbjs -t static-module -w commonjs -o compiled.js file1.proto file2.proto
621+
$> pbts -o compiled.d.ts compiled.js
622+
```
623+
624+
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:
630+
631+
```
632+
$> pbjs -t json-module -w commonjs -o bundle.js file1.proto file2.proto
633+
$> pbjs -t static-module file1.proto file2.proto | pbts -o bundle.d.ts -
634+
```
635+
636+
### Reflection vs. static code
637+
638+
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
650+
651+
pbjs.main([ "--target", "json-module", "path/to/myproto.proto" ], function(err, output) {
652+
if (err)
653+
throw err;
654+
// do something with output
655+
});
656+
```
657+
658+
Additional documentation
659+
------------------------
491660

492661
#### Protocol Buffers
493662
* [Google's Developer Guide](https://developers.google.com/protocol-buffers/docs/overview)
494663

495664
#### protobuf.js
496665
* [API Documentation](http://dcode.io/protobuf.js)
497-
* [CLI Documentation](./cli/README.md)
498666
* [CHANGELOG](https://github.com/dcodeIO/protobuf.js/blob/master/CHANGELOG.md)
499667
* [Frequently asked questions](https://github.com/dcodeIO/protobuf.js/wiki) on our wiki
500668

501669
#### Community
502670
* [Questions and answers](http://stackoverflow.com/search?tab=newest&q=protobuf.js) on StackOverflow
503671

504-
Command line
505-
------------
506-
Command line usage has moved to the (soon to be decoupled) [CLI package](./cli/README.md)
507-
508672
Performance
509673
-----------
510674
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

Comments
 (0)