UNPKG

keiro

Version:

A filesystem routing middleware

2 lines 3.05 kB
import{a as E}from"./chunk-67FMRIE7.js";import{b as O,c as W}from"./chunk-PQKMAP7B.js";import{a as p,c as x,d as F,e as b}from"./chunk-UT4OBDAW.js";import{a as g}from"./chunk-O5JRXUW3.js";import{a as v}from"./chunk-JRHKYSQG.js";import{b as M}from"./chunk-PP5SNXX7.js";import y from"path";import{existsSync as C}from"fs";import H from"os";import S from"url";import{createRouter as U}from"radix3";var L=(r=>(r[r.Dynamic=1]="Dynamic",r[r.Fixed=2]="Fixed",r))(L||{});function oe(o){let{cwd:e=process.cwd(),origin:r=process.env.ORIGIN,ignorePrefix:t="_",routesDir:n="src/routes",ignoreFiles:u=[],middleware:s="middleware",notFound:a="404",extensions:R=p,routeMapper:d=new M,getLocals:N=T,workers:c,prefix:m,skipOriginCheck:q=!1}=o||{};if(m&&g(!m.endsWith("/"),`Prefix should not end with "/": '${m}'`),s){let i=p.join(",");u.push(`**/**/${s}.{${i}}`)}if(a){let i=p.join(",");u.push(`**/**/${a}.{${i}}`)}if(!q&&r==null)throw new Error("Unable to determine the origin, set `ORIGIN` environment variable or pass the `origin` in the options of the 'fileSystemRouter'");g(R,"No file extensions specified"),g(y.isAbsolute(e),"cwd must be an absolute path");let l=y.posix.join(e,n);if(y.posix.relative(e,l).includes(".."))throw new Error("Routes dir is outside the current working directory");if(!C(l))throw new Error(`'${l}' don't exists`);let P={cwd:e,origin:r,prefix:m,ignorePrefix:t,routesDir:n,ignoreFiles:u,middleware:s,extensions:R,routeMapper:d};if(c){let i=H.cpus().length,f=typeof c=="boolean"?i:c.workerCount??i,I=s?h(l,s,p):void 0,j=(()=>{let k=2;return c===!0?w(k):w(c.pool??k)})();return{type:"worker",origin:r,workerCount:f,routeMapper:d,middlewareFilePath:I,poolFactory:j,initialOptions:P}}let D=(async()=>{if(s){let i=h(l,s,p);if(i){let f=S.pathToFileURL(i).href;return F(f)}}})(),$=(async()=>{if(a){let i=h(l,a,p);if(i){let f=S.pathToFileURL(i).href;return b(f)}}})();return{type:"handler",routerPromise:A({cwd:e,routesDir:n,prefix:m,ignorePrefix:t,ignoreFiles:u,routeMapper:d,extensions:R}),middlewarePromise:D,notFoundPromise:$,routeMapper:d,getLocals:N,initialOptions:P}}async function A(o){let e=v(o),r={},t=[];for(let[u,s]of Object.entries(e))t.push(x(s).then(a=>(r[u]=a,a)));return await Promise.all(t),U({routes:r})}function h(o,e,r){for(let t of r){let n=y.posix.join(o,`${e}.${t}`);if(C(n))return n}return null}function T(){return{}}function ue({request:o,params:e,getLocals:r}){let t=new URL(o.url),n=E.fromHeaders(o.headers);return r?Promise.resolve().then(async()=>{let u=await r({request:o,params:e,url:t,cookies:n,locals:{}});return{request:o,params:e,url:t,cookies:n,locals:u}}):{request:o,params:e,url:t,cookies:n,locals:{}}}function G(){return new Response(null,{status:404})}function ae(...o){return e=>{function r(t){let n=o[t];return n?n(e,()=>r(t+1)):G()}return r(0)}}function w(o){if(typeof o=="function")return o;switch(o){case 2:return(...e)=>new O(...e);case 1:return(...e)=>new W(...e);default:throw new Error("unreachable")}}export{ue as a,G as b,ae as c,w as d,L as e,oe as f}; //# sourceMappingURL=chunk-LIEYSLZ5.js.map