@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 1.8 kB
JavaScript
import e from"../../kv/cache-keys.mjs";import t from"../utils/create-service-context.mjs";import{getHttpCacheNamespaceTokens as n}from"../../kv/http-cache.mjs";import{hasher as r}from"node-object-hash";import{createMiddleware as i}from"hono/factory";const a=r({sort:!0,coerce:!0}),o=(t,n,r)=>{if(n.staticKey){let i=typeof n.staticKey==`function`?n.staticKey(t):n.staticKey;return!r||Object.keys(r).length===0?i:e.http.response(a.hash({staticKey:i,namespaceTokens:r}))}let{mode:i=`include-query`,includeHeaders:o=[]}=n,s={path:t.req.path};if(i===`include-query`){let e=new URL(t.req.url);s.query=Object.fromEntries(e.searchParams.entries())}if(o.length>0){s.headers={};for(let e of o){let n=t.req.header(e);n&&(s.headers[e]=n)}}if(n.keyContext){let e=n.keyContext(t);Object.keys(e).length>0&&(s.keyContext=e)}return r&&Object.keys(r).length>0&&(s.namespaceTokens=r),e.http.response(a.hash(s))},s=e=>{let t=e.req.header(`Cache-Control`),n=e.req.header(`Pragma`);return!!(t?.includes(`no-cache`)||t?.includes(`no-store`)||n===`no-cache`)},c=e=>i(async(r,i)=>{if(e.bypass?.(r)){await i(),r.header(`Cache-Control`,`private, no-store`),r.header(`Pragma`,`no-cache`);return}if(s(r))return await i();let a=t(r),c=a.kv,l=typeof e.tags==`function`?e.tags(r):e.tags,u=l&&l.length>0?await n(a,l):void 0,d=o(r,e,u);if(d===null)return await i();let f=await c.get(a,{key:d,hash:!0});if(f!==null){let e=Math.floor((Date.now()-f.cachedAt)/1e3);return r.header(`X-Cache`,`HIT`),r.header(`Age`,e.toString()),r.json(f.data)}await i();let p=r.res.clone();if(p.ok&&p.headers.get(`content-type`)?.includes(`application/json`)){let t=await p.json();await c.set(a,{key:d,value:{data:t,cachedAt:Date.now()},expirationTtl:e.ttl,hash:!0})}r.header(`X-Cache`,`MISS`)});export{c as default};
//# sourceMappingURL=cache.mjs.map