@graffiti-garden/implementation-local 0.2.4 → 0.2.6
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 +0 -1
- package/dist/database.browser.js +5 -5
- package/dist/database.browser.js.map +1 -1
- package/dist/database.cjs.js +1 -1
- package/dist/database.js +1 -1
- package/dist/database.js.map +1 -1
- package/dist/index.browser.js +6 -6
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/session-manager.browser.js +1 -1
- package/dist/session-manager.cjs.js +1 -1
- package/dist/session-manager.js +1 -1
- package/dist/src/database.d.ts +8 -4
- package/dist/src/database.d.ts.map +1 -1
- package/dist/src/index.d.ts +5 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/session-manager.d.ts.map +1 -1
- package/dist/src/synchronize.d.ts +17 -4
- package/dist/src/synchronize.d.ts.map +1 -1
- package/dist/src/utilities.d.ts +1 -0
- package/dist/src/utilities.d.ts.map +1 -1
- package/dist/synchronize.browser.js +4 -4
- package/dist/synchronize.browser.js.map +1 -1
- package/dist/synchronize.cjs.js +1 -1
- package/dist/synchronize.js +1 -1
- package/dist/utilities.browser.js +1 -1
- package/dist/utilities.cjs.js +1 -1
- package/dist/utilities.js +1 -1
- package/package.json +2 -2
- package/src/database.ts +140 -39
- package/src/index.ts +15 -9
- package/src/session-manager.ts +1 -0
- package/src/synchronize.ts +66 -9
- package/src/tests.spec.ts +4 -0
- package/src/utilities.ts +13 -0
package/dist/database.cjs.js
CHANGED
|
@@ -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
|
|
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 f(e,o){try{return e.compile(o)}catch(e){throw new t.GraffitiErrorInvalidSchema(e instanceof Error?e.message:void 0)}}function l(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||null===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/indexes",views:{objectsPerChannelAndLastModified:{map:function(t){const e=t.lastModified.toString().padStart(15,"0");t.channels.forEach((function(t){const o=encodeURIComponent(t)+"/"+e;emit(o)}))}.toString()},orphansPerActorAndLastModified:{map:function(t){if(0===t.channels.length){const e=t.lastModified.toString().padStart(15,"0"),o=encodeURIComponent(t.actor)+"/"+e;emit(o)}}.toString()},channelStatsPerActor:{map:function(t){t.tombstone||t.channels.forEach((function(e){const o=encodeURIComponent(t.actor)+"/"+encodeURIComponent(e);emit(o,t.lastModified)}))}.toString(),reduce:"_stats"}}}).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)),[])}docId(t){return i(t)+"/"+s()}get=async(...e)=>{const[o,n,r]=e,{location:i}=c(o),a=(await this.queryByLocation(i)).filter((t=>u(t,r)));if(!a.length)throw new t.GraffitiErrorNotFound;const s=a.reduce(((t,e)=>{return n=e,(o=t).lastModified>n.lastModified||o.lastModified===n.lastModified&&!o.tombstone&&n.tombstone?t:e;var o,n})),{_id:d,_rev:h,...m}=s;l(m,[],r);if(!f(this.ajv,n)(m))throw new t.GraffitiErrorSchemaMismatch;return m};async deleteAtLocation(t,e=!1){const o=(await this.queryByLocation(t)).filter((t=>!t.tombstone));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;if(o.source&&o.source!==this.source)throw new t.GraffitiErrorForbidden("Putting an object that does not match this source");const r={value:o.value,channels:o.channels,allowed:o.allowed,name:o.name??s(),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);if(s.tombstone)throw new t.GraffitiErrorNotFound("The object you are trying to patch has been deleted");const f={...s};for(const t of["value","channels","allowed"])d(r.applyPatch,t,o,f);if("object"!=typeof f.value||Array.isArray(f.value)||!f.value)throw new t.GraffitiErrorPatchError("value is no longer an object");if(!Array.isArray(f.channels)||!f.channels.every((t=>"string"==typeof t)))throw new t.GraffitiErrorPatchError("channels are no longer an array of strings");if(f.allowed&&(!Array.isArray(f.allowed)||!f.allowed.every((t=>"string"==typeof t))))throw new t.GraffitiErrorPatchError("allowed list is not an array of strings");return f.lastModified=(new Date).getTime(),await this.db.put({...f,_id:this.docId(f)}),await this.deleteAtLocation(f,!0),{...s,tombstone:!0,lastModified:f.lastModified}};queryLastModifiedSuffixes(t){let e="",o="";const n=t.properties?.lastModified;if(n?.minimum){let t=Math.ceil(n.minimum);t===n.minimum&&n.exclusiveMinimum&&t++,e=t.toString().padStart(15,"0")}if(n?.maximum){let t=Math.floor(n.maximum);t===n.maximum&&n.exclusiveMaximum&&t--,o=t.toString().padStart(15,"0")}return{startKeySuffix:e,endKeySuffix:o}}discover=(...t)=>{const[e,n,r]=t,i=f(this.ajv,n),{startKeySuffix:a,endKeySuffix:s}=this.queryLastModifiedSuffixes(n);return new o.Repeater((async(t,o)=>{const n=new Set;for(const o of e){const c=encodeURIComponent(o)+"/",d=c+a,f=c+s,h=await this.db.query("indexes/objectsPerChannelAndLastModified",{startkey:d,endkey:f,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)&&(l(d,e,r),i(d)&&await t({value:d})))}}return o(),{tombstoneRetention:this.tombstoneRetention}}))};recoverOrphans=(t,e)=>{const n=f(this.ajv,t),{startKeySuffix:r,endKeySuffix:i}=this.queryLastModifiedSuffixes(t),a=encodeURIComponent(e.actor)+"/",s=a+r,c=a+i;return new o.Repeater((async(t,e)=>{const o=await this.db.query("indexes/orphansPerActorAndLastModified",{startkey:s,endkey:c,include_docs:!0});for(const e of o.rows){const o=e.doc;if(!o)continue;const{_id:r,_rev:i,...a}=o;n(a)&&await t({value:a})}return e(),{tombstoneRetention:this.tombstoneRetention}}))};channelStats=t=>new o.Repeater((async(e,o)=>{const n=encodeURIComponent(t.actor)+"/",r=await this.db.query("indexes/channelStatsPerActor",{startkey:n,endkey:n+"",reduce:!0,group:!0});for(const t of r.rows){const o=t.key.split("/")[1];if("string"!=typeof o)continue;const{count:n,max:r}=t.value;"number"==typeof n&&"number"==typeof r&&await e({value:{channel:decodeURIComponent(o),count:n,lastModified:r}})}o()}))};
|
|
2
2
|
//# sourceMappingURL=database.cjs.js.map
|