sync-wire-O_oTp2Oy.js

1.3 KB · raw

1import{n as e}from"./chunk-CilyBKbf.js";var t=e({decodeSyncMessage:()=>a,encodeSyncMessage:()=>i,isSyncMessage:()=>o}),n=new TextEncoder,r=new TextDecoder(`utf-8`,{fatal:!0});function i(e){return u(n.encode(JSON.stringify(e)))}function a(e){try{let t=r.decode(d(e)),n=JSON.parse(t);if(o(n))return n}catch{}throw Error(`invalid sync message`)}function o(e){if(!s(e)||typeof e.type!=`string`)return!1;switch(e.type){case`SYNC_VAULT_SEND`:return c(e.vault_a)&&c(e.vault_b)&&l(e.sent_at);case`SYNC_VAULT_ACK`:return l(e.received_at);case`SYNC_ERROR`:return c(e.reason);case`PAIR_REQUEST`:return c(e.public_key_hex)&&c(e.device_name);case`PAIR_ACCEPT`:return c(e.public_key_hex)&&c(e.device_name);case`PAIR_REJECT`:return c(e.reason);default:return!1}}function s(e){return!!e&&typeof e==`object`&&!Array.isArray(e)}function c(e){return typeof e==`string`}function l(e){return typeof e==`number`&&Number.isFinite(e)}function u(e){let t=``;for(let n of e)t+=String.fromCharCode(n);return btoa(t).replace(/\+/g,`-`).replace(/\//g,`_`).replace(/=+$/g,``)}function d(e){if(!/^[A-Za-z0-9_-]*$/.test(e)||e.length%4==1)throw Error(`invalid sync message`);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}export{i as n,t as r,a as t};