Skip to content

Commit

Permalink
Add methodName to rpc methods to allow to determine methods after cod…
Browse files Browse the repository at this point in the history
…e minification
  • Loading branch information
Szpadel committed Jul 5, 2017
1 parent c43243b commit 2dba549
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ function buildService(ref, service) {
push("return this.rpcCall(" + escapeName(lcName) + ", $root." + exportName(method.resolvedRequestType) + ", $root." + exportName(method.resolvedResponseType) + ", request, callback);");
--indent;
push("};");
push(escapeName(service.name) + ".prototype" + util.safeProp(lcName) + ".methodName = \""+ escapeName(lcName) +"\";");
if (config.comments)
push("");
pushComment([
Expand Down
4 changes: 2 additions & 2 deletions examples/streaming-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ function performRequestOverTransportChannel(requestData, callback) {
// Listen for events:

greeter.on("data", function(response, method) {
console.log("data in " + method.name + ":", response.message);
console.log("data in " + method.nameName + ":", response.message);
});

greeter.on("end", function() {
console.log("end");
});

greeter.on("error", function(err, method) {
console.log("error in " + method.name + ":", err);
console.log("error in " + method.nameName + ":", err);
});

// Call methods:
Expand Down
12 changes: 10 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,10 @@ export namespace rpc {
* @param [callback] Node-style callback called with the error, if any, and the response message
* @returns Promise if `callback` has been omitted, otherwise `undefined`
*/
type ServiceMethod<TReq extends Message<TReq>, TRes extends Message<TRes>> = (request: (TReq|Properties<TReq>), callback?: rpc.ServiceMethodCallback<TRes>) => Promise<Message<TRes>>;
interface ServiceMethod<TReq extends Message<TReq>, TRes extends Message<TRes>> {
(request: (TReq|Properties<TReq>), callback?: rpc.ServiceMethodCallback<TRes>): Promise<Message<TRes>>;
methodName: string;
}

/** An RPC service as returned by {@link Service#create}. */
class Service extends util.EventEmitter {
Expand Down Expand Up @@ -1274,13 +1277,18 @@ export namespace rpc {
}
}

/**
* Method name enhanced with methodName required to determine service methods after minification
*/
export type RpcServiceMethod = Method & rpc.ServiceMethod<Message<{}>, Message<{}>>;

/**
* RPC implementation passed to {@link Service#create} performing a service request on network level, i.e. by utilizing http requests or websockets.
* @param method Reflected or static method being called
* @param requestData Request data
* @param callback Callback function
*/
type RPCImpl = (method: (Method|rpc.ServiceMethod<Message<{}>, Message<{}>>), requestData: Uint8Array, callback: RPCImplCallback) => void;
type RPCImpl = (method: RpcServiceMethod, requestData: Uint8Array, callback: RPCImplCallback) => void;

/**
* Node-style callback as used by {@link RPCImpl}.
Expand Down
5 changes: 3 additions & 2 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ Service.prototype.remove = function remove(object) {
Service.prototype.create = function create(rpcImpl, requestDelimited, responseDelimited) {
var rpcService = new rpc.Service(rpcImpl, requestDelimited, responseDelimited);
for (var i = 0, method; i < /* initializes */ this.methodsArray.length; ++i) {
rpcService[util.lcFirst((method = this._methodsArray[i]).resolve().name)] = util.codegen(["r","c"], util.lcFirst(method.name))("return this.rpcCall(m,q,s,r,c)")({
rpcService[util.lcFirst((method = this._methodsArray[i]).resolve().name)] = util.codegen(["r","c"], util.lcFirst(method.name))("m.methodName = n; return this.rpcCall(m,q,s,r,c)")({
m: method,
q: method.resolvedRequestType.ctor,
s: method.resolvedResponseType.ctor
s: method.resolvedResponseType.ctor,
n: method.name
});
}
return rpcService;
Expand Down
3 changes: 2 additions & 1 deletion tests/api_service-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tape.test("reflected services", function(test) {
function rpcImpl(method, requestData, callback) {
if (requestData) {
test.equal(method, DoSomething, "rpcImpl should reference the correct method");
test.equal(method.methodName, method.name, "should contain methodName with name of rpc method");
test.ok(callback, "rpcImpl should provide a callback");
setTimeout(function() {
callback(null, DoSomethingResponse.create());
Expand Down Expand Up @@ -115,4 +116,4 @@ tape.test("reflected services", function(test) {
service.end();
});

});
});
1 change: 1 addition & 0 deletions tests/data/rpc-es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const MyService = $root.MyService = (() => {
MyService.prototype.myMethod = function myMethod(request, callback) {
return this.rpcCall(myMethod, $root.MyRequest, $root.MyResponse, request, callback);
};
MyService.prototype.myMethod.methodName = 'myMethod';

/**
* Calls MyMethod.
Expand Down

0 comments on commit 2dba549

Please sign in to comment.