Skip to content

Commit

Permalink
Breaking: Reflected and statically generated services use common util…
Browse files Browse the repository at this point in the history
…ity, now work exactly the same
  • Loading branch information
dcodeIO committed Jan 21, 2017
1 parent 7299929 commit 27b1635
Show file tree
Hide file tree
Showing 38 changed files with 983 additions and 602 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@

// Variables
"init-declarations": 0, // because no-undef-init is on and we actually want undefineds
"no-catch-shadow": 1,
"no-catch-shadow": 0, // no IE8 support anyway
"no-label-var": 1,
"no-restricted-globals": 1,
"no-return-assign": 0, // can make sense.
Expand Down
103 changes: 8 additions & 95 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var Type = protobuf.Type,
var out = [];
var indent = 0;
var config = {};
var firstService = true;

static_target.description = "Static code without reflection";

Expand Down Expand Up @@ -53,7 +52,6 @@ function static_target(root, options, callback) {
out = [];
indent = 0;
config = {};
firstService = true;
}
}

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

if (firstService) {
firstService = false;

push("");
pushComment([
"RPC implementation passed to services performing a service request on network level, i.e. by utilizing http requests or websockets.",
"@typedef RPCImpl",
"@type {function}",
"@param {function} method Method being called",
"@param {Uint8Array} requestData Request data",
"@param {RPCCallback} callback Callback function",
"@returns {undefined}"
]);

push("");
pushComment([
"Node-style callback as used by {@link RPCImpl}.",
"@typedef RPCCallback",
"@type {function}",
"@param {?Error} error Error, if any, otherwise `null`",
"@param {Uint8Array} [responseData] Response data or `null` to signal end of stream, if there hasn't been an error",
"@returns {undefined}"
]);
}

push("");
pushComment([
"Constructs a new " + service.name + " service.",
service.comment ? "@classdesc " + service.comment : null,
"@exports " + fullName,
"@extends $protobuf.rpc.Service",
"@constructor",
"@param {RPCImpl} rpcImpl RPC implementation",
"@param {$protobuf.RPCImpl} rpcImpl RPC implementation",
"@param {boolean} [requestDelimited=false] Whether requests are length-delimited",
"@param {boolean} [responseDelimited=false] Whether responses are length-delimited"
]);
push("function " + name(service.name) + "(rpcImpl, requestDelimited, responseDelimited) {");
++indent;
push("$protobuf.rpc.Service.call(this, rpcImpl);");
push("");
pushComment([
"Whether requests are length-delimited.",
"@type {boolean}"
]);
push("this.requestDelimited = Boolean(requestDelimited);");
push("");
pushComment([
"Whether responses are length-delimited.",
"@type {boolean}"
]);
push("this.responseDelimited = Boolean(responseDelimited);");
push("$protobuf.rpc.Service.call(this, rpcImpl, requestDelimited, responseDelimited);");
--indent;
push("}");
push("");
Expand All @@ -580,7 +542,7 @@ function buildService(ref, service) {
push("");
pushComment([
"Creates new " + service.name + " service using the specified rpc implementation.",
"@param {RPCImpl} rpcImpl RPC implementation",
"@param {$protobuf.RPCImpl} rpcImpl RPC implementation",
"@param {boolean} [requestDelimited=false] Whether requests are length-delimited",
"@param {boolean} [responseDelimited=false] Whether responses are length-delimited",
"@returns {" + name(service.name) + "} RPC service. Useful where requests and/or responses are streamed."
Expand All @@ -599,10 +561,11 @@ function buildService(ref, service) {
var cbName = name(service.name) + "_" + name(lcName) + "_Callback";
pushComment([
"Callback as used by {@link " + name(service.name) + "#" + name(lcName) + "}.",
// This is a more specialized version of protobuf.rpc.ServiceCallback
"@typedef " + cbName,
"@type {function}",
"@param {?Error} error Error, if any",
"@param {" + method.resolvedResponseType.fullName.substring(1) + "} [response] " + method.resolvedResponseType.name + " or `null` if the service has been terminated server-side"
"@param {" + method.resolvedResponseType.fullName.substring(1) + "} [response] " + method.resolvedResponseType.name
]);
push("");
pushComment([
Expand All @@ -613,58 +576,8 @@ function buildService(ref, service) {
]);
push(name(service.name) + ".prototype" + util.safeProp(lcName) + " = function " + name(lcName) + "(request, callback) {");
++indent;
push("if (!request)");
++indent;
push("throw TypeError(\"request must be specified\");");
--indent;
push("if (!callback)");
++indent;
push("return $util.asPromise(" + name(lcName) + ", this, request);");
--indent;
push("var $self = this;");
push("this.rpc(" + name(lcName) + ", (this.requestDelimited");
++indent;
push("? $root" + method.resolvedRequestType.fullName + ".encodeDelimited(request)");
push(": $root" + method.resolvedRequestType.fullName + ".encode(request)");
--indent;
push(").finish(), function $rpcCallback(err, response) {");
++indent;
push("if (err) {");
++indent;
push("$self.emit(\"error\", err, " + name(lcName) + ");");
push("return callback(err);");
--indent;
push("}");
push("if (response === null) {");
++indent;
push("$self.end(true);");
push("return undefined;");
--indent;
push("}");
push("if (!(response instanceof $root" + method.resolvedResponseType.fullName + ")) {");
++indent;
push("try {");
++indent;
push("response = $self.responseDelimited");
++indent;
push("? $root" + method.resolvedResponseType.fullName + ".decodeDelimited(response)");
push(": $root" + method.resolvedResponseType.fullName + ".decode(response);");
--indent;
--indent;
push("} catch (err2) {");
++indent;
push("$self.emit(\"error\", err2, " + name(lcName) + ");");
push("return callback(err2);");
--indent;
push("}");
--indent;
push("}");
push("$self.emit(\"data\", response, " + name(lcName) + ");");
push("return callback(null, response);");
--indent;
push("});");
push("return undefined;");
--indent;
push("return this.rpcCall(" + name(lcName) + ", $root" + method.resolvedRequestType.fullName + ", $root" + method.resolvedResponseType.fullName + ", request, callback);");
--indent;
push("};");
if (config.comments)
push("");
Expand Down
Loading

0 comments on commit 27b1635

Please sign in to comment.