@graffiti-garden/implementation-local 0.4.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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)}},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.
1
+ import{d as v,f as w,g}from"./chunk-KNUPPOQC.js";v();g();w();v();g();w();var rt=class{},vt={type:"object",properties:{value:{type:"object"},channels:{type:"array",items:{type:"string"}},allowed:{type:"array",items:{type:"string"},nullable:!0},url:{type:"string"},actor:{type:"string"},lastModified:{type:"number"}},additionalProperties:!1,required:["value","channels","actor","url","lastModified"]},Mt={...vt,required:["value","channels"]};var S=class nt extends Error{constructor(e){super(e),this.name="GraffitiErrorForbidden",Object.setPrototypeOf(this,nt.prototype)}},O=class it extends Error{constructor(e){super(e),this.name="GraffitiErrorNotFound",Object.setPrototypeOf(this,it.prototype)}},ot=class at extends Error{constructor(e){super(e),this.name="GraffitiErrorInvalidSchema",Object.setPrototypeOf(this,at.prototype)}},st=class ft extends Error{constructor(e){super(e),this.name="GraffitiErrorSchemaMismatch",Object.setPrototypeOf(this,ft.prototype)}},ct=class ut extends Error{constructor(e){super(e),this.name="GraffitiErrorPatchTestFailed",Object.setPrototypeOf(this,ut.prototype)}},L=class lt extends Error{constructor(e){super(e),this.name="GraffitiErrorPatchError",Object.setPrototypeOf(this,lt.prototype)}};v();g();w();var q=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 t=new CustomEvent("initialized",{detail:{}});this.sessionEvents.dispatchEvent(t)})()}loggedInActors=[];getLoggedInActors(){if(typeof window<"u"){let e=window.localStorage.getItem("graffiti-actor");return e?e.split(",").map(decodeURIComponent):[]}else return this.loggedInActors}setLoggedInActors(e){typeof window<"u"?window.localStorage.setItem("graffiti-actor",e.map(encodeURIComponent).join(",")):this.loggedInActors=e}login=async e=>{let t=e?.actor;if(!t&&typeof window<"u"){let n=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 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};
3
+ Simply choose a username to log in.`);n&&(t=n)}let o;if(!t)o={error:new Error("No actor ID provided to login")};else{let n=this.getLoggedInActors();n.includes(t)||this.setLoggedInActors([...n,t]),o={session:{actor:t}}}let i=new CustomEvent("login",{detail:o});this.sessionEvents.dispatchEvent(i)};logout=async e=>{let t=this.getLoggedInActors(),o=t.includes(e.actor);o&&this.setLoggedInActors(t.filter(a=>a!==e.actor));let i=o?{actor:e.actor}:{actor:e.actor,error:new Error("Not logged in with that actor")},n=new CustomEvent("logout",{detail:i});this.sessionEvents.dispatchEvent(n)}};v();g();w();v();g();w();function dt(r){return typeof r=="string"?r:r.url}function H(r=24){let e=new Uint8Array(r);return crypto.getRandomValues(e),btoa(String.fromCodePoint(...e)).replace(/\+/g,"-").replace(/\//g,"_").replace(/\=+$/,"")}function ht(r,e,t,o){let i=t[e];if(!(!i||!i.length))try{o[e]=r(o[e],i,!0,!1).newDocument}catch(n){throw typeof n=="object"&&n&&"name"in n&&typeof n.name=="string"&&"message"in n&&typeof n.message=="string"?n.name==="TEST_OPERATION_FAILED"?new ct(n.message):new L(n.name+": "+n.message):n}}function N(r,e){try{return r.compile(e)}catch(t){throw new ot(t instanceof Error?t.message:void 0)}}function Q(r,e,t){r.actor!==t?.actor&&(r.allowed=r.allowed&&t?[t.actor]:void 0,r.channels=r.channels.filter(o=>e.includes(o)))}function W(r,e){return r.allowed===void 0||r.allowed===null||!!e?.actor&&(r.actor===e.actor||r.allowed.includes(e.actor))}v();g();w();var $=function(r,e){return $=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,o){t.__proto__=o}||function(t,o){for(var i in o)o.hasOwnProperty(i)&&(t[i]=o[i])},$(r,e)};function wt(r,e){$(r,e);function t(){this.constructor=r}r.prototype=e===null?Object.create(e):(t.prototype=e.prototype,new t)}function T(r,e,t,o){function i(n){return n instanceof t?n:new t(function(a){a(n)})}return new(t||(t=Promise))(function(n,a){function c(l){try{f(o.next(l))}catch(d){a(d)}}function u(l){try{f(o.throw(l))}catch(d){a(d)}}function f(l){l.done?n(l.value):i(l.value).then(c,u)}f((o=o.apply(r,e||[])).next())})}function j(r,e){var t={label:0,sent:function(){if(n[0]&1)throw n[1];return n[1]},trys:[],ops:[]},o,i,n,a;return a={next:c(0),throw:c(1),return:c(2)},typeof Symbol=="function"&&(a[Symbol.iterator]=function(){return this}),a;function c(f){return function(l){return u([f,l])}}function u(f){if(o)throw new TypeError("Generator is already executing.");for(;t;)try{if(o=1,i&&(n=f[0]&2?i.return:f[0]?i.throw||((n=i.return)&&n.call(i),0):i.next)&&!(n=n.call(i,f[1])).done)return n;switch(i=0,n&&(f=[f[0]&2,n.value]),f[0]){case 0:case 1:n=f;break;case 4:return t.label++,{value:f[1],done:!1};case 5:t.label++,i=f[1],f=[0];continue;case 7:f=t.ops.pop(),t.trys.pop();continue;default:if(n=t.trys,!(n=n.length>0&&n[n.length-1])&&(f[0]===6||f[0]===2)){t=0;continue}if(f[0]===3&&(!n||f[1]>n[0]&&f[1]<n[3])){t.label=f[1];break}if(f[0]===6&&t.label<n[1]){t.label=n[1],n=f;break}if(n&&t.label<n[2]){t.label=n[2],t.ops.push(f);break}n[2]&&t.ops.pop(),t.trys.pop();continue}f=e.call(r,t)}catch(l){f=[6,l],i=0}finally{o=n=0}if(f[0]&5)throw f[1];return{value:f[0]?f[1]:void 0,done:!0}}}function k(r){var e=typeof Symbol=="function"&&Symbol.iterator,t=e&&r[e],o=0;if(t)return t.call(r);if(r&&typeof r.length=="number")return{next:function(){return r&&o>=r.length&&(r=void 0),{value:r&&r[o++],done:!r}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function _(r){return this instanceof _?(this.v=r,this):new _(r)}function gt(r,e,t){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var o=t.apply(r,e||[]),i,n=[];return i={},a("next"),a("throw"),a("return"),i[Symbol.asyncIterator]=function(){return this},i;function a(s){o[s]&&(i[s]=function(p){return new Promise(function(h,m){n.push([s,p,h,m])>1||c(s,p)})})}function c(s,p){try{u(o[s](p))}catch(h){d(n[0][3],h)}}function u(s){s.value instanceof _?Promise.resolve(s.value.v).then(f,l):d(n[0][2],s)}function f(s){c("next",s)}function l(s){c("throw",s)}function d(s,p){s(p),n.shift(),n.length&&c(n[0][0],n[0][1])}}var yt=function(r){wt(e,r);function e(t){var o=r.call(this,t)||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 e}(Error),zt=function(){function r(e){if(e<0)throw new RangeError("Capacity may not be less than 0");this._c=e,this._q=[]}return Object.defineProperty(r.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"full",{get:function(){return this._q.length>=this._c},enumerable:!1,configurable:!0}),r.prototype.add=function(e){if(this.full)throw new Error("Buffer full");this._q.push(e)},r.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},r}(),Vt=function(){function r(e){if(e<1)throw new RangeError("Capacity may not be less than 1");this._c=e,this._q=[]}return Object.defineProperty(r.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"full",{get:function(){return!1},enumerable:!1,configurable:!0}),r.prototype.add=function(e){for(;this._q.length>=this._c;)this._q.shift();this._q.push(e)},r.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},r}(),Kt=function(){function r(e){if(e<1)throw new RangeError("Capacity may not be less than 1");this._c=e,this._q=[]}return Object.defineProperty(r.prototype,"empty",{get:function(){return this._q.length===0},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"full",{get:function(){return!1},enumerable:!1,configurable:!0}),r.prototype.add=function(e){this._q.length<this._c&&this._q.push(e)},r.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},r}();function Z(r){r!=null&&typeof r.then=="function"&&r.then(V,V)}var X=0,pt=1,M=2,J=3,tt=4,z=1024,V=function(){};function I(r){var e=r.err,t=Promise.resolve(r.execution).then(function(o){if(e!=null)throw e;return o});return r.err=void 0,r.execution=t.then(function(){},function(){}),r.pending===void 0?t:r.pending.then(function(){return t})}function A(r,e){var t=r.state>=J;return Promise.resolve(e).then(function(o){return!t&&r.state>=tt?I(r).then(function(i){return{value:i,done:!0}}):{value:o,done:t}})}function et(r,e){var t,o;if(!(r.state>=M))if(r.state=M,r.onnext(),r.onstop(),r.err==null&&(r.err=e),r.pushes.length===0&&(typeof r.buffer>"u"||r.buffer.empty))R(r);else try{for(var i=k(r.pushes),n=i.next();!n.done;n=i.next()){var a=n.value;a.resolve()}}catch(c){t={error:c}}finally{try{n&&!n.done&&(o=i.return)&&o.call(i)}finally{if(t)throw t.error}}}function R(r){var e,t;if(!(r.state>=J)){r.state<M&&et(r),r.state=J,r.buffer=void 0;try{for(var o=k(r.nexts),i=o.next();!i.done;i=o.next()){var n=i.value,a=r.pending===void 0?I(r):r.pending.then(function(){return I(r)});n.resolve(A(r,a))}}catch(c){e={error:c}}finally{try{i&&!i.done&&(t=o.return)&&t.call(o)}finally{if(e)throw e.error}}r.pushes=[],r.nexts=[]}}function mt(r){r.state>=tt||(r.state<J&&R(r),r.state=tt)}function Gt(r,e){if(Z(e),r.pushes.length>=z)throw new yt("No more than "+z+" pending calls to push are allowed on a single repeater.");if(r.state>=M)return Promise.resolve(void 0);var t=r.pending===void 0?Promise.resolve(e):r.pending.then(function(){return e});t=t.catch(function(u){r.state<M&&(r.err=u),mt(r)});var o;if(r.nexts.length){var i=r.nexts.shift();i.resolve(A(r,t)),r.nexts.length?o=Promise.resolve(r.nexts[0].value):typeof r.buffer<"u"&&!r.buffer.full?o=Promise.resolve(void 0):o=new Promise(function(u){return r.onnext=u})}else typeof r.buffer<"u"&&!r.buffer.full?(r.buffer.add(t),o=Promise.resolve(void 0)):o=new Promise(function(u){return r.pushes.push({resolve:u,value:t})});var n=!0,a={},c=o.catch(function(u){if(n)throw u});return a.then=function(u,f){return n=!1,Promise.prototype.then.call(o,u,f)},a.catch=function(u){return n=!1,Promise.prototype.catch.call(o,u)},a.finally=o.finally.bind(o),r.pending=t.then(function(){return c}).catch(function(u){r.err=u,mt(r)}),a}function Ot(r){var e=et.bind(null,r),t=new Promise(function(o){return r.onstop=o});return e.then=t.then.bind(t),e.catch=t.catch.bind(t),e.finally=t.finally.bind(t),e}function xt(r){if(!(r.state>=pt)){r.state=pt;var e=Gt.bind(null,r),t=Ot(r);r.execution=new Promise(function(o){return o(r.executor(e,t))}),r.execution.catch(function(){return et(r)})}}var F=new WeakMap,P=function(){function r(e,t){F.set(this,{executor:e,buffer:t,err:void 0,state:X,pushes:[],nexts:[],pending:void 0,execution:void 0,onnext:V,onstop:V})}return r.prototype.next=function(e){Z(e);var t=F.get(this);if(t===void 0)throw new Error("WeakMap error");if(t.nexts.length>=z)throw new yt("No more than "+z+" pending calls to next are allowed on a single repeater.");if(t.state<=X&&xt(t),t.onnext(e),typeof t.buffer<"u"&&!t.buffer.empty){var o=A(t,t.buffer.remove());if(t.pushes.length){var i=t.pushes.shift();t.buffer.add(i.value),t.onnext=i.resolve}return o}else if(t.pushes.length){var n=t.pushes.shift();return t.onnext=n.resolve,A(t,n.value)}else if(t.state>=M)return R(t),A(t,I(t));return new Promise(function(a){return t.nexts.push({resolve:a,value:e})})},r.prototype.return=function(e){Z(e);var t=F.get(this);if(t===void 0)throw new Error("WeakMap error");return R(t),t.execution=Promise.resolve(t.execution).then(function(){return e}),A(t,I(t))},r.prototype.throw=function(e){var t=F.get(this);if(t===void 0)throw new Error("WeakMap error");return t.state<=X||t.state>=M||typeof t.buffer<"u"&&!t.buffer.empty?(R(t),t.err==null&&(t.err=e),A(t,I(t))):this.next(Promise.reject(e))},r.prototype[Symbol.asyncIterator]=function(){return this},r.race=St,r.merge=jt,r.zip=Pt,r.latest=Et,r}();function K(r,e){var t,o,i=[],n=function(f){f!=null&&typeof f[Symbol.asyncIterator]=="function"?i.push(f[Symbol.asyncIterator]()):f!=null&&typeof f[Symbol.iterator]=="function"?i.push(f[Symbol.iterator]()):i.push(function(){return gt(this,arguments,function(){return j(this,function(s){switch(s.label){case 0:return e.yieldValues?[4,_(f)]:[3,3];case 1:return[4,s.sent()];case 2:s.sent(),s.label=3;case 3:return e.returnValues?[4,_(f)]:[3,5];case 4:return[2,s.sent()];case 5:return[2]}})})}())};try{for(var a=k(r),c=a.next();!c.done;c=a.next()){var u=c.value;n(u)}}catch(f){t={error:f}}finally{try{c&&!c.done&&(o=a.return)&&o.call(a)}finally{if(t)throw t.error}}return i}function St(r){var e=this,t=K(r,{returnValues:!0});return new P(function(o,i){return T(e,void 0,void 0,function(){var n,a,c,u,f,l;return j(this,function(d){switch(d.label){case 0:if(!t.length)return i(),[2];a=!1,i.then(function(){n(),a=!0}),d.label=1;case 1:d.trys.push([1,,5,7]),u=void 0,f=0,l=function(){var s,p,h,m,y,b;return j(this,function(x){switch(x.label){case 0:s=f;try{for(p=(y=void 0,k(t)),h=p.next();!h.done;h=p.next())m=h.value,Promise.resolve(m.next()).then(function(G){G.done?(i(),c===void 0&&(c=G)):f===s&&(f++,n(G))},function(G){return i(G)})}catch(G){y={error:G}}finally{try{h&&!h.done&&(b=p.return)&&b.call(p)}finally{if(y)throw y.error}}return[4,new Promise(function(G){return n=G})];case 1:return u=x.sent(),u===void 0?[3,3]:[4,o(u.value)];case 2:x.sent(),x.label=3;case 3:return[2]}})},d.label=2;case 2:return a?[3,4]:[5,l()];case 3:return d.sent(),[3,2];case 4:return[2,c&&c.value];case 5:return i(),[4,Promise.race(t.map(function(s){return s.return&&s.return()}))];case 6:return d.sent(),[7];case 7:return[2]}})})})}function jt(r){var e=this,t=K(r,{yieldValues:!0});return new P(function(o,i){return T(e,void 0,void 0,function(){var n,a,c,u=this;return j(this,function(f){switch(f.label){case 0:if(!t.length)return i(),[2];n=[],a=!1,i.then(function(){var l,d;a=!0;try{for(var s=k(n),p=s.next();!p.done;p=s.next()){var h=p.value;h()}}catch(m){l={error:m}}finally{try{p&&!p.done&&(d=s.return)&&d.call(s)}finally{if(l)throw l.error}}}),f.label=1;case 1:return f.trys.push([1,,3,4]),[4,Promise.all(t.map(function(l,d){return T(u,void 0,void 0,function(){var s,p;return j(this,function(h){switch(h.label){case 0:h.trys.push([0,,6,9]),h.label=1;case 1:return a?[3,5]:(Promise.resolve(l.next()).then(function(m){return n[d](m)},function(m){return i(m)}),[4,new Promise(function(m){n[d]=m})]);case 2:return s=h.sent(),s===void 0?[3,4]:s.done?(c=s,[2]):[4,o(s.value)];case 3:h.sent(),h.label=4;case 4:return[3,1];case 5:return[3,9];case 6:return p=l.return,p?[4,l.return()]:[3,8];case 7:p=h.sent(),h.label=8;case 8:return[7];case 9:return[2]}})})}))];case 2:return f.sent(),[2,c&&c.value];case 3:return i(),[7];case 4:return[2]}})})})}function Pt(r){var e=this,t=K(r,{returnValues:!0});return new P(function(o,i){return T(e,void 0,void 0,function(){var n,a,c,u;return j(this,function(f){switch(f.label){case 0:if(!t.length)return i(),[2,[]];a=!1,i.then(function(){n(),a=!0}),f.label=1;case 1:f.trys.push([1,,6,8]),f.label=2;case 2:return a?[3,5]:(Promise.all(t.map(function(l){return l.next()})).then(function(l){return n(l)},function(l){return i(l)}),[4,new Promise(function(l){return n=l})]);case 3:return c=f.sent(),c===void 0?[2]:(u=c.map(function(l){return l.value}),c.some(function(l){return l.done})?[2,u]:[4,o(u)]);case 4:return f.sent(),[3,2];case 5:return[3,8];case 6:return i(),[4,Promise.all(t.map(function(l){return l.return&&l.return()}))];case 7:return f.sent(),[7];case 8:return[2]}})})})}function Et(r){var e=this,t=K(r,{yieldValues:!0,returnValues:!0});return new P(function(o,i){return T(e,void 0,void 0,function(){var n,a,c,u,f,l=this;return j(this,function(d){switch(d.label){case 0:if(!t.length)return i(),[2,[]];a=[],c=!1,i.then(function(){var s,p;n();try{for(var h=k(a),m=h.next();!m.done;m=h.next()){var y=m.value;y()}}catch(b){s={error:b}}finally{try{m&&!m.done&&(p=h.return)&&p.call(h)}finally{if(s)throw s.error}}c=!0}),d.label=1;case 1:return d.trys.push([1,,5,7]),Promise.all(t.map(function(s){return s.next()})).then(function(s){return n(s)},function(s){return i(s)}),[4,new Promise(function(s){return n=s})];case 2:return u=d.sent(),u===void 0?[2]:(f=u.map(function(s){return s.value}),u.every(function(s){return s.done})?[2,f]:[4,o(f.slice())]);case 3:return d.sent(),[4,Promise.all(t.map(function(s,p){return T(l,void 0,void 0,function(){var h;return j(this,function(m){switch(m.label){case 0:if(u[p].done)return[2,u[p].value];m.label=1;case 1:return c?[3,4]:(Promise.resolve(s.next()).then(function(y){return a[p](y)},function(y){return i(y)}),[4,new Promise(function(y){return a[p]=y})]);case 2:return h=m.sent(),h===void 0?[2,u[p].value]:h.done?[2,h.value]:(f[p]=h.value,[4,o(f.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(t.map(function(s){return s.return&&s.return()}))];case 6:return d.sent(),[7];case 7:return[2]}})})})}var At="graffiti:local:",Y=class{db_;applyPatch_;ajv_;options;origin;get db(){return this.db_||(this.db_=(async()=>{let{default:e}=await import("./index-browser.es-G37SKL53.js"),t={name:"graffitiDb",...this.options.pouchDBOptions},o=new e(t.name,t);return await o.put({_id:"_design/indexes",views:{objectsPerChannelAndLastModified:{map:function(i){let n=i.lastModified.toString().padStart(15,"0");i.channels.forEach(function(a){let c=encodeURIComponent(a)+"/"+n;emit(c)})}.toString()},orphansPerActorAndLastModified:{map:function(i){if(i.channels.length===0){let n=i.lastModified.toString().padStart(15,"0"),a=encodeURIComponent(i.actor)+"/"+n;emit(a)}}.toString()},channelStatsPerActor:{map:function(i){i.tombstone||i.channels.forEach(function(n){let a=encodeURIComponent(i.actor)+"/"+encodeURIComponent(n);emit(a,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:e}=await import("./fast-json-patch-ZE7SZEYK.js");return e})()),this.applyPatch_}get ajv(){return this.ajv_||(this.ajv_=this.options.ajv?Promise.resolve(this.options.ajv):(async()=>{let{default:e}=await import("./ajv-6AI3HK2A.js");return new e({strict:!1})})()),this.ajv_}extractGraffitiObject(e){let{value:t,channels:o,allowed:i,url:n,actor:a,lastModified:c}=e;return{value:t,channels:o,allowed:i,url:n,actor:a,lastModified:c}}constructor(e){this.options=e??{},this.origin=this.options.origin??At,!this.origin.endsWith(":")&&!this.origin.endsWith("/")&&(this.origin+="/")}async allDocsAtLocation(e){let t=dt(e)+"/";return(await(await this.db).allDocs({startkey:t,endkey:t+"\uFFFF",include_docs:!0})).rows.map(n=>n.doc).reduce((n,a)=>(a&&n.push(a),n),[])}docId(e){return e.url+"/"+H()}get=async(...e)=>{let[t,o,i]=e,a=(await this.allDocsAtLocation(t)).filter(l=>W(l,i));if(!a.length)throw new O("The object you are trying to get either does not exist or you are not allowed to see it");let c=a.reduce((l,d)=>l.lastModified>d.lastModified||l.lastModified===d.lastModified&&!l.tombstone&&d.tombstone?l:d);if(c.tombstone)throw new O("The object you are trying to get either does not exist or you are not allowed to see it");let u=this.extractGraffitiObject(c);if(Q(u,[],i),!N(await this.ajv,o)(u))throw new st;return u};async deleteAtLocation(e,t={keepLatest:!1}){let o=await this.allDocsAtLocation(e),i=t.session?o.filter(s=>W(s,t.session)):o;if(i.length){if(t.session&&i.some(s=>s.actor!==t.session?.actor))throw new S("You cannot delete an object owned by another actor")}else throw new O("The object you are trying to delete either does not exist or you are not allowed to see it");let n=i.filter(s=>!s.tombstone);if(!n.length)return;let a=n.map(s=>s.lastModified).reduce((s,p)=>s>p?s:p),c=n.filter(s=>!t.keepLatest||s.lastModified<a),u=n.filter(s=>t.keepLatest&&s.lastModified===a);if(u.length){let s=u.map(h=>h._id).reduce((h,m)=>h>m?h:m),p=u.filter(h=>h._id!==s);c.push(...p)}let f=t.keepLatest?a:new Date().getTime(),l=await(await this.db).bulkDocs(c.map(s=>({...s,tombstone:!0,lastModified:f}))),d;for(let s of l)if("ok"in s){let{id:p}=s,h=c.find(m=>m._id===p);if(h){d={...this.extractGraffitiObject(h),lastModified:f};break}}return d}delete=async(...e)=>{let[t,o]=e,i=await this.deleteAtLocation(t,{session:o});if(!i)throw new O("The object has already been deleted");return i};put=async(...e)=>{let[t,o]=e;if(t.actor&&t.actor!==o.actor)throw new S("Cannot put an object with a different actor than the session actor");if(t.url){let c;try{c=await this.get(t.url,{},o)}catch(u){if(u instanceof O){if(!this.options.allowSettingArbitraryUrls)throw new O("The object you are trying to replace does not exist or you are not allowed to see it")}else throw u}if(c?.actor!==o.actor)throw new S("The object you are trying to replace is owned by another actor")}let i=(this.options.allowSettinngLastModified??!1)&&t.lastModified||new Date().getTime(),n={value:t.value,channels:t.channels,allowed:t.allowed,url:t.url??this.origin+H(),actor:o.actor,tombstone:!1,lastModified:i};await(await this.db).put({_id:this.docId(n),...n});let a=await this.deleteAtLocation(n,{keepLatest:!0});return a||{...n,value:{},channels:[],allowed:[],tombstone:!0}};patch=async(...e)=>{let[t,o,i]=e,n;try{n=await this.get(o,{},i)}catch(c){throw c instanceof O?new O("The object you are trying to patch does not exist or you are not allowed to see it"):c}if(n.actor!==i.actor)throw new S("The object you are trying to patch is owned by another actor");let a={...n};for(let c of["value","channels","allowed"])ht(await this.applyPatch,c,t,a);if(typeof a.value!="object"||Array.isArray(a.value)||!a.value)throw new L("value is no longer an object");if(!Array.isArray(a.channels)||!a.channels.every(c=>typeof c=="string"))throw new L("channels are no longer an array of strings");if(a.allowed&&(!Array.isArray(a.allowed)||!a.allowed.every(c=>typeof c=="string")))throw new L("allowed list is not an array of strings");return a.lastModified=new Date().getTime(),await(await this.db).put({...a,tombstone:!1,_id:this.docId(a)}),await this.deleteAtLocation(a,{keepLatest:!0}),{...n,lastModified:a.lastModified}};queryLastModifiedSuffixes(e,t){let o="",i="\uFFFF";if(typeof e=="object"&&e.properties?.lastModified&&typeof e.properties.lastModified=="object"){let n=e.properties.lastModified,a=t&&n.minimum?Math.max(t,n.minimum):t??n.minimum,c=n.exclusiveMinimum,u;c!==void 0?(u=Math.ceil(c),u===c&&u++):a!==void 0&&(u=Math.ceil(a)),u!==void 0&&(o=u.toString().padStart(15,"0"));let f=n.maximum,l=n.exclusiveMaximum,d;l!==void 0?(d=Math.floor(l),d===l&&d--):f!==void 0&&(d=Math.floor(f)),d!==void 0&&(i=d.toString().padStart(15,"0"))}return{startKeySuffix:o,endKeySuffix:i}}discoverMeta(e,t,o,i){let{startKeySuffix:n,endKeySuffix:a}=this.queryLastModifiedSuffixes(t,i),c=i!==void 0;return new P(async(f,l)=>{let d=N(await this.ajv,t),s=new Set;for(let h of e){let m=encodeURIComponent(h)+"/",y=m+n,b=m+a,x=await(await this.db).query("indexes/objectsPerChannelAndLastModified",{startkey:y,endkey:b,include_docs:!0});for(let G of x.rows){let E=G.doc;if(!E||!c&&E.tombstone)continue;let D=this.extractGraffitiObject(E);(!i||D.lastModified>i)&&(i=D.lastModified),!s.has(E._id)&&(s.add(E._id),W(E,o)&&(Q(D,e,o),d(D)&&await f({value:D,...E.tombstone?{tombstone:!0}:{}})))}}l();let p="discover:"+JSON.stringify({channels:e,schema:t,ifModifiedSince:i,actor:o?.actor});return{cursor:p,continue:()=>this.continueStream(p,o)}})}discover=(...e)=>this.discoverMeta(...e);recoverOrphansMeta(e,t,o){let{startKeySuffix:i,endKeySuffix:n}=this.queryLastModifiedSuffixes(e,o),a=encodeURIComponent(t.actor)+"/",c=a+i,u=a+n,f=o!==void 0;return new P(async(d,s)=>{let p=N(await this.ajv,e),h=await(await this.db).query("indexes/orphansPerActorAndLastModified",{startkey:c,endkey:u,include_docs:!0});for(let y of h.rows){let b=y.doc;if(!b||!f&&b.tombstone)continue;(!o||b.lastModified>o)&&(o=b.lastModified);let x=this.extractGraffitiObject(b);p(x)&&await d({value:x,...b.tombstone?{tombstone:!0}:{}})}s();let m="recover-orphans:"+JSON.stringify({schema:e,actor:t.actor,ifModifiedSince:o});return{cursor:m,continue:()=>this.continueStream(m,t)}})}recoverOrphans=(...e)=>this.recoverOrphansMeta(...e);channelStats=e=>new P(async(o,i)=>{let n=encodeURIComponent(e.actor)+"/",a=await(await this.db).query("indexes/channelStatsPerActor",{startkey:n,endkey:n+"\uFFFF",reduce:!0,group:!0});for(let u of a.rows){let f=u.key.split("/")[1];if(typeof f!="string")continue;let{count:l,max:d}=u.value;typeof l!="number"||typeof d!="number"||await o({value:{channel:decodeURIComponent(f),count:l,lastModified:d}})}i();let c="channel-stats";return{cursor:c,continue:()=>this.continueStream(c,e)}});continueStream=(e,t)=>{if(e==="channel-stats"){if(!t)throw new S("You must be logged in to continue the stream");return this.channelStats(t)}else if(e.startsWith("recover-orphans:")){let{schema:o,actor:i,ifModifiedSince:n}=JSON.parse(e.slice(16));if(!t||t.actor!==i)throw new S("You must be logged in as the actor same actor who started the stream");return this.recoverOrphansMeta(o,t,n)}else if(e.startsWith("discover:")){let{channels:o,schema:i,actor:n,ifModifiedSince:a}=JSON.parse(e.slice(9));if(t?.actor!==n)throw new S("You must be logged in as the actor same actor who started the stream");return this.discoverMeta(o,i,t,a)}else throw new O("Cursor not found")}};var bt=class extends rt{sessionManagerLocal=new q;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;continueStream;constructor(e){super();let t=new Y(e);this.put=t.put.bind(t),this.get=t.get.bind(t),this.patch=t.patch.bind(t),this.delete=t.delete.bind(t),this.discover=t.discover.bind(t),this.recoverOrphans=t.recoverOrphans.bind(t),this.channelStats=t.channelStats.bind(t),this.continueStream=t.continueStream.bind(t)}};export{bt as GraffitiLocal};
4
4
  /*! Bundled license information:
5
5
 
6
6
  @repeaterjs/repeater/repeater.js: