1.0.11 • Published 5 years ago

grpc-ts-middleware v1.0.11

Weekly downloads
42
License
MIT
Repository
github
Last release
5 years ago

gRPC Middleware

License Current Version npm npm.io

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: 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 {
  // 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();
1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

7 years ago

1.0.0

7 years ago