UNPKG

@liascript/exporter

Version:
1 lines 7.43 kB
function e(e,t,n,o){Object.defineProperty(e,t,{get:n,set:o,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("aYlFN",(function(n,o){e(n.exports,"Sync",(function(){return h}));var s=t("2Xs1f"),r=t("dM3DM"),i=t("5Owdb"),c=t("9kSnr"),l=t("akyql");const a={nostr:null,mqtt:null,torrent:null,ipfs:null};class h extends i.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){if(super.connect(e),a[this.backend])return void this.init(!0);const n=e=>{e.then((e=>{a[this.backend]=e.joinRoom,this.init(!0)})).catch((e=>this.init(!1,e.message)))};switch(this.backend){case"nostr":n(t("2Alpu"));break;case"mqtt":n(t("8VwUG"));break;case"torrent":n(t("bSCfm"));break;case"ipfs":n(t("7C2J3"))}}init(e,t){const n=this.uniqueID();if(e&&n){const e=JSON.parse("null");this.transport=new(0,c.TrysteroTransport)((0,r.default)({joinRoom:a[this.backend],appId:"liascript",password:this.password},e?{rtcConfig:e}:{})),this.provider=new(0,l.GenericProvider)(this.db.doc,this.transport),this.db.setAwareness(this.provider.awareness);let t=!1;const o=()=>{t||(t=!0,null!==this.syncFallbackTimer&&(clearTimeout(this.syncFallbackTimer),this.syncFallbackTimer=null),this.sendConnect())};this.provider.on("synced",(e=>{console.log("Trystero: document synchronized",e.synced),o()})),this.provider.on("status",(e=>{const t=e.state;console.log(`Trystero status: ${t}`),"connected"===t?this.syncFallbackTimer=setTimeout((()=>{console.log("Trystero: sync fallback, proceeding as first peer"),o()}),2e3):"disconnected"===t&&console.warn("Trystero: disconnected")})),this.provider.pubsub.subscribe("*",((e,t)=>{var n;null===(n=this.onReceive)||void 0===n||n.call(this,t,e)})),this.provider.connect({room:n})}else{let e=this.backend+" unknown error";t&&(e="Could not load resource: "+t),this.sendDisconnectError(e)}}pubsubSend(e,t){var n;this.provider&&(this.provider.pubsub.publish(e,t),this.replyOnReceive&&(null===(n=this.onReceive)||void 0===n||n.call(this,e,t)))}constructor(e,t,n,o,r,i=!1,c=!0){super(t,n,o,r,i,c),(0,s.default)(this,"syncFallbackTimer",null),this.backend=e}}})),t.register("9kSnr",(function(n,o){e(n.exports,"TrysteroTransport",(function(){return r}));var s=t("dM3DM");class r{log(e,t="info"){if(this.options.debug){const n="[TrysteroTransport]";switch(t){case"error":console.error(n,e);break;case"warn":console.warn(n,e);break;default:console.log(n,e)}}}get isConnected(){return this._connected}async connect(e){this._connected&&(this.log("Already connected, disconnecting first..."),this.disconnect());const t=e.room;if(!t)throw new Error("Room ID is required");this._room=t,this.log(`Connecting to room: ${t}`);const n={appId:this.options.appId};this.options.password&&(n.password=this.options.password),this.options.relayUrls&&(n.relayUrls=this.options.relayUrls),this.options.relayRedundancy&&(n.relayRedundancy=this.options.relayRedundancy),this.options.rtcConfig&&(n.rtcConfig=this.options.rtcConfig),this.options.turnConfig&&(n.turnConfig=this.options.turnConfig),this.options.rtcPolyfill&&(n.rtcPolyfill=this.options.rtcPolyfill),this.options.supabaseKey&&(n.supabaseKey=this.options.supabaseKey),this.options.firebaseApp&&(n.firebaseApp=this.options.firebaseApp),this.options.rootPath&&(n.rootPath=this.options.rootPath),void 0!==this.options.manualRelayReconnection&&(n.manualRelayReconnection=this.options.manualRelayReconnection),this.room=this.options.joinRoom(n,t,(e=>{this.log(`Join error: ${e.error}`,"error"),this.onJoinErrorCallback&&this.onJoinErrorCallback(e)}));const[o,s]=this.room.makeAction("yjs-update");this.sendUpdate=o,s(((e,t)=>{this.log(`Received update from ${t} (${e.byteLength} bytes)`),this._callback&&this._callback(new Uint8Array(e))})),this.room.onPeerJoin((e=>{this.peers.add(e),this.log(`Peer joined: ${e} (${this.peers.size} total)`)})),this.room.onPeerLeave((e=>{this.peers.delete(e),this.log(`Peer left: ${e} (${this.peers.size} remaining)`)})),this._connected=!0,this.log(`✅ Connected to room: ${t}`)}disconnect(){this._connected&&(this.log("Disconnecting..."),this.room&&(this.room.leave(),this.room=null),this.sendUpdate=null,this._callback=void 0,this._connected=!1,this.peers.clear(),this.log("✅ Disconnected"))}async send(e){this._connected&&this.sendUpdate?(this.log(`Sending update (${e.byteLength} bytes) to ${this.peers.size} peers`),await this.sendUpdate(e,null)):this.log("⚠️ Not connected, cannot send","warn")}onMessage(e){return this._callback=e,this.log("Message callback registered"),()=>{this._callback=void 0,this.log("Message callback unregistered")}}onJoinError(e){this.onJoinErrorCallback=e}getPeers(){return new Set(this.peers)}async ping(e){if(!this.room)throw new Error("Not connected to a room");return await this.room.ping(e)}constructor(e){this._connected=!1,this._room="",this.room=null,this.sendUpdate=null,this.peers=new Set,this.options=(0,s.default)({debug:!1},e)}}})),t.register("2Alpu",(function(e,n){e.exports=t("ebDiK")(t("ey3S0").getBundleURL("17irQ")+t("abmWv").resolve("lYuyw")).then((()=>t("g9MJ9")))})),t.register("8VwUG",(function(e,n){e.exports=t("ebDiK")(t("ey3S0").getBundleURL("17irQ")+t("abmWv").resolve("7bLGf")).then((()=>t("i0BT1")))})),t.register("bSCfm",(function(e,n){e.exports=t("ebDiK")(t("ey3S0").getBundleURL("17irQ")+t("abmWv").resolve("1lfZ9")).then((()=>t("hsMrW")))})),t.register("7C2J3",(function(e,n){e.exports=t("ebDiK")(t("ey3S0").getBundleURL("17irQ")+t("abmWv").resolve("cDOyx")).then((()=>t("62kvE")))})),t.register("jhEmn",(function(e,t){var n,o,s=e.exports={};function r(){throw new Error("setTimeout has not been defined")}function i(){throw new Error("clearTimeout has not been defined")}function c(e){if(n===setTimeout)return setTimeout(e,0);if((n===r||!n)&&setTimeout)return n=setTimeout,setTimeout(e,0);try{return n(e,0)}catch(t){try{return n.call(null,e,0)}catch(t){return n.call(this,e,0)}}}!function(){try{n="function"==typeof setTimeout?setTimeout:r}catch(e){n=r}try{o="function"==typeof clearTimeout?clearTimeout:i}catch(e){o=i}}();var l,a=[],h=!1,u=-1;function d(){h&&l&&(h=!1,l.length?a=l.concat(a):u=-1,a.length&&p())}function p(){if(!h){var e=c(d);h=!0;for(var t=a.length;t;){for(l=a,a=[];++u<t;)l&&l[u].run();u=-1,t=a.length}l=null,h=!1,function(e){if(o===clearTimeout)return clearTimeout(e);if((o===i||!o)&&clearTimeout)return o=clearTimeout,clearTimeout(e);try{return o(e)}catch(t){try{return o.call(null,e)}catch(t){return o.call(this,e)}}}(e)}}function f(e,t){this.fun=e,this.array=t}function b(){}s.nextTick=function(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var n=1;n<arguments.length;n++)t[n-1]=arguments[n];a.push(new f(e,t)),1!==a.length||h||c(p)},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=b,s.addListener=b,s.once=b,s.off=b,s.removeListener=b,s.removeAllListeners=b,s.emit=b,s.prependListener=b,s.prependOnceListener=b,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}}));