kriszyp / msgpackr

Ultra-fast MessagePack implementation with extension for record and structural cloning / msgpack.org[JavaScript/NodeJS]

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sequential Unpackr.unpack

o5k opened this issue · comments

commented

It looks like the sequential flag only supports use in unpackMultiple. That is:

const packr = new msgpackr.Packr({ sequential: true });
const unpackr = new msgpackr.Unpackr({ sequential: true });

unpackr.unpack(packr.pack({foo: 1, bar: 2})); // works, 14 bytes
unpackr.unpack(packr.pack({foo: 1, bar: 2})); // no longer works (3 bytes), throws "Data read, but end of buffer not reached 64"

While this does work:

const packr = new msgpackr.Packr({ sequential: true });
const unpackr = new msgpackr.Unpackr({ sequential: true });

const packages = [packr.pack({ foo: 1, bar: 2 }), packr.pack({ foo: 1, bar: 2 }), packr.pack({ foo: 1, bar: 2 })];
const buf = new Uint8Array(packages.reduce((p, c) => p + c.byteLength, 0));
packages.reduce((p, c) => {
    buf.set(c, p);
    return p + c.byteLength;
}, 0);

unpackr.unpackMultiple(buf, (dat) => { console.log(dat); });

It looks like the Unpackr doesn't remember any records it encountered after unpacking, unlike the Packr which does. Is there any way to get this behaviour?
My planned usecase was for a WebSocket. Since the messages can be ensured to be consistent and in order, wouldn't remembering records sequentially save both space and performance?

I'll take a look at making this better documented and more intuitive, but I think this works:

const packr = new msgpackr.Packr({ sequential: true });
const unpackr = new msgpackr.Unpackr({ structures: [] });

console.log(unpackr.unpackMultiple(packr.pack({foo: 1, bar: 2}))[0]); // works, 14 bytes
console.log(unpackr.unpackMultiple(packr.pack({foo: 1, bar: 2}))[0]); // works, 3 bytes
commented

Yep, that works. That's a fine stand-in for me, thank you very much! I'll keep this open if you want since it might still be a good idea to document this, it's not particularly intuitive I suppose lol

Published fix in v1.6.2.

commented

Awesome, works indeed!