Răsfoiți Sursa

Merge pull request #9516 from murgatroid99/node_addservice_argument_validation

Node: Validate arguments to addService, fix a couple of minor issues
Michael Lumish 8 ani în urmă
părinte
comite
de3ed4c670
2 a modificat fișierele cu 11 adăugiri și 6 ștergeri
  1. 4 5
      src/node/src/client.js
  2. 7 1
      src/node/src/server.js

+ 4 - 5
src/node/src/client.js

@@ -108,7 +108,7 @@ function _write(chunk, encoding, callback) {
        but passing an object that causes a serialization failure is a misuse
        but passing an object that causes a serialization failure is a misuse
        of the API anyway, so that's OK. The primary purpose here is to give the
        of the API anyway, so that's OK. The primary purpose here is to give the
        programmer a useful error and to stop the stream properly */
        programmer a useful error and to stop the stream properly */
-    this.call.cancelWithStatus(grpc.status.INTERNAL, "Serialization failure");
+    this.call.cancelWithStatus(grpc.status.INTERNAL, 'Serialization failure');
     callback(e);
     callback(e);
   }
   }
   if (_.isFinite(encoding)) {
   if (_.isFinite(encoding)) {
@@ -831,13 +831,12 @@ exports.waitForClientReady = function(client, deadline, callback) {
  */
  */
 exports.makeProtobufClientConstructor =  function(service, options) {
 exports.makeProtobufClientConstructor =  function(service, options) {
   var method_attrs = common.getProtobufServiceAttrs(service, options);
   var method_attrs = common.getProtobufServiceAttrs(service, options);
-  var deprecatedArgumentOrder = false;
-  if (options) {
-    deprecatedArgumentOrder = options.deprecatedArgumentOrder;
+  if (!options) {
+    options = {deprecatedArgumentOrder: false};
   }
   }
   var Client = exports.makeClientConstructor(
   var Client = exports.makeClientConstructor(
       method_attrs, common.fullyQualifiedName(service),
       method_attrs, common.fullyQualifiedName(service),
-      deprecatedArgumentOrder);
+      options);
   Client.service = service;
   Client.service = service;
   Client.service.grpc_options = options;
   Client.service.grpc_options = options;
   return Client;
   return Client;

+ 7 - 1
src/node/src/server.js

@@ -728,11 +728,17 @@ var defaultHandler = {
  *     method implementation for the provided service.
  *     method implementation for the provided service.
  */
  */
 Server.prototype.addService = function(service, implementation) {
 Server.prototype.addService = function(service, implementation) {
+  if (!_.isObjectLike(service) || !_.isObjectLike(implementation)) {
+    throw new Error('addService requires two objects as arguments');
+  }
+  if (_.keys(service).length === 0) {
+    throw new Error('Cannot add an empty service to a server');
+  }
   if (this.started) {
   if (this.started) {
     throw new Error('Can\'t add a service to a started server.');
     throw new Error('Can\'t add a service to a started server.');
   }
   }
   var self = this;
   var self = this;
-  _.each(service, function(attrs, name) {
+  _.forOwn(service, function(attrs, name) {
     var method_type;
     var method_type;
     if (attrs.requestStream) {
     if (attrs.requestStream) {
       if (attrs.responseStream) {
       if (attrs.responseStream) {