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

How to process bytes field #29

Closed
mattygiedt opened this issue Jul 24, 2013 · 6 comments
Closed

How to process bytes field #29

mattygiedt opened this issue Jul 24, 2013 · 6 comments

Comments

@mattygiedt
Copy link

How do I obtain the data of a 'bytes' field (shown as msg.delegate) into a Buffer?

new Buffer( msg.delegate ); ==> TypeError: First argument needs to be a number, array or string.

@chrisdew
Copy link
Owner

I'll have a look at this and get back to you in a day or two...

@mattygiedt
Copy link
Author

Hi Chris -- just wondering if you had a chance to look at it. The
'delegate' field is actually another protobuf message, if that helps in
your testing.

Didn't mean to close issue, thought that closed the comment box.

Regards,
-m

On Wed, Jul 24, 2013 at 10:14 AM, Chris Dew notifications@git.luolix.topwrote:

I'll have a look at this and get back to you in a day or two...


Reply to this email directly or view it on GitHubhttps://github.com//issues/29#issuecomment-21491973
.

@chrisdew
Copy link
Owner

Sorry for the delay, I will look this evening.

On 29 July 2013 12:39, mattygiedt notifications@github.com wrote:

Hi Chris -- just wondering if you had a chance to look at it. The
'delegate' field is actually another protobuf message, if that helps in
your testing.

Didn't mean to close issue, thought that closed the comment box.

Regards,
-m

On Wed, Jul 24, 2013 at 10:14 AM, Chris Dew notifications@git.luolix.topwrote:

I'll have a look at this and get back to you in a day or two...


Reply to this email directly or view it on GitHub<
https://github.com/chrisdew/protobuf/issues/29#issuecomment-21491973>
.


Reply to this email directly or view it on GitHubhttps://github.com//issues/29#issuecomment-21714154
.

@chrisdew
Copy link
Owner

Here's how I do it - I hope I've answered your question.

buftest.proto

package com.chrisdew.buftest;

message BufTest {
  optional float num  = 1;
  optional bytes payload = 2;
}

buftest.js

var fs = require('fs');
var Schema = require('protobuf').Schema;

// "schema" contains all message types defined in buftest.proto|desc.
var schema = new Schema(fs.readFileSync('buftest.desc'));

// The "BufTest" message.
var BufTest = schema['com.chrisdew.buftest.BufTest'];

var ob = { num: 42 };
ob.payload = new Buffer("Hello World");

var proto = BufTest.serialize(ob);
console.log('proto.length:', proto.length);

var outOb = BufTest.parse(proto);
console.log('unserialised:', JSON.stringify(outOb));

var payload = new Buffer(outOb.payload);
console.log(payload);

Makefile: (second line begins with a TAB not spaces)

all:
    protoc --descriptor_set_out=buftest.desc --include_imports buftest.proto

output:

$ node buftest.js 
proto.length: 18
unserialised: {"num":42,"payload":{"0":72,"1":101,"2":108,"3":108,"4":111,"5":32,"6":87,"7":111,"8":114,"9":108,"10":100,"length":11}}
payload: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>

@chrisdew
Copy link
Owner

P.S. Turning bytes into a JS object and then back into a buffer looks wasteful. Is there a more efficient way to get the (original) buffer?

@mattygiedt
Copy link
Author

Thanks Chris. It was actually obvious user error all along. Thanks for the protobuf node library!

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

2 participants