Registry / serialization / bytesish

bytesish

JSON →
library0.4.4jsnpmunverified

`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.

npm install bytesish
INSTALL
IMPORT
SIG · BYTESISH
B
bytesish
serializationjavascriptv0.4.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

bytes
import bytes from 'bytesish'
import { bytes } from 'bytesish'
The primary API is exported as the default, allowing direct access to the 'bytes' function and its static methods in ESM contexts.
bytes
const bytes = require('bytesish')
import bytes from 'bytesish'
Standard CommonJS import for Node.js environments. The 'bytes' object is directly available as the module's export.
bytes.toString
import bytes from 'bytesish'; bytes.toString(myView, 'base64')
import { toString } from 'bytesish'
Utility functions like `toString`, `native`, and `slice` are methods of the main `bytes` object and are not individually named exports.

Demonstrates basic instantiation, zero-copy conversions from various binary types, string encoding/decoding, and key utility functions like `native`, `slice`, and `concat`.

let bytes = require('bytesish'); // Instantiate DataView from a string (creates a copy) let view = bytes('hello world'); /* Zero-copy conversions (no new ArrayBuffer allocated) */ let bufferInstance = Buffer.from('hello world'); view = bytes(bufferInstance); // From Buffer instance to DataView let uint8ArrayInstance = (new TextEncoder()).encode('hello world'); view = bytes(uint8ArrayInstance); // From Uint8Array to DataView /* String conversions (creates a copy) */ let base64String = bytes.toString(view, 'base64'); console.log('Base64:', base64String); // SGVsbG8gd29ybGQ= // Convert back from base64 string to DataView let viewCopy = bytes(base64String, 'base64'); console.log('Decoded Base64:', bytes.toString(viewCopy, 'utf8')); // hello world // Example of converting DataView back to a native platform type (Buffer in Node.js) let nativeBuffer = bytes.native(view, 'utf8'); console.log('Native Buffer:', nativeBuffer.toString()); // hello world // Example of slicing (zero-copy if source is an ArrayBufferView) let slicedView = bytes.slice(view, 0, 5); console.log('Sliced:', bytes.toString(slicedView, 'utf8')); // hello // Example of concatenation (creates a new ArrayBuffer) let helloBytes = bytes('hello'); let worldBytes = bytes(' world'); let combinedArrayBuffer = bytes.concat([helloBytes, worldBytes]); console.log('Concatenated:', bytes.toString(combinedArrayBuffer, 'utf8')); // hello world
Debug
Known issues
gotchaBe 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.
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.
affects: >=0.1.0
gotchaWhile `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.
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.
affects: >=0.1.0
gotchaAs `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.
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.
affects: <1.0.0
Errors
Common errors & fixes
TypeError: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or ArrayBufferView. Received an instance of DataView
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`.
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.
Error: Unknown encoding: 'invalid-encoding'
Attempting to convert a binary type to a string or vice versa using an unsupported or misspelled encoding string.
fix
Ensure the `encoding` argument matches one of the supported standard encodings (e.g., 'utf8', 'hex', 'base64', 'ascii', 'latin1').
Upgrade
Version history
0.4.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
20 hits · last 30 days
node
16
OpenAI (training)
1
Resources
bytesish — npm install bytesish · libregistry