CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-js-base64

Yet another Base64 transcoder in pure-JS with comprehensive UTF-8 support and cross-platform compatibility

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

validation-utilities.mddocs/

Validation and Utility Functions

Validate Base64 strings and access version information with comprehensive validation logic that handles multiple Base64 formats.

Capabilities

Base64 Validation

Validates whether a value is a properly formatted Base64 string.

/**
 * Check if a value is a valid Base64 string
 * @param src - A value to check (typically string, but accepts any type)
 * @returns true if the value is a valid Base64 string, false otherwise
 */
function isValid(src: unknown): boolean;

Usage Examples:

import { isValid } from "js-base64";

// Basic validation
console.log(isValid("SGVsbG8=")); // true - valid Base64
console.log(isValid("Hello")); // false - not Base64
console.log(isValid("")); // true - empty string is valid Base64 (represents empty data)

// Type checking
console.log(isValid(123)); // false - not a string
console.log(isValid(null)); // false - not a string
console.log(isValid(undefined)); // false - not a string

// Padding validation
console.log(isValid("SGVsbG8=")); // true - with proper padding
console.log(isValid("SGVsbG8")); // true - padding can be omitted
console.log(isValid("SGVsbG")); // true - partial padding omitted

// Format variations
console.log(isValid("SGVs bG8=")); // true - whitespace is allowed
console.log(isValid("SGVsbG8+")); // true - standard Base64 characters
console.log(isValid("SGVsbG8-")); // true - URL-safe Base64 characters

// Invalid formats
console.log(isValid("SGVs+-bG8=")); // false - can't mix standard and URL-safe
console.log(isValid("SGVs@bG8=")); // false - invalid character (@)
console.log(isValid("SGVs=====")); // false - too much padding

// Real-world usage
function processBase64Input(input: unknown): string | null {
  if (isValid(input)) {
    return decode(input as string);
  }
  return null;
}

// Form validation
function validateBase64Field(value: string): { isValid: boolean; error?: string } {
  if (!value.trim()) {
    return { isValid: true }; // Empty is valid
  }
  
  if (!isValid(value)) {
    return { 
      isValid: false, 
      error: "Please enter a valid Base64 string" 
    };
  }
  
  return { isValid: true };
}

Version Information

Access the library version for compatibility checking and debugging.

/**
 * Current version of the js-base64 library
 */
const version: string;

/**
 * @deprecated Use lowercase 'version' instead
 * Legacy uppercase version constant
 */
const VERSION: string;

Usage Examples:

import { version, VERSION } from "js-base64";

// Check current version
console.log(version); // "3.7.8"

// Version compatibility checking
function checkCompatibility() {
  const minVersion = "3.0.0";
  const currentVersion = version;
  
  // Simple version comparison (for more complex needs, use a semver library)
  const [major, minor] = currentVersion.split('.').map(Number);
  const [minMajor, minMinor] = minVersion.split('.').map(Number);
  
  if (major > minMajor || (major === minMajor && minor >= minMinor)) {
    console.log("Compatible version");
  } else {
    console.log("Version too old");
  }
}

// Debugging information
function getDebugInfo() {
  return {
    library: "js-base64",
    version: version,
    environment: typeof window !== 'undefined' ? 'browser' : 'node',
    hasBuffer: typeof Buffer !== 'undefined',
    hasTextEncoder: typeof TextEncoder !== 'undefined'
  };
}

// Legacy usage (deprecated)
console.log(VERSION); // "3.7.8" - same as version, but deprecated

Validation Details

What isValid() Checks

The isValid function performs comprehensive validation:

  1. Type Check: Must be a string
  2. Character Set: Only valid Base64 characters (A-Z, a-z, 0-9, +, /, =) or URL-safe characters (A-Z, a-z, 0-9, -, _, =)
  3. Format Consistency: Cannot mix standard (+/) and URL-safe (-_) characters
  4. Padding Rules: Proper padding with = characters, or no padding
  5. Whitespace Handling: Ignores whitespace for validation

Advanced Validation Examples

import { isValid, decode } from "js-base64";

// Validation with error details (custom wrapper)
function validateBase64WithDetails(input: unknown): { 
  valid: boolean; 
  reason?: string; 
  data?: string 
} {
  if (typeof input !== 'string') {
    return { valid: false, reason: 'Input must be a string' };
  }
  
  if (!isValid(input)) {
    return { valid: false, reason: 'Invalid Base64 format' };
  }
  
  try {
    const decoded = decode(input);
    return { valid: true, data: decoded };
  } catch (error) {
    return { valid: false, reason: 'Base64 decoding failed' };
  }
}

// Usage in API validation
function apiEndpoint(request: { data: string }) {
  const validation = validateBase64WithDetails(request.data);
  
  if (!validation.valid) {
    throw new Error(`Invalid input: ${validation.reason}`);
  }
  
  // Process the valid Base64 data
  return { decoded: validation.data };
}

Performance Considerations

import { isValid } from "js-base64";

// For large amounts of data, validate before expensive operations
function processBase64Data(inputs: string[]) {
  // Pre-filter valid inputs
  const validInputs = inputs.filter(isValid);
  
  // Only decode valid inputs
  return validInputs.map(decode);
}

// Quick validation for user input
function onUserInput(value: string) {
  if (value.length > 1000000) {
    // Skip validation for very large strings to avoid UI blocking
    return false;
  }
  
  return isValid(value);
}

Key Features

  • Type Safe: Accepts any type but only validates strings
  • Format Flexible: Handles both standard and URL-safe Base64
  • Whitespace Tolerant: Ignores whitespace in validation
  • Padding Flexible: Works with or without padding
  • Performance Optimized: Fast validation without full decoding
  • Version Tracking: Access to library version for compatibility checks

docs

binary-data.md

browser-compatibility.md

index.md

prototype-extensions.md

string-operations.md

validation-utilities.md

tile.json