Files
SubMiner/vendor/hachidori/extension/base64.js
T
sudacode d9fdc7ef6d feat(dictionary): add Hachidori backend support
- Add backend selection, setup gating, Anki integration, and external host support
- Add launcher flags, documentation, packaging, and focused tests
- Open on-demand overlay modals on the first attempt
2026-09-22 00:21:19 -07:00

40 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-License-Identifier: GPL-3.0-or-later
// Base64 for media, captured audio and screenshots. Uint8Array.prototype.toBase64
// and Uint8Array.fromBase64 (Chrome 143+, Firefox 133+) do in about 0.5 ms per
// megabyte what the String.fromCodePoint/btoa and atob/Uint8Array.from loops
// take 4055 ms for, with identical results; both remain as the fallback.
const CHUNK = 0x8000;
export function encodeBase64(data, { btoa: toAscii = globalThis.btoa } = {}) {
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
if (typeof bytes.toBase64 === "function") {
try {
return bytes.toBase64();
} catch {
// Fall through to the portable path.
}
}
let binary = "";
for (let offset = 0; offset < bytes.length; offset += CHUNK) {
binary += String.fromCharCode.apply(null, bytes.subarray(offset, offset + CHUNK));
}
return toAscii(binary);
}
export function decodeBase64(text, { atob: fromAscii = globalThis.atob } = {}) {
if (typeof Uint8Array.fromBase64 === "function") {
try {
return Uint8Array.fromBase64(text);
} catch {
// Let the portable path produce its own result or error.
}
}
const binary = fromAscii(text);
const bytes = new Uint8Array(binary.length);
for (let index = 0; index < binary.length; index += 1) {
bytes[index] = binary.charCodeAt(index);
}
return bytes;
}