Byte message protocol and streaming parser for Node.js.
Rust version: https://github.com/iorust/msgp-rust
- Mspg: 23437.50 kb 661813.37 ops
- resp: 29296.88 kb 646412.41 ops
- Amp: 27343.75 kb 222419.93 ops
const Msgp = require('msgp')
Create a writeable like stream parser.
const msgp = new Msgp()
msgp.on('data', function (data) {
console.log(data.toString())
})
someSocketStream.pipe(msgp)
msgp.write(Msgp.encode(JSON.stringify({name: 'msgp'})))
msgp.on('data', (buffer) => {
console.log(JSON.parse(buffer.toString())) // {name: 'msgp'}
})
Encode string or buffer to buffer packet.
Msgp.encode(JSON.stringify({name: 'msgp'}))
// result: <Buffer 0f 7b 22 6e 61 6d 65 22 3a 22 6d 73 67 70 22 7d>
Decode buffer packet to message buffer.
let buf = new Buffer([0x0f, 0x7b, 0x22, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3a, 0x22, 0x6d, 0x73, 0x67, 0x70, 0x22, 0x7d])
JSON.parse(Msgp.decode(buf))
// result: {name: 'msgp'}
MIT © zensh