npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

better-buffer

v0.0.2

Published

Buffer subclass with some smack-your-head-obvious methods (push, pop, growToAccommodate, clone, dataLength, etc.)

Readme

// better-buffer

part of me wanted to call this module node-miyagi.

please god let someone get the joke. preferably a girl. preferably gorgeous.

why

It blows my mind that these methods don't exist on the core node.js Buffer object. srsly guise?

what

BetterBuffer(initialSize, growSize = 512)

Constructor is compatible with existing Buffer constructor, but also adds an optional growSize parameter. The only time that a BetterBuffer will grow is when you call the growToAccommodate() method -- there's no auto-growing on BetterBuffer::pushBack() or Buffer::copy() -- at least not yet.

BetterBuffer::growToAccommodate (dataLengthToAccommodate)

Ensures that the buffer is at least big enough to store dataLengthToAccommodate octets. The buffer will grow in intervals defind by its growSize member, so you may sometimes end up with a buffer that's slightly larger than necessary.

BetterBuffer::pushBack (sourceBuffer)

Queue-like functionality for buffers. Appends sourceBuffer to the buffer at the end of that buffer's actual data, as indicated by its dataLength member. So for example, if you have:

var myBuffer = new BetterBuffer(10, 5);
// myBuffer.length == 10
// myBuffer.dataLength == 0

...and you append, say, secondBuffer, the length of which is 6:

myBuffer.pushBack(secondBuffer);
// myBuffer.length == 10
// myBuffer.dataLength == 6

myBuffer.dataLength is now 6. Now let's append thirdBuffer, the length of which is 2:

myBuffer.pushBack(thirdBuffer);
// myBuffer.length == 10
// myBuffer.dataLength == 8

myBuffer.dataLength is now 8, and thirdBuffer was written into myBuffer starting at position 6.

In combination with BetterBuffer::popFront(), this makes it much easier to create queue- or stream-like behavior using simple buffer objects.

The buffer's dataLength property is incremented accordingly.

BetterBuffer::popFront (num)

Just a simple pop function, like you'd find on a stack or queue. Hacks off the first num octets in the buffer, shifts everything in the buffer forward by the same distance, and returns the popped octets in a new BetterBuffer object.

The buffer's dataLength property is decremented accordingly.

BetterBuffer::cloneDataIntoNewBuffer ()

Creates a new BetterBuffer containing an exact copy of the contents of the buffer. NOTE: the returned buffer's size is equal to the source buffer's dataLength property, not its length property.

For example, let's say you have a buffer myBuffer of size 256, and you've only pushBacked one buffer of size 100 into it. This means that myBuffer.length is 256, but myBuffer.dataLength is 100. So when you call cloneDataIntoNewBuffer, it will return a buffer of size 100, not 256.

tl;dr (for the hyperactive and/or distractible)

var myBuf = new BetterBuffer(10, 5);
myBuf.fill(0x00);
// myBuf.length == 10
// myBuf.dataLength == 0
// myBuf = <Buffer 00 00 00 00 00 00 00 00 00 00>

var smallerBuffer = new BetterBuffer(8);
smallerBuffer.fill(0xff); 
// smallerBuffer.length == 8
// smallerBuffer.dataLength == 0
// smallerBuffer = <Buffer ff ff ff ff ff ff ff ff>

// now we explicitly set smallerBuffer.dataLength -- it's not necessary, but
// it makes things clearer conceptually
smallerBuffer.dataLength = smallerBuffer.length;

// what happens when we call growToAccommodate to ensure we have enough room?
myBuf.growToAccommodate(smallerBuffer.dataLength);
// smallerBuffer is smaller than myBuf, so myBuf.length is still 10

// go ahead and eat the smaller buffer
myBuf.pushBack(smallerBuffer); 
// myBuf = <Buffer ff ff ff ff ff ff ff ff 00 00>
// myBuf.length == 10
// myBuf.dataLength == 8

var anotherBuffer = new BetterBuffer(8);
anotherBuffer.fill(0xaa);
// anotherBuffer.length == 8
// anotherBuffer.dataLength == 0
// anotherBuffer = <Buffer aa aa aa aa aa aa aa aa>

// again, we explicitly set anotherBuffer.dataLength for the sake of code readability
anotherBuffer.dataLength = anotherBuffer.length;

// let's try calling growToAccommodate again and see if anything happens this time
myBuf.growToAccommodate(myBuf.dataLength + anotherBuffer.dataLength);
// now, myBuf.length == 20 because:
//     myBuf.dataLength == 8
//     anotherBuffer.dataLength == 8
// and myBuf.growSize == 5 (myBuf's initial size was 10, and 10 + 5 + 5 == 20)

// now we can pushBack() safely
myBuf.pushBack(anotherBuffer);
// myBuf = <Buffer ff ff ff ff ff ff ff ff aa aa aa aa aa aa aa aa 00 00 00 00>
// myBuf.length == 20
// myBuf.dataLength == 16

// and just for good measure let's tack on a shitty example of popFront()
var justAPiece = myBuf.popFront(5);
// justAPiece = <Buffer ff ff ff ff ff>
// myBuf = <Buffer ff ff ff aa aa aa aa aa aa aa aa 00 00 00 00>

Authors and contributors

bryn austin bellomy < [email protected] >

License (MIT license)

Copyright (c) 2012 bryn austin bellomy, signalenvelope / signals.io »

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.