bl icon indicating copy to clipboard operation
bl copied to clipboard

Feature request: "Give me a buffer of any length"

Open mcclure opened this issue 4 years ago • 2 comments

So I am using a third party library which returns your BufferList. My own code has an existing "buffering" mechanism which largely reproduces the functions of BufferList— I have a readBytes(dst:Uint8Array, bytesMax:number) which abstracts over several types of data stream. I am now adding BufferLists as a stream that can be read from.

So what I want to work with my code is some way to get "some uint8array" from a BufferList. Your docs say there is slice, but they also say:

If the requested range spans a single internal buffer then a slice of that buffer will be returned which shares the original memory range of that Buffer. If the range spans multiple buffers then copy operations will likely occur to give you a uniform Buffer.

What I want is the longest possible buffer which does not involve copying. slice() can return a buffer without copying if I request a slice of the size of the first internal buffer, but it doesn't give me a way of asking what the internal buffer sizes are.

Expected behavior: There should be a readBuffer or readArbitraryBuffer which pops off the first internal buffer and returns it. The documented semantics could be something like "returns the longest buffer which can currently be returned without copying". This wording would allow for future refactoring or for cases where the user has already read several bytes out of an internal buffer (which means a slice() of the remainder of the internal buffer is needed).

mcclure avatar Jul 19 '21 20:07 mcclure

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar Jul 19 '21 21:07 mcollina

Sure, I'll give that a shot tomorrow.

mcclure avatar Jul 19 '21 21:07 mcclure