nice-grpc Server Reflection

3.0.4 · active · verified Sun Apr 19

nice-grpc-server-reflection is a package within the nice-grpc ecosystem that provides gRPC server reflection capabilities for Node.js servers built with nice-grpc. Currently at version 3.0.4, it enables external gRPC tools like `grpcurl`, Postman, or client libraries to dynamically discover and introspect the server's exposed services, methods, and their protobuf message definitions at runtime without requiring pre-compiled `.proto` files on the client side. The library integrates seamlessly with the `nice-grpc` server, accepting a pre-generated protobuf descriptor set (.bin file) and a list of fully-qualified service names. As part of the actively maintained `nice-grpc` monorepo, it receives frequent updates, leveraging nice-grpc's TypeScript-first design, modern Promises/Async Iterables API, and robust middleware support. Its primary differentiator is its tight integration and idiomatic usage within the `nice-grpc` framework, providing a clear path for enabling standard gRPC reflection in TypeScript-based Node.js gRPC services.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a nice-grpc server, define a simple gRPC service, generate a protobuf descriptor set, and enable the gRPC Server Reflection service. It includes the necessary `protoc` command to create the `.bin` file and shows how to integrate it with `nice-grpc`, allowing tools like `grpcurl` to inspect the running server's API.

import { Server as GrpcServer, ServiceDefinition, UntypedServiceImplementation } from 'nice-grpc';
import { ServerReflectionService, ServerReflection } from 'nice-grpc-server-reflection';
import * as fs from 'fs';
import * as path from 'path';

// 1. Define your .proto file (e.g., proto/greeting.proto):
//    syntax = "proto3";
//    package greeting;
//    service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} }
//    message HelloRequest { string name = 1; }
//    message HelloReply { string message = 1; }

// 2. Generate the descriptor set using protoc:
//    `protoc --descriptor_set_out=./proto/bundle.bin --include_imports -I./proto ./proto/greeting.proto`

// 3. Define your gRPC service implementation
const greeterService: ServiceDefinition & UntypedServiceImplementation = {
  path: '/greeting.Greeter/SayHello',
  requestStream: false,
  responseStream: false,
  requestType: {} as any, // In a real app, these would be generated types
  responseType: {} as any, // In a real app, these would be generated types
  async handler(request: { name: string }) {
    return { message: `Hello, ${request.name}!` };
  }
};

const main = async () => {
  const server = new GrpcServer();

  // Load the generated protobuf descriptor set
  const protoset = fs.readFileSync(path.join(__dirname, '../proto/bundle.bin'));

  // Add your application service
  server.add('greeting.Greeter', greeterService);

  // Add the server reflection service
  // Pass the protoset and a list of fully-qualified service names to expose
  server.add(
    ServerReflectionService,
    ServerReflection(
      protoset,
      ['greeting.Greeter']
    )
  );

  const port = 50051;
  await server.listen(`0.0.0.0:${port}`);
  console.log(`nice-grpc server with reflection listening on port ${port}`);

  // You can now use grpcurl to inspect and call services:
  // `grpcurl -plaintext localhost:50051 list`
  // `grpcurl -plaintext localhost:50051 describe greeting.Greeter`
  // `grpcurl -plaintext -d '{"name": "World"}' localhost:50051 greeting.Greeter/SayHello`
};

main().catch(console.error);

view raw JSON →