chrisdew / protobuf

Protocol Buffers for Node.JS
http://code.google.com/p/protobuf-for-node/
Apache License 2.0
234 stars 70 forks source link

How to process bytes field #29

Closed mattygiedt closed 11 years ago

mattygiedt commented 11 years ago

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 commented 11 years ago

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

mattygiedt commented 11 years ago

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@github.comwrote:

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/chrisdew/protobuf/issues/29#issuecomment-21491973 .

chrisdew commented 11 years ago

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@github.comwrote:

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/chrisdew/protobuf/issues/29#issuecomment-21714154 .

chrisdew commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

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