mirror of https://github.com/grpc/grpc-node.git
Upgrade Node dependency on Protobuf.js to version 6
This commit is contained in:
parent
de445c94ea
commit
4a9346e332
74
index.js
74
index.js
|
|
@ -52,32 +52,36 @@ var Metadata = require('./src/metadata.js');
|
||||||
|
|
||||||
var grpc = require('./src/grpc_extension');
|
var grpc = require('./src/grpc_extension');
|
||||||
|
|
||||||
|
var protobuf_js_5_common = require('./src/protobuf_js_5_common');
|
||||||
|
var protobuf_js_6_common = require('./src/protobuf_js_6_common');
|
||||||
|
|
||||||
grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii'));
|
grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii'));
|
||||||
|
|
||||||
/**
|
|
||||||
* Load a gRPC object from an existing ProtoBuf.Reflect object.
|
|
||||||
* @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
|
|
||||||
* @param {Object=} options Options to apply to the loaded object
|
|
||||||
* @return {Object<string, *>} The resulting gRPC object
|
|
||||||
*/
|
|
||||||
exports.loadObject = function loadObject(value, options) {
|
exports.loadObject = function loadObject(value, options) {
|
||||||
var result = {};
|
options = _.defaults(options, {'protobufjs_version': 5});
|
||||||
if (value.className === 'Namespace') {
|
switch (options.protobufjs_version) {
|
||||||
_.each(value.children, function(child) {
|
case 5: return protobuf_js_5_common.loadObject(value, options);
|
||||||
result[child.name] = loadObject(child, options);
|
case 6: return protobuf_js_6_common.loadObject(value, options);
|
||||||
});
|
default: throw new Error('Unrecognized protobufjs_version:',
|
||||||
return result;
|
options.protobufjs_version);
|
||||||
} else if (value.className === 'Service') {
|
|
||||||
return client.makeProtobufClientConstructor(value, options);
|
|
||||||
} else if (value.className === 'Message' || value.className === 'Enum') {
|
|
||||||
return value.build();
|
|
||||||
} else {
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var loadObject = exports.loadObject;
|
var loadObject = exports.loadObject;
|
||||||
|
|
||||||
|
function applyProtoRoot(filename, root) {
|
||||||
|
if (_.isString(filename)) {
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
filename.root = path.resolve(filename.root) + '/';
|
||||||
|
root.resolvePath = function(originPath, importPath, alreadyNormalized) {
|
||||||
|
return ProtoBuf.util.path.resolve(filename.root,
|
||||||
|
importPath,
|
||||||
|
alreadyNormalized);
|
||||||
|
};
|
||||||
|
return filename.file;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load a gRPC object from a .proto file. The options object can provide the
|
* Load a gRPC object from a .proto file. The options object can provide the
|
||||||
* following options:
|
* following options:
|
||||||
|
|
@ -99,29 +103,17 @@ var loadObject = exports.loadObject;
|
||||||
* @return {Object<string, *>} The resulting gRPC object
|
* @return {Object<string, *>} The resulting gRPC object
|
||||||
*/
|
*/
|
||||||
exports.load = function load(filename, format, options) {
|
exports.load = function load(filename, format, options) {
|
||||||
if (!format) {
|
/* Note: format is currently unused, because the API for loading a proto
|
||||||
format = 'proto';
|
file or a JSON file is identical in Protobuf.js 6. In the future, there is
|
||||||
}
|
still the possibility of adding other formats that would be loaded
|
||||||
var convertFieldsToCamelCaseOriginal = ProtoBuf.convertFieldsToCamelCase;
|
differently */
|
||||||
if(options && options.hasOwnProperty('convertFieldsToCamelCase')) {
|
options = _.defaults(options, common.defaultGrpcOptions);
|
||||||
ProtoBuf.convertFieldsToCamelCase = options.convertFieldsToCamelCase;
|
options.protobufjs_version = 6;
|
||||||
}
|
var root = new ProtoBuf.Root();
|
||||||
var builder;
|
var parse_options = {keepCase: !options.convertFieldsToCamelCase};
|
||||||
try {
|
return loadObject(root.loadSync(applyProtoRoot(filename, root),
|
||||||
switch(format) {
|
parse_options),
|
||||||
case 'proto':
|
options);
|
||||||
builder = ProtoBuf.loadProtoFile(filename);
|
|
||||||
break;
|
|
||||||
case 'json':
|
|
||||||
builder = ProtoBuf.loadJsonFile(filename);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new Error('Unrecognized format "' + format + '"');
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
ProtoBuf.convertFieldsToCamelCase = convertFieldsToCamelCaseOriginal;
|
|
||||||
}
|
|
||||||
return loadObject(builder.ns, options);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var log_template = _.template(
|
var log_template = _.template(
|
||||||
|
|
|
||||||
|
|
@ -228,7 +228,7 @@ function getServer(port, tls) {
|
||||||
server_creds = grpc.ServerCredentials.createInsecure();
|
server_creds = grpc.ServerCredentials.createInsecure();
|
||||||
}
|
}
|
||||||
var server = new grpc.Server(options);
|
var server = new grpc.Server(options);
|
||||||
server.addProtoService(testProto.TestService.service, {
|
server.addService(testProto.TestService.service, {
|
||||||
emptyCall: handleEmpty,
|
emptyCall: handleEmpty,
|
||||||
unaryCall: handleUnary,
|
unaryCall: handleUnary,
|
||||||
streamingOutputCall: handleStreamingOutput,
|
streamingOutputCall: handleStreamingOutput,
|
||||||
|
|
|
||||||
|
|
@ -132,7 +132,7 @@ function BenchmarkServer(host, port, tls, generic, response_size) {
|
||||||
streamingCall: makeStreamingGenericCall(response_size)
|
streamingCall: makeStreamingGenericCall(response_size)
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
server.addProtoService(serviceProto.BenchmarkService.service, {
|
server.addService(serviceProto.BenchmarkService.service, {
|
||||||
unaryCall: unaryCall,
|
unaryCall: unaryCall,
|
||||||
streamingCall: streamingCall
|
streamingCall: streamingCall
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -44,8 +44,8 @@ var serviceProto = grpc.load({
|
||||||
function runServer(port, benchmark_impl) {
|
function runServer(port, benchmark_impl) {
|
||||||
var server_creds = grpc.ServerCredentials.createInsecure();
|
var server_creds = grpc.ServerCredentials.createInsecure();
|
||||||
var server = new grpc.Server();
|
var server = new grpc.Server();
|
||||||
server.addProtoService(serviceProto.WorkerService.service,
|
server.addService(serviceProto.WorkerService.service,
|
||||||
new WorkerServiceImpl(benchmark_impl, server));
|
new WorkerServiceImpl(benchmark_impl, server));
|
||||||
var address = '0.0.0.0:' + port;
|
var address = '0.0.0.0:' + port;
|
||||||
server.bind(address, server_creds);
|
server.bind(address, server_creds);
|
||||||
server.start();
|
server.start();
|
||||||
|
|
|
||||||
|
|
@ -780,6 +780,8 @@ exports.makeClientConstructor = function(methods, serviceName,
|
||||||
_.assign(Client.prototype[name], attrs);
|
_.assign(Client.prototype[name], attrs);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Client.service = methods;
|
||||||
|
|
||||||
return Client;
|
return Client;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -822,26 +824,6 @@ exports.waitForClientReady = function(client, deadline, callback) {
|
||||||
checkState();
|
checkState();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a constructor for clients for the given service
|
|
||||||
* @param {ProtoBuf.Reflect.Service} service The service to generate a client
|
|
||||||
* for
|
|
||||||
* @param {Object=} options Options to apply to the client
|
|
||||||
* @return {function(string, Object)} New client constructor
|
|
||||||
*/
|
|
||||||
exports.makeProtobufClientConstructor = function(service, options) {
|
|
||||||
var method_attrs = common.getProtobufServiceAttrs(service, options);
|
|
||||||
if (!options) {
|
|
||||||
options = {deprecatedArgumentOrder: false};
|
|
||||||
}
|
|
||||||
var Client = exports.makeClientConstructor(
|
|
||||||
method_attrs, common.fullyQualifiedName(service),
|
|
||||||
options);
|
|
||||||
Client.service = service;
|
|
||||||
Client.service.grpc_options = options;
|
|
||||||
return Client;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map of status code names to status codes
|
* Map of status code names to status codes
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
115
src/common.js
115
src/common.js
|
|
@ -41,74 +41,6 @@
|
||||||
|
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a function that deserializes a specific type of protobuf.
|
|
||||||
* @param {function()} cls The constructor of the message type to deserialize
|
|
||||||
* @param {bool=} binaryAsBase64 Deserialize bytes fields as base64 strings
|
|
||||||
* instead of Buffers. Defaults to false
|
|
||||||
* @param {bool=} longsAsStrings Deserialize long values as strings instead of
|
|
||||||
* objects. Defaults to true
|
|
||||||
* @return {function(Buffer):cls} The deserialization function
|
|
||||||
*/
|
|
||||||
exports.deserializeCls = function deserializeCls(cls, binaryAsBase64,
|
|
||||||
longsAsStrings) {
|
|
||||||
if (binaryAsBase64 === undefined || binaryAsBase64 === null) {
|
|
||||||
binaryAsBase64 = false;
|
|
||||||
}
|
|
||||||
if (longsAsStrings === undefined || longsAsStrings === null) {
|
|
||||||
longsAsStrings = true;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Deserialize a buffer to a message object
|
|
||||||
* @param {Buffer} arg_buf The buffer to deserialize
|
|
||||||
* @return {cls} The resulting object
|
|
||||||
*/
|
|
||||||
return function deserialize(arg_buf) {
|
|
||||||
// Convert to a native object with binary fields as Buffers (first argument)
|
|
||||||
// and longs as strings (second argument)
|
|
||||||
return cls.decode(arg_buf).toRaw(binaryAsBase64, longsAsStrings);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
var deserializeCls = exports.deserializeCls;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a function that serializes objects to a buffer by protobuf class.
|
|
||||||
* @param {function()} Cls The constructor of the message type to serialize
|
|
||||||
* @return {function(Cls):Buffer} The serialization function
|
|
||||||
*/
|
|
||||||
exports.serializeCls = function serializeCls(Cls) {
|
|
||||||
/**
|
|
||||||
* Serialize an object to a Buffer
|
|
||||||
* @param {Object} arg The object to serialize
|
|
||||||
* @return {Buffer} The serialized object
|
|
||||||
*/
|
|
||||||
return function serialize(arg) {
|
|
||||||
return new Buffer(new Cls(arg).encode().toBuffer());
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
var serializeCls = exports.serializeCls;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
|
|
||||||
* @param {ProtoBuf.Reflect.Namespace} value The value to get the name of
|
|
||||||
* @return {string} The fully qualified name of the value
|
|
||||||
*/
|
|
||||||
exports.fullyQualifiedName = function fullyQualifiedName(value) {
|
|
||||||
if (value === null || value === undefined) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
var name = value.name;
|
|
||||||
var parent_name = fullyQualifiedName(value.parent);
|
|
||||||
if (parent_name !== '') {
|
|
||||||
name = parent_name + '.' + name;
|
|
||||||
}
|
|
||||||
return name;
|
|
||||||
};
|
|
||||||
|
|
||||||
var fullyQualifiedName = exports.fullyQualifiedName;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap a function to pass null-like values through without calling it. If no
|
* Wrap a function to pass null-like values through without calling it. If no
|
||||||
* function is given, just uses the identity;
|
* function is given, just uses the identity;
|
||||||
|
|
@ -127,43 +59,6 @@ exports.wrapIgnoreNull = function wrapIgnoreNull(func) {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a map from method names to method attributes for the service.
|
|
||||||
* @param {ProtoBuf.Reflect.Service} service The service to get attributes for
|
|
||||||
* @param {Object=} options Options to apply to these attributes
|
|
||||||
* @return {Object} The attributes map
|
|
||||||
*/
|
|
||||||
exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service,
|
|
||||||
options) {
|
|
||||||
var prefix = '/' + fullyQualifiedName(service) + '/';
|
|
||||||
var binaryAsBase64, longsAsStrings;
|
|
||||||
if (options) {
|
|
||||||
binaryAsBase64 = options.binaryAsBase64;
|
|
||||||
longsAsStrings = options.longsAsStrings;
|
|
||||||
}
|
|
||||||
/* This slightly awkward construction is used to make sure we only use
|
|
||||||
lodash@3.10.1-compatible functions. A previous version used
|
|
||||||
_.fromPairs, which would be cleaner, but was introduced in lodash
|
|
||||||
version 4 */
|
|
||||||
return _.zipObject(_.map(service.children, function(method) {
|
|
||||||
return _.camelCase(method.name);
|
|
||||||
}), _.map(service.children, function(method) {
|
|
||||||
return {
|
|
||||||
path: prefix + method.name,
|
|
||||||
requestStream: method.requestStream,
|
|
||||||
responseStream: method.responseStream,
|
|
||||||
requestType: method.resolvedRequestType,
|
|
||||||
responseType: method.resolvedResponseType,
|
|
||||||
requestSerialize: serializeCls(method.resolvedRequestType.build()),
|
|
||||||
requestDeserialize: deserializeCls(method.resolvedRequestType.build(),
|
|
||||||
binaryAsBase64, longsAsStrings),
|
|
||||||
responseSerialize: serializeCls(method.resolvedResponseType.build()),
|
|
||||||
responseDeserialize: deserializeCls(method.resolvedResponseType.build(),
|
|
||||||
binaryAsBase64, longsAsStrings)
|
|
||||||
};
|
|
||||||
}));
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The logger object for the gRPC module. Defaults to console.
|
* The logger object for the gRPC module. Defaults to console.
|
||||||
*/
|
*/
|
||||||
|
|
@ -184,3 +79,13 @@ exports.log = function log(severity, message) {
|
||||||
exports.logger.error(message);
|
exports.logger.error(message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default options for loading proto files into gRPC
|
||||||
|
*/
|
||||||
|
exports.defaultGrpcOptions = {
|
||||||
|
convertFieldsToCamelCase: false,
|
||||||
|
binaryAsBase64: false,
|
||||||
|
longsAsStrings: true,
|
||||||
|
deprecatedArgumentOrder: false
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,167 @@
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Copyright 2017, Google Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are
|
||||||
|
* met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above
|
||||||
|
* copyright notice, this list of conditions and the following disclaimer
|
||||||
|
* in the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
* * Neither the name of Google Inc. nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var _ = require('lodash');
|
||||||
|
var client = require('./client');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a function that deserializes a specific type of protobuf.
|
||||||
|
* @param {function()} cls The constructor of the message type to deserialize
|
||||||
|
* @param {bool=} binaryAsBase64 Deserialize bytes fields as base64 strings
|
||||||
|
* instead of Buffers. Defaults to false
|
||||||
|
* @param {bool=} longsAsStrings Deserialize long values as strings instead of
|
||||||
|
* objects. Defaults to true
|
||||||
|
* @return {function(Buffer):cls} The deserialization function
|
||||||
|
*/
|
||||||
|
exports.deserializeCls = function deserializeCls(cls, binaryAsBase64,
|
||||||
|
longsAsStrings) {
|
||||||
|
/**
|
||||||
|
* Deserialize a buffer to a message object
|
||||||
|
* @param {Buffer} arg_buf The buffer to deserialize
|
||||||
|
* @return {cls} The resulting object
|
||||||
|
*/
|
||||||
|
return function deserialize(arg_buf) {
|
||||||
|
// Convert to a native object with binary fields as Buffers (first argument)
|
||||||
|
// and longs as strings (second argument)
|
||||||
|
return cls.decode(arg_buf).toRaw(binaryAsBase64, longsAsStrings);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
var deserializeCls = exports.deserializeCls;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a function that serializes objects to a buffer by protobuf class.
|
||||||
|
* @param {function()} Cls The constructor of the message type to serialize
|
||||||
|
* @return {function(Cls):Buffer} The serialization function
|
||||||
|
*/
|
||||||
|
exports.serializeCls = function serializeCls(Cls) {
|
||||||
|
/**
|
||||||
|
* Serialize an object to a Buffer
|
||||||
|
* @param {Object} arg The object to serialize
|
||||||
|
* @return {Buffer} The serialized object
|
||||||
|
*/
|
||||||
|
return function serialize(arg) {
|
||||||
|
return new Buffer(new Cls(arg).encode().toBuffer());
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
var serializeCls = exports.serializeCls;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
|
||||||
|
* @param {ProtoBuf.Reflect.Namespace} value The value to get the name of
|
||||||
|
* @return {string} The fully qualified name of the value
|
||||||
|
*/
|
||||||
|
exports.fullyQualifiedName = function fullyQualifiedName(value) {
|
||||||
|
if (value === null || value === undefined) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
var name = value.name;
|
||||||
|
var parent_name = fullyQualifiedName(value.parent);
|
||||||
|
if (parent_name !== '') {
|
||||||
|
name = parent_name + '.' + name;
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
};
|
||||||
|
|
||||||
|
var fullyQualifiedName = exports.fullyQualifiedName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a map from method names to method attributes for the service.
|
||||||
|
* @param {ProtoBuf.Reflect.Service} service The service to get attributes for
|
||||||
|
* @param {Object=} options Options to apply to these attributes
|
||||||
|
* @return {Object} The attributes map
|
||||||
|
*/
|
||||||
|
exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service,
|
||||||
|
options) {
|
||||||
|
var prefix = '/' + fullyQualifiedName(service) + '/';
|
||||||
|
var binaryAsBase64, longsAsStrings;
|
||||||
|
if (options) {
|
||||||
|
binaryAsBase64 = options.binaryAsBase64;
|
||||||
|
longsAsStrings = options.longsAsStrings;
|
||||||
|
}
|
||||||
|
/* This slightly awkward construction is used to make sure we only use
|
||||||
|
lodash@3.10.1-compatible functions. A previous version used
|
||||||
|
_.fromPairs, which would be cleaner, but was introduced in lodash
|
||||||
|
version 4 */
|
||||||
|
return _.zipObject(_.map(service.children, function(method) {
|
||||||
|
return _.camelCase(method.name);
|
||||||
|
}), _.map(service.children, function(method) {
|
||||||
|
return {
|
||||||
|
path: prefix + method.name,
|
||||||
|
requestStream: method.requestStream,
|
||||||
|
responseStream: method.responseStream,
|
||||||
|
requestType: method.resolvedRequestType,
|
||||||
|
responseType: method.resolvedResponseType,
|
||||||
|
requestSerialize: serializeCls(method.resolvedRequestType.build()),
|
||||||
|
requestDeserialize: deserializeCls(method.resolvedRequestType.build(),
|
||||||
|
binaryAsBase64, longsAsStrings),
|
||||||
|
responseSerialize: serializeCls(method.resolvedResponseType.build()),
|
||||||
|
responseDeserialize: deserializeCls(method.resolvedResponseType.build(),
|
||||||
|
binaryAsBase64, longsAsStrings)
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
var getProtobufServiceAttrs = exports.getProtobufServiceAttrs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load a gRPC object from an existing ProtoBuf.Reflect object.
|
||||||
|
* @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load.
|
||||||
|
* @param {Object=} options Options to apply to the loaded object
|
||||||
|
* @return {Object<string, *>} The resulting gRPC object
|
||||||
|
*/
|
||||||
|
exports.loadObject = function loadObject(value, options) {
|
||||||
|
var result = {};
|
||||||
|
if (!value) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
if (value.hasOwnProperty('ns')) {
|
||||||
|
return loadObject(value.ns, options);
|
||||||
|
}
|
||||||
|
if (value.className === 'Namespace') {
|
||||||
|
_.each(value.children, function(child) {
|
||||||
|
result[child.name] = loadObject(child, options);
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
} else if (value.className === 'Service') {
|
||||||
|
return client.makeClientConstructor(getProtobufServiceAttrs(value, options),
|
||||||
|
options);
|
||||||
|
} else if (value.className === 'Message' || value.className === 'Enum') {
|
||||||
|
return value.build();
|
||||||
|
} else {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,154 @@
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Copyright 2017, Google Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are
|
||||||
|
* met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above
|
||||||
|
* copyright notice, this list of conditions and the following disclaimer
|
||||||
|
* in the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
* * Neither the name of Google Inc. nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var _ = require('lodash');
|
||||||
|
var client = require('./client');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a function that deserializes a specific type of protobuf.
|
||||||
|
* @param {function()} cls The constructor of the message type to deserialize
|
||||||
|
* @param {bool=} binaryAsBase64 Deserialize bytes fields as base64 strings
|
||||||
|
* instead of Buffers. Defaults to false
|
||||||
|
* @param {bool=} longsAsStrings Deserialize long values as strings instead of
|
||||||
|
* objects. Defaults to true
|
||||||
|
* @return {function(Buffer):cls} The deserialization function
|
||||||
|
*/
|
||||||
|
exports.deserializeCls = function deserializeCls(cls, options) {
|
||||||
|
var conversion_options = {
|
||||||
|
defaults: true,
|
||||||
|
bytes: options.binaryAsBase64 ? String : Buffer,
|
||||||
|
longs: options.longsAsStrings ? String : null,
|
||||||
|
enums: String
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* Deserialize a buffer to a message object
|
||||||
|
* @param {Buffer} arg_buf The buffer to deserialize
|
||||||
|
* @return {cls} The resulting object
|
||||||
|
*/
|
||||||
|
return function deserialize(arg_buf) {
|
||||||
|
return cls.decode(arg_buf).toObject(conversion_options);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
var deserializeCls = exports.deserializeCls;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a function that serializes objects to a buffer by protobuf class.
|
||||||
|
* @param {function()} Cls The constructor of the message type to serialize
|
||||||
|
* @return {function(Cls):Buffer} The serialization function
|
||||||
|
*/
|
||||||
|
exports.serializeCls = function serializeCls(cls) {
|
||||||
|
/**
|
||||||
|
* Serialize an object to a Buffer
|
||||||
|
* @param {Object} arg The object to serialize
|
||||||
|
* @return {Buffer} The serialized object
|
||||||
|
*/
|
||||||
|
return function serialize(arg) {
|
||||||
|
return cls.encode(arg).finish();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
var serializeCls = exports.serializeCls;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the fully qualified (dotted) name of a ProtoBuf.Reflect value.
|
||||||
|
* @param {ProtoBuf.ReflectionObject} value The value to get the name of
|
||||||
|
* @return {string} The fully qualified name of the value
|
||||||
|
*/
|
||||||
|
exports.fullyQualifiedName = function fullyQualifiedName(value) {
|
||||||
|
if (value === null || value === undefined) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
var name = value.name;
|
||||||
|
var parent_fqn = fullyQualifiedName(value.parent);
|
||||||
|
if (parent_fqn !== '') {
|
||||||
|
name = parent_fqn + '.' + name;
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
};
|
||||||
|
|
||||||
|
var fullyQualifiedName = exports.fullyQualifiedName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a map from method names to method attributes for the service.
|
||||||
|
* @param {ProtoBuf.Service} service The service to get attributes for
|
||||||
|
* @param {Object=} options Options to apply to these attributes
|
||||||
|
* @return {Object} The attributes map
|
||||||
|
*/
|
||||||
|
exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service,
|
||||||
|
options) {
|
||||||
|
var prefix = '/' + fullyQualifiedName(service) + '/';
|
||||||
|
service.resolveAll();
|
||||||
|
return _.zipObject(_.map(service.methods, function(method) {
|
||||||
|
return _.camelCase(method.name);
|
||||||
|
}), _.map(service.methods, function(method) {
|
||||||
|
return {
|
||||||
|
path: prefix + method.name,
|
||||||
|
requestStream: !!method.requestStream,
|
||||||
|
responseStream: !!method.responseStream,
|
||||||
|
requestType: method.resolvedRequestType,
|
||||||
|
responseType: method.resolvedResponseType,
|
||||||
|
requestSerialize: serializeCls(method.resolvedRequestType),
|
||||||
|
requestDeserialize: deserializeCls(method.resolvedRequestType, options),
|
||||||
|
responseSerialize: serializeCls(method.resolvedResponseType),
|
||||||
|
responseDeserialize: deserializeCls(method.resolvedResponseType, options)
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
var getProtobufServiceAttrs = exports.getProtobufServiceAttrs;
|
||||||
|
|
||||||
|
exports.loadObject = function loadObject(value, options) {
|
||||||
|
var result = {};
|
||||||
|
if (!value) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
if (value.hasOwnProperty('methods')) {
|
||||||
|
// It's a service object
|
||||||
|
var service_attrs = getProtobufServiceAttrs(value, options);
|
||||||
|
return client.makeClientConstructor(service_attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.hasOwnProperty('nested')) {
|
||||||
|
// It's a namespace or root object
|
||||||
|
_.each(value.nested, function(nested, name) {
|
||||||
|
result[name] = loadObject(nested, options);
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, it's not something we need to change
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|
@ -774,17 +774,33 @@ Server.prototype.addService = function(service, implementation) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a proto service to the server, with a corresponding implementation
|
* Add a proto service to the server, with a corresponding implementation
|
||||||
|
* @deprecated Use grpc.load and Server#addService instead
|
||||||
* @param {Protobuf.Reflect.Service} service The proto service descriptor
|
* @param {Protobuf.Reflect.Service} service The proto service descriptor
|
||||||
* @param {Object<String, function>} implementation Map of method names to
|
* @param {Object<String, function>} implementation Map of method names to
|
||||||
* method implementation for the provided service.
|
* method implementation for the provided service.
|
||||||
*/
|
*/
|
||||||
Server.prototype.addProtoService = function(service, implementation) {
|
Server.prototype.addProtoService = function(service, implementation) {
|
||||||
var options;
|
var options;
|
||||||
if (service.grpc_options) {
|
common.log(grpc.logVerbosity.INFO,
|
||||||
options = service.grpc_options;
|
'Server#addProtoService is deprecated. Use addService instead');
|
||||||
|
if (service.hasOwnProperty('children')) {
|
||||||
|
// Heuristically: this is a Protobuf.js 5 service object
|
||||||
|
var protobuf_js_5_common = require('./protobuf_js_5_common');
|
||||||
|
options = _.defaults(service.grpc_options, common.defaultGrpcOptions);
|
||||||
|
this.addService(
|
||||||
|
protobuf_js_5_common.getProtobufServiceAttrs(service, options),
|
||||||
|
implementation);
|
||||||
|
} else if (service.hasOwnProperty('methods')) {
|
||||||
|
// Heuristically: this is a Protobuf.js 6 service object
|
||||||
|
var protobuf_js_6_common = require('./protobuf_js_6_common');
|
||||||
|
options = _.defaults(service.grpc_options, common.defaultGrpcOptions);
|
||||||
|
this.addService(
|
||||||
|
protobuf_js_6_common.getProtobufServiceAttrs(service, options),
|
||||||
|
implementation);
|
||||||
|
} else {
|
||||||
|
// We assume that this is a service attributes object
|
||||||
|
this.addService(service, implementation);
|
||||||
}
|
}
|
||||||
this.addService(common.getProtobufServiceAttrs(service, options),
|
|
||||||
implementation);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ function getAllGauges(call) {
|
||||||
|
|
||||||
function MetricsServer(port) {
|
function MetricsServer(port) {
|
||||||
var server = new grpc.Server();
|
var server = new grpc.Server();
|
||||||
server.addProtoService(metrics.MetricsService.service, {
|
server.addService(metrics.MetricsService.service, {
|
||||||
getGauge: _.bind(getGauge, this),
|
getGauge: _.bind(getGauge, this),
|
||||||
getAllGauges: _.bind(getAllGauges, this)
|
getAllGauges: _.bind(getAllGauges, this)
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -34,17 +34,26 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
|
var _ = require('lodash');
|
||||||
|
|
||||||
var common = require('../src/common.js');
|
var common = require('../src/common');
|
||||||
|
var protobuf_js_6_common = require('../src/protobuf_js_6_common');
|
||||||
|
|
||||||
|
var serializeCls = protobuf_js_6_common.serializeCls;
|
||||||
|
var deserializeCls = protobuf_js_6_common.deserializeCls;
|
||||||
|
|
||||||
var ProtoBuf = require('protobufjs');
|
var ProtoBuf = require('protobufjs');
|
||||||
|
|
||||||
var messages_proto = ProtoBuf.loadProtoFile(
|
var messages_proto = new ProtoBuf.Root();
|
||||||
__dirname + '/test_messages.proto').build();
|
messages_proto = messages_proto.loadSync(
|
||||||
|
__dirname + '/test_messages.proto', {keepCase: true}).resolveAll();
|
||||||
|
|
||||||
|
var default_options = common.defaultGrpcOptions;
|
||||||
|
|
||||||
describe('Proto message long int serialize and deserialize', function() {
|
describe('Proto message long int serialize and deserialize', function() {
|
||||||
var longSerialize = common.serializeCls(messages_proto.LongValues);
|
var longSerialize = serializeCls(messages_proto.LongValues);
|
||||||
var longDeserialize = common.deserializeCls(messages_proto.LongValues);
|
var longDeserialize = deserializeCls(messages_proto.LongValues,
|
||||||
|
default_options);
|
||||||
var pos_value = '314159265358979';
|
var pos_value = '314159265358979';
|
||||||
var neg_value = '-27182818284590';
|
var neg_value = '-27182818284590';
|
||||||
it('should preserve positive int64 values', function() {
|
it('should preserve positive int64 values', function() {
|
||||||
|
|
@ -88,8 +97,9 @@ describe('Proto message long int serialize and deserialize', function() {
|
||||||
neg_value);
|
neg_value);
|
||||||
});
|
});
|
||||||
it('should deserialize as a number with the right option set', function() {
|
it('should deserialize as a number with the right option set', function() {
|
||||||
var longNumDeserialize = common.deserializeCls(messages_proto.LongValues,
|
var num_options = _.defaults({longsAsStrings: false}, default_options);
|
||||||
false, false);
|
var longNumDeserialize = deserializeCls(messages_proto.LongValues,
|
||||||
|
num_options);
|
||||||
var serialized = longSerialize({int_64: pos_value});
|
var serialized = longSerialize({int_64: pos_value});
|
||||||
assert.strictEqual(typeof longDeserialize(serialized).int_64, 'string');
|
assert.strictEqual(typeof longDeserialize(serialized).int_64, 'string');
|
||||||
/* With the longsAsStrings option disabled, long values are represented as
|
/* With the longsAsStrings option disabled, long values are represented as
|
||||||
|
|
@ -98,11 +108,12 @@ describe('Proto message long int serialize and deserialize', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('Proto message bytes serialize and deserialize', function() {
|
describe('Proto message bytes serialize and deserialize', function() {
|
||||||
var sequenceSerialize = common.serializeCls(messages_proto.SequenceValues);
|
var sequenceSerialize = serializeCls(messages_proto.SequenceValues);
|
||||||
var sequenceDeserialize = common.deserializeCls(
|
var sequenceDeserialize = deserializeCls(
|
||||||
messages_proto.SequenceValues);
|
messages_proto.SequenceValues, default_options);
|
||||||
var sequenceBase64Deserialize = common.deserializeCls(
|
var b64_options = _.defaults({binaryAsBase64: true}, default_options);
|
||||||
messages_proto.SequenceValues, true);
|
var sequenceBase64Deserialize = deserializeCls(
|
||||||
|
messages_proto.SequenceValues, b64_options);
|
||||||
var buffer_val = new Buffer([0x69, 0xb7]);
|
var buffer_val = new Buffer([0x69, 0xb7]);
|
||||||
var base64_val = 'abc=';
|
var base64_val = 'abc=';
|
||||||
it('should preserve a buffer', function() {
|
it('should preserve a buffer', function() {
|
||||||
|
|
|
||||||
|
|
@ -228,7 +228,7 @@ describe('client credentials', function() {
|
||||||
before(function() {
|
before(function() {
|
||||||
var proto = grpc.load(__dirname + '/test_service.proto');
|
var proto = grpc.load(__dirname + '/test_service.proto');
|
||||||
server = new grpc.Server();
|
server = new grpc.Server();
|
||||||
server.addProtoService(proto.TestService.service, {
|
server.addService(proto.TestService.service, {
|
||||||
unary: function(call, cb) {
|
unary: function(call, cb) {
|
||||||
call.sendMetadata(call.metadata);
|
call.sendMetadata(call.metadata);
|
||||||
cb(null, {});
|
cb(null, {});
|
||||||
|
|
|
||||||
|
|
@ -34,19 +34,23 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
|
var _ = require('lodash');
|
||||||
|
|
||||||
var surface_client = require('../src/client.js');
|
var surface_client = require('../src/client.js');
|
||||||
|
var common = require('../src/common');
|
||||||
|
|
||||||
var ProtoBuf = require('protobufjs');
|
var ProtoBuf = require('protobufjs');
|
||||||
|
|
||||||
var grpc = require('..');
|
var grpc = require('..');
|
||||||
|
|
||||||
var math_proto = ProtoBuf.loadProtoFile(__dirname +
|
var math_proto = new ProtoBuf.Root();
|
||||||
'/../../proto/math/math.proto');
|
math_proto = math_proto.loadSync(__dirname +
|
||||||
|
'/../../proto/math/math.proto', {keepCase: true});
|
||||||
|
|
||||||
var mathService = math_proto.lookup('math.Math');
|
var mathService = math_proto.lookup('math.Math');
|
||||||
|
var mathServiceAttrs = grpc.loadObject(
|
||||||
var _ = require('lodash');
|
mathService,
|
||||||
|
_.defaults({protobufjs_version: 6}, common.defaultGrpcOptions)).service;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is used for testing functions with multiple asynchronous calls that
|
* This is used for testing functions with multiple asynchronous calls that
|
||||||
|
|
@ -87,11 +91,6 @@ describe('File loader', function() {
|
||||||
grpc.load(__dirname + '/test_service.json', 'json');
|
grpc.load(__dirname + '/test_service.json', 'json');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('Should fail to load a file with an unknown format', function() {
|
|
||||||
assert.throws(function() {
|
|
||||||
grpc.load(__dirname + '/test_service.proto', 'fake_format');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
describe('surface Server', function() {
|
describe('surface Server', function() {
|
||||||
var server;
|
var server;
|
||||||
|
|
@ -132,29 +131,54 @@ describe('Server.prototype.addProtoService', function() {
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
server.forceShutdown();
|
server.forceShutdown();
|
||||||
});
|
});
|
||||||
it('Should succeed with a single service', function() {
|
it('Should succeed with a single proto service', function() {
|
||||||
assert.doesNotThrow(function() {
|
assert.doesNotThrow(function() {
|
||||||
server.addProtoService(mathService, dummyImpls);
|
server.addProtoService(mathService, dummyImpls);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
it('Should succeed with a single service attributes object', function() {
|
||||||
|
assert.doesNotThrow(function() {
|
||||||
|
server.addProtoService(mathServiceAttrs, dummyImpls);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('Server.prototype.addService', function() {
|
||||||
|
var server;
|
||||||
|
var dummyImpls = {
|
||||||
|
'div': function() {},
|
||||||
|
'divMany': function() {},
|
||||||
|
'fib': function() {},
|
||||||
|
'sum': function() {}
|
||||||
|
};
|
||||||
|
beforeEach(function() {
|
||||||
|
server = new grpc.Server();
|
||||||
|
});
|
||||||
|
afterEach(function() {
|
||||||
|
server.forceShutdown();
|
||||||
|
});
|
||||||
|
it('Should succeed with a single service', function() {
|
||||||
|
assert.doesNotThrow(function() {
|
||||||
|
server.addService(mathServiceAttrs, dummyImpls);
|
||||||
|
});
|
||||||
|
});
|
||||||
it('Should fail with conflicting method names', function() {
|
it('Should fail with conflicting method names', function() {
|
||||||
server.addProtoService(mathService, dummyImpls);
|
server.addService(mathServiceAttrs, dummyImpls);
|
||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
server.addProtoService(mathService, dummyImpls);
|
server.addService(mathServiceAttrs, dummyImpls);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('Should fail if the server has been started', function() {
|
it('Should fail if the server has been started', function() {
|
||||||
server.start();
|
server.start();
|
||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
server.addProtoService(mathService, dummyImpls);
|
server.addService(mathServiceAttrs, dummyImpls);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('Default handlers', function() {
|
describe('Default handlers', function() {
|
||||||
var client;
|
var client;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
server.addProtoService(mathService, {});
|
server.addService(mathServiceAttrs, {});
|
||||||
var port = server.bind('localhost:0', server_insecure_creds);
|
var port = server.bind('localhost:0', server_insecure_creds);
|
||||||
var Client = surface_client.makeProtobufClientConstructor(mathService);
|
var Client = grpc.loadObject(mathService, {protobufjs_version: 6});
|
||||||
client = new Client('localhost:' + port,
|
client = new Client('localhost:' + port,
|
||||||
grpc.credentials.createInsecure());
|
grpc.credentials.createInsecure());
|
||||||
server.start();
|
server.start();
|
||||||
|
|
@ -226,7 +250,7 @@ describe('waitForClientReady', function() {
|
||||||
server = new grpc.Server();
|
server = new grpc.Server();
|
||||||
port = server.bind('localhost:0', grpc.ServerCredentials.createInsecure());
|
port = server.bind('localhost:0', grpc.ServerCredentials.createInsecure());
|
||||||
server.start();
|
server.start();
|
||||||
Client = surface_client.makeProtobufClientConstructor(mathService);
|
Client = grpc.loadObject(mathService, {protobufjs_version: 6});
|
||||||
});
|
});
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
||||||
|
|
@ -283,16 +307,18 @@ describe('Echo service', function() {
|
||||||
var server;
|
var server;
|
||||||
var client;
|
var client;
|
||||||
before(function() {
|
before(function() {
|
||||||
var test_proto = ProtoBuf.loadProtoFile(__dirname + '/echo_service.proto');
|
var test_proto = new ProtoBuf.Root();
|
||||||
|
test_proto = test_proto.loadSync(__dirname + '/echo_service.proto',
|
||||||
|
{keepCase: true});
|
||||||
var echo_service = test_proto.lookup('EchoService');
|
var echo_service = test_proto.lookup('EchoService');
|
||||||
|
var Client = grpc.loadObject(echo_service, {protobufjs_version: 6});
|
||||||
server = new grpc.Server();
|
server = new grpc.Server();
|
||||||
server.addProtoService(echo_service, {
|
server.addService(Client.service, {
|
||||||
echo: function(call, callback) {
|
echo: function(call, callback) {
|
||||||
callback(null, call.request);
|
callback(null, call.request);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
var port = server.bind('localhost:0', server_insecure_creds);
|
var port = server.bind('localhost:0', server_insecure_creds);
|
||||||
var Client = surface_client.makeProtobufClientConstructor(echo_service);
|
|
||||||
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
||||||
server.start();
|
server.start();
|
||||||
});
|
});
|
||||||
|
|
@ -406,10 +432,13 @@ describe('Echo metadata', function() {
|
||||||
var server;
|
var server;
|
||||||
var metadata;
|
var metadata;
|
||||||
before(function() {
|
before(function() {
|
||||||
var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
|
var test_proto = new ProtoBuf.Root();
|
||||||
|
test_proto = test_proto.loadSync(__dirname + '/test_service.proto',
|
||||||
|
{keepCase: true});
|
||||||
var test_service = test_proto.lookup('TestService');
|
var test_service = test_proto.lookup('TestService');
|
||||||
|
var Client = grpc.loadObject(test_service, {protobufjs_version: 6});
|
||||||
server = new grpc.Server();
|
server = new grpc.Server();
|
||||||
server.addProtoService(test_service, {
|
server.addService(Client.service, {
|
||||||
unary: function(call, cb) {
|
unary: function(call, cb) {
|
||||||
call.sendMetadata(call.metadata);
|
call.sendMetadata(call.metadata);
|
||||||
cb(null, {});
|
cb(null, {});
|
||||||
|
|
@ -434,7 +463,6 @@ describe('Echo metadata', function() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
var port = server.bind('localhost:0', server_insecure_creds);
|
var port = server.bind('localhost:0', server_insecure_creds);
|
||||||
var Client = surface_client.makeProtobufClientConstructor(test_service);
|
|
||||||
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
||||||
server.start();
|
server.start();
|
||||||
metadata = new grpc.Metadata();
|
metadata = new grpc.Metadata();
|
||||||
|
|
@ -507,7 +535,9 @@ describe('Client malformed response handling', function() {
|
||||||
var client;
|
var client;
|
||||||
var badArg = new Buffer([0xFF]);
|
var badArg = new Buffer([0xFF]);
|
||||||
before(function() {
|
before(function() {
|
||||||
var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
|
var test_proto = new ProtoBuf.Root();
|
||||||
|
test_proto = test_proto.loadSync(__dirname + '/test_service.proto',
|
||||||
|
{keepCase: true});
|
||||||
var test_service = test_proto.lookup('TestService');
|
var test_service = test_proto.lookup('TestService');
|
||||||
var malformed_test_service = {
|
var malformed_test_service = {
|
||||||
unary: {
|
unary: {
|
||||||
|
|
@ -565,7 +595,7 @@ describe('Client malformed response handling', function() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
var port = server.bind('localhost:0', server_insecure_creds);
|
var port = server.bind('localhost:0', server_insecure_creds);
|
||||||
var Client = surface_client.makeProtobufClientConstructor(test_service);
|
var Client = grpc.loadObject(test_service, {protobufjs_version: 6});
|
||||||
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
||||||
server.start();
|
server.start();
|
||||||
});
|
});
|
||||||
|
|
@ -614,7 +644,9 @@ describe('Server serialization failure handling', function() {
|
||||||
var client;
|
var client;
|
||||||
var server;
|
var server;
|
||||||
before(function() {
|
before(function() {
|
||||||
var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
|
var test_proto = new ProtoBuf.Root();
|
||||||
|
test_proto = test_proto.loadSync(__dirname + '/test_service.proto',
|
||||||
|
{keepCase: true});
|
||||||
var test_service = test_proto.lookup('TestService');
|
var test_service = test_proto.lookup('TestService');
|
||||||
var malformed_test_service = {
|
var malformed_test_service = {
|
||||||
unary: {
|
unary: {
|
||||||
|
|
@ -672,7 +704,7 @@ describe('Server serialization failure handling', function() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
var port = server.bind('localhost:0', server_insecure_creds);
|
var port = server.bind('localhost:0', server_insecure_creds);
|
||||||
var Client = surface_client.makeProtobufClientConstructor(test_service);
|
var Client = grpc.loadObject(test_service, {protobufjs_version: 6});
|
||||||
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
||||||
server.start();
|
server.start();
|
||||||
});
|
});
|
||||||
|
|
@ -721,12 +753,15 @@ describe('Other conditions', function() {
|
||||||
var server;
|
var server;
|
||||||
var port;
|
var port;
|
||||||
before(function() {
|
before(function() {
|
||||||
var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
|
var test_proto = new ProtoBuf.Root();
|
||||||
|
test_proto = test_proto.loadSync(__dirname + '/test_service.proto',
|
||||||
|
{keepCase: true});
|
||||||
test_service = test_proto.lookup('TestService');
|
test_service = test_proto.lookup('TestService');
|
||||||
|
Client = grpc.loadObject(test_service, {protobufjs_version: 6});
|
||||||
server = new grpc.Server();
|
server = new grpc.Server();
|
||||||
var trailer_metadata = new grpc.Metadata();
|
var trailer_metadata = new grpc.Metadata();
|
||||||
trailer_metadata.add('trailer-present', 'yes');
|
trailer_metadata.add('trailer-present', 'yes');
|
||||||
server.addProtoService(test_service, {
|
server.addService(Client.service, {
|
||||||
unary: function(call, cb) {
|
unary: function(call, cb) {
|
||||||
var req = call.request;
|
var req = call.request;
|
||||||
if (req.error) {
|
if (req.error) {
|
||||||
|
|
@ -786,7 +821,6 @@ describe('Other conditions', function() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
port = server.bind('localhost:0', server_insecure_creds);
|
port = server.bind('localhost:0', server_insecure_creds);
|
||||||
Client = surface_client.makeProtobufClientConstructor(test_service);
|
|
||||||
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
||||||
server.start();
|
server.start();
|
||||||
});
|
});
|
||||||
|
|
@ -1067,17 +1101,19 @@ describe('Call propagation', function() {
|
||||||
var client;
|
var client;
|
||||||
var server;
|
var server;
|
||||||
before(function() {
|
before(function() {
|
||||||
var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
|
var test_proto = new ProtoBuf.Root();
|
||||||
|
test_proto = test_proto.loadSync(__dirname + '/test_service.proto',
|
||||||
|
{keepCase: true});
|
||||||
test_service = test_proto.lookup('TestService');
|
test_service = test_proto.lookup('TestService');
|
||||||
server = new grpc.Server();
|
server = new grpc.Server();
|
||||||
server.addProtoService(test_service, {
|
Client = grpc.loadObject(test_service, {protobufjs_version: 6});
|
||||||
|
server.addService(Client.service, {
|
||||||
unary: function(call) {},
|
unary: function(call) {},
|
||||||
clientStream: function(stream) {},
|
clientStream: function(stream) {},
|
||||||
serverStream: function(stream) {},
|
serverStream: function(stream) {},
|
||||||
bidiStream: function(stream) {}
|
bidiStream: function(stream) {}
|
||||||
});
|
});
|
||||||
var port = server.bind('localhost:0', server_insecure_creds);
|
var port = server.bind('localhost:0', server_insecure_creds);
|
||||||
Client = surface_client.makeProtobufClientConstructor(test_service);
|
|
||||||
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
||||||
server.start();
|
server.start();
|
||||||
});
|
});
|
||||||
|
|
@ -1112,7 +1148,7 @@ describe('Call propagation', function() {
|
||||||
});
|
});
|
||||||
call.cancel();
|
call.cancel();
|
||||||
};
|
};
|
||||||
proxy.addProtoService(test_service, proxy_impl);
|
proxy.addService(Client.service, proxy_impl);
|
||||||
var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
|
var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
|
||||||
proxy.start();
|
proxy.start();
|
||||||
var proxy_client = new Client('localhost:' + proxy_port,
|
var proxy_client = new Client('localhost:' + proxy_port,
|
||||||
|
|
@ -1134,7 +1170,7 @@ describe('Call propagation', function() {
|
||||||
});
|
});
|
||||||
call.cancel();
|
call.cancel();
|
||||||
};
|
};
|
||||||
proxy.addProtoService(test_service, proxy_impl);
|
proxy.addService(Client.service, proxy_impl);
|
||||||
var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
|
var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
|
||||||
proxy.start();
|
proxy.start();
|
||||||
var proxy_client = new Client('localhost:' + proxy_port,
|
var proxy_client = new Client('localhost:' + proxy_port,
|
||||||
|
|
@ -1154,7 +1190,7 @@ describe('Call propagation', function() {
|
||||||
});
|
});
|
||||||
call.cancel();
|
call.cancel();
|
||||||
};
|
};
|
||||||
proxy.addProtoService(test_service, proxy_impl);
|
proxy.addService(Client.service, proxy_impl);
|
||||||
var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
|
var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
|
||||||
proxy.start();
|
proxy.start();
|
||||||
var proxy_client = new Client('localhost:' + proxy_port,
|
var proxy_client = new Client('localhost:' + proxy_port,
|
||||||
|
|
@ -1178,7 +1214,7 @@ describe('Call propagation', function() {
|
||||||
});
|
});
|
||||||
call.cancel();
|
call.cancel();
|
||||||
};
|
};
|
||||||
proxy.addProtoService(test_service, proxy_impl);
|
proxy.addService(Client.service, proxy_impl);
|
||||||
var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
|
var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
|
||||||
proxy.start();
|
proxy.start();
|
||||||
var proxy_client = new Client('localhost:' + proxy_port,
|
var proxy_client = new Client('localhost:' + proxy_port,
|
||||||
|
|
@ -1209,7 +1245,7 @@ describe('Call propagation', function() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
proxy.addProtoService(test_service, proxy_impl);
|
proxy.addService(Client.service, proxy_impl);
|
||||||
var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
|
var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
|
||||||
proxy.start();
|
proxy.start();
|
||||||
var proxy_client = new Client('localhost:' + proxy_port,
|
var proxy_client = new Client('localhost:' + proxy_port,
|
||||||
|
|
@ -1233,7 +1269,7 @@ describe('Call propagation', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
proxy.addProtoService(test_service, proxy_impl);
|
proxy.addService(Client.service, proxy_impl);
|
||||||
var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
|
var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
|
||||||
proxy.start();
|
proxy.start();
|
||||||
var proxy_client = new Client('localhost:' + proxy_port,
|
var proxy_client = new Client('localhost:' + proxy_port,
|
||||||
|
|
@ -1253,14 +1289,14 @@ describe('Cancelling surface client', function() {
|
||||||
var server;
|
var server;
|
||||||
before(function() {
|
before(function() {
|
||||||
server = new grpc.Server();
|
server = new grpc.Server();
|
||||||
server.addProtoService(mathService, {
|
server.addService(mathServiceAttrs, {
|
||||||
'div': function(stream) {},
|
'div': function(stream) {},
|
||||||
'divMany': function(stream) {},
|
'divMany': function(stream) {},
|
||||||
'fib': function(stream) {},
|
'fib': function(stream) {},
|
||||||
'sum': function(stream) {}
|
'sum': function(stream) {}
|
||||||
});
|
});
|
||||||
var port = server.bind('localhost:0', server_insecure_creds);
|
var port = server.bind('localhost:0', server_insecure_creds);
|
||||||
var Client = surface_client.makeProtobufClientConstructor(mathService);
|
var Client = surface_client.makeClientConstructor(mathServiceAttrs);
|
||||||
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
client = new Client('localhost:' + port, grpc.credentials.createInsecure());
|
||||||
server.start();
|
server.start();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue