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

decode not working on client for ArrayBuffer #555

Closed
r-vianna opened this issue Dec 13, 2016 · 4 comments
Closed

decode not working on client for ArrayBuffer #555

r-vianna opened this issue Dec 13, 2016 · 4 comments
Labels

Comments

@r-vianna
Copy link

Hi,

I am using latest version (so setup slightly different from the examples online), but from the client when I pass my response.data to decode I get an empty message back. I then was making a new Uint8Array(response.data) and this was working but I run into stack overflow issues on larger sets of data.

Checked issue #553 and the examples you directed them too but still no luck.

Server Code

let TestMessage = protobuf.load('./public/protos/message.proto')
    .then((root) => {
        TestMessage = root.lookup('testpackage.TestMessage');
    })

router.get('/', function (req, res, next) {
    const isFinal = Number(req.query.count) >= Math.pow(10, 4);
    const count = isFinal ?
        Number(req.query.count) :
        Math.round(Number(req.query.count) * 1.5);
    const data = crypto.randomBytes(count).toString('hex');
    const msg = TestMessage.encode({
        count,
        data,
        isFinal
    }).finish();

    res.send(msg);
});

Client Code

import axios from 'axios';
import * as protobuf from 'protobufjs';

const testTextArea = document.querySelector('textarea.test-area');
let TestMessage = protobuf.load('./protos/message.proto')
    .then((root) => {
        TestMessage = root.lookup('testpackage.TestMessage');
    });


function init(count = 1) {
    return axios.get('/data', {
        params: {
            count
        },
        responseType: 'arraybuffer'
    })
        .then((response) => {
            const data = TestMessage.decode(response.data); 
            //TestMessage.decode(new Uint8Array(response.data)); <- works but have stack issues
            testTextArea.innerHTML = data.data;
            if (data.isFinal) { return; }

            return init(data.count);
        });
}
@dcodeIO
Copy link
Member

dcodeIO commented Dec 13, 2016

Using an Uint8Array is correct. Can you paste me one of those stack errors?

@r-vianna
Copy link
Author

Thanks for quick reply!

Sure thing.

RangeError: Maximum call stack size exceeded
    at Object.utf8.read (index.js:60)
    at Reader.read_string [as string] (reader.js:413)
    at Type._testpackage_TestMessage$decode [as decode] (eval at eof (index.js:102), <anonymous>:13:17)
    at test.js:19

dcodeIO added a commit that referenced this issue Dec 13, 2016
@dcodeIO
Copy link
Member

dcodeIO commented Dec 13, 2016

You must be using some really huge strings there.

Try again after: npm install @protobufjs/[email protected]

Or, if you are using master, just make sure that dependencies are up to date / use the new dist files.

@dcodeIO dcodeIO added the bug label Dec 13, 2016
@r-vianna
Copy link
Author

Yes... I am :)

Thanks. That did it!

@dcodeIO dcodeIO closed this as completed Dec 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants