Skip to content

Commit b4f4f48

Browse files
committed
New: Relieved the requirement to call .resolveAll() on roots in order to populate static code-compatible properties, see #653
1 parent 33da148 commit b4f4f48

22 files changed

+197
-196
lines changed

README.md

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
<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>
2-
<p align="center">
3-
<a href="https://travis-ci.org/dcodeIO/protobuf.js"><img src="https://travis-ci.org/dcodeIO/protobuf.js.svg?branch=master" alt=""></a>
4-
<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>
5-
<a href="https://npmjs.org/package/protobufjs"><img src="https://img.shields.io/npm/v/protobufjs.svg" alt=""></a>
6-
<a href="https://npmjs.org/package/protobufjs"><img src="https://img.shields.io/npm/dm/protobufjs.svg" alt=""></a>
7-
<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>
8-
</p>
2+
<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>
93

104
**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/)).
115

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

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

352-
1. Use `SomeMessage.create(...)` instead of `new SomeMessage(...)` because reflection objects do not provide a constructor.
346+
1. Instead of using `new SomeMessage(...)`, always use `SomeMessage.create(...)` because reflection objects do not provide a constructor.
353347
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")`).
354-
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).
355348

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

bench/fromJSON.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
3+
var newSuite = require("./suite");
4+
5+
var protobuf = require("..");
6+
7+
var Root = protobuf.Root;
8+
var root = protobuf.loadSync("tests/data/test.proto");
9+
var json = JSON.stringify(root);
10+
11+
newSuite("fromJSON")
12+
.add("Root.fromJSON", function() {
13+
Root.fromJSON(json);
14+
})
15+
.run();

bench/prof.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var fs = require("fs"),
55

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

8-
var commands = ["encode", "decode", "encode-browser", "decode-browser"];
8+
var commands = ["encode", "decode", "encode-browser", "decode-browser", "fromjson"];
99
if (commands.indexOf(process.argv[2]) < 0) { // 0: node, 1: prof.js
1010
process.stderr.write("usage: " + path.basename(process.argv[1]) + " <" + commands.join("|") + "> [iterations=10000000]\n");
1111
return;
@@ -48,6 +48,7 @@ var root, Test, data, count;
4848
if (process.argv.indexOf("--alt") < 0) {
4949
root = protobuf.parse(fs.readFileSync(require.resolve("../bench/bench.proto")).toString("utf8")).root;
5050
Test = root.lookup("Test");
51+
json = JSON.stringify(root);
5152
data = require("../bench/bench.json");
5253
count = 10000000;
5354
process.stdout.write("bench.proto");
@@ -58,6 +59,7 @@ if (process.argv.indexOf("--alt") < 0) {
5859
count = 1000;
5960
process.stdout.write("vector_tile.proto");
6061
}
62+
var json = JSON.stringify(root);
6163

6264
if (process.argv.length > 3 && /^\d+$/.test(process.argv[3]))
6365
count = parseInt(process.argv[3], 10);
@@ -84,4 +86,8 @@ switch (process.argv[2]) {
8486
for (var j = 0; j < count; ++j)
8587
Test.decode(buf);
8688
break;
89+
case "fromjson":
90+
for (var k = 0; k < 100000000; ++k)
91+
protobuf.Root.fromJSON(json).resolveAll();
92+
break;
8793
}

cli/targets/json-module.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ json_module.description = "JSON representation as a module"
77

88
function json_module(root, options, callback) {
99
try {
10-
var output = "var $root = protobuf.Root.fromJSON(" + JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim() + ").resolveAll();";
10+
var output = "var $root = protobuf.Root.fromJSON(" + JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim() + ");";
1111
output = util.wrap(output, options);
1212
process.nextTick(function() {
1313
callback(null, output);

dist/light/protobuf.js

+25-50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/light/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/light/protobuf.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/light/protobuf.min.js.gz

-42 Bytes
Binary file not shown.

dist/light/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/minimal/protobuf.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/minimal/protobuf.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/minimal/protobuf.min.js.gz

0 Bytes
Binary file not shown.

dist/protobuf.js

+25-50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

-39 Bytes
Binary file not shown.

dist/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)