Permalink
Browse files

New: Relieved the requirement to call .resolveAll() on roots in order…

… to populate static code-compatible properties, see #653
  • Loading branch information...
dcodeIO committed Jan 20, 2017
1 parent 33da148 commit b4f4f48f1949876ae92808b0a5ca5f2b29cc011c
@@ -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/)).
@@ -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:
@@ -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();
@@ -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;
@@ -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");
@@ -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);
@@ -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;
}
@@ -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);

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN -42 Bytes (100%) dist/light/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
BIN +0 Bytes (100%) dist/minimal/protobuf.min.js.gz
Binary file not shown.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN -39 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit b4f4f48

Please sign in to comment.