Skip to content

Commit 27b1635

Browse files
committed
Breaking: Reflected and statically generated services use common utility, now work exactly the same
1 parent 7299929 commit 27b1635

38 files changed

+983
-602
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090

9191
// Variables
9292
"init-declarations": 0, // because no-undef-init is on and we actually want undefineds
93-
"no-catch-shadow": 1,
93+
"no-catch-shadow": 0, // no IE8 support anyway
9494
"no-label-var": 1,
9595
"no-restricted-globals": 1,
9696
"no-return-assign": 0, // can make sense.

cli/targets/static.js

Lines changed: 8 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ var Type = protobuf.Type,
1717
var out = [];
1818
var indent = 0;
1919
var config = {};
20-
var firstService = true;
2120

2221
static_target.description = "Static code without reflection";
2322

@@ -53,7 +52,6 @@ function static_target(root, options, callback) {
5352
out = [];
5453
indent = 0;
5554
config = {};
56-
firstService = true;
5755
}
5856
}
5957

@@ -478,6 +476,7 @@ function buildType(ref, type) {
478476
pushComment([
479477
"Creates " + aOrAn(type.name) + " message from a plain object. Also converts values to their respective internal types.",
480478
"This is an alias of {@link " + fullName + ".fromObject}.",
479+
"@function",
481480
"@param {Object.<string,*>} object Plain object",
482481
"@returns {" + fullName + "} " + type.name
483482
]);
@@ -520,57 +519,20 @@ function buildType(ref, type) {
520519
function buildService(ref, service) {
521520
var fullName = service.fullName.substring(1);
522521

523-
if (firstService) {
524-
firstService = false;
525-
526-
push("");
527-
pushComment([
528-
"RPC implementation passed to services performing a service request on network level, i.e. by utilizing http requests or websockets.",
529-
"@typedef RPCImpl",
530-
"@type {function}",
531-
"@param {function} method Method being called",
532-
"@param {Uint8Array} requestData Request data",
533-
"@param {RPCCallback} callback Callback function",
534-
"@returns {undefined}"
535-
]);
536-
537-
push("");
538-
pushComment([
539-
"Node-style callback as used by {@link RPCImpl}.",
540-
"@typedef RPCCallback",
541-
"@type {function}",
542-
"@param {?Error} error Error, if any, otherwise `null`",
543-
"@param {Uint8Array} [responseData] Response data or `null` to signal end of stream, if there hasn't been an error",
544-
"@returns {undefined}"
545-
]);
546-
}
547-
548522
push("");
549523
pushComment([
550524
"Constructs a new " + service.name + " service.",
551525
service.comment ? "@classdesc " + service.comment : null,
552526
"@exports " + fullName,
553527
"@extends $protobuf.rpc.Service",
554528
"@constructor",
555-
"@param {RPCImpl} rpcImpl RPC implementation",
529+
"@param {$protobuf.RPCImpl} rpcImpl RPC implementation",
556530
"@param {boolean} [requestDelimited=false] Whether requests are length-delimited",
557531
"@param {boolean} [responseDelimited=false] Whether responses are length-delimited"
558532
]);
559533
push("function " + name(service.name) + "(rpcImpl, requestDelimited, responseDelimited) {");
560534
++indent;
561-
push("$protobuf.rpc.Service.call(this, rpcImpl);");
562-
push("");
563-
pushComment([
564-
"Whether requests are length-delimited.",
565-
"@type {boolean}"
566-
]);
567-
push("this.requestDelimited = Boolean(requestDelimited);");
568-
push("");
569-
pushComment([
570-
"Whether responses are length-delimited.",
571-
"@type {boolean}"
572-
]);
573-
push("this.responseDelimited = Boolean(responseDelimited);");
535+
push("$protobuf.rpc.Service.call(this, rpcImpl, requestDelimited, responseDelimited);");
574536
--indent;
575537
push("}");
576538
push("");
@@ -580,7 +542,7 @@ function buildService(ref, service) {
580542
push("");
581543
pushComment([
582544
"Creates new " + service.name + " service using the specified rpc implementation.",
583-
"@param {RPCImpl} rpcImpl RPC implementation",
545+
"@param {$protobuf.RPCImpl} rpcImpl RPC implementation",
584546
"@param {boolean} [requestDelimited=false] Whether requests are length-delimited",
585547
"@param {boolean} [responseDelimited=false] Whether responses are length-delimited",
586548
"@returns {" + name(service.name) + "} RPC service. Useful where requests and/or responses are streamed."
@@ -599,10 +561,11 @@ function buildService(ref, service) {
599561
var cbName = name(service.name) + "_" + name(lcName) + "_Callback";
600562
pushComment([
601563
"Callback as used by {@link " + name(service.name) + "#" + name(lcName) + "}.",
564+
// This is a more specialized version of protobuf.rpc.ServiceCallback
602565
"@typedef " + cbName,
603566
"@type {function}",
604567
"@param {?Error} error Error, if any",
605-
"@param {" + method.resolvedResponseType.fullName.substring(1) + "} [response] " + method.resolvedResponseType.name + " or `null` if the service has been terminated server-side"
568+
"@param {" + method.resolvedResponseType.fullName.substring(1) + "} [response] " + method.resolvedResponseType.name
606569
]);
607570
push("");
608571
pushComment([
@@ -613,58 +576,8 @@ function buildService(ref, service) {
613576
]);
614577
push(name(service.name) + ".prototype" + util.safeProp(lcName) + " = function " + name(lcName) + "(request, callback) {");
615578
++indent;
616-
push("if (!request)");
617-
++indent;
618-
push("throw TypeError(\"request must be specified\");");
619-
--indent;
620-
push("if (!callback)");
621-
++indent;
622-
push("return $util.asPromise(" + name(lcName) + ", this, request);");
623-
--indent;
624-
push("var $self = this;");
625-
push("this.rpc(" + name(lcName) + ", (this.requestDelimited");
626-
++indent;
627-
push("? $root" + method.resolvedRequestType.fullName + ".encodeDelimited(request)");
628-
push(": $root" + method.resolvedRequestType.fullName + ".encode(request)");
629-
--indent;
630-
push(").finish(), function $rpcCallback(err, response) {");
631-
++indent;
632-
push("if (err) {");
633-
++indent;
634-
push("$self.emit(\"error\", err, " + name(lcName) + ");");
635-
push("return callback(err);");
636-
--indent;
637-
push("}");
638-
push("if (response === null) {");
639-
++indent;
640-
push("$self.end(true);");
641-
push("return undefined;");
642-
--indent;
643-
push("}");
644-
push("if (!(response instanceof $root" + method.resolvedResponseType.fullName + ")) {");
645-
++indent;
646-
push("try {");
647-
++indent;
648-
push("response = $self.responseDelimited");
649-
++indent;
650-
push("? $root" + method.resolvedResponseType.fullName + ".decodeDelimited(response)");
651-
push(": $root" + method.resolvedResponseType.fullName + ".decode(response);");
652-
--indent;
653-
--indent;
654-
push("} catch (err2) {");
655-
++indent;
656-
push("$self.emit(\"error\", err2, " + name(lcName) + ");");
657-
push("return callback(err2);");
658-
--indent;
659-
push("}");
660-
--indent;
661-
push("}");
662-
push("$self.emit(\"data\", response, " + name(lcName) + ");");
663-
push("return callback(null, response);");
664-
--indent;
665-
push("});");
666-
push("return undefined;");
667-
--indent;
579+
push("return this.rpcCall(" + name(lcName) + ", $root" + method.resolvedRequestType.fullName + ", $root" + method.resolvedResponseType.fullName + ", request, callback);");
580+
--indent;
668581
push("};");
669582
if (config.comments)
670583
push("");

0 commit comments

Comments
 (0)