Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

grpc

Package Overview
Dependencies
Maintainers
2
Versions
138
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grpc - npm Package Compare versions

Comparing version 0.5.5 to 0.6.0

examples/qps_test.js

8

index.js

@@ -59,3 +59,3 @@ /*

} else if (value.className === 'Service') {
return client.makeClientConstructor(value);
return client.makeProtobufClientConstructor(value);
} else if (value.className === 'Message' || value.className === 'Enum') {

@@ -123,3 +123,3 @@ return value.build();

*/
exports.buildServer = server.makeServerConstructor;
exports.buildServer = server.makeProtobufServerConstructor;

@@ -146,1 +146,5 @@ /**

exports.getGoogleAuthDelegate = getGoogleAuthDelegate;
exports.makeGenericClientConstructor = client.makeClientConstructor;
exports.makeGenericServerConstructor = server.makeServerConstructor;

@@ -38,2 +38,3 @@ /*

var path = require('path');
var _ = require('underscore');
var grpc = require('..');

@@ -49,2 +50,4 @@ var testProto = grpc.load(__dirname + '/test.proto').grpc.testing;

'@developer.gserviceaccount.com');
var COMPUTE_ENGINE_USER = ('155450119199-r5aaqa2vqoa9g5mv2m6s3m1l293rlmel' +
'@developer.gserviceaccount.com');

@@ -270,2 +273,3 @@ /**

* Run one of the authentication tests.
* @param {string} expected_user The expected username in the response
* @param {Client} client The client to test against

@@ -275,3 +279,3 @@ * @param {function} done Callback to call when the test is completed. Included

*/
function authTest(client, done) {
function authTest(expected_user, client, done) {
(new GoogleAuth()).getApplicationDefault(function(err, credential) {

@@ -297,3 +301,3 @@ assert.ifError(err);

314159);
assert.strictEqual(resp.username, AUTH_USER);
assert.strictEqual(resp.username, expected_user);
assert.strictEqual(resp.oauth_scope, AUTH_SCOPE_RESPONSE);

@@ -322,4 +326,4 @@ });

cancel_after_first_response: cancelAfterFirstResponse,
compute_engine_creds: authTest,
service_account_creds: authTest
compute_engine_creds: _.partial(authTest, COMPUTE_ENGINE_USER),
service_account_creds: _.partial(authTest, AUTH_USER)
};

@@ -326,0 +330,0 @@

