INNER CODE UNIT · TypeScript
decodeBase64
cloudflare/web-bot-auth · packages/web-bot-auth/src/index.ts:137
function decodeBase64(value: string): Uint8Array | undefined {
const normalized = value.replace(/-/g, "+").replace(/_/g, "/");
const remainder = normalized.length % 4;
if (remainder === 1) return undefined;
const padded = normalized + "=".repeat((4 - remainder) % 4);
try {
return Uint8Array.from(atob(padded), (character) =>
character.charCodeAt(0)
);
} catch {
return undefined;
}
}
export function generateNonce(): string {
const nonce = new Uint8Array(NONCE_LENGTH_IN_BYTES);
crypto.getRandomValues(nonce);
return base64(nonce);