|
2 | 2 |
|
3 | 3 | Various helper utilities for working with buffers and binary data in TypeScript. |
4 | 4 |
|
| 5 | +##Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @jsonjoy.com/buffers |
| 9 | +``` |
| 10 | + |
| 11 | +##Features |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +##Core Classes |
| 16 | + |
| 17 | +###Writer |
| 18 | + |
| 19 | +A growable binary data writer with automatic buffer expansion. |
| 20 | + |
| 21 | +```typescript |
| 22 | +import {Writer}from'@jsonjoy.com/buffers/lib/Writer'; |
| 23 | + |
| 24 | +const writer=newWriter(); |
| 25 | +writer.u8(0x42);// Write unsigned 8-bit integer |
| 26 | +writer.u16(0x1234);// Write unsigned 16-bit integer |
| 27 | +writer.u32(0x12345678);// Write unsigned 32-bit integer |
| 28 | +writer.u64(0x123456789abcdefn);// Write unsigned 64-bit integer |
| 29 | +writer.f32(3.14);// Write 32-bit float |
| 30 | +writer.f64(3.141592653589793);// Write 64-bit float |
| 31 | +writer.utf8('Hello 🌍');// Write UTF-8 string |
| 32 | +writer.ascii('Hello');// Write ASCII string |
| 33 | + |
| 34 | +const data=writer.flush();// Get written data as Uint8Array |
| 35 | +``` |
| 36 | + |
| 37 | +###Reader |
| 38 | + |
| 39 | +A binary data reader for parsing binary buffers. |
| 40 | + |
| 41 | +```typescript |
| 42 | +import {Reader}from'@jsonjoy.com/buffers/lib/Reader'; |
| 43 | + |
| 44 | +const reader=newReader(); |
| 45 | +reader.reset(someUint8Array); |
| 46 | + |
| 47 | +const byte=reader.u8();// Read unsigned 8-bit integer |
| 48 | +const word=reader.u16();// Read unsigned 16-bit integer |
| 49 | +const dword=reader.u32();// Read unsigned 32-bit integer |
| 50 | +const qword=reader.u64();// Read unsigned 64-bit integer |
| 51 | +const float=reader.f32();// Read 32-bit float |
| 52 | +const double=reader.f64();// Read 64-bit float |
| 53 | +const text=reader.utf8(5);// Read UTF-8 string of 5 bytes |
| 54 | +const ascii=reader.ascii(5);// Read ASCII string of 5 characters |
| 55 | +``` |
| 56 | + |
| 57 | +###StreamingReader |
| 58 | + |
| 59 | +A streaming binary reader that can handle data arriving in chunks. |
| 60 | + |
| 61 | +```typescript |
| 62 | +import {StreamingReader}from'@jsonjoy.com/buffers/lib/StreamingReader'; |
| 63 | + |
| 64 | +const reader=newStreamingReader(); |
| 65 | +reader.push(chunk1); |
| 66 | +reader.push(chunk2); |
| 67 | + |
| 68 | +// Read data as it becomes available |
| 69 | +const value=reader.u32(); |
| 70 | +reader.consume();// Mark consumed data for cleanup |
| 71 | +``` |
| 72 | + |
| 73 | +###StreamingOctetReader |
| 74 | + |
| 75 | +A specialized streaming reader for byte-oriented protocols with optional XOR masking. |
| 76 | + |
| 77 | +```typescript |
| 78 | +import {StreamingOctetReader}from'@jsonjoy.com/buffers/lib/StreamingOctetReader'; |
| 79 | + |
| 80 | +const reader=newStreamingOctetReader(); |
| 81 | +reader.push(dataChunk); |
| 82 | + |
| 83 | +const byte=reader.u8(); |
| 84 | +const masked=reader.bufXor(length, [0x12,0x34,0x56,0x78],0); |
| 85 | +``` |
| 86 | + |
| 87 | +##Utility Functions |
| 88 | + |
| 89 | +###Buffer Operations |
| 90 | + |
| 91 | +```typescript |
| 92 | +// Array creation and manipulation |
| 93 | +import {b}from'@jsonjoy.com/buffers/lib/b'; |
| 94 | +import {concat,concatList}from'@jsonjoy.com/buffers/lib/concat'; |
| 95 | +import {copy}from'@jsonjoy.com/buffers/lib/copy'; |
| 96 | + |
| 97 | +const buffer=b(0x48,0x65,0x6c,0x6c,0x6f);// Create from bytes |
| 98 | +const combined=concat(buffer1,buffer2);// Concatenate two buffers |
| 99 | +const list=concatList([buf1,buf2,buf3]);// Concatenate array of buffers |
| 100 | +const duplicate=copy(originalBuffer);// Copy buffer |
| 101 | +``` |
| 102 | + |
| 103 | +###Comparison Functions |
| 104 | + |
| 105 | +```typescript |
| 106 | +import {cmpUint8Array}from'@jsonjoy.com/buffers/lib/cmpUint8Array'; |
| 107 | +import {cmpUint8Array2}from'@jsonjoy.com/buffers/lib/cmpUint8Array2'; |
| 108 | +import {cmpUint8Array3}from'@jsonjoy.com/buffers/lib/cmpUint8Array3'; |
| 109 | + |
| 110 | +const isEqual=cmpUint8Array(buf1,buf2);// Returns boolean |
| 111 | +const comparison=cmpUint8Array2(buf1,buf2);// Returns -1, 0, or 1 (byte-first) |
| 112 | +const comparison2=cmpUint8Array3(buf1,buf2);// Returns -1, 0, or 1 (length-first) |
| 113 | +``` |
| 114 | + |
| 115 | +###Type Checking |
| 116 | + |
| 117 | +```typescript |
| 118 | +import {isUint8Array}from'@jsonjoy.com/buffers/lib/isUint8Array'; |
| 119 | +import {isArrayBuffer}from'@jsonjoy.com/buffers/lib/isArrayBuffer'; |
| 120 | +import {isFloat32}from'@jsonjoy.com/buffers/lib/isFloat32'; |
| 121 | + |
| 122 | +if (isUint8Array(data)) {/* data is Uint8Array or Buffer*/ } |
| 123 | +if (isArrayBuffer(data)) {/* data is ArrayBuffer*/ } |
| 124 | +if (isFloat32(3.14)) {/* number can fit in float32*/ } |
| 125 | +``` |
| 126 | + |
| 127 | +###Conversion Functions |
| 128 | + |
| 129 | +```typescript |
| 130 | +import {toUint8Array}from'@jsonjoy.com/buffers/lib/toUint8Array'; |
| 131 | +import {bufferToUint8Array}from'@jsonjoy.com/buffers/lib/bufferToUint8Array'; |
| 132 | +import {toBuf}from'@jsonjoy.com/buffers/lib/toBuf'; |
| 133 | + |
| 134 | +const uint8=toUint8Array(data);// Convert various types to Uint8Array |
| 135 | +const converted=bufferToUint8Array(buf);// Convert Buffer to Uint8Array |
| 136 | +const encoded=toBuf('Hello 🌍');// Convert string to UTF-8 bytes |
| 137 | +``` |
| 138 | + |
| 139 | +###String Utilities |
| 140 | + |
| 141 | +```typescript |
| 142 | +import {ascii,utf8}from'@jsonjoy.com/buffers/lib/strings'; |
| 143 | + |
| 144 | +const asciiBytes=ascii`Hello World`;// ASCII string to bytes |
| 145 | +const utf8Bytes=utf8`Hello 🌍`;// UTF-8 string to bytes |
| 146 | +``` |
| 147 | + |
| 148 | +##UTF-8 Encoding/Decoding |
| 149 | + |
| 150 | +###High-Performance UTF-8 Decoding |
| 151 | + |
| 152 | +```typescript |
| 153 | +import {decodeUtf8}from'@jsonjoy.com/buffers/lib/utf8/decodeUtf8'; |
| 154 | + |
| 155 | +const text=decodeUtf8(uint8Array,offset,length); |
| 156 | +``` |
| 157 | + |
| 158 | +The package includes multiple optimized UTF-8 decoding implementations that automatically choose the best strategy based on: |
| 159 | +- Environment (Node.js vs Browser) |
| 160 | +- String length |
| 161 | +- Available APIs |
| 162 | + |
| 163 | +###UTF-8 Encoding |
| 164 | + |
| 165 | +```typescript |
| 166 | +import {encode}from'@jsonjoy.com/buffers/lib/utf8/encode'; |
| 167 | + |
| 168 | +const bytesWritten=encode(targetArray,'Hello 🌍',offset,maxLength); |
| 169 | +``` |
| 170 | + |
| 171 | +###Advanced UTF-8 Features |
| 172 | + |
| 173 | +```typescript |
| 174 | +import {CachedUtf8Decoder}from'@jsonjoy.com/buffers/lib/utf8/CachedUtf8Decoder'; |
| 175 | +import {isUtf8}from'@jsonjoy.com/buffers/lib/utf8/isUtf8'; |
| 176 | +import {decodeAscii}from'@jsonjoy.com/buffers/lib/utf8/decodeAscii'; |
| 177 | + |
| 178 | +const decoder=newCachedUtf8Decoder(); |
| 179 | +const text=decoder.decode(uint8Array,start,length); |
| 180 | + |
| 181 | +const isValidUtf8=isUtf8(uint8Array); |
| 182 | +const asciiText=decodeAscii(uint8Array,start,length); |
| 183 | +``` |
| 184 | + |
| 185 | +##Special Data Types |
| 186 | + |
| 187 | +###Slice |
| 188 | + |
| 189 | +A lightweight view into a buffer without copying data. |
| 190 | + |
| 191 | +```typescript |
| 192 | +import {Slice}from'@jsonjoy.com/buffers/lib/Slice'; |
| 193 | + |
| 194 | +const slice=newSlice(uint8Array,dataView,start,end); |
| 195 | +const subarray=slice.subarray();// Get the actual data |
| 196 | +``` |
| 197 | + |
| 198 | +###Float16 Support |
| 199 | + |
| 200 | +```typescript |
| 201 | +import {decodeF16}from'@jsonjoy.com/buffers/lib/f16'; |
| 202 | + |
| 203 | +const float32Value=decodeF16(binaryF16Value); |
| 204 | +``` |
| 205 | + |
| 206 | +##Debugging Utilities |
| 207 | + |
| 208 | +```typescript |
| 209 | +import {printOctets}from'@jsonjoy.com/buffers/lib/printOctets'; |
| 210 | + |
| 211 | +console.log(printOctets(uint8Array,16));// Print hex dump of first 16 bytes |
| 212 | +``` |
| 213 | + |
| 214 | +##Performance |
| 215 | + |
| 216 | +This library is designed for high performance with: |
| 217 | + |
| 218 | +-**Optimized UTF-8 handling**: Multiple implementations that choose the fastest method for each environment |
| 219 | +-**Minimal allocations**: Reusable readers and writers with buffer pooling |
| 220 | +-**Zero-copy operations**: Slices and views avoid unnecessary data copying |
| 221 | +-**Environment-specific optimizations**: Leverages Node.js Buffer APIs when available |
| 222 | + |
| 223 | +##Browser Support |
| 224 | + |
| 225 | +Works in all modern browsers and Node.js environments. The library automatically detects available APIs and chooses the most appropriate implementation. |
| 226 | + |
| 227 | +##TypeScript Support |
| 228 | + |
| 229 | +Full TypeScript support with comprehensive type definitions included. |
| 230 | + |
| 231 | +##License |
| 232 | + |
| 233 | +Apache-2.0 |
| 234 | + |