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

bufferutility

v3.0.4

Published

A Node and Bun compatible buffer tool that permit easier binary manipulation.

Downloads

9

Readme


Basic installation and usage

You can install this package by using your preferred node package manager (NPM, PNPM, Yarn, etc) or bun

npm install bufferutility # NPM
pnpm add bufferutility # PNPM
yarn add bufferutility # Yarn
bun add bufferutility # Bun

You can then start using the package by requiring it from your application as such:

import { BufferUtility } from 'bufferutility';

Common Usage

Create a new BufferUtility

import { BufferUtility } from 'bufferutility';

const buffer = new BufferUtility();

console.log(buffer);
// <BufferUtility>

buffer[0] = 0xfa; // or buffer.writeByte(0xfa, 0);

console.log(buffer);
// <BufferUtility fa>

console.log(buffer[0]); // or console.log(buffer.readByte(0));
// 250

Create a BufferUtility with different modules

BufferUtility actually have 3 modules you can use

  • Uint8ArrayModule (compatible with Node and Bun): It use an Uint8Array to store the data (default module).
  • NodeBufferModule (compatible with Node): It use a NodeJS Buffer to store the data.
  • FileSystemModule (compatible with Node and Bun): It use a file to store the data (permit "infinite" size).
// Uint8Array module
import { BufferUtility, Uint8ArrayModule } from 'bufferutility';

const Uint8Buffer = new BufferUtility([], {
  module: Uint8ArrayModule
});

Uint8Buffer.writeByte(15);

console.log(Uint8Buffer.buffer) // Uint8Array(1) [ 15 ]

// Node Buffer module
import { BufferUtility, NodeBufferModule } from 'bufferutility';

const NodeBuffer = new BufferUtility([], {
  module: NodeBufferModule
});

NodeBuffer.writeByte(15);

console.log(NodeBuffer.buffer) // <Buffer 0f>

// File System module
import { BufferUtility, FileSystemModule } from 'bufferutility';

const FSBuffer = new BufferUtility("file.txt", {
  module: FileSystemModule
}); 

FSBuffer.writeString("data");

import { readFileSync } from 'fs';

console.log(readFileSync("file.txt", "utf8")) // data

// FSBuffer.buffer return the file descriptor

Function: BufferUtility

isBufferUtility(obj)

  • obj : <Object>
  • Returns : <boolean>

Returns true if obj is a BufferUtility, false otherwise.

const buf1 = new BufferUtility("a BufferUtility");
const buf2 = Buffer.from("a Node Buffer");

console.log(BufferUtility.isBufferUtility(buf1));
// true

console.log(BufferUtility.isBufferUtility(buf2));
// false

byteLength(string[, encoding])

See https://nodejs.org/api/buffer.html#buffer_static_method_buffer_bytelength_string_encoding

Class: BufferUtility

new BufferUtility([data[, opts]])

  • data : <String> | <Array> | <Buffer> | <Uint8Array> | <Number> A property determinate the value of the BufferUtility. Default: undefined (empty)
  • opts : <Object> Set of configurable options to set on the Buffer. Can have the following fields:
    • module : <Module> The module used by the Buffer. Default: <Uint8ArrayModule>.
    • maxArrayLength : <Number> The max length an ArrayBuffer can take. Default: buffer.kMaxLength.
    • returnAsBigIntIfOverflow: <Boolean> Return a BigInt for read functions if value is under Number.MIN_SAFE_INTEGER or greater than Number.MAX_SAFE_INTEGER. Default: true.
    • returnAsBigInt: <Boolean> Always return a BigInt for read functions (override returnAsBigIntIfOverflow). Default: false.
    • offset : <Number> The forced start offset of the buffer source. Default: 0.
    • length : <Number> The forced length of the buffer source. Default: buf.length.
  • Returns : <BufferUtility>

Create a new BufferUtility instance from data and opts properties

const buf1 = new BufferUtility(); // Empty buf
const buf2 = new BufferUtility("test"); // Buf with test in bytes
const buf3 = new BufferUtility(64); // Alloc 64 bytes
const buf4 = new BufferUtility([54, 76]); // Buf with bytes 54 and 76

More documentation

More documentation on this readme will come soon, they are approximately the same as the version 2. You can check them here https://github.com/pharuxtan/BufferUtility/blob/v2/README.md#bufferutility

read7BitEncodedInt/read7BitEncodedInt64/write7BitEncodedInt/write7BitEncodedInt64 is replaced by readULEB128/writeULEB128/readSLEB128/writeSLEB128