Skip to content

Commit

Permalink
Other: Cache any regexp instance (perf); Docs: Documented throwing be…
Browse files Browse the repository at this point in the history
…havior of Reader.create and Message.decode
  • Loading branch information
dcodeIO committed Mar 5, 2017
1 parent 44a8d3a commit e7e123a
Show file tree
Hide file tree
Showing 35 changed files with 509 additions and 159 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ message AwesomeMessage {

```js
protobuf.load("awesome.proto", function(err, root) {
if (err) throw err;
if (err)
throw err;

// Obtain a message type
var AwesomeMessage = root.lookup("awesomepackage.AwesomeMessage");
Expand All @@ -112,9 +113,9 @@ protobuf.load("awesome.proto", function(err, root) {
var message = AwesomeMessage.create({ awesomeField: "AwesomeString" });

// Verify the message if necessary (i.e. when possibly incomplete or invalid)
var err = AwesomeMessage.verify(message);
if (err)
throw Error(err);
var errMsg = AwesomeMessage.verify(message);
if (errMsg)
throw Error(errMsg);

// Encode a message to an Uint8Array (browser) or Buffer (node)
var buffer = AwesomeMessage.encode(message).finish();
Expand All @@ -132,7 +133,7 @@ protobuf.load("awesome.proto", function(err, root) {
});
```

**Note** that `Message.encode` does not verify a message but tries to encode whatever is specified, which might result in a runtime error being thrown somewhere down the road. Instead, there is `Message.verify` to explicitly perform verification priorly (only) where necessary to avoid redundant assertions where messages are already known to be valid. `Message.decode` throws if a buffer is invalid.
**Note** that `Message.encode` does not implicitly verify a message but tries to encode whatever is specified, which might result in a runtime error being thrown somewhere down the road. Instead, there is `Message.verify` to explicitly perform verification priorly where necessary to avoid redundant assertions where messages are already known to be valid. `Message.decode` throws if a buffer is invalid.

Additionally, promise syntax can be used by omitting the callback, if preferred:

Expand Down
6 changes: 4 additions & 2 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,8 @@ function buildType(ref, type) {
"Decodes " + aOrAn(type.name) + " message from the specified reader or buffer.",
"@param {$protobuf.Reader|Uint8Array} " + (config.beautify ? "reader" : "r") + " Reader or buffer to decode from",
"@param {number} [" + (config.beautify ? "length" : "l") + "] Message length if known beforehand",
"@returns {" + fullName + "} " + type.name
"@returns {" + fullName + "} " + type.name,
"@throws {Error} If the payload is not a reader or valid buffer or required fields are missing"
]);
buildFunction(type, "decode", protobuf.decoder(type));

Expand All @@ -433,7 +434,8 @@ function buildType(ref, type) {
pushComment([
"Decodes " + aOrAn(type.name) + " message from the specified reader or buffer, length delimited.",
"@param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from",
"@returns {" + fullName + "} " + type.name
"@returns {" + fullName + "} " + type.name,
"@throws {Error} If the payload is not a reader or valid buffer or required fields are missing"
]);
push(name(type.name) + ".decodeDelimited = function decodeDelimited(reader) {");
++indent;
Expand Down
62 changes: 52 additions & 10 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.

6 changes: 3 additions & 3 deletions dist/light/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/light/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/light/protobuf.min.js.map

Large diffs are not rendered by default.

22 changes: 21 additions & 1 deletion dist/minimal/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/minimal/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/minimal/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/minimal/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/minimal/protobuf.min.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit e7e123a

Please sign in to comment.