npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@micl/grpc-server

v0.0.17

Published

micl grpc server

Downloads

175

Readme

@micl/grpc-server

npm version npm downloads license

一个基于装饰器的 Node.js gRPC 服务端框架,简化 gRPC 服务的开发流程。

✨ 特性

  1. 基于装饰器的服务定义,代码更简洁
  2. 支持多种 RPC 类型(一元、服务端流、客户端流、双向流)
  3. 自动加载 Proto 文件
  4. 完整的 TypeScript 类型支持
  5. 灵活的日志配置
  6. 简化的服务注册和启动流程

📦 安装

npm install @micl/grpc-server
# or
pnpm add @micl/grpc-server
# or
yarn add @micl/grpc-server

📚 模块导出

| 导出项 | 类型 | 描述 | |--------|------|------| | grpc | namespace | @grpc/grpc-js 命名空间 | | GrpcServerBuilder | class | gRPC 服务构建器 | | GrpcService | decorator | 类装饰器,标记 gRPC 服务 | | GrpcMethod | decorator | 方法装饰器,标记一元 RPC 方法 | | GrpcServerStream | decorator | 方法装饰器,标记服务端流式 RPC | | GrpcClientStream | decorator | 方法装饰器,标记客户端流式 RPC | | GrpcBidiStream | decorator | 方法装饰器,标记双向流式 RPC | | GrpcMethodType | enum | RPC 方法类型枚举 |

🎯 快速使用

定义 Proto 文件

// example.proto
syntax = "proto3";

package example;

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

创建服务类

import {
  GrpcService,
  GrpcMethod,
  GrpcServerStream,
} from '@micl/grpc-server';
import * as grpc from '@grpc/grpc-js';

@GrpcService('example.Greeter')
export class GreeterService {
  @GrpcMethod('SayHello')
  async sayHello(call, callback) {
    const reply = { message: `Hello ${call.request.name}` };
    callback(null, reply);
  }

  @GrpcServerStream('StreamGreetings')
  async streamGreetings(call) {
    for (let i = 0; i < 5; i++) {
      call.write({ message: `Hello ${call.request.name} - ${i}` });
    }
    call.end();
  }
}

启动服务

import { grpc, GrpcServerBuilder } from '@micl/grpc-server';
import { GreeterService } from './greeter.service';

const builder = new GrpcServerBuilder('./protos/example.proto');
builder.registerService(GreeterService);

const credentials = grpc.ServerCredentials.createInsecure();
builder.start(50051, credentials);

console.log('gRPC Server running on port 50051');

🛠 API 参考

GrpcMethodType 枚举

RPC 方法类型枚举。

export enum GrpcMethodType {
  UNARY = 'unary',
  SERVER_STREAM = 'server_stream',
  CLIENT_STREAM = 'client_stream',
  BIDI_STREAM = 'bidi',
}

@GrpcService(serviceName)

类装饰器,标记一个类为 gRPC 服务。

参数:

  • serviceName - 服务名称(需与 Proto 文件中的服务全名匹配)
@GrpcService('example.Greeter')
export class GreeterService {}

@GrpcMethod(methodName)

方法装饰器,标记一元 RPC 方法。

参数:

  • methodName - 方法名称(需与 Proto 文件中的方法名匹配)
@GrpcMethod('SayHello')
async sayHello(call, callback) {}

@GrpcServerStream(methodName)

方法装饰器,标记服务端流式 RPC 方法。

@GrpcServerStream('StreamData')
async streamData(call) {}

@GrpcClientStream(methodName)

方法装饰器,标记客户端流式 RPC 方法。

@GrpcClientStream('CollectData')
async collectData(call, callback) {}

@GrpcBidiStream(methodName)

方法装饰器,标记双向流式 RPC 方法。

@GrpcBidiStream('Chat')
async chat(call) {}

GrpcServerBuilder

constructor(protoPath, logger?)

创建 gRPC 服务构建器。

参数:

  • protoPath - Proto 文件路径或目录
  • logger - 日志记录器(可选,默认为 console)
const builder = new GrpcServerBuilder('./protos');

registerService(ServiceClass)

注册 gRPC 服务。

参数:

  • ServiceClass - 被 @GrpcService 装饰的服务类
builder.registerService(GreeterService);

start(port, credentials)

启动 gRPC 服务。

参数:

  • port - 服务监听端口
  • credentials - 服务器凭证
const credentials = grpc.ServerCredentials.createInsecure();
builder.start(50051, credentials);

shutdown()

关闭 gRPC 服务。

await builder.shutdown();

📝 完整示例

import {
  grpc,
  GrpcServerBuilder,
  GrpcService,
  GrpcMethod,
  GrpcServerStream,
} from '@micl/grpc-server';

@GrpcService('example.Greeter')
export class GreeterService {
  @GrpcMethod('SayHello')
  async sayHello(call, callback) {
    callback(null, { message: `Hello ${call.request.name}` });
  }

  @GrpcServerStream('StreamGreetings')
  async streamGreetings(call) {
    for (let i = 0; i < 5; i++) {
      call.write({ message: `Hello ${call.request.name} - ${i}` });
    }
    call.end();
  }
}

const builder = new GrpcServerBuilder('./protos/example.proto');
builder.registerService(GreeterService);

const credentials = grpc.ServerCredentials.createInsecure();
builder.start(50051, credentials);

console.log('gRPC Server running on port 50051');

🤝 贡献

欢迎提交 Issue 和 Pull Request 来完善这个模块。

📄 许可证

本项目采用 ISC 许可证 - 查看 LICENSE 文件了解详情。

Copyright (c) alexgogoing [email protected]

📞 支持

如有问题或建议,请提交 Issue 或联系维护者。


@micl/grpc-server - 简洁优雅的 gRPC 服务端框架 🚀