gRPC Middleware
A library that assists with the implementation of gRPC pre-, and post-call middleware.
Installation
npm install grpc-ts-middleware --save
Dependencies
- gRPC: Node.js gRPC Library.
Usage
import * as grpc from 'grpc';
import GrpcMiddleware, { GrpcCall } from 'grpc-ts-middleware';
import { EchoReply, EchoRequest } from './proto/echo_pb';
import { EchoManagerService } from './proto/echo_grpc_pb';
function doEcho(call: grpc.ServerUnaryCall<EchoRequest>, callback: grpc.sendUnaryData<EchoReply>) {
const reply = new EchoReply();
reply.setMessage(call.request.getMessage());
callback(null, reply);
}
function start(): grpc.Server {
const server: grpc.Server = new grpc.Server();
const grpcMiddleware = new GrpcMiddleware(
server,
[
(call: GrpcCall) => console.log('Pre-call handler 1', call),
(call: GrpcCall) => console.log('Pre-call handler 2', call)
],
[
(error: grpc.ServiceError | null, call: GrpcCall) =>
console.log('Post-call handler 1', call, error),
(error: grpc.ServiceError | null, call: GrpcCall) =>
console.log('Post-call handler 2', call, error)
]
);
grpcMiddleware.addService(EchoManagerService, { echo: doEcho });
grpcMiddleware.enableTracing();
server.bind('localhost:9090', grpc.ServerCredentials.createInsecure());
server.start();
return server;
}
start();