{"name":"@jsonjoy.com/buffers","version":"1.2.1","keywords":["buffer","buffers","uint8array","binary","utf8","string","encoding","decoding","reader","writer","streaming"],"author":{"url":"https://github.com/streamich","name":"streamich"},"license":"Apache-2.0","_id":"@jsonjoy.com/buffers@1.2.1","maintainers":[{"name":"json-joy-team","email":"team@jsonjoy.com"},{"name":"streamich","email":"vadimsdaleckis@gmail.com"}],"homepage":"https://github.com/jsonjoy-com/buffers","bugs":{"url":"https://github.com/jsonjoy-com/buffers/issues"},"dist":{"shasum":"8d99c7f67eaf724d3428dfd9826c6455266a5c83","tarball":"http://registry-npm.ywwl.com/@jsonjoy.com/buffers/download/@jsonjoy.com/buffers-1.2.1.tgz","fileCount":147,"integrity":"sha512-12cdlDwX4RUM3QxmUbVJWqZ/mrK6dFQH4Zxq6+r1YXKXYBNgZXndx2qbCJwh3+WWkCSn67IjnlG3XYTvmvYtgA==","signatures":[{"sig":"MEUCIGbFLQ1xLfMJHYosOdAWBB+X1462k/QUDxYcD/ltoKxnAiEAuCyWscO7Wv2jfisvXDNm2UWnkJk/1c7Dc2oX49tllOA=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":153920,"size":28959,"noattachment":false,"key":"/@jsonjoy.com/buffers/-/@jsonjoy.com/buffers-1.2.1.tgz"},"jest":{"verbose":true,"testRegex":".*/(__tests__|__jest__|demo)/.*\\.(test|spec)\\.ts$","transform":{"^.+\\.ts$":"ts-jest"},"moduleFileExtensions":["ts","js"],"testEnvironmentOptions":{"url":"http://localhost/"},"transformIgnorePatterns":[]},"main":"lib/index.js","types":"lib/index.d.ts","engines":{"node":">=10.0"},"funding":{"url":"https://github.com/sponsors/streamich","type":"github"},"gitHead":"f5595600335e369abda161f5a2bb75527a9ede97","private":false,"scripts":{"jest":"node -r ts-node/register ./node_modules/.bin/jest","lint":"yarn tslint","test":"jest --maxWorkers 7","build":"tsc --project tsconfig.build.json --module commonjs --target es2020 --outDir lib","clean":"rimraf lib typedocs coverage gh-pages yarn-error.log","tslint":"tslint 'src/**/*.{js,jsx,ts,tsx}' -t verbose --project .","test:ci":"yarn jest --maxWorkers 3 --no-cache","typedoc":"typedoc","coverage":"yarn test --collectCoverage","prettier":"prettier --ignore-path .gitignore --write \"src/**/*.{ts,tsx,js,jsx}\"","build:pages":"rimraf gh-pages && mkdir -p gh-pages && cp -r typedocs/* gh-pages && cp -r coverage gh-pages/coverage","deploy:pages":"gh-pages -d gh-pages","prettier:check":"prettier --ignore-path .gitignore --list-different 'src/**/*.{ts,tsx,js,jsx}'","publish-coverage-and-typedocs":"yarn typedoc && yarn coverage && yarn build:pages && yarn deploy:pages"},"typings":"lib/index.d.ts","_npmUser":{"name":"streamich","email":"vadimsdaleckis@gmail.com"},"prettier":{"semi":true,"useTabs":false,"tabWidth":2,"printWidth":120,"arrowParens":"always","singleQuote":true,"trailingComma":"all","bracketSpacing":false},"repository":{"url":"git+https://github.com/jsonjoy-com/buffers.git","type":"git"},"_npmVersion":"10.8.2","description":"Various helper utilities for working with buffers and binary data","directories":{},"_nodeVersion":"20.19.5","dependencies":{},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"jest":"^29.7.0","tslib":"^2.6.2","rimraf":"^5.0.0","tslint":"^6.1.3","ts-jest":"^29.1.2","ts-node":"^10.9.2","typedoc":"^0.25.12","prettier":"^3.2.5","benchmark":"^2.1.4","typescript":"^5.4.4","@types/jest":"^29.5.12","pretty-quick":"^3.1.3","json-pack-napi":"^0.0.2","@types/benchmark":"^2.1.2","tslint-config-common":"^1.6.2"},"peerDependencies":{"tslib":"2"},"_npmOperationalInternal":{"tmp":"tmp/buffers_1.2.1_1760636799701_0.15661044954095282","host":"s3://npm-registry-packages-npm-production"},"readme":"# buffers\n\nVarious helper utilities for working with buffers and binary data in TypeScript.\n\n## Installation\n\n```bash\nnpm install @jsonjoy.com/buffers\n```\n\n## Features\n\nThis 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.\n\n## Core Classes\n\n### Writer\n\nA growable binary data writer with automatic buffer expansion.\n\n```typescript\nimport {Writer} from '@jsonjoy.com/buffers/lib/Writer';\n\nconst writer = new Writer();\nwriter.u8(0x42);          // Write unsigned 8-bit integer\nwriter.u16(0x1234);       // Write unsigned 16-bit integer\nwriter.u32(0x12345678);   // Write unsigned 32-bit integer\nwriter.u64(0x123456789abcdefn); // Write unsigned 64-bit integer\nwriter.f32(3.14);         // Write 32-bit float\nwriter.f64(3.141592653589793); // Write 64-bit float\nwriter.utf8('Hello 🌍');  // Write UTF-8 string\nwriter.ascii('Hello');    // Write ASCII string\n\nconst data = writer.flush(); // Get written data as Uint8Array\n```\n\n### Reader\n\nA binary data reader for parsing binary buffers.\n\n```typescript\nimport {Reader} from '@jsonjoy.com/buffers/lib/Reader';\n\nconst reader = new Reader();\nreader.reset(someUint8Array);\n\nconst byte = reader.u8();     // Read unsigned 8-bit integer\nconst word = reader.u16();    // Read unsigned 16-bit integer\nconst dword = reader.u32();   // Read unsigned 32-bit integer\nconst qword = reader.u64();   // Read unsigned 64-bit integer\nconst float = reader.f32();   // Read 32-bit float\nconst double = reader.f64();  // Read 64-bit float\nconst text = reader.utf8(5);  // Read UTF-8 string of 5 bytes\nconst ascii = reader.ascii(5); // Read ASCII string of 5 characters\n```\n\n### StreamingReader\n\nA streaming binary reader that can handle data arriving in chunks.\n\n```typescript\nimport {StreamingReader} from '@jsonjoy.com/buffers/lib/StreamingReader';\n\nconst reader = new StreamingReader();\nreader.push(chunk1);\nreader.push(chunk2);\n\n// Read data as it becomes available\nconst value = reader.u32();\nreader.consume(); // Mark consumed data for cleanup\n```\n\n### StreamingOctetReader\n\nA specialized streaming reader for byte-oriented protocols with optional XOR masking.\n\n```typescript\nimport {StreamingOctetReader} from '@jsonjoy.com/buffers/lib/StreamingOctetReader';\n\nconst reader = new StreamingOctetReader();\nreader.push(dataChunk);\n\nconst byte = reader.u8();\nconst masked = reader.bufXor(length, [0x12, 0x34, 0x56, 0x78], 0);\n```\n\n## Utility Functions\n\n### Buffer Operations\n\n```typescript\n// Array creation and manipulation\nimport {b} from '@jsonjoy.com/buffers/lib/b';\nimport {concat, concatList} from '@jsonjoy.com/buffers/lib/concat';\nimport {copy} from '@jsonjoy.com/buffers/lib/copy';\n\nconst buffer = b(0x48, 0x65, 0x6c, 0x6c, 0x6f); // Create from bytes\nconst combined = concat(buffer1, buffer2);         // Concatenate two buffers\nconst list = concatList([buf1, buf2, buf3]);      // Concatenate array of buffers\nconst duplicate = copy(originalBuffer);           // Copy buffer\n```\n\n### Comparison Functions\n\n```typescript\nimport {cmpUint8Array} from '@jsonjoy.com/buffers/lib/cmpUint8Array';\nimport {cmpUint8Array2} from '@jsonjoy.com/buffers/lib/cmpUint8Array2';\nimport {cmpUint8Array3} from '@jsonjoy.com/buffers/lib/cmpUint8Array3';\n\nconst isEqual = cmpUint8Array(buf1, buf2);        // Returns boolean\nconst comparison = cmpUint8Array2(buf1, buf2);    // Returns -1, 0, or 1 (byte-first)\nconst comparison2 = cmpUint8Array3(buf1, buf2);   // Returns -1, 0, or 1 (length-first)\n```\n\n### Type Checking\n\n```typescript\nimport {isUint8Array} from '@jsonjoy.com/buffers/lib/isUint8Array';\nimport {isArrayBuffer} from '@jsonjoy.com/buffers/lib/isArrayBuffer';\nimport {isFloat32} from '@jsonjoy.com/buffers/lib/isFloat32';\n\nif (isUint8Array(data)) { /* data is Uint8Array or Buffer */ }\nif (isArrayBuffer(data)) { /* data is ArrayBuffer */ }\nif (isFloat32(3.14)) { /* number can fit in float32 */ }\n```\n\n### Conversion Functions\n\n```typescript\nimport {toUint8Array} from '@jsonjoy.com/buffers/lib/toUint8Array';\nimport {bufferToUint8Array} from '@jsonjoy.com/buffers/lib/bufferToUint8Array';\nimport {toBuf} from '@jsonjoy.com/buffers/lib/toBuf';\n\nconst uint8 = toUint8Array(data);           // Convert various types to Uint8Array\nconst converted = bufferToUint8Array(buf);  // Convert Buffer to Uint8Array\nconst encoded = toBuf('Hello 🌍');          // Convert string to UTF-8 bytes\n```\n\n### String Utilities\n\n```typescript\nimport {ascii, utf8} from '@jsonjoy.com/buffers/lib/strings';\n\nconst asciiBytes = ascii`Hello World`;      // ASCII string to bytes\nconst utf8Bytes = utf8`Hello 🌍`;           // UTF-8 string to bytes\n```\n\n## UTF-8 Encoding/Decoding\n\n### High-Performance UTF-8 Decoding\n\n```typescript\nimport {decodeUtf8} from '@jsonjoy.com/buffers/lib/utf8/decodeUtf8';\n\nconst text = decodeUtf8(uint8Array, offset, length);\n```\n\nThe package includes multiple optimized UTF-8 decoding implementations that automatically choose the best strategy based on:\n- Environment (Node.js vs Browser)\n- String length\n- Available APIs\n\n### UTF-8 Encoding\n\n```typescript\nimport {encode} from '@jsonjoy.com/buffers/lib/utf8/encode';\n\nconst bytesWritten = encode(targetArray, 'Hello 🌍', offset, maxLength);\n```\n\n### Advanced UTF-8 Features\n\n```typescript\nimport {CachedUtf8Decoder} from '@jsonjoy.com/buffers/lib/utf8/CachedUtf8Decoder';\nimport {isUtf8} from '@jsonjoy.com/buffers/lib/utf8/isUtf8';\nimport {decodeAscii} from '@jsonjoy.com/buffers/lib/utf8/decodeAscii';\n\nconst decoder = new CachedUtf8Decoder();\nconst text = decoder.decode(uint8Array, start, length);\n\nconst isValidUtf8 = isUtf8(uint8Array);\nconst asciiText = decodeAscii(uint8Array, start, length);\n```\n\n## Special Data Types\n\n### Slice\n\nA lightweight view into a buffer without copying data.\n\n```typescript\nimport {Slice} from '@jsonjoy.com/buffers/lib/Slice';\n\nconst slice = new Slice(uint8Array, dataView, start, end);\nconst subarray = slice.subarray(); // Get the actual data\n```\n\n### Float16 Support\n\n```typescript\nimport {decodeF16} from '@jsonjoy.com/buffers/lib/f16';\n\nconst float32Value = decodeF16(binaryF16Value);\n```\n\n## Debugging Utilities\n\n```typescript\nimport {printOctets} from '@jsonjoy.com/buffers/lib/printOctets';\n\nconsole.log(printOctets(uint8Array, 16)); // Print hex dump of first 16 bytes\n```\n\n## Performance\n\nThis library is designed for high performance with:\n\n- **Optimized UTF-8 handling**: Multiple implementations that choose the fastest method for each environment\n- **Minimal allocations**: Reusable readers and writers with buffer pooling\n- **Zero-copy operations**: Slices and views avoid unnecessary data copying\n- **Environment-specific optimizations**: Leverages Node.js Buffer APIs when available\n\n## Browser Support\n\nWorks in all modern browsers and Node.js environments. The library automatically detects available APIs and chooses the most appropriate implementation.\n\n## TypeScript Support\n\nFull TypeScript support with comprehensive type definitions included.\n\n## License\n\nApache-2.0\n\n","publish_time":1760636799909,"_cnpm_publish_time":1760636799909,"dist-tags":{"latest":"18.30.0"}}