sync-CmdMdEzY.js

4.6 KB · raw

1var e=`com.peachpasswords.sync`,t=null,n=[],r=[],i=[],a=[],o=[],s=[],c=[],l=[];function u(e){switch(e.type){case`PEER_DISCOVERED`:{let t=H(e.peer);for(let e of r)e(t);break}case`PEER_LOST`:for(let t of i)t(e.device_id);break;case`PEER_CONNECTED`:for(let t of o)t(e.connection_id,e.peer_addr);break;case`DISCONNECTED`:for(let t of s)t(e.connection_id);break;case`SYNC_RECEIVED`:for(let t of c)t(e.connection_id,e.data);break;case`ERROR`:f(e.message,e.connection_id??void 0);for(let t of l)t(e.message,e.connection_id??void 0);break;default:if(d(e),e.type===`CONNECTED`)for(let t of a)t(e.connection_id);break}}function d(e){let t=n.findIndex(t=>t.expected.includes(e.type));if(t===-1)return;let[r]=n.splice(t,1);clearTimeout(r.timeout),r.resolve(e)}function f(e,t){let r=n.findIndex(e=>t?e.expected.includes(`CONNECTED`):!0);if(r===-1)return;let[i]=n.splice(r,1);clearTimeout(i.timeout),i.reject(Error(e))}function p(e){let t=n;n=[];for(let n of t)clearTimeout(n.timeout),n.reject(Error(e))}function m(e){if(!t)throw Error(`sync native host is not connected`);t.postMessage(e)}function h(e,r,i=5e3){return t||g(),new Promise((t,a)=>{let o={expected:r,resolve:t,reject:a,timeout:setTimeout(()=>{n=n.filter(e=>e!==o),a(Error(`sync native host timed out waiting for ${r.join(`/`)}`))},i)};n.push(o);try{m(e)}catch(e){n=n.filter(e=>e!==o),clearTimeout(o.timeout),a(e instanceof Error?e:Error(String(e)))}})}function g(){if(!t){if(!chrome.runtime.connectNative)throw Error(`native messaging is not available in this browser context`);t=chrome.runtime.connectNative(e),t.onMessage.addListener(u),t.onDisconnect.addListener(()=>{t=null,p(`sync native host disconnected`);for(let e of s)e()})}}function _(){let e=t;t=null,p(`sync native host disconnected`),e?.disconnect()}function v(){return t!==null}async function y(){return(await h({type:`GET_IDENTITY`},[`IDENTITY`])).public_key_hex}async function b(){return(await h({type:`GET_TRUSTED_DEVICES`},[`TRUSTED_DEVICES`])).devices.map(U)}async function x(e,t){await h({type:`TRUST_DEVICE`,public_key_hex:e,device_name:t},[`TRUST_DEVICE_OK`])}async function S(e){await h({type:`REMOVE_TRUSTED_DEVICE`,public_key_hex:e},[`REMOVE_TRUSTED_DEVICE_OK`])}async function C(){return(await h({type:`ROTATE_IDENTITY`},[`ROTATE_IDENTITY_OK`])).public_key_hex}async function w(e){return(await h({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 T(){await h({type:`STOP_ADVERTISING`},[`STOP_ADVERTISING_OK`])}function E(){m({type:`DISCOVER`})}async function D(){await h({type:`STOP_DISCOVERY`},[`STOP_DISCOVERY_OK`])}async function O(e,t,n){return(await h({type:`CONNECT`,host:e,port:t,remote_x25519_public_key:n,local_x25519_private_key:``},[`CONNECTED`])).connection_id}function k(e){m({type:`DISCONNECT`,connection_id:e})}function A(e,t){m({type:`SYNC_SEND`,connection_id:e,data:t})}async function j(e){let t=await h({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=M(t.key);if(n.length!==32)throw Error(`sync native host returned ${n.length}-byte session key (want 32)`);return n}function M(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 N(){let e=await h({type:`STATUS`},[`STATUS`]);return{advertising:e.advertising,tcpPort:e.tcp_port??void 0,wsPort:e.ws_port??void 0,connections:e.connections}}function P(e){return V(r,e)}function F(e){return V(i,e)}function I(e){return V(a,e)}function L(e){return V(o,e)}function R(e){return V(s,e)}function z(e){return V(c,e)}function B(e){return V(l,e)}function V(e,t){return e.push(t),()=>{let n=e.indexOf(t);n>=0&&e.splice(n,1)}}function H(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 U(e){return{publicKeyHex:e.public_key_hex,deviceName:e.device_name,pairedAt:e.paired_at,lastSeen:e.last_seen}}export{e as SYNC_NATIVE_HOST_NAME,w as advertise,g as connectSyncHost,O as connectToPeer,k as disconnectFromPeer,_ as disconnectSyncHost,E as discoverPeers,y as getNativeIdentity,j as getNativeSessionKey,N as getNativeStatus,b as getTrustedDevices,v as isHostConnected,I as onConnected,R as onDisconnected,B as onError,L as onIncomingConnection,P as onPeerDiscovered,F as onPeerLost,z as onSyncMessage,S as removeTrustedDevice,C as rotateNativeIdentity,A as sendSyncData,T as stopAdvertising,D as stopDiscovery,x as trustDevice};