Gathering detailed insights and metrics for @cmdcode/buffy
Gathering detailed insights and metrics for @cmdcode/buffy
Gathering detailed insights and metrics for @cmdcode/buffy
Gathering detailed insights and metrics for @cmdcode/buffy
npm install @cmdcode/buffy
Typescript
Module System
Node Version
NPM Version
TypeScript (85.41%)
Shell (6.41%)
JavaScript (4.78%)
HTML (3.4%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
19 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jun 22, 2025
Latest Version
3.0.0
Package Id
@cmdcode/buffy@3.0.0
Unpacked Size
166.08 kB
Size
33.84 kB
File Count
89
NPM Version
10.9.0
Node Version
22.12.0
Published on
Jan 04, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
A compact swiss-army-knife for byte manipulation.
Features:
Buff
objects are instance of Uint8Array
.DataView.setUint8
for ultra-fast performace.This library is designed to support classic and modern ESM imports, in both a nodejs and browser environment.
Example install via NPM or yarn:
1npm install @cmdcode/buffy || yarn add @cmdcode/buffy
Classic import into a nodejs project:
1const { Buff, Bytes } = require('@cmdcode/buffy')
Modern import into an nodejs project:
1import { Buff, Bytes } from '@cmdcode/buffy'
Classic import into a browser-based project:
1<script src="https://unpkg.com/@cmdcode/buff/dist/script.js"></script> 2<script> 3 const { Buff, Bytes } = window.buff 4</script>
Modern import into a browser-based project:
1<script type="module"> 2 import { Buff, Bytes } from "https://unpkg.com/@cmdcode/buff/dist/module.mjs" 3</script>
The Buff
class is an extention of the base Uint8Array
class. It provides the same default functionality of a Uint8Array, and can be used as a drop-in replacement for Uint8Array. Typescript will treat Buff as a Uint8Array object.
1import { Buff, Bytes } from '@cmdcode/buffy' 2 3// Bytes covers value types that are convertable to Uint8Array. 4type Bytes = string | number | bigint | Uint8Array | Buff 5// You can optionally specify the endianess of data. 6type Endian = 'le' | 'be' 7 8const buffer = new Buff ( 9 bytes : Bytes | ArrayBuffer, 10 size ?: number, // Specify the size of the array (for padding) 11 endian ?: Endian // Specify the endianess of the array. 12) 13
You can convert from many different types and formats into a Buff
object.
1Buff 2 .big = (data : bigint, size ?: number) => Buff, 3 .bin = (data : string, size ?: number) => Buff, 4 .bytes = (data : Bytes, size ?: number) => Buff, 5 .hex = (data : string, size ?: number) => Buff, 6 .json = (data : T, replacer ?: Replacer) => Buff, 7 .num = (data : number, size ?: number) => Buff, 8 .str = (data : string, size ?: number) => Buff, 9 .uint = (data : Uint8Array, size ?: number) => Buff
With Buff
, you have access to an extensive API for converting between formats.
1const buffer = new Buff(data) 2 3/* Quicky convert into many formats using getters. */ 4 5buffer 6 .big => bigint // Convert to a BigInt. 7 .bin => string // Convert to a binary string. 8 .hex => string // Convert to a hex string. 9 .num => number // Convert to a Number. 10 .str => string // Convert to a UTF8 string. 11 .uint => Uint8Array // Convert to a pure Uint8Array. 12 13/* There are a few export methods that support extra params. */ 14 15buffer 16 .to_big : (endian ?: Endian) => bigint 17 .to_bin : () => string 18 .to_hex : (endian ?: Endian) => string 19 .to_json : (reviver ?: Reviver) => T 20 .to_num : (endian ?: Endian) => number
In addition to format conversion, you can perform many other convenient tasks.
1Buff
2 // Same as Uint8Array.from(), but returns a Buff object.
3 .from (data : Uint8Array | number[]) => Buff,
4 // Same as Uint8Array.of(), but returns a Buff object.
5 .of (...data : number[]) => Buff,
6 // Join together multiple arrays of bytes.
7 .join (array : Bytes[]) => Buff,
8 // Sort multiple arrays of bytes in lexicographic order.
9 .sort (arr : Bytes[], size ?: number) => Buff[],
10 // Return a buffer object with random data (uses webcrypto).
11 .random (size : number) => Buff,
12 // Converts a number into a 'varint' for byte streams.
13 .varint (num : number, endian ?: Endian) => Buff
14
15const buffer = new Buff(data)
16
17buffer
18 // Append data to your ubber object
19 .append (data : Bytes) => Buff
20 // Prepend data to your buffer object.
21 .prepend (data : Bytes) => Buff
22 // Same as Uint8Array.reverse(), but returns a Buff object.
23 .reverse () => Buff
24 // Identical to Uint8Array.set() method.
25 .set (array : ArrayLike<number>, offset ?: number) => void
26 // Same as Uint8Array.slice(), but returns a Buff object.
27 .slice (start ?: number, end ?: number) => Buff
28 // Same as Uint8Array.subarray(), but returns a Buff object.
29 .subarray (begin ?: number, end ?: number) => Buff
The Stream
tool will take a blob of data and allow you to consume it byte-per-byte.
1import { Stream } from '@cmdcode/buffy' 2 3// Convert data into a stream object. 4const stream = new Stream(data) 5 6// You can convert a buff object into a stream. 7const stream = new Buff(data).stream 8 9stream 10 // Reads x number of bytes, does not consume the stream. 11 .peek(size: number) => Buff 12 // Reads x number of bytes, consumes the stream. 13 .read(size: number) => bytes 14 // Reads the next bytes(s) as a varint, returns the number value. 15 .varint (endian ?: Endian) => number
Please feel free to post any questions or bug reports on the issues page!
This project uses eslint
and typescript
for development, tape
for unit tests, and rollup
for bundling releases.
1npm run test 2npm run release
All contributions are welcome!
Use this code however you like! No warranty!
No vulnerabilities found.
No security vulnerabilities found.