UNPKG

@liascript/exporter

Version:
1 lines 19.2 kB
!function(e,t,o,r,n){var s="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:{},i="function"==typeof s[r]&&s[r],c=i.cache||{},a="undefined"!=typeof module&&"function"==typeof module.require&&module.require.bind(module);function h(t,o){if(!c[t]){if(!e[t]){var n="function"==typeof s[r]&&s[r];if(!o&&n)return n(t,!0);if(i)return i(t,!0);if(a&&"string"==typeof t)return a(t);var d=new Error("Cannot find module '"+t+"'");throw d.code="MODULE_NOT_FOUND",d}p.resolve=function(o){var r=e[t][1][o];return null!=r?r:o},p.cache={};var l=c[t]=new h.Module(t);e[t][0].call(l.exports,p,l,l.exports,this)}return c[t].exports;function p(e){var t=p.resolve(e);return!1===t?{}:h(t)}}h.isParcelRequire=!0,h.Module=function(e){this.id=e,this.bundle=h,this.exports={}},h.modules=e,h.cache=c,h.parent=i,h.register=function(t,o){e[t]=[function(e,t){t.exports=o},{}]},Object.defineProperty(h,"root",{get:function(){return s[r]}}),s[r]=h;for(var d=0;d<t.length;d++)h(t[d])}({kwJov:[function(e,t,o){var r=e("@parcel/transformer-js/src/esmodule-helpers.js");r.defineInteropFlag(o),r.export(o,"Sync",(()=>c));var n=e("../Base/index"),s=e("../../../../node_modules/y-generic/dist/providers/peerjs/index"),i=e("y-generic");class c extends n.Sync{syncFallbackTimer=null;destroy(){null!==this.syncFallbackTimer&&(clearTimeout(this.syncFallbackTimer),this.syncFallbackTimer=null),super.destroy(),this.provider?.disconnect()}async connect(e){if(super.connect(e),this.host=e.config?.host||void 0,this.port=e.config?.port?parseInt(e.config.port,10):void 0,this.peerPath=e.config?.path||void 0,e.config?.iceServers)try{this.iceServers=JSON.parse(e.config.iceServers)}catch{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,s.PeerJSTransport)({peer:window.Peer,...Object.keys(t).length>0?{peerOptions:t}:{},...this.password?{password:this.password}:{}}),this.provider=new(0,i.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)=>{this.onReceive?.(t,e)})),this.provider.connect({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){this.provider&&(this.provider.pubsub.publish(e,t),this.replyOnReceive&&this.onReceive?.(e,t))}}},{"../Base/index":"2cTju","../../../../node_modules/y-generic/dist/providers/peerjs/index":"bILLl","y-generic":"3M9RT","@parcel/transformer-js/src/esmodule-helpers.js":"XTfsy"}],bILLl:[function(e,t,o){var r=e("@parcel/transformer-js/src/esmodule-helpers.js");r.defineInteropFlag(o),r.export(o,"PeerJSTransport",(()=>n));class n{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, ... })');this.options={peer:e.peer,peerOptions:e.peerOptions??{},password:e.password??"",maxConns:e.maxConns??20+Math.floor(15*Math.random()),debug:e.debug??!1}}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,{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,{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",(()=>{if(o)return;o=!0,clearTimeout(r),this.log("Connected to coordinator"),this.coordinatorConn=n;const t={conn:n,connected:!0,peerId:this.coordinatorPeerId};this.peers.set(this.coordinatorPeerId,t),this.knownPeers.add(this.coordinatorPeerId),this._peerConnectCallback?.(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 n=new this.options.peer(this.coordinatorPeerId,{debug:this.options.debug?3:0,...this.options.peerOptions}),s=setTimeout((()=>{this.log("❌ Timeout claiming coordinator ID"),n.destroy(),r(new Error("Timeout claiming coordinator ID"))}),5e3);n.on("open",(r=>{if(clearTimeout(s),r===this.coordinatorPeerId){this.log("✅ Successfully claimed coordinator ID:",r),this.peer=n,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()}})),n.on("error",(e=>{clearTimeout(s),this.log("❌ Error claiming coordinator ID:",e),n.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,{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{this.peer?.destroy()}catch(e){}this.peer=null,this._connected=!1;const t=()=>{this.log("🌐 Network restored – retrying reestablish as regular peer"),this.reestablishAsRegularPeer()};"undefined"==typeof navigator||navigator.onLine?setTimeout(t,3e3):window.addEventListener("online",t,{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",(()=>{o.connected=!0,this.knownPeers.add(t),this._peerConnectCallback?.(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)}}},{"@parcel/transformer-js/src/esmodule-helpers.js":"XTfsy"}],fDdlt:[function(e,t,o){var r,n,s=t.exports={};function i(){throw new Error("setTimeout has not been defined")}function c(){throw new Error("clearTimeout has not been defined")}function a(e){if(r===setTimeout)return setTimeout(e,0);if((r===i||!r)&&setTimeout)return r=setTimeout,setTimeout(e,0);try{return r(e,0)}catch(t){try{return r.call(null,e,0)}catch(t){return r.call(this,e,0)}}}!function(){try{r="function"==typeof setTimeout?setTimeout:i}catch(e){r=i}try{n="function"==typeof clearTimeout?clearTimeout:c}catch(e){n=c}}();var h,d=[],l=!1,p=-1;function u(){l&&h&&(l=!1,h.length?d=h.concat(d):p=-1,d.length&&g())}function g(){if(!l){var e=a(u);l=!0;for(var t=d.length;t;){for(h=d,d=[];++p<t;)h&&h[p].run();p=-1,t=d.length}h=null,l=!1,function(e){if(n===clearTimeout)return clearTimeout(e);if((n===c||!n)&&clearTimeout)return n=clearTimeout,clearTimeout(e);try{return n(e)}catch(t){try{return n.call(null,e)}catch(t){return n.call(this,e)}}}(e)}}function f(e,t){this.fun=e,this.array=t}function m(){}s.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];d.push(new f(e,t)),1!==d.length||l||a(g)},f.prototype.run=function(){this.fun.apply(null,this.array)},s.title="browser",s.browser=!0,s.env={},s.argv=[],s.version="",s.versions={},s.on=m,s.addListener=m,s.once=m,s.off=m,s.removeListener=m,s.removeAllListeners=m,s.emit=m,s.prependListener=m,s.prependOnceListener=m,s.listeners=function(e){return[]},s.binding=function(e){throw new Error("process.binding is not supported")},s.cwd=function(){return"/"},s.chdir=function(e){throw new Error("process.chdir is not supported")},s.umask=function(){return 0}},{}]},[],0,"parcelRequire55a5");