Skip to content

Commit 81e36a7

Browse files
committed
pbjs static target services support
1 parent 98d6ae1 commit 81e36a7

File tree

11 files changed

+466
-58
lines changed

11 files changed

+466
-58
lines changed

bench/index.js

+47-36
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,57 @@ protobuf.load(require.resolve("./bench.proto"), function onload(err, root) {
2323
var Test = root.lookup("Test");
2424

2525
protobuf.util.codegen.verbose = true;
26-
var buf = Test.encode(data).finish(),
27-
dec = Test.decode(buf);
2826

29-
var str = JSON.stringify(data),
30-
strbuf = Buffer.from(str, "utf8");
27+
var buf = Test.encode(data).finish();
3128

32-
newSuite("encoding")
33-
.add("Type.encode to buffer", function() {
29+
// warm up
30+
for (var i = 0; i < 500000; ++i)
3431
Test.encode(data).finish();
35-
})
36-
.add("JSON.stringify to string", function() {
37-
JSON.stringify(data);
38-
})
39-
.add("JSON.stringify to buffer", function() {
40-
new Buffer(JSON.stringify(data), "utf8");
41-
})
42-
.run();
43-
44-
newSuite("decoding")
45-
.add("Type.decode from buffer", function() {
32+
for (var i = 0; i < 1000000; ++i)
4633
Test.decode(buf);
47-
})
48-
.add("JSON.parse from string", function() {
49-
JSON.parse(str);
50-
})
51-
.add("JSON.parse from buffer", function() {
52-
JSON.parse(strbuf.toString("utf8"));
53-
})
54-
.run();
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");
40+
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();
52+
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();
64+
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();
5576

56-
newSuite("combined")
57-
.add("Type to/from buffer", function() {
58-
Test.decode(Test.encode(data).finish());
59-
})
60-
.add("JSON to/from string", function() {
61-
JSON.parse(JSON.stringify(data));
62-
})
63-
.add("JSON to/from buffer", function() {
64-
JSON.parse(new Buffer(JSON.stringify(data), "utf8").toString("utf8"));
65-
})
66-
.run();
77+
}, 3000);
6778

6879
});

cli/targets/static.js

+81-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module.exports = static_target;
66
// - You can specify a custom wrapper with the --wrap argument.
77
// - CommonJS modules depend on the minimal static runtime for reduced package size with browserify.
88
// - AMD and global scope depend on the full library for now.
9-
// - Services aren't supported, yet.
109

1110
var path = require("path"),
1211
fs = require("fs");
@@ -230,9 +229,88 @@ function buildType(ref, type) {
230229
}
231230

