{"id":12940,"library":"bytesish","title":"bytesish: Cross-Platform Binary API","description":"`bytesish` (current stable version 0.4.4) is a utility library designed to simplify cross-platform binary data handling in JavaScript applications, specifically targeting environments compatible with both Node.js and browsers. It addresses the complexity of dealing with disparate binary types like Node.js `Buffer` and various browser `ArrayBufferView` types (e.g., `Uint8Array`) by providing a consistent `DataView` interface without introducing large polyfills. The library's core differentiator is its focus on zero-copy conversions to and from `DataView` whenever possible, minimizing memory overhead and bundle size. It offers utilities for converting between binary types and strings (with various encodings), comparison, sorting, slicing, and concatenating binary data. `bytesish` does not create a new binary data type but leverages `DataView` for direct manipulation, aiming to be a lightweight helper for common binary operations that are otherwise difficult to implement cross-platform.","status":"active","version":"0.4.4","language":"javascript","source_language":"en","source_url":"https://github.com/mikeal/bytesish","tags":["javascript"],"install":[{"cmd":"npm install bytesish","lang":"bash","label":"npm"},{"cmd":"yarn add bytesish","lang":"bash","label":"yarn"},{"cmd":"pnpm add bytesish","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary API is exported as the default, allowing direct access to the 'bytes' function and its static methods in ESM contexts.","wrong":"import { bytes } from 'bytesish'","symbol":"bytes","correct":"import bytes from 'bytesish'"},{"note":"Standard CommonJS import for Node.js environments. The 'bytes' object is directly available as the module's export.","wrong":"import bytes from 'bytesish'","symbol":"bytes","correct":"const bytes = require('bytesish')"},{"note":"Utility functions like `toString`, `native`, and `slice` are methods of the main `bytes` object and are not individually named exports.","wrong":"import { toString } from 'bytesish'","symbol":"bytes.toString","correct":"import bytes from 'bytesish'; bytes.toString(myView, 'base64')"}],"quickstart":{"code":"let bytes = require('bytesish');\n\n// Instantiate DataView from a string (creates a copy)\nlet view = bytes('hello world');\n\n/* Zero-copy conversions (no new ArrayBuffer allocated) */\nlet bufferInstance = Buffer.from('hello world');\nview = bytes(bufferInstance); // From Buffer instance to DataView\n\nlet uint8ArrayInstance = (new TextEncoder()).encode('hello world');\nview = bytes(uint8ArrayInstance); // From Uint8Array to DataView\n\n/* String conversions (creates a copy) */\nlet base64String = bytes.toString(view, 'base64');\nconsole.log('Base64:', base64String); // SGVsbG8gd29ybGQ=\n\n// Convert back from base64 string to DataView\nlet viewCopy = bytes(base64String, 'base64');\nconsole.log('Decoded Base64:', bytes.toString(viewCopy, 'utf8')); // hello world\n\n// Example of converting DataView back to a native platform type (Buffer in Node.js)\nlet nativeBuffer = bytes.native(view, 'utf8');\nconsole.log('Native Buffer:', nativeBuffer.toString()); // hello world\n\n// Example of slicing (zero-copy if source is an ArrayBufferView)\nlet slicedView = bytes.slice(view, 0, 5);\nconsole.log('Sliced:', bytes.toString(slicedView, 'utf8')); // hello\n\n// Example of concatenation (creates a new ArrayBuffer)\nlet helloBytes = bytes('hello');\nlet worldBytes = bytes(' world');\nlet combinedArrayBuffer = bytes.concat([helloBytes, worldBytes]);\nconsole.log('Concatenated:', bytes.toString(combinedArrayBuffer, 'utf8')); // hello world","lang":"javascript","description":"Demonstrates basic instantiation, zero-copy conversions from various binary types, string encoding/decoding, and key utility functions like `native`, `slice`, and `concat`."},"warnings":[{"fix":"Always check whether an API is 'Zero Copy' or 'Memory Copy' in the documentation to understand its performance characteristics, especially in performance-critical loops or large data operations.","message":"Be aware of performance implications: operations like `bytes(binary_type)` or `bytes.slice()` are typically zero-copy, operating on shared `ArrayBuffer`s. However, converting from strings (`bytes(string, encoding)`) or using `bytes.memcopy()`, `bytes.concat()` will always result in a new `ArrayBuffer` allocation and data copy. Misunderstanding this can lead to unexpected memory usage or performance bottlenecks.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Use `bytes.native(dataView)` (for `Buffer` in Node.js) or `bytes.typedArray(dataView, Uint8Array)` (for `Uint8Array`) to convert the `DataView` to the platform's ideal binary type before passing to external functions.","message":"While `bytesish` centralizes on `DataView` for internal consistency and manipulation, many existing Node.js and browser APIs (e.g., Node.js file system APIs, WebCrypto APIs) may not directly accept `DataView`. You might need to convert the `DataView` back to a `Buffer` (in Node.js via `bytes.native()`) or `Uint8Array` (in browsers) before passing it to such external APIs.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Review the changelog carefully when upgrading to new 0.x versions and consider pinning to exact versions in production environments to avoid unexpected breaking changes.","message":"As `bytesish` is currently in version 0.x, its API surface may undergo breaking changes in minor releases. While the core philosophy is stable, specific function signatures or behavior might be adjusted before a 1.0 release.","severity":"gotcha","affected_versions":"<1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Convert the `DataView` to the expected native type using `bytes.native(myView)` for Node.js `Buffer` or `bytes.typedArray(myView, Uint8Array)` for a `Uint8Array` before passing it to the external API.","cause":"Passing a `DataView` (the internal consistent type `bytesish` uses) directly to an external API that expects a different specific binary type like `Buffer` or `Uint8Array`.","error":"TypeError: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or ArrayBufferView. Received an instance of DataView"},{"fix":"Ensure the `encoding` argument matches one of the supported standard encodings (e.g., 'utf8', 'hex', 'base64', 'ascii', 'latin1').","cause":"Attempting to convert a binary type to a string or vice versa using an unsupported or misspelled encoding string.","error":"Error: Unknown encoding: 'invalid-encoding'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}