Note that there are some explanatory texts on larger screens.

plurals
  1. POEncoding MessagePack objects containing Node.js Buffers
    primarykey
    data
    text
    <p>I'm using <a href="https://github.com/pgriess/node-msgpack" rel="nofollow">node-msgpack</a> to encode and decode messages passed around between machines. One thing I'd like to be able to do is wrap raw Buffer data in an object and encode that with Messagepack.</p> <pre><code>msgpack = require('msgpack') buf = &lt;Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 ...&gt; obj = {foo: buf} packed = msgpack.pack(obj) </code></pre> <p>In the example above, I wanted to do a consistency check on the raw bytes of buffers nested in an object. So <code>buf</code> was obtained like so :</p> <pre><code>var buf = fs.readFileSync('some_image.png'); </code></pre> <p>In a perfect world, I would have obtained :</p> <pre><code>new Buffer(msgpack.unpack(packed).foo); #&gt; &lt;Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 ...&gt; </code></pre> <p>Instead, I end up with some random number. Digging up a little deeper, I end up with the following curiosity:</p> <pre><code>enc = 'ascii' new Buffer(buf.toString(enc), enc) #&gt; &lt;Buffer *ef bf bd* 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 ...&gt; buf #&gt; &lt;Buffer *89* 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 02 00 ...&gt; </code></pre> <p>The first byte is the problem. I tried using different encodings with no luck. What is happening here and what can I do to do circumvent the issue?</p> <h3>EDIT:</h3> <p>Originally, the <code>buf</code> was a buffer I had generated with msgpack itself, thus double-packing data. To avoid any confusion, I replaced that with another buffer obtained by reading an image, which raised the same problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload