mirror of
https://github.com/Sevichecc/Urara-Blog.git
synced 2025-05-04 01:19:30 +08:00
1 line
No EOL
2.8 KiB
Text
1 line
No EOL
2.8 KiB
Text
var t=Object.defineProperty,e=(e,i,a)=>(((e,i,a)=>{i in e?t(e,i,{enumerable:!0,configurable:!0,writable:!0,value:a}):e[i]=a})(e,"symbol"!=typeof i?i+"":i,a),a);function i(t){return"string"==typeof t||t instanceof ArrayBuffer||t instanceof ReadableStream?t:JSON.stringify(t)}var a=t=>`cache://${encodeURIComponent(t)}`;function n(t){var e=(t,e)=>`${t}__${e}`;return{get:(i,a,n)=>r(t,e(i,a),n),put:(i,a,n,r)=>s(t,e(i,a),n,{toJSON:!0,...r}),del:(i,a)=>l(t,e(i,a))}}function r(t,e,i="json"){return"string"!=typeof i&&i.metadata?t.getWithMetadata(e,i):t.get(e,i)}function s(t,e,i,a){let n=(!a||!a.toJSON)&&"string"==typeof i||i instanceof ArrayBuffer||i instanceof ReadableStream?i:JSON.stringify(i);return t.put(e,n,a).then((()=>!0),(()=>!1))}function l(t,e){return t.delete(e).then((()=>!0),(()=>!1))}async function*o(t,e){let{prefix:i,limit:a,cursor:n,metadata:r}=e||{};for(;;){let e=await t.list({prefix:i,limit:a,cursor:n});if(n=e.cursor,yield{done:e.list_complete,keys:r?e.keys:e.keys.map((t=>t.name))},e.list_complete)return}}async function u(t,e){let{prefix:i,metadata:a=!1,limit:n=50,page:r=1}=e||{},s=o(t,{prefix:i,limit:n,metadata:a});for await(let t of s){if(--r&&t.done)return[];if(0===r)return t.keys}return[]}async function c(t,e){let i,a="";for(;;)if(i=await e(a=t()),null==i)return a}function f(t,e){return!t||e.startsWith(t+"~")?e:t+"~"+e}var h=class{constructor(t){e(this,"ns"),e(this,"cache"),e(this,"prefix",""),e(this,"ttl",0),this.cache=new class{get(t){let e=a(t);return caches.default.match(e)}put(t,e,n){if(!n)return Promise.resolve(!0);let r={"cache-control":`public,max-age=${n}`},s=null==e?null:i(e),l=new Response(s,{headers:r}),o=a(t);return caches.default.put(o,l).then((()=>!0),(()=>!1))}delete(t){let e=a(t);return caches.default.delete(e)}},this.ns=t}async list(t){t=t||{};let{limit:e,prefix:i=""}=t;this.prefix&&(t.prefix=f(this.prefix,i)),e&&(t.limit=Math.min(1e3,e));let a=o(this.ns,{...t,metadata:!1}),n=[];for await(let t of a){for(let i=0,a=this.prefix.length;i<t.keys.length;i++)if(n.push(t.keys[i].substring(a)),e&&n.length===e)return n;if(t.done)break}return n}async get(t){t=f(this.prefix,t);let e,i=this.ttl&&await this.cache.get(t);return i?e=await i.json():(e=await this.ns.get(t,"json"),this.ttl&&await this.cache.put(t,e,this.ttl)),this.onread&&await this.onread(t,e),e}async put(t,e){t=f(this.prefix,t);let a=i(e),n=await this.ns.put(t,a).then((()=>!0),(()=>!1));if(n&&this.ttl){let i=null==e?null:a;n=await this.cache.put(t,i,this.ttl)}return n&&this.onwrite&&await this.onwrite(t,e),n}async delete(t){t=f(this.prefix,t);let e="function"==typeof this.ondelete,i=e&&await this.ns.get(t,"json"),a=await this.ns.delete(t).then((()=>!0),(()=>!1));return a&&this.ttl&&(a=await this.cache.delete(t)),a&&e&&await this.ondelete(t,i),a}};export{n as Database,h as Entity,o as list,u as paginate,r as read,l as remove,c as until,s as write}; |