Urara-Blog/node_modules/.pnpm-store/v3/files/86/9ea977982a27a1f9f8bc2a47c911dae39cb376bcb3e5aa718b84f22ab14ffe87fa18d90a44627e94ef4cc21c4d9ff19ad844d835ccb9dd3f48aad2cfcaf2f7
2022-08-14 01:14:53 +08:00

1 line
No EOL
1.5 KiB
Text

import{parse as e}from"regexparam";import{finalize as r,STATUS_CODES as t}from"worktop/response";function n(...e){return async function(r,t){let n,a;for(n of e)if(a=await n(r,t))return a}}function a(){let n,a,s={};return n={add(r,t,n){let a=s[r];if(void 0===a&&(a=s[r]={__d:new Map,__s:{}}),t instanceof RegExp)a.__d.set(t,{keys:[],handler:n});else if(/[:|*]/.test(t)){let{keys:r,pattern:s}=e(t);a.__d.set(s,{keys:r,handler:n})}else a.__s[t]={keys:[],handler:n}},mount(e,r){a=a||{},a[e]=r.run},onerror(e,r){let{error:n,status:a=500}=r,s=n&&n.message||t[a];return new Response(s||String(a),{status:a})},async run(e,t){try{var o,i=[];(t=t||{}).url=new URL(e.url),t.defer=e=>{i.push(e)},t.bindings=t.bindings||{};var u=n.prepare&&await n.prepare(e,t);if(u&&u instanceof Response)return u;let f,l=t.url.pathname,p=l+"/";if(a&&l.length>1)for(f in a)if(p.startsWith(f))return t.url.pathname=l.substring(f.length)||"/",u=await a[f](new Request(t.url.href,e),t);if(f=function(e,r,t){let n,a,s,o,i,u={};if(a=e[r]){if(n=a.__s[t])return{params:u,handler:n.handler};for([s,o]of a.__d)if(i=s.exec(t),null!==i){if(void 0!==i.groups)for(n in i.groups)u[n]=i.groups[n];else if(o.keys.length>0)for(n=0;n<o.keys.length;)u[o.keys[n++]]=i[n];return{params:u,handler:o.handler}}}}(s,e.method,l),!f)return t.status=404,u=await n.onerror(e,t);t.params=f.params,u=await f.handler(e,t)}catch(r){t.status=500,t.error=r,u=await n.onerror(e,t)}finally{for(u=new Response(u?u.body:"OK",u);o=i.pop();)await o(u);return r(u,"HEAD"===e.method)}}}}export{a as Router,n as compose};