Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nick-polyak-ms opened this issue Sep 9, 2022 · 0 comments

Comments

@nick-polyak-ms
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant