Files

20 lines
692 B
TypeScript

const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/** Decode standard base64 (padding and whitespace ignored), without relying on atob or Buffer. */
export function decodeBase64(text: string): Uint8Array {
const clean = text.replace(/[^A-Za-z0-9+/]/g, "");
const out = new Uint8Array(Math.floor((clean.length * 3) / 4));
let bits = 0;
let bitCount = 0;
let outIndex = 0;
for (let i = 0; i < clean.length; i++) {
bits = (bits << 6) | BASE64_ALPHABET.indexOf(clean[i]!);
bitCount += 6;
if (bitCount >= 8) {
bitCount -= 8;
out[outIndex++] = (bits >> bitCount) & 0xff;
}
}
return out.subarray(0, outIndex);
}