gerpc
Expressive grpc client/server, with plugabble encoding.
Scope is limited to exactly my use case.
Why
While gRPC itself explicitly claims that using protobuf is optional, it seems the commonly-used libraries remove this option completely (or at the very least do not document how to access it).
Protobuf is a great idea, but it comes at the price of requiring you to specify your protocol upfront. While this by itself is also desireable, it might not be possible for a large project that is consider switching from HTTP to gRPC.
For those scenarios, one should be able to keep their existing encoding in place, and just rely on gRPC for transport.
Use
Client
const gerpc = ; // internal `grpc` is exposed if you need to access itconst grpc = gerpcgrpc; // encode: * -> Buffer { return Buffer;} // decode: Buffer -> * { return JSON;} // start the grpc client, with the provided default encoder and decoder// client credentials are also accepted as an optional second argumentconst client = gerpc; // wait until connected// optional timeout in ms can be provided as argument// resolves to the client instance, for chainingclient;
Server
const gerpc = ; // internal `grpc` is exposed if you need to access itconst grpc = gerpcgrpc; // encode: * -> Buffer { return Buffer;} // decode: Buffer -> * { return JSON;} // create the grpc server, with the provided default encoder and decoder// native options are also accepted as an optional second argumentconst server = gerpc; // name and handler must be provided to register a new method// method-specific encoder and decoder can be provided as optional third and fourth arguments, respectively// if not provided, the default encoder and decoder will be used instead// handler: request -> Promise(respose)servermethod'beep' { return Promise;};// returns server, for chaining // middleware: ({request, metadata, cancelled}, next) -> Promise({response, metadata})// next: () -> Promise({response, metadata})server;// returns server, for chaining // start the grpc server// host defaults to '0.0.0.0' and can be overriden// server credentials are also accepted as optional second argumentserverstartport: 8080;// returns { tryShutdown, forceShutdown }// tryShutdown: () -> Promise(undefined)// forceShutdown: () -> undefined // and now you have a running gRPC server, using (binary) JSON for encoding/decoding its messages
Example
See test.js
. Not a proper test (yet), but serves as working example, combining both use cases above.
Install
With npm do:
npm install gerpc
License
MIT