Skip to content

Commit 3aa984e

Browse files
committed
Initial loadSync (node only), see #529
1 parent 3891ab0 commit 3aa984e

11 files changed

+235
-113
lines changed

bench/index.js

+48-50
Original file line numberDiff line numberDiff line change
@@ -19,61 +19,59 @@ var protobuf = require("../src/index"),
1919
//
2020
// To experience the impact by yourself, increase string lengths within bench.json.
2121

22-
protobuf.load(require.resolve("./bench.proto"), function onload(err, root) {
23-
var Test = root.lookup("Test");
22+
var root = protobuf.loadSync(require.resolve("./bench.proto"));
23+
var Test = root.lookup("Test");
2424

25-
protobuf.codegen.verbose = true;
25+
protobuf.codegen.verbose = true;
2626

27-
var buf = Test.encode(data).finish();
27+
var buf = Test.encode(data).finish();
2828

29-
// warm up
30-
for (var i = 0; i < 500000; ++i)
31-
Test.encode(data).finish();
32-
for (var i = 0; i < 1000000; ++i)
33-
Test.decode(buf);
34-
console.log("");
35-
36-
// give the optimizer some time to do its job
37-
setTimeout(function() {
38-
var str = JSON.stringify(data),
39-
strbuf = Buffer.from(str, "utf8");
29+
// warm up
30+
for (var i = 0; i < 500000; ++i)
31+
Test.encode(data).finish();
32+
for (var i = 0; i < 1000000; ++i)
33+
Test.decode(buf);
34+
console.log("");
4035

41-
newSuite("encoding")
42-
.add("Type.encode to buffer", function() {
43-
Test.encode(data).finish();
44-
})
45-
.add("JSON.stringify to string", function() {
46-
JSON.stringify(data);
47-
})
48-
.add("JSON.stringify to buffer", function() {
49-
new Buffer(JSON.stringify(data), "utf8");
50-
})
51-
.run();
36+
// give the optimizer some time to do its job
37+
setTimeout(function() {
38+
var str = JSON.stringify(data),
39+
strbuf = Buffer.from(str, "utf8");
5240

53-
newSuite("decoding")
54-
.add("Type.decode from buffer", function() {
55-
Test.decode(buf);
56-
})
57-
.add("JSON.parse from string", function() {
58-
JSON.parse(str);
59-
})
60-
.add("JSON.parse from buffer", function() {
61-
JSON.parse(strbuf.toString("utf8"));
62-
})
63-
.run();
41+
newSuite("encoding")
42+
.add("Type.encode to buffer", function() {
43+
Test.encode(data).finish();
44+
})
45+
.add("JSON.stringify to string", function() {
46+
JSON.stringify(data);
47+
})
48+
.add("JSON.stringify to buffer", function() {
49+
new Buffer(JSON.stringify(data), "utf8");
50+
})
51+
.run();
6452

65-
newSuite("combined")
66-
.add("Type to/from buffer", function() {
67-
Test.decode(Test.encode(data).finish());
68-
})
69-
.add("JSON to/from string", function() {
70-
JSON.parse(JSON.stringify(data));
71-
})
72-
.add("JSON to/from buffer", function() {
73-
JSON.parse(new Buffer(JSON.stringify(data), "utf8").toString("utf8"));
74-
})
75-
.run();
53+
newSuite("decoding")
54+
.add("Type.decode from buffer", function() {
55+
Test.decode(buf);
56+
})
57+
.add("JSON.parse from string", function() {
58+
JSON.parse(str);
59+
})
60+
.add("JSON.parse from buffer", function() {
61+
JSON.parse(strbuf.toString("utf8"));
62+
})
63+
.run();
7664

77-
}, 3000);
65+
newSuite("combined")
66+
.add("Type to/from buffer", function() {
67+
Test.decode(Test.encode(data).finish());
68+
})
69+
.add("JSON to/from string", function() {
70+
JSON.parse(JSON.stringify(data));
71+
})
72+
.add("JSON to/from buffer", function() {
73+
JSON.parse(new Buffer(JSON.stringify(data), "utf8").toString("utf8"));
74+
})
75+
.run();
7876

79-
});
77+
}, 3000);

dist/protobuf.js

+77-25
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

82 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.

examples/streaming-rpc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ greeter.sayHello({ name: 'protocol' });
8585
greeter.sayHello({ name: 'buffers' });
8686
greeter.sayHello(Hello.create({ name: 'for' })); // or use runtime messages
8787

88-
// Listen to and emit your own events if you want:
88+
// Listen to and emit your own events if you like:
8989

9090
greeter.on("status", function(code, text) {
9191
console.log("status:", code, text);
9292
});
93+
9394
greeter.emit("status", 200, "OK");
9495

9596
// And, if applicable, end the service when you are done:

src/index.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var protobuf = global.protobuf = exports;
77
* @param {Root} root Root namespace, defaults to create a new one if omitted.
88
* @param {function(?Error, Root=)} callback Callback function
99
* @returns {undefined}
10-
* @throws {TypeError} If arguments are invalid
1110
*/
1211
function load(filename, root, callback) {
1312
if (typeof root === 'function') {
@@ -26,7 +25,6 @@ function load(filename, root, callback) {
2625
* @param {string|string[]} filename One or multiple files to load
2726
* @param {function(?Error, Root=)} callback Callback function
2827
* @returns {undefined}
29-
* @throws {TypeError} If arguments are invalid
3028
* @variation 2
3129
*/
3230
// function load(filename:string, callback:function):undefined
@@ -38,13 +36,27 @@ function load(filename, root, callback) {
3836
* @param {string|string[]} filename One or multiple files to load
3937
* @param {Root} [root] Root namespace, defaults to create a new one if omitted.
4038
* @returns {Promise<Root>} Promise
41-
* @throws {TypeError} If arguments are invalid
4239
* @variation 3
4340
*/
4441
// function load(filename:string, [root:Root]):Promise<Root>
4542

4643
protobuf.load = load;
4744

45+
/**
46+
* Synchronously loads one or multiple .proto or preprocessed .json files into a common root namespace.
47+
* @param {string|string[]} filename One or multiple files to load
48+
* @param {Root} [root] Root namespace, defaults to create a new one if omitted.
49+
* @returns {Root} Root namespace
50+
* @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
51+
*/
52+
function loadSync(filename, root) {
53+
if (!root)
54+
root = new protobuf.Root();
55+
return root.loadSync(filename);
56+
}
57+
58+
protobuf.loadSync = loadSync;
59+
4860
// Parser
4961
protobuf.tokenize = require("./tokenize");
5062
protobuf.parse = require("./parse");

0 commit comments

Comments
 (0)