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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tecace/protos-manager

v0.0.3

Published

多服务间的proto解决方案,通过npm或git进行管理,支持基于proto文件生成对应类型

Downloads

6

Readme

@tecace/protos-manager

注意

内部使用,先克隆再自行替换proto文件到src/protos下

描述

多服务间的proto管理解决方案,通过npm或git进行管理,支持基于proto文件生成对应.d.ts

功能

  • 自动转换proto文件,生成.d.ts,可以通过import { SomeType } from '@tecace/protos-manager' 进行引入,类型与proto文件内的message,rpc相对应
  • 提供api获取所有proto文件的package name
  • 提供api获取所有的proto文件绝对路径

生成路径数组,包名数组和类型文件

建议使用pnpm

在其他项目的根目录内执行以下命令,执行完成后可以移动protos-manager去其他位置(或不移动也行)

git clone --sparse [email protected]:ACE0220/tecace.git tecace
cd tecace
git sparse-checkout init --cone
git sparse-checkout set protos-manager

安装依赖

# path/to/protos-manager
pnpm install
# or
npm install

在tecace/protos-manager/src/protos替换自己所需的proto文件,执行以下命令

npm run build
# of
pnpm build

使用

笔者的是 nestjs 项目

syntax = "proto3";

package acemall_user.user;

message ReqUserNameAndPassword {
  required string user_name = 1;
  required string user_password = 2;
}

message UserInfo {
  int32 user_id = 1;
  required string userName = 2;
  string token = 3;
}

message ResUserInfo {
  required int32 code = 1;
  required string message = 2;
  UserInfo data = 3;
}

service UserService {
  rpc Register(ReqUserNameAndPassword) returns (ResUserInfo){}
}
// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './modules/app/app.module';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { protosLoader } from '@tecace/protos-manager'; // 包名由使用者定义,修改/path/to/protos-manager/package.json中的name即可
async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AppModule,
    {
      transport: Transport.GRPC,
      options: {
        url: 'localhost:5001',
        package: protosLoader.getPackages(), // 获取所有包名数组
        protoPath: protosLoader.getProtos(), // 获取所有protos路径数组
      },
    },
  );
  await app.listen();
}
bootstrap();
import { Controller } from '@nestjs/common';
import { GrpcMethod } from '@nestjs/microservices';
import { UserService } from './user.service';

// 上面的proto文件中的package是acemall_user.user,对应的ts中的顶层namespace就是acemall_user
import { acemall_user } from '@tecace/protos-manager'; 

@Controller()
export class UserController {
  constructor(
    private userService: UserService,
  ) {}

  @GrpcMethod('UserService', 'register')
  // 传入的data类型是acemall_user.user.ReqUserNameAndPassword
  // 对应proto文件中 package acemall_user.user 下的message ReqUserNameAndPassword
  async register(data: acemall_user.user.ReqUserNameAndPassword): Promise<acemall_user.user.ResUserInfo> {
    const res = await this.userService.createUser(data);
    return res;
  }

}

api

protosLoader.getPackages()

返回所有proto文件中的package名称数组

const packageNames: Array<string> = protosLoader.getPackage() // ["health", "acemall_user.user"]

protosLoader.getProtos()

返回所有proto文件绝对路径数据

// ['/path/to/user.proto', '/path/to/category.proto']
const protoAbsolutPaths: Array<string> = protosLoader.getProtos()