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 🙏

© 2024 – Pkg Stats / Ryan Hefner

compact-buffer-util

v1.3.0

Published

bit-precise reading and writing with Buffers.

Downloads

2

Readme

compact-buffer-util

compact-buffer-util is a package specializing in reading from and writing to node.js Buffers with bit-precision and automatically set offsets. ArrayBuffers are supported for browser usage, however you must use a tool like webpack to bundle this package. Each allows interaction with strings, numbers, and booleans. By default, data is read as a big endian to be consistent with irregular sizes like 9-bit or 23-bit integers.

Installation

npm install compact-buffer-util

Requires node.js v10.4.0 or later.

CompactBufferReader

Allows reading from a Buffer with bit precision. If you don't use bit precision, only standard node.js Buffer methods are used instead.

const CompactBufferReader = require('compact-buffer-util').CompactBufferReader;

let myBuf = Buffer.from([ 0x12, 0x34, 0x56, 0x78 ]),
    reader = new CompactBufferReader(myBuf);

reader.readUint16(); // 0x1234
reader.readUnsignedBits(4); // 0x5
reader.readUint8(); // 0x67
reader.readUint8(); // null

If you attempt to read outside the bounds of the Buffer, null will be returned, and the offset and bitOffset will not change.

CompactBufferWriter

Allows writing to a Buffer with bit precision. Like CompactBufferReader, only standard node.js Buffer methods are used if you don't change the bit offset.

const CompactBufferWriter = require('compact-buffer-util').CompactBufferWriter;

let writer = new CompactBufferWriter();
writer.writeUint8(0x12)
      .writeUnsignedBits(0x3, 4)
      .writeUint8(0x45);
console.log(writer.toBuffer()); // <Buffer 12 34 50>

It's worth noting that most CompactBufferWriter methods return this, allowing for method chaining. The only exceptions are toBuffer() and writeUTF8().

Use cases

This project was designed with multiplayer networking in mind to minimize bandwidth.

An example use case would be sending a party code that consists of letters A-Z; rather than sending the character code, or even a string of it, you can send a 5-bit number for each character, fitting every letter in the alphabet.

const CompactBufferWriter = require('compact-buffer-util').CompactBufferWriter;
const code = 'HELLO';

let writer = new CompactBufferWriter();
for (let i = 0; i < code.length; ++i)
    writer.writeUnsignedBits(code.charCodeAt(i) - 65, 5);

console.log(writer.toBuffer()); // <Buffer 41 4a c7 80>

And generally any number that might fit a different range than what normal bytes offer, like a map with dimensions just big enough to exceed the size of a byte, like 400x300, and each coordinate could be stored with 9 bits.

Reference

This package contains TypeScript declaration files.