@graffiti-garden/implementation-local 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,5 +23,5 @@ PERFORMANCE OF THIS SOFTWARE.
23
23
  * https://github.com/Starcounter-Jack/JSON-Patch
24
24
  * (c) 2017-2021 Joachim Wester
25
25
  * MIT license
26
- */function yc(e,t){void 0===t&&(t=!1);var r=pc.get(e.object);gc(r.value,e.object,e.patches,"",t),e.patches.length&&uc(r.value,e.patches);var n=e.patches;return n.length>0&&(e.patches=[],e.callback&&e.callback(n)),n}function gc(e,t,r,n,o){if(t!==e){"function"==typeof t.toJSON&&(t=t.toJSON());for(var s=Wa(t),i=Wa(e),a=!1,c=i.length-1;c>=0;c--){var u=e[f=i[c]];if(!Ha(t,f)||void 0===t[f]&&void 0!==u&&!1===Array.isArray(t))Array.isArray(e)===Array.isArray(t)?(o&&r.push({op:"test",path:n+"/"+Xa(f),value:Qa(u)}),r.push({op:"remove",path:n+"/"+Xa(f)}),a=!0):(o&&r.push({op:"test",path:n,value:e}),r.push({op:"replace",path:n,value:t}));else{var l=t[f];"object"==typeof u&&null!=u&&"object"==typeof l&&null!=l&&Array.isArray(u)===Array.isArray(l)?gc(u,l,r,n+"/"+Xa(f),o):u!==l&&(o&&r.push({op:"test",path:n+"/"+Xa(f),value:Qa(u)}),r.push({op:"replace",path:n+"/"+Xa(f),value:Qa(l)}))}}if(a||s.length!=i.length)for(c=0;c<s.length;c++){var f;Ha(e,f=s[c])||void 0===t[f]||r.push({op:"add",path:n+"/"+Xa(f),value:Qa(t[f])})}}}var _c=Object.freeze({__proto__:null,compare:function(e,t,r){void 0===r&&(r=!1);var n=[];return gc(e,t,n,"",r),n},generate:yc,observe:function(e,t){var r,n=function(e){return pc.get(e)}(e);if(n){var o=function(e,t){return e.observers.get(t)}(n,t);r=o&&o.observer}else n=new mc(e),pc.set(e,n);if(r)return r;if(r={},n.value=Qa(e),t){r.callback=t,r.next=null;var s=function(){yc(r)},i=function(){clearTimeout(r.next),r.next=setTimeout(s)};"undefined"!=typeof window&&(window.addEventListener("mouseup",i),window.addEventListener("keyup",i),window.addEventListener("mousedown",i),window.addEventListener("keydown",i),window.addEventListener("change",i))}return r.patches=[],r.object=e,r.unobserve=function(){yc(r),clearTimeout(r.next),function(e,t){e.observers.delete(t.callback)}(n,r),"undefined"!=typeof window&&(window.removeEventListener("mouseup",i),window.removeEventListener("keyup",i),window.removeEventListener("mousedown",i),window.removeEventListener("keydown",i),window.removeEventListener("change",i))},n.observers.set(t,new vc(t,r)),r},unobserve:function(e,t){t.unobserve()}});Object.assign({},hc,_c,{JsonPatchError:rc,deepClone:Qa,escapePathComponent:Xa,unescapePathComponent:Za});class wc{db;source="local";tombstoneRetention=864e5;ajv;constructor(e,t){this.ajv=t??new Fa({strict:!1}),this.source=e?.sourceName??this.source,this.tombstoneRetention=e?.tombstoneRetention??this.tombstoneRetention;const r={name:"graffitiDb",...e?.pouchDBOptions};this.db=new Lt(r.name,r),this.db.put({_id:"_design/index3",views:{byChannelAndLastModified:{map:function(e){const t=e.lastModified.toString().padStart(15,"0");e.channels.forEach((function(e){const r=encodeURIComponent(e)+"/"+t;emit(r)}))}.toString()}}}).catch((e=>{if(!e||"object"!=typeof e||!("name"in e)||"conflict"!==e.name)throw e}))}async queryByLocation(e){const t=ro(e)+"/";return(await this.db.allDocs({startkey:t,endkey:t+"￿",include_docs:!0})).rows.map((e=>e.doc)).reduce(((e,t)=>(t&&e.push(t),e)),[]).filter((e=>!e.tombstone))}docId(e){return ro(e)+"/"+oo()}get=async(...e)=>{const[r,o,s]=e,{location:i}=so(r),a=await this.queryByLocation(i);if(!a.length)throw new t;const c=a.reduce(((e,t)=>e.lastModified>t.lastModified?e:t)),{_id:u,_rev:l,...f}=c;if(!uo(c,s))throw new t;co(f,[],s);if(!ao(this.ajv,o)(f))throw new n;return f};async deleteAtLocation(e,t=!1){const r=await this.queryByLocation(e);if(!r.length)return;const n=r.map((e=>e.lastModified)).reduce(((e,t)=>e>t?e:t)),o=r.filter((e=>!t||e.lastModified<n)),s=r.filter((e=>t&&e.lastModified===n));if(s.length){const e=s.map((e=>e._id)).reduce(((e,t)=>e>t?e:t)),t=s.filter((t=>t._id!==e));o.push(...t)}const i=t?n:(new Date).getTime();let a;for(const e of o.sort(((e,t)=>e.lastModified-t.lastModified))){const t={...e,tombstone:!0,lastModified:i};try{await this.db.put(t)}catch(e){if(e&&"object"==typeof e&&"name"in e&&"conflict"===e.name)continue}const{_id:r,_rev:n,...o}=t;a=o}return a}delete=async(...r)=>{const[n,o]=r,{location:s}=so(n);if(s.actor!==o.actor)throw new e;const i=await this.deleteAtLocation(s);if(!i)throw new t;return i};put=async(...t)=>{const[r,n]=t;if(r.actor&&r.actor!==n.actor)throw new e;const o={value:r.value,channels:r.channels,allowed:r.allowed,name:r.name??oo(),source:r.source??this.source,actor:n.actor,tombstone:!1,lastModified:(new Date).getTime()};await this.db.put({_id:this.docId(o),...o});const s=await this.deleteAtLocation(o,!0);return s||{...o,value:{},channels:[],allowed:void 0,tombstone:!0}};patch=async(...t)=>{const[r,n,o]=t,{location:i}=so(n);if(i.actor!==o.actor)throw new e;const a=await this.get(n,{},o),c={...a};for(const e of["value","channels","allowed"])io(uc,e,r,c);if("object"!=typeof c.value||Array.isArray(c.value)||!c.value)throw new s("value is no longer an object");if(!Array.isArray(c.channels)||!c.channels.every((e=>"string"==typeof e)))throw new s("channels are no longer an array of strings");if(c.allowed&&(!Array.isArray(c.allowed)||!c.allowed.every((e=>"string"==typeof e))))throw new s("allowed list is not an array of strings");return c.lastModified=(new Date).getTime(),await this.db.put({...c,_id:this.docId(c)}),await this.deleteAtLocation(c,!0),{...a,tombstone:!0,lastModified:c.lastModified}};discover=(...e)=>{const[t,r,n]=e,o=ao(this.ajv,r);let s="",i="￿";const a=r.properties?.lastModified;if(a?.minimum){let e=Math.ceil(a.minimum);e===a.minimum&&a.exclusiveMinimum&&e++,s=e.toString().padStart(15,"0")}if(a?.maximum){let e=Math.floor(a.maximum);e===a.maximum&&a.exclusiveMaximum&&e--,i=e.toString().padStart(15,"0")}const c=new xo((async(e,r)=>{const a=new Set;for(const r of t){const c=encodeURIComponent(r),u=c+"/"+s,l=c+"/"+i,f=await this.db.query("index3/byChannelAndLastModified",{startkey:u,endkey:l,include_docs:!0});for(const r of f.rows){const s=r.doc;if(!s)continue;const{_id:i,_rev:c,...u}=s;a.has(i)||(a.add(i),uo(s,n)&&(co(u,t,n),o(u)&&e({value:u})))}}return r(),{tombstoneRetention:this.tombstoneRetention}}));return c};listChannels=(...e)=>async function*(){}();listOrphans=(...e)=>async function*(){}()}export{wc as GraffitiLocalDatabase};
26
+ */function yc(e,t){void 0===t&&(t=!1);var r=pc.get(e.object);gc(r.value,e.object,e.patches,"",t),e.patches.length&&uc(r.value,e.patches);var n=e.patches;return n.length>0&&(e.patches=[],e.callback&&e.callback(n)),n}function gc(e,t,r,n,o){if(t!==e){"function"==typeof t.toJSON&&(t=t.toJSON());for(var s=Wa(t),i=Wa(e),a=!1,c=i.length-1;c>=0;c--){var u=e[f=i[c]];if(!Ha(t,f)||void 0===t[f]&&void 0!==u&&!1===Array.isArray(t))Array.isArray(e)===Array.isArray(t)?(o&&r.push({op:"test",path:n+"/"+Xa(f),value:Qa(u)}),r.push({op:"remove",path:n+"/"+Xa(f)}),a=!0):(o&&r.push({op:"test",path:n,value:e}),r.push({op:"replace",path:n,value:t}));else{var l=t[f];"object"==typeof u&&null!=u&&"object"==typeof l&&null!=l&&Array.isArray(u)===Array.isArray(l)?gc(u,l,r,n+"/"+Xa(f),o):u!==l&&(o&&r.push({op:"test",path:n+"/"+Xa(f),value:Qa(u)}),r.push({op:"replace",path:n+"/"+Xa(f),value:Qa(l)}))}}if(a||s.length!=i.length)for(c=0;c<s.length;c++){var f;Ha(e,f=s[c])||void 0===t[f]||r.push({op:"add",path:n+"/"+Xa(f),value:Qa(t[f])})}}}var _c=Object.freeze({__proto__:null,compare:function(e,t,r){void 0===r&&(r=!1);var n=[];return gc(e,t,n,"",r),n},generate:yc,observe:function(e,t){var r,n=function(e){return pc.get(e)}(e);if(n){var o=function(e,t){return e.observers.get(t)}(n,t);r=o&&o.observer}else n=new mc(e),pc.set(e,n);if(r)return r;if(r={},n.value=Qa(e),t){r.callback=t,r.next=null;var s=function(){yc(r)},i=function(){clearTimeout(r.next),r.next=setTimeout(s)};"undefined"!=typeof window&&(window.addEventListener("mouseup",i),window.addEventListener("keyup",i),window.addEventListener("mousedown",i),window.addEventListener("keydown",i),window.addEventListener("change",i))}return r.patches=[],r.object=e,r.unobserve=function(){yc(r),clearTimeout(r.next),function(e,t){e.observers.delete(t.callback)}(n,r),"undefined"!=typeof window&&(window.removeEventListener("mouseup",i),window.removeEventListener("keyup",i),window.removeEventListener("mousedown",i),window.removeEventListener("keydown",i),window.removeEventListener("change",i))},n.observers.set(t,new vc(t,r)),r},unobserve:function(e,t){t.unobserve()}});Object.assign({},hc,_c,{JsonPatchError:rc,deepClone:Qa,escapePathComponent:Xa,unescapePathComponent:Za});class wc{db;source="local";tombstoneRetention=864e5;ajv;constructor(e,t){this.ajv=t??new Fa({strict:!1}),this.source=e?.sourceName??this.source,this.tombstoneRetention=e?.tombstoneRetention??this.tombstoneRetention;const r={name:"graffitiDb",...e?.pouchDBOptions};this.db=new Lt(r.name,r),this.db.put({_id:"_design/index3",views:{byChannelAndLastModified:{map:function(e){const t=e.lastModified.toString().padStart(15,"0");e.channels.forEach((function(e){const r=encodeURIComponent(e)+"/"+t;emit(r)}))}.toString()}}}).catch((e=>{if(!e||"object"!=typeof e||!("name"in e)||"conflict"!==e.name)throw e}))}async queryByLocation(e){const t=ro(e)+"/";return(await this.db.allDocs({startkey:t,endkey:t+"￿",include_docs:!0})).rows.map((e=>e.doc)).reduce(((e,t)=>(t&&e.push(t),e)),[]).filter((e=>!e.tombstone))}docId(e){return ro(e)+"/"+oo()}get=async(...e)=>{const[r,o,s]=e,{location:i}=so(r),a=await this.queryByLocation(i);if(!a.length)throw new t;const c=a.reduce(((e,t)=>e.lastModified>t.lastModified?e:t)),{_id:u,_rev:l,...f}=c;if(!uo(c,s))throw new t;co(f,[],s);if(!ao(this.ajv,o)(f))throw new n;return f};async deleteAtLocation(e,t=!1){const r=await this.queryByLocation(e);if(!r.length)return;const n=r.map((e=>e.lastModified)).reduce(((e,t)=>e>t?e:t)),o=r.filter((e=>!t||e.lastModified<n)),s=r.filter((e=>t&&e.lastModified===n));if(s.length){const e=s.map((e=>e._id)).reduce(((e,t)=>e>t?e:t)),t=s.filter((t=>t._id!==e));o.push(...t)}const i=t?n:(new Date).getTime(),a=await this.db.bulkDocs(o.map((e=>({...e,tombstone:!0,lastModified:i}))));let c;for(const e of a)if("ok"in e){const{id:t}=e,r=o.find((e=>e._id===t));if(r){const{_id:e,_rev:t,_conflicts:n,_attachments:o,...s}=r;c={...s,tombstone:!0,lastModified:i};break}}return c}delete=async(...r)=>{const[n,o]=r,{location:s}=so(n);if(s.actor!==o.actor)throw new e;const i=await this.deleteAtLocation(s);if(!i)throw new t;return i};put=async(...t)=>{const[r,n]=t;if(r.actor&&r.actor!==n.actor)throw new e;const o={value:r.value,channels:r.channels,allowed:r.allowed,name:r.name??oo(),source:r.source??this.source,actor:n.actor,tombstone:!1,lastModified:(new Date).getTime()};await this.db.put({_id:this.docId(o),...o});const s=await this.deleteAtLocation(o,!0);return s||{...o,value:{},channels:[],allowed:void 0,tombstone:!0}};patch=async(...t)=>{const[r,n,o]=t,{location:i}=so(n);if(i.actor!==o.actor)throw new e;const a=await this.get(n,{},o),c={...a};for(const e of["value","channels","allowed"])io(uc,e,r,c);if("object"!=typeof c.value||Array.isArray(c.value)||!c.value)throw new s("value is no longer an object");if(!Array.isArray(c.channels)||!c.channels.every((e=>"string"==typeof e)))throw new s("channels are no longer an array of strings");if(c.allowed&&(!Array.isArray(c.allowed)||!c.allowed.every((e=>"string"==typeof e))))throw new s("allowed list is not an array of strings");return c.lastModified=(new Date).getTime(),await this.db.put({...c,_id:this.docId(c)}),await this.deleteAtLocation(c,!0),{...a,tombstone:!0,lastModified:c.lastModified}};discover=(...e)=>{const[t,r,n]=e,o=ao(this.ajv,r);let s="",i="￿";const a=r.properties?.lastModified;if(a?.minimum){let e=Math.ceil(a.minimum);e===a.minimum&&a.exclusiveMinimum&&e++,s=e.toString().padStart(15,"0")}if(a?.maximum){let e=Math.floor(a.maximum);e===a.maximum&&a.exclusiveMaximum&&e--,i=e.toString().padStart(15,"0")}const c=new xo((async(e,r)=>{const a=new Set;for(const r of t){const c=encodeURIComponent(r),u=c+"/"+s,l=c+"/"+i,f=await this.db.query("index3/byChannelAndLastModified",{startkey:u,endkey:l,include_docs:!0});for(const r of f.rows){const s=r.doc;if(!s)continue;const{_id:i,_rev:c,...u}=s;a.has(i)||(a.add(i),uo(s,n)&&(co(u,t,n),o(u)&&e({value:u})))}}return r(),{tombstoneRetention:this.tombstoneRetention}}));return c};listChannels=(...e)=>async function*(){}();listOrphans=(...e)=>async function*(){}()}export{wc as GraffitiLocalDatabase};
27
27
  //# sourceMappingURL=database.browser.js.map
