msgp
v1.0.0
Published
Byte message protocol and streaming parser for Node.js.
Downloads
12
Maintainers
Readme
MSGP
Byte message protocol and streaming parser for Node.js.
Rust version: https://github.com/iorust/msgp-rust
Protocol
Bench
1000000 simple messages, amp vs msgp vs resp
- Mspg: 23437.50 kb 661813.37 ops
- resp: 29296.88 kb 646412.41 ops
- Amp: 27343.75 kb 222419.93 ops
API
const Msgp = require('msgp')
Class Msgp
new 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(buffer)
msgp.write(Msgp.encode(JSON.stringify({name: 'msgp'})))
Event: 'data'
msgp.on('data', (buffer) => {
console.log(JSON.parse(buffer.toString())) // {name: 'msgp'}
})
Event: 'error'
Event: 'drain'
Event: 'null'
Event: 'finish'
Class Method: Msgp.encode(string)
Class Method: Msgp.encode(buffer)
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>
Class Method: Msgp.decode(buffer)
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'}
License
MIT © zensh