@kakilangit/base64
v0.2.0
Published
WebAssembly bindings for Base64 Encoder/Decoder tool
Maintainers
Readme
@kakilangit/base64
WebAssembly bindings for Base64 Encoder/Decoder tool.
Overview
This package provides fast Base64 encoding and decoding functionality compiled to WebAssembly from Rust. It supports both standard Base64 and URL-safe Base64 alphabets, with optional padding.
Installation
npm install @kakilangit/base64Usage
import init, { encode_base64, decode_base64, Base64Alphabet } from '@kakilangit/base64';
// Initialize the WASM module
await init();
// Encode with standard alphabet and padding
const encoded = encode_base64("Hello, World!", Base64Alphabet.Standard, true);
console.log(encoded.encoded); // "SGVsbG8sIFdvcmxkIQ=="
// Encode with URL-safe alphabet (no padding)
const urlSafe = encode_base64("Hello+World=/", Base64Alphabet.UrlSafe, false);
// Decode
const decoded = decode_base64("SGVsbG8sIFdvcmxkIQ==", Base64Alphabet.Standard);
console.log(decoded.decoded); // "Hello, World!"
console.log(decoded.is_valid); // trueAPI
Functions
encode_base64(input, alphabet, pad)
Encodes a string to Base64.
input(string): The string to encodealphabet(Base64Alphabet): The alphabet to usepad(boolean): Whether to add padding characters
Returns EncodeResult:
original: Original input stringencoded: Encoded Base64 stringalphabet: Alphabet name usedpadded: Whether padding was addedbyte_count: Length of input in bytes
decode_base64(input, alphabet)
Decodes a Base64 string.
input(string): The Base64 string to decodealphabet(Base64Alphabet): The alphabet to use
Returns DecodeResult:
original: Original Base64 inputdecoded: Decoded stringis_valid: Whether the input was valid Base64byte_count: Length of decoded output in bytesalphabet: Alphabet name detected/used
Convenience Functions
encode_base64_standard(input, pad)- Encode using standard alphabetencode_base64_url_safe(input, pad)- Encode using URL-safe alphabetdecode_base64_standard(input)- Decode using standard alphabetdecode_base64_url_safe(input)- Decode using URL-safe alphabet
Enums
Base64Alphabet
Standard = 0- Standard Base64 alphabet (A-Z, a-z, 0-9, +, /)UrlSafe = 1- URL-safe Base64 alphabet (A-Z, a-z, 0-9, -, _)
Features
- Fast Base64 encoding/decoding via WebAssembly
- Standard RFC 4648 Base64 alphabet support
- URL-safe Base64 alphabet (RFC 4648 section 5)
- Optional padding control
- Input validation with detailed error messages
License
MIT
