import type { PersonaProtocol } from '../types'; import { PersonaRESTProtocol, PersonaWebRTCProtocol, PersonaWebSocketProtocol } from '../protocol'; /** * Converts a File object to a base64 string */ export function fileToBase64(file: File): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => { const base64 = reader.result as string; const base64WithoutPrefix = base64.split(';base64,')[1]; resolve(base64WithoutPrefix); }; reader.onerror = (error) => { reject(error); }; }); } /** * Extracts the first available protocol endpoint URL */ export function getFirstAvailableProtocolEndpoint(protocols: PersonaProtocol[]): string | null { const extractors: { [key: string]: (protocol: PersonaProtocol) => string } = { rest: (protocol) => (protocol as PersonaRESTProtocol).config.apiUrl, webrtc: (protocol) => (protocol as PersonaWebRTCProtocol).config.webrtcUrl.replace('/webrtc', '').replace('ws://', 'http://').replace('wss://', 'https://'), websocket: (protocol) => (protocol as PersonaWebSocketProtocol).config.webSocketUrl .replace('/websocket', '') .replace('ws://', 'http://') .replace('wss://', 'https://'), }; for (const protocol of protocols) { const extractor = extractors[protocol.getName()]; if (extractor) { return extractor(protocol); } } return null; }