eggjs/egg-grpc

Name: egg-grpc

Owner: egg

Description: grpc plugin for egg

Created: 2017-05-27 03:24:04.0

Updated: 2018-04-10 04:50:48.0

Pushed: 2018-04-26 06:51:19.0

Homepage: null

Size: 31

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

egg-grpc

NPM version build status Test coverage David deps Known Vulnerabilities npm download

grpc plugin for egg

Install
m i egg-grpc --save
s
app_root}/config/plugin.js
rts.grpc = {
able: true,
ckage: 'egg-grpc',

Configuration
app_root}/config/config.default.js
rts.grpc = {
dpoint: 'localhost:50051',
 dir: 'app/proto', // proto files dir, relative path
 property: 'grpc', // default attach to `ctx.grpc.**`
 loadOpts: { convertFieldsToCamelCase: true, }, // message field case: `string user_name` -> `userName`

see config/config.default.js for more detail.

Usage

fixtures:

proto
egg
??? test
    ??? game.proto
    ??? message.proto
uc
??? test.proto
share.proto
rotobuf
pp/proto/share.proto
ax = "proto3";

age egg.share;

age Status {
ring code = 1;
ring err_msg = 2;


ice ShowCase {
c Echo(Status) returns (Status) {}

quickstart:

ount by `package` define, not proto file path
t client = ctx.grpc.egg.share.showCase;
t result = yield client.echo({ code: 200 });
ole.log(result);
Folder Structure
Name Conversion

| term | case at proto | case when load | | ———– | —————— | ———————————- | | package | lowercase with . | camleCase if contains _ | | service | PascalCase | camleCase when initialize at ctx | | rpc | PascalCase | camleCase | | message | PascalCase | PascalCase at app | | field | snake_case | camleCase | | enums | CONSTANT_CASE | CONSTANT_CASE |

API
Custom Options
Service && Message

You can get service instance and invoke rpc by ctx.mypackage.myService.myRpc({ id: 1 }).

Usually, you don't need to instantiate a message instace, grpc will do it for you, however you can create it by new app.grpcProto.mypackage.SomeMessage({ id: 1 }).

Unary RPC

nary RPC, such as `rpc Echo(Request) returns (Response) {}`
param {Object} data - data sent to sever
param {Object|grpc.Metadata} [metadata] - metadata, support plain object
param {Object} [options] - { timeout }
return {Promise} response promise chain
see http://www.grpc.io/docs/guides/concepts.html#unary-rpc

onst client = ctx.grpc.<package>.<service>;
d client.echo(data, metadata, options);
d client.echo(data, options);
Client Streaming RPC

lient Streaming RPC, such as `rpc EchoClientStream(stream Request) returns (Response) {}`
param {Object|grpc.Metadata} [metadata] - metadata, support plain object
param {Object} [options] - { timeout }
param {Function} [callback] - callback for response, `(err, response) => {}`
return {Stream} write stream
see http://www.grpc.io/docs/guides/concepts.html#client-streaming-rpc

t stream = client.echoClientStream(meta, options, callback);
onst stream = client.echoClientStream(callback);
onst stream = client.echoClientStream(meta, callback);
rigger order: metadata -> callback -> status
am.once('metadata', meta => {});
am.once('status', status => {});
am.on('error', status => {});
end data to server or end
am.write(data1);
am.write(data2);
am.end(data4);
Server Streaming RPC

erver Streaming RPC, such as `rpc EchoServerStream(Request) returns (stream Response) {}`
param {Object} data - data sent to sever
param {Object|grpc.Metadata} [metadata] - metadata, support plain object
param {Object} [options] - { timeout }
return {Stream} read stream
see http://www.grpc.io/docs/guides/concepts.html#server-streaming-rpc

t stream = client.echoServerStream(data, meta, options);
rigger order: metadata -> data -> status -> end
am.on('data', response => {});
am.on('end', response => {});
am.once('metadata', meta => {});
am.once('status', status => {});
am.on('error', status => {});
Bidirectional Streaming RPC

idirectional Streaming RPC, such as `rpc echoStreamStream(stream Request) returns (stream Response) {}`
param {Object|grpc.Metadata} [metadata] - metadata, support plain object
param {Object} [options] - { timeout }
return {Stream} duplex stream
see http://www.grpc.io/docs/guides/concepts.html#bidirectional-streaming-rpc

t stream = client.echoStreamStream(meta, options);
rigger order: metadata -> data -> status -> end
am.on('data', response => {});
am.on('end', () => {});
am.once('metadata', meta => {});
am.once('status', status => {});
am.on('error', status => {});
end data to server or end
am.write(data1);
am.write(data2);
am.end(data3);
Example

see grpc.tests.js.

Questions & Suggestions

Please open an issue here.

License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.