{
"name": "grpc",
"version": "0.5.5",
"version": "0.6.0",
"author": "Google Inc.",

@@ -5,0 +5,0 @@ "description": "gRPC Library for Node",

@@ -38,5 +38,2 @@ /*

var capitalize = require('underscore.string/capitalize');
var decapitalize = require('underscore.string/decapitalize');
var grpc = require('bindings')('grpc.node');

@@ -248,2 +245,3 @@

}
emitter.emit('status', response.status);
if (response.status.code !== grpc.status.OK) {

@@ -255,3 +253,2 @@ var error = new Error(response.status.details);

}
emitter.emit('status', response.status);
emitter.emit('metadata', response.metadata);

@@ -321,2 +318,3 @@ callback(null, deserialize(response.read));

}
stream.emit('status', response.status);
if (response.status.code !== grpc.status.OK) {

@@ -328,3 +326,2 @@ var error = new Error(response.status.details);

}
stream.emit('status', response.status);
callback(null, deserialize(response.read));

@@ -471,9 +468,14 @@ });

/**
* Creates a constructor for clients for the given service
* @param {ProtoBuf.Reflect.Service} service The service to generate a client
* for
* Creates a constructor for a client with the given methods. The methods object
* maps method name to an object with the following keys:
* path: The path on the server for accessing the method. For example, for
* protocol buffers, we use "/service_name/method_name"
* requestStream: bool indicating whether the client sends a stream
* resonseStream: bool indicating whether the server sends a stream
* requestSerialize: function to serialize request objects
* responseDeserialize: function to deserialize response objects
* @param {Object} methods An object mapping method names to method attributes
* @return {function(string, Object)} New client constructor
*/
function makeClientConstructor(service) {
var prefix = '/' + common.fullyQualifiedName(service) + '/';
function makeClientConstructor(methods) {
/**

@@ -498,6 +500,6 @@ * Create a client with the given methods

_.each(service.children, function(method) {
_.each(methods, function(attrs, name) {
var method_type;
if (method.requestStream) {
if (method.responseStream) {
if (attrs.requestStream) {
if (attrs.responseStream) {
method_type = 'bidi';

@@ -508,3 +510,3 @@ } else {

} else {
if (method.responseStream) {
if (attrs.responseStream) {
method_type = 'server_stream';

@@ -515,11 +517,22 @@ } else {

}
var serialize = common.serializeCls(method.resolvedRequestType.build());
var deserialize = common.deserializeCls(
method.resolvedResponseType.build());
Client.prototype[decapitalize(method.name)] = requester_makers[method_type](
prefix + capitalize(method.name), serialize, deserialize);
Client.prototype[decapitalize(method.name)].serialize = serialize;
Client.prototype[decapitalize(method.name)].deserialize = deserialize;
var serialize = attrs.requestSerialize;
var deserialize = attrs.responseDeserialize;
Client.prototype[name] = requester_makers[method_type](
attrs.path, serialize, deserialize);
Client.prototype[name].serialize = serialize;
Client.prototype[name].deserialize = deserialize;
});
return Client;
}
/**
* Creates a constructor for clients for the given service
* @param {ProtoBuf.Reflect.Service} service The service to generate a client
* for
* @return {function(string, Object)} New client constructor
*/
function makeProtobufClientConstructor(service) {
var method_attrs = common.getProtobufServiceAttrs(service);
var Client = makeClientConstructor(method_attrs);
Client.service = service;

@@ -532,2 +545,4 @@

exports.makeProtobufClientConstructor = makeProtobufClientConstructor;
/**

@@ -534,0 +549,0 @@ * See docs for client.status

@@ -39,2 +39,3 @@ /*

var capitalize = require('underscore.string/capitalize');
var decapitalize = require('underscore.string/decapitalize');

@@ -114,2 +115,22 @@ /**

/**
* Return a map from method names to method attributes for the service.
* @param {ProtoBuf.Reflect.Service} service The service to get attributes for
* @return {Object} The attributes map
*/
function getProtobufServiceAttrs(service) {
var prefix = '/' + fullyQualifiedName(service) + '/';
return _.object(_.map(service.children, function(method) {
return [decapitalize(method.name), {
path: prefix + capitalize(method.name),
requestStream: method.requestStream,
responseStream: method.responseStream,
requestSerialize: serializeCls(method.resolvedRequestType.build()),
requestDeserialize: deserializeCls(method.resolvedRequestType.build()),
responseSerialize: serializeCls(method.resolvedResponseType.build()),
responseDeserialize: deserializeCls(method.resolvedResponseType.build())
}];
}));
}
/**
* See docs for deserializeCls

@@ -133,1 +154,3 @@ */

exports.wrapIgnoreNull = wrapIgnoreNull;
exports.getProtobufServiceAttrs = getProtobufServiceAttrs;

@@ -38,5 +38,2 @@ /*

var capitalize = require('underscore.string/capitalize');
var decapitalize = require('underscore.string/decapitalize');
var grpc = require('bindings')('grpc.node');

@@ -77,2 +74,5 @@

}
if (error.hasOwnProperty('metadata')) {
status.metadata = error.metadata;
}
var error_batch = {};

@@ -110,7 +110,7 @@ error_batch[grpc.opType.SEND_STATUS_FROM_SERVER] = status;

* response
* @param {Object=} metadata Optional trailing metadata to send with status
*/
function sendUnaryResponse(call, value, serialize) {
function sendUnaryResponse(call, value, serialize, metadata) {
var end_batch = {};
end_batch[grpc.opType.SEND_MESSAGE] = serialize(value);
end_batch[grpc.opType.SEND_STATUS_FROM_SERVER] = {
var status = {
code: grpc.status.OK,

@@ -120,2 +120,7 @@ details: 'OK',

};
if (metadata) {
status.metadata = metadata;
}
end_batch[grpc.opType.SEND_MESSAGE] = serialize(value);
end_batch[grpc.opType.SEND_STATUS_FROM_SERVER] = status;
call.startBatch(end_batch, function (){});

@@ -153,2 +158,3 @@ }

var details = 'Unknown Error';
var metadata = {};
if (err.hasOwnProperty('message')) {

@@ -163,3 +169,6 @@ details = err.message;

}
stream.status = {code: code, details: details, metadata: {}};
if (err.hasOwnProperty('metadata')) {
metadata = err.metadata;
}
stream.status = {code: code, details: details, metadata: metadata};
}

@@ -178,2 +187,13 @@ /**

stream.on('error', terminateCall);
/**
* Override of Writable#end method that allows for sending metadata with a
* success status.
* @param {Object=} metadata Metadata to send with the status
*/
stream.end = function(metadata) {
if (metadata) {
stream.status.metadata = metadata;
}
Writable.prototype.end.call(this);
};
}

@@ -348,7 +368,9 @@

}
handler.func(emitter, function sendUnaryData(err, value) {
handler.func(emitter, function sendUnaryData(err, value, trailer) {
if (err) {
err.metadata = trailer;
handleError(call, err);
} else {
sendUnaryResponse(call, value, handler.serialize, trailer);
}
sendUnaryResponse(call, value, handler.serialize);
});

@@ -392,8 +414,10 @@ });

call.startBatch(metadata_batch, function() {});
handler.func(stream, function(err, value) {
handler.func(stream, function(err, value, trailer) {
stream.terminate();
if (err) {
err.metadata = trailer;
handleError(call, err);
} else {
sendUnaryResponse(call, value, handler.serialize, trailer);
}
sendUnaryResponse(call, value, handler.serialize);
});

@@ -544,22 +568,16 @@ }

/**
* Creates a constructor for servers with a service defined by the methods
* object. The methods object has string keys and values of this form:
* {serialize: function, deserialize: function, client_stream: bool,
* server_stream: bool}
* @param {Object} methods Method descriptor for each method the server should
* expose
* @param {string} prefix The prefex to prepend to each method name
* @return {function(Object, Object)} New server constructor
* Create a constructor for servers with services defined by service_attr_map.
* That is an object that maps (namespaced) service names to objects that in
* turn map method names to objects with the following keys:
* path: The path on the server for accessing the method. For example, for
* protocol buffers, we use "/service_name/method_name"
* requestStream: bool indicating whether the client sends a stream
* resonseStream: bool indicating whether the server sends a stream
* requestDeserialize: function to deserialize request objects
* responseSerialize: function to serialize response objects
* @param {Object} service_attr_map An object mapping service names to method
* attribute map objects
* @return {function(Object, function, Object=)} New server constructor
*/
function makeServerConstructor(services) {
var qual_names = [];
_.each(services, function(service) {
_.each(service.children, function(method) {
var name = common.fullyQualifiedName(method);
if (_.indexOf(qual_names, name) !== -1) {
throw new Error('Method ' + name + ' exposed by more than one service');
}
qual_names.push(name);
});
});
function makeServerConstructor(service_attr_map) {
/**

@@ -578,4 +596,3 @@ * Create a server with the given handlers for all of the methods.

this.inner_server = server;
_.each(services, function(service) {
var service_name = common.fullyQualifiedName(service);
_.each(service_attr_map, function(service_attrs, service_name) {
if (service_handlers[service_name] === undefined) {

@@ -585,7 +602,6 @@ throw new Error('Handlers for service ' +

}
var prefix = '/' + common.fullyQualifiedName(service) + '/';
_.each(service.children, function(method) {
_.each(service_attrs, function(attrs, name) {
var method_type;
if (method.requestStream) {
if (method.responseStream) {
if (attrs.requestStream) {
if (attrs.responseStream) {
method_type = 'bidi';

@@ -596,3 +612,3 @@ } else {

} else {
if (method.responseStream) {
if (attrs.responseStream) {
method_type = 'server_stream';

@@ -603,15 +619,10 @@ } else {

}
if (service_handlers[service_name][decapitalize(method.name)] ===
undefined) {
throw new Error('Method handler for ' +
common.fullyQualifiedName(method) + ' not provided.');
if (service_handlers[service_name][name] === undefined) {
throw new Error('Method handler for ' + attrs.path +
' not provided.');
}
var serialize = common.serializeCls(
method.resolvedResponseType.build());
var deserialize = common.deserializeCls(
method.resolvedRequestType.build());
server.register(
prefix + capitalize(method.name),
service_handlers[service_name][decapitalize(method.name)],
serialize, deserialize, method_type);
var serialize = attrs.responseSerialize;
var deserialize = attrs.requestDeserialize;
server.register(attrs.path, service_handlers[service_name][name],
serialize, deserialize, method_type);
});

@@ -653,4 +664,37 @@ }, this);

/**
* Create a constructor for servers that serve the given services.
* @param {Array<ProtoBuf.Reflect.Service>} services The services that the
* servers will serve
* @return {function(Object, function, Object=)} New server constructor
*/
function makeProtobufServerConstructor(services) {
var qual_names = [];
var service_attr_map = {};
_.each(services, function(service) {
var service_name = common.fullyQualifiedName(service);
_.each(service.children, function(method) {
var name = common.fullyQualifiedName(method);
if (_.indexOf(qual_names, name) !== -1) {
throw new Error('Method ' + name + ' exposed by more than one service');
}
qual_names.push(name);
});
var method_attrs = common.getProtobufServiceAttrs(service);
if (!service_attr_map.hasOwnProperty(service_name)) {
service_attr_map[service_name] = {};
}
service_attr_map[service_name] = _.extend(service_attr_map[service_name],
method_attrs);
});
return makeServerConstructor(service_attr_map);
}
/**
* See documentation for makeServerConstructor
*/
exports.makeServerConstructor = makeServerConstructor;
/**
* See documentation for makeProtobufServerConstructor
*/
exports.makeProtobufServerConstructor = makeProtobufServerConstructor;

@@ -145,4 +145,4 @@ /*

batch[grpc.opType.SEND_INITIAL_METADATA] = {
'key1': [new Buffer('value1')],
'key2': [new Buffer('value2')]
'key1-bin': [new Buffer('value1')],
'key2-bin': [new Buffer('value2')]
};

@@ -149,0 +149,0 @@ call.startBatch(batch, function(err, resp) {

@@ -141,10 +141,10 @@ /*

assert.ifError(err);
assert(response['send metadata']);
assert(response['client close']);
assert(response.hasOwnProperty('metadata'));
assert.strictEqual(response.metadata.server_key[0].toString(),
'server_value');
assert.deepEqual(response.status, {'code': grpc.status.OK,
'details': status_text,
'metadata': {}});
assert.deepEqual(response,{
'send metadata': true,
'client close': true,
metadata: {server_key: ['server_value']},
status: {'code': grpc.status.OK,
'details': status_text,
'metadata': {}}
});
done();

@@ -156,3 +156,3 @@ });

assert.notEqual(new_call, null);
assert.strictEqual(new_call.metadata.client_key[0].toString(),
assert.strictEqual(new_call.metadata.client_key[0],
'client_value');

@@ -159,0 +159,0 @@ var server_call = new_call.call;

@@ -48,2 +48,4 @@ /*

var capitalize = require('underscore.string/capitalize');
describe('Surface server constructor', function() {

@@ -79,2 +81,212 @@ it('Should fail with conflicting method names', function() {

});
describe('Generic client and server', function() {
function toString(val) {
return val.toString();
}
function toBuffer(str) {
return new Buffer(str);
}
var string_service_attrs = {
'capitalize' : {
path: '/string/capitalize',
requestStream: false,
responseStream: false,
requestSerialize: toBuffer,
requestDeserialize: toString,
responseSerialize: toBuffer,
responseDeserialize: toString
}
};
describe('String client and server', function() {
var client;
var server;
before(function() {
var Server = grpc.makeGenericServerConstructor({
string: string_service_attrs
});
server = new Server({
string: {
capitalize: function(call, callback) {
callback(null, capitalize(call.request));
}
}
});
var port = server.bind('localhost:0');
server.listen();
var Client = grpc.makeGenericClientConstructor(string_service_attrs);
client = new Client('localhost:' + port);
});
after(function() {
server.shutdown();
});
it('Should respond with a capitalized string', function(done) {
client.capitalize('abc', function(err, response) {
assert.ifError(err);
assert.strictEqual(response, 'Abc');
done();
});
});
});
});
describe('Trailing metadata', function() {
var client;
var server;
before(function() {
var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
var test_service = test_proto.lookup('TestService');
var Server = grpc.buildServer([test_service]);
server = new Server({
TestService: {
unary: function(call, cb) {
var req = call.request;
if (req.error) {
cb(new Error('Requested error'), null, {metadata: ['yes']});
} else {
cb(null, {count: 1}, {metadata: ['yes']});
}
},
clientStream: function(stream, cb){
var count = 0;
var errored;
stream.on('data', function(data) {
if (data.error) {
errored = true;
cb(new Error('Requested error'), null, {metadata: ['yes']});
} else {
count += 1;
}
});
stream.on('end', function() {
if (!errored) {
cb(null, {count: count}, {metadata: ['yes']});
}
});
},
serverStream: function(stream) {
var req = stream.request;
if (req.error) {
var err = new Error('Requested error');
err.metadata = {metadata: ['yes']};
stream.emit('error', err);
} else {
for (var i = 0; i < 5; i++) {
stream.write({count: i});
}
stream.end({metadata: ['yes']});
}
},
bidiStream: function(stream) {
var count = 0;
stream.on('data', function(data) {
if (data.error) {
var err = new Error('Requested error');
err.metadata = {
metadata: ['yes'],
count: ['' + count]
};
stream.emit('error', err);
} else {
stream.write({count: count});
count += 1;
}
});
stream.on('end', function() {
stream.end({metadata: ['yes']});
});
}
}
});
var port = server.bind('localhost:0');
var Client = surface_client.makeProtobufClientConstructor(test_service);
client = new Client('localhost:' + port);
server.listen();
});
after(function() {
server.shutdown();
});
it('should be present when a unary call succeeds', function(done) {
var call = client.unary({error: false}, function(err, data) {
assert.ifError(err);
});
call.on('status', function(status) {
assert.deepEqual(status.metadata.metadata, ['yes']);
done();
});
});
it('should be present when a unary call fails', function(done) {
var call = client.unary({error: true}, function(err, data) {
assert(err);
});
call.on('status', function(status) {
assert.deepEqual(status.metadata.metadata, ['yes']);
done();
});
});
it('should be present when a client stream call succeeds', function(done) {
var call = client.clientStream(function(err, data) {
assert.ifError(err);
});
call.write({error: false});
call.write({error: false});
call.end();
call.on('status', function(status) {
assert.deepEqual(status.metadata.metadata, ['yes']);
done();
});
});
it('should be present when a client stream call fails', function(done) {
var call = client.clientStream(function(err, data) {
assert(err);
});
call.write({error: false});
call.write({error: true});
call.end();
call.on('status', function(status) {
assert.deepEqual(status.metadata.metadata, ['yes']);
done();
});
});
it('should be present when a server stream call succeeds', function(done) {
var call = client.serverStream({error: false});
call.on('data', function(){});
call.on('status', function(status) {
assert.strictEqual(status.code, grpc.status.OK);
assert.deepEqual(status.metadata.metadata, ['yes']);
done();
});
});
it('should be present when a server stream call fails', function(done) {
var call = client.serverStream({error: true});
call.on('data', function(){});
call.on('status', function(status) {
assert.notStrictEqual(status.code, grpc.status.OK);
assert.deepEqual(status.metadata.metadata, ['yes']);
done();
});
});
it('should be present when a bidi stream succeeds', function(done) {
var call = client.bidiStream();
call.write({error: false});
call.write({error: false});
call.end();
call.on('data', function(){});
call.on('status', function(status) {
assert.strictEqual(status.code, grpc.status.OK);
assert.deepEqual(status.metadata.metadata, ['yes']);
done();
});
});
it('should be present when a bidi stream fails', function(done) {
var call = client.bidiStream();
call.write({error: false});
call.write({error: true});
call.end();
call.on('data', function(){});
call.on('status', function(status) {
assert.notStrictEqual(status.code, grpc.status.OK);
assert.deepEqual(status.metadata.metadata, ['yes']);
done();
});
});
});
describe('Cancelling surface client', function() {

@@ -94,3 +306,3 @@ var client;

var port = server.bind('localhost:0');
var Client = surface_client.makeClientConstructor(mathService);
var Client = surface_client.makeProtobufClientConstructor(mathService);
client = new Client('localhost:' + port);

@@ -97,0 +309,0 @@ });

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc