npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bytesish

v0.4.4

Published

Cross-Platform Binary API

Downloads

1,726,755

Readme

bytesish

5002 1903

If you're writing a library that needs to work in Node.js and in Browsers, it's quite difficult to figure out what "the right thing" to do with binary is.

If you want to be compatible with Node.js libraries you'll need to accept and return Buffer instances. If you want to be compatible with Browser API's you'll need to accept and return a number of types, the browser is sort of a mess when it comes to binary with many different "views" of binary data.

The moment you use the Node.js Buffer API in a library that is bundled for use in Browsers the bundler will inject a rather large polyfill for the entire Buffer API. It's quite difficult to accept and return Buffer instances while avoiding this penalty.

However, there is some good news. No matter what the binary type there's an underlying ArrayBuffer associated with the instance. There's also one generic binary view object available in both Node.js and Browsers called DataView. This means that you can take any binary type and do a zero memcopy conversion to a DataView.

But there are some problems with DataView. Not all APIs take it in browsers and almost none accept it in Node.js. It's a great API for reading and writing to an ArrayBuffer but it lacks a lot of other functionality that can be difficult to accomplish cross-platform.

bytesish is here to help. This library helps you accept and convert different binary types into a consistent type, DataView, without loading any polyfills or other dependencies, then convert back into an ideal type for the platform your library is running in.

What bytesish does:

  • Returns a DataView from any known binary type (zero copy).
  • Creates a DataView from a string with any encoding.
  • Converts any type to a string of any encoding.
  • Converts any to an ideal native object (Buffer or Uint8Array).
  • Provides utility functions for comparison, sorting, copying and slices any binary type or string.

bytesish does not create a new Binary Type for accessing and manipulating binary data, because you can just use DataView for that. bytesish tries to be a small piece of code that does not contribute any more than necessary to your bundle size. It does this by containing only the binary operations you need that are difficult to do cross-platform (Node.js and Browsers).

let bytes = require('bytesish')
let view = bytes('hello world')

/* zero copy conversions */
view = bytes(Buffer.from('hello world')) // Buffer instance
view = bytes((new TextEncoder()).encode('hello world')) // Uint8Array

/* base64 conversions */
let base64String = bytes.toString(view, 'base64')
base64String = bytes.toString(Buffer.from('hello world'), 'base64')
base64String = bytes.toString('hello world', 'base64')

/* since this is a string conversion it will create a new binary instance */
let viewCopy = bytes(base64String, 'base64')

API

Zero Copy

bytes(from)

bytes.sort(a, b)

bytes.compare(a, b)

bytes.native(from[, encoding])

bytes.slice(from[, start=0[, end=from.byteLength]])

bytes.typedArray(from[, Class=Uint8Array])

Optimized (memcopy only when necessary)

bytes.arrayBuffer(from[, encoding])

Memory Copy

All memcopy APIs return an ArrayBuffer

bytes.memcopy(from[, encoding])

Returns an ArrayBuffer copy of the given binary or string.

bytes.memcopySlice(from[, start=0[, end=from.byteLength]])

Returns an ArrayBuffer copy from a slize of the given binary or string.

bytes.concat(values)

values is an iterable of binary or string types.

Returns a newly allocated ArrayBuffer contained the concatenated binary data.

String Conversions

bytes(from[, encoding])

bytes.toString(from[, outputEncoding])