@graffiti-garden/implementation-local 0.4.2 → 0.5.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.
- package/dist/browser/index.js +2 -2
- package/dist/browser/index.js.map +3 -3
- package/dist/cjs/database.js +474 -1
- package/dist/cjs/database.js.map +3 -3
- package/dist/cjs/index.js +50 -1
- package/dist/cjs/index.js.map +3 -3
- package/dist/cjs/session-manager.js +105 -2
- package/dist/cjs/session-manager.js.map +2 -2
- package/dist/cjs/tests.spec.js +10 -1
- package/dist/cjs/tests.spec.js.map +3 -3
- package/dist/cjs/utilities.js +79 -1
- package/dist/cjs/utilities.js.map +3 -3
- package/dist/database.d.ts +14 -10
- package/dist/database.d.ts.map +1 -1
- package/dist/esm/database.js +457 -1
- package/dist/esm/database.js.map +3 -3
- package/dist/esm/index.js +32 -1
- package/dist/esm/index.js.map +3 -3
- package/dist/esm/session-manager.js +85 -2
- package/dist/esm/session-manager.js.map +2 -2
- package/dist/esm/tests.spec.js +14 -1
- package/dist/esm/tests.spec.js.map +3 -3
- package/dist/esm/utilities.js +64 -1
- package/dist/esm/utilities.js.map +3 -3
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/utilities.d.ts +2 -7
- package/dist/utilities.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/database.ts +113 -54
- package/src/index.ts +0 -4
- package/src/tests.spec.ts +0 -2
- package/src/utilities.ts +5 -38
package/dist/browser/index.js
CHANGED
|
@@ -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
|
|
1
|
+
import{d as w,f as g,g as b}from"./chunk-KNUPPOQC.js";w();b();g();w();b();g();var te=class{},ge={type:"object",properties:{value:{type:"object"},channels:{type:"array",items:{type:"string"}},allowed:{type:"array",items:{type:"string"},nullable:!0},uri:{type:"string"},actor:{type:"string"},lastModified:{type:"number"},tombstone:{type:"boolean"}},additionalProperties:!1,required:["value","channels","actor","uri","lastModified","tombstone"]},Ie={...ge,required:["value","channels"]};var T=class re extends Error{constructor(r){super(r),this.name="GraffitiErrorForbidden",Object.setPrototypeOf(this,re.prototype)}},O=class ne extends Error{constructor(r){super(r),this.name="GraffitiErrorNotFound",Object.setPrototypeOf(this,ne.prototype)}},ie=class oe extends Error{constructor(r){super(r),this.name="GraffitiErrorInvalidSchema",Object.setPrototypeOf(this,oe.prototype)}},ae=class se extends Error{constructor(r){super(r),this.name="GraffitiErrorSchemaMismatch",Object.setPrototypeOf(this,se.prototype)}},fe=class ce extends Error{constructor(r){super(r),this.name="GraffitiErrorPatchTestFailed",Object.setPrototypeOf(this,ce.prototype)}},L=class ue extends Error{constructor(r){super(r),this.name="GraffitiErrorPatchError",Object.setPrototypeOf(this,ue.prototype)}};w();b();g();var U=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 r=window.localStorage.getItem("graffiti-actor");return r?r.split(",").map(decodeURIComponent):[]}else return this.loggedInActors}setLoggedInActors(r){typeof window<"u"?window.localStorage.setItem("graffiti-actor",r.map(encodeURIComponent).join(",")):this.loggedInActors=r}login=async r=>{let e=r?.actor;if(!e&&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 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.`);n&&(e=n)}let o;if(!e)o={error:new Error("No actor ID provided to login")};else{let n=this.getLoggedInActors();n.includes(e)||this.setLoggedInActors([...n,e]),o={session:{actor:e}}}let i=new CustomEvent("login",{detail:o});this.sessionEvents.dispatchEvent(i)};logout=async r=>{let e=this.getLoggedInActors(),o=e.includes(r.actor);o&&this.setLoggedInActors(e.filter(a=>a!==r.actor));let i=o?{actor:r.actor}:{actor:r.actor,error:new Error("Not logged in with that actor")},n=new CustomEvent("logout",{detail:i});this.sessionEvents.dispatchEvent(n)}};w();b();g();w();b();g();function le(t){return typeof t=="string"?t:t.uri}function H(t=24){let r=new Uint8Array(t);return crypto.getRandomValues(r),btoa(String.fromCodePoint(...r)).replace(/\+/g,"-").replace(/\//g,"_").replace(/\=+$/,"")}function de(t,r){return t.lastModified>r.lastModified||t.lastModified===r.lastModified&&!t.tombstone&&r.tombstone}function he(t,r,e,o){let i=e[r];if(!(!i||!i.length))try{o[r]=t(o[r],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 fe(n.message):new L(n.name+": "+n.message):n}}function q(t,r){try{return t.compile(r)}catch(e){throw new ie(e instanceof Error?e.message:void 0)}}function Q(t,r,e){t.actor!==e?.actor&&(t.allowed=t.allowed&&e?[e.actor]:void 0,t.channels=t.channels.filter(o=>r.includes(o)))}function C(t,r){return t.allowed===void 0||t.allowed===null||!!r?.actor&&(t.actor===r.actor||t.allowed.includes(r.actor))}w();b();g();var Y=function(t,r){return Y=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])},Y(t,r)};function be(t,r){Y(t,r);function e(){this.constructor=t}t.prototype=r===null?Object.create(r):(e.prototype=r.prototype,new e)}function M(t,r,e,o){function i(n){return n instanceof e?n:new e(function(a){a(n)})}return new(e||(e=Promise))(function(n,a){function c(u){try{f(o.next(u))}catch(p){a(p)}}function l(u){try{f(o.throw(u))}catch(p){a(p)}}function f(u){u.done?n(u.value):i(u.value).then(c,l)}f((o=o.apply(t,r||[])).next())})}function P(t,r){var e={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(u){return l([f,u])}}function l(f){if(o)throw new TypeError("Generator is already executing.");for(;e;)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 e.label++,{value:f[1],done:!1};case 5:e.label++,i=f[1],f=[0];continue;case 7:f=e.ops.pop(),e.trys.pop();continue;default:if(n=e.trys,!(n=n.length>0&&n[n.length-1])&&(f[0]===6||f[0]===2)){e=0;continue}if(f[0]===3&&(!n||f[1]>n[0]&&f[1]<n[3])){e.label=f[1];break}if(f[0]===6&&e.label<n[1]){e.label=n[1],n=f;break}if(n&&e.label<n[2]){e.label=n[2],e.ops.push(f);break}n[2]&&e.ops.pop(),e.trys.pop();continue}f=r.call(t,e)}catch(u){f=[6,u],i=0}finally{o=n=0}if(f[0]&5)throw f[1];return{value:f[0]?f[1]:void 0,done:!0}}}function I(t){var r=typeof Symbol=="function"&&Symbol.iterator,e=r&&t[r],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(r?"Object is not iterable.":"Symbol.iterator is not defined.")}function R(t){return this instanceof R?(this.v=t,this):new R(t)}function xe(t,r,e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var o=e.apply(t,r||[]),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(h){return new Promise(function(d,y){n.push([s,h,d,y])>1||c(s,h)})})}function c(s,h){try{l(o[s](h))}catch(d){p(n[0][3],d)}}function l(s){s.value instanceof R?Promise.resolve(s.value.v).then(f,u):p(n[0][2],s)}function f(s){c("next",s)}function u(s){c("throw",s)}function p(s,h){s(h),n.shift(),n.length&&c(n[0][0],n[0][1])}}var me=function(t){be(r,t);function r(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 r}(Error),We=function(){function t(r){if(r<0)throw new RangeError("Capacity may not be less than 0");this._c=r,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(r){if(this.full)throw new Error("Buffer full");this._q.push(r)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}(),He=function(){function t(r){if(r<1)throw new RangeError("Capacity may not be less than 1");this._c=r,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(r){for(;this._q.length>=this._c;)this._q.shift();this._q.push(r)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}(),Qe=function(){function t(r){if(r<1)throw new RangeError("Capacity may not be less than 1");this._c=r,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(r){this._q.length<this._c&&this._q.push(r)},t.prototype.remove=function(){if(this.empty)throw new Error("Buffer empty");return this._q.shift()},t}();function $(t){t!=null&&typeof t.then=="function"&&t.then(V,V)}var X=0,pe=1,S=2,F=3,Z=4,z=1024,V=function(){};function A(t){var r=t.err,e=Promise.resolve(t.execution).then(function(o){if(r!=null)throw r;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 j(t,r){var e=t.state>=F;return Promise.resolve(r).then(function(o){return!e&&t.state>=Z?A(t).then(function(i){return{value:i,done:!0}}):{value:o,done:e}})}function ee(t,r){var e,o;if(!(t.state>=S))if(t.state=S,t.onnext(),t.onstop(),t.err==null&&(t.err=r),t.pushes.length===0&&(typeof t.buffer>"u"||t.buffer.empty))D(t);else try{for(var i=I(t.pushes),n=i.next();!n.done;n=i.next()){var a=n.value;a.resolve()}}catch(c){e={error:c}}finally{try{n&&!n.done&&(o=i.return)&&o.call(i)}finally{if(e)throw e.error}}}function D(t){var r,e;if(!(t.state>=F)){t.state<S&&ee(t),t.state=F,t.buffer=void 0;try{for(var o=I(t.nexts),i=o.next();!i.done;i=o.next()){var n=i.value,a=t.pending===void 0?A(t):t.pending.then(function(){return A(t)});n.resolve(j(t,a))}}catch(c){r={error:c}}finally{try{i&&!i.done&&(e=o.return)&&e.call(o)}finally{if(r)throw r.error}}t.pushes=[],t.nexts=[]}}function ye(t){t.state>=Z||(t.state<F&&D(t),t.state=Z)}function Ge(t,r){if($(r),t.pushes.length>=z)throw new me("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(r):t.pending.then(function(){return r});e=e.catch(function(l){t.state<S&&(t.err=l),ye(t)});var o;if(t.nexts.length){var i=t.nexts.shift();i.resolve(j(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(l){return t.onnext=l})}else typeof t.buffer<"u"&&!t.buffer.full?(t.buffer.add(e),o=Promise.resolve(void 0)):o=new Promise(function(l){return t.pushes.push({resolve:l,value:e})});var n=!0,a={},c=o.catch(function(l){if(n)throw l});return a.then=function(l,f){return n=!1,Promise.prototype.then.call(o,l,f)},a.catch=function(l){return n=!1,Promise.prototype.catch.call(o,l)},a.finally=o.finally.bind(o),t.pending=e.then(function(){return c}).catch(function(l){t.err=l,ye(t)}),a}function Oe(t){var r=ee.bind(null,t),e=new Promise(function(o){return t.onstop=o});return r.then=e.then.bind(e),r.catch=e.catch.bind(e),r.finally=e.finally.bind(e),r}function Pe(t){if(!(t.state>=pe)){t.state=pe;var r=Ge.bind(null,t),e=Oe(t);t.execution=new Promise(function(o){return o(t.executor(r,e))}),t.execution.catch(function(){return ee(t)})}}var N=new WeakMap,E=function(){function t(r,e){N.set(this,{executor:r,buffer:e,err:void 0,state:X,pushes:[],nexts:[],pending:void 0,execution:void 0,onnext:V,onstop:V})}return t.prototype.next=function(r){$(r);var e=N.get(this);if(e===void 0)throw new Error("WeakMap error");if(e.nexts.length>=z)throw new me("No more than "+z+" pending calls to next are allowed on a single repeater.");if(e.state<=X&&Pe(e),e.onnext(r),typeof e.buffer<"u"&&!e.buffer.empty){var o=j(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 n=e.pushes.shift();return e.onnext=n.resolve,j(e,n.value)}else if(e.state>=S)return D(e),j(e,A(e));return new Promise(function(a){return e.nexts.push({resolve:a,value:r})})},t.prototype.return=function(r){$(r);var e=N.get(this);if(e===void 0)throw new Error("WeakMap error");return D(e),e.execution=Promise.resolve(e.execution).then(function(){return r}),j(e,A(e))},t.prototype.throw=function(r){var e=N.get(this);if(e===void 0)throw new Error("WeakMap error");return e.state<=X||e.state>=S||typeof e.buffer<"u"&&!e.buffer.empty?(D(e),e.err==null&&(e.err=r),j(e,A(e))):this.next(Promise.reject(r))},t.prototype[Symbol.asyncIterator]=function(){return this},t.race=Ee,t.merge=je,t.zip=Se,t.latest=Le,t}();function J(t,r){var e,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 xe(this,arguments,function(){return P(this,function(s){switch(s.label){case 0:return r.yieldValues?[4,R(f)]:[3,3];case 1:return[4,s.sent()];case 2:s.sent(),s.label=3;case 3:return r.returnValues?[4,R(f)]:[3,5];case 4:return[2,s.sent()];case 5:return[2]}})})}())};try{for(var a=I(t),c=a.next();!c.done;c=a.next()){var l=c.value;n(l)}}catch(f){e={error:f}}finally{try{c&&!c.done&&(o=a.return)&&o.call(a)}finally{if(e)throw e.error}}return i}function Ee(t){var r=this,e=J(t,{returnValues:!0});return new E(function(o,i){return M(r,void 0,void 0,function(){var n,a,c,l,f,u;return P(this,function(p){switch(p.label){case 0:if(!e.length)return i(),[2];a=!1,i.then(function(){n(),a=!0}),p.label=1;case 1:p.trys.push([1,,5,7]),l=void 0,f=0,u=function(){var s,h,d,y,m,x;return P(this,function(G){switch(G.label){case 0:s=f;try{for(h=(m=void 0,I(e)),d=h.next();!d.done;d=h.next())y=d.value,Promise.resolve(y.next()).then(function(v){v.done?(i(),c===void 0&&(c=v)):f===s&&(f++,n(v))},function(v){return i(v)})}catch(v){m={error:v}}finally{try{d&&!d.done&&(x=h.return)&&x.call(h)}finally{if(m)throw m.error}}return[4,new Promise(function(v){return n=v})];case 1:return l=G.sent(),l===void 0?[3,3]:[4,o(l.value)];case 2:G.sent(),G.label=3;case 3:return[2]}})},p.label=2;case 2:return a?[3,4]:[5,u()];case 3:return p.sent(),[3,2];case 4:return[2,c&&c.value];case 5:return i(),[4,Promise.race(e.map(function(s){return s.return&&s.return()}))];case 6:return p.sent(),[7];case 7:return[2]}})})})}function je(t){var r=this,e=J(t,{yieldValues:!0});return new E(function(o,i){return M(r,void 0,void 0,function(){var n,a,c,l=this;return P(this,function(f){switch(f.label){case 0:if(!e.length)return i(),[2];n=[],a=!1,i.then(function(){var u,p;a=!0;try{for(var s=I(n),h=s.next();!h.done;h=s.next()){var d=h.value;d()}}catch(y){u={error:y}}finally{try{h&&!h.done&&(p=s.return)&&p.call(s)}finally{if(u)throw u.error}}}),f.label=1;case 1:return f.trys.push([1,,3,4]),[4,Promise.all(e.map(function(u,p){return M(l,void 0,void 0,function(){var s,h;return P(this,function(d){switch(d.label){case 0:d.trys.push([0,,6,9]),d.label=1;case 1:return a?[3,5]:(Promise.resolve(u.next()).then(function(y){return n[p](y)},function(y){return i(y)}),[4,new Promise(function(y){n[p]=y})]);case 2:return s=d.sent(),s===void 0?[3,4]:s.done?(c=s,[2]):[4,o(s.value)];case 3:d.sent(),d.label=4;case 4:return[3,1];case 5:return[3,9];case 6:return h=u.return,h?[4,u.return()]:[3,8];case 7:h=d.sent(),d.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 Se(t){var r=this,e=J(t,{returnValues:!0});return new E(function(o,i){return M(r,void 0,void 0,function(){var n,a,c,l;return P(this,function(f){switch(f.label){case 0:if(!e.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(e.map(function(u){return u.next()})).then(function(u){return n(u)},function(u){return i(u)}),[4,new Promise(function(u){return n=u})]);case 3:return c=f.sent(),c===void 0?[2]:(l=c.map(function(u){return u.value}),c.some(function(u){return u.done})?[2,l]:[4,o(l)]);case 4:return f.sent(),[3,2];case 5:return[3,8];case 6:return i(),[4,Promise.all(e.map(function(u){return u.return&&u.return()}))];case 7:return f.sent(),[7];case 8:return[2]}})})})}function Le(t){var r=this,e=J(t,{yieldValues:!0,returnValues:!0});return new E(function(o,i){return M(r,void 0,void 0,function(){var n,a,c,l,f,u=this;return P(this,function(p){switch(p.label){case 0:if(!e.length)return i(),[2,[]];a=[],c=!1,i.then(function(){var s,h;n();try{for(var d=I(a),y=d.next();!y.done;y=d.next()){var m=y.value;m()}}catch(x){s={error:x}}finally{try{y&&!y.done&&(h=d.return)&&h.call(d)}finally{if(s)throw s.error}}c=!0}),p.label=1;case 1:return p.trys.push([1,,5,7]),Promise.all(e.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 l=p.sent(),l===void 0?[2]:(f=l.map(function(s){return s.value}),l.every(function(s){return s.done})?[2,f]:[4,o(f.slice())]);case 3:return p.sent(),[4,Promise.all(e.map(function(s,h){return M(u,void 0,void 0,function(){var d;return P(this,function(y){switch(y.label){case 0:if(l[h].done)return[2,l[h].value];y.label=1;case 1:return c?[3,4]:(Promise.resolve(s.next()).then(function(m){return a[h](m)},function(m){return i(m)}),[4,new Promise(function(m){return a[h]=m})]);case 2:return d=y.sent(),d===void 0?[2,l[h].value]:d.done?[2,d.value]:(f[h]=d.value,[4,o(f.slice())]);case 3:return y.sent(),[3,1];case 4:return[2]}})})}))];case 4:return[2,p.sent()];case 5:return i(),[4,Promise.all(e.map(function(s){return s.return&&s.return()}))];case 6:return p.sent(),[7];case 7:return[2]}})})})}var ve=864e5,Ae="graffiti:local:",K=class{db_;applyPatch_;ajv_;options;origin;get db(){return this.db_||(this.db_=(async()=>{let{default:r}=await import("./index-browser.es-G37SKL53.js"),e={name:"graffitiDb",...this.options.pouchDBOptions},o=new r(e.name,e);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:r}=await import("./fast-json-patch-ZE7SZEYK.js");return r})()),this.applyPatch_}get ajv(){return this.ajv_||(this.ajv_=this.options.ajv?Promise.resolve(this.options.ajv):(async()=>{let{default:r}=await import("./ajv-6AI3HK2A.js");return new r({strict:!1})})()),this.ajv_}constructor(r){this.options=r??{},this.origin=this.options.origin??Ae,!this.origin.endsWith(":")&&!this.origin.endsWith("/")&&(this.origin+="/")}async allDocsAtLocation(r){let e=le(r)+"/";return(await(await this.db).allDocs({startkey:e,endkey:e+"\uFFFF",include_docs:!0})).rows.map(n=>n.doc).reduce((n,a)=>(a&&n.push(a),n),[])}docId(r){return r.uri+"/"+H()}get=async(...r)=>{let[e,o,i]=r,a=(await this.allDocsAtLocation(e)).filter(d=>C(d,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((d,y)=>de(d,y)?d:y),{_id:l,_rev:f,_conflicts:u,_attachments:p,...s}=c;if(Q(s,[],i),!q(await this.ajv,o)(s))throw new ae;return s};async deleteAtLocation(r,e={keepLatest:!1}){let o=await this.allDocsAtLocation(r),i=e.session?o.filter(s=>C(s,e.session)):o;if(i.length){if(e.session&&i.some(s=>s.actor!==e.session?.actor))throw new T("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,h)=>s>h?s:h),c=n.filter(s=>!e.keepLatest||s.lastModified<a),l=n.filter(s=>e.keepLatest&&s.lastModified===a);if(l.length){let s=l.map(d=>d._id).reduce((d,y)=>d>y?d:y),h=l.filter(d=>d._id!==s);c.push(...h)}let f=e.keepLatest?a:new Date().getTime(),u=await(await this.db).bulkDocs(c.map(s=>({...s,tombstone:!0,lastModified:f}))),p;for(let s of u)if("ok"in s){let{id:h}=s,d=c.find(y=>y._id===h);if(d){let{_id:y,_rev:m,_conflicts:x,_attachments:G,...v}=d;p={...v,tombstone:!0,lastModified:f};break}}return p}delete=async(...r)=>{let[e,o]=r,i=await this.deleteAtLocation(e,{session:o});if(!i)throw new O("The object has already been deleted");return i};put=async(...r)=>{let[e,o]=r;if(e.actor&&e.actor!==o.actor)throw new T("Cannot put an object with a different actor than the session actor");if(e.uri){let c;try{c=await this.get(e.uri,{},o)}catch(l){if(l instanceof O){if(!this.options.allowSettingArbitraryUris)throw new O("The object you are trying to replace does not exist or you are not allowed to see it")}else throw l}if(c?.actor!==o.actor)throw new T("The object you are trying to replace is owned by another actor")}let i=(this.options.allowSettinngLastModified??!1)&&e.lastModified||new Date().getTime(),n={value:e.value,channels:e.channels,allowed:e.allowed,uri:e.uri??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(...r)=>{let[e,o,i]=r,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 T("The object you are trying to patch is owned by another actor");if(n.tombstone)throw new O("The object you are trying to patch has been deleted");let a={...n};for(let c of["value","channels","allowed"])he(await this.applyPatch,c,e,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,_id:this.docId(a)}),await this.deleteAtLocation(a,{keepLatest:!0}),{...n,tombstone:!0,lastModified:a.lastModified}};queryLastModifiedSuffixes(r){let e="",o="\uFFFF";if(typeof r=="object"&&r.properties?.lastModified&&typeof r.properties.lastModified=="object"){let i=r.properties.lastModified,n=i.minimum,a=i.exclusiveMinimum,c;a!==void 0?(c=Math.ceil(a),c===a&&c++):n!==void 0&&(c=Math.ceil(n)),c!==void 0&&(e=c.toString().padStart(15,"0"));let l=i.maximum,f=i.exclusiveMaximum,u;f!==void 0?(u=Math.floor(f),u===f&&u--):l!==void 0&&(u=Math.floor(l)),u!==void 0&&(o=u.toString().padStart(15,"0"))}return{startKeySuffix:e,endKeySuffix:o}}discover=(...r)=>{let[e,o,i]=r,{startKeySuffix:n,endKeySuffix:a}=this.queryLastModifiedSuffixes(o);return new E(async(l,f)=>{let u=q(await this.ajv,o),p=new Set;for(let s of e){let h=encodeURIComponent(s)+"/",d=h+n,y=h+a,m=await(await this.db).query("indexes/objectsPerChannelAndLastModified",{startkey:d,endkey:y,include_docs:!0});for(let x of m.rows){let G=x.doc;if(!G)continue;let{_id:v,_rev:Me,...W}=G;p.has(v)||(p.add(v),C(G,i)&&(Q(W,e,i),u(W)&&await l({value:W})))}}return f(),{tombstoneRetention:this.options.tombstoneRetention??ve}})};recoverOrphans=(r,e)=>{let{startKeySuffix:o,endKeySuffix:i}=this.queryLastModifiedSuffixes(r),n=encodeURIComponent(e.actor)+"/",a=n+o,c=n+i;return new E(async(f,u)=>{let p=q(await this.ajv,r),s=await(await this.db).query("indexes/orphansPerActorAndLastModified",{startkey:a,endkey:c,include_docs:!0});for(let h of s.rows){let d=h.doc;if(!d)continue;let{_id:y,_rev:m,...x}=d;p(x)&&await f({value:x})}return u(),{tombstoneRetention:this.options.tombstoneRetention??ve}})};channelStats=r=>new E(async(o,i)=>{let n=encodeURIComponent(r.actor)+"/",a=await(await this.db).query("indexes/channelStatsPerActor",{startkey:n,endkey:n+"\uFFFF",reduce:!0,group:!0});for(let c of a.rows){let l=c.key.split("/")[1];if(typeof l!="string")continue;let{count:f,max:u}=c.value;typeof f!="number"||typeof u!="number"||await o({value:{channel:decodeURIComponent(l),count:f,lastModified:u}})}i()})};var we=class extends te{sessionManagerLocal=new U;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(r){super();let e=new K(r);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{we as GraffitiLocal};
|
|
4
4
|
/*! Bundled license information:
|
|
5
5
|
|
|
6
6
|
@repeaterjs/repeater/repeater.js:
|