UNPKG

@liascript/exporter

Version:
1 lines 18.6 kB
function e(e,t,o,r){Object.defineProperty(e,t,{get:o,set:r,enumerable:!0,configurable:!0})}var t=("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:{}).parcelRequire55a5;t.register("b8krL",(function(o,r){e(o.exports,"Sync",(function(){return h}));var n=t("2Xs1f"),i=t("dM3DM"),s=t("5Owdb"),c=t("khIMP"),a=t("akyql");class h extends s.Sync{destroy(){var e;null!==this.syncFallbackTimer&&(clearTimeout(this.syncFallbackTimer),this.syncFallbackTimer=null),super.destroy(),null===(e=this.provider)||void 0===e||e.disconnect()}async connect(e){var t,o,r,n;if(super.connect(e),this.host=(null===(t=e.config)||void 0===t?void 0:t.host)||void 0,this.port=(null===(o=e.config)||void 0===o?void 0:o.port)?parseInt(e.config.port,10):void 0,this.peerPath=(null===(r=e.config)||void 0===r?void 0:r.path)||void 0,null===(n=e.config)||void 0===n?void 0:n.iceServers)try{this.iceServers=JSON.parse(e.config.iceServers)}catch(t){console.warn("PeerJS: invalid iceServers JSON, ignoring:",e.config.iceServers)}window.Peer?this.init(!0):this.load(["//unpkg.com/peerjs@1.5.4/dist/peerjs.min.js"],this)}init(e,t){const o=this.uniqueID();if(e&&window.Peer&&o)(async function(e){const t=(new TextEncoder).encode(e),o=await crypto.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(o)).map((e=>e.toString(16).padStart(2,"0"))).join("")})(o).then((e=>{const t={};this.host&&(t.host=this.host,t.secure=!0),void 0!==this.port&&(t.port=this.port),this.peerPath&&(t.path=this.peerPath),this.iceServers&&(t.config={iceServers:this.iceServers}),this.transport=new(0,c.PeerJSTransport)((0,i.default)({peer:window.Peer},Object.keys(t).length>0?{peerOptions:t}:{},this.password?{password:this.password}:{})),this.provider=new(0,a.GenericProvider)(this.db.doc,this.transport),this.db.setAwareness(this.provider.awareness);let o=!1;const r=()=>{o||(o=!0,null!==this.syncFallbackTimer&&(clearTimeout(this.syncFallbackTimer),this.syncFallbackTimer=null),this.sendConnect())};this.provider.on("synced",(e=>{console.log("PeerJS: document synchronized",e.synced),r()})),this.provider.on("status",(e=>{const t=e.state;console.log(`PeerJS status: ${t}`),"connected"===t?this.syncFallbackTimer=setTimeout((()=>{console.log("PeerJS: sync fallback, proceeding as first peer"),r()}),2e3):"disconnected"===t&&console.warn("PeerJS: disconnected")})),this.provider.pubsub.subscribe("*",((e,t)=>{var o;null===(o=this.onReceive)||void 0===o||o.call(this,t,e)})),this.provider.connect((0,i.default)({room:e},this.password?{password:this.password}:{}))}));else{let e="PeerJS unknown error";t?e="Could not load resource: "+t:window.Peer||(e="Could not load PeerJS library"),this.sendDisconnectError(e)}}pubsubSend(e,t){var o;this.provider&&(this.provider.pubsub.publish(e,t),this.replyOnReceive&&(null===(o=this.onReceive)||void 0===o||o.call(this,e,t)))}constructor(...e){super(...e),(0,n.default)(this,"syncFallbackTimer",null)}}})),t.register("khIMP",(function(o,r){e(o.exports,"PeerJSTransport",(function(){return i}));var n=t("dM3DM");class i{async connect(e){if(this._connected)throw new Error("Already connected");return this._room=e.room,this.coordinatorPeerId=`yjs-coordinator-${this._room}`,new Promise(((e,t)=>{try{let o=new this.options.peer(this.coordinatorPeerId,(0,n.default)({debug:this.options.debug?3:0},this.options.peerOptions));const r=setTimeout((()=>{o.destroy(),this.createRegularPeer(e,t)}),3e3);o.on("open",(t=>{clearTimeout(r),t===this.coordinatorPeerId&&(this.log("Coordinator: claimed ID"),this.peer=o,this.peerId=t,this.isCoordinator=!0,this.roomPeers.add(this.peerId),this._connected=!0,this.setupPeerDiscovery(),this.peer.on("connection",(e=>{this.handleIncomingConnection(e)})),e())})),o.on("error",(n=>{clearTimeout(r),n.message.includes("ID is taken")||n.message.includes("taken")||n.message.includes("already")||"unavailable-id"===n.type?(o.destroy(),this.createRegularPeer(e,t)):(this.log("❌ Unexpected error claiming coordinator:",n),o.destroy(),this.createRegularPeer(e,t))}))}catch(e){t(e)}}))}createRegularPeer(e,t){const o=Math.random().toString(36).substring(7);this.peerId=`yjs-${this._room}-${o}`,this.peer=new this.options.peer(this.peerId,(0,n.default)({debug:this.options.debug?3:0},this.options.peerOptions)),this.peer.on("open",(t=>{this.peerId=t,this._connected=!0,this.setupPeerDiscovery(),this.peer.on("connection",(e=>{this.handleIncomingConnection(e)})),this.setupCrossBrowserDiscovery().then((()=>{e()})).catch((t=>{this.log("⚠️ Cross-browser discovery failed, continuing with BroadcastChannel only:",t),e()}))})),this.peer.on("error",(e=>{this.log("Peer error:",e),this._connected||t(e)})),this.peer.on("disconnected",(()=>{this.log("Peer disconnected, attempting reconnect...")}))}disconnect(){if(this._connected){if(this.discoveryInterval&&(clearInterval(this.discoveryInterval),this.discoveryInterval=void 0),this.broadcastChannel&&(this.broadcastChannel.postMessage({type:"leave",peerId:this.peerId}),this.broadcastChannel.close(),this.broadcastChannel=void 0),this.coordinatorConn){try{this.sendCoordinationMessage(this.coordinatorConn,{type:"leave",peerId:this.peerId}),this.coordinatorConn.close()}catch(e){}this.coordinatorConn=void 0}if(this.isCoordinator){for(const e of this.roomPeers)if(e!==this.peerId){const t=this.peers.get(e);if(t&&t.connected)try{this.sendCoordinationMessage(t.conn,{type:"coordinator-leaving",peerId:this.peerId})}catch(e){}}this.isCoordinator=!1,this.roomPeers.clear()}for(const e of this.peers.values())try{e.conn.close()}catch(e){}this.peers.clear(),this.knownPeers.clear(),this.reElectionInProgress=!1,this.peer&&(this.peer.destroy(),this.peer=null),this._connected=!1}}onPeerConnect(e){return this._peerConnectCallback=e,()=>{this._peerConnectCallback=void 0}}send(e){if(!this._connected)return void this.log("Not connected, cannot send");const t=this.options.password?this.encrypt(e,this.options.password):e;for(const e of this.peers.values())if(e.connected)try{e.conn.send(t)}catch(t){this.log("Error sending to peer:",e.peerId,t)}}onMessage(e){return this._callback=e,()=>{this._callback=void 0}}get isConnected(){return this._connected}get connectedPeers(){return Array.from(this.peers.values()).filter((e=>e.connected)).length}setupPeerDiscovery(){if("undefined"==typeof BroadcastChannel)return void this.log("❌ BroadcastChannel not available in this browser");const e=`yjs-peerjs-${this._room}`;this.broadcastChannel=new BroadcastChannel(e),this.broadcastChannel.onmessage=e=>{const t=e.data;t.peerId!==this.peerId&&("announce"===t.type?(this.knownPeers.add(t.peerId),this.connectToPeer(t.peerId)):"leave"===t.type&&(this.knownPeers.delete(t.peerId),this.removePeer(t.peerId)))},this.announcePeer(),this.discoveryInterval=setInterval((()=>{this._connected&&this.peers.size<this.options.maxConns&&this.announcePeer()}),5e3)}announcePeer(){if(this.broadcastChannel){const e={type:"announce",peerId:this.peerId};this.broadcastChannel.postMessage(e)}}async setupCrossBrowserDiscovery(){return await new Promise((e=>setTimeout(e,500))),new Promise(((e,t)=>{let o=!1;const r=setTimeout((()=>{if(!o){o=!0;const e=new Error("Timeout connecting to coordinator");this.log("⏱️ Timeout connecting to coordinator"),t(e)}}),5e3);try{const n=this.peer.connect(this.coordinatorPeerId,{reliable:!0,serialization:"binary",metadata:{role:"peer",peerId:this.peerId}});n.on("open",(()=>{var t;if(o)return;o=!0,clearTimeout(r),this.log("Connected to coordinator"),this.coordinatorConn=n;const i={conn:n,connected:!0,peerId:this.coordinatorPeerId};this.peers.set(this.coordinatorPeerId,i),this.knownPeers.add(this.coordinatorPeerId),null===(t=this._peerConnectCallback)||void 0===t||t.call(this,this.coordinatorPeerId),this.sendCoordinationMessage(n,{type:"join",peerId:this.peerId}),n.on("data",(e=>{const t=e instanceof Uint8Array?e:new Uint8Array(e),o=this.tryDecodeCoordinationMessage(t);if(o)this.handleCoordinatorMessage(o);else if(this._callback)try{const e=this.options.password?this.decrypt(t,this.options.password):t;this._callback(e)}catch(e){this.log("Error handling coordinator data:",e)}})),n.on("close",(()=>{this.log("Coordinator disconnected"),this.peers.delete(this.coordinatorPeerId),this.knownPeers.delete(this.coordinatorPeerId),this.handleCoordinatorDisconnect()})),e()})),n.on("error",(e=>{o||(o=!0,clearTimeout(r),this.log("❌ Failed to connect to coordinator:",e.message),t(e))}))}catch(e){o||(o=!0,clearTimeout(r),this.log("⚠️ Error connecting to coordinator:",e),t(e))}}))}becomeCoordinator(){this.isCoordinator=!0,this.roomPeers.add(this.peerId),this.log("👑 I am now the coordinator. Room peers:",Array.from(this.roomPeers)),setInterval((()=>{if(this.isCoordinator){const e=Array.from(this.roomPeers).sort();e.length>0&&e[0]!==this.peerId&&(this.log("⚠️ I am no longer the lowest ID peer, stepping down as coordinator"),this.isCoordinator=!1)}}),1e4)}handleCoordinatorMessage(e){switch(e.type){case"peer-list":for(const t of e.peers)t===this.peerId||this.peers.has(t)||(this.knownPeers.add(t),this.connectToPeer(t));break;case"peer-joined":e.peerId===this.peerId||this.peers.has(e.peerId)||(this.knownPeers.add(e.peerId),this.connectToPeer(e.peerId));break;case"peer-left":this.knownPeers.delete(e.peerId),this.removePeer(e.peerId);break;case"coordinator-change":this.coordinatorPeerId=e.coordinatorId;break;case"coordinator-leaving":this.log("Coordinator leaving, re-election starting"),this.handleCoordinatorDisconnect()}}handleCoordinatorDisconnect(){if(this.reElectionInProgress)return void this.log("⏭️ Re-election already in progress, skipping");if("undefined"!=typeof navigator&&!navigator.onLine){this.log("📴 Offline – deferring re-election until network returns");const e=()=>{this.log("🌐 Network restored – resuming re-election"),this.handleCoordinatorDisconnect()};return void window.addEventListener("online",e,{once:!0})}this.log("🗳️ Coordinator disconnected, starting re-election"),this.reElectionInProgress=!0,this.coordinatorConn=void 0;const e=[this.peerId,...Array.from(this.knownPeers),...Array.from(this.peers.keys())].filter((e=>e!==this.coordinatorPeerId));e.sort();const t=e[0];this.log("🗳️ Coordinator election: Lowest peer ID is",t),t===this.peerId?(this.log("👑 I have the lowest ID, attempting to claim coordinator role"),this.transitionToCoordinator()):(this.log("⏳ Waiting for new coordinator:",t),setTimeout((()=>{this.attemptCoordinatorConnection()}),2e3))}attemptCoordinatorConnection(e=0){if(e>=5)return this.log("❌ Failed to connect to coordinator after",5,"attempts"),this.log("🔄 Attempting to become coordinator as fallback"),void this.transitionToCoordinator();this.log(`🔌 Attempting to connect to coordinator (attempt ${e+1}/5)`),this.setupCrossBrowserDiscovery().then((()=>{this.log("✅ Successfully connected to new coordinator"),this.reElectionInProgress=!1})).catch((t=>{this.log(`⚠️ Failed to connect to coordinator (attempt ${e+1}):`,t);const o=1e3*Math.pow(1.5,e);setTimeout((()=>{this.attemptCoordinatorConnection(e+1)}),o)}))}async transitionToCoordinator(){this.log("🔄 Transitioning to coordinator role...");const e=Array.from(this.peers.entries()),t=this._callback;this._room;this.peer&&this.peer.destroy(),this.peers.clear(),this._connected=!1;try{await new Promise(((o,r)=>{const i=new this.options.peer(this.coordinatorPeerId,(0,n.default)({debug:this.options.debug?3:0},this.options.peerOptions)),s=setTimeout((()=>{this.log("❌ Timeout claiming coordinator ID"),i.destroy(),r(new Error("Timeout claiming coordinator ID"))}),5e3);i.on("open",(r=>{if(clearTimeout(s),r===this.coordinatorPeerId){this.log("✅ Successfully claimed coordinator ID:",r),this.peer=i,this.peerId=r,this.isCoordinator=!0,this.roomPeers.clear(),this.roomPeers.add(this.peerId),this._connected=!0,this._callback=t,this.reElectionInProgress=!1,this.setupPeerDiscovery(),this.peer.on("connection",(e=>{this.handleIncomingConnection(e)})),this.log("🔗 Re-establishing connections with",e.length,"known peers");for(const[t,o]of e)t!==this.coordinatorPeerId&&(this.knownPeers.add(t),this.roomPeers.add(t));o()}})),i.on("error",(e=>{clearTimeout(s),this.log("❌ Error claiming coordinator ID:",e),i.destroy(),r(e)}))}))}catch(e){this.log("❌ Failed to transition to coordinator:",e),this.reElectionInProgress=!1,this.log("🔄 Falling back to regular peer"),this.reestablishAsRegularPeer()}}reestablishAsRegularPeer(){const e=Math.random().toString(36).substring(7);this.peerId=`yjs-${this._room}-${e}`,this.isCoordinator=!1,this.peer=new this.options.peer(this.peerId,(0,n.default)({debug:this.options.debug?3:0},this.options.peerOptions)),this.peer.on("open",(e=>{this.peerId=e,this._connected=!0,this.log("✅ Reestablished as regular peer:",e),this.setupPeerDiscovery(),this.peer.on("connection",(e=>{this.handleIncomingConnection(e)})),this.peer.on("disconnected",(()=>{this.log("Peer disconnected, attempting reconnect...")})),this.attemptCoordinatorConnection()})),this.peer.on("error",(e=>{this.log("❌ Error reestablishing as regular peer:",e);try{var t;null===(t=this.peer)||void 0===t||t.destroy()}catch(e){}this.peer=null,this._connected=!1;const o=()=>{this.log("🌐 Network restored – retrying reestablish as regular peer"),this.reestablishAsRegularPeer()};"undefined"==typeof navigator||navigator.onLine?setTimeout(o,3e3):window.addEventListener("online",o,{once:!0})}))}connectToPeer(e){if(e!==this.peerId&&!this.peers.has(e)&&!(this.peers.size>=this.options.maxConns||this.peerId&&this.peerId>e))try{const t=this.peer.connect(e,{reliable:!0,serialization:"binary"});if(!t)return void this.log("Failed to create connection object for:",e);this.setupConnection(t,e)}catch(t){this.log("Error connecting to peer:",e,t)}}handleIncomingConnection(e){const t=e.peer;if(this.peers.has(t))e.close();else{if(this.peers.size>=this.options.maxConns)return this.log("Max connections reached, rejecting peer:",t),void e.close();this.isCoordinator&&(this.roomPeers.add(t),e.on("close",(()=>{this.roomPeers.delete(t);for(const e of this.roomPeers)if(e!==this.peerId){const o=this.peers.get(e);if(o&&o.connected)try{this.sendCoordinationMessage(o.conn,{type:"peer-left",peerId:t})}catch(t){this.log("⚠️ Failed to notify peer of departure:",e)}}}))),this.setupConnection(e,t)}}setupConnection(e,t){const o={conn:e,connected:!1,peerId:t};this.peers.set(t,o),e.on("open",(()=>{var e;o.connected=!0,this.knownPeers.add(t),null===(e=this._peerConnectCallback)||void 0===e||e.call(this,t)})),e.on("data",(o=>{const r=o instanceof Uint8Array?o:new Uint8Array(o),n=this.tryDecodeCoordinationMessage(r);if(n){if("coordinator-change"===n.type||"coordinator-leaving"===n.type||"peer-joined"===n.type||"peer-left"===n.type||"peer-list"===n.type)return void this.handleCoordinatorMessage(n);if("join"===n.type&&this.isCoordinator){const o=Array.from(this.roomPeers).filter((e=>e!==this.peerId&&e!==t));this.sendCoordinationMessage(e,{type:"peer-list",peers:o});for(const e of this.roomPeers)if(e!==this.peerId&&e!==t){const o=this.peers.get(e);if(o&&o.connected)try{this.sendCoordinationMessage(o.conn,{type:"peer-joined",peerId:t})}catch(t){this.log("⚠️ Failed to notify peer of new joiner:",e)}}return}}if(this._callback)try{const e=this.options.password?this.decrypt(r,this.options.password):r;this._callback(e)}catch(e){this.log("Error handling peer data:",e)}})),e.on("close",(()=>{this.removePeer(t)})),e.on("error",(e=>{this.log("⚠️ Connection error:",t,e),this.removePeer(t)})),setTimeout((()=>{}),1e3)}removePeer(e){const t=this.peers.get(e);if(t){try{t.conn.close()}catch(e){}this.peers.delete(e)}}sendCoordinationMessage(e,t){try{const o=JSON.stringify(t),r=(new TextEncoder).encode(o);e.send(r)}catch(e){this.log("Error encoding coordination message:",e)}}tryDecodeCoordinationMessage(e){try{const t=(new TextDecoder).decode(e),o=JSON.parse(t);return o&&"object"==typeof o&&o.type&&"string"==typeof o.type?o:null}catch(e){return null}}encrypt(e,t){const o=this.hashPassword(t),r=new Uint8Array(e.length);for(let t=0;t<e.length;t++)r[t]=e[t]^o[t%o.length];return r}decrypt(e,t){return this.encrypt(e,t)}hashPassword(e){return(new TextEncoder).encode(e)}log(...e){this.options.debug&&console.log("[PeerJSTransport]",...e)}constructor(e){if(this._connected=!1,this._room="",this.peer=null,this.peerId="",this.peers=new Map,this.knownPeers=new Set,this.isCoordinator=!1,this.coordinatorPeerId="",this.roomPeers=new Set,this.reElectionInProgress=!1,!e.peer)throw new Error('PeerJSTransport requires the "peer" option. Please provide the PeerJS constructor: import Peer from "peerjs"; new PeerJSTransport({ peer: Peer, ... })');var t,o,r,n;this.options={peer:e.peer,peerOptions:null!==(t=e.peerOptions)&&void 0!==t?t:{},password:null!==(o=e.password)&&void 0!==o?o:"",maxConns:null!==(r=e.maxConns)&&void 0!==r?r:20+Math.floor(15*Math.random()),debug:null!==(n=e.debug)&&void 0!==n&&n}}}})),t.register("jhEmn",(function(e,t){var o,r,n=e.exports={};function i(){throw new Error("setTimeout has not been defined")}function s(){throw new Error("clearTimeout has not been defined")}function c(e){if(o===setTimeout)return setTimeout(e,0);if((o===i||!o)&&setTimeout)return o=setTimeout,setTimeout(e,0);try{return o(e,0)}catch(t){try{return o.call(null,e,0)}catch(t){return o.call(this,e,0)}}}!function(){try{o="function"==typeof setTimeout?setTimeout:i}catch(e){o=i}try{r="function"==typeof clearTimeout?clearTimeout:s}catch(e){r=s}}();var a,h=[],d=!1,l=-1;function p(){d&&a&&(d=!1,a.length?h=a.concat(h):l=-1,h.length&&u())}function u(){if(!d){var e=c(p);d=!0;for(var t=h.length;t;){for(a=h,h=[];++l<t;)a&&a[l].run();l=-1,t=h.length}a=null,d=!1,function(e){if(r===clearTimeout)return clearTimeout(e);if((r===s||!r)&&clearTimeout)return r=clearTimeout,clearTimeout(e);try{return r(e)}catch(t){try{return r.call(null,e)}catch(t){return r.call(this,e)}}}(e)}}function g(e,t){this.fun=e,this.array=t}function f(){}n.nextTick=function(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var o=1;o<arguments.length;o++)t[o-1]=arguments[o];h.push(new g(e,t)),1!==h.length||d||c(u)},g.prototype.run=function(){this.fun.apply(null,this.array)},n.title="browser",n.browser=!0,n.env={},n.argv=[],n.version="",n.versions={},n.on=f,n.addListener=f,n.once=f,n.off=f,n.removeListener=f,n.removeAllListeners=f,n.emit=f,n.prependListener=f,n.prependOnceListener=f,n.listeners=function(e){return[]},n.binding=function(e){throw new Error("process.binding is not supported")},n.cwd=function(){return"/"},n.chdir=function(e){throw new Error("process.chdir is not supported")},n.umask=function(){return 0}}));