{"version":3,"file":"base64-CvLhUKQv.mjs","names":[],"sources":["../src/utils/base64.ts"],"sourcesContent":["/**\n * Base64 encoding/decoding utilities.\n *\n * Uses native Uint8Array.prototype.toBase64 / Uint8Array.fromBase64 when\n * available (workerd, Node 26+, modern browsers), falls back to btoa/atob.\n *\n * All base64url encoding uses the { alphabet: \"base64url\" } option natively\n * or manual character replacement as fallback.\n *\n * Delete the fallback paths when the minimum Node version supports these\n * methods natively.\n */\n\nconst hasNative =\n\ttypeof Uint8Array.prototype.toBase64 === \"function\" &&\n\ttypeof Uint8Array.fromBase64 === \"function\";\n\n// Regex patterns for base64url character replacement\nconst BASE64_PLUS_PATTERN = /\\+/g;\nconst BASE64_SLASH_PATTERN = /\\//g;\nconst BASE64_PADDING_PATTERN = /=+$/;\nconst BASE64URL_DASH_PATTERN = /-/g;\nconst BASE64URL_UNDERSCORE_PATTERN = /_/g;\n\n// ---------------------------------------------------------------------------\n// Standard base64 (for opaque tokens, cursors, Basic Auth, etc.)\n// ---------------------------------------------------------------------------\n\n/** Encode a UTF-8 string as standard base64. */\nexport function encodeBase64(str: string): string {\n\tconst bytes = new TextEncoder().encode(str);\n\tif (hasNative) return bytes.toBase64();\n\tlet binary = \"\";\n\tfor (const b of bytes) binary += String.fromCharCode(b);\n\treturn btoa(binary);\n}\n\n/** Decode a standard base64 string to a UTF-8 string. */\nexport function decodeBase64(base64: string): string {\n\treturn new TextDecoder().decode(decodeBase64Bytes(base64));\n}\n\n/** Decode a standard base64 string to raw bytes (for binary payloads). */\nexport function decodeBase64Bytes(base64: string): Uint8Array {\n\tif (hasNative) return Uint8Array.fromBase64(base64);\n\tconst binary = atob(base64);\n\tconst bytes = new Uint8Array(binary.length);\n\tfor (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);\n\treturn bytes;\n}\n\n// ---------------------------------------------------------------------------\n// Base64url (for tokens, HMAC signatures, PKCE, etc.)\n// ---------------------------------------------------------------------------\n\n/** Encode bytes as base64url without padding. */\nexport function encodeBase64url(bytes: Uint8Array): string {\n\tif (hasNative) return bytes.toBase64({ alphabet: \"base64url\", omitPadding: true });\n\tlet binary = \"\";\n\tfor (const b of bytes) binary += String.fromCharCode(b);\n\treturn btoa(binary)\n\t\t.replace(BASE64_PLUS_PATTERN, \"-\")\n\t\t.replace(BASE64_SLASH_PATTERN, \"_\")\n\t\t.replace(BASE64_PADDING_PATTERN, \"\");\n}\n\n/** Decode a base64url string (with or without padding) to bytes. */\nexport function decodeBase64url(encoded: string): Uint8Array {\n\tif (hasNative) return Uint8Array.fromBase64(encoded, { alphabet: \"base64url\" });\n\tconst base64 = encoded\n\t\t.replace(BASE64URL_DASH_PATTERN, \"+\")\n\t\t.replace(BASE64URL_UNDERSCORE_PATTERN, \"/\");\n\tconst padded = base64 + \"=\".repeat((4 - (base64.length % 4)) % 4);\n\tconst binary = atob(padded);\n\tconst bytes = new Uint8Array(binary.length);\n\tfor (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);\n\treturn bytes;\n}\n"],"mappings":";;;;;;;;;;;;;AAaA,MAAM,YACL,OAAO,WAAW,UAAU,aAAa,cACzC,OAAO,WAAW,eAAe;AAGlC,MAAM,sBAAsB;AAC5B,MAAM,uBAAuB;AAC7B,MAAM,yBAAyB;AAC/B,MAAM,yBAAyB;AAC/B,MAAM,+BAA+B;;AAOrC,SAAgB,aAAa,KAAqB;CACjD,MAAM,QAAQ,IAAI,aAAa,CAAC,OAAO,IAAI;AAC3C,KAAI,UAAW,QAAO,MAAM,UAAU;CACtC,IAAI,SAAS;AACb,MAAK,MAAM,KAAK,MAAO,WAAU,OAAO,aAAa,EAAE;AACvD,QAAO,KAAK,OAAO;;;AAIpB,SAAgB,aAAa,QAAwB;AACpD,QAAO,IAAI,aAAa,CAAC,OAAO,kBAAkB,OAAO,CAAC;;;AAI3D,SAAgB,kBAAkB,QAA4B;AAC7D,KAAI,UAAW,QAAO,WAAW,WAAW,OAAO;CACnD,MAAM,SAAS,KAAK,OAAO;C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