@@ -1,2 +1,2 @@
1
- "use strict";var t=require("@graffiti-garden/api"),e=require("pouchdb"),o=require("@repeaterjs/repeater"),n=require("ajv-draft-04"),r=require("fast-json-patch");const i=t=>`${t.source}/${encodeURIComponent(t.actor)}/${encodeURIComponent(t.name)}`,a=e=>{const o=e.split("/"),n=o.pop(),r=o.pop();if(!n||!r||!o.length)throw new t.GraffitiErrorInvalidUri;return{name:decodeURIComponent(n),actor:decodeURIComponent(r),source:o.join("/")}};function s(t=16){const e=new Uint8Array(t);crypto.getRandomValues(e);return btoa(String.fromCodePoint(...e)).replace(/\+/g,"-").replace(/\//g,"_").replace(/\=+$/,"")}function c(t){return"string"==typeof t?{location:a(t),uri:t}:{location:{name:t.name,actor:t.actor,source:t.source},uri:i(t)}}function d(e,o,n,r){const i=n[o];if(i&&i.length)try{r[o]=e(r[o],i,!0,!1).newDocument}catch(e){throw"object"==typeof e&&e&&"name"in e&&"string"==typeof e.name&&"message"in e&&"string"==typeof e.message?"TEST_OPERATION_FAILED"===e.name?new t.GraffitiErrorPatchTestFailed(e.message):new t.GraffitiErrorPatchError(e.name+": "+e.message):e}}function l(e,o){try{return e.compile(o)}catch(e){throw new t.GraffitiErrorInvalidSchema(e instanceof Error?e.message:void 0)}}function f(t,e,o){t.actor!==o?.actor&&(t.allowed=t.allowed&&o?[o.actor]:void 0,t.channels=t.channels.filter((t=>e.includes(t))))}function u(t,e){return void 0===t.allowed||!!e?.actor&&(t.actor===e.actor||t.allowed.includes(e.actor))}exports.GraffitiLocalDatabase=class{db;source="local";tombstoneRetention=864e5;ajv;constructor(t,o){this.ajv=o??new n({strict:!1}),this.source=t?.sourceName??this.source,this.tombstoneRetention=t?.tombstoneRetention??this.tombstoneRetention;const r={name:"graffitiDb",...t?.pouchDBOptions};this.db=new e(r.name,r),this.db.put({_id:"_design/index3",views:{byChannelAndLastModified:{map:function(t){const e=t.lastModified.toString().padStart(15,"0");t.channels.forEach((function(t){const o=encodeURIComponent(t)+"/"+e;emit(o)}))}.toString()}}}).catch((t=>{if(!t||"object"!=typeof t||!("name"in t)||"conflict"!==t.name)throw t}))}async queryByLocation(t){const e=i(t)+"/";return(await this.db.allDocs({startkey:e,endkey:e+"￿",include_docs:!0})).rows.map((t=>t.doc)).reduce(((t,e)=>(e&&t.push(e),t)),[]).filter((t=>!t.tombstone))}docId(t){return i(t)+"/"+s()}get=async(...e)=>{const[o,n,r]=e,{location:i}=c(o),a=await this.queryByLocation(i);if(!a.length)throw new t.GraffitiErrorNotFound;const s=a.reduce(((t,e)=>t.lastModified>e.lastModified?t:e)),{_id:d,_rev:h,...m}=s;if(!u(s,r))throw new t.GraffitiErrorNotFound;f(m,[],r);if(!l(this.ajv,n)(m))throw new t.GraffitiErrorSchemaMismatch;return m};async deleteAtLocation(t,e=!1){const o=await this.queryByLocation(t);if(!o.length)return;const n=o.map((t=>t.lastModified)).reduce(((t,e)=>t>e?t:e)),r=o.filter((t=>!e||t.lastModified<n)),i=o.filter((t=>e&&t.lastModified===n));if(i.length){const t=i.map((t=>t._id)).reduce(((t,e)=>t>e?t:e)),e=i.filter((e=>e._id!==t));r.push(...e)}const a=e?n:(new Date).getTime();let s;for(const t of r.sort(((t,e)=>t.lastModified-e.lastModified))){const e={...t,tombstone:!0,lastModified:a};try{await this.db.put(e)}catch(t){if(t&&"object"==typeof t&&"name"in t&&"conflict"===t.name)continue}const{_id:o,_rev:n,...r}=e;s=r}return s}delete=async(...e)=>{const[o,n]=e,{location:r}=c(o);if(r.actor!==n.actor)throw new t.GraffitiErrorForbidden;const i=await this.deleteAtLocation(r);if(!i)throw new t.GraffitiErrorNotFound;return i};put=async(...e)=>{const[o,n]=e;if(o.actor&&o.actor!==n.actor)throw new t.GraffitiErrorForbidden;const r={value:o.value,channels:o.channels,allowed:o.allowed,name:o.name??s(),source:o.source??this.source,actor:n.actor,tombstone:!1,lastModified:(new Date).getTime()};await this.db.put({_id:this.docId(r),...r});const i=await this.deleteAtLocation(r,!0);return i||{...r,value:{},channels:[],allowed:void 0,tombstone:!0}};patch=async(...e)=>{const[o,n,i]=e,{location:a}=c(n);if(a.actor!==i.actor)throw new t.GraffitiErrorForbidden;const s=await this.get(n,{},i),l={...s};for(const t of["value","channels","allowed"])d(r.applyPatch,t,o,l);if("object"!=typeof l.value||Array.isArray(l.value)||!l.value)throw new t.GraffitiErrorPatchError("value is no longer an object");if(!Array.isArray(l.channels)||!l.channels.every((t=>"string"==typeof t)))throw new t.GraffitiErrorPatchError("channels are no longer an array of strings");if(l.allowed&&(!Array.isArray(l.allowed)||!l.allowed.every((t=>"string"==typeof t))))throw new t.GraffitiErrorPatchError("allowed list is not an array of strings");return l.lastModified=(new Date).getTime(),await this.db.put({...l,_id:this.docId(l)}),await this.deleteAtLocation(l,!0),{...s,tombstone:!0,lastModified:l.lastModified}};discover=(...t)=>{const[e,n,r]=t,i=l(this.ajv,n);let a="",s="￿";const c=n.properties?.lastModified;if(c?.minimum){let t=Math.ceil(c.minimum);t===c.minimum&&c.exclusiveMinimum&&t++,a=t.toString().padStart(15,"0")}if(c?.maximum){let t=Math.floor(c.maximum);t===c.maximum&&c.exclusiveMaximum&&t--,s=t.toString().padStart(15,"0")}return new o.Repeater((async(t,o)=>{const n=new Set;for(const o of e){const c=encodeURIComponent(o),d=c+"/"+a,l=c+"/"+s,h=await this.db.query("index3/byChannelAndLastModified",{startkey:d,endkey:l,include_docs:!0});for(const o of h.rows){const a=o.doc;if(!a)continue;const{_id:s,_rev:c,...d}=a;n.has(s)||(n.add(s),u(a,r)&&(f(d,e,r),i(d)&&t({value:d})))}}return o(),{tombstoneRetention:this.tombstoneRetention}}))};listChannels=(...t)=>async function*(){}();listOrphans=(...t)=>async function*(){}()};
1
+ "use strict";var t=require("@graffiti-garden/api"),e=require("pouchdb"),o=require("@repeaterjs/repeater"),n=require("ajv-draft-04"),r=require("fast-json-patch");const i=t=>`${t.source}/${encodeURIComponent(t.actor)}/${encodeURIComponent(t.name)}`,a=e=>{const o=e.split("/"),n=o.pop(),r=o.pop();if(!n||!r||!o.length)throw new t.GraffitiErrorInvalidUri;return{name:decodeURIComponent(n),actor:decodeURIComponent(r),source:o.join("/")}};function s(t=16){const e=new Uint8Array(t);crypto.getRandomValues(e);return btoa(String.fromCodePoint(...e)).replace(/\+/g,"-").replace(/\//g,"_").replace(/\=+$/,"")}function c(t){return"string"==typeof t?{location:a(t),uri:t}:{location:{name:t.name,actor:t.actor,source:t.source},uri:i(t)}}function d(e,o,n,r){const i=n[o];if(i&&i.length)try{r[o]=e(r[o],i,!0,!1).newDocument}catch(e){throw"object"==typeof e&&e&&"name"in e&&"string"==typeof e.name&&"message"in e&&"string"==typeof e.message?"TEST_OPERATION_FAILED"===e.name?new t.GraffitiErrorPatchTestFailed(e.message):new t.GraffitiErrorPatchError(e.name+": "+e.message):e}}function l(e,o){try{return e.compile(o)}catch(e){throw new t.GraffitiErrorInvalidSchema(e instanceof Error?e.message:void 0)}}function f(t,e,o){t.actor!==o?.actor&&(t.allowed=t.allowed&&o?[o.actor]:void 0,t.channels=t.channels.filter((t=>e.includes(t))))}function u(t,e){return void 0===t.allowed||!!e?.actor&&(t.actor===e.actor||t.allowed.includes(e.actor))}exports.GraffitiLocalDatabase=class{db;source="local";tombstoneRetention=864e5;ajv;constructor(t,o){this.ajv=o??new n({strict:!1}),this.source=t?.sourceName??this.source,this.tombstoneRetention=t?.tombstoneRetention??this.tombstoneRetention;const r={name:"graffitiDb",...t?.pouchDBOptions};this.db=new e(r.name,r),this.db.put({_id:"_design/index3",views:{byChannelAndLastModified:{map:function(t){const e=t.lastModified.toString().padStart(15,"0");t.channels.forEach((function(t){const o=encodeURIComponent(t)+"/"+e;emit(o)}))}.toString()}}}).catch((t=>{if(!t||"object"!=typeof t||!("name"in t)||"conflict"!==t.name)throw t}))}async queryByLocation(t){const e=i(t)+"/";return(await this.db.allDocs({startkey:e,endkey:e+"￿",include_docs:!0})).rows.map((t=>t.doc)).reduce(((t,e)=>(e&&t.push(e),t)),[]).filter((t=>!t.tombstone))}docId(t){return i(t)+"/"+s()}get=async(...e)=>{const[o,n,r]=e,{location:i}=c(o),a=await this.queryByLocation(i);if(!a.length)throw new t.GraffitiErrorNotFound;const s=a.reduce(((t,e)=>t.lastModified>e.lastModified?t:e)),{_id:d,_rev:h,...m}=s;if(!u(s,r))throw new t.GraffitiErrorNotFound;f(m,[],r);if(!l(this.ajv,n)(m))throw new t.GraffitiErrorSchemaMismatch;return m};async deleteAtLocation(t,e=!1){const o=await this.queryByLocation(t);if(!o.length)return;const n=o.map((t=>t.lastModified)).reduce(((t,e)=>t>e?t:e)),r=o.filter((t=>!e||t.lastModified<n)),i=o.filter((t=>e&&t.lastModified===n));if(i.length){const t=i.map((t=>t._id)).reduce(((t,e)=>t>e?t:e)),e=i.filter((e=>e._id!==t));r.push(...e)}const a=e?n:(new Date).getTime(),s=await this.db.bulkDocs(r.map((t=>({...t,tombstone:!0,lastModified:a}))));let c;for(const t of s)if("ok"in t){const{id:e}=t,o=r.find((t=>t._id===e));if(o){const{_id:t,_rev:e,_conflicts:n,_attachments:r,...i}=o;c={...i,tombstone:!0,lastModified:a};break}}return c}delete=async(...e)=>{const[o,n]=e,{location:r}=c(o);if(r.actor!==n.actor)throw new t.GraffitiErrorForbidden;const i=await this.deleteAtLocation(r);if(!i)throw new t.GraffitiErrorNotFound;return i};put=async(...e)=>{const[o,n]=e;if(o.actor&&o.actor!==n.actor)throw new t.GraffitiErrorForbidden;const r={value:o.value,channels:o.channels,allowed:o.allowed,name:o.name??s(),source:o.source??this.source,actor:n.actor,tombstone:!1,lastModified:(new Date).getTime()};await this.db.put({_id:this.docId(r),...r});const i=await this.deleteAtLocation(r,!0);return i||{...r,value:{},channels:[],allowed:void 0,tombstone:!0}};patch=async(...e)=>{const[o,n,i]=e,{location:a}=c(n);if(a.actor!==i.actor)throw new t.GraffitiErrorForbidden;const s=await this.get(n,{},i),l={...s};for(const t of["value","channels","allowed"])d(r.applyPatch,t,o,l);if("object"!=typeof l.value||Array.isArray(l.value)||!l.value)throw new t.GraffitiErrorPatchError("value is no longer an object");if(!Array.isArray(l.channels)||!l.channels.every((t=>"string"==typeof t)))throw new t.GraffitiErrorPatchError("channels are no longer an array of strings");if(l.allowed&&(!Array.isArray(l.allowed)||!l.allowed.every((t=>"string"==typeof t))))throw new t.GraffitiErrorPatchError("allowed list is not an array of strings");return l.lastModified=(new Date).getTime(),await this.db.put({...l,_id:this.docId(l)}),await this.deleteAtLocation(l,!0),{...s,tombstone:!0,lastModified:l.lastModified}};discover=(...t)=>{const[e,n,r]=t,i=l(this.ajv,n);let a="",s="￿";const c=n.properties?.lastModified;if(c?.minimum){let t=Math.ceil(c.minimum);t===c.minimum&&c.exclusiveMinimum&&t++,a=t.toString().padStart(15,"0")}if(c?.maximum){let t=Math.floor(c.maximum);t===c.maximum&&c.exclusiveMaximum&&t--,s=t.toString().padStart(15,"0")}return new o.Repeater((async(t,o)=>{const n=new Set;for(const o of e){const c=encodeURIComponent(o),d=c+"/"+a,l=c+"/"+s,h=await this.db.query("index3/byChannelAndLastModified",{startkey:d,endkey:l,include_docs:!0});for(const o of h.rows){const a=o.doc;if(!a)continue;const{_id:s,_rev:c,...d}=a;n.has(s)||(n.add(s),u(a,r)&&(f(d,e,r),i(d)&&t({value:d})))}}return o(),{tombstoneRetention:this.tombstoneRetention}}))};listChannels=(...t)=>async function*(){}();listOrphans=(...t)=>async function*(){}()};
2
2
  //# sourceMappingURL=database.cjs.js.map