buff
The swiss-army-knife of byte manipulation.
Features:
- Move between data formats with ease!
- Encode / decode between Base58, Base64, Bech32 and more.
Buff
object recognized as native Uint8Array
.
- Prepend, append, sort, split and join arrays of multiple formats.
- Read and prefix varints.
- Convert blobs of data into consumable streams.
- Uses
DataView.setUint8
for ultra-fast performace.
- Supports endianess for all the things!
How to Import
This library is designed to support classic and modern ESM imports, in both a nodejs and browser environment.
Example install via NPM or yarn:
npm install @cmdcode/buff || yarn add @cmdcode/buff
Classic import into a nodejs project:
const { Buff, Bytes } = require('@cmdcode/buff')
Modern import into an nodejs project:
import { Buff, Bytes } from '@cmdcode/buff'
Classic import into a browser-based project:
<script src="https://unpkg.com/@cmdcode/buff/dist/browser.js"></script>
<script>
const { Buff, Bytes } = window.buff
</script>
Modern import into a browser-based project:
<script type="module">
import { Buff, Bytes } from "https://unpkg.com/@cmdcode/buff/dist/module.mjs"
</script>
How to Use
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.
import { Buff, Bytes } from '@cmdcode/buff'
type Bytes = string | number | bigint | Uint8Array | Buff
type Endian = 'le' | 'be'
const bytes = new Buff (
data : Bytes | Bytes[] | ArrayBuffer,
size ?: number,
endian ?: Endian
)
You can convert from many different types and formats into a Buff
object.
Buff
.any = (data : any, size ?: number) => Buff,
.raw = (data : Uint8Array, size ?: number) => Buff,
.str = (data : string, size ?: number) => Buff,
.hex = (data : string, size ?: number) => Buff,
.bin = (data : string, size ?: number) => Buff,
.num = (data : number, size ?: number) => Buff,
.big = (data : bigint, size ?: number) => Buff,
.bytes = (data : Bytes, size ?: number) => Buff,
.json = (data : T, replacer ?: Replacer) => Buff,
.b58chk = (data : string) => Buff,
.base64 = (data : string) => Buff,
.b64url = (data : string) => Buff,
.bech32 = (
data : string,
limit ?: number,
chk_prefix ?: string
) => Buff,
.bech32m = (
data : string,
limit ?: number,
chk_prefix ?: string
) => Buff
}
With Buff
, you have access to an extensive API for converting between formats.
const bytes = new Buff(data)
bytes
.arr => number[]
.num => number
.big => bigint
.str => string
.hex => string
.raw => Uint8Array
.bin => string
.b58chk => string
.base64 => string
.b64url => string
.digest => Buff
.id => string
.stream => Stream
bytes
.toNum : (endian ?: Endian) => number
.toBig : (endian ?: Endian) => bigint
.toBin : () => string
.toHash : () => Buff
.toJson : (reviver ?: Reviver) => T
.toBech32 : (prefix : string, limit ?: number) => string
.toBech32m : (prefix : string, limit ?: number) => string
In addition to format conversion, you can perform many other convenient tasks.
Buff = {
encode : (str : string) => Uint8Array,
decode : (bytes : Uint8Array) => string,
from (data : Uint8Array | number[]) => Buff
of (...data : number[]) => Buff,
join : (array : Bytes[]) => Buff,
sort (arr : Bytes[], size ?: number) => Buff[],
random (size : number) => Buff,
varInt : (num : number, endian ?: Endian) => Buff
}
const bytes = new Buff(data)
bytes
.append (data : Bytes) => Buff
.prepend (data : Bytes) => Buff
.prefixSize (endian ?: Endian) => Buff
.reverse () => Buff
.set (array : ArrayLike<number>, offset ?: number) => void
.slice (start ?: number, end ?: number) => Buff
.subarray (begin ?: number, end ?: number) => Buff
.write (data : Bytes, offset ?: number) => void
The Stream
tool will take a blob of data and allow you to consume it byte-per-byte.
import { Stream } from '@cmdcode/buff'
const stream = new Stream(data)
const stream = new Buff(data).stream
stream
.peek(size: number) => Buff
.read(size: number) => bytes
.readSize (endian ?: Endian) => number
Dependencies
This library uses minimal dependences.
@scure/base
For performing encoding and decoding of many formats.
https://github.com/paulmillr/scure-base
@noble/hashes
For creating hashes using sha256
.
https://github.com/paulmillr/noble-hashes
Special thanks to Paul Miller for his wonderful work.
Bugs / Issues
Please feel free to post any questions or bug reports on the issues page!
Development / Testing
This project uses eslint
and typescript
for development, tape
for unit tests, and rollup
for bundling releases.
yarn test || npm run test
yarn release || npm run release
Contributions
All contributions are welcome!
License
Use this code however you like! No warranty!