@grapecity-software/js-collaboration-ot 18.1.4 → 18.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("@grapecity-software/js-collaboration"));else if("function"==typeof define&&define.amd)define(["@grapecity-software/js-collaboration"],t);else{var s="object"==typeof exports?t(require("@grapecity-software/js-collaboration")):t(e["@grapecity-software/js-collaboration"]);for(var r in s)("object"==typeof exports?exports:e)[r]=s[r]}}(this,(e=>(()=>{"use strict";var t={619:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Db=void 0,t.Db=class{async getCommittedOpVersion(e,t,s){const r=await this.getOps(e,0,t);for(let e=r.length;e--;){const t=r[e];if(s.src===t.src&&s.seq===t.seq)return t.v}}}},985:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.MemoryDb=void 0;const r=s(619);class n extends r.Db{constructor(e){var t;super(),this.ops=new Map,this.documents=new Map,this.fragments=new Map,this.mockWaitTime=null!==(t=null==e?void 0:e.mockWaitTime)&&void 0!==t?t:0}async getSnapshot(e){return this.execute((async()=>{const t=this.documents.get(e);if(!t)return;const s=await this.getFragments(e);return{id:e,v:t.snapshotVersion,type:t.type,fragments:s}}))}getDocument(e){return this.execute((()=>{const t=this.documents.get(e);if(t)return JSON.parse(JSON.stringify(t))}))}getFragments(e){return this.execute((()=>{const t=this.fragments.get(e);return t?JSON.parse(JSON.stringify(Object.fromEntries(t))):void 0}))}getFragment(e,t){return this.execute((()=>{var s;const r=null===(s=this.fragments.get(e))||void 0===s?void 0:s.get(t);if(r)return JSON.parse(JSON.stringify(r))}))}getOps(e,t,s){return this.execute((()=>{const r=this.ops.get(e);if(!r)return[];s=null!=s?s:Number.MAX_SAFE_INTEGER;const n=r.filter((e=>e.v>=t&&e.v<s));return JSON.parse(JSON.stringify(n))}))}commitOp(e,t,s){return this.execute((()=>{var r;if(t=JSON.parse(JSON.stringify(t)),s=JSON.parse(JSON.stringify(s)),t.create){if(this.documents.has(e))return!1;const{version:r,snapshotVersion:n,type:i}=s;return this.documents.set(e,{id:e,version:r,snapshotVersion:n,type:i}),this.ops.set(e,[t]),this.fragments.set(e,new Map),!0}if(t.del)return!!this.documents.has(e)&&(this.documents.delete(e),this.ops.delete(e),this.fragments.delete(e),!0);{const n=null===(r=this.ops.get(e))||void 0===r?void 0:r.length;return t.v===n&&(this.ops.get(e).push(t),this.documents.get(e).version=s.version,!0)}}))}commitSnapshot(e,t){return this.execute((()=>{var s,r,n;t=JSON.parse(JSON.stringify(t));const i=this.documents.get(e);if(t.fromVersion!==i.snapshotVersion)return!1;if(t.v<=i.snapshotVersion)return!1;i.snapshotVersion=t.v;const o=t.fragmentsChanges;if(o.deleteSnapshot)this.fragments.delete(e);else{const t=this.fragments.get(e);Object.entries(null!==(s=o.createFragments)&&void 0!==s?s:{}).forEach((async([e,s])=>{t.set(e,s)})),Object.entries(null!==(r=o.updateFragments)&&void 0!==r?r:{}).forEach((async([e,s])=>{t.set(e,s)})),null===(n=o.deleteFragments)||void 0===n||n.forEach((e=>{t.delete(e)}))}return!0}))}close(){return this.execute((()=>{}))}dump(){const e={ops:this.ops,documents:this.documents,fragments:this.fragments};return JSON.parse(JSON.stringify(e,((e,t)=>t instanceof Map?Object.fromEntries(t.entries()):t)))}_wait(){return new Promise((e=>setTimeout(e,this.mockWaitTime)))}async execute(e){return await this._wait(),await e()}}t.MemoryDb=n},347:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.DocumentController=void 0;const r=s(127),n=s(961),i=s(660),o=r.LoggerManager.createLogger("OT.Document"),a=i.MessageActions,c=i.OTError.CODES,_="ot-doc-subscribed";function u(e){return{id:e.id,v:e.v,data:e.data,type:e.type}}t.DocumentController=class{constructor(e){this.service=e}async trigger(e,t,s){await this.service.trigger(e,t,s)}send(e,t){e.send(JSON.stringify(t),i.OT_DOC_MESSAGE_TYPE)}_sendOp(e,t){const s={a:a.op,v:t.v,src:t.src,seq:t.seq};"op"in t&&(s.op=t.op),t.create&&(s.create=t.create),t.del&&(s.del=!0),this.send(e,s)}_broadcastOp(e,t){const s={a:i.MessageActions.op,v:t.v,src:t.src,seq:t.seq};"op"in t&&(s.op=t.op),t.create&&(s.create=t.create),t.del&&(s.del=!0),e.broadcastWithFilter(JSON.stringify(s),i.OT_DOC_MESSAGE_TYPE,(t=>e.id!==t.id&&!!t.innerTags.get(_)))}_sendOps(e,t){if(t&&!t.length)for(let s=0;s<t.length;s++)this._sendOp(e,t[s])}async _reply(e,t,s){s.a=t.a;const r={connection:e,request:t,reply:s};await this.trigger("reply",r,(s=>{s?this._replyError(e,t,s):this.send(e,r.reply)}))}async _replyError(e,t,s){t.error=function(e){return e instanceof i.OTError?{code:e.code,message:e.message}:e instanceof Error?{code:c.ERR_UNKNOWN_ERROR,message:e.message}:"string"==typeof e?{code:c.ERR_UNKNOWN_ERROR,message:e}:void o.warn("unknown error",e)}(s),this.send(e,t)}async _messageHandler(e,t){const s=this;if("string"!=typeof t)return void s.close(e,new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Received non-object message"));let r;try{r=JSON.parse(t)}catch(t){return void s.close(e,new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Received non-object message"))}await this.trigger("receive",{connection:e,request:r},(async t=>{t?await s._replyError(e,r,t):await s._handleMessage(e,r,((t,n)=>{t?s._replyError(e,r,t):s._reply(e,r,n||{})}))}))}_checkRequest(e){if(e.a===a.op&&!(0,n.isNullOrUndefined)(e.v)&&("number"!=typeof e.v||e.v<0))return"Invalid version"}async _handleMessage(e,t,s){const r=this._checkRequest(t);if(r)return s(new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,r));switch(t.a){case a.fetch:return await this._fetch(e,t.v,s);case a.subscribe:return await this._subscribe(e,t.v,s);case a.unsubscribe:return this._unsubscribe(e);case a.op:return await this._submit(e,t,s);case a.fetchHistorySnapshot:return await this._fetchHistorySnapshot(e,t,s);default:return await s(new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Invalid or unknown message"))}}async _fetch(e,t,s){if(!(0,n.isNullOrUndefined)(t))return await this._fetchOps(e,t,s);await this.service._fetch(e.roomId,{connection:e},((e,t)=>e?s(e):s(null,{data:u(t)})))}async _fetchOps(e,t,s){await this.service._getOps(e.roomId,t,void 0,{connection:e},((t,r)=>t?s(t):(this._sendOps(e,r),s())))}async _subscribe(e,t,s){const r=this;await this.service._subscribe(e.roomId,t,{connection:e},((t,n)=>{if(t)return s(t);const{ops:i,snapshot:o}=n;return i&&r._sendOps(e,i),e.innerTags.set(_,!0),o?s(null,{data:u(o)}):s()}))}_unsubscribe(e){e.innerTags.delete(_)}async _submit(e,t,s){const r=function(e,t){const s=e.src||t;return e.op?function(e,t,s,r,n){return{src:e,seq:t,v:s,op:r,x:n,m:void 0}}(s,e.seq,e.v,e.op,e.x):e.create?function(e,t,s,r,n){return{src:e,seq:t,v:s,create:r,x:n,m:void 0}}(s,e.seq,e.v,e.create,e.x):e.del?function(e,t,s,r,n){return{src:e,seq:t,v:s,del:r,x:n,m:void 0}}(s,e.seq,e.v,e.del,e.x):void 0}(t,e.id);return r?r.seq>=i.util.MAX_SAFE_INTEGER?s(new i.OTError(c.ERR_CONNECTION_SEQ_INTEGER_OVERFLOW,"Connection seq has exceeded the max safe integer, maybe from being open for too long")):await this._submitOp(e,r,s):s(new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Invalid op message"))}async _submitOp(e,t,s){const r=this;await this.service._submit(e.roomId,t,{connection:e},(t=>{r._broadcastOp(e,t.op)}),((n,o)=>{const a={src:t.src,seq:t.seq,v:t.v};return n?n instanceof i.OTError&&n.code==c.ERR_OP_ALREADY_SUBMITTED?s(null,a):s(n):(r._sendOps(e,o),s(null,a))}))}async _fetchHistorySnapshot(e,t,s){if(void 0===t.v)return s(new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Invalid fetch history snapshot message"));await this.service._fetchHistorySnapshot(e.roomId,t.v,{connection:e},((e,r)=>e?s(e):s(null,{id:t.id,data:r})))}close(e,t){t&&o.warn("Connection closed due to error",e.id,t.stack||t),e.close()}}},763:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.documentFeature=function(e){const t=e||new r.DocumentServices,s=new n.DocumentController(t);return{middlewares:{message:async(e,t)=>e.type!==i.OT_DOC_MESSAGE_TYPE?await t():s._messageHandler(e.connection,e.data)}}};const r=s(89),n=s(347),i=s(660)},89:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.DocumentServices=void 0;const r=s(660),n=s(127),i=s(961),o=s(985),a=s(259),c=s(551),_=s(155),u=s(936),p=s(469),h=s(36),E=s(769),O=r.OTError.CODES;class d extends n.Middleware{constructor(e){var t,s,r;super(),e=null!=e?e:{},this.db=null!==(t=e.db)&&void 0!==t?t:new o.MemoryDb,this.milestoneDb=null!==(s=e.milestoneDb)&&void 0!==s?s:new u.MilestoneDB,this.maxSubmitRetries=e.maxSubmitRetries,this.submitSnapshotBatchSize=null!==(r=e.submitSnapshotBatchSize)&&void 0!==r?r:10}async fetch(e,t){let s;const r=await this._fetch(e,t||{},(e=>{s=e}));if(s)throw s;return r}async _fetch(e,t,s){const r=Date.now(),n=this;try{const i=await n._getSnapshot(e,t);return await n._sanitizeSnapshots([i],a.SnapshotTypes.current,t,(async o=>o?s(o):(await n._emitTiming("fetch",Date.now()-r,Object.assign(Object.assign({},t),{id:e})),s(null,i)))),i}catch(e){await s(e)}}async getOps(e,t,s,r){let n;const i=await this._getOps(e,t,s,r||{},(e=>{n=e}));if(n)throw n;return i}async _getOps(e,t,s,r,n){const i=this,o=Date.now();return await i._getSanitizedOps(e,t,s,r||{},(async(a,c)=>a?n(a):(await i._emitTiming("getOps",Date.now()-o,Object.assign(Object.assign({},r),{id:e,from:t,to:s})),n(null,c))))}async _subscribe(e,t,s,r){const n=this,o=Date.now();(0,i.isNullOrUndefined)(t)?await n._fetch(e,s,(async(i,a)=>i?r(i):(await n._emitTiming("subscribe.snapshot",Date.now()-o,Object.assign(Object.assign({},s),{id:e,version:t})),r(null,{snapshot:a})))):await n._getSanitizedOps(e,t,void 0,s,(async(i,a)=>i?r(i):(await n._emitTiming("subscribe.ops",Date.now()-o,Object.assign(Object.assign({},s),{id:e,version:t})),r(null,{ops:a}))))}async submit(e,t,s,r){let n;const i=await this._submit(e,t,s||{},r||(()=>{}),(e=>{n=e}));if(n)throw n;return i}async _submit(e,t,s,r,n){const i=_.ot.checkOp(t);if(i)return n(i),[];const o=this,a=new c.SubmitRequest(this,e,t,s),u={connection:s.connection,request:a},p=(e,t)=>(o.emit("submitRequestEnd",{request:a,err:e}),n(e,t));return await o.trigger("submit",u,(async t=>{if(t)return p(t);await a.submit(r,(async t=>{if(t)return p(t);await o._sanitizeOps(e,a.ops,u,(async e=>e?p(e):(o._emitTiming("submit.op.total",Date.now()-a.start,a),p(null,a.ops))))}))})),a.ops}async fetchHistorySnapshot(e,t,s){let r;const n=await this._fetchHistorySnapshot(e,t,s||{},(e=>{r=e}));if(r)throw r;return n}async _fetchHistorySnapshot(e,t,s,r){try{const n=Date.now(),i=await this._fetchHistorySnapshotImp(e,t),o=a.SnapshotTypes.byVersion;return await this._sanitizeSnapshots([i],o,s,(async e=>e?r(e):(await this._emitTiming("fetchSnapshot",Date.now()-n,Object.assign(Object.assign({},s),{version:t})),r(null,i)))),i}catch(e){await r(e)}}async _fetchHistorySnapshotImp(e,t){const s=this.db;if(void 0===t)return await this._getSnapshot(e);const n=await this.milestoneDb.getMilestoneSnapshot(e,t),i=n?n.v:0,o=await s.getOps(e,i,t),a=await this._buildSnapshotFromOps(e,n,o);if(t>a.v)throw new r.OTError(O.ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT,"Requested version exceeds latest snapshot version");return a}async _buildSnapshotFromOps(e,t,s){const r=null!=t?t:{id:e,v:0},n=new h.SnapshotFragmentsRequest(e,void 0,r.fragments),i=await _.ot.applyOps(r,s,n);if(i)throw i;return n.applyChangesToSnapshot(r),_.ot.composeFragments(r),r}async _sanitizeSnapshots(e,t,s,r){const n=new p.ReadSnapshotsRequest(e,t,s);await this.trigger("readSnapshots",{connection:s.connection,request:n},(e=>e?r((0,i.wrapError)(e,O.ERR_SNAPSHOT_READS_REJECTED)):n.hasSnapshotRejection()?r(n.getReadSnapshotsError()):r()))}async _getSanitizedOps(e,t,s,r,n){const i=await this.db.getOps(e,t,s,null==r?void 0:r.options);return await this._sanitizeOps(e,i,r,(e=>e?n(e):n(null,i))),i}async _sanitizeOps(e,t,s,r){for(const n of t){let t;if(await this._sanitizeOp(e,n,s,(e=>{t=e})),t)return r(t)}return r()}async _sanitizeOp(e,t,s,r){await this.trigger("readOp",{connection:s.connection,request:{id:e,op:t,context:s}},(e=>e?r((0,i.wrapError)(e)):r()))}async _getSnapshot(e,t){var s;const r=null==t?void 0:t.options,n=await this.db.getDocument(e,r);if(!n)return{id:e,v:0};if(n.snapshotVersion<n.version){const s=new E.SubmitSnapshotRequest(this,e,n,Object.assign(Object.assign({},t),{trigger:E.SubmitSnapshotTrigger.fetchSnapshot}));await s.submitSnapshot((e=>{if(e)throw e}))}const i=null!==(s=await this.db.getSnapshot(e,r))&&void 0!==s?s:{id:e,v:0};return _.ot.composeFragments(i),i}_emitTiming(e,t,s){return this.emit("timing",{action:e,cost:t,m:s})}}t.DocumentServices=d},259:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Hooks=t.SnapshotTypes=void 0,t.SnapshotTypes={current:"current",byVersion:"byVersion",byTimestamp:"byTimestamp"},t.Hooks={submitRequestEnd:"submitRequestEnd",timing:"timing"}},936:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.MilestoneDB=void 0;const r=s(660),n=s(961),i=r.OTError.CODES;t.MilestoneDB=class{constructor(e){var t;this.interval=null!==(t=null==e?void 0:e.interval)&&void 0!==t?t:1e3,this.map=new Map}saveMilestoneSnapshot(e){return new Promise((t=>{let s=this.map.get(e.id);s||(s=[],this.map.set(e.id,s)),s.push(e),s.sort(((e,t)=>e.v-t.v)),t(!0)}))}getMilestoneSnapshot(e,t){const s=function(e){return function(t,s){return!(0,n.isNullOrUndefined)(e)&&s.v>e}}(t);return this._findMilestoneSnapshot(e,s)}_findMilestoneSnapshot(e,t){return new Promise((s=>{if(!e)throw new r.OTError(i.ERR_MILESTONE_ARGUMENT_INVALID,"Missing ID");const n=this._getMilestoneSnapshots(e);let o;for(let e=0;e<n.length;e++){const s=n[e];if(t(o,s))break;o=s}s(o)}))}_getMilestoneSnapshots(e){var t;return null!==(t=this.map.get(e))&&void 0!==t?t:[]}}},155:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.ot=t.composeFragments=t.applyOps=t.transform=t.apply=t.normalizeType=t.checkOp=void 0;const r=s(513),n=s(961),i=s(660),o=i.OTError.CODES,a="default";t.checkOp=function(e){if((0,n.isNullOrUndefined)(e)||"object"!=typeof e)return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Op must be an object");if((0,n.isNullOrUndefined)(e.create)){if((0,n.isNullOrUndefined)(e.del)){if(!("op"in e))return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Missing op, create, or del")}else if(!0!==e.del)return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"del value must be true")}else{if("object"!=typeof e.create)return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Create data must be an object");const t=e.create.type;if("string"!=typeof t)return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Missing create type");const s=r.TypesManager.map[t];if((0,n.isNullOrUndefined)(s)||"object"!=typeof s)return new i.OTError(o.ERR_DOC_TYPE_NOT_RECOGNIZED,"Unknown type")}return(0,n.isNullOrUndefined)(e.src)||"string"==typeof e.src?(0,n.isNullOrUndefined)(e.seq)||"number"==typeof e.seq?(0,n.isNullOrUndefined)(e.src)&&!(0,n.isNullOrUndefined)(e.seq)||!(0,n.isNullOrUndefined)(e.src)&&(0,n.isNullOrUndefined)(e.seq)?new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Both src and seq must be set together"):(0,n.isNullOrUndefined)(e.m)||"object"==typeof e.m?void 0:new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"op.m must be an object or null"):new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"seq must be a number"):new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"src must be a string")},t.normalizeType=function(e){return r.TypesManager.map[e]&&r.TypesManager.map[e].uri},t.apply=async function(e,t,s){if("object"!=typeof e)return new i.OTError(o.ERR_APPLY_SNAPSHOT_NOT_PROVIDED,"Missing snapshot");if(!(0,n.isNullOrUndefined)(e.v)&&!(0,n.isNullOrUndefined)(t.v)&&e.v!==t.v)return new i.OTError(o.ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT,"Version mismatch");if(t.create){const n=t.create,c=r.TypesManager.map[n.type];if(!c)return new i.OTError(o.ERR_DOC_TYPE_NOT_RECOGNIZED,"Unknown type");try{let t;t=c.createFragments?c.createFragments(n.data):{[a]:c.create(n.data)},s.create(t),e.type=c.uri,e.v++}catch(e){return e}}else if(t.del)s.delete(),e.data=void 0,e.type=void 0,e.v++;else if("op"in t){const n=await async function(e,t,s){if(!e.type)return new i.OTError(o.ERR_DOC_DOES_NOT_EXIST,"Document does not exist");if(void 0===t)return new i.OTError(o.ERR_OT_OP_NOT_PROVIDED,"Missing op");const n=r.TypesManager.map[e.type];if(!n)return new i.OTError(o.ERR_DOC_TYPE_NOT_RECOGNIZED,"Unknown type");try{if(n.applyFragments)await n.applyFragments(s,t);else{const e=await s.getFragment(a),r=n.apply(e,t);s.updateFragment(a,r)}}catch(e){return new i.OTError(o.ERR_OT_OP_NOT_APPLIED,e.message)}}(e,t.op,s);if(n)return n;e.v++}else e.v++},t.transform=function(e,t,s){if(!(0,n.isNullOrUndefined)(t.v)&&t.v!==s.v)return new i.OTError(o.ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM,"Version mismatch");if(s.del){if(t.create||"op"in t)return new i.OTError(o.ERR_DOC_WAS_DELETED,"Document was deleted")}else{if(s.create&&("op"in t||t.create||t.del)||"op"in s&&t.create)return new i.OTError(o.ERR_DOC_ALREADY_CREATED,"Document was created remotely");if("op"in s&&"op"in t){if(!e)return new i.OTError(o.ERR_DOC_DOES_NOT_EXIST,"Document does not exist");if("string"==typeof e&&!(e=r.TypesManager.map[e]))return new i.OTError(o.ERR_DOC_TYPE_NOT_RECOGNIZED,"Unknown type");try{t.op=e.transform(t.op,s.op,"left")}catch(e){return new i.OTError(o.ERR_OT_OP_TRANSFORM_FAILED,"Op transform failed")}}}(0,n.isNullOrUndefined)(t.v)||t.v++},t.applyOps=async function(e,s,r){for(let n=0;n<s.length;n++){const i=s[n];e.v=i.v;const o=await(0,t.apply)(e,i,r);if(o)return o}},t.composeFragments=function(e){if(!e.type)return;const t=r.TypesManager.map[e.type];t&&e.fragments&&(t.composeFragments?e.data=t.composeFragments(e.fragments):e.data=e.fragments[a],delete e.fragments)},t.ot={checkOp:t.checkOp,normalizeType:t.normalizeType,apply:t.apply,transform:t.transform,applyOps:t.applyOps,composeFragments:t.composeFragments}},469:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.ReadSnapshotsRequest=void 0;const r=s(660),n=s(961);t.ReadSnapshotsRequest=class{constructor(e,t,s){this._idToError=null,this.snapshots=e,this.snapshotType=t,this.context=s||{}}rejectSnapshotRead(e,t){this._idToError||(this._idToError=Object.create(null)),this._idToError[e.id]=t}rejectSnapshotReadSilent(e,t){this.rejectSnapshotRead(e,this.silentRejectionError(t))}silentRejectionError(e){return new r.OTError(r.OTError.CODES.ERR_SNAPSHOT_READ_SILENT_REJECTION,e)}hasSnapshotRejection(){return!(0,n.isNullOrUndefined)(this._idToError)}getReadSnapshotsError(){const e=this.snapshots,t=this._idToError;if(0===e.length)return;if(1===e.length){return t[e[0].id]||void 0}const s=new r.OTError(r.OTError.CODES.ERR_SNAPSHOT_READS_REJECTED);return s.idToError=t,s}}},36:(e,t)=>{function s(e,t){var s,r,n;const i=null!==(s=e.fragments)&&void 0!==s?s:{};if(t.deleteSnapshot)e.fragments={};else{for(const[e,s]of Object.entries(null!==(r=t.createFragments)&&void 0!==r?r:{}))i[e]=s;for(const[e,s]of Object.entries(null!==(n=t.updateFragments)&&void 0!==n?n:{}))i[e]=s;t.deleteFragments&&t.deleteFragments.length>0&&t.deleteFragments.forEach((e=>delete i[e])),e.fragments=i}}Object.defineProperty(t,"__esModule",{value:!0}),t.SnapshotFragmentsRequest=void 0,t.applyChangesToSnapshot=s,t.SnapshotFragmentsRequest=class{constructor(e,t,s){this._fragmentsMap=new Map,this._createFragments=new Map,this._updateFragments=new Map,this._deleteFragments=new Set,this._deleteSnapshot=!1,this._docId=e,this._db=t,s&&Object.entries(s).forEach((([e,t])=>{this._fragmentsMap.set(e,t)}))}async _loadFragment(e){const t=await this._db.getFragment(this._docId,e);this._fragmentsMap.set(e,t)}async getFragment(e){return this._db&&!this._fragmentsMap.has(e)&&await this._loadFragment(e),this._fragmentsMap.get(e)}createFragment(e,t){this._deleteFragments.has(e)?(this._deleteFragments.delete(e),this._updateFragments.set(e,t)):this._createFragments.set(e,t),this._fragmentsMap.set(e,t)}updateFragment(e,t){this._deleteFragments.has(e)||(this._createFragments.has(e)?this._createFragments.set(e,t):this._updateFragments.set(e,t),this._fragmentsMap.set(e,t))}deleteFragment(e){this._deleteFragments.add(e),this._createFragments.delete(e),this._updateFragments.delete(e),this._fragmentsMap.set(e,void 0)}create(e){Object.entries(e).forEach((([e,t])=>{this.createFragment(e,t)}))}delete(){this._deleteSnapshot=!0,this._createFragments.clear(),this._updateFragments.clear(),this._deleteFragments.clear(),this._fragmentsMap.clear()}getChanges(){return{createFragments:Object.fromEntries(this._createFragments.entries()),updateFragments:Object.fromEntries(this._updateFragments.entries()),deleteFragments:Array.from(this._deleteFragments),deleteSnapshot:this._deleteSnapshot}}applyChangesToSnapshot(e){s(e,this.getChanges())}}},551:function(e,t,s){var r,n=this&&this.__createBinding||(Object.create?function(e,t,s,r){void 0===r&&(r=s);var n=Object.getOwnPropertyDescriptor(t,s);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,r,n)}:function(e,t,s,r){void 0===r&&(r=s),e[r]=t[s]}),i=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),o=this&&this.__importStar||(r=function(e){return r=Object.getOwnPropertyNames||function(e){var t=[];for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&(t[t.length]=s);return t},r(e)},function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var s=r(e),o=0;o<s.length;o++)"default"!==s[o]&&n(t,e,s[o]);return i(t,e),t});Object.defineProperty(t,"__esModule",{value:!0}),t.SubmitRequest=void 0;const a=s(961),c=s(660),_=o(s(155)),u=s(769),p=s(513),h=c.OTError.CODES,E=p.TypesManager.getType;t.SubmitRequest=class{constructor(e,t,s,r){this.doc=e,this.context=r||{},this.id=t,this.op=s,this.extra=s.x,delete s.x,this.start=Date.now(),this._addOpMeta(),this.maxRetries=e.maxSubmitRetries,this.retries=0,this.ops=[]}async submit(e,t){var s;const r=this,n=this.doc.db,i=this.id,o=this.op,c=Date.now(),_=null!==(s=await n.getDocument(i))&&void 0!==s?s:{id:i,version:0,snapshotVersion:0};r.document=_,await r.doc._emitTiming("submit.op.db.getDocumentInfo",Date.now()-c,r);const u=_.version;if((0,a.isNullOrUndefined)(o.v)){if(o.create&&_.type&&o.src){const e=await n.getCommittedOpVersion(i,u,o);return(0,a.isNullOrUndefined)(e)?t(r.alreadyCreatedError()):(o.v=e,t(r.alreadySubmittedError()))}o.v=u}if(o.v>u)return t(r.newerVersionError());if(o.v<u){const e=Date.now(),s=o.v,a=await n.getOps(i,s,u);if(r.doc._emitTiming("submit.op.db.getOpsForTransform",Date.now()-e,r),!a||a.length!==u-s)return t(r.missingOpsError());const c=Date.now(),_=r._transformOp(a);if(_)return t(_);if(await r.doc._emitTiming("submit.op.transform",Date.now()-c,r),o.v!==u)return t(r.versionAfterTransformError())}if(_.type){if(o.create)return t(r.alreadyCreatedError())}else if(!o.create)return t(r.docDoesNotExistError());r._updateDocument(),await r.commitOp(e,t)}_addOpMeta(){this.op.m={ts:this.start},this.op.create&&(this.op.create.type=_.normalizeType(this.op.create.type))}_updateDocument(){if(!this.document)return;const e=this.document,t=this.op;t.create?(e.type=_.normalizeType(t.create.type),e.snapshotVersion=0):t.del&&(e.type=void 0),e.version++,this._updateDocumentMeta()}_updateDocumentMeta(){const e=this.document.m||(this.document.m={});this.op.create&&(e.ctime=this.start),e.mtime=this.start}_transformOp(e){const t=E(this.document.type);for(let s=0;s<e.length;s++){const r=e[s];if(this.op.src&&this.op.src===r.src&&this.op.seq===r.seq)return this.alreadySubmittedError();if(this.op.v!==r.v)return this.versionDuringTransformError();const n=_.transform(t,this.op,r);if(n)return n;delete r.m,this.ops.push(r)}}async commitOp(e,t){const s=this,r=this.doc;await s._trigger("commit",(async n=>{if(n)return t(n);const i=Date.now();if(!await r.db.commitOp(s.id,s.op,s.document,s.context.options))return await s._retry(e,t);s.op.m=void 0,await r._emitTiming("submit.op.db.commit",Date.now()-i,s),e(s),await s._trigger("afterWrite",(async e=>{if(e)return t(e);const n=new u.SubmitSnapshotRequest(r,s.id,s.document,Object.assign(Object.assign({},s.context),{trigger:u.SubmitSnapshotTrigger.submitOp,op:s.op}));await n.submitSnapshot(t)}))}))}async _retry(e,t){if(this.retries++,null!=this.maxRetries&&this.retries>this.maxRetries)return t(this.maxRetriesError());await this.doc._emitTiming("submit.op.retry",Date.now()-this.start,this),await this.submit(e,t)}async _trigger(e,t){const s=Object.assign(Object.assign({},this.context),{request:this});await this.doc.trigger(e,s,t),this.context.options=s.options}alreadyCreatedError(){return new c.OTError(h.ERR_DOC_ALREADY_CREATED,"Invalid op submitted. Document already created")}docDoesNotExistError(){return new c.OTError(h.ERR_DOC_DOES_NOT_EXIST,"Document does not exist")}rejectedError(){return new c.OTError(h.ERR_OP_SUBMIT_REJECTED,"Op submit rejected")}alreadySubmittedError(){return new c.OTError(h.ERR_OP_ALREADY_SUBMITTED,"Op already submitted")}maxRetriesError(){return new c.OTError(h.ERR_MAX_SUBMIT_RETRIES_EXCEEDED,"Op submit failed. Exceeded max submit retries of "+this.maxRetries)}versionDuringTransformError(){return new c.OTError(h.ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM,"Op submit failed. Versions mismatched during op transform")}newerVersionError(){return new c.OTError(h.ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT,"Invalid op submitted. Op version newer than current snapshot")}missingOpsError(){return new c.OTError(h.ERR_SUBMIT_TRANSFORM_OPS_NOT_FOUND,"Op submit failed. DB missing ops needed to transform it up to the current snapshot version")}versionAfterTransformError(){return new c.OTError(h.ERR_OP_VERSION_MISMATCH_AFTER_TRANSFORM,"Op submit failed. Op version mismatches snapshot after op transform")}}},769:function(e,t,s){var r,n=this&&this.__createBinding||(Object.create?function(e,t,s,r){void 0===r&&(r=s);var n=Object.getOwnPropertyDescriptor(t,s);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,r,n)}:function(e,t,s,r){void 0===r&&(r=s),e[r]=t[s]}),i=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),o=this&&this.__importStar||(r=function(e){return r=Object.getOwnPropertyNames||function(e){var t=[];for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&(t[t.length]=s);return t},r(e)},function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var s=r(e),o=0;o<s.length;o++)"default"!==s[o]&&n(t,e,s[o]);return i(t,e),t});Object.defineProperty(t,"__esModule",{value:!0}),t.SubmitSnapshotRequest=t.SubmitSnapshotTrigger=void 0;const a=s(127),c=s(660),_=s(36),u=o(s(155)),p=c.OTError.CODES,h=a.LoggerManager.createLogger("OT.Document");var E;!function(e){e.submitOp="submitOp",e.fetchSnapshot="fetchSnapshot",e.manual="manual"}(E||(t.SubmitSnapshotTrigger=E={})),t.SubmitSnapshotRequest=class{constructor(e,t,s,r){this.doc=e,this.context=r||{},this.id=t,this.document=s,this.fromVersion=s.snapshotVersion,this.toVersion=s.version,this.start=Date.now(),this.saveSnapshot=null,this.saveMilestoneSnapshot=null,this.maxRetries=e.maxSubmitRetries,this.retries=0,this.snapshot=null}async submitSnapshot(e){const t=this,s=this.doc;await t._trigger("submitSnapshot",(async r=>{if(r)return e(r);if(!t._shouldSubmitSnapshot())return e();const n=Date.now(),i=await s.db.getOps(t.id,t.fromVersion,t.toVersion);if(await t.doc._emitTiming("submit.snapshot.db.getOpsForApply",Date.now()-n,t),!i||i.length!==t.toVersion-t.fromVersion)return h.error("submit.snapshot.missingApplyOps",{id:t.id,fromVersion:t.fromVersion,toVersion:t.toVersion}),e();t.ops=i,await this.apply((async t=>{if(t)return e(t);await this.commitSnapshot(e)}))}))}_addSnapshotMeta(){if(!this.snapshot)return;const e=this.snapshot;this.ops.forEach((t=>{const s=e.m||(e.m={});t.create&&(s.ctime=this.start),s.mtime=this.start}))}async apply(e){const t=this,s=this.doc.db,r=this.id,n=this.ops,i=t.document,o=Date.now();t.snapshot={id:r,fromVersion:i.snapshotVersion,v:i.snapshotVersion,type:i.type},t._addSnapshotMeta(),await t._trigger("apply",(async i=>{if(i)return e(i);const a=t.snapshot,c=new _.SnapshotFragmentsRequest(r,s),p=await u.applyOps(a,n,c);return p?(h.error("submit.snapshot.apply",p),e(p)):(a.fragmentsChanges=c.getChanges(),t.doc._emitTiming("submit.snapshot.apply",Date.now()-o,t),e())}))}async commitSnapshot(e){const t=this,s=this.doc;await t._trigger("commitSnapshot",(async r=>{if(r)return e(r);const n=Date.now();if(!await s.db.commitSnapshot(t.id,t.snapshot,t.context.options))return await this._retry(e);if(await s._emitTiming("submit.snapshot.db.commit",Date.now()-n,t),t._shouldSaveMilestoneSnapshot(t.snapshot)){const e=await t.doc.db.getSnapshot(t.id);(0,_.applyChangesToSnapshot)(e,t.snapshot.fragmentsChanges),await t.doc.milestoneDb.saveMilestoneSnapshot(e)}return e()}))}async _retry(e){if(this.retries++,null!=this.maxRetries&&this.retries>this.maxRetries)return e(this.maxRetriesError());const t=Date.now(),s=await this.doc.db.getDocument(this.id,this.context.options);return this.doc._emitTiming("submit.snapshot.retry.db.getDocumentInfo",Date.now()-t,this),s?s.snapshotVersion!==this.fromVersion?s.snapshotVersion>=this.toVersion?e():(this.document=s,this.fromVersion=s.snapshotVersion,await this.doc._emitTiming("submit.snapshot.retry",Date.now()-this.start,this),await this.submitSnapshot(e)):(await this.doc._emitTiming("submit.snapshot.retry",Date.now()-this.start,this),await this.submitSnapshot(e)):e()}_shouldSubmitSnapshot(){var e,t;const s=this.fromVersion,r=this.toVersion,n=null===(e=this.context)||void 0===e?void 0:e.trigger,i=null===(t=this.context)||void 0===t?void 0:t.op;return!(n!==E.submitOp||!i||!i.create&&!i.del)||n===E.fetchSnapshot||0===s||!(r<=s)&&(null===this.saveSnapshot?r-s>=this.doc.submitSnapshotBatchSize:this.saveSnapshot)}_shouldSaveMilestoneSnapshot(e){return null===this.saveMilestoneSnapshot?e&&e.v%this.doc.milestoneDb.interval==0:this.saveMilestoneSnapshot}async _trigger(e,t){const s=Object.assign(Object.assign({},this.context),{request:this});await this.doc.trigger(e,s,t),this.context.options=s.options}maxRetriesError(){return new c.OTError(p.ERR_MAX_SUBMIT_SNAPSHOT_RETRIES_EXCEEDED,"Snapshot submit failed. Exceeded max submit retries of "+this.maxRetries)}}},513:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.TypesManager=void 0;class s{static register(e){e.uri&&(s.map[e.uri]=e),e.name&&(s.map[e.name]=e)}static getType(e){return s.map[e]}}t.TypesManager=s,s.map={}},961:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.isNullOrUndefined=function(e){return null==e},t.wrapError=function(e,t){if(e instanceof r.OTError)return e;t=null!=t?t:r.OTError.CODES.ERR_UNKNOWN_ERROR;const s="string"==typeof e?e:e.message;return new r.OTError(t,s)};const r=s(660)},127:t=>{t.exports=e},872:(e,t)=>{var s;Object.defineProperty(t,"__esModule",{value:!0}),t.OTError=t.ERROR_CODES=void 0,function(e){e[e.ERR_DOC_ALREADY_CREATED=0]="ERR_DOC_ALREADY_CREATED",e[e.ERR_DOC_DOES_NOT_EXIST=1]="ERR_DOC_DOES_NOT_EXIST",e[e.ERR_DOC_TYPE_NOT_RECOGNIZED=2]="ERR_DOC_TYPE_NOT_RECOGNIZED",e[e.ERR_DOC_WAS_DELETED=3]="ERR_DOC_WAS_DELETED",e[e.ERR_MAX_SUBMIT_RETRIES_EXCEEDED=4]="ERR_MAX_SUBMIT_RETRIES_EXCEEDED",e[e.ERR_MAX_SUBMIT_SNAPSHOT_RETRIES_EXCEEDED=5]="ERR_MAX_SUBMIT_SNAPSHOT_RETRIES_EXCEEDED",e[e.ERR_MESSAGE_BADLY_FORMED=6]="ERR_MESSAGE_BADLY_FORMED",e[e.ERR_OP_ALREADY_SUBMITTED=7]="ERR_OP_ALREADY_SUBMITTED",e[e.ERR_HARD_ROLLBACK_FETCH_FAILED=8]="ERR_HARD_ROLLBACK_FETCH_FAILED",e[e.ERR_OT_OP_NOT_APPLIED=9]="ERR_OT_OP_NOT_APPLIED",e[e.ERR_OT_OP_TRANSFORM_FAILED=10]="ERR_OT_OP_TRANSFORM_FAILED",e[e.ERR_SNAPSHOT_READS_REJECTED=11]="ERR_SNAPSHOT_READS_REJECTED",e[e.ERR_SNAPSHOT_READ_SILENT_REJECTION=12]="ERR_SNAPSHOT_READ_SILENT_REJECTION",e[e.ERR_SUBMIT_TRANSFORM_OPS_NOT_FOUND=13]="ERR_SUBMIT_TRANSFORM_OPS_NOT_FOUND",e[e.ERR_CONNECTION_SEQ_INTEGER_OVERFLOW=14]="ERR_CONNECTION_SEQ_INTEGER_OVERFLOW",e[e.ERR_OT_OP_BADLY_FORMED=15]="ERR_OT_OP_BADLY_FORMED",e[e.ERR_HISTORY_SNAPSHOT_NOT_FOUND=16]="ERR_HISTORY_SNAPSHOT_NOT_FOUND",e[e.ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT=17]="ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT",e[e.ERR_DOC_MISSING_VERSION=18]="ERR_DOC_MISSING_VERSION",e[e.ERR_OP_SUBMIT_REJECTED=19]="ERR_OP_SUBMIT_REJECTED",e[e.ERR_PENDING_OP_REMOVED_BY_OP_SUBMIT_REJECTED=20]="ERR_PENDING_OP_REMOVED_BY_OP_SUBMIT_REJECTED",e[e.ERR_MILESTONE_ARGUMENT_INVALID=21]="ERR_MILESTONE_ARGUMENT_INVALID",e[e.ERR_OT_OP_NOT_PROVIDED=22]="ERR_OT_OP_NOT_PROVIDED",e[e.ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT=23]="ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT",e[e.ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM=24]="ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM",e[e.ERR_INGESTED_SNAPSHOT_HAS_NO_VERSION=25]="ERR_INGESTED_SNAPSHOT_HAS_NO_VERSION",e[e.ERR_APPLY_SNAPSHOT_NOT_PROVIDED=26]="ERR_APPLY_SNAPSHOT_NOT_PROVIDED",e[e.ERR_OP_VERSION_MISMATCH_AFTER_TRANSFORM=27]="ERR_OP_VERSION_MISMATCH_AFTER_TRANSFORM",e[e.ERR_UNKNOWN_ERROR=1e3]="ERR_UNKNOWN_ERROR"}(s||(t.ERROR_CODES=s={}));class r extends Error{constructor(e,t){super(t),this.code=e,this.name="OTError",this.code=e}}t.OTError=r,r.CODES=s},660:function(e,t,s){var r=this&&this.__createBinding||(Object.create?function(e,t,s,r){void 0===r&&(r=s);var n=Object.getOwnPropertyDescriptor(t,s);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,r,n)}:function(e,t,s,r){void 0===r&&(r=s),e[r]=t[s]}),n=this&&this.__exportStar||function(e,t){for(var s in e)"default"===s||Object.prototype.hasOwnProperty.call(t,s)||r(t,e,s)};Object.defineProperty(t,"__esModule",{value:!0}),t.OT_DOC_MESSAGE_TYPE=void 0,n(s(872),t),n(s(549),t),n(s(802),t),t.OT_DOC_MESSAGE_TYPE="ot-doc"},549:(e,t)=>{var s;Object.defineProperty(t,"__esModule",{value:!0}),t.MessageActions=void 0,function(e){e[e.fetch=0]="fetch",e[e.fetchOps=1]="fetchOps",e[e.fetchHistorySnapshot=2]="fetchHistorySnapshot",e[e.subscribe=3]="subscribe",e[e.unsubscribe=4]="unsubscribe",e[e.op=5]="op"}(s||(t.MessageActions=s={}))},802:(e,t)=>{function s(e,t,s){let r=!1;return e.forEach((function(e){e&&(e(t,s),r=!0)})),r}function r(e){return e?JSON.parse(JSON.stringify(e)):e}Object.defineProperty(t,"__esModule",{value:!0}),t.util=t.nextTick=t.MAX_SAFE_INTEGER=void 0,t.callEach=s,t.clone=r,t.MAX_SAFE_INTEGER=9007199254740991,t.nextTick=function(e){if("undefined"!=typeof process&&process.nextTick)return process.nextTick(e);setTimeout((function(){e()}))},t.util={MAX_SAFE_INTEGER:t.MAX_SAFE_INTEGER,callEach:s,nextTick:t.nextTick,clone:r}}},s={};function r(e){var n=s[e];if(void 0!==n)return n.exports;var i=s[e]={exports:{}};return t[e].call(i.exports,i,i.exports,r),i.exports}var n={};return(()=>{var e=n;Object.defineProperty(e,"__esModule",{value:!0}),e.documentFeature=e.MemoryDb=e.Db=e.DocumentServices=e.TypesManager=void 0,r(259),r(36);const t=r(89);Object.defineProperty(e,"DocumentServices",{enumerable:!0,get:function(){return t.DocumentServices}});const s=r(619);Object.defineProperty(e,"Db",{enumerable:!0,get:function(){return s.Db}});const i=r(985);Object.defineProperty(e,"MemoryDb",{enumerable:!0,get:function(){return i.MemoryDb}});const o=r(763);Object.defineProperty(e,"documentFeature",{enumerable:!0,get:function(){return o.documentFeature}});const a=r(513);Object.defineProperty(e,"TypesManager",{enumerable:!0,get:function(){return a.TypesManager}})})(),n})()));
1
+ !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("@grapecity-software/js-collaboration"));else if("function"==typeof define&&define.amd)define(["@grapecity-software/js-collaboration"],t);else{var s="object"==typeof exports?t(require("@grapecity-software/js-collaboration")):t(e["@grapecity-software/js-collaboration"]);for(var r in s)("object"==typeof exports?exports:e)[r]=s[r]}}(this,(e=>(()=>{"use strict";var t={619:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Db=void 0,t.Db=class{async getCommittedOpVersion(e,t,s){const r=await this.getOps(e,0,t);for(let e=r.length;e--;){const t=r[e];if(s.src===t.src&&s.seq===t.seq)return t.v}}}},985:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.MemoryDb=void 0;const r=s(619);class n extends r.Db{constructor(e){var t;super(),this.ops=new Map,this.documents=new Map,this.fragments=new Map,this.mockWaitTime=null!==(t=null==e?void 0:e.mockWaitTime)&&void 0!==t?t:0}async getSnapshot(e){return this.execute((async()=>{const t=this.documents.get(e);if(!t)return;const s=await this.getFragments(e);return{id:e,v:t.snapshotVersion,type:t.type,fragments:s}}))}getDocument(e){return this.execute((()=>{const t=this.documents.get(e);if(t)return JSON.parse(JSON.stringify(t))}))}getFragments(e){return this.execute((()=>{const t=this.fragments.get(e);return t?JSON.parse(JSON.stringify(Object.fromEntries(t))):void 0}))}getFragment(e,t){return this.execute((()=>{var s;const r=null===(s=this.fragments.get(e))||void 0===s?void 0:s.get(t);if(r)return JSON.parse(JSON.stringify(r))}))}getOps(e,t,s){return this.execute((()=>{const r=this.ops.get(e);if(!r)return[];s=null!=s?s:Number.MAX_SAFE_INTEGER;const n=r.filter((e=>e.v>=t&&e.v<s));return JSON.parse(JSON.stringify(n))}))}commitOp(e,t,s){return this.execute((()=>{var r;if(t=JSON.parse(JSON.stringify(t)),s=JSON.parse(JSON.stringify(s)),t.create){if(this.documents.has(e))return!1;const{version:r,snapshotVersion:n,type:i}=s;return this.documents.set(e,{id:e,version:r,snapshotVersion:n,type:i}),this.ops.set(e,[t]),this.fragments.set(e,new Map),!0}if(t.del)return!!this.documents.has(e)&&(this.documents.delete(e),this.ops.delete(e),this.fragments.delete(e),!0);{const n=null===(r=this.ops.get(e))||void 0===r?void 0:r.length;return t.v===n&&(this.ops.get(e).push(t),this.documents.get(e).version=s.version,!0)}}))}commitSnapshot(e,t){return this.execute((()=>{var s,r,n;t=JSON.parse(JSON.stringify(t));const i=this.documents.get(e);if(t.fromVersion!==i.snapshotVersion)return!1;if(t.v<=i.snapshotVersion)return!1;i.snapshotVersion=t.v;const o=t.fragmentsChanges;if(o.deleteSnapshot)this.fragments.delete(e);else{const t=this.fragments.get(e);Object.entries(null!==(s=o.createFragments)&&void 0!==s?s:{}).forEach((async([e,s])=>{t.set(e,s)})),Object.entries(null!==(r=o.updateFragments)&&void 0!==r?r:{}).forEach((async([e,s])=>{t.set(e,s)})),null===(n=o.deleteFragments)||void 0===n||n.forEach((e=>{t.delete(e)}))}return!0}))}close(){return this.execute((()=>{}))}dump(){const e={ops:this.ops,documents:this.documents,fragments:this.fragments};return JSON.parse(JSON.stringify(e,((e,t)=>t instanceof Map?Object.fromEntries(t.entries()):t)))}_wait(){return new Promise((e=>setTimeout(e,this.mockWaitTime)))}async execute(e){return await this._wait(),await e()}}t.MemoryDb=n},347:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.DocumentController=void 0;const r=s(127),n=s(961),i=s(660),o=r.LoggerManager.createLogger("OT.Document"),a=i.MessageActions,c=i.OTError.CODES,_="ot-doc-subscribed";function u(e){return{id:e.id,v:e.v,data:e.data,type:e.type}}t.DocumentController=class{constructor(e){this.service=e}async trigger(e,t,s){await this.service.trigger(e,t,s)}send(e,t){e.send(JSON.stringify(t),i.OT_DOC_MESSAGE_TYPE)}_sendOp(e,t){const s={a:a.op,v:t.v,src:t.src,seq:t.seq};"op"in t&&(s.op=t.op),t.create&&(s.create=t.create),t.del&&(s.del=!0),this.send(e,s)}_broadcastOp(e,t){const s={a:i.MessageActions.op,v:t.v,src:t.src,seq:t.seq};"op"in t&&(s.op=t.op),t.create&&(s.create=t.create),t.del&&(s.del=!0),e.broadcastWithFilter(JSON.stringify(s),i.OT_DOC_MESSAGE_TYPE,(t=>e.id!==t.id&&!!t.innerTags.get(_)))}_sendOps(e,t){if(t&&0!==t.length)for(let s=0;s<t.length;s++)this._sendOp(e,t[s])}async _reply(e,t,s){s.a=t.a;const r={connection:e,request:t,reply:s};await this.trigger("reply",r,(s=>{s?this._replyError(e,t,s):this.send(e,r.reply)}))}async _replyError(e,t,s){t.error=function(e){return e instanceof i.OTError?{code:e.code,message:e.message}:e instanceof Error?{code:c.ERR_UNKNOWN_ERROR,message:e.message}:"string"==typeof e?{code:c.ERR_UNKNOWN_ERROR,message:e}:void o.warn("unknown error",e)}(s),this.send(e,t)}async _messageHandler(e,t){const s=this;if("string"!=typeof t)return void s.close(e,new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Received non-object message"));let r;try{r=JSON.parse(t)}catch(t){return void s.close(e,new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Received non-object message"))}await this.trigger("receive",{connection:e,request:r},(async t=>{t?await s._replyError(e,r,t):await s._handleMessage(e,r,((t,n)=>{t?s._replyError(e,r,t):s._reply(e,r,n||{})}))}))}_checkRequest(e){if(e.a===a.op&&!(0,n.isNullOrUndefined)(e.v)&&("number"!=typeof e.v||e.v<0))return"Invalid version"}async _handleMessage(e,t,s){const r=this._checkRequest(t);if(r)return s(new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,r));switch(t.a){case a.fetch:return await this._fetch(e,t.v,s);case a.subscribe:return await this._subscribe(e,t.v,s);case a.unsubscribe:return this._unsubscribe(e);case a.op:return await this._submit(e,t,s);case a.fetchHistorySnapshot:return await this._fetchHistorySnapshot(e,t,s);default:return await s(new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Invalid or unknown message"))}}async _fetch(e,t,s){if(!(0,n.isNullOrUndefined)(t))return await this._fetchOps(e,t,s);await this.service._fetch(e.roomId,{connection:e},((e,t)=>e?s(e):s(null,{data:u(t)})))}async _fetchOps(e,t,s){await this.service._getOps(e.roomId,t,void 0,{connection:e},((t,r)=>t?s(t):(this._sendOps(e,r),s())))}async _subscribe(e,t,s){const r=this;await this.service._subscribe(e.roomId,t,{connection:e},((t,n)=>{if(t)return s(t);const{ops:i,snapshot:o}=n;return i&&r._sendOps(e,i),e.innerTags.set(_,!0),o?s(null,{data:u(o)}):s()}))}_unsubscribe(e){e.innerTags.delete(_)}async _submit(e,t,s){const r=function(e,t){const s=e.src||t;return e.op?function(e,t,s,r,n){return{src:e,seq:t,v:s,op:r,x:n,m:void 0}}(s,e.seq,e.v,e.op,e.x):e.create?function(e,t,s,r,n){return{src:e,seq:t,v:s,create:r,x:n,m:void 0}}(s,e.seq,e.v,e.create,e.x):e.del?function(e,t,s,r,n){return{src:e,seq:t,v:s,del:r,x:n,m:void 0}}(s,e.seq,e.v,e.del,e.x):void 0}(t,e.id);return r?r.seq>=i.util.MAX_SAFE_INTEGER?s(new i.OTError(c.ERR_CONNECTION_SEQ_INTEGER_OVERFLOW,"Connection seq has exceeded the max safe integer, maybe from being open for too long")):await this._submitOp(e,r,s):s(new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Invalid op message"))}async _submitOp(e,t,s){const r=this;await this.service._submit(e.roomId,t,{connection:e},(t=>{r._broadcastOp(e,t.op)}),((n,o)=>{const a={src:t.src,seq:t.seq,v:t.v};return n?n instanceof i.OTError&&n.code==c.ERR_OP_ALREADY_SUBMITTED?s(null,a):s(n):(r._sendOps(e,o),s(null,a))}))}async _fetchHistorySnapshot(e,t,s){if(void 0===t.v)return s(new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Invalid fetch history snapshot message"));await this.service._fetchHistorySnapshot(e.roomId,t.v,{connection:e},((e,r)=>e?s(e):s(null,{id:t.id,data:r})))}close(e,t){t&&o.warn("Connection closed due to error",e.id,t.stack||t),e.close()}}},763:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.documentFeature=function(e){const t=e||new r.DocumentServices,s=new n.DocumentController(t);return{middlewares:{message:async(e,t)=>e.type!==i.OT_DOC_MESSAGE_TYPE?await t():s._messageHandler(e.connection,e.data)}}};const r=s(89),n=s(347),i=s(660)},89:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.DocumentServices=void 0;const r=s(660),n=s(127),i=s(961),o=s(985),a=s(259),c=s(551),_=s(155),u=s(936),p=s(469),h=s(36),E=s(769),O=r.OTError.CODES;class d extends n.Middleware{constructor(e){var t,s,r;super(),e=null!=e?e:{},this.db=null!==(t=e.db)&&void 0!==t?t:new o.MemoryDb,this.milestoneDb=null!==(s=e.milestoneDb)&&void 0!==s?s:new u.MilestoneDB,this.maxSubmitRetries=e.maxSubmitRetries,this.submitSnapshotBatchSize=null!==(r=e.submitSnapshotBatchSize)&&void 0!==r?r:10}async fetch(e,t){let s;const r=await this._fetch(e,t||{},(e=>{s=e}));if(s)throw s;return r}async _fetch(e,t,s){const r=Date.now(),n=this;try{const i=await n._getSnapshot(e,t);return await n._sanitizeSnapshots([i],a.SnapshotTypes.current,t,(async o=>o?s(o):(await n._emitTiming("fetch",Date.now()-r,Object.assign(Object.assign({},t),{id:e})),s(null,i)))),i}catch(e){await s(e)}}async getOps(e,t,s,r){let n;const i=await this._getOps(e,t,s,r||{},(e=>{n=e}));if(n)throw n;return i}async _getOps(e,t,s,r,n){const i=this,o=Date.now();return await i._getSanitizedOps(e,t,s,r||{},(async(a,c)=>a?n(a):(await i._emitTiming("getOps",Date.now()-o,Object.assign(Object.assign({},r),{id:e,from:t,to:s})),n(null,c))))}async _subscribe(e,t,s,r){const n=this,o=Date.now();(0,i.isNullOrUndefined)(t)?await n._fetch(e,s,(async(i,a)=>i?r(i):(await n._emitTiming("subscribe.snapshot",Date.now()-o,Object.assign(Object.assign({},s),{id:e,version:t})),r(null,{snapshot:a})))):await n._getSanitizedOps(e,t,void 0,s,(async(i,a)=>i?r(i):(await n._emitTiming("subscribe.ops",Date.now()-o,Object.assign(Object.assign({},s),{id:e,version:t})),r(null,{ops:a}))))}async submit(e,t,s,r){let n;const i=await this._submit(e,t,s||{},r||(()=>{}),(e=>{n=e}));if(n)throw n;return i}async _submit(e,t,s,r,n){const i=_.ot.checkOp(t);if(i)return n(i),[];const o=this,a=new c.SubmitRequest(this,e,t,s),u={connection:s.connection,request:a},p=(e,t)=>(o.emit("submitRequestEnd",{request:a,err:e}),n(e,t));return await o.trigger("submit",u,(async t=>{if(t)return p(t);await a.submit(r,(async t=>{if(t)return p(t);await o._sanitizeOps(e,a.ops,u,(async e=>e?p(e):(o._emitTiming("submit.op.total",Date.now()-a.start,a),p(null,a.ops))))}))})),a.ops}async fetchHistorySnapshot(e,t,s){let r;const n=await this._fetchHistorySnapshot(e,t,s||{},(e=>{r=e}));if(r)throw r;return n}async _fetchHistorySnapshot(e,t,s,r){try{const n=Date.now(),i=await this._fetchHistorySnapshotImp(e,t),o=a.SnapshotTypes.byVersion;return await this._sanitizeSnapshots([i],o,s,(async e=>e?r(e):(await this._emitTiming("fetchSnapshot",Date.now()-n,Object.assign(Object.assign({},s),{version:t})),r(null,i)))),i}catch(e){await r(e)}}async _fetchHistorySnapshotImp(e,t){const s=this.db;if(void 0===t)return await this._getSnapshot(e);const n=await this.milestoneDb.getMilestoneSnapshot(e,t),i=n?n.v:0,o=await s.getOps(e,i,t),a=await this._buildSnapshotFromOps(e,n,o);if(t>a.v)throw new r.OTError(O.ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT,"Requested version exceeds latest snapshot version");return a}async _buildSnapshotFromOps(e,t,s){const r=null!=t?t:{id:e,v:0},n=new h.SnapshotFragmentsRequest(e,void 0,r.fragments),i=await _.ot.applyOps(r,s,n);if(i)throw i;return n.applyChangesToSnapshot(r),_.ot.composeFragments(r),r}async _sanitizeSnapshots(e,t,s,r){const n=new p.ReadSnapshotsRequest(e,t,s);await this.trigger("readSnapshots",{connection:s.connection,request:n},(e=>e?r((0,i.wrapError)(e,O.ERR_SNAPSHOT_READS_REJECTED)):n.hasSnapshotRejection()?r(n.getReadSnapshotsError()):r()))}async _getSanitizedOps(e,t,s,r,n){const i=await this.db.getOps(e,t,s,null==r?void 0:r.options);return await this._sanitizeOps(e,i,r,(e=>e?n(e):n(null,i))),i}async _sanitizeOps(e,t,s,r){for(const n of t){let t;if(await this._sanitizeOp(e,n,s,(e=>{t=e})),t)return r(t)}return r()}async _sanitizeOp(e,t,s,r){await this.trigger("readOp",{connection:s.connection,request:{id:e,op:t,context:s}},(e=>e?r((0,i.wrapError)(e)):r()))}async _getSnapshot(e,t){var s;const r=null==t?void 0:t.options,n=await this.db.getDocument(e,r);if(!n)return{id:e,v:0};if(n.snapshotVersion<n.version){const s=new E.SubmitSnapshotRequest(this,e,n,Object.assign(Object.assign({},t),{trigger:E.SubmitSnapshotTrigger.fetchSnapshot}));await s.submitSnapshot((e=>{if(e)throw e}))}const i=null!==(s=await this.db.getSnapshot(e,r))&&void 0!==s?s:{id:e,v:0};return _.ot.composeFragments(i),i}_emitTiming(e,t,s){return this.emit("timing",{action:e,cost:t,m:s})}}t.DocumentServices=d},259:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Hooks=t.SnapshotTypes=void 0,t.SnapshotTypes={current:"current",byVersion:"byVersion",byTimestamp:"byTimestamp"},t.Hooks={submitRequestEnd:"submitRequestEnd",timing:"timing"}},936:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.MilestoneDB=void 0;const r=s(660),n=s(961),i=r.OTError.CODES;t.MilestoneDB=class{constructor(e){var t;this.interval=null!==(t=null==e?void 0:e.interval)&&void 0!==t?t:1e3,this.map=new Map}saveMilestoneSnapshot(e){return new Promise((t=>{let s=this.map.get(e.id);s||(s=[],this.map.set(e.id,s)),s.push(e),s.sort(((e,t)=>e.v-t.v)),t(!0)}))}getMilestoneSnapshot(e,t){const s=function(e){return function(t,s){return!(0,n.isNullOrUndefined)(e)&&s.v>e}}(t);return this._findMilestoneSnapshot(e,s)}_findMilestoneSnapshot(e,t){return new Promise((s=>{if(!e)throw new r.OTError(i.ERR_MILESTONE_ARGUMENT_INVALID,"Missing ID");const n=this._getMilestoneSnapshots(e);let o;for(let e=0;e<n.length;e++){const s=n[e];if(t(o,s))break;o=s}s(o)}))}_getMilestoneSnapshots(e){var t;return null!==(t=this.map.get(e))&&void 0!==t?t:[]}}},155:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.ot=t.composeFragments=t.applyOps=t.transform=t.apply=t.normalizeType=t.checkOp=void 0;const r=s(513),n=s(961),i=s(660),o=i.OTError.CODES,a="default";t.checkOp=function(e){if((0,n.isNullOrUndefined)(e)||"object"!=typeof e)return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Op must be an object");if((0,n.isNullOrUndefined)(e.create)){if((0,n.isNullOrUndefined)(e.del)){if(!("op"in e))return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Missing op, create, or del")}else if(!0!==e.del)return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"del value must be true")}else{if("object"!=typeof e.create)return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Create data must be an object");const t=e.create.type;if("string"!=typeof t)return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Missing create type");const s=r.TypesManager.map[t];if((0,n.isNullOrUndefined)(s)||"object"!=typeof s)return new i.OTError(o.ERR_DOC_TYPE_NOT_RECOGNIZED,"Unknown type")}return(0,n.isNullOrUndefined)(e.src)||"string"==typeof e.src?(0,n.isNullOrUndefined)(e.seq)||"number"==typeof e.seq?(0,n.isNullOrUndefined)(e.src)&&!(0,n.isNullOrUndefined)(e.seq)||!(0,n.isNullOrUndefined)(e.src)&&(0,n.isNullOrUndefined)(e.seq)?new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Both src and seq must be set together"):(0,n.isNullOrUndefined)(e.m)||"object"==typeof e.m?void 0:new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"op.m must be an object or null"):new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"seq must be a number"):new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"src must be a string")},t.normalizeType=function(e){return r.TypesManager.map[e]&&r.TypesManager.map[e].uri},t.apply=async function(e,t,s){if("object"!=typeof e)return new i.OTError(o.ERR_APPLY_SNAPSHOT_NOT_PROVIDED,"Missing snapshot");if(!(0,n.isNullOrUndefined)(e.v)&&!(0,n.isNullOrUndefined)(t.v)&&e.v!==t.v)return new i.OTError(o.ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT,"Version mismatch");if(t.create){const n=t.create,c=r.TypesManager.map[n.type];if(!c)return new i.OTError(o.ERR_DOC_TYPE_NOT_RECOGNIZED,"Unknown type");try{let t;t=c.createFragments?c.createFragments(n.data):{[a]:c.create(n.data)},s.create(t),e.type=c.uri,e.v++}catch(e){return e}}else if(t.del)s.delete(),e.data=void 0,e.type=void 0,e.v++;else if("op"in t){const n=await async function(e,t,s){if(!e.type)return new i.OTError(o.ERR_DOC_DOES_NOT_EXIST,"Document does not exist");if(void 0===t)return new i.OTError(o.ERR_OT_OP_NOT_PROVIDED,"Missing op");const n=r.TypesManager.map[e.type];if(!n)return new i.OTError(o.ERR_DOC_TYPE_NOT_RECOGNIZED,"Unknown type");try{if(n.applyFragments)await n.applyFragments(s,t);else{const e=await s.getFragment(a),r=n.apply(e,t);s.updateFragment(a,r)}}catch(e){return new i.OTError(o.ERR_OT_OP_NOT_APPLIED,e.message)}}(e,t.op,s);if(n)return n;e.v++}else e.v++},t.transform=function(e,t,s){if(!(0,n.isNullOrUndefined)(t.v)&&t.v!==s.v)return new i.OTError(o.ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM,"Version mismatch");if(s.del){if(t.create||"op"in t)return new i.OTError(o.ERR_DOC_WAS_DELETED,"Document was deleted")}else{if(s.create&&("op"in t||t.create||t.del)||"op"in s&&t.create)return new i.OTError(o.ERR_DOC_ALREADY_CREATED,"Document was created remotely");if("op"in s&&"op"in t){if(!e)return new i.OTError(o.ERR_DOC_DOES_NOT_EXIST,"Document does not exist");if("string"==typeof e&&!(e=r.TypesManager.map[e]))return new i.OTError(o.ERR_DOC_TYPE_NOT_RECOGNIZED,"Unknown type");try{t.op=e.transform(t.op,s.op,"left")}catch(e){return new i.OTError(o.ERR_OT_OP_TRANSFORM_FAILED,"Op transform failed")}}}(0,n.isNullOrUndefined)(t.v)||t.v++},t.applyOps=async function(e,s,r){for(let n=0;n<s.length;n++){const i=s[n];e.v=i.v;const o=await(0,t.apply)(e,i,r);if(o)return o}},t.composeFragments=function(e){if(!e.type)return;const t=r.TypesManager.map[e.type];t&&e.fragments&&(t.composeFragments?e.data=t.composeFragments(e.fragments):e.data=e.fragments[a],delete e.fragments)},t.ot={checkOp:t.checkOp,normalizeType:t.normalizeType,apply:t.apply,transform:t.transform,applyOps:t.applyOps,composeFragments:t.composeFragments}},469:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.ReadSnapshotsRequest=void 0;const r=s(660),n=s(961);t.ReadSnapshotsRequest=class{constructor(e,t,s){this._idToError=null,this.snapshots=e,this.snapshotType=t,this.context=s||{}}rejectSnapshotRead(e,t){this._idToError||(this._idToError=Object.create(null)),this._idToError[e.id]=t}rejectSnapshotReadSilent(e,t){this.rejectSnapshotRead(e,this.silentRejectionError(t))}silentRejectionError(e){return new r.OTError(r.OTError.CODES.ERR_SNAPSHOT_READ_SILENT_REJECTION,e)}hasSnapshotRejection(){return!(0,n.isNullOrUndefined)(this._idToError)}getReadSnapshotsError(){const e=this.snapshots,t=this._idToError;if(0===e.length)return;if(1===e.length){return t[e[0].id]||void 0}const s=new r.OTError(r.OTError.CODES.ERR_SNAPSHOT_READS_REJECTED);return s.idToError=t,s}}},36:(e,t)=>{function s(e,t){var s,r,n;const i=null!==(s=e.fragments)&&void 0!==s?s:{};if(t.deleteSnapshot)e.fragments={};else{for(const[e,s]of Object.entries(null!==(r=t.createFragments)&&void 0!==r?r:{}))i[e]=s;for(const[e,s]of Object.entries(null!==(n=t.updateFragments)&&void 0!==n?n:{}))i[e]=s;t.deleteFragments&&t.deleteFragments.length>0&&t.deleteFragments.forEach((e=>delete i[e])),e.fragments=i}}Object.defineProperty(t,"__esModule",{value:!0}),t.SnapshotFragmentsRequest=void 0,t.applyChangesToSnapshot=s,t.SnapshotFragmentsRequest=class{constructor(e,t,s){this._fragmentsMap=new Map,this._createFragments=new Map,this._updateFragments=new Map,this._deleteFragments=new Set,this._deleteSnapshot=!1,this._docId=e,this._db=t,s&&Object.entries(s).forEach((([e,t])=>{this._fragmentsMap.set(e,t)}))}async _loadFragment(e){const t=await this._db.getFragment(this._docId,e);this._fragmentsMap.set(e,t)}async getFragment(e){return this._db&&!this._fragmentsMap.has(e)&&await this._loadFragment(e),this._fragmentsMap.get(e)}createFragment(e,t){this._deleteFragments.has(e)?(this._deleteFragments.delete(e),this._updateFragments.set(e,t)):this._createFragments.set(e,t),this._fragmentsMap.set(e,t)}updateFragment(e,t){this._deleteFragments.has(e)||(this._createFragments.has(e)?this._createFragments.set(e,t):this._updateFragments.set(e,t),this._fragmentsMap.set(e,t))}deleteFragment(e){this._deleteFragments.add(e),this._createFragments.delete(e),this._updateFragments.delete(e),this._fragmentsMap.set(e,void 0)}create(e){Object.entries(e).forEach((([e,t])=>{this.createFragment(e,t)}))}delete(){this._deleteSnapshot=!0,this._createFragments.clear(),this._updateFragments.clear(),this._deleteFragments.clear(),this._fragmentsMap.clear()}getChanges(){return{createFragments:Object.fromEntries(this._createFragments.entries()),updateFragments:Object.fromEntries(this._updateFragments.entries()),deleteFragments:Array.from(this._deleteFragments),deleteSnapshot:this._deleteSnapshot}}applyChangesToSnapshot(e){s(e,this.getChanges())}}},551:function(e,t,s){var r,n=this&&this.__createBinding||(Object.create?function(e,t,s,r){void 0===r&&(r=s);var n=Object.getOwnPropertyDescriptor(t,s);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,r,n)}:function(e,t,s,r){void 0===r&&(r=s),e[r]=t[s]}),i=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),o=this&&this.__importStar||(r=function(e){return r=Object.getOwnPropertyNames||function(e){var t=[];for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&(t[t.length]=s);return t},r(e)},function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var s=r(e),o=0;o<s.length;o++)"default"!==s[o]&&n(t,e,s[o]);return i(t,e),t});Object.defineProperty(t,"__esModule",{value:!0}),t.SubmitRequest=void 0;const a=s(961),c=s(660),_=o(s(155)),u=s(769),p=s(513),h=c.OTError.CODES,E=p.TypesManager.getType;t.SubmitRequest=class{constructor(e,t,s,r){this.doc=e,this.context=r||{},this.id=t,this.op=s,this.extra=s.x,delete s.x,this.start=Date.now(),this._addOpMeta(),this.maxRetries=e.maxSubmitRetries,this.retries=0,this.ops=[]}async submit(e,t){var s;const r=this,n=this.doc.db,i=this.id,o=this.op,c=Date.now(),_=null!==(s=await n.getDocument(i))&&void 0!==s?s:{id:i,version:0,snapshotVersion:0};r.document=_,await r.doc._emitTiming("submit.op.db.getDocumentInfo",Date.now()-c,r);const u=_.version;if((0,a.isNullOrUndefined)(o.v)){if(o.create&&_.type&&o.src){const e=await n.getCommittedOpVersion(i,u,o);return(0,a.isNullOrUndefined)(e)?t(r.alreadyCreatedError()):(o.v=e,t(r.alreadySubmittedError()))}o.v=u}if(o.v>u)return t(r.newerVersionError());if(o.v<u){const e=Date.now(),s=o.v,a=await n.getOps(i,s,u);if(r.doc._emitTiming("submit.op.db.getOpsForTransform",Date.now()-e,r),!a||a.length!==u-s)return t(r.missingOpsError());const c=Date.now(),_=r._transformOp(a);if(_)return t(_);if(await r.doc._emitTiming("submit.op.transform",Date.now()-c,r),o.v!==u)return t(r.versionAfterTransformError())}if(_.type){if(o.create)return t(r.alreadyCreatedError())}else if(!o.create)return t(r.docDoesNotExistError());r._updateDocument(),await r.commitOp(e,t)}_addOpMeta(){this.op.m={ts:this.start},this.op.create&&(this.op.create.type=_.normalizeType(this.op.create.type))}_updateDocument(){if(!this.document)return;const e=this.document,t=this.op;t.create?(e.type=_.normalizeType(t.create.type),e.snapshotVersion=0):t.del&&(e.type=void 0),e.version++,this._updateDocumentMeta()}_updateDocumentMeta(){const e=this.document.m||(this.document.m={});this.op.create&&(e.ctime=this.start),e.mtime=this.start}_transformOp(e){const t=E(this.document.type);for(let s=0;s<e.length;s++){const r=e[s];if(this.op.src&&this.op.src===r.src&&this.op.seq===r.seq)return this.alreadySubmittedError();if(this.op.v!==r.v)return this.versionDuringTransformError();const n=_.transform(t,this.op,r);if(n)return n;delete r.m,this.ops.push(r)}}async commitOp(e,t){const s=this,r=this.doc;await s._trigger("commit",(async n=>{if(n)return t(n);const i=Date.now();if(!await r.db.commitOp(s.id,s.op,s.document,s.context.options))return await s._retry(e,t);s.op.m=void 0,await r._emitTiming("submit.op.db.commit",Date.now()-i,s),e(s),await s._trigger("afterWrite",(async e=>{if(e)return t(e);const n=new u.SubmitSnapshotRequest(r,s.id,s.document,Object.assign(Object.assign({},s.context),{trigger:u.SubmitSnapshotTrigger.submitOp,op:s.op}));await n.submitSnapshot(t)}))}))}async _retry(e,t){if(this.retries++,null!=this.maxRetries&&this.retries>this.maxRetries)return t(this.maxRetriesError());await this.doc._emitTiming("submit.op.retry",Date.now()-this.start,this),await this.submit(e,t)}async _trigger(e,t){const s=Object.assign(Object.assign({},this.context),{request:this});await this.doc.trigger(e,s,t),this.context.options=s.options}alreadyCreatedError(){return new c.OTError(h.ERR_DOC_ALREADY_CREATED,"Invalid op submitted. Document already created")}docDoesNotExistError(){return new c.OTError(h.ERR_DOC_DOES_NOT_EXIST,"Document does not exist")}rejectedError(){return new c.OTError(h.ERR_OP_SUBMIT_REJECTED,"Op submit rejected")}alreadySubmittedError(){return new c.OTError(h.ERR_OP_ALREADY_SUBMITTED,"Op already submitted")}maxRetriesError(){return new c.OTError(h.ERR_MAX_SUBMIT_RETRIES_EXCEEDED,"Op submit failed. Exceeded max submit retries of "+this.maxRetries)}versionDuringTransformError(){return new c.OTError(h.ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM,"Op submit failed. Versions mismatched during op transform")}newerVersionError(){return new c.OTError(h.ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT,"Invalid op submitted. Op version newer than current snapshot")}missingOpsError(){return new c.OTError(h.ERR_SUBMIT_TRANSFORM_OPS_NOT_FOUND,"Op submit failed. DB missing ops needed to transform it up to the current snapshot version")}versionAfterTransformError(){return new c.OTError(h.ERR_OP_VERSION_MISMATCH_AFTER_TRANSFORM,"Op submit failed. Op version mismatches snapshot after op transform")}}},769:function(e,t,s){var r,n=this&&this.__createBinding||(Object.create?function(e,t,s,r){void 0===r&&(r=s);var n=Object.getOwnPropertyDescriptor(t,s);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,r,n)}:function(e,t,s,r){void 0===r&&(r=s),e[r]=t[s]}),i=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),o=this&&this.__importStar||(r=function(e){return r=Object.getOwnPropertyNames||function(e){var t=[];for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&(t[t.length]=s);return t},r(e)},function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var s=r(e),o=0;o<s.length;o++)"default"!==s[o]&&n(t,e,s[o]);return i(t,e),t});Object.defineProperty(t,"__esModule",{value:!0}),t.SubmitSnapshotRequest=t.SubmitSnapshotTrigger=void 0;const a=s(127),c=s(660),_=s(36),u=o(s(155)),p=c.OTError.CODES,h=a.LoggerManager.createLogger("OT.Document");var E;!function(e){e.submitOp="submitOp",e.fetchSnapshot="fetchSnapshot",e.manual="manual"}(E||(t.SubmitSnapshotTrigger=E={})),t.SubmitSnapshotRequest=class{constructor(e,t,s,r){this.doc=e,this.context=r||{},this.id=t,this.document=s,this.fromVersion=s.snapshotVersion,this.toVersion=s.version,this.start=Date.now(),this.saveSnapshot=null,this.saveMilestoneSnapshot=null,this.maxRetries=e.maxSubmitRetries,this.retries=0,this.snapshot=null}async submitSnapshot(e){const t=this,s=this.doc;await t._trigger("submitSnapshot",(async r=>{if(r)return e(r);if(!t._shouldSubmitSnapshot())return e();const n=Date.now(),i=await s.db.getOps(t.id,t.fromVersion,t.toVersion);if(await t.doc._emitTiming("submit.snapshot.db.getOpsForApply",Date.now()-n,t),!i||i.length!==t.toVersion-t.fromVersion)return h.error("submit.snapshot.missingApplyOps",{id:t.id,fromVersion:t.fromVersion,toVersion:t.toVersion}),e();t.ops=i,await this.apply((async t=>{if(t)return e(t);await this.commitSnapshot(e)}))}))}_addSnapshotMeta(){if(!this.snapshot)return;const e=this.snapshot;this.ops.forEach((t=>{const s=e.m||(e.m={});t.create&&(s.ctime=this.start),s.mtime=this.start}))}async apply(e){const t=this,s=this.doc.db,r=this.id,n=this.ops,i=t.document,o=Date.now();t.snapshot={id:r,fromVersion:i.snapshotVersion,v:i.snapshotVersion,type:i.type},t._addSnapshotMeta(),await t._trigger("apply",(async i=>{if(i)return e(i);const a=t.snapshot,c=new _.SnapshotFragmentsRequest(r,s),p=await u.applyOps(a,n,c);return p?(h.error("submit.snapshot.apply",p),e(p)):(a.fragmentsChanges=c.getChanges(),t.doc._emitTiming("submit.snapshot.apply",Date.now()-o,t),e())}))}async commitSnapshot(e){const t=this,s=this.doc;await t._trigger("commitSnapshot",(async r=>{if(r)return e(r);const n=Date.now();if(!await s.db.commitSnapshot(t.id,t.snapshot,t.context.options))return await this._retry(e);if(await s._emitTiming("submit.snapshot.db.commit",Date.now()-n,t),t._shouldSaveMilestoneSnapshot()){const e=await t.doc.db.getSnapshot(t.id);await t.doc.milestoneDb.saveMilestoneSnapshot(e)}return e()}))}async _retry(e){if(this.retries++,null!=this.maxRetries&&this.retries>this.maxRetries)return e(this.maxRetriesError());const t=Date.now(),s=await this.doc.db.getDocument(this.id,this.context.options);return this.doc._emitTiming("submit.snapshot.retry.db.getDocumentInfo",Date.now()-t,this),s?s.snapshotVersion!==this.fromVersion?s.snapshotVersion>=this.toVersion?e():(this.document=s,this.fromVersion=s.snapshotVersion,await this.doc._emitTiming("submit.snapshot.retry",Date.now()-this.start,this),await this.submitSnapshot(e)):(await this.doc._emitTiming("submit.snapshot.retry",Date.now()-this.start,this),await this.submitSnapshot(e)):e()}_shouldSubmitSnapshot(){var e,t;const s=this.fromVersion,r=this.toVersion,n=null===(e=this.context)||void 0===e?void 0:e.trigger,i=null===(t=this.context)||void 0===t?void 0:t.op;return!(r<=s)&&(!(n!==E.submitOp||!i||!i.create&&!i.del)||n===E.fetchSnapshot||0===s||!!this._shouldSaveMilestoneSnapshot()||(null===this.saveSnapshot?r-s>=this.doc.submitSnapshotBatchSize:this.saveSnapshot))}_shouldSaveMilestoneSnapshot(){return null===this.saveMilestoneSnapshot?this.toVersion%this.doc.milestoneDb.interval==0:this.saveMilestoneSnapshot}async _trigger(e,t){const s=Object.assign(Object.assign({},this.context),{request:this});await this.doc.trigger(e,s,t),this.context.options=s.options}maxRetriesError(){return new c.OTError(p.ERR_MAX_SUBMIT_SNAPSHOT_RETRIES_EXCEEDED,"Snapshot submit failed. Exceeded max submit retries of "+this.maxRetries)}}},513:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.TypesManager=void 0;class s{static register(e){e.uri&&(s.map[e.uri]=e),e.name&&(s.map[e.name]=e)}static getType(e){return s.map[e]}}t.TypesManager=s,s.map={}},961:(e,t,s)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.isNullOrUndefined=function(e){return null==e},t.wrapError=function(e,t){if(e instanceof r.OTError)return e;t=null!=t?t:r.OTError.CODES.ERR_UNKNOWN_ERROR;const s="string"==typeof e?e:e.message;return new r.OTError(t,s)};const r=s(660)},127:t=>{t.exports=e},872:(e,t)=>{var s;Object.defineProperty(t,"__esModule",{value:!0}),t.OTError=t.ERROR_CODES=void 0,function(e){e[e.ERR_DOC_ALREADY_CREATED=0]="ERR_DOC_ALREADY_CREATED",e[e.ERR_DOC_DOES_NOT_EXIST=1]="ERR_DOC_DOES_NOT_EXIST",e[e.ERR_DOC_TYPE_NOT_RECOGNIZED=2]="ERR_DOC_TYPE_NOT_RECOGNIZED",e[e.ERR_DOC_WAS_DELETED=3]="ERR_DOC_WAS_DELETED",e[e.ERR_MAX_SUBMIT_RETRIES_EXCEEDED=4]="ERR_MAX_SUBMIT_RETRIES_EXCEEDED",e[e.ERR_MAX_SUBMIT_SNAPSHOT_RETRIES_EXCEEDED=5]="ERR_MAX_SUBMIT_SNAPSHOT_RETRIES_EXCEEDED",e[e.ERR_MESSAGE_BADLY_FORMED=6]="ERR_MESSAGE_BADLY_FORMED",e[e.ERR_OP_ALREADY_SUBMITTED=7]="ERR_OP_ALREADY_SUBMITTED",e[e.ERR_HARD_ROLLBACK_FETCH_FAILED=8]="ERR_HARD_ROLLBACK_FETCH_FAILED",e[e.ERR_OT_OP_NOT_APPLIED=9]="ERR_OT_OP_NOT_APPLIED",e[e.ERR_OT_OP_TRANSFORM_FAILED=10]="ERR_OT_OP_TRANSFORM_FAILED",e[e.ERR_SNAPSHOT_READS_REJECTED=11]="ERR_SNAPSHOT_READS_REJECTED",e[e.ERR_SNAPSHOT_READ_SILENT_REJECTION=12]="ERR_SNAPSHOT_READ_SILENT_REJECTION",e[e.ERR_SUBMIT_TRANSFORM_OPS_NOT_FOUND=13]="ERR_SUBMIT_TRANSFORM_OPS_NOT_FOUND",e[e.ERR_CONNECTION_SEQ_INTEGER_OVERFLOW=14]="ERR_CONNECTION_SEQ_INTEGER_OVERFLOW",e[e.ERR_OT_OP_BADLY_FORMED=15]="ERR_OT_OP_BADLY_FORMED",e[e.ERR_HISTORY_SNAPSHOT_NOT_FOUND=16]="ERR_HISTORY_SNAPSHOT_NOT_FOUND",e[e.ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT=17]="ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT",e[e.ERR_DOC_MISSING_VERSION=18]="ERR_DOC_MISSING_VERSION",e[e.ERR_OP_SUBMIT_REJECTED=19]="ERR_OP_SUBMIT_REJECTED",e[e.ERR_PENDING_OP_REMOVED_BY_OP_SUBMIT_REJECTED=20]="ERR_PENDING_OP_REMOVED_BY_OP_SUBMIT_REJECTED",e[e.ERR_MILESTONE_ARGUMENT_INVALID=21]="ERR_MILESTONE_ARGUMENT_INVALID",e[e.ERR_OT_OP_NOT_PROVIDED=22]="ERR_OT_OP_NOT_PROVIDED",e[e.ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT=23]="ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT",e[e.ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM=24]="ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM",e[e.ERR_INGESTED_SNAPSHOT_HAS_NO_VERSION=25]="ERR_INGESTED_SNAPSHOT_HAS_NO_VERSION",e[e.ERR_APPLY_SNAPSHOT_NOT_PROVIDED=26]="ERR_APPLY_SNAPSHOT_NOT_PROVIDED",e[e.ERR_OP_VERSION_MISMATCH_AFTER_TRANSFORM=27]="ERR_OP_VERSION_MISMATCH_AFTER_TRANSFORM",e[e.ERR_UNKNOWN_ERROR=1e3]="ERR_UNKNOWN_ERROR"}(s||(t.ERROR_CODES=s={}));class r extends Error{constructor(e,t){super(t),this.code=e,this.name="OTError",this.code=e}}t.OTError=r,r.CODES=s},660:function(e,t,s){var r=this&&this.__createBinding||(Object.create?function(e,t,s,r){void 0===r&&(r=s);var n=Object.getOwnPropertyDescriptor(t,s);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,r,n)}:function(e,t,s,r){void 0===r&&(r=s),e[r]=t[s]}),n=this&&this.__exportStar||function(e,t){for(var s in e)"default"===s||Object.prototype.hasOwnProperty.call(t,s)||r(t,e,s)};Object.defineProperty(t,"__esModule",{value:!0}),t.OT_DOC_MESSAGE_TYPE=void 0,n(s(872),t),n(s(549),t),n(s(802),t),t.OT_DOC_MESSAGE_TYPE="ot-doc"},549:(e,t)=>{var s;Object.defineProperty(t,"__esModule",{value:!0}),t.MessageActions=void 0,function(e){e[e.fetch=0]="fetch",e[e.fetchOps=1]="fetchOps",e[e.fetchHistorySnapshot=2]="fetchHistorySnapshot",e[e.subscribe=3]="subscribe",e[e.unsubscribe=4]="unsubscribe",e[e.op=5]="op"}(s||(t.MessageActions=s={}))},802:(e,t)=>{function s(e,t,s){let r=!1;return e.forEach((function(e){e&&(e(t,s),r=!0)})),r}function r(e){return e?JSON.parse(JSON.stringify(e)):e}Object.defineProperty(t,"__esModule",{value:!0}),t.util=t.nextTick=t.MAX_SAFE_INTEGER=void 0,t.callEach=s,t.clone=r,t.MAX_SAFE_INTEGER=9007199254740991,t.nextTick=function(e){if("undefined"!=typeof process&&process.nextTick)return process.nextTick(e);setTimeout((function(){e()}))},t.util={MAX_SAFE_INTEGER:t.MAX_SAFE_INTEGER,callEach:s,nextTick:t.nextTick,clone:r}}},s={};function r(e){var n=s[e];if(void 0!==n)return n.exports;var i=s[e]={exports:{}};return t[e].call(i.exports,i,i.exports,r),i.exports}var n={};return(()=>{var e=n;Object.defineProperty(e,"__esModule",{value:!0}),e.documentFeature=e.MemoryDb=e.Db=e.DocumentServices=e.TypesManager=void 0,r(259),r(36);const t=r(89);Object.defineProperty(e,"DocumentServices",{enumerable:!0,get:function(){return t.DocumentServices}});const s=r(619);Object.defineProperty(e,"Db",{enumerable:!0,get:function(){return s.Db}});const i=r(985);Object.defineProperty(e,"MemoryDb",{enumerable:!0,get:function(){return i.MemoryDb}});const o=r(763);Object.defineProperty(e,"documentFeature",{enumerable:!0,get:function(){return o.documentFeature}});const a=r(513);Object.defineProperty(e,"TypesManager",{enumerable:!0,get:function(){return a.TypesManager}})})(),n})()));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grapecity-software/js-collaboration-ot",
3
- "version": "18.1.4",
3
+ "version": "18.2.0",
4
4
  "type": "commonjs",
5
5
  "main": "./dist/index.js",
6
6
  "exports": {
@@ -25,7 +25,7 @@
25
25
  "license": "Commercial",
26
26
  "description": "SpreadJS Collaboration plugin",
27
27
  "dependencies": {
28
- "@grapecity-software/js-collaboration": "18.1.4"
28
+ "@grapecity-software/js-collaboration": "18.2.0"
29
29
  },
30
30
  "homepage": "http://www.grapecity.com.cn/"
31
31
  }