protobufjs 6.11.3 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +7 -174
  2. package/dist/light/protobuf.js +40 -16
  3. package/dist/light/protobuf.js.map +1 -1
  4. package/dist/light/protobuf.min.js +3 -3
  5. package/dist/light/protobuf.min.js.map +1 -1
  6. package/dist/minimal/protobuf.js +27 -10
  7. package/dist/minimal/protobuf.js.map +1 -1
  8. package/dist/minimal/protobuf.min.js +3 -3
  9. package/dist/minimal/protobuf.min.js.map +1 -1
  10. package/dist/protobuf.js +109 -46
  11. package/dist/protobuf.js.map +1 -1
  12. package/dist/protobuf.min.js +3 -3
  13. package/dist/protobuf.min.js.map +1 -1
  14. package/index.d.ts +1 -4
  15. package/package.json +9 -28
  16. package/scripts/postinstall.js +0 -3
  17. package/src/converter.js +1 -1
  18. package/src/decoder.js +4 -3
  19. package/src/field.js +6 -3
  20. package/src/namespace.js +1 -2
  21. package/src/parse.js +38 -14
  22. package/src/roots.js +1 -1
  23. package/src/tokenize.js +31 -16
  24. package/src/util/minimal.js +24 -7
  25. package/src/wrappers.js +2 -2
  26. package/CHANGELOG.md +0 -1027
  27. package/bin/pbjs +0 -6
  28. package/bin/pbts +0 -6
  29. package/cli/LICENSE +0 -33
  30. package/cli/README.md +0 -174
  31. package/cli/bin/pbjs +0 -6
  32. package/cli/bin/pbts +0 -6
  33. package/cli/index.d.ts +0 -3
  34. package/cli/index.js +0 -3
  35. package/cli/lib/tsd-jsdoc/LICENSE +0 -21
  36. package/cli/lib/tsd-jsdoc/README.md +0 -23
  37. package/cli/lib/tsd-jsdoc/plugin.js +0 -21
  38. package/cli/lib/tsd-jsdoc/publish.js +0 -705
  39. package/cli/lib/tsd-jsdoc.json +0 -18
  40. package/cli/package.json +0 -8
  41. package/cli/package.standalone.json +0 -32
  42. package/cli/pbjs.d.ts +0 -9
  43. package/cli/pbjs.js +0 -330
  44. package/cli/pbts.d.ts +0 -9
  45. package/cli/pbts.js +0 -197
  46. package/cli/targets/json-module.js +0 -38
  47. package/cli/targets/json.js +0 -8
  48. package/cli/targets/proto.js +0 -326
  49. package/cli/targets/proto2.js +0 -10
  50. package/cli/targets/proto3.js +0 -10
  51. package/cli/targets/static-module.js +0 -29
  52. package/cli/targets/static.js +0 -711
  53. package/cli/util.js +0 -183
  54. package/cli/wrappers/amd.js +0 -7
  55. package/cli/wrappers/closure.js +0 -7
  56. package/cli/wrappers/commonjs.js +0 -7
  57. package/cli/wrappers/default.js +0 -15
  58. package/cli/wrappers/es6.js +0 -5
  59. package/package-lock.json +0 -7870
  60. package/scripts/changelog.js +0 -150
