You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Various helper utilities for working with buffers and binary data in TypeScript.
Installation
npm install @jsonjoy.com/buffers
Features
This package provides high-performance utilities for working with binary data, buffers, and UTF-8 text encoding/decoding. It includes optimized implementations for both Node.js and browser environments.
Core Classes
Writer
A growable binary data writer with automatic buffer expansion.
import{Writer}from'@jsonjoy.com/buffers/lib/Writer';constwriter=newWriter();writer.u8(0x42);// Write unsigned 8-bit integerwriter.u16(0x1234);// Write unsigned 16-bit integerwriter.u32(0x12345678);// Write unsigned 32-bit integerwriter.u64(0x123456789abcdefn);// Write unsigned 64-bit integerwriter.f32(3.14);// Write 32-bit floatwriter.f64(3.141592653589793);// Write 64-bit floatwriter.utf8('Hello 🌍');// Write UTF-8 stringwriter.ascii('Hello');// Write ASCII stringconstdata=writer.flush();// Get written data as Uint8Array
A streaming binary reader that can handle data arriving in chunks.
import{StreamingReader}from'@jsonjoy.com/buffers/lib/StreamingReader';constreader=newStreamingReader();reader.push(chunk1);reader.push(chunk2);// Read data as it becomes availableconstvalue=reader.u32();reader.consume();// Mark consumed data for cleanup
StreamingOctetReader
A specialized streaming reader for byte-oriented protocols with optional XOR masking.
// Array creation and manipulationimport{b}from'@jsonjoy.com/buffers/lib/b';import{concat,concatList}from'@jsonjoy.com/buffers/lib/concat';import{copy}from'@jsonjoy.com/buffers/lib/copy';constbuffer=b(0x48,0x65,0x6c,0x6c,0x6f);// Create from bytesconstcombined=concat(buffer1,buffer2);// Concatenate two buffersconstlist=concatList([buf1,buf2,buf3]);// Concatenate array of buffersconstduplicate=copy(originalBuffer);// Copy buffer
Comparison Functions
import{cmpUint8Array}from'@jsonjoy.com/buffers/lib/cmpUint8Array';import{cmpUint8Array2}from'@jsonjoy.com/buffers/lib/cmpUint8Array2';import{cmpUint8Array3}from'@jsonjoy.com/buffers/lib/cmpUint8Array3';constisEqual=cmpUint8Array(buf1,buf2);// Returns booleanconstcomparison=cmpUint8Array2(buf1,buf2);// Returns -1, 0, or 1 (byte-first)constcomparison2=cmpUint8Array3(buf1,buf2);// Returns -1, 0, or 1 (length-first)
Type Checking
import{isUint8Array}from'@jsonjoy.com/buffers/lib/isUint8Array';import{isArrayBuffer}from'@jsonjoy.com/buffers/lib/isArrayBuffer';import{isFloat32}from'@jsonjoy.com/buffers/lib/isFloat32';if(isUint8Array(data)){/* data is Uint8Array or Buffer */}if(isArrayBuffer(data)){/* data is ArrayBuffer */}if(isFloat32(3.14)){/* number can fit in float32 */}
Conversion Functions
import{toUint8Array}from'@jsonjoy.com/buffers/lib/toUint8Array';import{bufferToUint8Array}from'@jsonjoy.com/buffers/lib/bufferToUint8Array';import{toBuf}from'@jsonjoy.com/buffers/lib/toBuf';constuint8=toUint8Array(data);// Convert various types to Uint8Arrayconstconverted=bufferToUint8Array(buf);// Convert Buffer to Uint8Arrayconstencoded=toBuf('Hello 🌍');// Convert string to UTF-8 bytes
String Utilities
import{ascii,utf8}from'@jsonjoy.com/buffers/lib/strings';constasciiBytes=ascii`Hello World`;// ASCII string to bytesconstutf8Bytes=utf8`Hello 🌍`;// UTF-8 string to bytes
A lightweight view into a buffer without copying data.
import{Slice}from'@jsonjoy.com/buffers/lib/Slice';constslice=newSlice(uint8Array,dataView,start,end);constsubarray=slice.subarray();// Get the actual data
import{printOctets}from'@jsonjoy.com/buffers/lib/printOctets';console.log(printOctets(uint8Array,16));// Print hex dump of first 16 bytes
Performance
This library is designed for high performance with:
Optimized UTF-8 handling: Multiple implementations that choose the fastest method for each environment
Minimal allocations: Reusable readers and writers with buffer pooling
Zero-copy operations: Slices and views avoid unnecessary data copying
Environment-specific optimizations: Leverages Node.js Buffer APIs when available
Browser Support
Works in all modern browsers and Node.js environments. The library automatically detects available APIs and chooses the most appropriate implementation.
TypeScript Support
Full TypeScript support with comprehensive type definitions included.
License
Apache-2.0
About
Essential Node.js and browser byte array manipulation tools