@graffiti-garden/implementation-local 0.6.0 → 0.6.1
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 -20
- package/dist/browser/index.js.map +4 -4
- package/dist/cjs/database.js +130 -139
- package/dist/cjs/database.js.map +3 -3
- package/dist/cjs/index.js +11 -2
- package/dist/cjs/index.js.map +2 -2
- package/dist/database.d.ts +8 -4
- package/dist/database.d.ts.map +1 -1
- package/dist/esm/database.js +130 -139
- package/dist/esm/database.js.map +3 -3
- package/dist/esm/index.js +11 -2
- package/dist/esm/index.js.map +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/package.json +1 -2
- package/src/database.ts +200 -202
- package/src/index.ts +16 -3
package/dist/browser/index.js
CHANGED
|
@@ -1,22 +1,4 @@
|
|
|
1
|
-
import{d as
|
|
1
|
+
import{d as m,f as y,g as b}from"./chunk-KNUPPOQC.js";m();b();y();m();b();y();var T=class{},H={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"]},Y={...H,required:["value","channels"]};var v=class I extends Error{constructor(e){super(e),this.name="GraffitiErrorForbidden",Object.setPrototypeOf(this,I.prototype)}},g=class D extends Error{constructor(e){super(e),this.name="GraffitiErrorNotFound",Object.setPrototypeOf(this,D.prototype)}},C=class U extends Error{constructor(e){super(e),this.name="GraffitiErrorInvalidSchema",Object.setPrototypeOf(this,U.prototype)}},k=class B extends Error{constructor(e){super(e),this.name="GraffitiErrorSchemaMismatch",Object.setPrototypeOf(this,B.prototype)}},_=class N extends Error{constructor(e){super(e),this.name="GraffitiErrorPatchTestFailed",Object.setPrototypeOf(this,N.prototype)}},G=class R extends Error{constructor(e){super(e),this.name="GraffitiErrorPatchError",Object.setPrototypeOf(this,R.prototype)}};m();b();y();var E=class{sessionEvents=new EventTarget;constructor(){(async()=>{await Promise.resolve();for(let r of this.getLoggedInActors()){let i=new CustomEvent("login",{detail:{session:{actor:r}}});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 o=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.`);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
|
-
/*! Bundled license information:
|
|
5
|
-
|
|
6
|
-
@repeaterjs/repeater/repeater.js:
|
|
7
|
-
(*! *****************************************************************************
|
|
8
|
-
Copyright (c) Microsoft Corporation.
|
|
9
|
-
|
|
10
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
11
|
-
purpose with or without fee is hereby granted.
|
|
12
|
-
|
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
14
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
15
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
17
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
20
|
-
***************************************************************************** *)
|
|
21
|
-
*/
|
|
3
|
+
Simply choose a username to log in.`);o&&(t=o)}let r;if(!t)r={error:new Error("No actor ID provided to login")};else{let o=this.getLoggedInActors();o.includes(t)||this.setLoggedInActors([...o,t]),r={session:{actor:t}}}let i=new CustomEvent("login",{detail:r});this.sessionEvents.dispatchEvent(i)};logout=async e=>{let t=this.getLoggedInActors(),r=t.includes(e.actor);r&&this.setLoggedInActors(t.filter(n=>n!==e.actor));let i=r?{actor:e.actor}:{actor:e.actor,error:new Error("Not logged in with that actor")},o=new CustomEvent("logout",{detail:i});this.sessionEvents.dispatchEvent(o)}};m();b();y();m();b();y();function F(f){return typeof f=="string"?f:f.url}function A(f=24){let e=new Uint8Array(f);return crypto.getRandomValues(e),btoa(String.fromCodePoint(...e)).replace(/\+/g,"-").replace(/\//g,"_").replace(/\=+$/,"")}function W(f,e,t,r){let i=t[e];if(!(!i||!i.length))try{r[e]=f(r[e],i,!0,!1).newDocument}catch(o){throw typeof o=="object"&&o&&"name"in o&&typeof o.name=="string"&&"message"in o&&typeof o.message=="string"?o.name==="TEST_OPERATION_FAILED"?new _(o.message):new G(o.name+": "+o.message):o}}function x(f,e){try{return f.compile(e)}catch(t){throw new C(t instanceof Error?t.message:void 0)}}function L(f,e,t){f.actor!==t?.actor&&(f.allowed=f.allowed&&t?[t.actor]:void 0,f.channels=f.channels.filter(r=>e.includes(r)))}function M(f,e){return f.allowed===void 0||f.allowed===null||!!e?.actor&&(f.actor===e.actor||f.allowed.includes(e.actor))}var V="graffiti:local:",J=6e4,P=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},r=new e(t.name,t);return await r.put({_id:"_design/indexes",views:{objectsPerChannelAndLastModified:{map:function(i){let o=i.lastModified.toString().padStart(15,"0");i.channels.forEach(function(n){let a=encodeURIComponent(n)+"/"+o;emit(a)})}.toString()},orphansPerActorAndLastModified:{map:function(i){if(i.channels.length===0){let o=i.lastModified.toString().padStart(15,"0"),n=encodeURIComponent(i.actor)+"/"+o;emit(n)}}.toString()},channelStatsPerActor:{map:function(i){i.tombstone||i.channels.forEach(function(o){let n=encodeURIComponent(i.actor)+"/"+encodeURIComponent(o);emit(n,i.lastModified)})}.toString(),reduce:"_stats"}}}).catch(i=>{if(!(i&&typeof i=="object"&&"name"in i&&i.name==="conflict"))throw i}),r})()),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:r,allowed:i,url:o,actor:n,lastModified:a}=e;return{value:t,channels:r,allowed:i,url:o,actor:n,lastModified:a}}constructor(e){this.options=e??{},this.origin=this.options.origin??V,!this.origin.endsWith(":")&&!this.origin.endsWith("/")&&(this.origin+="/")}async allDocsAtLocation(e){let t=F(e)+"/";return(await(await this.db).allDocs({startkey:t,endkey:t+"\uFFFF",include_docs:!0})).rows.map(o=>o.doc).reduce((o,n)=>(n&&o.push(n),o),[])}docId(e){return e.url+"/"+A()}get=async(...e)=>{let[t,r,i]=e,n=(await this.allDocsAtLocation(t)).filter(l=>M(l,i));if(!n.length)throw new g("The object you are trying to get either does not exist or you are not allowed to see it");let a=n.reduce((l,d)=>l.lastModified>d.lastModified||l.lastModified===d.lastModified&&!l.tombstone&&d.tombstone?l:d);if(a.tombstone)throw new g("The object you are trying to get either does not exist or you are not allowed to see it");let c=this.extractGraffitiObject(a);if(L(c,[],i),!x(await this.ajv,r)(c))throw new k;return c};async deleteAtLocation(e,t={keepLatest:!1}){let r=await this.allDocsAtLocation(e),i=t.session?r.filter(s=>M(s,t.session)):r;if(i.length){if(t.session&&i.some(s=>s.actor!==t.session?.actor))throw new v("You cannot delete an object owned by another actor")}else throw new g("The object you are trying to delete either does not exist or you are not allowed to see it");let o=i.filter(s=>!s.tombstone);if(!o.length)return;let n=o.map(s=>s.lastModified).reduce((s,u)=>s>u?s:u),a=o.filter(s=>!t.keepLatest||s.lastModified<n),c=o.filter(s=>t.keepLatest&&s.lastModified===n);if(c.length){let s=c.map(p=>p._id).reduce((p,w)=>p>w?p:w),u=c.filter(p=>p._id!==s);a.push(...u)}let h=t.keepLatest?n:new Date().getTime(),l=await(await this.db).bulkDocs(a.map(s=>({...s,tombstone:!0,lastModified:h}))),d;for(let s of l)if("ok"in s){let{id:u}=s,p=a.find(w=>w._id===u);if(p){d={...this.extractGraffitiObject(p),lastModified:h};break}}return d}delete=async(...e)=>{let[t,r]=e,i=await this.deleteAtLocation(t,{session:r});if(!i)throw new g("The object has already been deleted");return i};put=async(...e)=>{let[t,r]=e;if(t.actor&&t.actor!==r.actor)throw new v("Cannot put an object with a different actor than the session actor");if(t.url){let a;try{a=await this.get(t.url,{},r)}catch(c){if(c instanceof g){if(!this.options.allowSettingArbitraryUrls)throw new g("The object you are trying to replace does not exist or you are not allowed to see it")}else throw c}if(a?.actor!==r.actor)throw new v("The object you are trying to replace is owned by another actor")}let i=(this.options.allowSettinngLastModified??!1)&&t.lastModified||new Date().getTime(),o={value:t.value,channels:t.channels,allowed:t.allowed,url:t.url??this.origin+A(),actor:r.actor,tombstone:!1,lastModified:i};await(await this.db).put({_id:this.docId(o),...o});let n=await this.deleteAtLocation(o,{keepLatest:!0});return n||{...o,value:{},channels:[],allowed:[],tombstone:!0}};patch=async(...e)=>{let[t,r,i]=e,o;try{o=await this.get(r,{},i)}catch(a){throw a instanceof g?new g("The object you are trying to patch does not exist or you are not allowed to see it"):a}if(o.actor!==i.actor)throw new v("The object you are trying to patch is owned by another actor");let n={...o};for(let a of["value","channels","allowed"])W(await this.applyPatch,a,t,n);if(typeof n.value!="object"||Array.isArray(n.value)||!n.value)throw new G("value is no longer an object");if(!Array.isArray(n.channels)||!n.channels.every(a=>typeof a=="string"))throw new G("channels are no longer an array of strings");if(n.allowed&&(!Array.isArray(n.allowed)||!n.allowed.every(a=>typeof a=="string")))throw new G("allowed list is not an array of strings");return n.lastModified=new Date().getTime(),await(await this.db).put({...n,tombstone:!1,_id:this.docId(n)}),await this.deleteAtLocation(n,{keepLatest:!0}),{...o,lastModified:n.lastModified}};queryLastModifiedSuffixes(e,t){let r="",i="\uFFFF";if(typeof e=="object"&&e.properties?.lastModified&&typeof e.properties.lastModified=="object"){let o=e.properties.lastModified,n=t&&o.minimum?Math.max(t,o.minimum):t??o.minimum,a=o.exclusiveMinimum,c;a!==void 0?(c=Math.ceil(a),c===a&&c++):n!==void 0&&(c=Math.ceil(n)),c!==void 0&&(r=c.toString().padStart(15,"0"));let h=o.maximum,l=o.exclusiveMaximum,d;l!==void 0?(d=Math.floor(l),d===l&&d--):h!==void 0&&(d=Math.floor(h)),d!==void 0&&(i=d.toString().padStart(15,"0"))}return{startKeySuffix:r,endKeySuffix:i}}async*streamObjects(e,t,r,i,o,n,a,c){let h=n!==void 0,l=await(await this.db).query(e,{startkey:t,endkey:r,include_docs:!0});for(let d of l.rows){let s=d.doc;if(!s||c?.has(s._id)||(c?.add(s._id),!h&&s.tombstone))continue;let u=this.extractGraffitiObject(s);if(a){if(!M(u,o))continue;L(u,a,o)}i(u)&&(yield s.tombstone?{tombstone:!0,object:{url:u.url,lastModified:u.lastModified}}:{object:u})}}async*discoverMeta(e,t){let[r,i,o]=e,n=x(await this.ajv,i),{startKeySuffix:a,endKeySuffix:c}=this.queryLastModifiedSuffixes(i,t),h=new Set,l=new Date().getTime();for(let d of r){let s=encodeURIComponent(d)+"/",u=s+a,p=s+c,w=this.streamObjects("indexes/objectsPerChannelAndLastModified",u,p,n,o,t,r,h);for await(let K of w)yield K}return l-J}async*recoverOrphansMeta(e,t){let[r,i]=e,{startKeySuffix:o,endKeySuffix:n}=this.queryLastModifiedSuffixes(r,t),a=encodeURIComponent(i.actor)+"/",c=a+o,h=a+n,l=x(await this.ajv,r),d=new Date().getTime(),s=this.streamObjects("indexes/orphansPerActorAndLastModified",c,h,l,i,t);for await(let u of s)yield u;return d-J}async*discoverContinue(e,t){let r=this.discoverMeta(e,t);for(;;){let i=await r.next();if(i.done){let o=i.value;return{continue:()=>this.discoverContinue(e,o),cursor:""}}yield i.value}}discover=(...e)=>{let t=this.discoverMeta(e),r=this;return async function*(){for(;;){let i=await t.next();if(i.done)return{continue:()=>r.discoverContinue(e,i.value),cursor:""};i.value.tombstone||(yield i.value)}}()};async*recoverContinue(e,t){let r=this.recoverOrphansMeta(e,t);for(;;){let i=await r.next();if(i.done){let o=i.value;return{continue:()=>this.recoverContinue(e,o),cursor:""}}yield i.value}}recoverOrphans=(...e)=>{let t=this.recoverOrphansMeta(e),r=this;return async function*(){for(;;){let i=await t.next();if(i.done)return{continue:()=>r.recoverContinue(e,i.value),cursor:""};i.value.tombstone||(yield i.value)}}()};channelStats=e=>{let t=this;return async function*(){let r=encodeURIComponent(e.actor)+"/",i=await(await t.db).query("indexes/channelStatsPerActor",{startkey:r,endkey:r+"\uFFFF",reduce:!0,group:!0});for(let o of i.rows){let n=o.key.split("/")[1];if(typeof n!="string")continue;let{count:a,max:c}=o.value;typeof a!="number"||typeof c!="number"||(yield{value:{channel:decodeURIComponent(n),count:a,lastModified:c}})}}()};continueObjectStream=(e,t)=>{throw new g("Cursor not found")}};var q=class extends T{sessionManagerLocal=new E;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;continueObjectStream;constructor(e){super();let t=new P(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.continueObjectStream=t.continueObjectStream.bind(t)}};function z(f){return f?42:"Hello"}var Gt=z(!1),vt=z(!0);export{q as GraffitiLocal};
|
|
22
4
|
//# sourceMappingURL=index.js.map
|