0.1.0 • Published 2 years ago

x.proto v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

proto

解析 Protobuf 数据工具库,支持 Node、浏览器、小程序。

用法

  1. 安装
    tnpm i --save-dev x.proto
  2. 配置 Webpack loader
    module.exports = {
      module: {
        rules: [
          {
            test: /\.proto$/,
            use: {
              loader: 'x.proto/loader',
            },
          },
        ],
      },
    };
  3. 代码

    import Proto from 'x.proto';
    import protos from '../protos/test.proto';
    
    const codec = new Proto(protos);
    
    // 用法1
    codec.decode(package.cmd, data/*, {
      binaryAsBase64?: boolean;
      longsAsStrings?: boolean;
      bufferAsStrings?: boolean;
      length?: number;
      encoding?: string;
    }*/);
    
    // 用法2
    const cmdCodec = codec.lookup(package.cmd);
    cmdCodec.decode(data/*, {
      binaryAsBase64?: boolean;
      longsAsStrings?: boolean;
      bufferAsStrings?: boolean;
      length?: number;
      encoding?: string;
    }*/);
    
    // buffer 转 string
    codec.decodeBuffer(ArrayBuffer | object, clone?: true by default);