All files / src/utils helpers.ts

96.15% Statements 75/78
80% Branches 8/10
100% Functions 4/4
96.15% Lines 75/78

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 791x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 23x 23x 23x 23x 23x 23x 23x 5x 5x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 2x 2x 2x 23x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 68x 68x 6x 6x 6x 6x 6x 6x 6x 6x 6x       6x  
/**
 * Utility helper functions
 */
 
/**
 * Checks if running in browser environment
 */
export function isBrowser(): boolean {
  return typeof window !== 'undefined' && typeof document !== 'undefined';
}
 
/**
 * Checks if running in Node.js environment
 */
export function isNode(): boolean {
  return typeof process !== 'undefined' && process?.versions?.node != null;
}
 
/**
 * Decodes base64 string to UTF-8 string using TextDecoder
 * Handles URL-safe base64 encoding and padding issues
 */
export function decodeBase64(base64Data: string): string {
  try {
    // Remove whitespace
    let cleaned = base64Data.trim().replace(/\s/g, '');
 
    // Handle URL-safe base64: replace - with + and _ with /
    cleaned = cleaned.replace(/-/g, '+').replace(/_/g, '/');
 
    // Add padding if needed (base64 strings must be multiple of 4)
    while (cleaned.length % 4 !== 0) {
      cleaned += '=';
    }
 
    // Decode base64 to binary string
    const binaryString = atob(cleaned);
 
    // Convert binary string to Uint8Array
    const bytes = Uint8Array.from(binaryString, (c) => c.charCodeAt(0));
 
    // Decode bytes to UTF-8 string using TextDecoder
    const decoder = new TextDecoder('utf-8');
    return decoder.decode(bytes);
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : 'Unknown error';
    throw new Error(`Failed to decode base64 data: ${errorMessage}`);
  }
}
 
/**
 * Encodes a UTF-8 string to a base64 string using TextEncoder
 
 */
export function encodeBase64(plainText: string): string {
  try {
    // Encode string to UTF-8 bytes
    const encoder = new TextEncoder();
    const bytes = encoder.encode(plainText);
 
    // Convert bytes to binary string
    let binary = '';
    for (let i = 0; i < bytes.length; i++) {
      binary += String.fromCharCode(bytes[i]);
    }
 
    // Encode binary string to base64
    const base64 = btoa(binary);
    //* Produces URL-safe base64 encoding (replaces + and / with - and _ and removes padding)
    // Convert to URL-safe base64: replace + with - and / with _ and remove =
    // base64 = base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
 
    return base64;
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : 'Unknown error';
    throw new Error(`Failed to encode base64 data: ${errorMessage}`);
  }
}