@grapecity-software/js-collaboration-ot 18.1.2 → 18.1.3

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.
package/dist/index.d.ts CHANGED
@@ -258,8 +258,16 @@ export interface IDocMiddlewareContextBase {
258
258
  * Context for reading an operation in document middleware.
259
259
  */
260
260
  export interface IDocReadOpMiddlewareContext<T> extends IDocMiddlewareContextBase {
261
+ request: IReadOpRequest<T>;
262
+ }
263
+
264
+ /**
265
+ * Request for reading an operation in document middleware.
266
+ */
267
+ export interface IReadOpRequest<T> {
261
268
  id: string;
262
269
  op: IOp<T>;
270
+ context: IContext;
263
271
  }
264
272
 
265
273
  /**
@@ -312,8 +320,6 @@ export interface ICommitSnapshot<S = unknown> extends ISnapshot<S> {
312
320
 
313
321
  /**
314
322
  * Context object passed to "readSnapshots" middleware functions.
315
- * @param {ISnapshot<S>[]} snapshots - snapshots being read
316
- * @param {keyof Backend.prototype.SNAPSHOT_TYPES} snapshotType - the type of snapshot read being
317
323
  */
318
324
  export declare class ReadSnapshotsRequest<S> {
319
325
  /**
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- !function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("@grapecity-software/js-collaboration"));else if("function"==typeof define&&define.amd)define(["@grapecity-software/js-collaboration"],e);else{var s="object"==typeof exports?e(require("@grapecity-software/js-collaboration")):e(t["@grapecity-software/js-collaboration"]);for(var r in s)("object"==typeof exports?exports:t)[r]=s[r]}}(this,(t=>(()=>{"use strict";var e={619:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Db=void 0,e.Db=class{async getCommittedOpVersion(t,e,s){const r=await this.getOps(t,0,e);for(let t=r.length;t--;){const e=r[t];if(s.src===e.src&&s.seq===e.seq)return e.v}}}},985:(t,e,s)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.MemoryDb=void 0;const r=s(619);class n extends r.Db{constructor(t){var e;super(),this.ops=new Map,this.documents=new Map,this.fragments=new Map,this.mockWaitTime=null!==(e=null==t?void 0:t.mockWaitTime)&&void 0!==e?e:0}async getSnapshot(t){return this.execute((async()=>{const e=this.documents.get(t);if(!e)return;const s=await this.getFragments(t);return{id:t,v:e.snapshotVersion,type:e.type,fragments:s}}))}getDocument(t){return this.execute((()=>{const e=this.documents.get(t);if(e)return JSON.parse(JSON.stringify(e))}))}getFragments(t){return this.execute((()=>{const e=this.fragments.get(t);return e?JSON.parse(JSON.stringify(Object.fromEntries(e))):void 0}))}getFragment(t,e){return this.execute((()=>{var s;const r=null===(s=this.fragments.get(t))||void 0===s?void 0:s.get(e);if(r)return JSON.parse(JSON.stringify(r))}))}getOps(t,e,s){return this.execute((()=>{const r=this.ops.get(t);if(!r)return[];s=null!=s?s:Number.MAX_SAFE_INTEGER;const n=r.filter((t=>t.v>=e&&t.v<s));return JSON.parse(JSON.stringify(n))}))}commitOp(t,e,s){return this.execute((()=>{var r;if(e=JSON.parse(JSON.stringify(e)),s=JSON.parse(JSON.stringify(s)),e.create){if(this.documents.has(t))return!1;const{version:r,snapshotVersion:n,type:i}=s;return this.documents.set(t,{id:t,version:r,snapshotVersion:n,type:i}),this.ops.set(t,[e]),this.fragments.set(t,new Map),!0}if(e.del)return!!this.documents.has(t)&&(this.documents.delete(t),this.ops.delete(t),this.fragments.delete(t),!0);{const n=null===(r=this.ops.get(t))||void 0===r?void 0:r.length;return e.v===n&&(this.ops.get(t).push(e),this.documents.get(t).version=s.version,!0)}}))}commitSnapshot(t,e){return this.execute((()=>{var s,r,n;e=JSON.parse(JSON.stringify(e));const i=this.documents.get(t);if(e.fromVersion!==i.snapshotVersion)return!1;if(e.v<=i.snapshotVersion)return!1;i.snapshotVersion=e.v;const o=e.fragmentsChanges;if(o.deleteSnapshot)this.fragments.delete(t);else{const e=this.fragments.get(t);Object.entries(null!==(s=o.createFragments)&&void 0!==s?s:{}).forEach((async([t,s])=>{e.set(t,s)})),Object.entries(null!==(r=o.updateFragments)&&void 0!==r?r:{}).forEach((async([t,s])=>{e.set(t,s)})),null===(n=o.deleteFragments)||void 0===n||n.forEach((t=>{e.delete(t)}))}return!0}))}close(){return this.execute((()=>{}))}dump(){const t={ops:this.ops,documents:this.documents,fragments:this.fragments};return JSON.parse(JSON.stringify(t,((t,e)=>e instanceof Map?Object.fromEntries(e.entries()):e)))}_wait(){return new Promise((t=>setTimeout(t,this.mockWaitTime)))}async execute(t){return await this._wait(),await t()}}e.MemoryDb=n},347:(t,e,s)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.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;function _(t){return{id:t.id,v:t.v,data:t.data,type:t.type}}e.DocumentController=class{constructor(t){this.service=t}async trigger(t,e,s){await this.service.trigger(t,e,s)}send(t,e){t.send(JSON.stringify(e),i.OT_DOC_MESSAGE_TYPE)}_sendOp(t,e){const s={a:a.op,v:e.v,src:e.src,seq:e.seq};"op"in e&&(s.op=e.op),e.create&&(s.create=e.create),e.del&&(s.del=!0),this.send(t,s)}_broadcastOp(t,e){const s={a:i.MessageActions.op,v:e.v,src:e.src,seq:e.seq};"op"in e&&(s.op=e.op),e.create&&(s.create=e.create),e.del&&(s.del=!0),t.broadcast(JSON.stringify(s),i.OT_DOC_MESSAGE_TYPE)}_sendOps(t,e){if(e&&!e.length)for(let s=0;s<e.length;s++)this._sendOp(t,e[s])}async _reply(t,e,s){s.a=e.a;const r={connection:t,request:e,reply:s};await this.trigger("reply",r,(s=>{s?this._replyError(t,e,s):this.send(t,r.reply)}))}async _replyError(t,e,s){e.error=function(t){return t instanceof i.OTError?{code:t.code,message:t.message}:t instanceof Error?{code:c.ERR_UNKNOWN_ERROR,message:t.message}:"string"==typeof t?{code:c.ERR_UNKNOWN_ERROR,message:t}:void o.warn("unknown error",t)}(s),this.send(t,e)}async _messageHandler(t,e){const s=this;if("string"!=typeof e)return void s.close(t,new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Received non-object message"));let r;try{r=JSON.parse(e)}catch(e){return void s.close(t,new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Received non-object message"))}await this.trigger("receive",{connection:t,request:r},(async e=>{e?await s._replyError(t,r,e):await s._handleMessage(t,r,((e,n)=>{e?s._replyError(t,r,e):s._reply(t,r,n||{})}))}))}_checkRequest(t){if(t.a===a.op&&!(0,n.isNullOrUndefined)(t.v)&&("number"!=typeof t.v||t.v<0))return"Invalid version"}async _handleMessage(t,e,s){const r=this._checkRequest(e);if(r)return s(new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,r));switch(e.a){case a.fetch:return await this._fetch(t,e.v,s);case a.subscribe:return await this._subscribe(t,e.v,s);case a.unsubscribe:return this._unsubscribe();case a.op:return await this._submit(t,e,s);case a.fetchHistorySnapshot:return await this._fetchHistorySnapshot(t,e,s);default:return await s(new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Invalid or unknown message"))}}async _fetch(t,e,s){if(!(0,n.isNullOrUndefined)(e))return await this._fetchOps(t,e,s);await this.service._fetch(t.roomId,{connection:t},((t,e)=>t?s(t):s(null,{data:_(e)})))}async _fetchOps(t,e,s){await this.service._getOps(t.roomId,e,void 0,{connection:t},((e,r)=>e?s(e):(this._sendOps(t,r),s())))}async _subscribe(t,e,s){const r=this;await this.service._subscribe(t.roomId,e,{connection:t},((e,n)=>{if(e)return s(e);const{ops:i,snapshot:o}=n;return i&&r._sendOps(t,i),o?s(null,{data:_(o)}):s()}))}_unsubscribe(){}async _submit(t,e,s){const r=function(t,e){const s=t.src||e;return t.op?function(t,e,s,r,n){return{src:t,seq:e,v:s,op:r,x:n,m:void 0}}(s,t.seq,t.v,t.op,t.x):t.create?function(t,e,s,r,n){return{src:t,seq:e,v:s,create:r,x:n,m:void 0}}(s,t.seq,t.v,t.create,t.x):t.del?function(t,e,s,r,n){return{src:t,seq:e,v:s,del:r,x:n,m:void 0}}(s,t.seq,t.v,t.del,t.x):void 0}(e,t.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(t,r,s):s(new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Invalid op message"))}async _submitOp(t,e,s){const r=this;await this.service._submit(t.roomId,e,{connection:t},(e=>{r._broadcastOp(t,e.op)}),((n,o)=>{const a={src:e.src,seq:e.seq,v:e.v};return n?n instanceof i.OTError&&n.code==c.ERR_OP_ALREADY_SUBMITTED?s(null,a):s(n):(r._sendOps(t,o),s(null,a))}))}async _fetchHistorySnapshot(t,e,s){if(void 0===e.v)return s(new i.OTError(c.ERR_MESSAGE_BADLY_FORMED,"Invalid fetch history snapshot message"));await this.service._fetchHistorySnapshot(t.roomId,e.v,{connection:t},((t,r)=>t?s(t):s(null,{id:e.id,data:r})))}close(t,e){e&&o.warn("Connection closed due to error",t.id,e.stack||e),t.close()}}},763:(t,e,s)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.documentFeature=function(t){const e=t||new r.DocumentServices,s=new n.DocumentController(e);return{middlewares:{message:async(t,e)=>t.type!==i.OT_DOC_MESSAGE_TYPE?await e():s._messageHandler(t.connection,t.data)}}};const r=s(89),n=s(347),i=s(660)},89:(t,e,s)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.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),O=s(769),d=r.OTError.CODES;class E extends n.Middleware{constructor(t){var e,s,r;super(),t=null!=t?t:{},this.db=null!==(e=t.db)&&void 0!==e?e:new o.MemoryDb,this.milestoneDb=null!==(s=t.milestoneDb)&&void 0!==s?s:new u.MilestoneDB,this.maxSubmitRetries=t.maxSubmitRetries,this.submitSnapshotBatchSize=null!==(r=t.submitSnapshotBatchSize)&&void 0!==r?r:10}async fetch(t,e){let s;const r=await this._fetch(t,e||{},(t=>{s=t}));if(s)throw s;return r}async _fetch(t,e,s){const r=Date.now(),n=this;try{const i=await n._getSnapshot(t,e);return await n._sanitizeSnapshots([i],a.SnapshotTypes.current,e,(async o=>o?s(o):(await n._emitTiming("fetch",Date.now()-r,Object.assign(Object.assign({},e),{id:t})),s(null,i)))),i}catch(t){await s(t)}}async getOps(t,e,s,r){let n;const i=await this._getOps(t,e,s,r||{},(t=>{n=t}));if(n)throw n;return i}async _getOps(t,e,s,r,n){const i=this,o=Date.now();return await i._getSanitizedOps(t,e,s,r||{},(async(a,c)=>a?n(a):(await i._emitTiming("getOps",Date.now()-o,Object.assign(Object.assign({},r),{id:t,from:e,to:s})),n(null,c))))}async _subscribe(t,e,s,r){const n=this,o=Date.now();(0,i.isNullOrUndefined)(e)?await n._fetch(t,s,(async(i,a)=>i?r(i):(await n._emitTiming("subscribe.snapshot",Date.now()-o,Object.assign(Object.assign({},s),{id:t,version:e})),r(null,{snapshot:a})))):await n._getSanitizedOps(t,e,void 0,s,(async(i,a)=>i?r(i):(await n._emitTiming("subscribe.ops",Date.now()-o,Object.assign(Object.assign({},s),{id:t,version:e})),r(null,{ops:a}))))}async submit(t,e,s,r){let n;const i=await this._submit(t,e,s||{},r||(()=>{}),(t=>{n=t}));if(n)throw n;return i}async _submit(t,e,s,r,n){const i=this,o=new c.SubmitRequest(this,t,e,s),a=Object.assign(Object.assign({},s),{request:o}),u=(t,e)=>(i.emit("submitRequestEnd",{request:o,err:t}),n(t,e)),p=_.ot.checkOp(e);return p?(u(p),[]):(await i.trigger("submit",a,(async e=>{if(e)return u(e);await o.submit(r,(async e=>{if(e)return u(e);await i._sanitizeOps(t,o.ops,a,(async t=>t?u(t):(i._emitTiming("submit.op.total",Date.now()-o.start,o),u(null,o.ops))))}))})),o.ops)}async fetchHistorySnapshotByTimestamp(t,e,s){const r=Date.now(),n=await this._fetchHistorySnapshotByTimestamp(t,e),i=[n],o=a.SnapshotTypes.byTimestamp;return await this._sanitizeSnapshots(i,o,s||{},(()=>{})),this._emitTiming("fetchSnapshot",Date.now()-r,Object.assign({},s)),n}async _fetchHistorySnapshotByTimestamp(t,e){const s=this.db,r=this.milestoneDb;let n,i=0;if(void 0===e)return await this._getSnapshot(t);let o=await r.getMilestoneSnapshotAtOrBeforeTime(t,e);const a=o;o&&(i=o.v),o=await r.getMilestoneSnapshotAtOrAfterTime(t,e),o&&(n=o.v);const c=await s.getOps(t,i,n,{metadata:!0});return function(t,e){for(let s=0;s<t.length;s++){const r=t[s];if((r.m&&r.m.ts)>e)return void(t.length=s)}}(c,e),o=await this._buildSnapshotFromOps(t,a,c),o}async fetchHistorySnapshot(t,e,s){let r;const n=await this._fetchHistorySnapshot(t,e,s||{},(t=>{r=t}));if(r)throw r;return n}async _fetchHistorySnapshot(t,e,s,r){try{const n=Date.now(),i=await this._fetchHistorySnapshotImp(t,e),o=a.SnapshotTypes.byVersion;return await this._sanitizeSnapshots([i],o,s,(async t=>t?r(t):(await this._emitTiming("fetchSnapshot",Date.now()-n,Object.assign(Object.assign({},s),{version:e})),r(null,i)))),i}catch(t){await r(t)}}async _fetchHistorySnapshotImp(t,e){const s=this.db;if(void 0===e)return await this._getSnapshot(t);const n=await this.milestoneDb.getMilestoneSnapshot(t,e),i=n?n.v:0,o=await s.getOps(t,i,e),a=await this._buildSnapshotFromOps(t,n,o);if(e>a.v)throw new r.OTError(d.ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT,"Requested version exceeds latest snapshot version");return a}async _buildSnapshotFromOps(t,e,s){const r=null!=e?e:{id:t,v:0},n=new h.SnapshotFragmentsRequest(t,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(t,e,s,r){const n=new p.ReadSnapshotsRequest(t,e);await this.trigger("readSnapshots",Object.assign(Object.assign({},s),{request:n}),(t=>t?r(t):n.hasSnapshotRejection()?r(n.getReadSnapshotsError()):r()))}async _getSanitizedOps(t,e,s,r,n){const i=await this.db.getOps(t,e,s,null==r?void 0:r.options);return await this._sanitizeOps(t,i,r,(t=>t?n(t):n(null,i))),i}async _sanitizeOps(t,e,s,r){for(const n of e){let e;if(await this._sanitizeOp(t,n,s,(t=>{e=t})),e)return r(e)}return r()}async _sanitizeOp(t,e,s,r){await this.trigger("readOp",Object.assign(Object.assign({},s),{id:t,op:e}),r)}async _getSnapshot(t,e){var s;const r=null==e?void 0:e.options,n=await this.db.getDocument(t,r);if(!n)return{id:t,v:0};if(n.snapshotVersion<n.version){const s=new O.SubmitSnapshotRequest(this,t,n,Object.assign(Object.assign({},e),{trigger:O.SubmitSnapshotTrigger.fetchSnapshot}));await s.submitSnapshot((t=>{if(t)throw t}))}const i=null!==(s=await this.db.getSnapshot(t,r))&&void 0!==s?s:{id:t,v:0};return _.ot.composeFragments(i),i}_emitTiming(t,e,s){return this.emit("timing",{action:t,cost:e,m:s})}}e.DocumentServices=E},259:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Hooks=e.SnapshotTypes=void 0,e.SnapshotTypes={current:"current",byVersion:"byVersion",byTimestamp:"byTimestamp"},e.Hooks={submitRequestEnd:"submitRequestEnd",timing:"timing"}},936:(t,e,s)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.MilestoneDB=void 0;const r=s(660),n=s(961),i=r.OTError.CODES;e.MilestoneDB=class{constructor(t){var e;this.interval=null!==(e=null==t?void 0:t.interval)&&void 0!==e?e:1e3,this.map=new Map}saveMilestoneSnapshot(t){return new Promise((e=>{let s=this.map.get(t.id);s||(s=[],this.map.set(t.id,s)),s.push(t),s.sort(((t,e)=>t.v-e.v)),e(!0)}))}getMilestoneSnapshot(t,e){const s=function(t){return function(e,s){return!(0,n.isNullOrUndefined)(t)&&s.v>t}}(e);return this._findMilestoneSnapshot(t,s)}getMilestoneSnapshotAtOrBeforeTime(t,e){const s=function(t){return function(e,s){if((0,n.isNullOrUndefined)(t))return!!e;const r=s&&s.m&&s.m.mtime;return void 0!==r&&r>t}}(e);return this._findMilestoneSnapshot(t,s)}getMilestoneSnapshotAtOrAfterTime(t,e){const s=function(t){return function(e){if((0,n.isNullOrUndefined)(t))return!1;const s=e&&e.m&&e.m.mtime;return void 0!==s&&s>=t}}(e);return this._findMilestoneSnapshot(t,s)}_findMilestoneSnapshot(t,e){return new Promise((s=>{if(!t)throw new r.OTError(i.ERR_MILESTONE_ARGUMENT_INVALID,"Missing ID");const n=this._getMilestoneSnapshots(t);let o;for(let t=0;t<n.length;t++){const s=n[t];if(e(o,s))break;o=s}s(o)}))}_getMilestoneSnapshots(t){var e;return null!==(e=this.map.get(t))&&void 0!==e?e:[]}}},155:(t,e,s)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.ot=e.composeFragments=e.applyOps=e.transform=e.apply=e.normalizeType=e.checkOp=void 0;const r=s(513),n=s(961),i=s(660),o=i.OTError.CODES,a="default";e.checkOp=function(t){if((0,n.isNullOrUndefined)(t)||"object"!=typeof t)return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Op must be an object");if((0,n.isNullOrUndefined)(t.create)){if((0,n.isNullOrUndefined)(t.del)){if(!("op"in t))return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Missing op, create, or del")}else if(!0!==t.del)return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"del value must be true")}else{if("object"!=typeof t.create)return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Create data must be an object");const e=t.create.type;if("string"!=typeof e)return new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Missing create type");const s=r.TypesManager.map[e];if((0,n.isNullOrUndefined)(s)||"object"!=typeof s)return new i.OTError(o.ERR_DOC_TYPE_NOT_RECOGNIZED,"Unknown type")}return(0,n.isNullOrUndefined)(t.src)||"string"==typeof t.src?(0,n.isNullOrUndefined)(t.seq)||"number"==typeof t.seq?(0,n.isNullOrUndefined)(t.src)&&!(0,n.isNullOrUndefined)(t.seq)||!(0,n.isNullOrUndefined)(t.src)&&(0,n.isNullOrUndefined)(t.seq)?new i.OTError(o.ERR_OT_OP_BADLY_FORMED,"Both src and seq must be set together"):(0,n.isNullOrUndefined)(t.m)||"object"==typeof t.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")},e.normalizeType=function(t){return r.TypesManager.map[t]&&r.TypesManager.map[t].uri},e.apply=async function(t,e,s){if("object"!=typeof t)return new i.OTError(o.ERR_APPLY_SNAPSHOT_NOT_PROVIDED,"Missing snapshot");if(!(0,n.isNullOrUndefined)(t.v)&&!(0,n.isNullOrUndefined)(e.v)&&t.v!==e.v)return new i.OTError(o.ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT,"Version mismatch");if(e.create){const n=e.create,c=r.TypesManager.map[n.type];if(!c)return new i.OTError(o.ERR_DOC_TYPE_NOT_RECOGNIZED,"Unknown type");try{let e;e=c.createFragments?c.createFragments(n.data):{[a]:c.create(n.data)},s.create(e),t.type=c.uri,t.v++}catch(t){return t}}else if(e.del)s.delete(),t.data=void 0,t.type=void 0,t.v++;else if("op"in e){const n=await async function(t,e,s){if(!t.type)return new i.OTError(o.ERR_DOC_DOES_NOT_EXIST,"Document does not exist");if(void 0===e)return new i.OTError(o.ERR_OT_OP_NOT_PROVIDED,"Missing op");const n=r.TypesManager.map[t.type];if(!n)return new i.OTError(o.ERR_DOC_TYPE_NOT_RECOGNIZED,"Unknown type");try{if(n.applyFragments)await n.applyFragments(s,e);else{const t=await s.getFragment(a),r=n.apply(t,e);s.updateFragment(a,r)}}catch(t){return new i.OTError(o.ERR_OT_OP_NOT_APPLIED,t.message)}}(t,e.op,s);if(n)return n;t.v++}else t.v++},e.transform=function(t,e,s){if(!(0,n.isNullOrUndefined)(e.v)&&e.v!==s.v)return new i.OTError(o.ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM,"Version mismatch");if(s.del){if(e.create||"op"in e)return new i.OTError(o.ERR_DOC_WAS_DELETED,"Document was deleted")}else{if(s.create&&("op"in e||e.create||e.del)||"op"in s&&e.create)return new i.OTError(o.ERR_DOC_ALREADY_CREATED,"Document was created remotely");if("op"in s&&"op"in e){if(!t)return new i.OTError(o.ERR_DOC_DOES_NOT_EXIST,"Document does not exist");if("string"==typeof t&&!(t=r.TypesManager.map[t]))return new i.OTError(o.ERR_DOC_TYPE_NOT_RECOGNIZED,"Unknown type");try{e.op=t.transform(e.op,s.op,"left")}catch(t){return new i.OTError(o.ERR_OT_OP_TRANSFORM_FAILED,"Op transform failed")}}}(0,n.isNullOrUndefined)(e.v)||e.v++},e.applyOps=async function(t,s,r){for(let n=0;n<s.length;n++){const i=s[n];t.v=i.v;const o=await(0,e.apply)(t,i,r);if(o)return o}},e.composeFragments=function(t){if(!t.type)return;const e=r.TypesManager.map[t.type];e&&t.fragments&&(e.composeFragments?t.data=e.composeFragments(t.fragments):t.data=t.fragments[a])},e.ot={checkOp:e.checkOp,normalizeType:e.normalizeType,apply:e.apply,transform:e.transform,applyOps:e.applyOps,composeFragments:e.composeFragments}},469:(t,e,s)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.ReadSnapshotsRequest=void 0;const r=s(660),n=s(961);e.ReadSnapshotsRequest=class{constructor(t,e){this._idToError=null,this.snapshots=t,this.snapshotType=e}rejectSnapshotRead(t,e){this._idToError||(this._idToError=Object.create(null)),this._idToError[t.id]=e}rejectSnapshotReadSilent(t,e){this.rejectSnapshotRead(t,this.silentRejectionError(e))}silentRejectionError(t){return new r.OTError(r.OTError.CODES.ERR_SNAPSHOT_READ_SILENT_REJECTION,t)}hasSnapshotRejection(){return!(0,n.isNullOrUndefined)(this._idToError)}getReadSnapshotsError(){const t=this.snapshots,e=this._idToError;if(0===t.length)return;if(1===t.length){return e[t[0].id]||void 0}const s=new r.OTError(r.OTError.CODES.ERR_SNAPSHOT_READS_REJECTED);return s.idToError=e,s}}},36:(t,e)=>{function s(t,e){var s,r,n;const i=null!==(s=t.fragments)&&void 0!==s?s:{};if(e.deleteSnapshot)t.fragments={};else{for(const[t,s]of Object.entries(null!==(r=e.createFragments)&&void 0!==r?r:{}))i[t]=s;for(const[t,s]of Object.entries(null!==(n=e.updateFragments)&&void 0!==n?n:{}))i[t]=s;e.deleteFragments&&e.deleteFragments.length>0&&e.deleteFragments.forEach((t=>delete i[t])),t.fragments=i}}Object.defineProperty(e,"__esModule",{value:!0}),e.SnapshotFragmentsRequest=void 0,e.applyChangesToSnapshot=s,e.SnapshotFragmentsRequest=class{constructor(t,e,s){this._fragmentsMap=new Map,this._createFragments=new Map,this._updateFragments=new Map,this._deleteFragments=new Set,this._deleteSnapshot=!1,this._docId=t,this._db=e,s&&Object.entries(s).forEach((([t,e])=>{this._fragmentsMap.set(t,e)}))}async _loadFragment(t){const e=await this._db.getFragment(this._docId,t);this._fragmentsMap.set(t,e)}async getFragment(t){return this._db&&!this._fragmentsMap.has(t)&&await this._loadFragment(t),this._fragmentsMap.get(t)}createFragment(t,e){this._deleteFragments.has(t)?(this._deleteFragments.delete(t),this._updateFragments.set(t,e)):this._createFragments.set(t,e),this._fragmentsMap.set(t,e)}updateFragment(t,e){this._deleteFragments.has(t)||(this._createFragments.has(t)?this._createFragments.set(t,e):this._updateFragments.set(t,e),this._fragmentsMap.set(t,e))}deleteFragment(t){this._deleteFragments.add(t),this._createFragments.delete(t),this._updateFragments.delete(t),this._fragmentsMap.set(t,void 0)}create(t){Object.entries(t).forEach((([t,e])=>{this.createFragment(t,e)}))}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(t){s(t,this.getChanges())}}},551:function(t,e,s){var r=this&&this.__createBinding||(Object.create?function(t,e,s,r){void 0===r&&(r=s);var n=Object.getOwnPropertyDescriptor(e,s);n&&!("get"in n?!e.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return e[s]}}),Object.defineProperty(t,r,n)}:function(t,e,s,r){void 0===r&&(r=s),t[r]=e[s]}),n=this&&this.__setModuleDefault||(Object.create?function(t,e){Object.defineProperty(t,"default",{enumerable:!0,value:e})}:function(t,e){t.default=e}),i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var s in t)"default"!==s&&Object.prototype.hasOwnProperty.call(t,s)&&r(e,t,s);return n(e,t),e};Object.defineProperty(e,"__esModule",{value:!0}),e.SubmitRequest=void 0;const o=s(961),a=s(660),c=i(s(155)),_=s(769),u=s(513),p=a.OTError.CODES,h=u.TypesManager.getType;e.SubmitRequest=class{constructor(t,e,s,r){this.doc=t,this.context=r||{},this.id=e,this.op=s,this.extra=s.x,delete s.x,this.start=Date.now(),this._addOpMeta(),this.maxRetries=t.maxSubmitRetries,this.retries=0,this.ops=[]}async submit(t,e){var s;const r=this,n=this.doc.db,i=this.id,a=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,o.isNullOrUndefined)(a.v)){if(a.create&&_.type&&a.src){const t=await n.getCommittedOpVersion(i,u,a);return(0,o.isNullOrUndefined)(t)?e(r.alreadyCreatedError()):(a.v=t,e(r.alreadySubmittedError()))}a.v=u}if(a.v>u)return e(r.newerVersionError());if(a.v<u){const t=Date.now(),s=a.v,o=await n.getOps(i,s,u);if(r.doc._emitTiming("submit.op.db.getOpsForTransform",Date.now()-t,r),!o||o.length!==u-s)return e(r.missingOpsError());const c=Date.now(),_=r._transformOp(o);if(_)return e(_);if(await r.doc._emitTiming("submit.op.transform",Date.now()-c,r),a.v!==u)return e(r.versionAfterTransformError())}if(a.create&&_.type)return e(r.alreadyCreatedError());r._updateDocument(),await r.commitOp(t,e)}_addOpMeta(){this.op.m={ts:this.start},this.op.create&&(this.op.create.type=c.normalizeType(this.op.create.type))}_updateDocument(){if(!this.document)return;const t=this.document,e=this.op;e.create?(t.type=c.normalizeType(e.create.type),t.snapshotVersion=0):e.del&&(t.type=void 0),t.version++,this._updateDocumentMeta()}_updateDocumentMeta(){const t=this.document.m||(this.document.m={});this.op.create&&(t.ctime=this.start),t.mtime=this.start}_transformOp(t){const e=h(this.document.type);for(let s=0;s<t.length;s++){const r=t[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=c.transform(e,this.op,r);if(n)return n;delete r.m,this.ops.push(r)}}async commitOp(t,e){const s=this,r=this.doc;await s._trigger("commit",(async n=>{if(n)return e(n);const i=Date.now();if(!await r.db.commitOp(s.id,s.op,s.document,s.options))return await s._retry(t,e);s.op.m=void 0,await r._emitTiming("submit.op.db.commit",Date.now()-i,s),t(s),await s._trigger("afterWrite",(async t=>{if(t)return e(t);const n=new _.SubmitSnapshotRequest(r,s.id,s.document,Object.assign(Object.assign({},s.context),{trigger:_.SubmitSnapshotTrigger.submitOp,op:s.op}));await n.submitSnapshot(e)}))}))}async _retry(t,e){if(this.retries++,null!=this.maxRetries&&this.retries>this.maxRetries)return e(this.maxRetriesError());await this.doc._emitTiming("submit.op.retry",Date.now()-this.start,this),await this.submit(t,e)}async _trigger(t,e){const s=Object.assign(Object.assign({},this.context),{request:this});await this.doc.trigger(t,s,e),this.context.options=s.options}alreadyCreatedError(){return new a.OTError(p.ERR_DOC_ALREADY_CREATED,"Invalid op submitted. Document already created")}rejectedError(){return new a.OTError(p.ERR_OP_SUBMIT_REJECTED,"Op submit rejected")}alreadySubmittedError(){return new a.OTError(p.ERR_OP_ALREADY_SUBMITTED,"Op already submitted")}maxRetriesError(){return new a.OTError(p.ERR_MAX_SUBMIT_RETRIES_EXCEEDED,"Op submit failed. Exceeded max submit retries of "+this.maxRetries)}versionDuringTransformError(){return new a.OTError(p.ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM,"Op submit failed. Versions mismatched during op transform")}newerVersionError(){return new a.OTError(p.ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT,"Invalid op submitted. Op version newer than current snapshot")}missingOpsError(){return new a.OTError(p.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 a.OTError(p.ERR_OP_VERSION_MISMATCH_AFTER_TRANSFORM,"Op submit failed. Op version mismatches snapshot after op transform")}}},769:function(t,e,s){var r=this&&this.__createBinding||(Object.create?function(t,e,s,r){void 0===r&&(r=s);var n=Object.getOwnPropertyDescriptor(e,s);n&&!("get"in n?!e.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return e[s]}}),Object.defineProperty(t,r,n)}:function(t,e,s,r){void 0===r&&(r=s),t[r]=e[s]}),n=this&&this.__setModuleDefault||(Object.create?function(t,e){Object.defineProperty(t,"default",{enumerable:!0,value:e})}:function(t,e){t.default=e}),i=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var s in t)"default"!==s&&Object.prototype.hasOwnProperty.call(t,s)&&r(e,t,s);return n(e,t),e};Object.defineProperty(e,"__esModule",{value:!0}),e.SubmitSnapshotRequest=e.SubmitSnapshotTrigger=void 0;const o=s(127),a=s(660),c=s(36),_=i(s(155)),u=a.OTError.CODES,p=o.LoggerManager.createLogger("OT.Document");var h;!function(t){t.submitOp="submitOp",t.fetchSnapshot="fetchSnapshot",t.manual="manual"}(h||(e.SubmitSnapshotTrigger=h={})),e.SubmitSnapshotRequest=class{constructor(t,e,s,r){this.doc=t,this.context=r||{},this.id=e,this.document=s,this.fromVersion=s.snapshotVersion,this.toVersion=s.version,this.start=Date.now(),this.saveSnapshot=null,this.saveMilestoneSnapshot=null,this.maxRetries=t.maxSubmitRetries,this.retries=0,this.snapshot=null}async submitSnapshot(t){const e=this,s=this.doc;await e._trigger("submitSnapshot",(async r=>{if(r)return t(r);if(!e._shouldSubmitSnapshot())return t();const n=Date.now(),i=await s.db.getOps(e.id,e.fromVersion,e.toVersion);if(await e.doc._emitTiming("submit.snapshot.db.getOpsForApply",Date.now()-n,e),!i||i.length!==e.toVersion-e.fromVersion)return p.error("submit.snapshot.missingApplyOps",{id:e.id,fromVersion:e.fromVersion,toVersion:e.toVersion}),t();e.ops=i,await this.apply((async e=>{if(e)return t(e);await this.commitSnapshot(t)}))}))}_addSnapshotMeta(){if(!this.snapshot)return;const t=this.snapshot;this.ops.forEach((e=>{const s=t.m||(t.m={});e.create&&(s.ctime=this.start),s.mtime=this.start}))}async apply(t){const e=this,s=this.doc.db,r=this.id,n=this.ops,i=e.document,o=Date.now();e.snapshot={id:r,fromVersion:i.snapshotVersion,v:i.snapshotVersion,type:i.type},e._addSnapshotMeta(),await e._trigger("apply",(async i=>{if(i)return t(i);const a=e.snapshot,u=new c.SnapshotFragmentsRequest(r,s),h=await _.applyOps(a,n,u);return h?(p.error("submit.snapshot.apply",h),t(h)):(a.fragmentsChanges=u.getChanges(),e.doc._emitTiming("submit.snapshot.apply",Date.now()-o,e),t())}))}async commitSnapshot(t){const e=this,s=this.doc;await e._trigger("commitSnapshot",(async r=>{if(r)return t(r);const n=Date.now();if(!await s.db.commitSnapshot(e.id,e.snapshot,e.context.options))return await this._retry(t);if(await s._emitTiming("submit.snapshot.db.commit",Date.now()-n,e),e._shouldSaveMilestoneSnapshot(e.snapshot)){const t=await e.doc.db.getSnapshot(e.id);(0,c.applyChangesToSnapshot)(t,e.snapshot.fragmentsChanges),await e.doc.milestoneDb.saveMilestoneSnapshot(t)}return t()}))}async _retry(t){if(this.retries++,null!=this.maxRetries&&this.retries>this.maxRetries)return t(this.maxRetriesError());const e=Date.now(),s=await this.doc.db.getDocument(this.id,this.context.options);return this.doc._emitTiming("submit.snapshot.retry.db.getDocumentInfo",Date.now()-e,this),s?s.snapshotVersion!==this.fromVersion?s.snapshotVersion>=this.toVersion?t():(this.document=s,this.fromVersion=s.snapshotVersion,await this.doc._emitTiming("submit.snapshot.retry",Date.now()-this.start,this),await this.submitSnapshot(t)):(await this.doc._emitTiming("submit.snapshot.retry",Date.now()-this.start,this),await this.submitSnapshot(t)):t()}_shouldSubmitSnapshot(){var t,e;const s=this.fromVersion,r=this.toVersion,n=null===(t=this.context)||void 0===t?void 0:t.trigger,i=null===(e=this.context)||void 0===e?void 0:e.op;return!(n!==h.submitOp||!i||!i.create&&!i.del)||n===h.fetchSnapshot||0===s||!(r<=s)&&(null===this.saveSnapshot?r-s>=this.doc.submitSnapshotBatchSize:this.saveSnapshot)}_shouldSaveMilestoneSnapshot(t){return null===this.saveMilestoneSnapshot?t&&t.v%this.doc.milestoneDb.interval==0:this.saveMilestoneSnapshot}async _trigger(t,e){const s=Object.assign(Object.assign({},this.context),{request:this});await this.doc.trigger(t,s,e),this.context.options=s.options}maxRetriesError(){return new a.OTError(u.ERR_MAX_SUBMIT_SNAPSHOT_RETRIES_EXCEEDED,"Snapshot submit failed. Exceeded max submit retries of "+this.maxRetries)}}},513:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.TypesManager=void 0;const s={uri:"default",create:()=>{throw new Error("Function not implemented.")},apply:()=>{throw new Error("Function not implemented.")},transform:()=>{throw new Error("Function not implemented.")}};class r{static get defaultType(){return s}static register(t){t.uri&&(r.map[t.uri]=t),t.name&&(r.map[t.name]=t)}static getType(t){return r.map[t]}}e.TypesManager=r,r.map={},r.register(s)},961:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.isNullOrUndefined=function(t){return null==t}},127:e=>{e.exports=t},872:(t,e)=>{var s;Object.defineProperty(e,"__esModule",{value:!0}),e.OTError=e.ERROR_CODES=void 0,function(t){t[t.ERR_DOC_ALREADY_CREATED=0]="ERR_DOC_ALREADY_CREATED",t[t.ERR_DOC_DOES_NOT_EXIST=1]="ERR_DOC_DOES_NOT_EXIST",t[t.ERR_DOC_TYPE_NOT_RECOGNIZED=2]="ERR_DOC_TYPE_NOT_RECOGNIZED",t[t.ERR_DOC_WAS_DELETED=3]="ERR_DOC_WAS_DELETED",t[t.ERR_MAX_SUBMIT_RETRIES_EXCEEDED=4]="ERR_MAX_SUBMIT_RETRIES_EXCEEDED",t[t.ERR_MAX_SUBMIT_SNAPSHOT_RETRIES_EXCEEDED=5]="ERR_MAX_SUBMIT_SNAPSHOT_RETRIES_EXCEEDED",t[t.ERR_MESSAGE_BADLY_FORMED=6]="ERR_MESSAGE_BADLY_FORMED",t[t.ERR_OP_ALREADY_SUBMITTED=7]="ERR_OP_ALREADY_SUBMITTED",t[t.ERR_HARD_ROLLBACK_FETCH_FAILED=8]="ERR_HARD_ROLLBACK_FETCH_FAILED",t[t.ERR_OT_OP_NOT_APPLIED=9]="ERR_OT_OP_NOT_APPLIED",t[t.ERR_OT_OP_TRANSFORM_FAILED=10]="ERR_OT_OP_TRANSFORM_FAILED",t[t.ERR_SNAPSHOT_READS_REJECTED=11]="ERR_SNAPSHOT_READS_REJECTED",t[t.ERR_SUBMIT_TRANSFORM_OPS_NOT_FOUND=12]="ERR_SUBMIT_TRANSFORM_OPS_NOT_FOUND",t[t.ERR_UNKNOWN_ERROR=13]="ERR_UNKNOWN_ERROR",t[t.ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT=14]="ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT",t[t.ERR_APPLY_SNAPSHOT_NOT_PROVIDED=15]="ERR_APPLY_SNAPSHOT_NOT_PROVIDED",t[t.ERR_CONNECTION_SEQ_INTEGER_OVERFLOW=16]="ERR_CONNECTION_SEQ_INTEGER_OVERFLOW",t[t.ERR_DOC_MISSING_VERSION=17]="ERR_DOC_MISSING_VERSION",t[t.ERR_INGESTED_SNAPSHOT_HAS_NO_VERSION=18]="ERR_INGESTED_SNAPSHOT_HAS_NO_VERSION",t[t.ERR_MILESTONE_ARGUMENT_INVALID=19]="ERR_MILESTONE_ARGUMENT_INVALID",t[t.ERR_OP_SUBMIT_REJECTED=20]="ERR_OP_SUBMIT_REJECTED",t[t.ERR_PENDING_OP_REMOVED_BY_OP_SUBMIT_REJECTED=21]="ERR_PENDING_OP_REMOVED_BY_OP_SUBMIT_REJECTED",t[t.ERR_OP_VERSION_MISMATCH_AFTER_TRANSFORM=22]="ERR_OP_VERSION_MISMATCH_AFTER_TRANSFORM",t[t.ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM=23]="ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM",t[t.ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT=24]="ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT",t[t.ERR_OT_OP_BADLY_FORMED=25]="ERR_OT_OP_BADLY_FORMED",t[t.ERR_OT_OP_NOT_PROVIDED=26]="ERR_OT_OP_NOT_PROVIDED",t[t.ERR_SNAPSHOT_READ_SILENT_REJECTION=27]="ERR_SNAPSHOT_READ_SILENT_REJECTION",t[t.ERR_INFLIGHT_OP_MISSING=28]="ERR_INFLIGHT_OP_MISSING",t[t.ERR_HISTORY_SNAPSHOT_NOT_FOUND=29]="ERR_HISTORY_SNAPSHOT_NOT_FOUND"}(s||(e.ERROR_CODES=s={}));class r extends Error{constructor(t,e){super(e),this.code=t,this.name="OTError",this.code=t}}e.OTError=r,r.CODES=s},660:function(t,e,s){var r=this&&this.__createBinding||(Object.create?function(t,e,s,r){void 0===r&&(r=s);var n=Object.getOwnPropertyDescriptor(e,s);n&&!("get"in n?!e.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return e[s]}}),Object.defineProperty(t,r,n)}:function(t,e,s,r){void 0===r&&(r=s),t[r]=e[s]}),n=this&&this.__exportStar||function(t,e){for(var s in t)"default"===s||Object.prototype.hasOwnProperty.call(e,s)||r(e,t,s)};Object.defineProperty(e,"__esModule",{value:!0}),e.OT_DOC_MESSAGE_TYPE=void 0,n(s(872),e),n(s(549),e),n(s(802),e),e.OT_DOC_MESSAGE_TYPE="ot-doc"},549:(t,e)=>{var s;Object.defineProperty(e,"__esModule",{value:!0}),e.MessageActions=void 0,function(t){t[t.fetch=0]="fetch",t[t.fetchOps=1]="fetchOps",t[t.fetchHistorySnapshot=2]="fetchHistorySnapshot",t[t.subscribe=3]="subscribe",t[t.unsubscribe=4]="unsubscribe",t[t.op=5]="op"}(s||(e.MessageActions=s={}))},802:(t,e)=>{function s(t,e,s){let r=!1;return t.forEach((function(t){t&&(t(e,s),r=!0)})),r}function r(t){return t?JSON.parse(JSON.stringify(t)):t}Object.defineProperty(e,"__esModule",{value:!0}),e.util=e.nextTick=e.MAX_SAFE_INTEGER=void 0,e.callEach=s,e.clone=r,e.MAX_SAFE_INTEGER=9007199254740991,e.nextTick=function(t){if("undefined"!=typeof process&&process.nextTick)return process.nextTick(t);setTimeout((function(){t()}))},e.util={MAX_SAFE_INTEGER:e.MAX_SAFE_INTEGER,callEach:s,nextTick:e.nextTick,clone:r}}},s={};function r(t){var n=s[t];if(void 0!==n)return n.exports;var i=s[t]={exports:{}};return e[t].call(i.exports,i,i.exports,r),i.exports}var n={};return(()=>{var t=n;Object.defineProperty(t,"__esModule",{value:!0}),t.documentFeature=t.MemoryDb=t.Db=t.DocumentServices=t.TypesManager=void 0,r(259),r(36);const e=r(89);Object.defineProperty(t,"DocumentServices",{enumerable:!0,get:function(){return e.DocumentServices}});const s=r(619);Object.defineProperty(t,"Db",{enumerable:!0,get:function(){return s.Db}});const i=r(985);Object.defineProperty(t,"MemoryDb",{enumerable:!0,get:function(){return i.MemoryDb}});const o=r(763);Object.defineProperty(t,"documentFeature",{enumerable:!0,get:function(){return o.documentFeature}});const a=r(513);Object.defineProperty(t,"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&&!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})()));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grapecity-software/js-collaboration-ot",
3
- "version": "18.1.2",
3
+ "version": "18.1.3",
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.2"
28
+ "@grapecity-software/js-collaboration": "18.1.3"
29
29
  },
30
30
  "homepage": "http://www.grapecity.com.cn/"
31
31
  }