foliojs/restructure

Fixed-length types not encoding as expected

pmarrapese opened this issue · 0 comments

Version: 1.0.0
Issue: I'm using restructure to unpack/pack objects for use in a binary protocol. Given "C-like structures" are a supported feature, I expected the "encode" function to align fields based on the spec provided.

If the spec states a string is 10 bytes long, shouldn't a 3-byte string be null-padded when encoded?

For example:

let stream = new r.EncodeStream();
stream.on('data', console.log);

let encoder = new r.Struct({
  foo: new r.Buffer(10),
  bar: new r.String(10)
});

encoder.encode(stream, {
  foo: Buffer.from('foo'),
  bar: 'bar'
});
stream.end();

Expected:

<Buffer 66 6f 6f 00 00 00 00 00 00 00>
<Buffer 62 61 72 00 00 00 00 00 00 00>

Actual:

<Buffer 66 6f 6f>
<Buffer 62 61 72>