/node-grpc-experimental-server-interceptors

gRPC experimental server interceptors for Node.js

Primary LanguageJavaScript

gRPC experimental server interceptors for Node.js

Since gRPC Node.js don't support server interceptors, I attempt to implement the experimental feature in TypeScript.

Inspired by echo health team work, but implement with more friendly koa-like API.

For client interceptors, please visit the proposal and source code in grpc

Usage

ges short for gRPC experimental server.

npm i ges
import ExperimentalServer from 'ges';

const server = new ExperimentalServer();

server.addService(GreeterService, { sayHello });

// 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('response is ', context.response);
  }
});

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

Server API details here

Design decision

As soon as grpc-node supports server interceptors, official solution should be drop in replacement of this repository.

Simulate go implementation

So I would make the interceptor mechanism as close as current go interceptor implementation.

Non-Intrusive API

ExperimentalServer is inherited from the original grpc Server. So you still can access all api exposed by grpc Server.

You can treat ExperimentalServer as original server if you don't add any interceptor. I believe it don't have any performance effect.

server.use is the only one extent API.

Unlike mali, you don't need to change server method implementation handler. You should even notice interceptors exist

// mali way
async function sayHello(ctx) {
  ctx.res = { message: 'Hello '.concat(ctx.req.name) };
}

// grpc original handler just works
function sayHello(call: ServerUnaryCall<HelloRequest>, callback: sendUnaryData<unknown>): void {
  const reply = new HelloReply();
  callback(null, reply);
}

Interceptors