@hedystia/astro-bun
Version:
<div align="center"> <p> <strong>🚀 @hedystia/astro-bun</strong> </p>
45 lines (44 loc) • 6.05 kB
JavaScript
import{App as y}from"astro/app";var h={100:"Continue",101:"Switching Protocols",102:"Processing",103:"Early Hints",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",208:"Already Reported",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",306:"Unused",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a Teapot",421:"Misdirected Request",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",425:"Too Early",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Timeout",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",508:"Loop Detected",510:"Not Extended",511:"Network Authentication Required"};function g(r){return typeof r=="string"?r:typeof r=="boolean"?r?"0.0.0.0":"localhost":r}async function p(r,e,n,s){let o=Bun.file(e),u=`/${s.assets}/`;async function t(i){let a=new URL(`./${i}.html`,n),m=Bun.file(a);if(await m.exists())return new Response(m,{status:i,statusText:h[i]||"Unknown Error",headers:{"Content-Type":"text/html"}});let c=`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${i} ${h[i]||"Error"}</title>
<style>
body {
background-color: #121212;
color: #e0e0e0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
text-align: center;
}
.error-container {
background-color: #1e1e1e;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
h1 {
font-size: 3rem;
margin-bottom: 1rem;
}
p {
font-size: 1.5rem;
color: #a0a0a0;
}
</style>
</head>
<body>
<div class="error-container">
<h1>${i}</h1>
<p>${h[i]||"An unexpected error occurred"}</p>
</div>
</body>
</html>
`;return new Response(c,{status:i,headers:{"Content-Type":"text/html"}})}return await o.exists()?(i=>i.startsWith(u))(r)?new Response(o,{headers:{"Cache-Control":"public, max-age=31536000, immutable"}}):new Response(o):t(404)}import f from"cluster";import T from"os";function N(r,e){return{handle:S(r,e),running:()=>l!==null,start:()=>b(r,e),stop:()=>{l&&(l.stop(),l=null)}}}var l=null;function b(r,e){if(f.isPrimary&&e.cluster){let n=T.cpus().length;for(let s=0;s<n;s++)f.fork();f.on("exit",(s,o,u)=>{console.warn(`Worker ${s.process.pid} died`),f.fork()})}else{let s=new y(r).getAdapterLogger(),o=typeof e.host=="string"&&e.host.endsWith(".sock"),u=o?{unix:String(process.env.UNIX_SOCKET??e.host)}:{hostname:process.env.HOST??g(e.host)??"127.0.0.1",port:process.env.PORT?Number.parseInt(process.env.PORT):e.port,reusePort:e.cluster?!0:process.env.REUSE_PORT?!!process.env.REUSE_PORT:e.reusePort??!1,idleTimeout:process.env.IDLE_TIMEOUT?Number.parseInt(process.env.IDLE_TIMEOUT):e.idleTimeout??10};l=Bun.serve({development:process.env.APP_ENV==="development"||process.env.NODE_ENV==="development",error:d=>(s.error(`${d}`),new Response(`<pre>${d}
${d.stack}</pre>`,{headers:{"Content-Type":"text/html"},status:500})),lowMemoryMode:process.env.LOW_MEMORY_MODE?!!process.env.LOW_MEMORY_MODE:e.lowMemoryMode??!1,maxRequestBodySize:process.env.MAX_REQUEST_BODY_SIZE?Number.parseInt(process.env.MAX_REQUEST_BODY_SIZE):e.maxRequestBodySize??1024*1024*128,fetch:S(r,e),tls:{cert:process.env.TLS_CERT_PATH??e.tls?.certPath,key:process.env.TLS_KEY_PATH??e.tls?.keyPath,lowMemoryMode:process.env.LOW_MEMORY_MODE?!!process.env.LOW_MEMORY_MODE:e.lowMemoryMode??!1},...u});let t=()=>{l&&(l.stop(),l=null),process.exit(0)};process.on("SIGINT",t),process.on("SIGTERM",t),process.on("exit",t),s.info(`Server listening on ${o?`unix socket: ${e.host}`:l.url.href}`)}}function S(r,e){let n=e.client??new URL("../client/",import.meta.url).href,s=new y(r);return async(o,u)=>{let t=new URL(o.url);if((a=>{if(r.assets.has(a))return!0;let m=`/${e.assets}/`;return a.startsWith(m)?!0:[".js",".css",".png",".jpg",".jpeg",".gif",".svg",".ico",".woff",".woff2",".ttf",".eot",".pdf",".zip",".webp",".avif",".mp4",".webm",".mp3",".wav",".ogg",".json",".xml",".txt"].some(v=>a.toLowerCase().endsWith(v))})(t.pathname)){let a=new URL(`.${t.pathname}`,n);if(await Bun.file(a).exists())return p(t.pathname,a,n,e);let c=new URL(`./${s.removeBase(t.pathname)}`,n);return p(t.pathname,c,n,e)}let x=s.match(o);if(!x){let a=new URL(`.${t.pathname}`,n);if(await Bun.file(a).exists())return p(t.pathname,a,n,e);let c=new URL(`./${s.removeBase(t.pathname)}`,n);if(await Bun.file(c).exists())return p(t.pathname,c,n,e);let w=[t.pathname.endsWith("/")?new URL(`.${t.pathname}index.html`,n):null,t.pathname.endsWith("/")?null:new URL(`.${t.pathname}/index.html`,n),t.pathname.endsWith("/")?new URL(`./${s.removeBase(t.pathname)}index.html`,n):null,t.pathname.endsWith("/")?null:new URL(`./${s.removeBase(t.pathname)}/index.html`,n)].filter(Boolean);for(let E of w)if(await Bun.file(E).exists())return p(t.pathname,E,n,e);let P=new URL(`.${t.pathname}`,n);return p(t.pathname,P,n,e)}let i=u.requestIP(o)?.address||"127.0.0.1";return s.render(o,{addCookieHeader:!0,routeData:x,clientAddress:i})}}export{N as createExports,b as start};