@graffiti-garden/implementation-local 0.4.2 → 0.4.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.
@@ -1,6 +1,6 @@
1
- import{d as w,f as g,g as b}from"./chunk-KNUPPOQC.js";w();b();g();w();b();g();var ie=class{objectToUri(t){return this.locationToUri(t)}},Oe={type:"object",properties:{value:{type:"object"},channels:{type:"array",items:{type:"string"}},allowed:{type:"array",items:{type:"string"},nullable:!0},actor:{type:"string"},name:{type:"string"},source:{type:"string"},lastModified:{type:"number"},tombstone:{type:"boolean"}},required:["value","channels","actor","name","source","lastModified","tombstone"]},Re={...Oe,required:["value","channels"]};var R=class oe extends Error{constructor(n){super(n),this.name="GraffitiErrorForbidden",Object.setPrototypeOf(this,oe.prototype)}},k=class ae extends Error{constructor(n){super(n),this.name="GraffitiErrorNotFound",Object.setPrototypeOf(this,ae.prototype)}},se=class fe extends Error{constructor(n){super(n),this.name="GraffitiErrorInvalidSchema",Object.setPrototypeOf(this,fe.prototype)}},ce=class ue extends Error{constructor(n){super(n),this.name="GraffitiErrorSchemaMismatch",Object.setPrototypeOf(this,ue.prototype)}},le=class de extends Error{constructor(n){super(n),this.name="GraffitiErrorPatchTestFailed",Object.setPrototypeOf(this,de.prototype)}},j=class he extends Error{constructor(n){super(n),this.name="GraffitiErrorPatchError",Object.setPrototypeOf(this,he.prototype)}},pe=class me extends Error{constructor(n){super(n),this.name="GraffitiErrorInvalidUri",Object.setPrototypeOf(this,me.prototype)}};w();b();g();var C=class{sessionEvents=new EventTarget;constructor(){(async()=>{await Promise.resolve();for(let o of this.getLoggedInActors()){let i=new CustomEvent("login",{detail:{session:{actor:o}}});this.sessionEvents.dispatchEvent(i)}let e=new CustomEvent("initialized",{detail:{}});this.sessionEvents.dispatchEvent(e)})()}loggedInActors=[];getLoggedInActors(){if(typeof window<"u"){let n=window.localStorage.getItem("graffiti-actor");return n?n.split(",").map(decodeURIComponent):[]}else return this.loggedInActors}setLoggedInActors(n){typeof window<"u"?window.localStorage.setItem("graffiti-actor",n.map(encodeURIComponent).join(",")):this.loggedInActors=n}login=async n=>{let e=n?.actor;if(!e&&typeof window<"u"){let r=window.prompt(`This is an insecure implementation of the Graffiti API for *demo purposes only*. Do not store any sensitive information here.
1
+ import{d as w,f as g,g as b}from"./chunk-KNUPPOQC.js";w();b();g();w();b();g();var ie=class{objectToUri(t){return this.locationToUri(t)}},Ee={type:"object",properties:{value:{type:"object"},channels:{type:"array",items:{type:"string"}},allowed:{type:"array",items:{type:"string"},nullable:!0},actor:{type:"string"},name:{type:"string"},source:{type:"string"},lastModified:{type:"number"},tombstone:{type:"boolean"}},required:["value","channels","actor","name","source","lastModified","tombstone"]},Re={...Ee,required:["value","channels"]};var R=class oe extends Error{constructor(n){super(n),this.name="GraffitiErrorForbidden",Object.setPrototypeOf(this,oe.prototype)}},k=class ae extends Error{constructor(n){super(n),this.name="GraffitiErrorNotFound",Object.setPrototypeOf(this,ae.prototype)}},se=class fe extends Error{constructor(n){super(n),this.name="GraffitiErrorInvalidSchema",Object.setPrototypeOf(this,fe.prototype)}},ce=class ue extends Error{constructor(n){super(n),this.name="GraffitiErrorSchemaMismatch",Object.setPrototypeOf(this,ue.prototype)}},le=class de extends Error{constructor(n){super(n),this.name="GraffitiErrorPatchTestFailed",Object.setPrototypeOf(this,de.prototype)}},j=class he extends Error{constructor(n){super(n),this.name="GraffitiErrorPatchError",Object.setPrototypeOf(this,he.prototype)}},pe=class me extends Error{constructor(n){super(n),this.name="GraffitiErrorInvalidUri",Object.setPrototypeOf(this,me.prototype)}};w();b();g();var C=class{sessionEvents=new EventTarget;constructor(){(async()=>{await Promise.resolve();for(let o of this.getLoggedInActors()){let i=new CustomEvent("login",{detail:{session:{actor:o}}});this.sessionEvents.dispatchEvent(i)}let e=new CustomEvent("initialized",{detail:{}});this.sessionEvents.dispatchEvent(e)})()}loggedInActors=[];getLoggedInActors(){if(typeof window<"u"){let n=window.localStorage.getItem("graffiti-actor");return n?n.split(",").map(decodeURIComponent):[]}else return this.loggedInActors}setLoggedInActors(n){typeof window<"u"?window.localStorage.setItem("graffiti-actor",n.map(encodeURIComponent).join(",")):this.loggedInActors=n}login=async n=>{let e=n?.actor;if(!e&&typeof window<"u"){let r=window.prompt(`This is an insecure implementation of the Graffiti API for *demo purposes only*. Do not store any sensitive information here.
2
2
 
3
- Simply choose a username to log in.`);r&&(e=r)}let o;if(!e)o={error:new Error("No actor ID provided to login")};else{let r=this.getLoggedInActors();r.includes(e)||this.setLoggedInActors([...r,e]),o={session:{actor:e}}}let i=new CustomEvent("login",{detail:o});this.sessionEvents.dispatchEvent(i)};logout=async n=>{let e=this.getLoggedInActors(),o=e.includes(n.actor);o&&this.setLoggedInActors(e.filter(s=>s!==n.actor));let i=o?{actor:n.actor}:{actor:n.actor,error:new Error("Not logged in with that actor")},r=new CustomEvent("logout",{detail:i});this.sessionEvents.dispatchEvent(r)}};w();b();g();w();b();g();var M=t=>`${t.source}/${encodeURIComponent(t.actor)}/${encodeURIComponent(t.name)}`,H=t=>{let n=t.split("/"),e=n.pop(),o=n.pop();if(!e||!o||!n.length)throw new pe;return{name:decodeURIComponent(e),actor:decodeURIComponent(o),source:n.join("/")}};function Q(t=16){let n=new Uint8Array(t);return crypto.getRandomValues(n),btoa(String.fromCodePoint(...n)).replace(/\+/g,"-").replace(/\//g,"_").replace(/\=+$/,"")}function q(t){return typeof t=="string"?{location:H(t),uri:t}:{location:{name:t.name,actor:t.actor,source:t.source},uri:M(t)}}function ye(t,n){return t.lastModified>n.lastModified||t.lastModified===n.lastModified&&!t.tombstone&&n.tombstone}function ve(t,n,e,o){let i=e[n];if(!(!i||!i.length))try{o[n]=t(o[n],i,!0,!1).newDocument}catch(r){throw typeof r=="object"&&r&&"name"in r&&typeof r.name=="string"&&"message"in r&&typeof r.message=="string"?r.name==="TEST_OPERATION_FAILED"?new le(r.message):new j(r.name+": "+r.message):r}}function N(t,n){try{return t.compile(n)}catch(e){throw new se(e instanceof Error?e.message:void 0)}}function X(t,n,e){t.actor!==e?.actor&&(t.allowed=t.allowed&&e?[e.actor]:void 0,t.channels=t.channels.filter(o=>n.includes(o)))}function Y(t,n){return t.allowed===void 0||t.allowed===null||!!n?.actor&&(t.actor===n.actor||t.allowed.includes(n.actor))}w();b();g();var ee=function(t,n){return ee=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,o){e.__proto__=o}||function(e,o){for(var i in o)o.hasOwnProperty(i)&&(e[i]=o[i])},ee(t,n)};function Pe(t,n){ee(t,n);function e(){this.constructor=t}t.prototype=n===null?Object.create(n):(e.prototype=n.prototype,new e)}function L(t,n,e,o){function i(r){return r instanceof e?r:new e(function(s){s(r)})}return new(e||(e=Promise))(function(r,s){function f(l){try{a(o.next(l))}catch(d){s(d)}}function u(l){try{a(o.throw(l))}catch(d){s(d)}}function a(l){l.done?r(l.value):i(l.value).then(f,u)}a((o=o.apply(t,n||[])).next())})}function E(t,n){var e={label:0,sent:function(){if(r[0]&1)throw r[1];return r[1]},trys:[],ops:[]},o,i,r,s;return s={next:f(0),throw:f(1),return:f(2)},typeof Symbol=="function"&&(s[Symbol.iterator]=function(){return this}),s;function f(a){return function(l){return u([a,l])}}function u(a){if(o)throw new TypeError("Generator is already executing.");for(;e;)try{if(o=1,i&&(r=a[0]&2?i.return:a[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,a[1])).done)return r;switch(i=0,r&&(a=[a[0]&2,r.value]),a[0]){case 0:case 1:r=a;break;case 4:return e.label++,{value:a[1],done:!1};case 5:e.label++,i=a[1],a=[0];continue;case 7:a=e.ops.pop(),e.trys.pop();continue;default:if(r=e.trys,!(r=r.length>0&&r[r.length-1])&&(a[0]===6||a[0]===2)){e=0;continue}if(a[0]===3&&(!r||a[1]>r[0]&&a[1]<r[3])){e.label=a[1];break}if(a[0]===6&&e.label<r[1]){e.label=r[1],r=a;break}if(r&&e.label<r[2]){e.label=r[2],e.ops.push(a);break}r[2]&&e.ops.pop(),e.trys.pop();continue}a=n.call(t,e)}catch(l){a=[6,l],i=0}finally{o=r=0}if(a[0]&5)throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}}function I(t){var n=typeof Symbol=="function"&&Symbol.iterator,e=n&&t[n],o=0;if(e)return e.call(t);if(t&&typeof t.length=="number")return{next:function(){return t&&o>=t.length&&(t=void 0),{value:t&&t[o++],done:!t}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}function U(t){return this instanceof U?(this.v=t,this):new U(t)}function Se(t,n,e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var o=e.apply(t,n||[]),i,r=[];return i={},s("next"),s("throw"),s("return"),i[Symbol.asyncIterator]=function(){return this},i;function s(c){o[c]&&(i[c]=function(h){return new Promise(function(p,m){r.push([c,h,p,m])>1||f(c,h)})})}function f(c,h){try{u(o[c](h))}catch(p){d(r[0][3],p)}}function u(c){c.value instanceof U?Promise.resolve(c.value.v).then(a,l):d(r[0][2],c)}function a(c){f("next",c)}function l(c){f("throw",c)}function d(c,h){c(h),r.shift(),r.length&&f(r[0][0],r[0][1])}}var be=function(t){Pe(n,t);function n(e){var o=t.call(this,e)||this;return Object.defineProperty(o,"name",{value:"RepeaterOverflowError",enumerable:!1}),typeof Object.setPrototypeOf=="function"?Object.setPrototypeOf(o,o.constructor.prototype):o.__proto__=o.constructor.prototype,typeof Error.captureStackTrace=="function"&&Error.captureStackTrace(o,o.constructor),o}return n}(Error),Qe=function(){function t(n){if(n<0)throw new RangeError("Capacity may not be less than 0");this._c=n,this._q=[]}return Object.defineProperty(t.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"full",{get:function(){return this._q.length>=this._c},enumerable:!1,configurable:!0}),t.prototype.add=function(n){if(this.full)throw new Error("Buffer full");this._q.push(n)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}(),Xe=function(){function t(n){if(n<1)throw new RangeError("Capacity may not be less than 1");this._c=n,this._q=[]}return Object.defineProperty(t.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"full",{get:function(){return!1},enumerable:!1,configurable:!0}),t.prototype.add=function(n){for(;this._q.length>=this._c;)this._q.shift();this._q.push(n)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}(),Ye=function(){function t(n){if(n<1)throw new RangeError("Capacity may not be less than 1");this._c=n,this._q=[]}return Object.defineProperty(t.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"full",{get:function(){return!1},enumerable:!1,configurable:!0}),t.prototype.add=function(n){this._q.length<this._c&&this._q.push(n)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}();function te(t){t!=null&&typeof t.then=="function"&&t.then(J,J)}var Z=0,we=1,S=2,V=3,ne=4,z=1024,J=function(){};function A(t){var n=t.err,e=Promise.resolve(t.execution).then(function(o){if(n!=null)throw n;return o});return t.err=void 0,t.execution=e.then(function(){},function(){}),t.pending===void 0?e:t.pending.then(function(){return e})}function P(t,n){var e=t.state>=V;return Promise.resolve(n).then(function(o){return!e&&t.state>=ne?A(t).then(function(i){return{value:i,done:!0}}):{value:o,done:e}})}function re(t,n){var e,o;if(!(t.state>=S))if(t.state=S,t.onnext(),t.onstop(),t.err==null&&(t.err=n),t.pushes.length===0&&(typeof t.buffer>"u"||t.buffer.empty))D(t);else try{for(var i=I(t.pushes),r=i.next();!r.done;r=i.next()){var s=r.value;s.resolve()}}catch(f){e={error:f}}finally{try{r&&!r.done&&(o=i.return)&&o.call(i)}finally{if(e)throw e.error}}}function D(t){var n,e;if(!(t.state>=V)){t.state<S&&re(t),t.state=V,t.buffer=void 0;try{for(var o=I(t.nexts),i=o.next();!i.done;i=o.next()){var r=i.value,s=t.pending===void 0?A(t):t.pending.then(function(){return A(t)});r.resolve(P(t,s))}}catch(f){n={error:f}}finally{try{i&&!i.done&&(e=o.return)&&e.call(o)}finally{if(n)throw n.error}}t.pushes=[],t.nexts=[]}}function ge(t){t.state>=ne||(t.state<V&&D(t),t.state=ne)}function je(t,n){if(te(n),t.pushes.length>=z)throw new be("No more than "+z+" pending calls to push are allowed on a single repeater.");if(t.state>=S)return Promise.resolve(void 0);var e=t.pending===void 0?Promise.resolve(n):t.pending.then(function(){return n});e=e.catch(function(u){t.state<S&&(t.err=u),ge(t)});var o;if(t.nexts.length){var i=t.nexts.shift();i.resolve(P(t,e)),t.nexts.length?o=Promise.resolve(t.nexts[0].value):typeof t.buffer<"u"&&!t.buffer.full?o=Promise.resolve(void 0):o=new Promise(function(u){return t.onnext=u})}else typeof t.buffer<"u"&&!t.buffer.full?(t.buffer.add(e),o=Promise.resolve(void 0)):o=new Promise(function(u){return t.pushes.push({resolve:u,value:e})});var r=!0,s={},f=o.catch(function(u){if(r)throw u});return s.then=function(u,a){return r=!1,Promise.prototype.then.call(o,u,a)},s.catch=function(u){return r=!1,Promise.prototype.catch.call(o,u)},s.finally=o.finally.bind(o),t.pending=e.then(function(){return f}).catch(function(u){t.err=u,ge(t)}),s}function Me(t){var n=re.bind(null,t),e=new Promise(function(o){return t.onstop=o});return n.then=e.then.bind(e),n.catch=e.catch.bind(e),n.finally=e.finally.bind(e),n}function Ae(t){if(!(t.state>=we)){t.state=we;var n=je.bind(null,t),e=Me(t);t.execution=new Promise(function(o){return o(t.executor(n,e))}),t.execution.catch(function(){return re(t)})}}var F=new WeakMap,O=function(){function t(n,e){F.set(this,{executor:n,buffer:e,err:void 0,state:Z,pushes:[],nexts:[],pending:void 0,execution:void 0,onnext:J,onstop:J})}return t.prototype.next=function(n){te(n);var e=F.get(this);if(e===void 0)throw new Error("WeakMap error");if(e.nexts.length>=z)throw new be("No more than "+z+" pending calls to next are allowed on a single repeater.");if(e.state<=Z&&Ae(e),e.onnext(n),typeof e.buffer<"u"&&!e.buffer.empty){var o=P(e,e.buffer.remove());if(e.pushes.length){var i=e.pushes.shift();e.buffer.add(i.value),e.onnext=i.resolve}return o}else if(e.pushes.length){var r=e.pushes.shift();return e.onnext=r.resolve,P(e,r.value)}else if(e.state>=S)return D(e),P(e,A(e));return new Promise(function(s){return e.nexts.push({resolve:s,value:n})})},t.prototype.return=function(n){te(n);var e=F.get(this);if(e===void 0)throw new Error("WeakMap error");return D(e),e.execution=Promise.resolve(e.execution).then(function(){return n}),P(e,A(e))},t.prototype.throw=function(n){var e=F.get(this);if(e===void 0)throw new Error("WeakMap error");return e.state<=Z||e.state>=S||typeof e.buffer<"u"&&!e.buffer.empty?(D(e),e.err==null&&(e.err=n),P(e,A(e))):this.next(Promise.reject(n))},t.prototype[Symbol.asyncIterator]=function(){return this},t.race=Le,t.merge=Ie,t.zip=Be,t.latest=Te,t}();function K(t,n){var e,o,i=[],r=function(a){a!=null&&typeof a[Symbol.asyncIterator]=="function"?i.push(a[Symbol.asyncIterator]()):a!=null&&typeof a[Symbol.iterator]=="function"?i.push(a[Symbol.iterator]()):i.push(function(){return Se(this,arguments,function(){return E(this,function(c){switch(c.label){case 0:return n.yieldValues?[4,U(a)]:[3,3];case 1:return[4,c.sent()];case 2:c.sent(),c.label=3;case 3:return n.returnValues?[4,U(a)]:[3,5];case 4:return[2,c.sent()];case 5:return[2]}})})}())};try{for(var s=I(t),f=s.next();!f.done;f=s.next()){var u=f.value;r(u)}}catch(a){e={error:a}}finally{try{f&&!f.done&&(o=s.return)&&o.call(s)}finally{if(e)throw e.error}}return i}function Le(t){var n=this,e=K(t,{returnValues:!0});return new O(function(o,i){return L(n,void 0,void 0,function(){var r,s,f,u,a,l;return E(this,function(d){switch(d.label){case 0:if(!e.length)return i(),[2];s=!1,i.then(function(){r(),s=!0}),d.label=1;case 1:d.trys.push([1,,5,7]),u=void 0,a=0,l=function(){var c,h,p,m,y,x;return E(this,function(G){switch(G.label){case 0:c=a;try{for(h=(y=void 0,I(e)),p=h.next();!p.done;p=h.next())m=p.value,Promise.resolve(m.next()).then(function(v){v.done?(i(),f===void 0&&(f=v)):a===c&&(a++,r(v))},function(v){return i(v)})}catch(v){y={error:v}}finally{try{p&&!p.done&&(x=h.return)&&x.call(h)}finally{if(y)throw y.error}}return[4,new Promise(function(v){return r=v})];case 1:return u=G.sent(),u===void 0?[3,3]:[4,o(u.value)];case 2:G.sent(),G.label=3;case 3:return[2]}})},d.label=2;case 2:return s?[3,4]:[5,l()];case 3:return d.sent(),[3,2];case 4:return[2,f&&f.value];case 5:return i(),[4,Promise.race(e.map(function(c){return c.return&&c.return()}))];case 6:return d.sent(),[7];case 7:return[2]}})})})}function Ie(t){var n=this,e=K(t,{yieldValues:!0});return new O(function(o,i){return L(n,void 0,void 0,function(){var r,s,f,u=this;return E(this,function(a){switch(a.label){case 0:if(!e.length)return i(),[2];r=[],s=!1,i.then(function(){var l,d;s=!0;try{for(var c=I(r),h=c.next();!h.done;h=c.next()){var p=h.value;p()}}catch(m){l={error:m}}finally{try{h&&!h.done&&(d=c.return)&&d.call(c)}finally{if(l)throw l.error}}}),a.label=1;case 1:return a.trys.push([1,,3,4]),[4,Promise.all(e.map(function(l,d){return L(u,void 0,void 0,function(){var c,h;return E(this,function(p){switch(p.label){case 0:p.trys.push([0,,6,9]),p.label=1;case 1:return s?[3,5]:(Promise.resolve(l.next()).then(function(m){return r[d](m)},function(m){return i(m)}),[4,new Promise(function(m){r[d]=m})]);case 2:return c=p.sent(),c===void 0?[3,4]:c.done?(f=c,[2]):[4,o(c.value)];case 3:p.sent(),p.label=4;case 4:return[3,1];case 5:return[3,9];case 6:return h=l.return,h?[4,l.return()]:[3,8];case 7:h=p.sent(),p.label=8;case 8:return[7];case 9:return[2]}})})}))];case 2:return a.sent(),[2,f&&f.value];case 3:return i(),[7];case 4:return[2]}})})})}function Be(t){var n=this,e=K(t,{returnValues:!0});return new O(function(o,i){return L(n,void 0,void 0,function(){var r,s,f,u;return E(this,function(a){switch(a.label){case 0:if(!e.length)return i(),[2,[]];s=!1,i.then(function(){r(),s=!0}),a.label=1;case 1:a.trys.push([1,,6,8]),a.label=2;case 2:return s?[3,5]:(Promise.all(e.map(function(l){return l.next()})).then(function(l){return r(l)},function(l){return i(l)}),[4,new Promise(function(l){return r=l})]);case 3:return f=a.sent(),f===void 0?[2]:(u=f.map(function(l){return l.value}),f.some(function(l){return l.done})?[2,u]:[4,o(u)]);case 4:return a.sent(),[3,2];case 5:return[3,8];case 6:return i(),[4,Promise.all(e.map(function(l){return l.return&&l.return()}))];case 7:return a.sent(),[7];case 8:return[2]}})})})}function Te(t){var n=this,e=K(t,{yieldValues:!0,returnValues:!0});return new O(function(o,i){return L(n,void 0,void 0,function(){var r,s,f,u,a,l=this;return E(this,function(d){switch(d.label){case 0:if(!e.length)return i(),[2,[]];s=[],f=!1,i.then(function(){var c,h;r();try{for(var p=I(s),m=p.next();!m.done;m=p.next()){var y=m.value;y()}}catch(x){c={error:x}}finally{try{m&&!m.done&&(h=p.return)&&h.call(p)}finally{if(c)throw c.error}}f=!0}),d.label=1;case 1:return d.trys.push([1,,5,7]),Promise.all(e.map(function(c){return c.next()})).then(function(c){return r(c)},function(c){return i(c)}),[4,new Promise(function(c){return r=c})];case 2:return u=d.sent(),u===void 0?[2]:(a=u.map(function(c){return c.value}),u.every(function(c){return c.done})?[2,a]:[4,o(a.slice())]);case 3:return d.sent(),[4,Promise.all(e.map(function(c,h){return L(l,void 0,void 0,function(){var p;return E(this,function(m){switch(m.label){case 0:if(u[h].done)return[2,u[h].value];m.label=1;case 1:return f?[3,4]:(Promise.resolve(c.next()).then(function(y){return s[h](y)},function(y){return i(y)}),[4,new Promise(function(y){return s[h]=y})]);case 2:return p=m.sent(),p===void 0?[2,u[h].value]:p.done?[2,p.value]:(a[h]=p.value,[4,o(a.slice())]);case 3:return m.sent(),[3,1];case 4:return[2]}})})}))];case 4:return[2,d.sent()];case 5:return i(),[4,Promise.all(e.map(function(c){return c.return&&c.return()}))];case 6:return d.sent(),[7];case 7:return[2]}})})})}var xe=864e5,Ge="local",W=class{db_;applyPatch_;ajv_;options;get db(){return this.db_||(this.db_=(async()=>{let{default:n}=await import("./index-browser.es-G37SKL53.js"),e={name:"graffitiDb",...this.options.pouchDBOptions},o=new n(e.name,e);return await o.put({_id:"_design/indexes",views:{objectsPerChannelAndLastModified:{map:function(i){let r=i.lastModified.toString().padStart(15,"0");i.channels.forEach(function(s){let f=encodeURIComponent(s)+"/"+r;emit(f)})}.toString()},orphansPerActorAndLastModified:{map:function(i){if(i.channels.length===0){let r=i.lastModified.toString().padStart(15,"0"),s=encodeURIComponent(i.actor)+"/"+r;emit(s)}}.toString()},channelStatsPerActor:{map:function(i){i.tombstone||i.channels.forEach(function(r){let s=encodeURIComponent(i.actor)+"/"+encodeURIComponent(r);emit(s,i.lastModified)})}.toString(),reduce:"_stats"}}}).catch(i=>{if(!(i&&typeof i=="object"&&"name"in i&&i.name==="conflict"))throw i}),o})()),this.db_}get applyPatch(){return this.applyPatch_||(this.applyPatch_=(async()=>{let{applyPatch:n}=await import("./fast-json-patch-ZE7SZEYK.js");return n})()),this.applyPatch_}get ajv(){return this.ajv_||(this.ajv_=(async()=>{let{default:n}=await import("./ajv-6AI3HK2A.js");return new n({strict:!1})})()),this.ajv_}constructor(n){this.options=n??{}}async queryByLocation(n){let e=M(n)+"/";return(await(await this.db).allDocs({startkey:e,endkey:e+"\uFFFF",include_docs:!0})).rows.map(r=>r.doc).reduce((r,s)=>(s&&r.push(s),r),[])}docId(n){return M(n)+"/"+Q()}get=async(...n)=>{let[e,o,i]=n,{location:r}=q(e),f=(await this.queryByLocation(r)).filter(m=>Y(m,i));if(!f.length)throw new k;let u=f.reduce((m,y)=>ye(m,y)?m:y),{_id:a,_rev:l,_conflicts:d,_attachments:c,...h}=u;if(X(h,[],i),!N(await this.ajv,o)(h))throw new ce;return h};async deleteAtLocation(n,e=!1){let i=(await this.queryByLocation(n)).filter(d=>!d.tombstone);if(!i.length)return;let r=i.map(d=>d.lastModified).reduce((d,c)=>d>c?d:c),s=i.filter(d=>!e||d.lastModified<r),f=i.filter(d=>e&&d.lastModified===r);if(f.length){let d=f.map(h=>h._id).reduce((h,p)=>h>p?h:p),c=f.filter(h=>h._id!==d);s.push(...c)}let u=e?r:new Date().getTime(),a=await(await this.db).bulkDocs(s.map(d=>({...d,tombstone:!0,lastModified:u}))),l;for(let d of a)if("ok"in d){let{id:c}=d,h=s.find(p=>p._id===c);if(h){let{_id:p,_rev:m,_conflicts:y,_attachments:x,...G}=h;l={...G,tombstone:!0,lastModified:u};break}}return l}delete=async(...n)=>{let[e,o]=n,{location:i}=q(e);if(i.actor!==o.actor)throw new R;let r=await this.deleteAtLocation(i);if(!r)throw new k;return r};put=async(...n)=>{let[e,o]=n;if(e.actor&&e.actor!==o.actor)throw new R;if(e.source&&e.source!==(this.options.sourceName??Ge)&&!(this.options.allowOtherSources??!1))throw new R("Putting an object that does not match this source");let i=(this.options.allowSettinngLastModified??!1)&&e.lastModified||new Date().getTime(),r={value:e.value,channels:e.channels,allowed:e.allowed,name:e.name??Q(),source:e.source??this.options.sourceName??Ge,actor:o.actor,tombstone:!1,lastModified:i};await(await this.db).put({_id:this.docId(r),...r});let s=await this.deleteAtLocation(r,!0);return s||{...r,value:{},channels:[],allowed:void 0,tombstone:!0}};patch=async(...n)=>{let[e,o,i]=n,{location:r}=q(o);if(r.actor!==i.actor)throw new R;let s=await this.get(o,{},i);if(s.tombstone)throw new k("The object you are trying to patch has been deleted");let f={...s};for(let u of["value","channels","allowed"])ve(await this.applyPatch,u,e,f);if(typeof f.value!="object"||Array.isArray(f.value)||!f.value)throw new j("value is no longer an object");if(!Array.isArray(f.channels)||!f.channels.every(u=>typeof u=="string"))throw new j("channels are no longer an array of strings");if(f.allowed&&(!Array.isArray(f.allowed)||!f.allowed.every(u=>typeof u=="string")))throw new j("allowed list is not an array of strings");return f.lastModified=new Date().getTime(),await(await this.db).put({...f,_id:this.docId(f)}),await this.deleteAtLocation(f,!0),{...s,tombstone:!0,lastModified:f.lastModified}};queryLastModifiedSuffixes(n){let e="",o="\uFFFF";if(typeof n=="object"&&n.properties?.lastModified&&typeof n.properties.lastModified=="object"){let i=n.properties.lastModified,r=i.minimum,s=i.exclusiveMinimum,f;s!==void 0?(f=Math.ceil(s),f===s&&f++):r!==void 0&&(f=Math.ceil(r)),f!==void 0&&(e=f.toString().padStart(15,"0"));let u=i.maximum,a=i.exclusiveMaximum,l;a!==void 0?(l=Math.floor(a),l===a&&l--):u!==void 0&&(l=Math.floor(u)),l!==void 0&&(o=l.toString().padStart(15,"0"))}return{startKeySuffix:e,endKeySuffix:o}}discover=(...n)=>{let[e,o,i]=n,{startKeySuffix:r,endKeySuffix:s}=this.queryLastModifiedSuffixes(o);return new O(async(u,a)=>{let l=N(await this.ajv,o),d=new Set;for(let c of e){let h=encodeURIComponent(c)+"/",p=h+r,m=h+s,y=await(await this.db).query("indexes/objectsPerChannelAndLastModified",{startkey:p,endkey:m,include_docs:!0});for(let x of y.rows){let G=x.doc;if(!G)continue;let{_id:v,_rev:_e,...$}=G;d.has(v)||(d.add(v),Y(G,i)&&(X($,e,i),l($)&&await u({value:$})))}}return a(),{tombstoneRetention:this.options.tombstoneRetention??xe}})};recoverOrphans=(n,e)=>{let{startKeySuffix:o,endKeySuffix:i}=this.queryLastModifiedSuffixes(n),r=encodeURIComponent(e.actor)+"/",s=r+o,f=r+i;return new O(async(a,l)=>{let d=N(await this.ajv,n),c=await(await this.db).query("indexes/orphansPerActorAndLastModified",{startkey:s,endkey:f,include_docs:!0});for(let h of c.rows){let p=h.doc;if(!p)continue;let{_id:m,_rev:y,...x}=p;d(x)&&await a({value:x})}return l(),{tombstoneRetention:this.options.tombstoneRetention??xe}})};channelStats=n=>new O(async(o,i)=>{let r=encodeURIComponent(n.actor)+"/",s=await(await this.db).query("indexes/channelStatsPerActor",{startkey:r,endkey:r+"\uFFFF",reduce:!0,group:!0});for(let f of s.rows){let u=f.key.split("/")[1];if(typeof u!="string")continue;let{count:a,max:l}=f.value;typeof a!="number"||typeof l!="number"||await o({value:{channel:decodeURIComponent(u),count:a,lastModified:l}})}i()})};var Ee=class extends ie{locationToUri=M;uriToLocation=H;sessionManagerLocal=new C;login=this.sessionManagerLocal.login.bind(this.sessionManagerLocal);logout=this.sessionManagerLocal.logout.bind(this.sessionManagerLocal);sessionEvents=this.sessionManagerLocal.sessionEvents;put;get;patch;delete;discover;recoverOrphans;channelStats;constructor(n){super();let e=new W(n);this.put=e.put.bind(e),this.get=e.get.bind(e),this.patch=e.patch.bind(e),this.delete=e.delete.bind(e),this.discover=e.discover.bind(e),this.recoverOrphans=e.recoverOrphans.bind(e),this.channelStats=e.channelStats.bind(e)}};export{Ee as GraffitiLocal};
3
+ Simply choose a username to log in.`);r&&(e=r)}let o;if(!e)o={error:new Error("No actor ID provided to login")};else{let r=this.getLoggedInActors();r.includes(e)||this.setLoggedInActors([...r,e]),o={session:{actor:e}}}let i=new CustomEvent("login",{detail:o});this.sessionEvents.dispatchEvent(i)};logout=async n=>{let e=this.getLoggedInActors(),o=e.includes(n.actor);o&&this.setLoggedInActors(e.filter(s=>s!==n.actor));let i=o?{actor:n.actor}:{actor:n.actor,error:new Error("Not logged in with that actor")},r=new CustomEvent("logout",{detail:i});this.sessionEvents.dispatchEvent(r)}};w();b();g();w();b();g();var M=t=>`${t.source}/${encodeURIComponent(t.actor)}/${encodeURIComponent(t.name)}`,H=t=>{let n=t.split("/"),e=n.pop(),o=n.pop();if(!e||!o||!n.length)throw new pe;return{name:decodeURIComponent(e),actor:decodeURIComponent(o),source:n.join("/")}};function Q(t=16){let n=new Uint8Array(t);return crypto.getRandomValues(n),btoa(String.fromCodePoint(...n)).replace(/\+/g,"-").replace(/\//g,"_").replace(/\=+$/,"")}function q(t){return typeof t=="string"?{location:H(t),uri:t}:{location:{name:t.name,actor:t.actor,source:t.source},uri:M(t)}}function ye(t,n){return t.lastModified>n.lastModified||t.lastModified===n.lastModified&&!t.tombstone&&n.tombstone}function ve(t,n,e,o){let i=e[n];if(!(!i||!i.length))try{o[n]=t(o[n],i,!0,!1).newDocument}catch(r){throw typeof r=="object"&&r&&"name"in r&&typeof r.name=="string"&&"message"in r&&typeof r.message=="string"?r.name==="TEST_OPERATION_FAILED"?new le(r.message):new j(r.name+": "+r.message):r}}function N(t,n){try{return t.compile(n)}catch(e){throw new se(e instanceof Error?e.message:void 0)}}function X(t,n,e){t.actor!==e?.actor&&(t.allowed=t.allowed&&e?[e.actor]:void 0,t.channels=t.channels.filter(o=>n.includes(o)))}function Y(t,n){return t.allowed===void 0||t.allowed===null||!!n?.actor&&(t.actor===n.actor||t.allowed.includes(n.actor))}w();b();g();var ee=function(t,n){return ee=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,o){e.__proto__=o}||function(e,o){for(var i in o)o.hasOwnProperty(i)&&(e[i]=o[i])},ee(t,n)};function Oe(t,n){ee(t,n);function e(){this.constructor=t}t.prototype=n===null?Object.create(n):(e.prototype=n.prototype,new e)}function L(t,n,e,o){function i(r){return r instanceof e?r:new e(function(s){s(r)})}return new(e||(e=Promise))(function(r,s){function f(l){try{a(o.next(l))}catch(d){s(d)}}function u(l){try{a(o.throw(l))}catch(d){s(d)}}function a(l){l.done?r(l.value):i(l.value).then(f,u)}a((o=o.apply(t,n||[])).next())})}function P(t,n){var e={label:0,sent:function(){if(r[0]&1)throw r[1];return r[1]},trys:[],ops:[]},o,i,r,s;return s={next:f(0),throw:f(1),return:f(2)},typeof Symbol=="function"&&(s[Symbol.iterator]=function(){return this}),s;function f(a){return function(l){return u([a,l])}}function u(a){if(o)throw new TypeError("Generator is already executing.");for(;e;)try{if(o=1,i&&(r=a[0]&2?i.return:a[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,a[1])).done)return r;switch(i=0,r&&(a=[a[0]&2,r.value]),a[0]){case 0:case 1:r=a;break;case 4:return e.label++,{value:a[1],done:!1};case 5:e.label++,i=a[1],a=[0];continue;case 7:a=e.ops.pop(),e.trys.pop();continue;default:if(r=e.trys,!(r=r.length>0&&r[r.length-1])&&(a[0]===6||a[0]===2)){e=0;continue}if(a[0]===3&&(!r||a[1]>r[0]&&a[1]<r[3])){e.label=a[1];break}if(a[0]===6&&e.label<r[1]){e.label=r[1],r=a;break}if(r&&e.label<r[2]){e.label=r[2],e.ops.push(a);break}r[2]&&e.ops.pop(),e.trys.pop();continue}a=n.call(t,e)}catch(l){a=[6,l],i=0}finally{o=r=0}if(a[0]&5)throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}}function I(t){var n=typeof Symbol=="function"&&Symbol.iterator,e=n&&t[n],o=0;if(e)return e.call(t);if(t&&typeof t.length=="number")return{next:function(){return t&&o>=t.length&&(t=void 0),{value:t&&t[o++],done:!t}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}function U(t){return this instanceof U?(this.v=t,this):new U(t)}function Se(t,n,e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var o=e.apply(t,n||[]),i,r=[];return i={},s("next"),s("throw"),s("return"),i[Symbol.asyncIterator]=function(){return this},i;function s(c){o[c]&&(i[c]=function(h){return new Promise(function(p,m){r.push([c,h,p,m])>1||f(c,h)})})}function f(c,h){try{u(o[c](h))}catch(p){d(r[0][3],p)}}function u(c){c.value instanceof U?Promise.resolve(c.value.v).then(a,l):d(r[0][2],c)}function a(c){f("next",c)}function l(c){f("throw",c)}function d(c,h){c(h),r.shift(),r.length&&f(r[0][0],r[0][1])}}var be=function(t){Oe(n,t);function n(e){var o=t.call(this,e)||this;return Object.defineProperty(o,"name",{value:"RepeaterOverflowError",enumerable:!1}),typeof Object.setPrototypeOf=="function"?Object.setPrototypeOf(o,o.constructor.prototype):o.__proto__=o.constructor.prototype,typeof Error.captureStackTrace=="function"&&Error.captureStackTrace(o,o.constructor),o}return n}(Error),Qe=function(){function t(n){if(n<0)throw new RangeError("Capacity may not be less than 0");this._c=n,this._q=[]}return Object.defineProperty(t.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"full",{get:function(){return this._q.length>=this._c},enumerable:!1,configurable:!0}),t.prototype.add=function(n){if(this.full)throw new Error("Buffer full");this._q.push(n)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}(),Xe=function(){function t(n){if(n<1)throw new RangeError("Capacity may not be less than 1");this._c=n,this._q=[]}return Object.defineProperty(t.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"full",{get:function(){return!1},enumerable:!1,configurable:!0}),t.prototype.add=function(n){for(;this._q.length>=this._c;)this._q.shift();this._q.push(n)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}(),Ye=function(){function t(n){if(n<1)throw new RangeError("Capacity may not be less than 1");this._c=n,this._q=[]}return Object.defineProperty(t.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"full",{get:function(){return!1},enumerable:!1,configurable:!0}),t.prototype.add=function(n){this._q.length<this._c&&this._q.push(n)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}();function te(t){t!=null&&typeof t.then=="function"&&t.then(J,J)}var Z=0,we=1,S=2,V=3,ne=4,z=1024,J=function(){};function A(t){var n=t.err,e=Promise.resolve(t.execution).then(function(o){if(n!=null)throw n;return o});return t.err=void 0,t.execution=e.then(function(){},function(){}),t.pending===void 0?e:t.pending.then(function(){return e})}function O(t,n){var e=t.state>=V;return Promise.resolve(n).then(function(o){return!e&&t.state>=ne?A(t).then(function(i){return{value:i,done:!0}}):{value:o,done:e}})}function re(t,n){var e,o;if(!(t.state>=S))if(t.state=S,t.onnext(),t.onstop(),t.err==null&&(t.err=n),t.pushes.length===0&&(typeof t.buffer>"u"||t.buffer.empty))D(t);else try{for(var i=I(t.pushes),r=i.next();!r.done;r=i.next()){var s=r.value;s.resolve()}}catch(f){e={error:f}}finally{try{r&&!r.done&&(o=i.return)&&o.call(i)}finally{if(e)throw e.error}}}function D(t){var n,e;if(!(t.state>=V)){t.state<S&&re(t),t.state=V,t.buffer=void 0;try{for(var o=I(t.nexts),i=o.next();!i.done;i=o.next()){var r=i.value,s=t.pending===void 0?A(t):t.pending.then(function(){return A(t)});r.resolve(O(t,s))}}catch(f){n={error:f}}finally{try{i&&!i.done&&(e=o.return)&&e.call(o)}finally{if(n)throw n.error}}t.pushes=[],t.nexts=[]}}function ge(t){t.state>=ne||(t.state<V&&D(t),t.state=ne)}function je(t,n){if(te(n),t.pushes.length>=z)throw new be("No more than "+z+" pending calls to push are allowed on a single repeater.");if(t.state>=S)return Promise.resolve(void 0);var e=t.pending===void 0?Promise.resolve(n):t.pending.then(function(){return n});e=e.catch(function(u){t.state<S&&(t.err=u),ge(t)});var o;if(t.nexts.length){var i=t.nexts.shift();i.resolve(O(t,e)),t.nexts.length?o=Promise.resolve(t.nexts[0].value):typeof t.buffer<"u"&&!t.buffer.full?o=Promise.resolve(void 0):o=new Promise(function(u){return t.onnext=u})}else typeof t.buffer<"u"&&!t.buffer.full?(t.buffer.add(e),o=Promise.resolve(void 0)):o=new Promise(function(u){return t.pushes.push({resolve:u,value:e})});var r=!0,s={},f=o.catch(function(u){if(r)throw u});return s.then=function(u,a){return r=!1,Promise.prototype.then.call(o,u,a)},s.catch=function(u){return r=!1,Promise.prototype.catch.call(o,u)},s.finally=o.finally.bind(o),t.pending=e.then(function(){return f}).catch(function(u){t.err=u,ge(t)}),s}function Me(t){var n=re.bind(null,t),e=new Promise(function(o){return t.onstop=o});return n.then=e.then.bind(e),n.catch=e.catch.bind(e),n.finally=e.finally.bind(e),n}function Ae(t){if(!(t.state>=we)){t.state=we;var n=je.bind(null,t),e=Me(t);t.execution=new Promise(function(o){return o(t.executor(n,e))}),t.execution.catch(function(){return re(t)})}}var F=new WeakMap,E=function(){function t(n,e){F.set(this,{executor:n,buffer:e,err:void 0,state:Z,pushes:[],nexts:[],pending:void 0,execution:void 0,onnext:J,onstop:J})}return t.prototype.next=function(n){te(n);var e=F.get(this);if(e===void 0)throw new Error("WeakMap error");if(e.nexts.length>=z)throw new be("No more than "+z+" pending calls to next are allowed on a single repeater.");if(e.state<=Z&&Ae(e),e.onnext(n),typeof e.buffer<"u"&&!e.buffer.empty){var o=O(e,e.buffer.remove());if(e.pushes.length){var i=e.pushes.shift();e.buffer.add(i.value),e.onnext=i.resolve}return o}else if(e.pushes.length){var r=e.pushes.shift();return e.onnext=r.resolve,O(e,r.value)}else if(e.state>=S)return D(e),O(e,A(e));return new Promise(function(s){return e.nexts.push({resolve:s,value:n})})},t.prototype.return=function(n){te(n);var e=F.get(this);if(e===void 0)throw new Error("WeakMap error");return D(e),e.execution=Promise.resolve(e.execution).then(function(){return n}),O(e,A(e))},t.prototype.throw=function(n){var e=F.get(this);if(e===void 0)throw new Error("WeakMap error");return e.state<=Z||e.state>=S||typeof e.buffer<"u"&&!e.buffer.empty?(D(e),e.err==null&&(e.err=n),O(e,A(e))):this.next(Promise.reject(n))},t.prototype[Symbol.asyncIterator]=function(){return this},t.race=Le,t.merge=Ie,t.zip=Be,t.latest=Te,t}();function K(t,n){var e,o,i=[],r=function(a){a!=null&&typeof a[Symbol.asyncIterator]=="function"?i.push(a[Symbol.asyncIterator]()):a!=null&&typeof a[Symbol.iterator]=="function"?i.push(a[Symbol.iterator]()):i.push(function(){return Se(this,arguments,function(){return P(this,function(c){switch(c.label){case 0:return n.yieldValues?[4,U(a)]:[3,3];case 1:return[4,c.sent()];case 2:c.sent(),c.label=3;case 3:return n.returnValues?[4,U(a)]:[3,5];case 4:return[2,c.sent()];case 5:return[2]}})})}())};try{for(var s=I(t),f=s.next();!f.done;f=s.next()){var u=f.value;r(u)}}catch(a){e={error:a}}finally{try{f&&!f.done&&(o=s.return)&&o.call(s)}finally{if(e)throw e.error}}return i}function Le(t){var n=this,e=K(t,{returnValues:!0});return new E(function(o,i){return L(n,void 0,void 0,function(){var r,s,f,u,a,l;return P(this,function(d){switch(d.label){case 0:if(!e.length)return i(),[2];s=!1,i.then(function(){r(),s=!0}),d.label=1;case 1:d.trys.push([1,,5,7]),u=void 0,a=0,l=function(){var c,h,p,m,y,x;return P(this,function(G){switch(G.label){case 0:c=a;try{for(h=(y=void 0,I(e)),p=h.next();!p.done;p=h.next())m=p.value,Promise.resolve(m.next()).then(function(v){v.done?(i(),f===void 0&&(f=v)):a===c&&(a++,r(v))},function(v){return i(v)})}catch(v){y={error:v}}finally{try{p&&!p.done&&(x=h.return)&&x.call(h)}finally{if(y)throw y.error}}return[4,new Promise(function(v){return r=v})];case 1:return u=G.sent(),u===void 0?[3,3]:[4,o(u.value)];case 2:G.sent(),G.label=3;case 3:return[2]}})},d.label=2;case 2:return s?[3,4]:[5,l()];case 3:return d.sent(),[3,2];case 4:return[2,f&&f.value];case 5:return i(),[4,Promise.race(e.map(function(c){return c.return&&c.return()}))];case 6:return d.sent(),[7];case 7:return[2]}})})})}function Ie(t){var n=this,e=K(t,{yieldValues:!0});return new E(function(o,i){return L(n,void 0,void 0,function(){var r,s,f,u=this;return P(this,function(a){switch(a.label){case 0:if(!e.length)return i(),[2];r=[],s=!1,i.then(function(){var l,d;s=!0;try{for(var c=I(r),h=c.next();!h.done;h=c.next()){var p=h.value;p()}}catch(m){l={error:m}}finally{try{h&&!h.done&&(d=c.return)&&d.call(c)}finally{if(l)throw l.error}}}),a.label=1;case 1:return a.trys.push([1,,3,4]),[4,Promise.all(e.map(function(l,d){return L(u,void 0,void 0,function(){var c,h;return P(this,function(p){switch(p.label){case 0:p.trys.push([0,,6,9]),p.label=1;case 1:return s?[3,5]:(Promise.resolve(l.next()).then(function(m){return r[d](m)},function(m){return i(m)}),[4,new Promise(function(m){r[d]=m})]);case 2:return c=p.sent(),c===void 0?[3,4]:c.done?(f=c,[2]):[4,o(c.value)];case 3:p.sent(),p.label=4;case 4:return[3,1];case 5:return[3,9];case 6:return h=l.return,h?[4,l.return()]:[3,8];case 7:h=p.sent(),p.label=8;case 8:return[7];case 9:return[2]}})})}))];case 2:return a.sent(),[2,f&&f.value];case 3:return i(),[7];case 4:return[2]}})})})}function Be(t){var n=this,e=K(t,{returnValues:!0});return new E(function(o,i){return L(n,void 0,void 0,function(){var r,s,f,u;return P(this,function(a){switch(a.label){case 0:if(!e.length)return i(),[2,[]];s=!1,i.then(function(){r(),s=!0}),a.label=1;case 1:a.trys.push([1,,6,8]),a.label=2;case 2:return s?[3,5]:(Promise.all(e.map(function(l){return l.next()})).then(function(l){return r(l)},function(l){return i(l)}),[4,new Promise(function(l){return r=l})]);case 3:return f=a.sent(),f===void 0?[2]:(u=f.map(function(l){return l.value}),f.some(function(l){return l.done})?[2,u]:[4,o(u)]);case 4:return a.sent(),[3,2];case 5:return[3,8];case 6:return i(),[4,Promise.all(e.map(function(l){return l.return&&l.return()}))];case 7:return a.sent(),[7];case 8:return[2]}})})})}function Te(t){var n=this,e=K(t,{yieldValues:!0,returnValues:!0});return new E(function(o,i){return L(n,void 0,void 0,function(){var r,s,f,u,a,l=this;return P(this,function(d){switch(d.label){case 0:if(!e.length)return i(),[2,[]];s=[],f=!1,i.then(function(){var c,h;r();try{for(var p=I(s),m=p.next();!m.done;m=p.next()){var y=m.value;y()}}catch(x){c={error:x}}finally{try{m&&!m.done&&(h=p.return)&&h.call(p)}finally{if(c)throw c.error}}f=!0}),d.label=1;case 1:return d.trys.push([1,,5,7]),Promise.all(e.map(function(c){return c.next()})).then(function(c){return r(c)},function(c){return i(c)}),[4,new Promise(function(c){return r=c})];case 2:return u=d.sent(),u===void 0?[2]:(a=u.map(function(c){return c.value}),u.every(function(c){return c.done})?[2,a]:[4,o(a.slice())]);case 3:return d.sent(),[4,Promise.all(e.map(function(c,h){return L(l,void 0,void 0,function(){var p;return P(this,function(m){switch(m.label){case 0:if(u[h].done)return[2,u[h].value];m.label=1;case 1:return f?[3,4]:(Promise.resolve(c.next()).then(function(y){return s[h](y)},function(y){return i(y)}),[4,new Promise(function(y){return s[h]=y})]);case 2:return p=m.sent(),p===void 0?[2,u[h].value]:p.done?[2,p.value]:(a[h]=p.value,[4,o(a.slice())]);case 3:return m.sent(),[3,1];case 4:return[2]}})})}))];case 4:return[2,d.sent()];case 5:return i(),[4,Promise.all(e.map(function(c){return c.return&&c.return()}))];case 6:return d.sent(),[7];case 7:return[2]}})})})}var xe=864e5,Ge="local",W=class{db_;applyPatch_;ajv_;options;get db(){return this.db_||(this.db_=(async()=>{let{default:n}=await import("./index-browser.es-G37SKL53.js"),e={name:"graffitiDb",...this.options.pouchDBOptions},o=new n(e.name,e);return await o.put({_id:"_design/indexes",views:{objectsPerChannelAndLastModified:{map:function(i){let r=i.lastModified.toString().padStart(15,"0");i.channels.forEach(function(s){let f=encodeURIComponent(s)+"/"+r;emit(f)})}.toString()},orphansPerActorAndLastModified:{map:function(i){if(i.channels.length===0){let r=i.lastModified.toString().padStart(15,"0"),s=encodeURIComponent(i.actor)+"/"+r;emit(s)}}.toString()},channelStatsPerActor:{map:function(i){i.tombstone||i.channels.forEach(function(r){let s=encodeURIComponent(i.actor)+"/"+encodeURIComponent(r);emit(s,i.lastModified)})}.toString(),reduce:"_stats"}}}).catch(i=>{if(!(i&&typeof i=="object"&&"name"in i&&i.name==="conflict"))throw i}),o})()),this.db_}get applyPatch(){return this.applyPatch_||(this.applyPatch_=(async()=>{let{applyPatch:n}=await import("./fast-json-patch-ZE7SZEYK.js");return n})()),this.applyPatch_}get ajv(){return this.ajv_||(this.ajv_=this.options.ajv?Promise.resolve(this.options.ajv):(async()=>{let{default:n}=await import("./ajv-6AI3HK2A.js");return new n({strict:!1})})()),this.ajv_}constructor(n){this.options=n??{}}async queryByLocation(n){let e=M(n)+"/";return(await(await this.db).allDocs({startkey:e,endkey:e+"\uFFFF",include_docs:!0})).rows.map(r=>r.doc).reduce((r,s)=>(s&&r.push(s),r),[])}docId(n){return M(n)+"/"+Q()}get=async(...n)=>{let[e,o,i]=n,{location:r}=q(e),f=(await this.queryByLocation(r)).filter(m=>Y(m,i));if(!f.length)throw new k;let u=f.reduce((m,y)=>ye(m,y)?m:y),{_id:a,_rev:l,_conflicts:d,_attachments:c,...h}=u;if(X(h,[],i),!N(await this.ajv,o)(h))throw new ce;return h};async deleteAtLocation(n,e=!1){let i=(await this.queryByLocation(n)).filter(d=>!d.tombstone);if(!i.length)return;let r=i.map(d=>d.lastModified).reduce((d,c)=>d>c?d:c),s=i.filter(d=>!e||d.lastModified<r),f=i.filter(d=>e&&d.lastModified===r);if(f.length){let d=f.map(h=>h._id).reduce((h,p)=>h>p?h:p),c=f.filter(h=>h._id!==d);s.push(...c)}let u=e?r:new Date().getTime(),a=await(await this.db).bulkDocs(s.map(d=>({...d,tombstone:!0,lastModified:u}))),l;for(let d of a)if("ok"in d){let{id:c}=d,h=s.find(p=>p._id===c);if(h){let{_id:p,_rev:m,_conflicts:y,_attachments:x,...G}=h;l={...G,tombstone:!0,lastModified:u};break}}return l}delete=async(...n)=>{let[e,o]=n,{location:i}=q(e);if(i.actor!==o.actor)throw new R;let r=await this.deleteAtLocation(i);if(!r)throw new k;return r};put=async(...n)=>{let[e,o]=n;if(e.actor&&e.actor!==o.actor)throw new R;if(e.source&&e.source!==(this.options.sourceName??Ge)&&!(this.options.allowOtherSources??!1))throw new R("Putting an object that does not match this source");let i=(this.options.allowSettinngLastModified??!1)&&e.lastModified||new Date().getTime(),r={value:e.value,channels:e.channels,allowed:e.allowed,name:e.name??Q(),source:e.source??this.options.sourceName??Ge,actor:o.actor,tombstone:!1,lastModified:i};await(await this.db).put({_id:this.docId(r),...r});let s=await this.deleteAtLocation(r,!0);return s||{...r,value:{},channels:[],allowed:void 0,tombstone:!0}};patch=async(...n)=>{let[e,o,i]=n,{location:r}=q(o);if(r.actor!==i.actor)throw new R;let s=await this.get(o,{},i);if(s.tombstone)throw new k("The object you are trying to patch has been deleted");let f={...s};for(let u of["value","channels","allowed"])ve(await this.applyPatch,u,e,f);if(typeof f.value!="object"||Array.isArray(f.value)||!f.value)throw new j("value is no longer an object");if(!Array.isArray(f.channels)||!f.channels.every(u=>typeof u=="string"))throw new j("channels are no longer an array of strings");if(f.allowed&&(!Array.isArray(f.allowed)||!f.allowed.every(u=>typeof u=="string")))throw new j("allowed list is not an array of strings");return f.lastModified=new Date().getTime(),await(await this.db).put({...f,_id:this.docId(f)}),await this.deleteAtLocation(f,!0),{...s,tombstone:!0,lastModified:f.lastModified}};queryLastModifiedSuffixes(n){let e="",o="\uFFFF";if(typeof n=="object"&&n.properties?.lastModified&&typeof n.properties.lastModified=="object"){let i=n.properties.lastModified,r=i.minimum,s=i.exclusiveMinimum,f;s!==void 0?(f=Math.ceil(s),f===s&&f++):r!==void 0&&(f=Math.ceil(r)),f!==void 0&&(e=f.toString().padStart(15,"0"));let u=i.maximum,a=i.exclusiveMaximum,l;a!==void 0?(l=Math.floor(a),l===a&&l--):u!==void 0&&(l=Math.floor(u)),l!==void 0&&(o=l.toString().padStart(15,"0"))}return{startKeySuffix:e,endKeySuffix:o}}discover=(...n)=>{let[e,o,i]=n,{startKeySuffix:r,endKeySuffix:s}=this.queryLastModifiedSuffixes(o);return new E(async(u,a)=>{let l=N(await this.ajv,o),d=new Set;for(let c of e){let h=encodeURIComponent(c)+"/",p=h+r,m=h+s,y=await(await this.db).query("indexes/objectsPerChannelAndLastModified",{startkey:p,endkey:m,include_docs:!0});for(let x of y.rows){let G=x.doc;if(!G)continue;let{_id:v,_rev:_e,...$}=G;d.has(v)||(d.add(v),Y(G,i)&&(X($,e,i),l($)&&await u({value:$})))}}return a(),{tombstoneRetention:this.options.tombstoneRetention??xe}})};recoverOrphans=(n,e)=>{let{startKeySuffix:o,endKeySuffix:i}=this.queryLastModifiedSuffixes(n),r=encodeURIComponent(e.actor)+"/",s=r+o,f=r+i;return new E(async(a,l)=>{let d=N(await this.ajv,n),c=await(await this.db).query("indexes/orphansPerActorAndLastModified",{startkey:s,endkey:f,include_docs:!0});for(let h of c.rows){let p=h.doc;if(!p)continue;let{_id:m,_rev:y,...x}=p;d(x)&&await a({value:x})}return l(),{tombstoneRetention:this.options.tombstoneRetention??xe}})};channelStats=n=>new E(async(o,i)=>{let r=encodeURIComponent(n.actor)+"/",s=await(await this.db).query("indexes/channelStatsPerActor",{startkey:r,endkey:r+"\uFFFF",reduce:!0,group:!0});for(let f of s.rows){let u=f.key.split("/")[1];if(typeof u!="string")continue;let{count:a,max:l}=f.value;typeof a!="number"||typeof l!="number"||await o({value:{channel:decodeURIComponent(u),count:a,lastModified:l}})}i()})};var Pe=class extends ie{locationToUri=M;uriToLocation=H;sessionManagerLocal=new C;login=this.sessionManagerLocal.login.bind(this.sessionManagerLocal);logout=this.sessionManagerLocal.logout.bind(this.sessionManagerLocal);sessionEvents=this.sessionManagerLocal.sessionEvents;put;get;patch;delete;discover;recoverOrphans;channelStats;constructor(n){super();let e=new W(n);this.put=e.put.bind(e),this.get=e.get.bind(e),this.patch=e.patch.bind(e),this.delete=e.delete.bind(e),this.discover=e.discover.bind(e),this.recoverOrphans=e.recoverOrphans.bind(e),this.channelStats=e.channelStats.bind(e)}};export{Pe as GraffitiLocal};
4
4
  /*! Bundled license information:
5
5
 
6
6
  @repeaterjs/repeater/repeater.js: