Package Exports
- webbuf
- webbuf/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (webbuf) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
WebBuf
WebBuf is a powerful, flexible class that extends JavaScript's Uint8Array to provide additional functionality for handling binary data. It includes methods for manipulating binary data, converting to and from different formats (e.g., base64, hex, strings), and reading/writing values in both little-endian and big-endian formats. This library is ideal for applications that need efficient and low-level control over binary data, like encoding/decoding or working with protocols.
Features
- Extended
Uint8Arraywith extra methods for binary manipulation. - Conversion methods: supports Base64, Hex, Strings, and arrays.
- Efficient concatenation and allocation methods.
- Clone and copy functionality for working with buffers.
- Support for reading and writing unsigned and signed integers in both Little Endian (LE) and Big Endian (BE) formats, including 8-bit, 16-bit, 32-bit, 64-bit, 128-bit, and 256-bit integers.
- Utility methods like
compare,equals,fill, and more.
Installation
Install via npm:
npm install webbufUsage
Basic Usage
import { WebBuf } from 'webbuf';
// Allocating a buffer
const buf = WebBuf.alloc(10);
// Filling the buffer
buf.fill(0);
// Working with base64
const base64Str = buf.toBase64();
const decodedBuf = WebBuf.fromBase64(base64Str);
// Cloning a buffer
const clonedBuf = buf.clone();
// Comparing buffers
const anotherBuf = WebBuf.alloc(10);
console.log(buf.equals(anotherBuf)); // falseConversion Examples
String to WebBuf:
const buf = WebBuf.fromString('Hello, World!'); console.log(buf.toString()); // Outputs: Hello, World!
Hex to WebBuf:
const hexBuf = WebBuf.fromHex('48656c6c6f'); console.log(hexBuf.toString()); // Outputs: Hello
Base64 to WebBuf:
const base64Buf = WebBuf.fromBase64('SGVsbG8='); console.log(base64Buf.toString()); // Outputs: Hello
Reading and Writing Data
Read/Write Integers:
const buf = WebBuf.alloc(8); // Writing a 32-bit little-endian integer buf.writeUint32LE(123456, 0); // Reading the value back const val = buf.readUint32LE(0); console.log(val); // Outputs: 123456
Working with BigInt:
const buf = WebBuf.alloc(16); // Writing a 128-bit little-endian BigInt buf.writeBigUint128LE(0x1234567890abcdef1234567890abcdefn, 0); // Reading the value back const bigVal = buf.readBigUint128LE(0); console.log(bigVal.toString(16)); // Outputs: 1234567890abcdef1234567890abcdef
API Reference
Static Methods
WebBuf.concat(list: Uint8Array[]): WebBuf
Concatenates a list ofUint8ArrayorWebBufinto a singleWebBuf.WebBuf.alloc(size: number): WebBuf
Allocates a newWebBufof the specified size.WebBuf.fromUint8Array(buffer: Uint8Array): WebBuf
Returns aWebBufthat is a view of the same data as the inputUint8Array.WebBuf.fromArray(array: number[]): WebBuf
Creates aWebBuffrom an array of numbers.WebBuf.fromString(str: string): WebBuf
Converts a string into aWebBuf.WebBuf.fromHex(hex: string): WebBuf
Converts a hex string to aWebBuf.WebBuf.fromBase64(b64: string): WebBuf
Converts a base64 string into aWebBuf.WebBuf.from(source, mapFn?, thisArg?): WebBuf
Overrides theUint8Array.frommethod to return aWebBuf.
Instance Methods
toBase64(): string
Converts theWebBufto a base64 string.toString(): string
Converts theWebBufto a UTF-8 string.toHex(): string
Converts theWebBufto a hexadecimal string.toArray(): number[]
Converts theWebBufto a standard array of numbers.clone(): WebBuf
Returns a copy of theWebBuf.compare(other: WebBuf): number
Compares two buffers lexicographically. Returns0if equal,-1ifthisis smaller, or1ifthisis larger.equals(other: WebBuf): boolean
Returnstrueif the contents of two buffers are identical.copy(target: WebBuf, targetStart?: number, sourceStart?: number, sourceEnd?: number): number
Copies a section ofWebBufdata to anotherWebBuf.
Reading/Writing Numbers
Read and write unsigned integers (8-bit, 16-bit, 32-bit, 64-bit, 128-bit, 256-bit) in both Little Endian (LE) and Big Endian (BE) formats:
readUintLE,readUintBEreadUint8,readUint16LE,readUint16BEreadUint32LE,readUint32BEreadBigUint64LE,readBigUint64BEwriteUintLE,writeUintBE,writeBigUint64LE,writeBigUint64BEwriteBigUint128LE,writeBigUint128BEwriteBigUint256LE,writeBigUint256BE
Read and write signed integers in both Little Endian and Big Endian formats:
readIntLE,readIntBEreadInt8,readInt16LE,readInt16BEreadInt32LE,readInt32BEreadBigInt64LE,readBigInt64BEreadBigInt128LE,readBigInt128BEreadBigInt256LE,readBigInt256BE
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues for any improvements or bugs.
License
This project is licensed under the MIT License.