import type {} from './browser-context.ts'; interface Exchange { offer: string; answer: string; senderFingerprints: string[]; receiverFingerprints: string[]; senderResult: { ok: boolean }; } // Serialized by the runner; all runtime dependencies live in this function or window. export async function crossBrowserDriver(provider: string) { const role = new URL(location.href).searchParams.get('role'); if (role !== 'sender' && role !== 'receiver') throw new Error('Invalid role'); const { click, input, wait, element } = window.__hellcon; const pause = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); const post = async (key: string, value: unknown) => fetch('/.test/' + key, {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify(value)}); const get = async (key: K): Promise => (await fetch('/.test/' + key)).json(); const stage = async (text: string) => post(role+'Stage', text); const peers: RTCPeerConnection[] = []; const NativePC = window.RTCPeerConnection; window.RTCPeerConnection = class extends NativePC { constructor(config?: RTCConfiguration) { super(config); peers.push(this); } }; setInterval(()=>post(role+'Diagnostic',{status:document.querySelector('[role=status]')?.textContent,error:document.querySelector('[role=alert]')?.textContent,download:!!document.querySelector('a.download')}),2000); try { await wait(()=>document.querySelector('#stun-provider')); if(role==='receiver'){click('Receive');await pause(20);} if(role==='sender'){ window.__hellcon.chooseFile(2*1024*1024+7, 'cross-browser.bin'); await pause(20); } input('#stun-provider',provider);await pause(20); if(role==='sender'){ await stage('creating offer'); click('Create code');await wait(()=>document.querySelector('#output-code')); await post('offer',element('#output-code').value); await stage('waiting for answer'); const answer=await wait(()=>get('answer'));input('#answer-input',answer);await pause(20);click('Connect'); }else{ await stage('waiting for offer'); const offer=await wait(()=>get('offer'));input('#offer-input',offer);await pause(20); await stage('creating answer');click('Accept & create answer');await wait(()=>document.querySelector('#output-code')); await post('answer',element('#output-code').value); } await stage('comparing fingerprints');await wait(()=>document.querySelector('.fingerprint')); const fps=[...document.querySelectorAll('.fp-row code')].map(el=>el.textContent); await post(role+'Fingerprints',fps); const other=await wait(()=>get(role === 'sender' ? 'receiverFingerprints' : 'senderFingerprints')); if(fps[0]!==other[1]||fps[1]!==other[0])throw new Error('Fingerprints do not match'); click('Both fingerprints match — confirm'); if(role==='receiver'){ await stage('waiting for metadata');await wait(()=>document.querySelector('.incoming'));click('Receive in memory (up to 64 MiB)'); await stage('receiving');await wait(()=>document.querySelector('a.download')); await wait(()=>get('senderResult')); await pause(250); if(!document.querySelector('a.download'))throw new Error('Download was revoked after sender shutdown'); await stage('checking downloaded bytes'); const bytes=new Uint8Array(await (await fetch(element('a.download').href)).arrayBuffer()); if(bytes.length!==2*1024*1024+7||!bytes.every(b=>b===123))throw new Error('File content mismatch'); }else{ await stage('waiting for receipt');await wait(()=>element('[role=status]').textContent?.includes('confirmed the complete file')); click('Disconnect'); } await post(role+'Result',{ok:true}); }catch(error){ await post(role+'Result',{ok:false,error:error instanceof Error ? error.message : String(error),peers:peers.map(pc=>({connection:pc.connectionState,gathering:pc.iceGatheringState,ice:pc.iceConnectionState,localFingerprints:pc.localDescription?.sdp.match(/^a=fingerprint:.*$/gm),remoteFingerprints:pc.remoteDescription?.sdp.match(/^a=fingerprint:.*$/gm)}))}); } }