javascript - 如何通过导入加载 proto 文件

标签 javascript protocol-buffers proto3 protobuf.js

我正在使用dcodeIO/protobuf.js lib(版本 6.8.4)用于在浏览器中解析 protobuf 消息。 只要不导入另一个原始文件,我就可以让它使用简单的原始文件。

在主文件中导入其他原始文件会破坏一切。

这就是我所拥有的:

  • 文件结构

    - assets/
        |-api/
        |    |-v1/
        |    |    |-messageB.proto
        |    |-messageA.proto
    - foo.js
    
  • messageA.proto

    syntax = "proto3";
    package com.assets.api;
    import "api/v1/messageB.proto";
    
    message MessageA{
        MessageB foo = 0;
    }
    
  • messageB.proto

    syntax = "proto3";
    package com.assets.api.v1;
    
    message MessageB {
        string bar= 0;
    }
    
  • 6.8.4:

    var MessageProto = null;
    protobuf.load({root:"assets", file:"api/messageA.proto"}, function (err, root) {
        if (root) { MessageAProto = root.lookupType("com.assets.api.MessageA");
    }});
    data = MessageProto .decode(rawData);
    

.

    Error: no such type

at Root.lookupType (http://localhost:63342/xx/build/app/vendor/protobuf/dist/protobuf.js:3463:15)
at http://localhost:63342/xx/build/app/src/app/asset/asset.module.js:320:53
at finish (http://localhost:63342/xx/build/app/vendor/protobuf/dist/protobuf.js:5212:9)
at Root.load (http://localhost:63342/xx/build/app/vendor/protobuf/dist/protobuf.js:5316:9)
at Object.load (http://localhost:63342/xx/build/app/vendor/protobuf/dist/protobuf.js:2547:17)
at new <anonymous> (http://localhost:63342/xx/build/app/src/app/asset/asset.module.js:316:22)
at Object.instantiate (http://localhost:63342/xx/build/app/vendor/angular/angular.js:4786:14)
at $controller (http://localhost:63342/xx/build/app/vendor/angular/angular.js:10607:28)
at Object.<anonymous> (http://localhost:63342/xx/build/app/vendor/angular-ui-router/release/angular-ui-router.js:4081:28)
at http://localhost:63342/xx/build/app/vendor/angular/angular.js:1259:18
  • 5.0:

    var MessageProto = null;
    dcodeIO.ProtoBuf.convertFieldsToCamelCase = true;
    dcodeIO.ProtoBuf.loadProtoFile({root: "assets", file: "api/messageA.proto"}, function (err, builder) {
        if (builder) { MessageProto = builder.build("com.assets.api.MessageA");
     }});
    data = MessageProto .decode(rawData);
    

    OK
    

最佳答案

您需要首先检查 protobuf.load 是否已加载您的组件。如果没有加载,它将无法工作。

您可以将负载更改为loadSync

    var MessageProto = null;
    protobuf.loadSync({root:"assets", file:"api/messageA.proto"}, function(err, root) {
        if (root) { MessageAProto = root.lookupType("com.assets.api.MessageA");
    }});

data = MessageProto.decode(rawData);

您可以选择使用静态方法。

    import { AwesomeMessage } from "./bundle.js";

    // example code
    let message = AwesomeMessage.create({ awesomeField: "hello" });
    let buffer  = AwesomeMessage.encode(message).finish();
    let decoded = AwesomeMessage.decode(buffer);

要创建静态文件,请使用

pbjs -t json-module -w commonjs -o bundle.js file1.proto file2.proto

关于javascript - 如何通过导入加载 proto 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48573510/

相关文章:

java - 在运行时动态解析 .proto 文本文件以生成描述符

javascript - 根据第一个选择的日期限制日期范围

javascript - 为什么 diffbot 在这里看不到价格?

C# Google.ProtocolBuffers 反序列化方法 (proto3)

protocol-buffers - GRPC:在浏览器和服务器之间传递带有 proto.Message 字段的 JSON

java - 覆盖 Protocol Buffer 在将字段设置为 null 时抛出 NPE 的默认行为

android - 单元测试 proto3 生成的对象与验证

javascript - 使用 JavaScript 更改单击时的页面背景以匹配单击的页面元素的颜色

javascript - 您见过的网站上使用的最有趣/最不寻常的设计元素是什么?

java - 如何从 Protocol Buffer 序列化数据中获取 byte[] 以写入数据库?