UNPKG

@liascript/exporter

Version:
1 lines 15.2 kB
!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("izF7z",(function(n,r){e(n.exports,"Sync",(function(){return y}));var o=t("f1evb"),s=t("hnmIs"),i=t("9Klpk"),a=t("3bWnf"),u=t("eT7f3"),c=t("9WzNc"),l=t("div3d"),d=t("kb0qF"),p=t("bWNdT"),h=t("32Euv"),f=t("auvHb"),g=t("2lzFl"),v=t("733OA");Object.defineProperty(Array.prototype,"flat",{value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1;return this.reduce((function(t,n){return t.concat(Array.isArray(n)&&e>1?n.flat(e-1):n)}),[])}});var y=function(e){"use strict";(0,d.default)(n,e);var t=(0,p.default)(n);function n(){var e;return(0,i.default)(this,n),e=t.apply(this,arguments),(0,u.default)((0,o.default)(e),"store",""),(0,u.default)((0,o.default)(e),"gunServer",[]),(0,u.default)((0,o.default)(e),"persistent",!1),(0,u.default)((0,o.default)(e),"syncFallbackTimer",null),e}return(0,a.default)(n,[{key:"destroy",value:function(){var e;null!==this.syncFallbackTimer&&(clearTimeout(this.syncFallbackTimer),this.syncFallbackTimer=null),(0,c.default)((0,l.default)(n.prototype),"destroy",this).call(this),this.gunServer=[],null===(e=this.provider)||void 0===e||e.disconnect()}},{key:"uniqueID",value:function(){var e=(0,c.default)((0,l.default)(n.prototype),"uniqueID",this).call(this);return e?btoa(e+(this.persistent?"p":"")):null}},{key:"connect",value:function(e){var t=this,r=this;return(0,s.default)((function(){var o,s,i;return(0,h.__generator)(this,(function(a){return(0,c.default)((0,l.default)(n.prototype),"connect",t).call(r,e),r.gunServer=(null===(o=e.config)||void 0===o?void 0:o.urls)||[],r.persistent=(null===(s=e.config)||void 0===s?void 0:s.persistent)||!1,window.Gun&&(null===(i=window.Gun)||void 0===i?void 0:i.SEA)?r.init(!0):r.load(["https://cdn.jsdelivr.net/npm/gun/gun.js","https://cdn.jsdelivr.net/npm/gun/sea.js"],r),[2]}))}))()}},{key:"init",value:function(e,t){var n=this;if(0==this.gunServer.length)return this.sendDisconnectError("You have to provide at least one relay server.");var r=this.uniqueID();if(e&&window.Gun&&r){var o;this.transport=new(0,g.GunTransport)({gun:window.Gun,sea:null===(o=window.Gun)||void 0===o?void 0:o.SEA,peers:this.gunServer,debug:!1,batchInterval:200,gunOptions:{localStorage:!1,radisk:!1},password:this.password}),this.store=r,this.provider=new(0,v.GenericProvider)(this.db.doc,this.transport),this.db.setAwareness(this.provider.awareness);var s=!1,i=function(){s||(s=!0,null!==n.syncFallbackTimer&&(clearTimeout(n.syncFallbackTimer),n.syncFallbackTimer=null),n.sendConnect())};this.provider.on("synced",(function(e){console.log("Document synchronized",e.synced),i()})),this.provider.on("status",(function(e){var t=e.state;console.log("Status changed: ".concat(t),"info"),"connected"===t?n.syncFallbackTimer=setTimeout((function(){console.log("Sync fallback: no remote peers, proceeding as first peer"),i()}),2e3):"disconnected"===t?console.warn("Disconnected from GunDB relay server"):console.warn("GunDB status: ".concat(t))})),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({room:this.store})}else{var a,u="GunDB unknown error";t?u="Could not load resource: "+t:window.Gun?(null===(a=window.Gun)||void 0===a?void 0:a.SEA)||(u="Could not load GunDB SEA security module"):u="Could not load GunDB interface",this.sendDisconnectError(u)}}},{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}(f.Sync)})),t.register("9WzNc",(function(n,r){e(n.exports,"default",(function(){return i}));var o=t("jwduU");function s(e,t,n){return s="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(e,t,n){var r=(0,o.default)(e,t);if(r){var s=Object.getOwnPropertyDescriptor(r,t);return s.get?s.get.call(n||e):s.value}},s(e,t,n)}function i(e,t,n){return s(e,t,n)}})),t.register("jwduU",(function(n,r){e(n.exports,"default",(function(){return s}));var o=t("div3d");function s(e,t){for(;!Object.prototype.hasOwnProperty.call(e,t)&&null!==(e=(0,o.default)(e)););return e}})),t.register("2lzFl",(function(n,r){e(n.exports,"GunTransport",(function(){return d}));var o=t("hnmIs"),s=t("9Klpk"),i=t("3bWnf"),a=t("bgoEC"),u=t("5ENmm"),c=t("6Wo7Q"),l=t("32Euv"),d=function(){"use strict";function e(t){if((0,s.default)(this,e),this._connected=!1,this._room="",this.gun=null,this.roomNode=null,this.updateListener=null,this.lastUpdateTime=0,this.updateBatch=[],this.processedUpdates=new Set,this.connectionTime=0,this.pendingUpdates=new Map,this.updateSlot=0,this.BUFFER_SIZE=20,this.awarenessListener=null,this.lastAwarenessId="",this.encryptionEnabled=!1,!t.gun)throw new Error('GunTransport requires the "gun" option. Please provide the Gun constructor: import Gun from "gun"; new GunTransport({ gun: Gun, ... })');if(t.password&&!t.sea)throw new Error('GunTransport requires the "sea" option when using password encryption. Please provide Gun.SEA: import "gun/sea"; new GunTransport({ gun: Gun, sea: Gun.SEA, password: "..." })');var n,r,o,i;this.options={gun:t.gun,peers:null!==(n=t.peers)&&void 0!==n?n:[],gunOptions:null!==(r=t.gunOptions)&&void 0!==r?r:{},debug:null!==(o=t.debug)&&void 0!==o&&o,batchInterval:null!==(i=t.batchInterval)&&void 0!==i?i:100,password:t.password,sea:t.sea},this.encryptionEnabled=!(!t.password||!t.sea),this.encryptionEnabled&&this.log("🔐 Encryption enabled")}return(0,i.default)(e,[{key:"connect",value:function(e){var t=this;return(0,o.default)((function(){var n;return(0,l.__generator)(this,(function(r){if(t._connected)throw new Error("Already connected");return t._room=e.room,t.connectionTime=Date.now(),t.log("🔗 Initializing Gun..."),n=(0,a.default)({localStorage:!1,radisk:!1},t.options.gunOptions),t.options.peers.length>0&&(n.peers=t.options.peers,t.log("📡 Connecting to peers:",t.options.peers)),t.gun=new t.options.gun(n),t.roomNode=t.gun.get("yjs-room-".concat(t._room)),t.log("✅ Gun initialized for room:",t._room),t.setupUpdateListener(),t.setupAwarenessListener(),t._connected=!0,[2]}))}))()}},{key:"setupUpdateListener",value:function(){var e=this,t=0,n=!1;this.roomNode.get("updates").once((function(t){if(!t)return n=!0,void e.log("📭 No existing updates found");e.log("📥 Loading initial state..."),Object.keys(t).forEach((function(n){if("_"!==n){var r=t[n];if(r&&r.data){var o=r.sequence||Math.floor(r.timestamp/100),s="".concat(n,"-").concat(o);e.processedUpdates.has(s)||e.pendingUpdates.set(s,r)}}})),e.processPendingUpdates(),n=!0,e.log("✅ Initial state loaded")})),this.updateListener=this.roomNode.get("updates").map().on((function(r,o){if(n&&r&&r.data&&!(r.timestamp&&r.timestamp<e.connectionTime)){var s=r.sequence||Math.floor(r.timestamp/100),i="".concat(o,"-").concat(s);if(!e.processedUpdates.has(i)){e.pendingUpdates.set(i,r);var a=Date.now();a-t<300?e.throttleTimeout||(e.throttleTimeout=setTimeout((function(){e.processPendingUpdates(),t=Date.now(),e.throttleTimeout=void 0}),300)):(t=a,e.processPendingUpdates())}}})),this.log("👂 Listening for new updates...")}},{key:"processPendingUpdates",value:function(){var e=this;return(0,o.default)((function(){var t,n,r,o,s,i,a,c,d,p,h,f,g,v;return(0,l.__generator)(this,(function(l){switch(l.label){case 0:if(0===e.pendingUpdates.size)return[2];t=Array.from(e.pendingUpdates.entries()),e.pendingUpdates.clear(),n=!0,r=!1,o=void 0,l.label=1;case 1:l.trys.push([1,9,10,11]),s=t[Symbol.iterator](),l.label=2;case 2:if(n=(i=s.next()).done)return[3,8];a=(0,u.default)(i.value,2),c=a[0],d=a[1],e.processedUpdates.add(c),e.processedUpdates.size>200&&(p=Array.from(e.processedUpdates)).slice(0,p.length-200).forEach((function(t){e.processedUpdates.delete(t)})),l.label=3;case 3:return l.trys.push([3,6,,7]),h=d.data,d.encrypted&&e.encryptionEnabled?[4,e.decrypt(h)]:[3,5];case 4:if(!(h=l.sent()))return e.log("❌ Failed to decrypt update (wrong password?)"),[3,7];l.label=5;case 5:return f=e.base64ToUint8Array(h),e._callback&&e._callback(f),1===t.length&&e.log("📥 Received update:",f.length,"bytes",d.encrypted?"(decrypted)":""),[3,7];case 6:return g=l.sent(),e.log("❌ Error processing update:",g),[3,7];case 7:return n=!0,[3,2];case 8:return[3,11];case 9:return v=l.sent(),r=!0,o=v,[3,11];case 10:try{n||null==s.return||s.return()}finally{if(r)throw o}return[7];case 11:return t.length>1&&e.log("📥 Processed ".concat(t.length," batched updates")),[2]}}))}))()}},{key:"disconnect",value:function(){this._connected&&(this.log("👋 Disconnecting..."),this.batchTimeout&&(clearTimeout(this.batchTimeout),this.batchTimeout=void 0),this.throttleTimeout&&(clearTimeout(this.throttleTimeout),this.throttleTimeout=void 0),this.processPendingUpdates(),this.flushBatch(),this.updateListener&&(this.updateListener=null),this.awarenessListener&&(this.awarenessListener=null),this.roomNode=null,this.gun=null,this._connected=!1,this.processedUpdates.clear(),this.pendingUpdates.clear(),this.log("✅ Disconnected"))}},{key:"send",value:function(e){var t=this;this._connected&&this.roomNode?1!==this.peekMessageType(e)?(this.updateBatch.push(e),this.batchTimeout&&clearTimeout(this.batchTimeout),this.batchTimeout=setTimeout((function(){t.flushBatch()}),this.options.batchInterval)):this.sendAwareness(e):this.log("⚠️ Not connected, cannot send")}},{key:"peekMessageType",value:function(e){return e.length<5?-1:e[4]}},{key:"sendAwareness",value:function(e){var t=this;return(0,o.default)((function(){var n,r;return(0,l.__generator)(this,(function(o){switch(o.label){case 0:return n=t.uint8ArrayToBase64(e),t.encryptionEnabled?[4,t.encrypt(n)]:[3,2];case 1:n=o.sent(),o.label=2;case 2:return r="aware-".concat(Date.now(),"-").concat(Math.random().toString(36).substr(2,5)),t.lastAwarenessId=r,t.roomNode.get("awareness").put({data:n,id:r,timestamp:Date.now(),encrypted:t.encryptionEnabled}),t.log("📤 Sent awareness update",t.encryptionEnabled?"(encrypted)":""),[2]}}))}))()}},{key:"setupAwarenessListener",value:function(){var e,t=this;this.awarenessListener=this.roomNode.get("awareness").on((e=(0,o.default)((function(e){var n,r,o;return(0,l.__generator)(this,(function(s){switch(s.label){case 0:if(!e||!e.data)return[2];if(e.id===t.lastAwarenessId)return[2];if(e.timestamp&&e.timestamp<t.connectionTime)return[2];s.label=1;case 1:return s.trys.push([1,4,,5]),n=e.data,e.encrypted&&t.encryptionEnabled?[4,t.decrypt(n)]:[3,3];case 2:if(!(n=s.sent()))return t.log("❌ Failed to decrypt awareness (wrong password?)"),[2];s.label=3;case 3:return r=t.base64ToUint8Array(n),t._callback&&t._callback(r),t.log("📥 Received awareness update",e.encrypted?"(decrypted)":""),[3,5];case 4:return o=s.sent(),t.log("❌ Error processing awareness:",o),[3,5];case 5:return[2]}}))})),function(t){return e.apply(this,arguments)})),this.log("👂 Listening for awareness updates...")}},{key:"flushBatch",value:function(){var e=this;return(0,o.default)((function(){var t,n,r,o,s,i,a,u,c,d,p,h,f;return(0,l.__generator)(this,(function(l){switch(l.label){case 0:if(0===e.updateBatch.length)return[2];t=e.updateBatch.reduce((function(e,t){return e+t.length}),0),n=new Uint8Array(t),r=0,o=!0,s=!1,i=void 0;try{for(a=e.updateBatch[Symbol.iterator]();!(o=(u=a.next()).done);o=!0)c=u.value,n.set(c,r),r+=c.length}catch(e){s=!0,i=e}finally{try{o||null==a.return||a.return()}finally{if(s)throw i}}return e.updateBatch=[],d=e.uint8ArrayToBase64(n),e.encryptionEnabled?[4,e.encrypt(d)]:[3,2];case 1:d=l.sent(),l.label=2;case 2:return p=e.generateUpdateId(),h=Date.now(),f=Math.floor(h/100),e.processedUpdates.add("".concat(p,"-").concat(f)),e.roomNode.get("updates").get(p).put({data:d,timestamp:h,sequence:f,size:n.length,encrypted:e.encryptionEnabled}),e.log("📤 Sent update:",n.length,"bytes",e.encryptionEnabled?"(encrypted)":""),[2]}}))}))()}},{key:"onMessage",value:function(e){var t=this;return this._callback=e,function(){t._callback=void 0}}},{key:"isConnected",get:function(){return this._connected}},{key:"generateUpdateId",value:function(){var e="slot-".concat(this.updateSlot);return this.updateSlot=(this.updateSlot+1)%this.BUFFER_SIZE,e}},{key:"uint8ArrayToBase64",value:function(e){for(var t="",n=0;n<e.length;n++)t+=String.fromCharCode(e[n]);return btoa(t)}},{key:"base64ToUint8Array",value:function(e){for(var t=atob(e),n=new Uint8Array(t.length),r=0;r<t.length;r++)n[r]=t.charCodeAt(r);return n}},{key:"encrypt",value:function(e){var t=this;return(0,o.default)((function(){return(0,l.__generator)(this,(function(n){switch(n.label){case 0:return t.options.sea&&t.options.password?[4,t.options.sea.encrypt(e,t.options.password)]:[2,e];case 1:return[2,n.sent()]}}))}))()}},{key:"decrypt",value:function(e){var t=this;return(0,o.default)((function(){var n;return(0,l.__generator)(this,(function(r){switch(r.label){case 0:if(!t.options.sea||!t.options.password)return[2,e];r.label=1;case 1:return r.trys.push([1,3,,4]),[4,t.options.sea.decrypt(e,t.options.password)];case 2:return[2,r.sent()||null];case 3:return n=r.sent(),t.log("❌ Decryption failed:",n),[2,null];case 4:return[2]}}))}))()}},{key:"log",value:function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];var r;this.options.debug&&(r=console).log.apply(r,["[GunTransport]"].concat((0,c.default)(t)))}}]),e}()})),t.register("84cK9",(function(e,t){var n,r,o=e.exports={};function s(){throw new Error("setTimeout has not been defined")}function i(){throw new Error("clearTimeout has not been defined")}function a(e){if(n===setTimeout)return setTimeout(e,0);if((n===s||!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:s}catch(e){n=s}try{r="function"==typeof clearTimeout?clearTimeout:i}catch(e){r=i}}();var u,c=[],l=!1,d=-1;function p(){l&&u&&(l=!1,u.length?c=u.concat(c):d=-1,c.length&&h())}function h(){if(!l){var e=a(p);l=!0;for(var t=c.length;t;){for(u=c,c=[];++d<t;)u&&u[d].run();d=-1,t=c.length}u=null,l=!1,function(e){if(r===clearTimeout)return clearTimeout(e);if((r===i||!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 f(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];c.push(new f(e,t)),1!==c.length||l||a(h)},f.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}}))}();