package/README.md CHANGED
@@ -27,14 +27,6 @@ Contents
27
27
  * [Using services](#using-services)
28
28
  * [Usage with TypeScript](#usage-with-typescript)<br />
29
29
 
30
- * [Command line](#command-line)<br />
31
- How to use the command line utility.
32
-
33
- * [pbjs for JavaScript](#pbjs-for-javascript)
34
- * [pbts for TypeScript](#pbts-for-typescript)
35
- * [Reflection vs. static code](#reflection-vs-static-code)
36
- * [Command line API](#command-line-api)<br />
37
-
38
30
  * [Additional documentation](#additional-documentation)<br />
39
31
  A list of available documentation resources.
40
32
 
@@ -60,6 +52,12 @@ $> npm install protobufjs [--save --save-prefix=~]
60
52
  var protobuf = require("protobufjs");
61
53
  ```
62
54
 
55
+ The command line utility lives in the protobufjs-cli package and must be installed separately:
56
+
57
+ ```
58
+ $> npm install protobufjs-cli [--save --save-prefix=~]
59
+ ```
60
+
63
61
  **Note** that this library's versioning scheme is not semver-compatible for historical reasons. For guaranteed backward compatibility, always depend on `~6.A.B` instead of `^6.A.B` (hence the `--save-prefix` above).
64
62
 
65
63
  ### Browsers
@@ -84,7 +82,7 @@ The library supports CommonJS and AMD loaders and also exports globally as `prot
84
82
 
85
83
  Where bundle size is a factor, there are additional stripped-down versions of the [full library][dist-full] (~19kb gzipped) available that exclude certain functionality:
86
84
 
87
- * When working with JSON descriptors (i.e. generated by [pbjs](#pbjs-for-javascript)) and/or reflection only, see the [light library][dist-light] (~16kb gzipped) that excludes the parser. CommonJS entry point is:
85
+ * When working with JSON descriptors (i.e. generated by [pbjs](cli/README.md#pbjs-for-javascript)) and/or reflection only, see the [light library][dist-light] (~16kb gzipped) that excludes the parser. CommonJS entry point is:
88
86
 
89
87
  ```js
90
88
  var protobuf = require("protobufjs/light");
@@ -597,171 +595,6 @@ Other notes:
597
595
 
598
596
  **ProTip!** Not as pretty, but you can [use decorators in plain JavaScript](https://github.com/dcodeIO/protobuf.js/blob/master/examples/js-decorators.js) as well.
599
597
 
600
- Command line
601
- ------------
602
-
603
- **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.
604
-
605
- The command line interface (CLI) can be used to translate between file formats and to generate static code as well as TypeScript definitions.
606
-
607
- ### pbjs for JavaScript
608
-
609
- ```
610
- Translates between file formats and generates static code.
611
-
612
- -t, --target Specifies the target format. Also accepts a path to require a custom target.
613
-
614
- json JSON representation
615
- json-module JSON representation as a module
616
- proto2 Protocol Buffers, Version 2
617
- proto3 Protocol Buffers, Version 3
618
- static Static code without reflection (non-functional on its own)
619
- static-module Static code without reflection as a module
620
-
621
- -p, --path Adds a directory to the include path.
622
-
623
- -o, --out Saves to a file instead of writing to stdout.
624
-
625
- --sparse Exports only those types referenced from a main file (experimental).
626
-
627
- Module targets only:
628
-
629
- -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.
630
-
631
- default Default wrapper supporting both CommonJS and AMD
632
- commonjs CommonJS wrapper
633
- amd AMD wrapper
634
- es6 ES6 wrapper (implies --es6)
635
- closure A closure adding to protobuf.roots where protobuf is a global
636
-
637
- -r, --root Specifies an alternative protobuf.roots name.
638
-
639
- -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:
640
-
641
- eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins
642
-
643
- --es6 Enables ES6 syntax (const/let instead of var)
644
-
645
- Proto sources only:
646
-
647
- --keep-case Keeps field casing instead of converting to camel case.
648
-
649
- Static targets only:
650
-
651
- --no-create Does not generate create functions used for reflection compatibility.
652
- --no-encode Does not generate encode functions.
653
- --no-decode Does not generate decode functions.
654
- --no-verify Does not generate verify functions.
655
- --no-convert Does not generate convert functions like from/toObject
656
- --no-delimited Does not generate delimited encode/decode functions.
657
- --no-beautify Does not beautify generated code.
658
- --no-comments Does not output any JSDoc comments.
659
-
660
- --force-long Enforces the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.
661
- --force-number Enforces the use of 'number' for s-/u-/int64 and s-/fixed64 fields.
662
- --force-message Enforces the use of message instances instead of plain objects.
663
-
664
- usage: pbjs [options] file1.proto file2.json ... (or pipe) other | pbjs [options] -
665
- ```
666
-
667
- 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):
668
-
669
- ```
670
- $> pbjs -t json file1.proto file2.proto > bundle.json
671
- ```
672
-
673
- Now, either include this file in your final bundle:
674
-
675
- ```js
676
- var root = protobuf.Root.fromJSON(require("./bundle.json"));
677
- ```
678
-
679
- or load it the usual way:
680
-
681
- ```js
682
- protobuf.load("bundle.json", function(err, root) {
683
- ...
684
- });
685
- ```
686
-
687
- Generated static code, on the other hand, works with just the **minimal** library. For example
688
-
689
- ```
690
- $> pbjs -t static-module -w commonjs -o compiled.js file1.proto file2.proto
691
- ```
692
-
693
- will generate static code for definitions within `file1.proto` and `file2.proto` to a CommonJS module `compiled.js`.
694
-
695
- **ProTip!** Documenting your .proto files with `/** ... */`-blocks or (trailing) `/// ...` lines translates to generated static code.
696
-
697
-
698
- ### pbts for TypeScript
699
-
700
- ```
701
- Generates TypeScript definitions from annotated JavaScript files.
702
-
703
- -o, --out Saves to a file instead of writing to stdout.
704
-
705
- -g, --global Name of the global object in browser environments, if any.
706
-
707
- --no-comments Does not output any JSDoc comments.
708
-
709
- Internal flags:
710
-
711
- -n, --name Wraps everything in a module of the specified name.
712
-
713
- -m, --main Whether building the main library without any imports.
714
-
715
- usage: pbts [options] file1.js file2.js ... (or) other | pbts [options] -
716
- ```
717
-
718
- 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`:
719
-
720
- ```
721
- $> pbjs -t static-module -w commonjs -o compiled.js file1.proto file2.proto
722
- $> pbts -o compiled.d.ts compiled.js
723
- ```
724
-
725
- 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:
726
-
727
- 1. Instead of using `new SomeMessage(...)`, always use `SomeMessage.create(...)` because reflection objects do not provide a constructor.
728
- 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")`).
729
-
730
- For example, the following generates a JSON module `bundle.js` and a `bundle.d.ts`, but no static code:
731
-
732
- ```
733
- $> pbjs -t json-module -w commonjs -o bundle.js file1.proto file2.proto
734
- $> pbjs -t static-module file1.proto file2.proto | pbts -o bundle.d.ts -
735
- ```
736
-
737
- ### Reflection vs. static code
738
-
739
- 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.
740
-
741
- 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.
742
-
743
- 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.
744
-
745
- | Source | Library | Advantages | Tradeoffs
746
- |--------|---------|------------|-----------
747
- | .proto | full | Easily editable<br />Interoperability with other libraries<br />No compile step | Some parsing and possibly network overhead
748
- | JSON | light | Easily editable<br />No parsing overhead<br />Single bundle (no network overhead) | protobuf.js specific<br />Has a compile step
749
- | 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
750
-
751
- ### Command line API
752
-
753
- Both utilities can be used programmatically by providing command line arguments and a callback to their respective `main` functions:
754
-
755
- ```js
756
- var pbjs = require("protobufjs/cli/pbjs"); // or require("protobufjs/cli").pbjs / .pbts
757
-
758
- pbjs.main([ "--target", "json-module", "path/to/myproto.proto" ], function(err, output) {
759
- if (err)
760
- throw err;
761
- // do something with output
762
- });
763
- ```
764
-
765
598
  Additional documentation
766
599
  ------------------------
767
600
 
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * protobuf.js v6.11.0 (c) 2016, daniel wirtz
3
- * compiled thu, 29 apr 2021 02:20:44 utc
2
+ * protobuf.js v6.10.2 (c) 2016, daniel wirtz
3
+ * compiled fri, 08 jul 2022 16:17:13 utc
4
4
  * licensed under the bsd-3-clause license
5
5
  * see: https://github.com/dcodeio/protobuf.js for details
6
6
  */
@@ -1182,7 +1182,7 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
1182
1182
  case "bytes": gen
1183
1183
  ("if(typeof d%s===\"string\")", prop)
1184
1184
  ("util.base64.decode(d%s,m%s=util.newBuffer(util.base64.length(d%s)),0)", prop, prop, prop)
1185
- ("else if(d%s.length)", prop)
1185
+ ("else if(d%s.length >= 0)", prop)
1186
1186
  ("m%s=d%s", prop, prop);
1187
1187
  break;
1188
1188
  case "string": gen
@@ -1439,7 +1439,7 @@ function decoder(mtype) {
1439
1439
  var field = mtype._fieldsArray[i].resolve(),
1440
1440
  type = field.resolvedType instanceof Enum ? "int32" : field.type,
1441
1441
  ref = "m" + util.safeProp(field.name); gen
1442
- ("case %i:", field.id);
1442
+ ("case %i: {", field.id);
1443
1443
 
1444
1444
  // Map fields
1445
1445
  if (field.map) { gen
@@ -1510,8 +1510,9 @@ function decoder(mtype) {
1510
1510
  else gen
1511
1511
  ("%s=r.%s()", ref, type);
1512
1512
  gen
1513
- ("break");
1514
- // Unknown fields
1513
+ ("break")
1514
+ ("}");
1515
+ // Unknown fields
1515
1516
  } gen
1516
1517
  ("default:")
1517
1518
  ("r.skipType(t&7)")
@@ -2091,6 +2092,9 @@ Field.prototype.resolve = function resolve() {
2091
2092
  this.typeDefault = null;
2092
2093
  else // instanceof Enum
2093
2094
  this.typeDefault = this.resolvedType.values[Object.keys(this.resolvedType.values)[0]]; // first defined
2095
+ } else if (this.options && this.options.proto3_optional) {
2096
+ // proto3 scalar value marked optional; should default to null
2097
+ this.typeDefault = null;
2094
2098
  }
2095
2099
 
2096
2100
  // use explicitly set default value if present
@@ -4491,7 +4495,7 @@ module.exports = {};
4491
4495
  /**
4492
4496
  * Named roots.
4493
4497
  * This is where pbjs stores generated structures (the option `-r, --root` specifies a name).
4494
- * Can also be used manually to make roots available accross modules.
4498
+ * Can also be used manually to make roots available across modules.
4495
4499
  * @name roots
4496
4500
  * @type {Object.<string,Root>}
4497
4501
  * @example
@@ -5823,6 +5827,9 @@ util.decorateEnum = function decorateEnum(object) {
5823
5827
  util.setProperty = function setProperty(dst, path, value) {
5824
5828
  function setProp(dst, path, value) {
5825
5829
  var part = path.shift();
5830
+ if (part === "__proto__") {
5831
+ return dst;
5832
+ }
5826
5833
  if (path.length > 0) {
5827
5834
  dst[part] = setProp(dst[part] || {}, path, value);
5828
5835
  } else {
@@ -6340,13 +6347,30 @@ function newError(name) {
6340
6347
  merge(this, properties);
6341
6348
  }
6342
6349
 
6343
- (CustomError.prototype = Object.create(Error.prototype)).constructor = CustomError;
6344
-
6345
- Object.defineProperty(CustomError.prototype, "name", { get: function() { return name; } });
6346
-
6347
- CustomError.prototype.toString = function toString() {
6348
- return this.name + ": " + this.message;
6349
- };
6350
+ CustomError.prototype = Object.create(Error.prototype, {
6351
+ constructor: {
6352
+ value: CustomError,
6353
+ writable: true,
6354
+ enumerable: false,
6355
+ configurable: true,
6356
+ },
6357
+ name: {
6358
+ get() { return name; },
6359
+ set: undefined,
6360
+ enumerable: false,
6361
+ // configurable: false would accurately preserve the behavior of
6362
+ // the original, but I'm guessing that was not intentional.
6363
+ // For an actual error subclass, this property would
6364
+ // be configurable.
6365
+ configurable: true,
6366
+ },
6367
+ toString: {
6368
+ value() { return this.name + ": " + this.message; },
6369
+ writable: true,
6370
+ enumerable: false,
6371
+ configurable: true,
6372
+ },
6373
+ });
6350
6374
 
6351
6375
  return CustomError;
6352
6376
  }
@@ -6710,7 +6734,7 @@ wrappers[".google.protobuf.Any"] = {
6710
6734
  if (type) {
6711
6735
  // type_url does not accept leading "."
6712
6736
  var type_url = object["@type"].charAt(0) === "." ?
6713
- object["@type"].substr(1) : object["@type"];
6737
+ object["@type"].slice(1) : object["@type"];
6714
6738
  // type_url prefix is optional, but path seperator is required
6715
6739
  if (type_url.indexOf("/") === -1) {
6716
6740
  type_url = "/" + type_url;
@@ -6748,7 +6772,7 @@ wrappers[".google.protobuf.Any"] = {
6748
6772
  if (!(message instanceof this.ctor) && message instanceof Message) {
6749
6773
  var object = message.$type.toObject(message, options);
6750
6774
  var messageName = message.$type.fullName[0] === "." ?
6751
- message.$type.fullName.substr(1) : message.$type.fullName;
6775
+ message.$type.fullName.slice(1) : message.$type.fullName;
6752
6776
  // Default to type.googleapis.com prefix if no prefix is used
6753
6777
  if (prefix === "") {
6754
6778
  prefix = googleApi;