URL-safe base64 encoding and decoding β encode, decode, validate, JSON, hex, buffers
- π― Type-safe - Full TypeScript support
- π URL-safe - No
+,/, or=characters in output - π¦ Buffer support - Encode/decode
Uint8ArrayandBuffer - β 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
npm install @chaisser/base64-url
# or
yarn add @chaisser/base64-url
# or
pnpm add @chaisser/base64-urlimport { 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' }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.
import { encode, decode } from '@chaisser/base64-url';
encode('hello'); // 'aGVsbG8'
decode('aGVsbG8'); // 'hello'
// Unicode
encode('hΓ©llo wΓΆrld π'); // URL-safe outputimport { 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]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=='import { isBase64Url, validate } from '@chaisser/base64-url';
isBase64Url('aGVsbG8'); // true
isBase64Url('a+b/c='); // false
validate('aGVsbG8'); // ok
validate('a+b/c='); // throws Errorimport { encodeJSON, decodeJSON } from '@chaisser/base64-url';
const token = encodeJSON({ sub: 'user123', exp: 1700000000 });
const payload = decodeJSON<{ sub: string; exp: number }>(token);import { encodeHex, decodeHex } from '@chaisser/base64-url';
encodeHex('68656c6c6f'); // 'aGVsbG8'
decodeHex('aGVsbG8'); // '68656c6c6f'| Function | Signature | Description |
|---|---|---|
encode(input) |
(string) β string |
Encode UTF-8 string to base64url |
decode(input) |
(string) β string |
Decode base64url to UTF-8 string |
| 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 |
| Function | Signature | Description |
|---|---|---|
escape(input) |
(string) β string |
Standard base64 β URL-safe |
unescape(input) |
(string) β string |
URL-safe β standard base64 |
| Function | Signature | Description |
|---|---|---|
isBase64Url(input) |
(string) β boolean |
Check if string is valid base64url |
validate(input) |
(string) β void |
Throw if not valid base64url |
| Function | Signature | Description |
|---|---|---|
encodeJSON(obj) |
(unknown) β string |
JSON.stringify β base64url |
decodeJSON(input) |
(string) β T |
base64url β JSON.parse |
| Function | Signature | Description |
|---|---|---|
encodeHex(hex) |
(string) β string |
Hex string β base64url |
decodeHex(input) |
(string) β string |
base64url β hex string |
Explore our other utility packages in the @chaisser namespace:
- @chaisser/base64-url (this package) - URL-safe base64 encoding/decoding
- @chaisser/chunk-array - Split arrays into chunks
- @chaisser/string-wizard - Advanced string manipulation
- @chaisser/type-guard - Runtime type guards and validators
- @chaisser/uuid-v7 - Time-ordered UUID v7 generator
- @chaisser/wait-for - Promise-based wait utilities
- @chaisser/regex-humanizer - Regex to human-readable descriptions
- @chaisser/password-strength - Password strength checker
- @chaisser/human-time - Human-readable time formatting
- @chaisser/obj-path - Safe dot-notation object access
- @chaisser/debounce-throttle - Rate limiting utilities
- @chaisser/color-utils - Color conversion utilities
- @chaisser/deep-clone - Deep cloning functions
- @chaisser/array-group-by - Array grouping utilities
- @chaisser/merge-objects - Object merge utilities
- @chaisser/event-emitter - Typed event emitter
- @chaisser/unique-by - Array uniqueness utilities
- @chaisser/memoize - Function memoization with TTL and cache size limit
MIT - Free to use in personal and commercial projects
Doruk Karaboncuk doruk.karaboncuk@interaktifis.com
- GitHub: @Chaisser
- NPM: @chaisser/base64-url
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new features
- Submit pull requests
- Improve documentation
For issues, questions, or suggestions, please reach out through:
- Email: doruk.karaboncuk@interaktifis.com
- GitHub Issues: Create an issue
Made with β€οΈ by @chaisser