Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methodName to rpc methods to allow to determine methods after code minification #857

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ matrix:
- node_js: 6
script: set -e; if [ -n "$SAUCE_USERNAME" ]; then npm install zuul@^3.11.1 zuul-ngrok@^4.0.0; travis_wait npm run zuul; sleep 3; fi
- node_js: 6
script: npm install codeclimate-test-reporter@^0.4.1; npm run coverage-ci
script: npm install codeclimate-test-reporter@^0.4.1; CODECLIMATE_REPO_TOKEN=20d88c0ad0ae53be53f48ef57cfec8ff6d8fc86381f742b50747335f3cebdc69 npm run coverage-ci
10 changes: 4 additions & 6 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,7 @@ function buildType(ref, type) {
type.fieldsArray.forEach(function(field) {
var prop = util.safeProp(field.name);
prop = prop.substring(1, prop.charAt(0) === "[" ? prop.length - 1 : prop.length);
var jsType = toJsType(field);
if (field.optional)
jsType = jsType + "|null";
typeDef.push("@property {" + jsType + "} " + (field.optional ? "[" + prop + "]" : prop) + " " + (field.comment || type.name + " " + field.name));
typeDef.push("@property {" + toJsType(field) + "} " + (field.optional ? "[" + prop + "]" : field.name) + " " + (field.comment || type.name + " " + field.name));
});
push("");
pushComment(typeDef);
Expand All @@ -394,10 +391,10 @@ function buildType(ref, type) {
push("");
var jsType = toJsType(field);
if (field.optional && !field.map && !field.repeated && field.resolvedType instanceof Type)
jsType = jsType + "|null|undefined";
jsType = "(" + jsType + "|undefined)";
pushComment([
field.comment || type.name + " " + field.name + ".",
"@member {" + jsType + "} " + escapeName(field.name),
"@member {" + jsType + "}" + escapeName(field.name),
"@memberof " + exportName(type),
"@instance"
]);
Expand Down Expand Up @@ -658,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
23 changes: 9 additions & 14 deletions dist/light/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/light/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/light/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/light/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/light/protobuf.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/minimal/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/minimal/protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/minimal/protobuf.min.js.gz
Binary file not shown.
23 changes: 9 additions & 14 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nameName?

});

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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protobufjs",
"version": "6.8.1",
"version": "6.8.0",
"versionScheme": "~",
"description": "Protocol Buffers for JavaScript (& TypeScript).",
"author": "Daniel Wirtz <dcode+protobufjs@dcode.io>",
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();
});

});
});
6 changes: 3 additions & 3 deletions tests/data/comments.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as $protobuf from "../..";

export interface ITest1 {
field1?: (string|null);
field2?: (number|null);
field3?: (boolean|null);
field1?: string;
field2?: number;
field3?: boolean;
}

export class Test1 {
Expand Down
12 changes: 6 additions & 6 deletions tests/data/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ $root.Test1 = (function() {
* Properties of a Test1.
* @exports ITest1
* @interface ITest1
* @property {string|null} [field1] Field with a comment.
* @property {number|null} [field2] Test1 field2
* @property {boolean|null} [field3] Field with a comment and a <a href="http://example.com/foo/">link</a>
* @property {string} [field1] Field with a comment.
* @property {number} [field2] Test1 field2
* @property {boolean} [field3] Field with a comment and a <a href="http://example.com/foo/">link</a>
*/

/**
Expand All @@ -39,23 +39,23 @@ $root.Test1 = (function() {

/**
* Field with a comment.
* @member {string} field1
* @member {string}field1
* @memberof Test1
* @instance
*/
Test1.prototype.field1 = "";

/**
* Test1 field2.
* @member {number} field2
* @member {number}field2
* @memberof Test1
* @instance
*/
Test1.prototype.field2 = 0;

/**
* Field with a comment and a <a href="http://example.com/foo/">link</a>
* @member {boolean} field3
* @member {boolean}field3
* @memberof Test1
* @instance
*/
Expand Down
18 changes: 9 additions & 9 deletions tests/data/convert.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as $protobuf from "../..";

export interface IMessage {
stringVal?: (string|null);
stringRepeated?: (string[]|null);
uint64Val?: (number|Long|null);
uint64Repeated?: ((number|Long)[]|null);
bytesVal?: (Uint8Array|null);
bytesRepeated?: (Uint8Array[]|null);
enumVal?: (Message.SomeEnum|null);
enumRepeated?: (Message.SomeEnum[]|null);
int64Map?: ({ [k: string]: (number|Long) }|null);
stringVal?: string;
stringRepeated?: string[];
uint64Val?: (number|Long);
uint64Repeated?: (number|Long)[];
bytesVal?: Uint8Array;
bytesRepeated?: Uint8Array[];
enumVal?: Message.SomeEnum;
enumRepeated?: Message.SomeEnum[];
int64Map?: { [k: string]: (number|Long) };
}

export class Message {
Expand Down
Loading