Assemble and disassemble binary messages used in cryptocoin applications.
Assemble a binary message out of chunks of data. All the methods return the builder itself, so are chainable. General usage is to use the various put* methods and then call raw() to get the output as a Buffer.
var b = new MessageBuilder();
b.putInt8(255).putInt16(0xbeef);
b.put(new Buffer([1,2,3,4,5]));
console.log(b.raw().toString('hex')); // 'ffbeef0102030405'
putInt8, otherwise input is expected to be a Buffer, which is appended to the messageGiven a string of binary data saved in a buffer, walk through it looking for structured data. Each of the methods returns the specified data from the current point in the data, and increments the pointer to the end of the data extracted. The point of this class is to allow you to not have to constantly check "is there enough data in the buffer to complete my next action?" or "did the last action fail?" and defer those checks to the end of parsing, making for cleaner code.
var p = new MessageParser(rawData);
var itemNum = p.readVarInt();
for (var i = 0; i < itemNum; i++) {
items.push(s.readUInt32LE());
}
if (p.hasFailed) {
throw new Error('Ran out of data before this point!');
}
The MessageParser object has a hasFailed property, which is set to true if the actions request cannot be completed (pointer goes beyond the length of the buffer, usually). There is also a failedStack property that holds a stack trace from when the first failure occurred (in case you want to diagnose when in the process it failed)