UNPKG

keiro

Version:

A filesystem routing middleware

2 lines 2.04 kB
import{a as k,b as l,c as b}from"../chunk-LIEYSLZ5.js";import{b as g}from"../chunk-67FMRIE7.js";import"../chunk-PQKMAP7B.js";import{c as h,d as y,e as m,f,h as w,i as R}from"../chunk-UT4OBDAW.js";import"../chunk-O5JRXUW3.js";import"../chunk-JRHKYSQG.js";import"../chunk-PP5SNXX7.js";import"../chunk-F2YAVI33.js";import{createRouter as P}from"radix3";import{parentPort as n,workerData as H,threadId as W}from"worker_threads";import T from"url";var{router:E,middleware:x,handle404:U}=await v();n.on("message",A);var M=["get","head"];async function A(e){let t=new TransformStream;switch(e.type){case"request":{let i=R(e.headers),a=!M.includes(e.method.toLowerCase()),d=new Request(e.url,{method:e.method,body:a?t.readable:null,headers:i,duplex:"half"});await D(d);break}case"body":{await t.writable.getWriter().write(e.data);break}case"done":{await t.writable.close();break}}}async function D(e){if(!n)throw new Error("Handler was not executed as a worker");let t=new URL(e.url),i=E.lookup(t.pathname),{params:a={},...d}=i||{},c=f(e,d)||l,p=b(c,U),s=await(async()=>{let r=await k({request:e,params:a}),o;return x?o=await x(r,p):o=await c(r,p),g(o,r.cookies),o})(),u=w(s.headers);if(process.env.NODE_ENV!=="production"&&(u["X-Worker-Id"]=[W.toString()]),q(s.headers)){let r=await s.text();n.postMessage({type:"response",status:s.status,statusText:s.statusText,headers:u,body:r})}else{if(n.postMessage({type:"response",status:s.status,statusText:s.statusText,headers:u}),s.body){let r=s.body.getReader();for(;;){let{value:o,done:F}=await r.read();if(F)break;n.postMessage({type:"chunk",data:o,done:!1})}}n.postMessage({type:"chunk",done:!0})}}function q(e){let t=e.get("content-type");return t!=null&&t.startsWith("text/")}async function v(){let{middlewareFilePath:e,notFoundFilePath:t,routesFilePaths:i}=H,a={};for(let[s,u]of Object.entries(i))a[s]=await h(u);let d=P({routes:a}),c=e?await y(T.pathToFileURL(e).href):void 0,p=t?await m(T.pathToFileURL(t).href):l;return{router:d,middleware:c,handle404:p}} //# sourceMappingURL=worker.js.map