@liascript/exporter
Version:
A generic exporter for LiaScript
1 lines • 10.1 kB
JavaScript
!function(){function e(e,t,n,r){Object.defineProperty(e,t,{get:n,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("d5gtu",(function(n,r){e(n.exports,"Sync",(function(){return m}));var o=t("f1evb"),i=t("hnmIs"),c=t("9Klpk"),s=t("3bWnf"),u=t("eT7f3"),a=t("9WzNc"),l=t("div3d"),f=t("kb0qF"),h=t("bWNdT"),d=t("32Euv"),v=t("auvHb"),g=t("6iW4q"),y=t("733OA"),m=function(e){"use strict";(0,f.default)(n,e);var t=(0,h.default)(n);function n(){var e;return(0,c.default)(this,n),e=t.apply(this,arguments),(0,u.default)((0,o.default)(e),"syncFallbackTimer",null),e}return(0,s.default)(n,[{key:"destroy",value:function(){var e;null!==this.syncFallbackTimer&&(clearTimeout(this.syncFallbackTimer),this.syncFallbackTimer=null),(0,a.default)((0,l.default)(n.prototype),"destroy",this).call(this),null===(e=this.provider)||void 0===e||e.disconnect()}},{key:"connect",value:function(e){var t=this,r=this;return(0,i.default)((function(){var o;return(0,d.__generator)(this,(function(i){return(0,a.default)((0,l.default)(n.prototype),"connect",t).call(r,e),console.warn("WebSocket sync is experimental. Please report any issues you encounter.",e.config),r.serverUrl=null===(o=e.config)||void 0===o?void 0:o.url,r.serverUrl?(r.init(!0),[2]):[2,r.sendDisconnectError("You have to provide a WebSocket server URL.")]}))}))()}},{key:"init",value:function(e,t){var n=this,r=this.uniqueID(this.password);if(e&&r){this.transport=new(0,g.WebSocketTransport),this.provider=new(0,y.GenericProvider)(this.db.doc,this.transport,{verifyUpdates:!1}),this.db.setAwareness(this.provider.awareness);var o=!1,i=function(){o||(o=!0,null!==n.syncFallbackTimer&&(clearTimeout(n.syncFallbackTimer),n.syncFallbackTimer=null),n.sendConnect())};this.provider.on("synced",(function(e){console.log("WebSocket: document synchronized",e.synced),i()})),this.provider.on("status",(function(e){var t=e.state;console.log("WebSocket status: ".concat(t)),"connected"===t?n.syncFallbackTimer=setTimeout((function(){console.log("WebSocket: sync fallback, proceeding as first peer"),i()}),2e3):"disconnected"===t&&console.warn("WebSocket: disconnected")})),this.provider.pubsub.subscribe("*",(function(e,t){var r,o;null===(o=(r=n).onReceive)||void 0===o||o.call(r,t,e)})),this.provider.connect({serverUrl:this.serverUrl,room:r})}else{var c="WebSocket unknown error";t&&(c="Could not connect: "+t),this.sendDisconnectError(c)}}},{key:"pubsubSend",value:function(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)))}}]),n}(v.Sync)})),t.register("9WzNc",(function(n,r){e(n.exports,"default",(function(){return c}));var o=t("jwduU");function i(e,t,n){return i="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(e,t,n){var r=(0,o.default)(e,t);if(r){var i=Object.getOwnPropertyDescriptor(r,t);return i.get?i.get.call(n||e):i.value}},i(e,t,n)}function c(e,t,n){return i(e,t,n)}})),t.register("jwduU",(function(n,r){e(n.exports,"default",(function(){return i}));var o=t("div3d");function i(e,t){for(;!Object.prototype.hasOwnProperty.call(e,t)&&null!==(e=(0,o.default)(e)););return e}})),t.register("6iW4q",(function(n,r){e(n.exports,"WebSocketTransport",(function(){return f}));var o=t("hnmIs"),i=t("9Klpk"),c=t("3bWnf"),s=t("6Wo7Q"),u=t("32Euv"),a=function(){for(var e=new Uint32Array(256),t=0;t<256;t++){for(var n=t,r=0;r<8;r++)n=1&n?3988292384^n>>>1:n>>>1;e[t]=n}return e}();function l(e){var t=function(e){for(var t=4294967295,n=0;n<e.length;n++)t=t>>>8^a[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}var f=function(){"use strict";function e(){(0,i.default)(this,e),this.ws=null,this.config=null,this._isConnected=!1,this.debug=!1,this.reconnectAttempts=0,this.intentionalDisconnect=!1,this.messageQueue=[],this.receivedBuffer=[]}return(0,c.default)(e,[{key:"isConnected",get:function(){return this._isConnected}},{key:"connect",value:function(e){var t=this;return(0,o.default)((function(){var n,r;return(0,u.__generator)(this,(function(o){if(t.config=e,t.debug=null!==(n=e.debug)&&void 0!==n&&n,t.intentionalDisconnect=!1,!e.serverUrl)throw new Error("WebSocket serverUrl is required");if(!e.room)throw new Error("Room name is required");return(r=e.serverUrl).endsWith("/")&&(r=r.slice(0,-1)),r="".concat(r,"/").concat(encodeURIComponent(e.room)),t.log("Connecting to WebSocket server: ".concat(r)),[2,new Promise((function(n,o){try{t.ws=new WebSocket(r,e.protocols),t.ws.binaryType="arraybuffer";var i=setTimeout((function(){t.ws&&t.ws.readyState!==WebSocket.OPEN&&(t.ws.close(),o(new Error("WebSocket connection timeout")))}),1e4);t.ws.onopen=function(){clearTimeout(i),t._isConnected=!0,t.reconnectAttempts=0,t.log("✅ WebSocket connected to room: ".concat(e.room)),t.flushMessageQueue(),n()},t.ws.onmessage=function(e){t.handleMessage(e.data)},t.ws.onerror=function(e){clearTimeout(i),t.log("❌ WebSocket error:",e),t._isConnected||o(new Error("WebSocket connection failed"))},t.ws.onclose=function(n){var r;clearTimeout(i),t._isConnected=!1,t.log("WebSocket closed: code=".concat(n.code,", reason=").concat(n.reason||"none")),t.intentionalDisconnect||null!==(r=e.autoReconnect)&&void 0!==r&&!r||t.attemptReconnect()}}catch(e){o(e)}}))]}))}))()}},{key:"disconnect",value:function(){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}},{key:"send",value:function(e){if(!this.ws||this.ws.readyState!==WebSocket.OPEN)return this.log("⚠️ WebSocket not ready, queueing message"),void this.messageQueue.push(e);try{var t=function(e){return e.length>=4?e.subarray(4):e}(e);this.ws.send(t),this.log("📤 Sent ".concat(t.length," bytes (").concat(e.length," with header)"))}catch(e){this.log("❌ Send error:",e)}}},{key:"onMessage",value:function(e){var t=this;if(this.messageCallback=e,this.receivedBuffer.length>0){this.log("📦 Flushing ".concat(this.receivedBuffer.length," buffered received messages"));var n=!0,r=!1,o=void 0;try{for(var i,c=this.receivedBuffer[Symbol.iterator]();!(n=(i=c.next()).done);n=!0){e(i.value)}}catch(e){r=!0,o=e}finally{try{n||null==c.return||c.return()}finally{if(r)throw o}}this.receivedBuffer=[]}return function(){t.messageCallback=void 0}}},{key:"handleMessage",value:function(e){try{if("string"==typeof e)return void this.log("📨 Received text message:",e);var t=l(new Uint8Array(e));this.log("📨 Received ".concat(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)}}},{key:"flushMessageQueue",value:function(){if(0!==this.messageQueue.length){this.log("📦 Flushing ".concat(this.messageQueue.length," queued messages"));var e=!0,t=!1,n=void 0;try{for(var r,o=this.messageQueue[Symbol.iterator]();!(e=(r=o.next()).done);e=!0){var i=r.value;this.send(i)}}catch(e){t=!0,n=e}finally{try{e||null==o.return||o.return()}finally{if(t)throw n}}this.messageQueue=[]}}},{key:"attemptReconnect",value:function(){if(this.config){var e,t=null!==(e=this.config.maxReconnectAttempts)&&void 0!==e?e:0;if(t>0&&this.reconnectAttempts>=t)this.log("❌ Max reconnection attempts (".concat(t,") reached"));else{var n;this.reconnectAttempts++;var r=null!==(n=this.config.reconnectDelay)&&void 0!==n?n:2e3;this.log("🔄 Attempting reconnection #".concat(this.reconnectAttempts," in ").concat(r,"ms..."));var i=this;this.reconnectTimer=setTimeout((0,o.default)((function(){var e;return(0,u.__generator)(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,i.connect(i.config)];case 1:return t.sent(),i.log("✅ Reconnected successfully"),[3,3];case 2:return e=t.sent(),i.log("❌ Reconnection failed:",e),[3,3];case 3:return[2]}}))})),r)}}}},{key:"log",value:function(e){for(var t=arguments.length,n=new Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];var o;this.debug&&(o=console).log.apply(o,["[WebSocketTransport] ".concat(e)].concat((0,s.default)(n)))}}]),e}()})),t.register("84cK9",(function(e,t){var n,r,o=e.exports={};function i(){throw new Error("setTimeout has not been defined")}function c(){throw new Error("clearTimeout has not been defined")}function s(e){if(n===setTimeout)return setTimeout(e,0);if((n===i||!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:i}catch(e){n=i}try{r="function"==typeof clearTimeout?clearTimeout:c}catch(e){r=c}}();var u,a=[],l=!1,f=-1;function h(){l&&u&&(l=!1,u.length?a=u.concat(a):f=-1,a.length&&d())}function d(){if(!l){var e=s(h);l=!0;for(var t=a.length;t;){for(u=a,a=[];++f<t;)u&&u[f].run();f=-1,t=a.length}u=null,l=!1,function(e){if(r===clearTimeout)return clearTimeout(e);if((r===c||!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 v(e,t){this.fun=e,this.array=t}function g(){}o.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 v(e,t)),1!==a.length||l||s(d)},v.prototype.run=function(){this.fun.apply(null,this.array)},o.title="browser",o.browser=!0,o.env={},o.argv=[],o.version="",o.versions={},o.on=g,o.addListener=g,o.once=g,o.off=g,o.removeListener=g,o.removeAllListeners=g,o.emit=g,o.prependListener=g,o.prependOnceListener=g,o.listeners=function(e){return[]},o.binding=function(e){throw new Error("process.binding is not supported")},o.cwd=function(){return"/"},o.chdir=function(e){throw new Error("process.chdir is not supported")},o.umask=function(){return 0}}))}();