I've spend the last hour trying to do a simple hello world with messagepack between Python and Javascript (well, ClojureScript ...). For some reason I can't read the msgpack payload into a javascript buffer correctly.
I'm using msgpack-python
and msgpack-lite
.
Python
In Python I'm generating my payload and writing to a binary file as follows:
bts = msgpack.packb({"blah": 1, "bleh": [0, 1]}, default=encode)
with open('/tmp/sample.msgpack', 'wb') as f:
f.write(bts)
This message appears to be correct, since I can verify its hexdump in https://kawanet.github.io/msgpack-lite/, though I do need to add spaces in the generated string ... I used the following line to generate the hex representation:
codecs.getencoder('hex_codec')(bts)[0]`)
# b'82a4626c6568920001a4626c616801'
Javascript
I'm serving this binary file using http-server -p 3000 --cors
. The response body of a simple GET in Javascript land is as follows: ��bleh���blah
. My problem is that I can't load it into a Uint8Array correctly:
Uint8Array.from(body)
// Uint8Array(15) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
new Uint8Array(body)
// Uint8Array(0)
What am I doing wrong? Clearly the payload is not all zeros as we can see in the messy byte representation. I'm also quite surprised that using .from()
generates an array of the correct length but then fails to set the content.