https://github.com/nicolaspearson/grpc.ts.middleware
gRPC pre-call and post-call middleware
https://github.com/nicolaspearson/grpc.ts.middleware
grpc middleware nodejs tracing typescript
Last synced: 6 months ago
JSON representation
gRPC pre-call and post-call middleware
- Host: GitHub
- URL: https://github.com/nicolaspearson/grpc.ts.middleware
- Owner: nicolaspearson
- License: mit
- Archived: true
- Created: 2018-12-21T07:39:01.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-02-13T13:04:09.000Z (about 4 years ago)
- Last Synced: 2025-01-09T20:42:22.391Z (about 1 year ago)
- Topics: grpc, middleware, nodejs, tracing, typescript
- Language: TypeScript
- Homepage:
- Size: 662 KB
- Stars: 3
- Watchers: 2
- Forks: 5
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gRPC Middleware
[![License][license-image]][license-url]
[](https://www.npmjs.com/package/grpc-ts-middleware)
[](https://www.npmjs.com/package/grpc-ts-middleware)

[license-url]: https://opensource.org/licenses/MIT
[license-image]: https://img.shields.io/npm/l/make-coverage-badge.svg
A library that assists with the implementation of gRPC pre-, and post-call middleware.
This library has **zero** external dependencies, but it is assumed that you are using the `grpc` library.
### Installation
```
npm install grpc-ts-middleware --save
```
Install the `grpc` library:
```
npm install grpc --save
```
### Dependencies
- [gRPC](https://www.npmjs.com/package/grpc): Node.js gRPC Library.
### Usage
```typescript
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, callback: grpc.sendUnaryData) {
const reply = new EchoReply();
reply.setMessage(call.request.getMessage());
callback(null, reply);
}
function start(): grpc.Server {
// Create the server
const server: grpc.Server = new grpc.Server();
// Create the middleware object
const grpcMiddleware = new GrpcMiddleware(
// An instance of the gRPC server
server,
// An array of functions to be invoked prior
// to the execution of the gRPC call
[
(call: GrpcCall) => console.log('Pre-call handler 1', call),
(call: GrpcCall) => console.log('Pre-call handler 2', call)
],
// An array of functions to be invoked after the gRPC call
// has been executed, but before returning the result
[
(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)
]
);
// Add gRPC services that you want the middleware to monitor
grpcMiddleware.addService(EchoManagerService, { echo: doEcho });
// Enable propagation of Jaeger tracing headers
grpcMiddleware.enableTracing();
// Bind and start the server
server.bind('localhost:9090', grpc.ServerCredentials.createInsecure());
server.start();
return server;
}
start();
```