sync-kQd3KSmO.js

5.6 KB · raw

1import{n as e}from"./chunk-CilyBKbf.js";var t=e({SYNC_NATIVE_HOST_NAME:()=>n,advertise:()=>E,connectSyncHost:()=>v,connectToPeer:()=>A,disconnectFromPeer:()=>j,disconnectSyncHost:()=>y,discoverPeers:()=>O,enterPairingMode:()=>F,exitPairingMode:()=>I,getIdentityPublicKey:()=>B,getNativeIdentity:()=>x,getNativeSessionKey:()=>N,getNativeStatus:()=>V,getTrustedDevices:()=>S,isHostConnected:()=>b,onConnected:()=>W,onDisconnected:()=>K,onError:()=>J,onIncomingConnection:()=>G,onPeerDiscovered:()=>H,onPeerLost:()=>U,onSyncMessage:()=>q,removeTrustedDevice:()=>w,resolvePeerByCode:()=>z,rotateNativeIdentity:()=>T,sendSyncData:()=>M,startPairingServer:()=>L,stopAdvertising:()=>D,stopDiscovery:()=>k,stopPairingServer:()=>R,trustDevice:()=>C}),n=`com.peachpasswords.sync`,r=null,i=[],a=[],o=[],s=[],c=[],l=[],u=[],d=[];function f(e){switch(e.type){case`PEER_DISCOVERED`:{let t=X(e.peer);for(let e of a)e(t);break}case`PEER_LOST`:for(let t of o)t(e.device_id);break;case`PEER_CONNECTED`:{let t={connectionId:e.connection_id,peerAddr:e.peer_addr,isPairingOnly:e.is_pairing_only===!0,peerPublicKeyHex:e.peer_public_key_hex};for(let e of c)e(t);break}case`DISCONNECTED`:for(let t of l)t(e.connection_id);break;case`SYNC_RECEIVED`:for(let t of u)t(e.connection_id,e.data);break;case`ERROR`:m(e.message,e.connection_id??void 0);for(let t of d)t(e.message,e.connection_id??void 0);break;default:if(p(e),e.type===`CONNECTED`)for(let t of s)t(e.connection_id);break}}function p(e){let t=i.findIndex(t=>t.expected.includes(e.type));if(t===-1)return;let[n]=i.splice(t,1);clearTimeout(n.timeout),n.resolve(e)}function m(e,t){let n=i.findIndex(e=>t?e.expected.includes(`CONNECTED`):!0);if(n===-1)return;let[r]=i.splice(n,1);clearTimeout(r.timeout),r.reject(Error(e))}function h(e){let t=i;i=[];for(let n of t)clearTimeout(n.timeout),n.reject(Error(e))}function g(e){if(!r)throw Error(`sync native host is not connected`);r.postMessage(e)}function _(e,t,n=5e3){return r||v(),new Promise((r,a)=>{let o={expected:t,resolve:r,reject:a,timeout:setTimeout(()=>{i=i.filter(e=>e!==o),a(Error(`sync native host timed out waiting for ${t.join(`/`)}`))},n)};i.push(o);try{g(e)}catch(e){i=i.filter(e=>e!==o),clearTimeout(o.timeout),a(e instanceof Error?e:Error(String(e)))}})}function v(){if(!r){if(!chrome.runtime.connectNative)throw Error(`native messaging is not available in this browser context`);r=chrome.runtime.connectNative(n),r.onMessage.addListener(f),r.onDisconnect.addListener(()=>{r=null,h(`sync native host disconnected`);for(let e of l)e()})}}function y(){let e=r;r=null,h(`sync native host disconnected`),e?.disconnect()}function b(){return r!==null}async function x(){return(await _({type:`GET_IDENTITY`},[`IDENTITY`])).public_key_hex}async function S(){return(await _({type:`GET_TRUSTED_DEVICES`},[`TRUSTED_DEVICES`])).devices.map(Z)}async function C(e,t){await _({type:`TRUST_DEVICE`,public_key_hex:e,device_name:t},[`TRUST_DEVICE_OK`])}async function w(e){await _({type:`REMOVE_TRUSTED_DEVICE`,public_key_hex:e},[`REMOVE_TRUSTED_DEVICE_OK`])}async function T(){return(await _({type:`ROTATE_IDENTITY`},[`ROTATE_IDENTITY_OK`])).public_key_hex}async function E(e){return(await _({type:`ADVERTISE`,x25519_public_key:e.x25519PublicKey??``,device_id:e.deviceId,name:e.name,pairing:e.pairing??!1,port:e.port??0},[`ADVERTISING`])).port}async function D(){await _({type:`STOP_ADVERTISING`},[`STOP_ADVERTISING_OK`])}function O(){g({type:`DISCOVER`})}async function k(){await _({type:`STOP_DISCOVERY`},[`STOP_DISCOVERY_OK`])}async function A(e,t,n){return(await _({type:`CONNECT`,host:e,port:t,remote_x25519_public_key:n,local_x25519_private_key:``},[`CONNECTED`])).connection_id}function j(e){g({type:`DISCONNECT`,connection_id:e})}function M(e,t){g({type:`SYNC_SEND`,connection_id:e,data:t})}async function N(e){let t=await _({type:`GET_SESSION_KEY`,connection_id:e},[`SESSION_KEY`]);if(t.connection_id!==e)throw Error(`sync native host returned session key for unexpected connection`);let n=P(t.key);if(n.length!==32)throw Error(`sync native host returned ${n.length}-byte session key (want 32)`);return n}function P(e){let t=e.replace(/-/g,`+`).replace(/_/g,`/`).padEnd(Math.ceil(e.length/4)*4,`=`),n=atob(t),r=new Uint8Array(n.length);for(let e=0;e<n.length;e+=1)r[e]=n.charCodeAt(e);return r}async function F(){await _({type:`ENTER_PAIRING_MODE`},[`PAIRING_MODE_ENTERED`])}async function I(){await _({type:`EXIT_PAIRING_MODE`},[`PAIRING_MODE_EXITED`])}async function L(e){let t=await _({type:`START_PAIRING_SERVER`,token:e.token,device_id:e.deviceId,name:e.name},[`PAIRING_SERVER_STARTED`],15e3);return{tcpPort:t.tcp_port,wsPort:t.ws_port??void 0}}async function R(){await _({type:`STOP_PAIRING_SERVER`},[`STOP_PAIRING_SERVER_OK`])}async function z(e,t=8e3){return X((await _({type:`RESOLVE_PEER_BY_CODE`,token:e,timeout_ms:t},[`PEER_RESOLVED`],t+2e3)).peer)}async function B(){return x()}async function V(){let e=await _({type:`STATUS`},[`STATUS`]);return{advertising:e.advertising,tcpPort:e.tcp_port??void 0,wsPort:e.ws_port??void 0,connections:e.connections}}function H(e){return Y(a,e)}function U(e){return Y(o,e)}function W(e){return Y(s,e)}function G(e){return Y(c,e)}function K(e){return Y(l,e)}function q(e){return Y(u,e)}function J(e){return Y(d,e)}function Y(e,t){return e.push(t),()=>{let n=e.indexOf(t);n>=0&&e.splice(n,1)}}function X(e){return{host:e.host,port:e.port,wsPort:e.ws_port??void 0,name:e.name,pairing:e.pairing??!1,deviceId:e.device_id,x25519PublicKey:e.x25519_public_key}}function Z(e){return{publicKeyHex:e.public_key_hex,deviceName:e.device_name,pairedAt:e.paired_at,lastSeen:e.last_seen}}export{B as a,H as c,M as d,D as f,C as h,O as i,U as l,t as m,A as n,K as o,R as p,j as r,G as s,E as t,q as u};