1import{a as e}from"./dist-Cgg5UYwv.js";import{n as t,t as n}from"./sync-wire-CJq_rgJM.js";import{i as r,n as i}from"./noise-js-CmGg_nNe.js";var a=1024*1024,o=300*1e3,s=e(`Sync`);function c(e){let t=atob(e),n=new Uint8Array(t.length);for(let e=0;e<t.length;e++)n[e]=t.charCodeAt(e);return n}function l(e){let t=``;for(let n of e)t+=String.fromCharCode(n);return btoa(t)}async function u(e){let{tcpPort:u}=await s.startPairingTcpServer();await s.startPairingBeacon({token:e.token,tcpPort:u,x25519PublicKey:e.publicKeyHex,deviceId:e.deviceId,name:e.deviceName});let f=new Uint8Array,p=[],m=!1,h=null,g=!1;function _(e){let t=new Uint8Array(f.length+e.length);t.set(f,0),t.set(e,f.length),f=t,y()}function v(){if(f.length<4)return null;let e=new DataView(f.buffer,f.byteOffset,4).getUint32(0,!1);if(e>a)throw Error(`incoming frame too large: ${e}`);if(f.length<4+e)return null;let t=f.slice(4,4+e);return f=f.slice(4+e),t}function y(){for(;p.length>0;){let e;try{e=v()}catch(e){p.shift()?.reject(e instanceof Error?e:Error(String(e)));continue}if(!e)return;p.shift().resolve(e)}}function b(e){for(;p.length>0;)p.shift().reject(e)}function x(){return m&&f.length<4?Promise.reject(h??Error(`connection closed`)):new Promise((e,t)=>{p.push({resolve:e,reject:t}),y()})}async function S(e){if(m)throw h??Error(`connection closed`);let t=e.length,n=new Uint8Array(4+t);new DataView(n.buffer).setUint32(0,t,!1),n.set(e,4),await s.sendPairingChunk({dataBase64:l(n)})}let C=await s.addListener(`pairingChunk`,e=>{if(!g)try{_(c(e.dataBase64))}catch(e){b(e instanceof Error?e:Error(String(e)))}}),w=await s.addListener(`pairingEnded`,()=>{m=!0,h??=Error(`connection closed`),b(h)}),T=await s.addListener(`pairingConnected`,()=>{E()});async function E(){let a;try{a=await i(e.privateKey,e.publicKey,S,x)}catch(e){console.warn(`[peach-pair] noise responder failed:`,e),D();return}let s=async e=>S(a.encrypt(e)),c=async()=>a.decrypt(await x()),l;try{l=(await r(a,e.privateKey,e.publicKey,s,c)).initiatorPublicKey}catch(e){console.warn(`[peach-pair] auth failed:`,e),D();return}let u;try{let e=await Promise.race([c(),new Promise((e,t)=>setTimeout(()=>t(Error(`PAIR_REQUEST timeout`)),o))]),t=n(new TextDecoder().decode(e));if(t.type!==`PAIR_REQUEST`)throw Error(`unexpected frame: ${t.type}`);u=t}catch(e){console.warn(`[peach-pair] PAIR_REQUEST read failed:`,e),D();return}if(u.public_key_hex.toLowerCase()!==d(l)){console.warn(`[peach-pair] PAIR_REQUEST public_key_hex did not match authenticated key`),D();return}e.onPairRequest?.(u.device_name);try{await s(new TextEncoder().encode(t({type:`PAIR_ACCEPT`,public_key_hex:d(e.publicKey),device_name:e.deviceName})))}catch(e){console.warn(`[peach-pair] PAIR_ACCEPT send failed:`,e),D();return}try{await e.onPaired({deviceId:u.public_key_hex,displayName:u.device_name,publicKeyHex:u.public_key_hex})}catch(e){console.warn(`[peach-pair] onPaired callback failed:`,e)}D()}async function D(){if(!g){g=!0;try{await C.remove()}catch{}try{await w.remove()}catch{}try{await T.remove()}catch{}try{await s.stopPairingServer()}catch{}m=!0,h??=Error(`pairing server stopped`),b(h)}}return{stop:D}}function d(e){return Array.from(e,e=>e.toString(16).padStart(2,`0`)).join(``)}export{u as startCapacitorPairingServer};