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

arraybuffer-encoding

v1.1.0

Published

ArrayBuffer to base64 and hex encoding/decoding utilities

Downloads

174

Readme

npm Node.js CI License: MIT

ArrayBuffer encoding and decoding tools

This package contains utilities to encode and decode ArrayBuffer objects to base64 and hex with full support for Unicode and binary data. It's especially designed for usage in web browsers, but it works on Node.js too.

  • Encode and decode ArrayBuffer objects (and typed arrays like Uint8Array, and strings) to base64 and hex, for the browser and Node.js
  • Supports base64 "standard" and "URL-safe" encodings, with optional padding; custom base64 encodings can be defined as well
  • Clean, well-documented APIs
  • Reasonable performance

Full API documentation available at https://italypaleale.github.io/arraybuffer/

Installation

Install from NPM:

npm i arraybuffer-encoding

Base64

This package offers support for encoding and decoding base64 in two variants:

  • "Standard" base64 (RFC 4648 §4): uses + and / for characters 62 and 63 respectively. Encoded strings have = padding characters added at the end by the Encode function, if necessary.
    Import with:

    import {Encode, Decode} from 'arraybuffer-encoding/base64/standard'

    Can also use arraybuffer-encoding/base64 as alias.

  • URL-safe base64 (RFC 4648 §5): uses - and _ for characters 62 and 63 respectively. Encoded strings are never padded by the Encode function.
    Import with:

    import {Encode, Decode} from 'arraybuffer-encoding/base64/url'

In all cases, the Decode function accepts strings with or without padding.

Encoding to base64

To encode an ArrayBuffer to base64, use the Encode(ab: ArrayBuffer): string method:

import {Encode} from 'arraybuffer-encoding/base64'
const ab = new ArrayBuffer(...)
const encoded = Encode(ab)

Typed arrays, such as Uint8Array objects, can be passed as well:

import {Encode} from 'arraybuffer-encoding/base64'
const array = new Uint8Array(...)
const encoded = Encode(ab)

To encode a UTF-8 string, convert it to a typed array object first:

import {Encode} from 'arraybuffer-encoding/base64'
const str = 'Hello world'
const textEnc = new TextEncoder()
const encoded = Encode(textEnc.encode(str))

Decoding base64

To decode a base64-encoded string, use the Decode(str: string): ArrayBuffer method, which returns an ArrayBuffer object:

import {Decode} from 'arraybuffer-encoding/base64'
const encoded = 'RGV2cw'
const ab = Decode(encoded)

The decoded ArrayBuffer can be converted to a string (for example, UTF-8 encoded) with:

import {Decode} from 'arraybuffer-encoding/base64'
const encoded = 'RGV2cw'
const ab = Decode(encoded)
const textDecoder = new TextDecoder('utf-8')
const str = textDecoder.decode(ab)

Custom base64 charset

If needed, you can define your own base64 charset by using the Encoding class in arraybuffer-encoding/base64/encoding:

import {Encoding} from 'arraybuffer-encoding/base64/encoding'
const base64 = new Encoding(charset, noPadding)

The constructor accepts two parameters:

  • charset: string is a string of 64 characters containing the base64 charset to use
  • noPadding: boolean if true, the Encode method never adds padding at the end of the string (note that the Decode method always considers padding optional)

The object contains two methods:

  • Encode(ab: ArrayBuffer): string
  • Decode(str: string): ArrayBuffer

Hex

To import the hex encoding and decoding utilities, use:

import {Encode, Decode} from 'arraybuffer-encoding/hex'

Encoding to hex

To encode an ArrayBuffer to hex, use the Encode(ab: ArrayBuffer): string method:

import {Encode} from 'arraybuffer-encoding/hex'
const ab = new ArrayBuffer(...)
const encoded = Encode(ab)

Typed arrays, such as Uint8Array objects, can be passed as well:

import {Encode} from 'arraybuffer-encoding/hex'
const array = new Uint8Array(...)
const encoded = Encode(ab)

To encode a UTF-8 string, convert it to a typed array object first:

import {Encode} from 'arraybuffer-encoding/hex'
const str = 'Hello world'
const textEnc = new TextEncoder()
const encoded = Encode(textEnc.encode(str))

Decoding hex

To decode a hex-encoded string, use the Decode(str: string): ArrayBuffer method, which returns an ArrayBuffer object:

import {Decode} from 'arraybuffer-encoding/hex'
const encoded = '54686520717569636b2062726f776e20f09fa68a206a756d7073206f766572203133206c617a7920f09f90b62e'
const ab = Decode(encoded)

The decoded ArrayBuffer can be converted to a string (for example, UTF-8 encoded) with:

import {Decode} from 'arraybuffer-encoding/hex'
const encoded = '54686520717569636b2062726f776e20f09fa68a206a756d7073206f766572203133206c617a7920f09f90b62e'
const ab = Decode(encoded)
const textDecoder = new TextDecoder('utf-8')
const str = textDecoder.decode(ab)

License

This package is released under a MIT license.