@liascript/exporter
Version:
A generic exporter for LiaScript
1 lines • 18.2 kB
JavaScript
function e(e,t,s,n){Object.defineProperty(e,t,{get:s,set:n,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("akyql",(function(s,n){e(s.exports,"GenericProvider",(function(){return t("hJFk3").GenericProvider}));t("hJFk3"),t("k5rhS")})),t.register("hJFk3",(function(s,n){e(s.exports,"GenericProvider",(function(){return g}));var i=t("9a6qY"),r=t("933AX"),a=t("beHgD"),o=t("8fTKk"),c=t("4VypT"),d=t("ewMJf"),h=t("iabNZ");const l=(()=>{const e=new Uint32Array(256);for(let t=0;t<256;t++){let s=t;for(let e=0;e<8;e++)s=1&s?s>>>1^3988292384:s>>>1;e[t]=s}return e})();function u(e){let t=4294967295;for(let s=0;s<e.length;s++)t=t>>>8^l[255&(t^e[s])];return(4294967295^t)>>>0}function p(e){const t=u(e),s=new Uint8Array(4+e.length);return s[0]=t>>>24&255,s[1]=t>>>16&255,s[2]=t>>>8&255,s[3]=255&t,s.set(e,4),s}function _(e){const t=i.encodeStateAsUpdate(e);let s=0;for(let e=0;e<t.length;e++)s=(s<<5)-s+t[e]|0;return s}class w extends d.Observable{publish(e,t){this.provider._sendPubSub(e,t)}subscribe(e,t){const s=(s,n)=>{"*"!==e&&e!==n||t(s,n)};return this.on("message",s),()=>this.off("message",s)}_handleMessage(e,t){this.emit("message",[t,e])}constructor(e){super(),this.provider=e}}class g extends d.Observable{async connect(e){if(this._destroying)throw new Error("Provider is being destroyed");if("connected"!==this._status.state)if("connecting"!==this._status.state){this._setStatus({state:"connecting"});try{if(this._setupBroadcastChannel(e),await this.transport.connect(e),this._unsubscribeTransport=this.transport.onMessage((e=>{this._handleIncomingMessage(e)})),this.transport.onPeerConnect){const e=this.transport.onPeerConnect((e=>{this._destroying||this.syncNow()})),t=this._unsubscribeTransport;this._unsubscribeTransport=()=>{null==t||t(),e()}}this._setStatus({state:"connected"}),this.syncNow(),this._broadcastAwareness([this.doc.clientID]),this._syncInterval>0&&(this._syncIntervalId=setInterval((()=>{if(this.transport.isConnected&&!this._destroying){const e=Date.now();this._syncRequestTimes=this._syncRequestTimes.filter((t=>e-t<this._syncRequestWindowMs)),this._syncRequestTimes.length<this._maxSyncRequestsPerWindow&&this._sendSyncStep1()}}),this._syncInterval))}catch(e){throw this._setStatus({state:"error",error:e instanceof Error?e:new Error(String(e))}),e}}else console.warn("[GenericProvider] Connection already in progress, ignoring connect() call");else console.warn("[GenericProvider] Already connected, ignoring connect() call")}disconnect(){if(void 0!==this._syncIntervalId&&(clearInterval(this._syncIntervalId),this._syncIntervalId=void 0),this._corruptedMessageCount=0,this._lastCorruptedMessageTime=0,void 0!==this._batchTimeoutId&&(clearTimeout(this._batchTimeoutId),this._batchTimeoutId=void 0,this._pendingUpdate&&this.transport.isConnected&&this._sendUpdate(this._pendingUpdate),this._pendingUpdate=null),void 0!==this._awarenessTimeoutId&&(clearTimeout(this._awarenessTimeoutId),this._awarenessTimeoutId=void 0,this._pendingAwarenessClients.size>0&&this.transport.isConnected)){const e=Array.from(this._pendingAwarenessClients);this._sendAwarenessNow(e)}this._pendingAwarenessClients.clear(),this._disconnectBroadcastChannel(),this._unsubscribeTransport&&(this._unsubscribeTransport(),this._unsubscribeTransport=void 0),r.removeAwarenessStates(this.awareness,[this.doc.clientID],"disconnect"),this.transport.disconnect(),this._synced=!1,this._setStatus({state:"disconnected"})}destroy(){this._destroying=!0,void 0!==this._syncIntervalId&&(clearInterval(this._syncIntervalId),this._syncIntervalId=void 0),void 0!==this._batchTimeoutId&&(clearTimeout(this._batchTimeoutId),this._batchTimeoutId=void 0,this._pendingUpdate&&this.transport.isConnected&&this._sendUpdate(this._pendingUpdate),this._pendingUpdate=null),this.disconnect(),this._updateHandler&&(this.doc.off("update",this._updateHandler),this._updateHandler=void 0),this._awarenessUpdateHandler&&(this.awareness.off("update",this._awarenessUpdateHandler),this._awarenessUpdateHandler=void 0),this._beforeUnloadHandler&&"undefined"!=typeof window&&(window.removeEventListener("beforeunload",this._beforeUnloadHandler),this._beforeUnloadHandler=void 0),this.awareness.destroy(),super.destroy()}get status(){return this._status}get connected(){return this.transport.isConnected}get bcConnected(){return this._bcConnected}get synced(){return this._synced}syncNow(){if(!this.transport.isConnected)return void console.warn("Cannot sync: transport not connected");const e=i.encodeStateAsUpdate(this.doc);e.length>0&&this._sendUpdate(e),this._sendSyncStep1(),this._broadcastAwareness([this.doc.clientID])}_setupDocumentSync(){this._updateHandler=(e,t)=>{t!==this&&(this._batchUpdates>0?this._batchUpdate(e):this._sendUpdate(e))},this.doc.on("update",this._updateHandler)}_batchUpdate(e){if(this._pendingUpdate)try{this._pendingUpdate=i.mergeUpdates([this._pendingUpdate,e])}catch(t){console.error("[GenericProvider] Failed to merge updates:",t),this._sendUpdate(this._pendingUpdate),this._pendingUpdate=e}else this._pendingUpdate=e;void 0!==this._batchTimeoutId&&clearTimeout(this._batchTimeoutId),this._batchTimeoutId=setTimeout((()=>{this._pendingUpdate&&(this._sendUpdate(this._pendingUpdate),this._pendingUpdate=null),this._batchTimeoutId=void 0}),this._batchUpdates)}_setupAwarenessSync(){this._awarenessUpdateHandler=({added:e,updated:t,removed:s},n)=>{const i=e.concat(t).concat(s);this._broadcastAwareness(i)},this.awareness.on("update",this._awarenessUpdateHandler),"undefined"!=typeof window&&(this._beforeUnloadHandler=()=>{r.removeAwarenessStates(this.awareness,[this.doc.clientID],"window unload"),this._disconnectBroadcastChannel()},window.addEventListener("beforeunload",this._beforeUnloadHandler))}_handleIncomingMessage(e){const t=function(e){if(e.length<4)return null;const t=(e[0]<<24|e[1]<<16|e[2]<<8|e[3])>>>0,s=e.subarray(4);return u(s)!==t?null:s}(e);if(null!==t)try{const e=c.createDecoder(t),n=c.readVarUint(e);switch(n){case 0:{const t=o.createEncoder();o.writeVarUint(t,0);a.readSyncMessage(e,t,this.doc,this)!==a.messageYjsSyncStep2||this._synced||(this._synced=!0,this.emit("synced",[!0])),o.length(t)>1&&this._send(o.toUint8Array(t));break}case 1:r.applyAwarenessUpdate(this.awareness,c.readVarUint8Array(e),this);break;case 2:{const t=c.readVarString(e),s=c.readVarUint8Array(e);try{const e=(new TextDecoder).decode(s),n=JSON.parse(e);this.pubsub._handleMessage(t,n)}catch(e){console.error("Error decoding pub/sub message:",e)}break}case 3:{const t=c.readVarUint(e),n=c.readVarUint(e);var s;const i=null!==(s=this._remoteSeqNums.get(n))&&void 0!==s?s:-1;if(t<=i){console.warn(`[GenericProvider] Duplicate or out-of-order update detected from client ${n}: seqNum ${t} <= lastSeen ${i}`);break}if(i>=0&&t>i+1){const e=t-i-1;console.warn(`[GenericProvider] Sequence gap detected from client ${n}: expected ${i+1}, got ${t} (gap of ${e} messages)`),this._sendSyncStep1()}this._remoteSeqNums.set(n,t);const r=o.createEncoder();o.writeVarUint(r,0);const d=a.readSyncMessage(e,r,this.doc,this),h=c.readVarInt(e),l=_(this.doc);if(l!==h){this._hashMismatchCount++;const e=Date.now();e-this._lastHashMismatchTime>1e4&&(this._hashMismatchCount=1),this._lastHashMismatchTime=e;const t=Math.min(1e4,10*Math.pow(5,this._hashMismatchCount-1));console.warn(`[GenericProvider] Hash mismatch #${this._hashMismatchCount} detected! Local: ${l}, Expected: ${h}`),console.warn(`[GenericProvider] Re-sync scheduled in ${t}ms...`),setTimeout((()=>{this.transport.isConnected&&!this._destroying&&this.syncNow()}),t)}else this._hashMismatchCount=0;d!==a.messageYjsSyncStep2||this._synced||l!==h||(this._synced=!0,this.emit("synced",[!0])),o.length(r)>1&&this._send(o.toUint8Array(r));break}default:console.warn("Unknown message type:",n)}}catch(e){console.error("[GenericProvider] Error handling message:",e)}else{this._corruptedMessageCount++;const e=Date.now();e-this._lastCorruptedMessageTime>1e4&&(this._corruptedMessageCount=1),this._lastCorruptedMessageTime=e,console.warn(`[GenericProvider] 💥 Corrupted message rejected (#${this._corruptedMessageCount}): CRC32 checksum mismatch. This is expected if data corruption simulation is enabled.`);const t=Math.min(5e3,100*Math.pow(5,Math.min(this._corruptedMessageCount-1,3)));setTimeout((()=>{this.transport.isConnected&&!this._destroying&&this._sendSyncStep1()}),t)}}_sendSyncStep1(){const e=Date.now();if(this._syncRequestTimes=this._syncRequestTimes.filter((t=>e-t<this._syncRequestWindowMs)),this._syncRequestTimes.length>=this._maxSyncRequestsPerWindow)return void console.warn(`[GenericProvider] Sync rate limit exceeded (${this._maxSyncRequestsPerWindow} requests per ${this._syncRequestWindowMs/1e3}s), throttling...`);this._syncRequestTimes.push(e);const t=o.createEncoder();o.writeVarUint(t,0),a.writeSyncStep1(t,this.doc),this._send(o.toUint8Array(t))}_sendUpdate(e){const t=o.createEncoder();if(this._verifyUpdates){o.writeVarUint(t,3),o.writeVarUint(t,this._localSeqNum++),o.writeVarUint(t,this.doc.clientID),a.writeUpdate(t,e);const s=_(this.doc);o.writeVarInt(t,s)}else o.writeVarUint(t,0),a.writeUpdate(t,e);this._send(o.toUint8Array(t))}_sendAwarenessUpdate(e){const t=o.createEncoder();o.writeVarUint(t,1),o.writeVarUint8Array(t,r.encodeAwarenessUpdate(this.awareness,e)),this._send(o.toUint8Array(t))}_sendPubSub(e,t){if(this.transport.isConnected)try{const s=o.createEncoder();o.writeVarUint(s,2),o.writeVarString(s,e);const n=JSON.stringify(t),i=(new TextEncoder).encode(n);o.writeVarUint8Array(s,i),this._send(o.toUint8Array(s))}catch(e){console.error("Error sending pub/sub message:",e)}else console.warn("Cannot send pub/sub message: not connected")}_broadcastAwareness(e){if(0===e.length)return;if(this._awarenessInterval<=0)return void this._sendAwarenessNow(e);for(const t of e)this._pendingAwarenessClients.add(t);if(void 0!==this._awarenessTimeoutId)return;const t=Date.now()-this._lastAwarenessTime,s=Math.max(0,this._awarenessInterval-t);this._awarenessTimeoutId=setTimeout((()=>{this._awarenessTimeoutId=void 0,this._lastAwarenessTime=Date.now();const e=Array.from(this._pendingAwarenessClients);this._pendingAwarenessClients.clear(),e.length>0&&this._sendAwarenessNow(e)}),s)}_sendAwarenessNow(e){const t=o.createEncoder();o.writeVarUint(t,1),o.writeVarUint8Array(t,r.encodeAwarenessUpdate(this.awareness,e)),this._send(o.toUint8Array(t))}_setupBroadcastChannel(e){if(this._disableBc||"undefined"==typeof BroadcastChannel||"undefined"==typeof window)return;this._bcChannel=`yjs-${e.room}`,this._bcSubscriber=(e,t)=>{if(t===this)return;const s=new Uint8Array(e);this._handleIncomingMessage(s)},h.subscribe(this._bcChannel,this._bcSubscriber),this._bcConnected=!0;const t=o.createEncoder();o.writeVarUint(t,0),a.writeSyncStep1(t,this.doc),h.publish(this._bcChannel,p(o.toUint8Array(t)),this);const s=o.createEncoder();if(o.writeVarUint(s,0),a.writeSyncStep2(s,this.doc),h.publish(this._bcChannel,p(o.toUint8Array(s)),this),null!==this.awareness.getLocalState()){const e=o.createEncoder();o.writeVarUint(e,1),o.writeVarUint8Array(e,r.encodeAwarenessUpdate(this.awareness,[this.doc.clientID])),h.publish(this._bcChannel,p(o.toUint8Array(e)),this)}}_disconnectBroadcastChannel(){if(!this._bcConnected||!this._bcSubscriber)return;const e=o.createEncoder();o.writeVarUint(e,1),o.writeVarUint8Array(e,r.encodeAwarenessUpdate(this.awareness,[this.doc.clientID],new Map)),h.publish(this._bcChannel,p(o.toUint8Array(e)),this),h.unsubscribe(this._bcChannel,this._bcSubscriber),this._bcConnected=!1,this._bcSubscriber=void 0}_send(e){const t=p(e);if(this._bcConnected&&h.publish(this._bcChannel,t,this),this.transport.isConnected)try{const e=this.transport.send(t);e instanceof Promise&&e.catch((e=>{console.error("Error sending data:",e)}))}catch(e){console.error("Error sending data:",e)}}_setStatus(e){this._status=e,this.emit("status",[e])}_testSetSequenceNumber(e){this._localSeqNum=e,console.warn(`[GenericProvider TEST] Sequence number set to ${e} (MAX_SAFE_INTEGER: ${Number.MAX_SAFE_INTEGER})`)}_testGetSequenceNumber(){return this._localSeqNum}constructor(e,t,s={}){var n,i,a,o,c;super(),this._status={state:"disconnected"},this._synced=!1,this._destroying=!1,this._bcChannel="",this._bcConnected=!1,this._hashMismatchCount=0,this._lastHashMismatchTime=0,this._syncRequestTimes=[],this._maxSyncRequestsPerWindow=20,this._syncRequestWindowMs=1e4,this._localSeqNum=0,this._remoteSeqNums=new Map,this._corruptedMessageCount=0,this._lastCorruptedMessageTime=0,this._batchUpdates=0,this._pendingUpdate=null,this._awarenessInterval=100,this._pendingAwarenessClients=new Set,this._lastAwarenessTime=0,this.doc=e,this.transport=t,this.pubsub=new w(this),this.awareness=s.awareness||new r.Awareness(e),this._syncInterval=null!==(n=s.syncInterval)&&void 0!==n?n:5e3,this._verifyUpdates=null===(i=s.verifyUpdates)||void 0===i||i,this._batchUpdates=null!==(a=s.batchUpdates)&&void 0!==a?a:0,this._disableBc=null!==(o=s.disableBc)&&void 0!==o&&o,this._awarenessInterval=null!==(c=s.awarenessInterval)&&void 0!==c?c:100,this._setupDocumentSync(),this._setupAwarenessSync()}}})),t.register("933AX",(function(s,n){e(s.exports,"Awareness",(function(){return p})),e(s.exports,"removeAwarenessStates",(function(){return _})),e(s.exports,"encodeAwarenessUpdate",(function(){return w})),e(s.exports,"applyAwarenessUpdate",(function(){return g}));var i=t("dM3DM"),r=t("aQXMn"),a=t("8fTKk"),o=t("4VypT"),c=t("fyQrk"),d=t("bktFk"),h=t("ewMJf"),l=t("3oYST");const u=3e4;class p extends h.Observable{destroy(){this.emit("destroy",[this]),this.setLocalState(null),super.destroy(),clearInterval(this._checkInterval)}getLocalState(){return this.states.get(this.clientID)||null}setLocalState(e){const t=this.clientID,s=this.meta.get(t),n=void 0===s?0:s.clock+1,i=this.states.get(t);null===e?this.states.delete(t):this.states.set(t,e),this.meta.set(t,{clock:n,lastUpdated:c.getUnixTime()});const r=[],a=[],o=[],d=[];null===e?d.push(t):null==i?null!=e&&r.push(t):(a.push(t),l.equalityDeep(i,e)||o.push(t)),(r.length>0||o.length>0||d.length>0)&&this.emit("change",[{added:r,updated:o,removed:d},"local"]),this.emit("update",[{added:r,updated:a,removed:d},"local"])}setLocalStateField(e,t){const s=this.getLocalState();null!==s&&this.setLocalState((0,r.default)((0,i.default)({},s),{[e]:t}))}getStates(){return this.states}constructor(e){super(),this.doc=e,this.clientID=e.clientID,this.states=new Map,this.meta=new Map,this._checkInterval=setInterval((()=>{const e=c.getUnixTime();null!==this.getLocalState()&&15e3<=e-this.meta.get(this.clientID).lastUpdated&&this.setLocalState(this.getLocalState());const t=[];this.meta.forEach(((s,n)=>{n!==this.clientID&&u<=e-s.lastUpdated&&this.states.has(n)&&t.push(n)})),t.length>0&&_(this,t,"timeout")}),d.floor(3e3)),e.on("destroy",(()=>{this.destroy()})),this.setLocalState({})}}const _=(e,t,s)=>{const n=[];for(let s=0;s<t.length;s++){const i=t[s];if(e.states.has(i)){if(e.states.delete(i),i===e.clientID){const t=e.meta.get(i);e.meta.set(i,{clock:t.clock+1,lastUpdated:c.getUnixTime()})}n.push(i)}}n.length>0&&(e.emit("change",[{added:[],updated:[],removed:n},s]),e.emit("update",[{added:[],updated:[],removed:n},s]))},w=(e,t,s=e.states)=>{const n=t.length,i=a.createEncoder();a.writeVarUint(i,n);for(let r=0;r<n;r++){const n=t[r],o=s.get(n)||null,c=e.meta.get(n).clock;a.writeVarUint(i,n),a.writeVarUint(i,c),a.writeVarString(i,JSON.stringify(o))}return a.toUint8Array(i)},g=(e,t,s)=>{const n=o.createDecoder(t),i=c.getUnixTime(),r=[],a=[],d=[],h=[],u=o.readVarUint(n);for(let t=0;t<u;t++){const t=o.readVarUint(n);let s=o.readVarUint(n);const c=JSON.parse(o.readVarString(n)),u=e.meta.get(t),p=e.states.get(t),_=void 0===u?0:u.clock;(_<s||_===s&&null===c&&e.states.has(t))&&(null===c?t===e.clientID&&null!=e.getLocalState()?s++:e.states.delete(t):e.states.set(t,c),e.meta.set(t,{clock:s,lastUpdated:i}),void 0===u&&null!==c?r.push(t):void 0!==u&&null===c?h.push(t):null!==c&&(l.equalityDeep(c,p)||d.push(t),a.push(t)))}(r.length>0||d.length>0||h.length>0)&&e.emit("change",[{added:r,updated:d,removed:h},s]),(r.length>0||a.length>0||h.length>0)&&e.emit("update",[{added:r,updated:a,removed:h},s])}})),t.register("beHgD",(function(s,n){e(s.exports,"messageYjsSyncStep2",(function(){return o})),e(s.exports,"writeSyncStep1",(function(){return c})),e(s.exports,"writeSyncStep2",(function(){return d})),e(s.exports,"writeUpdate",(function(){return l})),e(s.exports,"readSyncMessage",(function(){return p}));var i=t("8fTKk"),r=t("4VypT"),a=t("9a6qY");const o=1,c=(e,t)=>{i.writeVarUint(e,0);const s=a.encodeStateVector(t);i.writeVarUint8Array(e,s)},d=(e,t,s)=>{i.writeVarUint(e,o),i.writeVarUint8Array(e,a.encodeStateAsUpdate(t,s))},h=(e,t,s,n)=>{try{a.applyUpdate(t,r.readVarUint8Array(e),s)}catch(e){null!=n&&n(e),console.error("Caught error while handling a Yjs update",e)}},l=(e,t)=>{i.writeVarUint(e,2),i.writeVarUint8Array(e,t)},u=h,p=(e,t,s,n,i)=>{const a=r.readVarUint(e);switch(a){case 0:((e,t,s)=>{d(t,s,r.readVarUint8Array(e))})(e,t,s);break;case o:h(e,s,n,i);break;case 2:u(e,s,n,i);break;default:throw new Error("Unknown message type")}return a}})),t.register("iabNZ",(function(s,n){e(s.exports,"subscribe",(function(){return l})),e(s.exports,"unsubscribe",(function(){return u})),e(s.exports,"publish",(function(){return p}));var i=t("l1iu4"),r=t("2JauM"),a=t("ggKo5"),o=t("lqOdn");const c=new Map;const d="undefined"==typeof BroadcastChannel?class{postMessage(e){o.varStorage.setItem(this.room,a.toBase64(a.createUint8ArrayFromArrayBuffer(e)))}close(){o.offChange(this._onChange)}constructor(e){this.room=e,this.onmessage=null,this._onChange=t=>t.key===e&&null!==this.onmessage&&this.onmessage({data:a.fromBase64(t.newValue||"")}),o.onChange(this._onChange)}}:BroadcastChannel,h=e=>i.setIfUndefined(c,e,(()=>{const t=r.create(),s=new d(e);return s.onmessage=e=>t.forEach((t=>t(e.data,"broadcastchannel"))),{bc:s,subs:t}})),l=(e,t)=>(h(e).subs.add(t),t),u=(e,t)=>{const s=h(e),n=s.subs.delete(t);return n&&0===s.subs.size&&(s.bc.close(),c.delete(e)),n},p=(e,t,s=null)=>{const n=h(e);n.bc.postMessage(t),n.subs.forEach((e=>e(t,s)))}})),t.register("k5rhS",(function(e,s){t("9a6qY")}));