+const e=new Set([`/`,`/troubleshooting`]);async function t(e){let t={object:!1,workers:!1};if(!e.cache)return t;try{t.object=(await e.cache.getObjectCacheStatus()).configured}catch{}try{typeof e.cache.getWorkersCacheStatus==`function`&&(t.workers=(await e.cache.getWorkersCacheStatus()).configured)}catch{}return t}function n(e){let t=e.trim();if(t.startsWith(`//`))return{ok:!1,message:`Protocol-relative URLs are not allowed`};if(!t)return{ok:!1,message:`Path is required`};let n;if(/^[a-zA-Z][a-zA-Z+\-.]*:\/\//.test(t))try{n=new URL(t).pathname||`/`}catch{return{ok:!1,message:`Invalid URL`}}else{let e=t.split(`#`)[0]??t;n=e.split(`?`)[0]??e}return n.startsWith(`/`)||(n=`/${n}`),n=n.replace(/\/{2,}/g,`/`),n.length>2048?{ok:!1,message:`Path is too long`}:{ok:!0,path:n}}function r(e,t){let n=t?.pathInitial??``,r=[{type:`header`,text:`Troubleshooting`},{type:`context`,text:`Resolve EmDash shenanigans`},{type:`divider`},{type:`header`,text:`CMS object cache`},{type:`context`,text:`Clears EmDash’s optional KV/memory object cache (content queries, menus, settings, taxonomies).`}];return e.object?r.push({type:`form`,block_id:`object-cache`,fields:[],submit:{label:`Clear object cache`,action_id:`purge_object_cache`}}):r.push({type:`banner`,variant:`default`,title:`Object Cache Not Configured`,description:`Enable with objectCache: kvCache({ binding: "CACHE" }) in astro.config.`}),r.push({type:`divider`}),r.push({type:`header`,text:`Workers Cache`},{type:`context`,text:`Purges native Workers Caching`}),e.workers?(r.push({type:`form`,block_id:`workers-cache-all`,fields:[],submit:{label:`Clear all Workers Cache`,action_id:`purge_workers_cache`}}),r.push({type:`form`,block_id:`workers-cache-path`,fields:[{type:`text_input`,action_id:`path`,label:`Path or URL`,placeholder:`/posts/hello or https://example.com/posts/hello`,initial_value:n}],submit:{label:`Clear path`,action_id:`purge_workers_cache_path`}}),r.push({type:`context`,text:`Path purge matches prefixes (e.g. /posts/hello also clears /posts/hello-world).`})):r.push({type:`banner`,variant:`default`,title:`Workers Cache Not Available`,description:`Requires wrangler cache.enabled and a runtime with cache.purge (production Workers).`}),{blocks:r,...t?.toast?{toast:t.toast}:{}}}async function i(e){let n=await t(e);if(!e.cache||!n.object)return r(n,{toast:{message:`Object Cache Not Configured`,type:`error`}});try{let t=(await e.cache.purgeObjectCache()).purged.length,i=t===1?`namespace`:`namespaces`;return r({...n,object:!0},{toast:{message:`Object cache cleared (${t} ${i})`,type:`success`}})}catch(t){let i=t instanceof Error?t.message:`Unknown error`;return e.log.error(`Object cache purge failed`,t),r(n,{toast:{message:`Object cache purge failed: ${i}`,type:`error`}})}}async function a(e){let n=await t(e);if(!e.cache||!n.workers||typeof e.cache.purgeWorkersCache!=`function`)return r(n,{toast:{message:`Workers Cache Not Available`,type:`error`}});try{let t=await e.cache.purgeWorkersCache();return!t.configured||!t.purged?r({...n,workers:!1},{toast:{message:`Workers Cache Not Available`,type:`error`}}):r({...n,workers:!0},{toast:{message:`Workers Cache cleared`,type:`success`}})}catch(t){let i=t instanceof Error?t.message:`Unknown error`;return e.log.error(`Workers Cache purge failed`,t),r(n,{toast:{message:`Workers Cache purge failed: ${i}`,type:`error`}})}}async function o(e,i){let a=await t(e),o=typeof i?.path==`string`?i.path:``;if(!e.cache||!a.workers||typeof e.cache.purgeWorkersCache!=`function`)return r(a,{toast:{message:`Workers Cache Not Available`,type:`error`},pathInitial:o});let s=n(o);if(!s.ok)return r(a,{toast:{message:s.message,type:`error`},pathInitial:o});try{let t=await e.cache.purgeWorkersCache({pathPrefixes:[s.path]});return!t.configured||!t.purged?r({...a,workers:!1},{toast:{message:`Workers Cache Not Available`,type:`error`},pathInitial:o}):r({...a,workers:!0},{toast:{message:`Workers Cache cleared for ${s.path}`,type:`success`},pathInitial:s.path})}catch(t){let n=t instanceof Error?t.message:`Unknown error`;return e.log.error(`Workers Cache path purge failed`,t),r(a,{toast:{message:`Workers Cache purge failed: ${n}`,type:`error`},pathInitial:o})}}const s={routes:{admin:{handler:async(n,s)=>{let c=n.input;if(c.type===`page_load`&&c.page&&e.has(c.page))return r(await t(s));if(c.type===`form_submit`){if(c.action_id===`purge_object_cache`)return i(s);if(c.action_id===`purge_workers_cache`)return a(s);if(c.action_id===`purge_workers_cache_path`)return o(s,c.values)}return c.type===`block_action`&&c.action_id===`purge_object_cache`?i(s):c.type===`block_action`&&c.action_id===`purge_workers_cache`?a(s):{blocks:[]}}}}};export{s as default,n as normalizePathPrefix};
0 commit comments