UNPKG

@liascript/exporter

Version:
1 lines 11.4 kB
!function(){function e(e,n,t,r){Object.defineProperty(e,n,{get:t,set:r,enumerable:!0,configurable:!0})}var n=("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:{}).parcelRequire55a5;n.register("aYnRb",(function(t,r){e(t.exports,"Sync",(function(){return v}));var i=n("f1evb"),o=n("hnmIs"),s=n("9Klpk"),u=n("3bWnf"),c=n("eT7f3"),a=n("9WzNc"),l=n("div3d"),h=n("kb0qF"),f=n("bgoEC"),d=n("bWNdT"),b=n("32Euv"),g=n("auvHb"),p=n("cERT2"),y=n("733OA"),v=function(e){"use strict";(0,h.default)(t,e);var n=(0,d.default)(t);function t(){var e;return(0,s.default)(this,t),e=n.apply(this,arguments),(0,c.default)((0,i.default)(e),"syncFallbackTimer",null),e}return(0,u.default)(t,[{key:"destroy",value:function(){var e;null!==this.syncFallbackTimer&&(clearTimeout(this.syncFallbackTimer),this.syncFallbackTimer=null),(0,a.default)((0,l.default)(t.prototype),"destroy",this).call(this),null===(e=this.provider)||void 0===e||e.disconnect()}},{key:"connect",value:function(e){var n=this,r=this;return(0,o.default)((function(){var i,o;return(0,b.__generator)(this,(function(s){return(0,a.default)((0,l.default)(t.prototype),"connect",n).call(r,e),r.publishKey=null===(i=e.config)||void 0===i?void 0:i.publishKey,r.subscribeKey=null===(o=e.config)||void 0===o?void 0:o.subscribeKey,window.PubNub?r.init(!0):r.load(["//cdn.pubnub.com/sdk/javascript/pubnub.10.2.7.min.js"],r),[2]}))}))()}},{key:"init",value:function(e,n){var t=this;if(!this.publishKey||!this.subscribeKey)return this.sendDisconnectError("You have to provide a valid pair of keys");var r=this.uniqueID();if(e&&window.PubNub&&r){this.transport=new(0,p.PubNubTransport),this.provider=new(0,y.GenericProvider)(this.db.doc,this.transport),this.db.setAwareness(this.provider.awareness);var i=!1,o=function(){i||(i=!0,null!==t.syncFallbackTimer&&(clearTimeout(t.syncFallbackTimer),t.syncFallbackTimer=null),t.sendConnect())};this.provider.on("synced",(function(e){console.log("PubNub: document synchronized",e.synced),o()})),this.provider.on("status",(function(e){var n=e.state;console.log("PubNub status: ".concat(n)),"connected"===n?t.syncFallbackTimer=setTimeout((function(){console.log("PubNub: sync fallback, proceeding as first peer"),o()}),2e3):"disconnected"===n&&console.warn("PubNub: disconnected")})),this.provider.connect((0,f.default)({room:r,publishKey:this.publishKey,subscribeKey:this.subscribeKey},this.password?{cipherKey:this.password}:{}))}else{var s="PubNub unknown error";n?s="Could not load resource: "+n:window.PubNub||(s="Could not load PubNub SDK"),this.sendDisconnectError(s)}}}]),t}(g.Sync)})),n.register("9WzNc",(function(t,r){e(t.exports,"default",(function(){return s}));var i=n("jwduU");function o(e,n,t){return o="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(e,n,t){var r=(0,i.default)(e,n);if(r){var o=Object.getOwnPropertyDescriptor(r,n);return o.get?o.get.call(t||e):o.value}},o(e,n,t)}function s(e,n,t){return o(e,n,t)}})),n.register("jwduU",(function(t,r){e(t.exports,"default",(function(){return o}));var i=n("div3d");function o(e,n){for(;!Object.prototype.hasOwnProperty.call(e,n)&&null!==(e=(0,i.default)(e)););return e}})),n.register("cERT2",(function(t,r){e(t.exports,"PubNubTransport",(function(){return a}));var i=n("hnmIs"),o=n("9Klpk"),s=n("3bWnf"),u=n("6Wo7Q"),c=n("32Euv"),a=function(){"use strict";function e(){(0,o.default)(this,e),this.pubnub=null,this.channel="",this.uuid="",this._isConnected=!1,this.config=null,this.debug=!1,this.messageBuffer=[],this.chunkBuffer=new Map,this.MAX_MESSAGE_SIZE=3e4}return(0,s.default)(e,[{key:"isConnected",get:function(){return this._isConnected}},{key:"connect",value:function(e){var n=this;return(0,i.default)((function(){var t,r;return(0,c.__generator)(this,(function(i){if(n.config=e,n.debug=null!==(t=e.debug)&&void 0!==t&&t,!e.publishKey||!e.subscribeKey)throw new Error("PubNub requires publishKey and subscribeKey");if(!e.room)throw new Error("Room name is required");if(n.log("Initializing PubNub transport..."),void 0===globalThis.PubNub)throw new Error("PubNub SDK not loaded. Include the PubNub script from CDN: https://cdn.pubnub.com/sdk/javascript/pubnub.10.2.7.min.js");return n.uuid=n.generateUUID(),n.channel=btoa(e.room),n.log("Connecting as ".concat(n.uuid," to channel ").concat(n.channel)),r={publishKey:e.publishKey,subscribeKey:e.subscribeKey,userId:n.uuid},e.cipherKey&&(r.cipherKey=e.cipherKey,n.log("Message encryption enabled")),n.pubnub=new PubNub(r),[2,new Promise((function(e,t){var r=setTimeout((function(){t(new Error("PubNub connection timeout"))}),1e4);n.pubnub.addListener({status:function(t){n.log("Status: ".concat(t.category),t),"PNConnectedCategory"===t.category?(n._isConnected=!0,clearTimeout(r),n.log("✅ Connected to PubNub"),e()):"PNNetworkDownCategory"===t.category?(n.log("⚠️ Network is down"),n._isConnected=!1):"PNNetworkUpCategory"===t.category&&(n.log("✅ Network is back up"),n._isConnected=!0)},message:function(e){if(e.publisher!==n.uuid&&e.message)try{if("object"==typeof e.message&&e.message.chunked)return void n.handleChunkedMessage(e.message,e.publisher);var t="string"==typeof e.message?n.base64ToUint8(e.message):new Uint8Array(e.message);n.log("📨 Received ".concat(t.length," bytes from ").concat(e.publisher)),n.messageCallback?n.messageCallback(t):(n.log("⚠️ Message received before callback registered, buffering..."),n.messageBuffer.push(t))}catch(e){n.log("❌ Error processing message:",e)}},presence:function(e){n.log("Presence: ".concat(e.action),e)}}),n.pubnub.subscribe({channels:[n.channel],withPresence:!0})}))]}))}))()}},{key:"disconnect",value:function(){this.pubnub&&(this.log("Disconnecting from PubNub..."),this.pubnub.unsubscribeAll(),this.pubnub=null),this._isConnected=!1}},{key:"send",value:function(e){var n=this;if(this.pubnub&&this._isConnected)try{var t,r,i=this.uint8ToBase64(e),o=i.length;if(o>this.MAX_MESSAGE_SIZE)return this.log("📦 Message too large (".concat(o," bytes), chunking into smaller pieces...")),void this.sendChunked(i,e.length);this.log("📤 Sending ".concat(e.length," bytes")),this.pubnub.publish({channel:this.channel,message:i,storeInHistory:null!==(r=null===(t=this.config)||void 0===t?void 0:t.storeInHistory)&&void 0!==r&&r}).then((function(e){n.log("✅ Message published",e)})).catch((function(e){n.log("❌ Publish error:",e)}))}catch(e){this.log("❌ Send error:",e)}else this.log("⚠️ Cannot send: not connected")}},{key:"sendChunked",value:function(e,n){for(var t=this,r=this.generateUUID(),i=[],o=0;o<e.length;o+=this.MAX_MESSAGE_SIZE)i.push(e.slice(o,o+this.MAX_MESSAGE_SIZE));this.log("📦 Splitting ".concat(n," bytes into ").concat(i.length," chunks (id: ").concat(r.slice(0,8),"...)")),i.forEach((function(e,n){var o={chunked:!0,id:r,index:n,total:i.length,data:e};t.pubnub.publish({channel:t.channel,message:o,storeInHistory:!1}).then((function(){t.log("✅ Chunk ".concat(n+1,"/").concat(i.length," sent"))})).catch((function(e){t.log("❌ Failed to send chunk ".concat(n+1,":"),e)}))}))}},{key:"handleChunkedMessage",value:function(e,n){var t=e.id,r=e.index,i=e.total,o=e.data;this.log("📨 Received chunk ".concat(r+1,"/").concat(i," from ").concat(n)),this.chunkBuffer.has(t)||this.chunkBuffer.set(t,new Map);var s=this.chunkBuffer.get(t);if(s.set(r,o),s.size===i){this.log("📦 All chunks received, reassembling message ".concat(t.slice(0,8),"..."));for(var u="",c=0;c<i;c++){var a=s.get(c);if(!a)return this.log("❌ Missing chunk ".concat(c,", cannot reassemble")),void this.chunkBuffer.delete(t);u+=a}this.chunkBuffer.delete(t);try{var l=this.base64ToUint8(u);this.log("✅ Message reassembled: ".concat(l.length," bytes from ").concat(n)),this.messageCallback?this.messageCallback(l):this.messageBuffer.push(l)}catch(e){this.log("❌ Error reassembling chunked message:",e)}}}},{key:"onMessage",value:function(e){var n=this;if(this.messageCallback=e,this.messageBuffer.length>0){this.log("📦 Flushing ".concat(this.messageBuffer.length," buffered messages..."));var t=!0,r=!1,i=void 0;try{for(var o,s=this.messageBuffer[Symbol.iterator]();!(t=(o=s.next()).done);t=!0){e(o.value)}}catch(e){r=!0,i=e}finally{try{t||null==s.return||s.return()}finally{if(r)throw i}}this.messageBuffer=[]}return function(){n.messageCallback=void 0}}},{key:"getPresence",value:function(){var e=this;return(0,i.default)((function(){var n,t,r;return(0,c.__generator)(this,(function(i){switch(i.label){case 0:if(!e.pubnub||!e._isConnected)return[2,[]];i.label=1;case 1:return i.trys.push([1,3,,4]),[4,e.pubnub.hereNow({channels:[e.channel],includeUUIDs:!0})];case 2:return t=i.sent(),[2,((null===(n=t.channels[e.channel])||void 0===n?void 0:n.occupants)||[]).map((function(e){return e.uuid})).filter((function(n){return n!==e.uuid}))];case 3:return r=i.sent(),e.log("Error getting presence:",r),[2,[]];case 4:return[2]}}))}))()}},{key:"uint8ToBase64",value:function(e){for(var n="",t=0;t<e.length;t+=8192){var r,i=e.subarray(t,Math.min(t+8192,e.length));n+=(r=String).fromCharCode.apply(r,(0,u.default)(i))}return btoa(n)}},{key:"base64ToUint8",value:function(e){return new Uint8Array(atob(e).split("").map((function(e){return e.charCodeAt(0)})))}},{key:"generateUUID",value:function(){return"undefined"!=typeof crypto&&crypto.randomUUID?crypto.randomUUID():"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){var n=16*Math.random()|0;return("x"===e?n:3&n|8).toString(16)}))}},{key:"log",value:function(e){for(var n=arguments.length,t=new Array(n>1?n-1:0),r=1;r<n;r++)t[r-1]=arguments[r];var i;this.debug&&(i=console).log.apply(i,["[PubNubTransport] ".concat(e)].concat((0,u.default)(t)))}}]),e}()})),n.register("84cK9",(function(e,n){var t,r,i=e.exports={};function o(){throw new Error("setTimeout has not been defined")}function s(){throw new Error("clearTimeout has not been defined")}function u(e){if(t===setTimeout)return setTimeout(e,0);if((t===o||!t)&&setTimeout)return t=setTimeout,setTimeout(e,0);try{return t(e,0)}catch(n){try{return t.call(null,e,0)}catch(n){return t.call(this,e,0)}}}!function(){try{t="function"==typeof setTimeout?setTimeout:o}catch(e){t=o}try{r="function"==typeof clearTimeout?clearTimeout:s}catch(e){r=s}}();var c,a=[],l=!1,h=-1;function f(){l&&c&&(l=!1,c.length?a=c.concat(a):h=-1,a.length&&d())}function d(){if(!l){var e=u(f);l=!0;for(var n=a.length;n;){for(c=a,a=[];++h<n;)c&&c[h].run();h=-1,n=a.length}c=null,l=!1,function(e){if(r===clearTimeout)return clearTimeout(e);if((r===s||!r)&&clearTimeout)return r=clearTimeout,clearTimeout(e);try{return r(e)}catch(n){try{return r.call(null,e)}catch(n){return r.call(this,e)}}}(e)}}function b(e,n){this.fun=e,this.array=n}function g(){}i.nextTick=function(e){var n=new Array(arguments.length-1);if(arguments.length>1)for(var t=1;t<arguments.length;t++)n[t-1]=arguments[t];a.push(new b(e,n)),1!==a.length||l||u(d)},b.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=g,i.addListener=g,i.once=g,i.off=g,i.removeListener=g,i.removeAllListeners=g,i.emit=g,i.prependListener=g,i.prependOnceListener=g,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}}))}();