232231
function buildService(ref, service) {
232+
var fullName = service.fullName.substring(1);
233+
233234
push("");
234-
push(name(ref) + "." + name(service.name) + " = {};");
235-
// TODO: Services are just empty objects currently
235+
pushComment([
236+
"Constructs a new " + service.name + ".",
237+
"@exports " + fullName,
238+
"@constructor",
239+
"@param {function(function, Uint8Array, function)} rpc RPC implementation",
240+
"@param {boolean} [requestDelimited=false] Whether requests are length-delimited",
241+
"@param {boolean} [responseDelimited=false] Whether responses are length-delimited"
242+
]);
243+
push("function " + name(service.name) + "(rpc, requestDelimited, responseDelimited) {");
244+
++indent;
245+
push("");
246+
pushComment([
247+
"RPC implementation.",
248+
"@type {function(function, Uint8Array, function)}"
249+
]);
250+
push("this.rpc = rpc;");
251+
push("");
252+
pushComment([
253+
"Whether requests are length-delimited.",
254+
"@type {boolean}"
255+
]);
256+
push("this.requestDelimited = Boolean(requestDelimited);");
257+
push("");
258+
pushComment([
259+
"Whether responses are length-delimited.",
260+
"@type {boolean}"
261+
]);
262+
push("this.responseDelimited = Boolean(responseDelimited);");
263+
--indent;
264+
push("};");
265+
service.getMethodsArray().forEach(function(method) {
266+
method.resolve();
267+
var lcName = method.name.substring(0, 1).toLowerCase() + method.name.substring(1);
268+
push("");
269+
pushComment([
270+
"Calls " + method.name + ".",
271+
"@param {" + method.resolvedRequestType.fullName.substring(1) + "|Object} request " + method.resolvedRequestType.name + " or plain object",
272+
"@param {function(?Error, " + method.resolvedResponseType.fullName.substring(1) + "=)} callback Node-style callback called with the error, if any, and " + method.resolvedResponseType.name,
273+
"@returns {undefined}"
274+
]);
275+
push(name(service.name) + ".prototype." + name(lcName) + " = function " + name(lcName) + "(request, callback) {");
276+
++indent;
277+
push("var requestData;");
278+
push("try {");
279+
++indent;
280+
push("requestData = (this.requestDelimited && $root" + name(method.resolvedRequestType.fullName) + ".encodeDelimited(request) || $root" + name(method.resolvedRequestType.fullName) + ".encode(request)).finish();");
281+
--indent;
282+
push("} catch (err) {");
283+
++indent;
284+
push("(typeof setImmediate === 'function' && setImmediate || setTimeout)(function() { callback(err); });");
285+
push("return;");
286+
--indent;
287+
push("}");
288+
push("var self = this;");
289+
push("this.rpc(" + name(lcName) + ", requestData, function(err, responseData) {");
290+
++indent;
291+
push("if (err) {");
292+
++indent;
293+
push("callback(err);");
294+
push("return;");
295+
--indent;
296+
push("}");
297+
push("var response;");
298+
push("try {");
299+
++indent;
300+
push("response = self.responseDelimited && $root" + name(method.resolvedResponseType.fullName) + ".decodeDelimited(responseData) || $root" + name(method.resolvedResponseType.fullName) + ".decode(responseData);");
301+
--indent;
302+
push("} catch (err2) {");
303+
++indent;
304+
push("callback(err2);");
305+
push("return;");
306+
--indent;
307+
push("}");
308+
push("callback(null, response);");
309+
--indent;
310+
push("});");
311+
--indent;
312+
push("};");
313+
});
236314
}
237315

238316
function buildEnum(ref, enm) {

dist/protobuf.js

+7-6
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

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

src/service.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ ServicePrototype.remove = function remove(object) {
152152
/**
153153
* Creates a runtime service using the specified rpc implementation.
154154
* @param {function(Method, Uint8Array, function)} rpc RPC implementation ({@link RPCImpl|see})
155-
* @param {boolean} [requestDelimited=false] Whether request data is length delimited
156-
* @param {boolean} [responseDelimited=false] Whether response data is length delimited
155+
* @param {boolean} [requestDelimited=false] Whether requests are length-delimited
156+
* @param {boolean} [responseDelimited=false] Whether responses are length-delimited
157157
* @returns {Object} Runtime service
158158
*/
159159
ServicePrototype.create = function create(rpc, requestDelimited, responseDelimited) {
@@ -162,7 +162,8 @@ ServicePrototype.create = function create(rpc, requestDelimited, responseDelimit
162162
value: rpc
163163
});
164164
this.getMethodsArray().forEach(function(method) {
165-
rpcService[method.name] = function(request, callback) {
165+
var lcName = method.name.substring(0, 1).toLowerCase() + method.name.substring(1);
166+
rpcService[lcName] = function(request, callback) {
166167
method.resolve();
167168
var requestData;
168169
try {

src/writer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,8 @@ BufferWriterPrototype.string = function write_string_buffer(value) {
546546
? byteLength(value)
547547
: util.Buffer.byteLength(value);
548548
return len
549-
? this.uint32(len).push(writeStringBuffer, len, value)
550-
: this.push(writeByte, 1, 0);
549+
? this.uint32(len).push(writeStringBuffer, len, value)
550+
: this.push(writeByte, 1, 0);
551551
};
552552

553553
/**

0 commit comments

Comments
 (0)