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

b64web

v1.1.0

Published

Base64 and Base64URL (RFC 4648) encoding/decoding/validation for web browsers

Downloads

28

Readme

b64web

Base64 and Base64URL (RFC 4648) encoding/decoding/validation for web browsers

npm npm bundle size Top Language MIT License

Features

  • Encodes binary data or text string to Base64 or Base64URL data.
  • Decodes Base64 or Base64URL data to binary data or text string.
  • Autodetects alphabets and padding.
  • Validates Base64/Base64URL data, respects canonical encodings.
  • ES6 module, typings available

Install

npm i b64web

Usage

let b64s: string; // Base64/Base64URL encoded ASCII string
let data: ArrayBuffer; // binary data
let text: string; //text string data

Decoding Base64 data

// decode 'b64s' to binary data
data = decode(b64s);
// decode 'b64s' to binary data, convert the data to text string using text decoder 'decoder'
text = decode(b64s, decoder);

Encoding Base64 data

// encode binary data 'data' to Base64 ASCII string
b64s = encode(data);
// encode binary data 'data' to Base64URL ASCII string
b64s = encode(data, { urlsafe: true }); 
// encode text sting 'text' to binary data, convert to Base64 ASCII string
b64s = encode(text);

Validating Base64 data

// validate Base64/Base64URL encoded ASCII string
let valid = validate(b64s);

Examples

import * as B64 from 'b64web';

const s1 = 'foob';
const s2 = '\u{1F601} \u{1F4A3}!'; // '😁 💣!'
const buf = Uint32Array.from([0xFFFFFFF8]).buffer; // [248,255,255,255]

// Encode text string
const e1 = B64.encode(s1); // 'Zm9vYg=='
const e2 = B64.encode(s1, { nopadding: true }); // 'Zm9vYg'
const e3 = B64.encode(s2); // '8J+YgSDwn5KjIQ=='
const e4 = B64.encode(s2, { urlsafe: true, nopadding: true }); // '8J-YgSDwn5KjIQ'

// Decode to text string
const t1 = B64.decode(e1, 'utf-8'); // 'foob'
const t2 = B64.decode(e2, 'utf-8'); // 'foob'
const t3 = B64.decode(e3, 'utf-8'); // '😁 💣!'
const t4 = B64.decode(e4, 'utf-8'); // '😁 💣!'

// Decode to binary data
const b1 = B64.decode(e1); // [102,111,111,98]
const b2 = B64.decode(e2); // [102,111,111,98]
const b3 = B64.decode(e3); // [240,159,152,129,32,240,159,146,163,33]
const b4 = B64.decode(e4); // [240,159,152,129,32,240,159,146,163,33]

// Encode binary data
const x1 = B64.encode(b1); // 'Zm9vYg=='
const x2 = B64.encode(b3, { nopadding: true }); // '8J+YgSDwn5KjIQ'
const x3 = B64.encode(buf); // '+P///w=='
const x4 = B64.encode(buf, { urlsafe: true }); // '-P___w=='

// Validate
const v1 = B64.validate('Zm9vYg=='); // true
const v2 = B64.validate('Zm9vYg'); // true
const v3 = B64.validate('Zm9vYg', { nopadding: false }); // false, padding required
const v4 = B64.validate('Zm9vYh=='); // false, non-canonical
const v5 = B64.validate('8J-YgSDwn5KjIQ'); // true
const v6 = B64.validate('8J-YgSDwn5KjIQ', { urlsafe: false } ); // false, BASE64 required
const v7 = B64.validate('8J+YgSDwn5KjIQ', { urlsafe: true } ); // false, BASE64URL required

// TEXT <-> BINARY
const c1 = B64.stob(s1); // [102,111,111,98]
const d1 = B64.btos(c1); // 'foob'

Encoding a text string is equivalent to converting the text string to a binary buffer and encoding the buffer.

encode(text, { encoder }) === encode(stob(text, encoder))

Decoding Base64 data to text string is equivalent to decoding the data to binary buffer and converting the buffer.

decode(data, decoder) === btos(decode(data), decoder)