@liascript/exporter
Version:
A generic exporter for LiaScript
1 lines • 9.99 kB
JavaScript
!function(e,t,s,n,i){var r="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:{},o="function"==typeof r[n]&&r[n],u=o.cache||{},c="undefined"!=typeof module&&"function"==typeof module.require&&module.require.bind(module);function h(t,s){if(!u[t]){if(!e[t]){var i="function"==typeof r[n]&&r[n];if(!s&&i)return i(t,!0);if(o)return o(t,!0);if(c&&"string"==typeof t)return c(t);var l=new Error("Cannot find module '"+t+"'");throw l.code="MODULE_NOT_FOUND",l}d.resolve=function(s){var n=e[t][1][s];return null!=n?n:s},d.cache={};var a=u[t]=new h.Module(t);e[t][0].call(a.exports,d,a,a.exports,this)}return u[t].exports;function d(e){var t=d.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=u,h.parent=o,h.register=function(t,s){e[t]=[function(e,t){t.exports=s},{}]},Object.defineProperty(h,"root",{get:function(){return r[n]}}),r[n]=h;for(var l=0;l<t.length;l++)h(t[l])}({"6B3Kr":[function(e,t,s){var n=e("@parcel/transformer-js/src/esmodule-helpers.js");n.defineInteropFlag(s),n.export(s,"Sync",(()=>u));var i=e("../Base/index"),r=e("../../../../node_modules/y-generic/dist/providers/pubnub/index"),o=e("y-generic");class u extends i.Sync{syncFallbackTimer=null;destroy(){null!==this.syncFallbackTimer&&(clearTimeout(this.syncFallbackTimer),this.syncFallbackTimer=null),super.destroy(),this.provider?.disconnect()}async connect(e){super.connect(e),this.publishKey=e.config?.publishKey,this.subscribeKey=e.config?.subscribeKey,window.PubNub?this.init(!0):this.load(["//cdn.pubnub.com/sdk/javascript/pubnub.10.2.7.min.js"],this)}init(e,t){if(!this.publishKey||!this.subscribeKey)return this.sendDisconnectError("You have to provide a valid pair of keys");const s=this.uniqueID();if(e&&window.PubNub&&s){this.transport=new(0,r.PubNubTransport),this.provider=new(0,o.GenericProvider)(this.db.doc,this.transport),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("PubNub: document synchronized",e.synced),t()})),this.provider.on("status",(e=>{const s=e.state;console.log(`PubNub status: ${s}`),"connected"===s?this.syncFallbackTimer=setTimeout((()=>{console.log("PubNub: sync fallback, proceeding as first peer"),t()}),2e3):"disconnected"===s&&console.warn("PubNub: disconnected")})),this.provider.connect({room:s,publishKey:this.publishKey,subscribeKey:this.subscribeKey,...this.password?{cipherKey:this.password}:{}})}else{let e="PubNub unknown error";t?e="Could not load resource: "+t:window.PubNub||(e="Could not load PubNub SDK"),this.sendDisconnectError(e)}}}},{"../Base/index":"2cTju","../../../../node_modules/y-generic/dist/providers/pubnub/index":"jrlOF","y-generic":"3M9RT","@parcel/transformer-js/src/esmodule-helpers.js":"XTfsy"}],jrlOF:[function(e,t,s){var n=e("@parcel/transformer-js/src/esmodule-helpers.js");n.defineInteropFlag(s),n.export(s,"PubNubTransport",(()=>i));class i{constructor(){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}get isConnected(){return this._isConnected}async connect(e){if(this.config=e,this.debug=e.debug??!1,!e.publishKey||!e.subscribeKey)throw new Error("PubNub requires publishKey and subscribeKey");if(!e.room)throw new Error("Room name is required");if(this.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");this.uuid=this.generateUUID(),this.channel=btoa(e.room),this.log(`Connecting as ${this.uuid} to channel ${this.channel}`);const t={publishKey:e.publishKey,subscribeKey:e.subscribeKey,userId:this.uuid};return e.cipherKey&&(t.cipherKey=e.cipherKey,this.log("Message encryption enabled")),this.pubnub=new PubNub(t),new Promise(((e,t)=>{const s=setTimeout((()=>{t(new Error("PubNub connection timeout"))}),1e4);this.pubnub.addListener({status:t=>{this.log(`Status: ${t.category}`,t),"PNConnectedCategory"===t.category?(this._isConnected=!0,clearTimeout(s),this.log("✅ Connected to PubNub"),e()):"PNNetworkDownCategory"===t.category?(this.log("⚠️ Network is down"),this._isConnected=!1):"PNNetworkUpCategory"===t.category&&(this.log("✅ Network is back up"),this._isConnected=!0)},message:e=>{if(e.publisher!==this.uuid&&e.message)try{if("object"==typeof e.message&&e.message.chunked)return void this.handleChunkedMessage(e.message,e.publisher);const t="string"==typeof e.message?this.base64ToUint8(e.message):new Uint8Array(e.message);this.log(`📨 Received ${t.length} bytes from ${e.publisher}`),this.messageCallback?this.messageCallback(t):(this.log("⚠️ Message received before callback registered, buffering..."),this.messageBuffer.push(t))}catch(e){this.log("❌ Error processing message:",e)}},presence:e=>{this.log(`Presence: ${e.action}`,e)}}),this.pubnub.subscribe({channels:[this.channel],withPresence:!0})}))}disconnect(){this.pubnub&&(this.log("Disconnecting from PubNub..."),this.pubnub.unsubscribeAll(),this.pubnub=null),this._isConnected=!1}send(e){if(this.pubnub&&this._isConnected)try{const t=this.uint8ToBase64(e),s=t.length;if(s>this.MAX_MESSAGE_SIZE)return this.log(`📦 Message too large (${s} bytes), chunking into smaller pieces...`),void this.sendChunked(t,e.length);this.log(`📤 Sending ${e.length} bytes`),this.pubnub.publish({channel:this.channel,message:t,storeInHistory:this.config?.storeInHistory??!1}).then((e=>{this.log("✅ Message published",e)})).catch((e=>{this.log("❌ Publish error:",e)}))}catch(e){this.log("❌ Send error:",e)}else this.log("⚠️ Cannot send: not connected")}sendChunked(e,t){const s=this.generateUUID(),n=[];for(let t=0;t<e.length;t+=this.MAX_MESSAGE_SIZE)n.push(e.slice(t,t+this.MAX_MESSAGE_SIZE));this.log(`📦 Splitting ${t} bytes into ${n.length} chunks (id: ${s.slice(0,8)}...)`),n.forEach(((e,t)=>{const i={chunked:!0,id:s,index:t,total:n.length,data:e};this.pubnub.publish({channel:this.channel,message:i,storeInHistory:!1}).then((()=>{this.log(`✅ Chunk ${t+1}/${n.length} sent`)})).catch((e=>{this.log(`❌ Failed to send chunk ${t+1}:`,e)}))}))}handleChunkedMessage(e,t){const{id:s,index:n,total:i,data:r}=e;this.log(`📨 Received chunk ${n+1}/${i} from ${t}`),this.chunkBuffer.has(s)||this.chunkBuffer.set(s,new Map);const o=this.chunkBuffer.get(s);if(o.set(n,r),o.size===i){this.log(`📦 All chunks received, reassembling message ${s.slice(0,8)}...`);let e="";for(let t=0;t<i;t++){const n=o.get(t);if(!n)return this.log(`❌ Missing chunk ${t}, cannot reassemble`),void this.chunkBuffer.delete(s);e+=n}this.chunkBuffer.delete(s);try{const s=this.base64ToUint8(e);this.log(`✅ Message reassembled: ${s.length} bytes from ${t}`),this.messageCallback?this.messageCallback(s):this.messageBuffer.push(s)}catch(e){this.log("❌ Error reassembling chunked message:",e)}}}onMessage(e){if(this.messageCallback=e,this.messageBuffer.length>0){this.log(`📦 Flushing ${this.messageBuffer.length} buffered messages...`);for(const t of this.messageBuffer)e(t);this.messageBuffer=[]}return()=>{this.messageCallback=void 0}}async getPresence(){if(!this.pubnub||!this._isConnected)return[];try{const e=await this.pubnub.hereNow({channels:[this.channel],includeUUIDs:!0});return(e.channels[this.channel]?.occupants||[]).map((e=>e.uuid)).filter((e=>e!==this.uuid))}catch(e){return this.log("Error getting presence:",e),[]}}uint8ToBase64(e){let t="";for(let s=0;s<e.length;s+=8192){const n=e.subarray(s,Math.min(s+8192,e.length));t+=String.fromCharCode(...n)}return btoa(t)}base64ToUint8(e){return new Uint8Array(atob(e).split("").map((e=>e.charCodeAt(0))))}generateUUID(){return"undefined"!=typeof crypto&&crypto.randomUUID?crypto.randomUUID():"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(e=>{const t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)}))}log(e,...t){this.debug&&console.log(`[PubNubTransport] ${e}`,...t)}}},{"@parcel/transformer-js/src/esmodule-helpers.js":"XTfsy"}],fDdlt:[function(e,t,s){var n,i,r=t.exports={};function o(){throw new Error("setTimeout has not been defined")}function u(){throw new Error("clearTimeout has not been defined")}function c(e){if(n===setTimeout)return setTimeout(e,0);if((n===o||!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:o}catch(e){n=o}try{i="function"==typeof clearTimeout?clearTimeout:u}catch(e){i=u}}();var h,l=[],a=!1,d=-1;function b(){a&&h&&(a=!1,h.length?l=h.concat(l):d=-1,l.length&&f())}function f(){if(!a){var e=c(b);a=!0;for(var t=l.length;t;){for(h=l,l=[];++d<t;)h&&h[d].run();d=-1,t=l.length}h=null,a=!1,function(e){if(i===clearTimeout)return clearTimeout(e);if((i===u||!i)&&clearTimeout)return i=clearTimeout,clearTimeout(e);try{return i(e)}catch(t){try{return i.call(null,e)}catch(t){return i.call(this,e)}}}(e)}}function g(e,t){this.fun=e,this.array=t}function p(){}r.nextTick=function(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var s=1;s<arguments.length;s++)t[s-1]=arguments[s];l.push(new g(e,t)),1!==l.length||a||c(f)},g.prototype.run=function(){this.fun.apply(null,this.array)},r.title="browser",r.browser=!0,r.env={},r.argv=[],r.version="",r.versions={},r.on=p,r.addListener=p,r.once=p,r.off=p,r.removeListener=p,r.removeAllListeners=p,r.emit=p,r.prependListener=p,r.prependOnceListener=p,r.listeners=function(e){return[]},r.binding=function(e){throw new Error("process.binding is not supported")},r.cwd=function(){return"/"},r.chdir=function(e){throw new Error("process.chdir is not supported")},r.umask=function(){return 0}},{}]},[],0,"parcelRequire55a5");