UNPKG

@liascript/exporter

Version:
1 lines 7.83 kB
function e(e,t,n,s){Object.defineProperty(e,t,{get:n,set:s,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("ezjQL",(function(n,s){e(n.exports,"Sync",(function(){return l}));var i=t("2Xs1f"),r=t("5Owdb"),o=t("kHtds"),c=t("akyql");class l extends r.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;if(super.connect(e),console.warn("WebSocket sync is experimental. Please report any issues you encounter.",e.config),this.serverUrl=null===(t=e.config)||void 0===t?void 0:t.url,!this.serverUrl)return this.sendDisconnectError("You have to provide a WebSocket server URL.");this.init(!0)}init(e,t){const n=this.uniqueID(this.password);if(e&&n){this.transport=new(0,o.WebSocketTransport),this.provider=new(0,c.GenericProvider)(this.db.doc,this.transport,{verifyUpdates:!1}),this.db.setAwareness(this.provider.awareness);let e=!1;const t=()=>{e||(e=!0,null!==this.syncFallbackTimer&&(clearTimeout(this.syncFallbackTimer),this.syncFallbackTimer=null),this.sendConnect())};this.provider.on("synced",(e=>{console.log("WebSocket: document synchronized",e.synced),t()})),this.provider.on("status",(e=>{const n=e.state;console.log(`WebSocket status: ${n}`),"connected"===n?this.syncFallbackTimer=setTimeout((()=>{console.log("WebSocket: sync fallback, proceeding as first peer"),t()}),2e3):"disconnected"===n&&console.warn("WebSocket: disconnected")})),this.provider.pubsub.subscribe("*",((e,t)=>{var n;null===(n=this.onReceive)||void 0===n||n.call(this,t,e)})),this.provider.connect({serverUrl:this.serverUrl,room:n})}else{let e="WebSocket unknown error";t&&(e="Could not connect: "+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){super(...e),(0,i.default)(this,"syncFallbackTimer",null)}}})),t.register("kHtds",(function(t,n){e(t.exports,"WebSocketTransport",(function(){return r}));const s=(()=>{const e=new Uint32Array(256);for(let t=0;t<256;t++){let n=t;for(let e=0;e<8;e++)n=1&n?3988292384^n>>>1:n>>>1;e[t]=n}return e})();function i(e){const t=function(e){let t=4294967295;for(let n=0;n<e.length;n++)t=t>>>8^s[255&(t^e[n])];return(4294967295^t)>>>0}(e),n=new Uint8Array(4+e.length);return n[0]=t>>>24&255,n[1]=t>>>16&255,n[2]=t>>>8&255,n[3]=255&t,n.set(e,4),n}class r{get isConnected(){return this._isConnected}async connect(e){var t;if(this.config=e,this.debug=null!==(t=e.debug)&&void 0!==t&&t,this.intentionalDisconnect=!1,!e.serverUrl)throw new Error("WebSocket serverUrl is required");if(!e.room)throw new Error("Room name is required");let n=e.serverUrl;return n.endsWith("/")&&(n=n.slice(0,-1)),n=`${n}/${encodeURIComponent(e.room)}`,this.log(`Connecting to WebSocket server: ${n}`),new Promise(((t,s)=>{try{this.ws=new WebSocket(n,e.protocols),this.ws.binaryType="arraybuffer";const i=setTimeout((()=>{this.ws&&this.ws.readyState!==WebSocket.OPEN&&(this.ws.close(),s(new Error("WebSocket connection timeout")))}),1e4);this.ws.onopen=()=>{clearTimeout(i),this._isConnected=!0,this.reconnectAttempts=0,this.log(`✅ WebSocket connected to room: ${e.room}`),this.flushMessageQueue(),t()},this.ws.onmessage=e=>{this.handleMessage(e.data)},this.ws.onerror=e=>{clearTimeout(i),this.log("❌ WebSocket error:",e),this._isConnected||s(new Error("WebSocket connection failed"))},this.ws.onclose=t=>{var n;clearTimeout(i),this._isConnected=!1,this.log(`WebSocket closed: code=${t.code}, reason=${t.reason||"none"}`),this.intentionalDisconnect||null!==(n=e.autoReconnect)&&void 0!==n&&!n||this.attemptReconnect()}}catch(e){s(e)}}))}disconnect(){this.intentionalDisconnect=!0,this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=void 0),this.ws&&(this.log("Disconnecting from WebSocket..."),this.ws.close(1e3,"Client disconnect"),this.ws=null),this._isConnected=!1}send(e){if(!this.ws||this.ws.readyState!==WebSocket.OPEN)return this.log("⚠️ WebSocket not ready, queueing message"),void this.messageQueue.push(e);try{const t=function(e){return e.length>=4?e.subarray(4):e}(e);this.ws.send(t),this.log(`📤 Sent ${t.length} bytes (${e.length} with header)`)}catch(e){this.log("❌ Send error:",e)}}onMessage(e){if(this.messageCallback=e,this.receivedBuffer.length>0){this.log(`📦 Flushing ${this.receivedBuffer.length} buffered received messages`);for(const t of this.receivedBuffer)e(t);this.receivedBuffer=[]}return()=>{this.messageCallback=void 0}}handleMessage(e){try{if("string"==typeof e)return void this.log("📨 Received text message:",e);const t=i(new Uint8Array(e));this.log(`📨 Received ${t.length-4} bytes`),this.messageCallback?this.messageCallback(t):(this.log("⏳ Buffering message (callback not yet registered)"),this.receivedBuffer.push(t))}catch(e){this.log("❌ Error handling message:",e)}}flushMessageQueue(){if(0!==this.messageQueue.length){this.log(`📦 Flushing ${this.messageQueue.length} queued messages`);for(const e of this.messageQueue)this.send(e);this.messageQueue=[]}}attemptReconnect(){if(!this.config)return;var e;const t=null!==(e=this.config.maxReconnectAttempts)&&void 0!==e?e:0;if(t>0&&this.reconnectAttempts>=t)return void this.log(`❌ Max reconnection attempts (${t}) reached`);var n;this.reconnectAttempts++;const s=null!==(n=this.config.reconnectDelay)&&void 0!==n?n:2e3;this.log(`🔄 Attempting reconnection #${this.reconnectAttempts} in ${s}ms...`),this.reconnectTimer=setTimeout((async()=>{try{await this.connect(this.config),this.log("✅ Reconnected successfully")}catch(e){this.log("❌ Reconnection failed:",e)}}),s)}log(e,...t){this.debug&&console.log(`[WebSocketTransport] ${e}`,...t)}constructor(){this.ws=null,this.config=null,this._isConnected=!1,this.debug=!1,this.reconnectAttempts=0,this.intentionalDisconnect=!1,this.messageQueue=[],this.receivedBuffer=[]}}})),t.register("jhEmn",(function(e,t){var n,s,i=e.exports={};function r(){throw new Error("setTimeout has not been defined")}function o(){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{s="function"==typeof clearTimeout?clearTimeout:o}catch(e){s=o}}();var l,u=[],a=!1,h=-1;function d(){a&&l&&(a=!1,l.length?u=l.concat(u):h=-1,u.length&&f())}function f(){if(!a){var e=c(d);a=!0;for(var t=u.length;t;){for(l=u,u=[];++h<t;)l&&l[h].run();h=-1,t=u.length}l=null,a=!1,function(e){if(s===clearTimeout)return clearTimeout(e);if((s===o||!s)&&clearTimeout)return s=clearTimeout,clearTimeout(e);try{return s(e)}catch(t){try{return s.call(null,e)}catch(t){return s.call(this,e)}}}(e)}}function g(e,t){this.fun=e,this.array=t}function m(){}i.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];u.push(new g(e,t)),1!==u.length||a||c(f)},g.prototype.run=function(){this.fun.apply(null,this.array)},i.title="browser",i.browser=!0,i.env={},i.argv=[],i.version="",i.versions={},i.on=m,i.addListener=m,i.once=m,i.off=m,i.removeListener=m,i.removeAllListeners=m,i.emit=m,i.prependListener=m,i.prependOnceListener=m,i.listeners=function(e){return[]},i.binding=function(e){throw new Error("process.binding is not supported")},i.cwd=function(){return"/"},i.chdir=function(e){throw new Error("process.chdir is not supported")},i.umask=function(){return 0}}));