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

@agile-dev/wx-protobuf

v1.0.0

Published

微信小程序使用的 protobuf 协议开发工具

Downloads

4

Readme

wx-protobuf

微信小程序使用的 protobuf 协议开发工具

使用教程

  • 全局安装 protobufjs.proto文件进行转换成json
$ npm install -g protobufjs
  • 检测是否安装成功
 $ pbjs
  • 先创建一个 test.proto 文件
// awesome.proto
 syntax = "proto3";

 message Test {
 	number userId = 1;
 	staring userName = 2;
 }
  • 接下来我们来转换 test.proto 文件;
$ pbjs -t json test.proto > test.json
  • 这时我们就会得到一个 test.json 文件
 {
  "nested": {
  	"Test": {
   		"fields": {
     	  "userId": {
       	   "type": "number",
       	   "id": 1
     	  },
     	  "userName": {
       	   "type": "string",
       	   "id": 2
     	  }
   	   }
     }
   }
 }
  • 由于Json 文件不能直接在微信小程序内使用,所以我们需要用js把协议配置导出来
export default {
  "nested": {
  	"Test": {
   		"fields": {
     	  "userId": {
       	   "type": "number",
       	   "id": 1
     	  },
     	  "userName": {
       	   "type": "string",
       	   "id": 2
     	  }
   	   }
     }
   }
 }
  • 直接下载全部文件到你的微信制定的目录地址中,然后使用SE6语法
import wxProtobuf from '你放置的地址/wxProtobuf/index' 

初始化 wxProtobuf 程序

由于使用的是单例模式,所以不要多次初始化,初始化后的配置全局获取到的都是同一个实例.

import wxProtobuf from '你放置的地址/wxProtobuf/index';
import textRoot from '你放置的地址/test.json';

wxProtobuf.init({
  roots:{ // roots 的 key 会变成 存在 this._rootArray 里 的 key
    textRoot: textRoot,
    ...
  }
})

// 如果在init 的时候 传入了 root ,就会自动 执行 root.lookupType 这个方法 挂载 json 文件内的所有的 message

加密

import wxProtobuf from '你放置的地址/wxProtobuf/index';

const buffer = wxProtobuf.encode('Test',{userId:1,userName:'user'});

console.log(buffer) // 这个就是我们要传给后台的 二进制流

解密

import wxProtobuf from '你放置的地址/wxProtobuf/index';

const buffer = wxProtobuf.encode('Test',{userId:1,userName:'user'});

console.log(buffer) // 这个就是我们要传给后台的 二进制流

const loginData = wxProtobuf.decode('Test',buffer);

console.log(loginData) // 这个就是 讲 二进制 转换成 可以用的 obj 数据