3.0.0 • Published 7 months ago

@brandonxiang/ges v3.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
7 months ago

grpc-experimental-server

gRPC experimental server that supports koa-like interceptors

Usage

npm i ges grpc
import ExperimentalServer from 'ges';

const server = new ExperimentalServer();

server.addService(/* ... */);

// add interceptor
server.use(async (context, next) => {
  // preprocess
  const start = Date.now();
  try {
    await next();
  } finally {
    // postprocess
    const costtime = Date.now() - start;
    console.log('costtime is', costtime);
    console.log('unary response is ', context.response);
  }
});

serer.bind(/* ... */);
server.start();

gRPC has 4 kinds of call:

handle typerequest is stream or notresponse is stream or not
handleUnaryCall
handleClientStreamingCall
handleServerStreamingCall
handleBidiStreamingCall

Context

  • call current gRPC call
  • definition the method definition of current call
  • response response if response is not stream
  • onFinished(...) you can listen on call finish event, no matter response of current call is stream or not. So you don't need to care about what kind is the call. It is very useful to do something like tracing, logging

Notes

  • await next() would wait the call to be finished if response is not stream.
3.0.0

7 months ago