@graffiti-garden/implementation-local 0.6.1 → 0.6.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.
- package/README.md +4 -1
- package/dist/browser/index.js +2 -2
- package/dist/browser/index.js.map +3 -3
- package/dist/cjs/database.js +69 -14
- package/dist/cjs/database.js.map +2 -2
- package/dist/cjs/index.js +0 -9
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/utilities.js.map +2 -2
- package/dist/database.d.ts +8 -1
- package/dist/database.d.ts.map +1 -1
- package/dist/esm/database.js +69 -14
- package/dist/esm/database.js.map +2 -2
- package/dist/esm/index.js +0 -9
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/utilities.js +0 -1
- package/dist/esm/utilities.js.map +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/utilities.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/database.ts +97 -16
- package/src/index.ts +0 -13
- package/src/utilities.ts +0 -1
package/README.md
CHANGED
|
@@ -72,4 +72,7 @@ import * as GraffitiUtilities from "@graffiti-garden/implementation-local/utilit
|
|
|
72
72
|
|
|
73
73
|
## TODO
|
|
74
74
|
|
|
75
|
-
-
|
|
75
|
+
- Permanently delete content after it has been deleted for a certain amount of time
|
|
76
|
+
- Obscure cursors and make them only usable once.
|
|
77
|
+
- They should expire, too, if they have been around longer than the delete timer.
|
|
78
|
+
- Fix the LAST_MODIFIED_BUFFER hack.
|
package/dist/browser/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{d as m,f as y,g as b}from"./chunk-KNUPPOQC.js";m();b();y();m();b();y();var
|
|
1
|
+
import{d as m,f as y,g as b}from"./chunk-KNUPPOQC.js";m();b();y();m();b();y();var C=class{},z={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={...z,required:["value","channels"]};var w=class I extends Error{constructor(t){super(t),this.name="GraffitiErrorForbidden",Object.setPrototypeOf(this,I.prototype)}},g=class T extends Error{constructor(t){super(t),this.name="GraffitiErrorNotFound",Object.setPrototypeOf(this,T.prototype)}},D=class U extends Error{constructor(t){super(t),this.name="GraffitiErrorInvalidSchema",Object.setPrototypeOf(this,U.prototype)}},k=class B extends Error{constructor(t){super(t),this.name="GraffitiErrorSchemaMismatch",Object.setPrototypeOf(this,B.prototype)}},_=class N extends Error{constructor(t){super(t),this.name="GraffitiErrorPatchTestFailed",Object.setPrototypeOf(this,N.prototype)}},G=class R extends Error{constructor(t){super(t),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 e=new CustomEvent("initialized",{detail:{}});this.sessionEvents.dispatchEvent(e)})()}loggedInActors=[];getLoggedInActors(){if(typeof window<"u"){let t=window.localStorage.getItem("graffiti-actor");return t?t.split(",").map(decodeURIComponent):[]}else return this.loggedInActors}setLoggedInActors(t){typeof window<"u"?window.localStorage.setItem("graffiti-actor",t.map(encodeURIComponent).join(",")):this.loggedInActors=t}login=async t=>{let e=t?.actor;if(!e&&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.`);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};
|
|
3
|
+
Simply choose a username to log in.`);o&&(e=o)}let r;if(!e)r={error:new Error("No actor ID provided to login")};else{let o=this.getLoggedInActors();o.includes(e)||this.setLoggedInActors([...o,e]),r={session:{actor:e}}}let i=new CustomEvent("login",{detail:r});this.sessionEvents.dispatchEvent(i)};logout=async t=>{let e=this.getLoggedInActors(),r=e.includes(t.actor);r&&this.setLoggedInActors(e.filter(n=>n!==t.actor));let i=r?{actor:t.actor}:{actor:t.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 J(d){return typeof d=="string"?d:d.url}function A(d=24){let t=new Uint8Array(d);return crypto.getRandomValues(t),btoa(String.fromCodePoint(...t)).replace(/\+/g,"-").replace(/\//g,"_").replace(/\=+$/,"")}function F(d,t,e,r){let i=e[t];if(!(!i||!i.length))try{r[t]=d(r[t],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(d,t){try{return d.compile(t)}catch(e){throw new D(e instanceof Error?e.message:void 0)}}function L(d,t,e){d.actor!==e?.actor&&(d.allowed=d.allowed&&e?[e.actor]:void 0,d.channels=d.channels.filter(r=>t.includes(r)))}function P(d,t){return d.allowed===void 0||d.allowed===null||!!t?.actor&&(d.actor===t.actor||d.allowed.includes(t.actor))}var K="graffiti:local:",V=6e4,M=class{db_;applyPatch_;ajv_;options;origin;get db(){return this.db_||(this.db_=(async()=>{let{default:t}=await import("./index-browser.es-G37SKL53.js"),e={name:"graffitiDb",...this.options.pouchDBOptions},r=new t(e.name,e);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 t=await import("./fast-json-patch-ZE7SZEYK.js");return t.applyPatch||t.default.applyPatch})()),this.applyPatch_}get ajv(){return this.ajv_||(this.ajv_=this.options.ajv?Promise.resolve(this.options.ajv):(async()=>{let{default:t}=await import("./ajv-6AI3HK2A.js");return new t({strict:!1})})()),this.ajv_}extractGraffitiObject(t){let{value:e,channels:r,allowed:i,url:o,actor:n,lastModified:a}=t;return{value:e,channels:r,allowed:i,url:o,actor:n,lastModified:a}}constructor(t){this.options=t??{},this.origin=this.options.origin??K,!this.origin.endsWith(":")&&!this.origin.endsWith("/")&&(this.origin+="/")}async allDocsAtLocation(t){let e=J(t)+"/";return(await(await this.db).allDocs({startkey:e,endkey:e+"\uFFFF",include_docs:!0})).rows.map(o=>o.doc).reduce((o,n)=>(n&&o.push(n),o),[])}docId(t){return t.url+"/"+A()}get=async(...t)=>{let[e,r,i]=t,n=(await this.allDocsAtLocation(e)).filter(l=>P(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,c)=>l.lastModified>c.lastModified||l.lastModified===c.lastModified&&!l.tombstone&&c.tombstone?l:c);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 f=this.extractGraffitiObject(a);if(L(f,[],i),!x(await this.ajv,r)(f))throw new k;return f};async deleteAtLocation(t,e={keepLatest:!1}){let r=await this.allDocsAtLocation(t),i=e.session?r.filter(s=>P(s,e.session)):r;if(i.length){if(e.session&&i.some(s=>s.actor!==e.session?.actor))throw new w("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,p)=>s>p?s:p),a=o.filter(s=>!e.keepLatest||s.lastModified<n),f=o.filter(s=>e.keepLatest&&s.lastModified===n);if(f.length){let s=f.map(h=>h._id).reduce((h,v)=>h>v?h:v),p=f.filter(h=>h._id!==s);a.push(...p)}let u=e.keepLatest?n:new Date().getTime(),l=await(await this.db).bulkDocs(a.map(s=>({...s,tombstone:!0,lastModified:u}))),c;for(let s of l)if("ok"in s){let{id:p}=s,h=a.find(v=>v._id===p);if(h){c={...this.extractGraffitiObject(h),lastModified:u};break}}return c}delete=async(...t)=>{let[e,r]=t,i=await this.deleteAtLocation(e,{session:r});if(!i)throw new g("The object has already been deleted");return i};put=async(...t)=>{let[e,r]=t;if(e.actor&&e.actor!==r.actor)throw new w("Cannot put an object with a different actor than the session actor");if(e.url){let a;try{a=await this.get(e.url,{},r)}catch(f){if(f 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 f}if(a?.actor!==r.actor)throw new w("The object you are trying to replace is owned by another actor")}let i=(this.options.allowSettinngLastModified??!1)&&e.lastModified||new Date().getTime(),o={value:e.value,channels:e.channels,allowed:e.allowed,url:e.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(...t)=>{let[e,r,i]=t,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 w("The object you are trying to patch is owned by another actor");let n={...o};for(let a of["value","channels","allowed"])F(await this.applyPatch,a,e,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(t,e){let r="",i="\uFFFF";if(typeof t=="object"&&t.properties?.lastModified&&typeof t.properties.lastModified=="object"){let o=t.properties.lastModified,n=e&&o.minimum?Math.max(e,o.minimum):e??o.minimum,a=o.exclusiveMinimum,f;a!==void 0?(f=Math.ceil(a),f===a&&f++):n!==void 0&&(f=Math.ceil(n)),f!==void 0&&(r=f.toString().padStart(15,"0"));let u=o.maximum,l=o.exclusiveMaximum,c;l!==void 0?(c=Math.floor(l),c===l&&c--):u!==void 0&&(c=Math.floor(u)),c!==void 0&&(i=c.toString().padStart(15,"0"))}return{startKeySuffix:r,endKeySuffix:i}}async*streamObjects(t,e,r,i,o,n,a,f){if(n!==void 0){let l=this.options.continueBuffer??1e3,c=Date.now()-n;c<l&&await new Promise(s=>setTimeout(s,l-c)),n-=V}let u=await(await this.db).query(t,{startkey:e,endkey:r,include_docs:!0});for(let l of u.rows){let c=l.doc;if(!c||f?.has(c._id)||(f?.add(c._id),n===void 0&&c.tombstone))continue;let s=this.extractGraffitiObject(c);if(a){if(!P(s,o))continue;L(s,a,o)}i(s)&&(yield c.tombstone?{tombstone:!0,object:{url:s.url,lastModified:s.lastModified}}:{object:s})}}async*discoverMeta(t,e){let[r,i,o]=t,n=x(await this.ajv,i),{startKeySuffix:a,endKeySuffix:f}=this.queryLastModifiedSuffixes(i,e),u=new Set,l=new Date().getTime();for(let c of r){let s=encodeURIComponent(c)+"/",p=s+a,h=s+f,v=this.streamObjects("indexes/objectsPerChannelAndLastModified",p,h,n,o,e,r,u);for await(let q of v)yield q}return l}async*recoverOrphansMeta(t,e){let[r,i]=t,{startKeySuffix:o,endKeySuffix:n}=this.queryLastModifiedSuffixes(r,e),a=encodeURIComponent(i.actor)+"/",f=a+o,u=a+n,l=x(await this.ajv,r),c=new Date().getTime(),s=this.streamObjects("indexes/orphansPerActorAndLastModified",f,u,l,i,e);for await(let p of s)yield p;return c}discoverCursor(t,e){return"discover:"+JSON.stringify({channels:t[0],schema:t[1],actor:t[2]?.actor,ifModifiedSince:e})}async*discoverContinue(t,e){let r=this.discoverMeta(t,e);for(;;){let i=await r.next();if(i.done){let o=i.value;return{continue:()=>this.discoverContinue(t,o),cursor:this.discoverCursor(t,o)}}yield i.value}}discover=(...t)=>{let e=this.discoverMeta(t),r=this;return async function*(){for(;;){let i=await e.next();if(i.done)return{continue:()=>r.discoverContinue(t,i.value),cursor:r.discoverCursor(t,i.value)};i.value.tombstone||(yield i.value)}}()};recoverOrphansCursor(t,e){return"orphans:"+JSON.stringify({schema:t[0],actor:t[1]?.actor,ifModifiedSince:e})}async*recoverOrphansContinue(t,e){let r=this.recoverOrphansMeta(t,e);for(;;){let i=await r.next();if(i.done){let o=i.value;return{continue:()=>this.recoverOrphansContinue(t,o),cursor:this.recoverOrphansCursor(t,o)}}yield i.value}}recoverOrphans=(...t)=>{let e=this.recoverOrphansMeta(t),r=this;return async function*(){for(;;){let i=await e.next();if(i.done)return{continue:()=>r.recoverOrphansContinue(t,i.value),cursor:r.recoverOrphansCursor(t,i.value)};i.value.tombstone||(yield i.value)}}()};channelStats=t=>{let e=this;return async function*(){let r=encodeURIComponent(t.actor)+"/",i=await(await e.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:f}=o.value;typeof a!="number"||typeof f!="number"||(yield{value:{channel:decodeURIComponent(n),count:a,lastModified:f}})}}()};continueObjectStream=(t,e)=>{if(t.startsWith("discover:")){let{channels:r,schema:i,actor:o,ifModifiedSince:n}=JSON.parse(t.slice(9));if(o&&o!==e?.actor)throw new w("Cannot continue a cursor for another actor");return this.discoverContinue([r,i,e],n)}else if(t.startsWith("orphans:")){let{schema:r,actor:i,ifModifiedSince:o}=JSON.parse(t.slice(8));if(!e||i!==e?.actor)throw new w("Cannot continue a cursor for another actor");return this.recoverOrphansContinue([r,e],o)}else throw new g("Cursor not found")}};var W=class extends C{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(t){super();let e=new M(t);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),this.continueObjectStream=e.continueObjectStream.bind(e)}};export{W as GraffitiLocal};
|
|
4
4
|
//# sourceMappingURL=index.js.map
|