Skip to content

Commit

Permalink
New: Relieved the requirement to call .resolveAll() on roots in order…
Browse files Browse the repository at this point in the history
… to populate static code-compatible properties, see #653
  • Loading branch information
dcodeIO committed Jan 20, 2017
1 parent 33da148 commit b4f4f48
Show file tree
Hide file tree
Showing 22 changed files with 197 additions and 196 deletions.
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<h1><p align="center"><img alt="protobuf.js" src="https://github.com/dcodeIO/protobuf.js/raw/master/pbjs.png" width="120" height="104" /></p></h1>
<p align="center">
<a href="https://travis-ci.org/dcodeIO/protobuf.js"><img src="https://travis-ci.org/dcodeIO/protobuf.js.svg?branch=master" alt=""></a>
<a href="https://coveralls.io/github/dcodeIO/protobuf.js"><img src="https://coveralls.io/repos/github/dcodeIO/protobuf.js/badge.svg?branch=master" alt=""></a>
<a href="https://npmjs.org/package/protobufjs"><img src="https://img.shields.io/npm/v/protobufjs.svg" alt=""></a>
<a href="https://npmjs.org/package/protobufjs"><img src="https://img.shields.io/npm/dm/protobufjs.svg" alt=""></a>
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=dcode%40dcode.io&item_name=Open%20Source%20Software%20Donation&item_number=dcodeIO%2Fprotobuf.js"><img alt="donate ❤" src="https://img.shields.io/badge/donate-❤-ff2244.svg"></a>
</p>
<p align="center"><a href="https://travis-ci.org/dcodeIO/protobuf.js"><img src="https://travis-ci.org/dcodeIO/protobuf.js.svg?branch=master" alt=""></a> <a href="https://coveralls.io/github/dcodeIO/protobuf.js"><img src="https://coveralls.io/repos/github/dcodeIO/protobuf.js/badge.svg?branch=master" alt=""></a> <a href="https://npmjs.org/package/protobufjs"><img src="https://img.shields.io/npm/v/protobufjs.svg" alt=""></a> <a href="https://npmjs.org/package/protobufjs"><img src="https://img.shields.io/npm/dm/protobufjs.svg" alt=""></a> <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=dcode%40dcode.io&item_name=Open%20Source%20Software%20Donation&item_number=dcodeIO%2Fprotobuf.js"><img alt="donate ❤" src="https://img.shields.io/badge/donate-❤-ff2244.svg"></a></p>

**Protocol Buffers** are a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more, originally designed at Google ([see](https://developers.google.com/protocol-buffers/)).

Expand Down Expand Up @@ -349,9 +343,8 @@ $> pbts -o compiled.d.ts compiled.js

Additionally, TypeScript definitions of static modules are compatible with reflection, as long as the following conditions are met:

1. Use `SomeMessage.create(...)` instead of `new SomeMessage(...)` because reflection objects do not provide a constructor.
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")`).
3. When using a TypeScript definition with custom code, `resolveAll()` must be called once on the root instance to populate these additional properties (JSON modules do this automatically).

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

Expand Down
15 changes: 15 additions & 0 deletions bench/fromJSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

var newSuite = require("./suite");

var protobuf = require("..");

var Root = protobuf.Root;
var root = protobuf.loadSync("tests/data/test.proto");
var json = JSON.stringify(root);

newSuite("fromJSON")
.add("Root.fromJSON", function() {
Root.fromJSON(json);
})
.run();
8 changes: 7 additions & 1 deletion bench/prof.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var fs = require("fs"),

// A profiling stub to measure encoding / decoding performance using benchmark data.

var commands = ["encode", "decode", "encode-browser", "decode-browser"];
var commands = ["encode", "decode", "encode-browser", "decode-browser", "fromjson"];
if (commands.indexOf(process.argv[2]) < 0) { // 0: node, 1: prof.js
process.stderr.write("usage: " + path.basename(process.argv[1]) + " <" + commands.join("|") + "> [iterations=10000000]\n");
return;
Expand Down Expand Up @@ -48,6 +48,7 @@ var root, Test, data, count;
if (process.argv.indexOf("--alt") < 0) {
root = protobuf.parse(fs.readFileSync(require.resolve("../bench/bench.proto")).toString("utf8")).root;
Test = root.lookup("Test");
json = JSON.stringify(root);
data = require("../bench/bench.json");
count = 10000000;
process.stdout.write("bench.proto");
Expand All @@ -58,6 +59,7 @@ if (process.argv.indexOf("--alt") < 0) {
count = 1000;
process.stdout.write("vector_tile.proto");
}
var json = JSON.stringify(root);

if (process.argv.length > 3 && /^\d+$/.test(process.argv[3]))
count = parseInt(process.argv[3], 10);
Expand All @@ -84,4 +86,8 @@ switch (process.argv[2]) {
for (var j = 0; j < count; ++j)
Test.decode(buf);
break;
case "fromjson":
for (var k = 0; k < 100000000; ++k)
protobuf.Root.fromJSON(json).resolveAll();
break;
}
2 changes: 1 addition & 1 deletion cli/targets/json-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ json_module.description = "JSON representation as a module"

function json_module(root, options, callback) {
try {
var output = "var $root = protobuf.Root.fromJSON(" + JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim() + ").resolveAll();";
var output = "var $root = protobuf.Root.fromJSON(" + JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim() + ");";
output = util.wrap(output, options);
process.nextTick(function() {
callback(null, output);
Expand Down
75 changes: 25 additions & 50 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.

2 changes: 1 addition & 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.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/minimal/protobuf.min.js.gz
Binary file not shown.
75 changes: 25 additions & 50 deletions dist/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/protobuf.js.map

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

Loading

0 comments on commit b4f4f48

Please sign in to comment.