Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support parsing of complex options #1744

Merged
merged 28 commits into from Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0f88cb1
feat: proto3 optional support
alexander-fenster Apr 9, 2021
2d48c59
chore: pre-release v6.11.0-pre
alexander-fenster Apr 9, 2021
c39195c
fix: rebuild
alexander-fenster Apr 9, 2021
e87b9d3
fix: fromObject should not initialize oneof members (#1597)
alexander-fenster Apr 29, 2021
703d0f6
chore: release v6.11.0
alexander-fenster Apr 29, 2021
9201173
chore: rebuild
alexander-fenster Apr 29, 2021
b37b296
feat: add --no-service option for pbjs static target (#1577)
mdouglass Apr 14, 2021
2de8d50
deps: set @types/node to >= (#1575)
indutny Apr 14, 2021
18fb310
chore: rebuild
alexander-fenster Apr 29, 2021
49b19fb
docs: update changelog
alexander-fenster Apr 29, 2021
ecd6a48
fix: parse.js "parent.add(oneof)“ error (#1602)
leon776 Apr 29, 2021
a2ccda1
chore: release v6.11.1
alexander-fenster Apr 29, 2021
da9fdd2
fix(types): bring back Field.rule to .d.ts
alexander-fenster Apr 30, 2021
d127871
fix: rebuild type, release v6.11.2
alexander-fenster Apr 30, 2021
37285d0
build: configure backports
bcoe May 20, 2022
7afd0a3
build: configure 6.x as default branch
bcoe May 20, 2022
b5f1391
fix: do not let setProperty change the prototype (#1731)
alexander-fenster May 20, 2022
a8681ce
fix(deps): use eslint 8.x (#1728)
alexander-fenster May 19, 2022
b2c6a5c
build: run tests if ci label added (#1734)
bcoe May 20, 2022
c2c17ae
build: publish to main
bcoe May 20, 2022
b130dfd
chore(6.x): release 6.11.3 (#1737)
github-actions[bot] May 20, 2022
26d0135
Support parsing of complex options
NikhilVerma Jun 14, 2022
7ef4203
Use readValue to read the proto value and add better example
NikhilVerma Jun 14, 2022
7186394
Fix lint issues
NikhilVerma Jun 18, 2022
20cab5b
Merge branch 'master' into comp_options
alexander-fenster Jul 7, 2022
06a993d
fix: rollback files
alexander-fenster Jul 7, 2022
108fa38
Merge branch 'master' into comp_options
alexander-fenster Jul 7, 2022
bda81ee
Re-do parse logic to take arrays into account and make it simpler
NikhilVerma Jul 7, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/release.yaml
@@ -0,0 +1,43 @@
on:
push:
branches:
- 6.x
name: release-please
jobs:
release-please-pr:
runs-on: ubuntu-latest
steps:
- id: release-pr
uses: google-github-actions/release-please-action@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
release-type: node
package-name: protobufjs
command: release-pr
default-branch: 6.x
release-please-release:
runs-on: ubuntu-latest
steps:
- uses: google-github-actions/release-please-action@v3
id: tag-release
with:
token: ${{ secrets.GITHUB_TOKEN }}
release-type: node
package-name: protobufjs
command: github-release
default-branch: 6.x
- uses: actions/checkout@v2
if: ${{ steps.tag-release.outputs.release_created }}
- uses: actions/setup-node@v1
if: ${{ steps.tag-release.outputs.release_created }}
with:
node-version: 14
registry-url: 'https://wombat-dressing-room.appspot.com/'
- name: publish
if: ${{ steps.tag-release.outputs.release_created }}
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
run: |
npm install
npm publish

1 change: 1 addition & 0 deletions .github/workflows/test.yml
Expand Up @@ -4,6 +4,7 @@ on:
branches:
- master
pull_request:
types: [assigned, opened, synchronize, reopened, labeled]
jobs:
lint:
runs-on: ubuntu-latest
Expand Down
36 changes: 36 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,41 @@
# Changelog

### [6.11.3](https://github.com/protobufjs/protobuf.js/compare/v6.11.2...v6.11.3) (2022-05-20)


### Bug Fixes

* **deps:** use eslint 8.x ([#1728](https://github.com/protobufjs/protobuf.js/issues/1728)) ([a8681ce](https://github.com/protobufjs/protobuf.js/commit/a8681ceab4763e706a848121a2dde56791b89eea))
* do not let setProperty change the prototype ([#1731](https://github.com/protobufjs/protobuf.js/issues/1731)) ([b5f1391](https://github.com/protobufjs/protobuf.js/commit/b5f1391dff5515894830a6570e6d73f5511b2e8f))

### [6.11.2](https://www.github.com/protobufjs/protobuf.js/compare/v6.11.1...v6.11.2) (2021-04-30)

* regenerated index.d.ts to fix the unintended breaking change in types.


### [6.11.1](https://www.github.com/protobufjs/protobuf.js/compare/v6.11.0...v6.11.1) (2021-04-29)


### Bug Fixes

* parse.js "parent.add(oneof)“ error (@leon776) ([#1602](https://github.com/protobufjs/protobuf.js/pull/1602))

## [6.11.0](https://www.github.com/protobufjs/protobuf.js/compare/v6.10.2...v6.11.0) (2021-04-28)


### Features

* support for proto3 `optional` fields (@alexander-fenster) ([#1584](https://github.com/protobufjs/protobuf.js/pull/1584))
* add `--no-service` option for `pbjs` (@mdouglass) ([#1577](https://github.com/protobufjs/protobuf.js/pull/1577))

### Bug Fixes

* do not assign `oneof` members to default values, use `null` instead (@alexander-fenster) ([#1597](https://github.com/protobufjs/protobuf.js/pull/1597))

### Dependencies

* set `@types/node` to `>= 13.7.0` in dependencies (@indutny) ([#1575](https://github.com/protobufjs/protobuf.js/pull/1575))

### [6.10.2](https://www.github.com/protobufjs/protobuf.js/compare/v6.10.1...v6.10.2) (2020-11-13)


Expand Down
163 changes: 163 additions & 0 deletions cli/README.md
Expand Up @@ -7,5 +7,168 @@ Command line interface (CLI) for [protobuf.js](https://github.com/dcodeIO/protob
* [CLI Documentation](https://github.com/dcodeIO/protobuf.js#command-line)

**Note** that moving the CLI to its own package is a work in progress. At the moment, it's still part of the main package.
* [pbjs for JavaScript](#pbjs-for-javascript)
* [pbts for TypeScript](#pbts-for-typescript)
* [Reflection vs. static code](#reflection-vs-static-code)
* [Command line API](#command-line-api)<br />

### pbjs for JavaScript

```
Translates between file formats and generates static code.

-t, --target Specifies the target format. Also accepts a path to require a custom target.

json JSON representation
json-module JSON representation as a module
proto2 Protocol Buffers, Version 2
proto3 Protocol Buffers, Version 3
static Static code without reflection (non-functional on its own)
static-module Static code without reflection as a module

-p, --path Adds a directory to the include path.

-o, --out Saves to a file instead of writing to stdout.

--sparse Exports only those types referenced from a main file (experimental).

Module targets only:

-w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.

default Default wrapper supporting both CommonJS and AMD
commonjs CommonJS wrapper
amd AMD wrapper
es6 ES6 wrapper (implies --es6)
closure A closure adding to protobuf.roots where protobuf is a global

-r, --root Specifies an alternative protobuf.roots name.

-l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:

eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins

--es6 Enables ES6 syntax (const/let instead of var)

Proto sources only:

--keep-case Keeps field casing instead of converting to camel case.

Static targets only:

--no-create Does not generate create functions used for reflection compatibility.
--no-encode Does not generate encode functions.
--no-decode Does not generate decode functions.
--no-verify Does not generate verify functions.
--no-convert Does not generate convert functions like from/toObject
--no-delimited Does not generate delimited encode/decode functions.
--no-beautify Does not beautify generated code.
--no-comments Does not output any JSDoc comments.
--no-service Does not output service classes.

--force-long Enforces the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.
--force-number Enforces the use of 'number' for s-/u-/int64 and s-/fixed64 fields.
--force-message Enforces the use of message instances instead of plain objects.

usage: pbjs [options] file1.proto file2.json ... (or pipe) other | pbjs [options] -
```

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):

```
$> pbjs -t json file1.proto file2.proto > bundle.json
```

Now, either include this file in your final bundle:

```js
var root = protobuf.Root.fromJSON(require("./bundle.json"));
```

or load it the usual way:

```js
protobuf.load("bundle.json", function(err, root) {
...
});
```

Generated static code, on the other hand, works with just the **minimal** library. For example

```
$> pbjs -t static-module -w commonjs -o compiled.js file1.proto file2.proto
```

will generate static code for definitions within `file1.proto` and `file2.proto` to a CommonJS module `compiled.js`.

**ProTip!** Documenting your .proto files with `/** ... */`-blocks or (trailing) `/// ...` lines translates to generated static code.


### pbts for TypeScript

```
Generates TypeScript definitions from annotated JavaScript files.

-o, --out Saves to a file instead of writing to stdout.

-g, --global Name of the global object in browser environments, if any.

--no-comments Does not output any JSDoc comments.

Internal flags:

-n, --name Wraps everything in a module of the specified name.

-m, --main Whether building the main library without any imports.

usage: pbts [options] file1.js file2.js ... (or) other | pbts [options] -
```

Picking up on the example above, the following not only generates static code to a CommonJS module `compiled.js` but also its respective TypeScript definitions to `compiled.d.ts`:

```
$> pbjs -t static-module -w commonjs -o compiled.js file1.proto file2.proto
$> pbts -o compiled.d.ts compiled.js
```

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:

1. Instead of using `new SomeMessage(...)`, always use `SomeMessage.create(...)` because reflection objects do not provide a constructor.
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")`).

For example, the following generates a JSON module `bundle.js` and a `bundle.d.ts`, but no static code:

```
$> pbjs -t json-module -w commonjs -o bundle.js file1.proto file2.proto
$> pbjs -t static-module file1.proto file2.proto | pbts -o bundle.d.ts -
```

### Reflection vs. 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.

Static code, on the other hand, requires just the minimal library, but generates additional source code without any reflection features. This also implies that there is a break-even point where statically generated code becomes larger than descriptor-based code once the amount of code generated exceeds the size of the full respectively light library.

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.

| Source | Library | Advantages | Tradeoffs
|--------|---------|------------|-----------
| .proto | full | Easily editable<br />Interoperability with other libraries<br />No compile step | Some parsing and possibly network overhead
| JSON | light | Easily editable<br />No parsing overhead<br />Single bundle (no network overhead) | protobuf.js specific<br />Has a compile step
| static | minimal | Works where `eval` access is restricted<br />Fully documented<br />Small footprint for small protos | Can be hard to edit<br />No reflection<br />Has a compile step

### Command line API

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"); // or require("protobufjs-cli").pbjs / .pbts

pbjs.main([ "--target", "json-module", "path/to/myproto.proto" ], function(err, output) {
if (err)
throw err;
// do something with output
});
```

**License:** [BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause)
1 change: 1 addition & 0 deletions cli/package.json
@@ -1,6 +1,7 @@
{
"version": "6.9.0",
"dependencies": {
"escodegen": "^2.0.0",
"espree": "^7.1.0",
"tmp": "^0.2.1"
}
Expand Down
4 changes: 3 additions & 1 deletion cli/pbjs.js
Expand Up @@ -44,7 +44,7 @@ exports.main = function main(args, callback) {
"force-message": "strict-message"
},
string: [ "target", "out", "path", "wrap", "dependency", "root", "lint" ],
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "service", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
default: {
target: "json",
create: true,
Expand All @@ -55,6 +55,7 @@ exports.main = function main(args, callback) {
delimited: true,
beautify: true,
comments: true,
service: true,
es6: null,
lint: lintDefault,
"keep-case": false,
Expand Down Expand Up @@ -135,6 +136,7 @@ exports.main = function main(args, callback) {
" --no-delimited Does not generate delimited encode/decode functions.",
" --no-beautify Does not beautify generated code.",
" --no-comments Does not output any JSDoc comments.",
" --no-service Does not output service classes.",
"",
" --force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.",
" --force-number Enfores the use of 'number' for s-/u-/int64 and s-/fixed64 fields.",
Expand Down
8 changes: 7 additions & 1 deletion cli/targets/static.js
Expand Up @@ -109,6 +109,10 @@ function aOrAn(name) {
function buildNamespace(ref, ns) {
if (!ns)
return;

if (ns instanceof Service && !config.service)
return;

if (ns.name !== "") {
push("");
if (!ref && config.es6)
Expand Down Expand Up @@ -390,7 +394,7 @@ function buildType(ref, type) {
if (config.comments) {
push("");
var jsType = toJsType(field);
if (field.optional && !field.map && !field.repeated && field.resolvedType instanceof Type)
if (field.optional && !field.map && !field.repeated && field.resolvedType instanceof Type || field.partOf)
jsType = jsType + "|null|undefined";
pushComment([
field.comment || type.name + " " + field.name + ".",
Expand All @@ -406,6 +410,8 @@ function buildType(ref, type) {
push(escapeName(type.name) + ".prototype" + prop + " = $util.emptyArray;"); // overwritten in constructor
else if (field.map)
push(escapeName(type.name) + ".prototype" + prop + " = $util.emptyObject;"); // overwritten in constructor
else if (field.partOf)
push(escapeName(type.name) + ".prototype" + prop + " = null;"); // do not set default value for oneof members
else if (field.long)
push(escapeName(type.name) + ".prototype" + prop + " = $util.Long ? $util.Long.fromBits("
+ JSON.stringify(field.typeDefault.low) + ","
Expand Down
22 changes: 17 additions & 5 deletions dist/light/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/light/protobuf.js.map

Large diffs are not rendered by default.