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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@142vip/egg-grpc-server

v0.0.1-alpha.5

Published

Egg.js框架下使用grpc的插件,简化grpc服务端配置

Readme

@142vip/egg-grpc-server

NPM version

安装

# npm
npm install @142vip/egg-grpc-server
# pnpm
pnpm i @142vip/egg-grpc-server

配置

默认配置

@142vip/egg-grpc-server插件,做如下默认限定:

  • 只在agent.js中加载,防止在app.js中加载,因为app.js是在每个worker中都加载的,同一端口会被占用,导致启动失败。
  • 限定proto文件的加载方式
const { defaultPluginConfig, PluginLoader } = require('@142vip/egg')
const { name: pkgName } = require('../package.json')

module.exports = {
  grpcServer: defaultPluginConfig(pkgName, {
    default: {
      loaderOptions: {
        keepCase: true,
        longs: Stri,
        enums: String,
        defaults: true,
        oneofs: true,
      },
    },
    // Grpc只在agent.js中加载
    loaders: [PluginLoader.AGENT],
  }),
}

单实例配置

const { exampleProto } = require('@142vip/egg-grpc-server/example/example-grpc')
const { GrpcConnectURI } = require('@142vip/grpc')

module.exports = {
  grpcServer: {
    client: {
      connectUri: GrpcConnectURI.PORT_50001,
      protoPaths: [exampleProto],
    },
  },
}

多实例配置

const { exampleProto } = require('@142vip/egg-grpc-server/example/example-grpc')
const { GrpcConnectURI } = require('@142vip/grpc')

module.exports = {
  grpcServer: {
    clients: {
      example1: {
        connectUri: GrpcConnectURI.PORT_50001,
        protoPaths: [exampleProto],
      },
      example2: {
        connectUri: GrpcConnectURI.PORT_50002,
        protoPaths: [exampleProto],
      },
    },
  },
}

更多配置

@142vip/egg-grpc-client客户端:

@142vip/egg-grpc-server服务端:

使用

定义proto文件

实现rpc方法

基于Egg框架的目录风格,在app目录下,新建grpc目录。所有的rpc方法通过使用该目录下的js文件管理。例如,新建Example.js文件

// app/grpc/Example.js

const BaseGrpcService = require('@142vip/egg-grpc-server/core/base-grpc.service')

/**
 * 直接继承GrpcExampleService方法,用来演示
 * - 可以另外拓展
 */
class Example extends BaseGrpcService {
  async test() {
    const { ctx } = this
    console.log('test:', ctx.method)
  }

  // async ClientToServer(requestData) {
  //   console.log(11, this)
  //   const { app } = this
  //   console.log(123, app)
  //   console.log(123123, app.grpc)
  //   return await clientToServer(requestData)
  // }
}

module.exports = Example

注意:在Egg框架下的所有rpc方法实现,都要继承BaseGrpcService父类实现,类似框架的Service加载模式,这样才能在对应实现类中使用this.app对象,融入Egg框架体系

参考

证书

MIT

Copyright (c) 2019-present, @142vip 储凡

仅供学习参考,商业使用请保留作者版权信息,作者不保证也不承担任何软件的使用风险。