JoshGlazebrook / smart-buffer

smart-buffer is a Buffer wrapper that adds automatic read & write offset tracking, string operations, data insertions, and more.
https://www.npmjs.com/package/smart-buffer
MIT License
102 stars 17 forks source link

FR: writeBufferWithLength and readBufferWithLength #36

Open hpx7 opened 3 years ago

hpx7 commented 3 years ago

I'm not sure what the best name for it would be, but would be nice to have convenience methods for the following common scenario:

// write
const data = ...
const buf = new SmartBuffer()
  .writeUInt32LE(data.length)
  .writeBuffer(data)
  .toBuffer();

...

// read
const reader = SmartBuffer.from(buf);
const data = reader.readBuffer(reader.readUInt32LE());

This is useful when you're constructing complex messages with multiple buffers inside them.

soanvig commented 2 years ago

Just saw this browsing the issues from curiosity. I'm not the library author, but I'll leave a comment.

Don't ask to add bloat and dozens of configuration options to libraries. Let's keep them simple, and do one thing, and do it right.

If you need such a feature, extend SmartBuffer, add these two functions, and use your own "wrapper" in the code.

To be honest - I would make use of that feature, so it's nothing personal. I have the exact case in my project. The problem I see is that features are being added and added to libraries, which makes them less maintainable, and actually less usable. If some feature is not a blocker (meaning that something cannot be done with current API), and it's not complex to implement by library user, it should be added only if would be used by majority of the users.