/** Read the RFC 5987 filename first to preserve non-ASCII names. */ export function filenameFromDisposition( header: string | null, fallback = "export", ): string { if (!header) return fallback; const encoded = /filename\*=UTF-8''([^;]+)/i.exec(header); if (encoded?.[1]) { try { return decodeURIComponent(encoded[1].trim()); } catch { // Malformed percent-encoding — fall through to the plain form. } } const plain = /filename="?([^";]+)"?/i.exec(header); return plain?.[1]?.trim() || fallback; }