node-binary-reader
node-binary-reader copied to clipboard
Support `read(0)`
trafficstars
Allowing .read(0) enables simpler code in cases like the following (which I just ran in to):
reader.read(0x2, function(byteCount, buffer) {
// The next 2 bytes indicate the size of the string that follows.
var descriptionLength = buffer.readUIntLE(0, byteCount);
reader.read(descriptionLength, function(byteCount, buffer) {
// Imagine lots of code here that reads the string.
// The exact same code path must be taken if `descriptionLength == 0`,
// but currently binary-reader doesn’t allow that and forces me to duplicate
// code or to wrap everything in promises to avoid the sync/async difference.
// This patch fixes all that by simply allowing `.read(0)`.
});
Thanks for considering merging this. I really enjoy using this module in my hobby project!