Skip to content

Commit 2dba549

Browse files
committed
Add methodName to rpc methods to allow to determine methods after code minification
1 parent c43243b commit 2dba549

File tree

6 files changed

+19
-7
lines changed

6 files changed

+19
-7
lines changed

cli/targets/static.js

+1
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ function buildService(ref, service) {
655655
push("return this.rpcCall(" + escapeName(lcName) + ", $root." + exportName(method.resolvedRequestType) + ", $root." + exportName(method.resolvedResponseType) + ", request, callback);");
656656
--indent;
657657
push("};");
658+
push(escapeName(service.name) + ".prototype" + util.safeProp(lcName) + ".methodName = \""+ escapeName(lcName) +"\";");
658659
if (config.comments)
659660
push("");
660661
pushComment([

examples/streaming-rpc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ function performRequestOverTransportChannel(requestData, callback) {
7878
// Listen for events:
7979

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

8484
greeter.on("end", function() {
8585
console.log("end");
8686
});
8787

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

9292
// Call methods:

index.d.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,10 @@ export namespace rpc {
12331233
* @param [callback] Node-style callback called with the error, if any, and the response message
12341234
* @returns Promise if `callback` has been omitted, otherwise `undefined`
12351235
*/
1236-
type ServiceMethod<TReq extends Message<TReq>, TRes extends Message<TRes>> = (request: (TReq|Properties<TReq>), callback?: rpc.ServiceMethodCallback<TRes>) => Promise<Message<TRes>>;
1236+
interface ServiceMethod<TReq extends Message<TReq>, TRes extends Message<TRes>> {
1237+
(request: (TReq|Properties<TReq>), callback?: rpc.ServiceMethodCallback<TRes>): Promise<Message<TRes>>;
1238+
methodName: string;
1239+
}
12371240

12381241
/** An RPC service as returned by {@link Service#create}. */
12391242
class Service extends util.EventEmitter {
@@ -1274,13 +1277,18 @@ export namespace rpc {
12741277
}
12751278
}
12761279

1280+
/**
1281+
* Method name enhanced with methodName required to determine service methods after minification
1282+
*/
1283+
export type RpcServiceMethod = Method & rpc.ServiceMethod<Message<{}>, Message<{}>>;
1284+
12771285
/**
12781286
* RPC implementation passed to {@link Service#create} performing a service request on network level, i.e. by utilizing http requests or websockets.
12791287
* @param method Reflected or static method being called
12801288
* @param requestData Request data
12811289
* @param callback Callback function
12821290
*/
1283-
type RPCImpl = (method: (Method|rpc.ServiceMethod<Message<{}>, Message<{}>>), requestData: Uint8Array, callback: RPCImplCallback) => void;
1291+
type RPCImpl = (method: RpcServiceMethod, requestData: Uint8Array, callback: RPCImplCallback) => void;
12841292

12851293
/**
12861294
* Node-style callback as used by {@link RPCImpl}.

src/service.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ Service.prototype.remove = function remove(object) {
152152
Service.prototype.create = function create(rpcImpl, requestDelimited, responseDelimited) {
153153
var rpcService = new rpc.Service(rpcImpl, requestDelimited, responseDelimited);
154154
for (var i = 0, method; i < /* initializes */ this.methodsArray.length; ++i) {
155-
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)")({
155+
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)")({
156156
m: method,
157157
q: method.resolvedRequestType.ctor,
158-
s: method.resolvedResponseType.ctor
158+
s: method.resolvedResponseType.ctor,
159+
n: method.name
159160
});
160161
}
161162
return rpcService;

tests/api_service-rpc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tape.test("reflected services", function(test) {
3232
function rpcImpl(method, requestData, callback) {
3333
if (requestData) {
3434
test.equal(method, DoSomething, "rpcImpl should reference the correct method");
35+
test.equal(method.methodName, method.name, "should contain methodName with name of rpc method");
3536
test.ok(callback, "rpcImpl should provide a callback");
3637
setTimeout(function() {
3738
callback(null, DoSomethingResponse.create());
@@ -115,4 +116,4 @@ tape.test("reflected services", function(test) {
115116
service.end();
116117
});
117118

118-
});
119+
});

tests/data/rpc-es6.js

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const MyService = $root.MyService = (() => {
6161
MyService.prototype.myMethod = function myMethod(request, callback) {
6262
return this.rpcCall(myMethod, $root.MyRequest, $root.MyResponse, request, callback);
6363
};
64+
MyService.prototype.myMethod.methodName = 'myMethod';
6465

6566
/**
6667
* Calls MyMethod.

0 commit comments

Comments
 (0)