Skip to content

Chaisser/base64-url

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” @chaisser/base64-url

URL-safe base64 encoding and decoding β€” encode, decode, validate, JSON, hex, buffers


✨ Features

  • 🎯 Type-safe - Full TypeScript support
  • πŸ”— URL-safe - No +, /, or = characters in output
  • πŸ“¦ Buffer support - Encode/decode Uint8Array and Buffer
  • βœ… Validation - Check and validate base64url strings
  • πŸ“‹ JSON helpers - Encode/decode JSON objects directly
  • πŸ”§ Hex helpers - Encode/decode hex strings
  • πŸ”„ Escape/unescape - Convert between standard and URL-safe base64
  • πŸͺΆ Zero dependencies - Lightweight and tree-shakeable
  • 🏎️ ESM + CJS - Dual module format support

πŸ“¦ Installation

npm install @chaisser/base64-url
# or
yarn add @chaisser/base64-url
# or
pnpm add @chaisser/base64-url

πŸš€ Quick Start

import { encode, decode, encodeJSON, decodeJSON } from '@chaisser/base64-url';

// Encode/decode strings
encode('hello world');   // 'aGVsbG8gd29ybGQ'
decode('aGVsbG8gd29ybGQ'); // 'hello world'

// JSON round-trip
const token = encodeJSON({ userId: 42, role: 'admin' });
// 'eyJ1c2VySWQiOjQyLCJyb2xlIjoiYWRtaW4ifQ'
const data = decodeJSON(token);
// { userId: 42, role: 'admin' }

πŸ“– What It Does

This package provides URL-safe base64 encoding and decoding utilities for JavaScript and TypeScript. It replaces + with -, / with _, and strips trailing = padding per RFC 4648 Β§5. Supports strings, buffers, hex, JSON, and validation.


πŸ’‘ Usage Examples

String Encode / Decode

import { encode, decode } from '@chaisser/base64-url';

encode('hello');         // 'aGVsbG8'
decode('aGVsbG8');       // 'hello'

// Unicode
encode('héllo wârld 🌍'); // URL-safe output

Buffer / Uint8Array

import { encodeBuffer, decodeToBuffer, decodeToUint8Array } from '@chaisser/base64-url';

const bytes = new Uint8Array([104, 101, 108, 108, 111]);
encodeBuffer(bytes);          // 'aGVsbG8'

const buf = decodeToBuffer('aGVsbG8');
buf.toString('utf-8');        // 'hello'

const arr = decodeToUint8Array('aGVsbG8');
// Uint8Array [104, 101, 108, 108, 111]

Escape / Unescape (convert formats)

import { escape, unescape } from '@chaisser/base64-url';

// Standard base64 β†’ URL-safe
escape('a+b/c==');  // 'a-b_c'

// URL-safe β†’ Standard base64 (with padding)
unescape('a-b_c');  // 'a+b/c=='

Validation

import { isBase64Url, validate } from '@chaisser/base64-url';

isBase64Url('aGVsbG8');   // true
isBase64Url('a+b/c=');    // false

validate('aGVsbG8');      // ok
validate('a+b/c=');       // throws Error

JSON Helpers

import { encodeJSON, decodeJSON } from '@chaisser/base64-url';

const token = encodeJSON({ sub: 'user123', exp: 1700000000 });
const payload = decodeJSON<{ sub: string; exp: number }>(token);

Hex Helpers

import { encodeHex, decodeHex } from '@chaisser/base64-url';

encodeHex('68656c6c6f');  // 'aGVsbG8'
decodeHex('aGVsbG8');     // '68656c6c6f'

πŸ“š API Reference

String Encoding

Function Signature Description
encode(input) (string) β†’ string Encode UTF-8 string to base64url
decode(input) (string) β†’ string Decode base64url to UTF-8 string

Buffer Encoding

Function Signature Description
encodeBuffer(input) (Uint8Array | Buffer) β†’ string Encode bytes to base64url
decodeToBuffer(input) (string) β†’ Buffer Decode to Buffer
decodeToUint8Array(input) (string) β†’ Uint8Array Decode to Uint8Array

Format Conversion

Function Signature Description
escape(input) (string) β†’ string Standard base64 β†’ URL-safe
unescape(input) (string) β†’ string URL-safe β†’ standard base64

Validation

Function Signature Description
isBase64Url(input) (string) β†’ boolean Check if string is valid base64url
validate(input) (string) β†’ void Throw if not valid base64url

JSON

Function Signature Description
encodeJSON(obj) (unknown) β†’ string JSON.stringify β†’ base64url
decodeJSON(input) (string) β†’ T base64url β†’ JSON.parse

Hex

Function Signature Description
encodeHex(hex) (string) β†’ string Hex string β†’ base64url
decodeHex(input) (string) β†’ string base64url β†’ hex string

πŸ”— Related Packages

Explore our other utility packages in the @chaisser namespace:


πŸ”’ License

MIT - Free to use in personal and commercial projects


πŸ‘¨ Developed by

Doruk Karaboncuk doruk.karaboncuk@interaktifis.com


πŸ“„ Repository


🀝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests
  • Improve documentation

πŸ“ž Support

For issues, questions, or suggestions, please reach out through:


Made with ❀️ by @chaisser

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors