protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

Trying to insert protobufjs code into the browser results in lookupType not working

Open nick-polyak-ms opened this issue 3 years ago • 0 comments

Hi, I have a very simple code that works perfectly fine in nodejs but not working in browser. In nodejs I have two files Simple.js and Simple.proto here is the content of Simple.proto

syntax = "proto3";
package subscriptions;

message TestTopicMessage
{
    string str = 1;
}

And here is the content of Simple.js:

    const protobuf = require('protobufjs');
    
    (async () => {
        const root = await protobuf.load('Simple.proto');
        const testTopicMsgType = root.lookupType("subscriptions.TestTopicMessage");
    
        var bufferMsg = testTopicMsgType.encode({ str: 'Hello World' }).finish();
    
        var restoredMessage = testTopicMsgType.decode(bufferMsg);
    
        console.log(restoredMessage);
    })();

Everything works file. I get the message etc.

When i try something very similar in the browser:

<script src="node_modules\protobufjs\dist\protobuf.js" type="text/javascript"></script>
    (async () => {
        const root = await protobuf.load('Simple.proto');
        const msgType = root.lookupType("subscriptions.TestTopicMessage");
    })();

lookupType throws an exception stating that it cannot find type "subscriptions.TestTopicMessage". Seems like the file Simple.proto is not found, even though it is located in the same folder as my Simple.html file. I tried passing full path to the file - it did not help. Please, tell me what I am doing wrong. thanks

nick-polyak-ms avatar Sep 09 '22 16:09 nick-polyak-ms