deserved 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +2 -2
  2. package/cli.js +10 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -31,7 +31,7 @@ bunx deserved [path] [options]
31
31
  | `-o, --open` | Open the browser |
32
32
  | `--cors` | Enable CORS |
33
33
  | `--no-dir` | Disable directory listing |
34
- | `--no-cache` | Disable cache headers |
34
+ | `--cache` | Long-lived, immutable cache headers for assets |
35
35
  | `--no-compress` | Disable gzip |
36
36
  | `-q, --quiet` | Suppress request logs |
37
37
 
@@ -56,6 +56,6 @@ bunx deserved dist --watch
56
56
  - SPA mode (HTML Accept only; assets still 404)
57
57
  - Optional live reload (`--watch`) via Server-Sent Events
58
58
  - Gzip compression
59
- - Smart cache headers (HTML `no-cache`, assets long-lived)
59
+ - Cache headers off by default (`no-cache`); opt in per-run with `--cache` for long-lived, immutable asset caching
60
60
  - Directory listings (disable with `--no-dir`)
61
61
  - Colored request logs
package/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env bun
2
2
  // @bun
3
- import{resolve as M0}from"path";var L="0.1.0";import{watch as j0}from"fs";import{networkInterfaces as I0}from"os";import{relative as V0,resolve as E0}from"path";import{readdirSync as o,realpathSync as z,statSync as A}from"fs";import{extname as r,join as s,normalize as t,resolve as S,sep as c}from"path";var{escapeHTML:_}=globalThis.Bun;var a=new TextEncoder,T="application/octet-stream",e={".html":"text/html;charset=utf-8",".htm":"text/html;charset=utf-8",".css":"text/css;charset=utf-8",".js":"text/javascript;charset=utf-8",".mjs":"text/javascript;charset=utf-8",".json":"application/json;charset=utf-8",".svg":"image/svg+xml",".txt":"text/plain;charset=utf-8",".xml":"application/xml",".wasm":"application/wasm",".woff":"font/woff",".woff2":"font/woff2",".map":"application/json"};function u0(u,$){let O=u.type;if(O&&O!==T&&O!=="")return O;let D=r($).toLowerCase();return e[D]??T}function M(u){let $=u.split(";")[0]?.trim().toLowerCase()??"";return $.startsWith("text/")||$==="application/javascript"||$==="text/javascript"||$==="application/json"||$==="application/xml"||$==="image/svg+xml"||$.endsWith("+json")||$.endsWith("+xml")}function $0(u){return u.startsWith("text/html")}function O0(u,$,O=""){return`"${u}-${Math.trunc($)}${O}"`}function W0(u,$){let O=u.headers.get("If-None-Match");if(!O)return!1;return O.split(",").some((D)=>D.trim().replace(/^W\//,"")===$)}function D0(u,$){let O=u.headers.get("If-Modified-Since");if(!O)return!1;let D=Date.parse(O);if(Number.isNaN(D))return!1;return Math.trunc($/1000)*1000<=D}function Q0(u,$,O){return u.headers.get("If-None-Match")?W0(u,$):D0(u,O)}function Y0(u){let $=u.headers.get("Accept");if(!$||$==="*/*")return!0;return $.includes("text/html")||$.includes("application/xhtml")}function Z0(u){return(u.headers.get("Accept-Encoding")??"").includes("gzip")}function X0(u){if(u.startsWith("/__"))return!1;let $=u.slice(u.lastIndexOf("/")+1);if(!$)return!0;if($.includes("."))return!1;return!0}function H(u,$){let O=S(u),D=i(O);if(!D)return null;let Z=$.replace(/^\/+/,""),Q=G0(Z);for(let W of Q){let k=b(O,W);if(!k)continue;let B=R(D,k);if(!B)continue;try{let X=A(B);if(X.isFile())return{path:B,file:Bun.file(B),size:X.size,mtimeMs:X.mtimeMs}}catch{}}return null}function G0(u){if(!u||u===".")return["index.html"];let $=u.endsWith("/")?u.slice(0,-1):u;if(!$)return["index.html"];if(u.endsWith("/"))return[`${$}/index.html`];return[$,`${$}.html`,`${$}/index.html`]}function b(u,$){let O=S(u),D=t(s(O,$));if(D!==O&&!D.startsWith(O+c))return null;return D}function R(u,$){try{let O=z($);if(O!==u&&!O.startsWith(u+c))return null;return O}catch{return null}}var n=new Map;function i(u){let $=n.get(u);if($===void 0){try{$=z(u)}catch{$=null}n.set(u,$)}return $}function N0(u,$){let O=S(u),D=i(O);if(!D)return null;let Z=$.replace(/^\/+/,"").replace(/\/+$/,""),Q=b(O,Z||".");if(!Q)return null;let W=R(D,Q);if(!W)return null;try{if(A(W).isDirectory())return W}catch{return null}return null}function k0(u){return o(u,{withFileTypes:!0}).filter(($)=>!$.name.startsWith(".")).map(($)=>({name:$.name,dir:$.isDirectory()})).sort(($,O)=>{if($.dir!==O.dir)return $.dir?-1:1;return $.name.localeCompare(O.name)})}function x0(u,$){let O=u.endsWith("/")?u:`${u}/`,D=B0(u),Z=$.map((W)=>{let k=J0(O,W.name)+(W.dir?"/":""),B=W.dir?`${W.name}/`:W.name;return` <li><a href="${_(k)}">${_(B)}</a></li>`}).join(`
4
- `),Q=D!==null?` <li><a href="${_(D)}">../</a></li>
3
+ import{resolve as T0}from"path";var L="0.2.0";import{watch as I0}from"fs";import{networkInterfaces as K0}from"os";import{relative as V0,resolve as f0}from"path";import{readdirSync as o,realpathSync as g,statSync as b}from"fs";import{extname as r,join as s,normalize as t,resolve as w,sep as R}from"path";var{escapeHTML:_}=globalThis.Bun;var a=new TextEncoder,z="application/octet-stream",e={".html":"text/html;charset=utf-8",".htm":"text/html;charset=utf-8",".css":"text/css;charset=utf-8",".js":"text/javascript;charset=utf-8",".mjs":"text/javascript;charset=utf-8",".json":"application/json;charset=utf-8",".svg":"image/svg+xml",".txt":"text/plain;charset=utf-8",".xml":"application/xml",".wasm":"application/wasm",".woff":"font/woff",".woff2":"font/woff2",".map":"application/json"};function u0(u,$){let O=u.type;if(O&&O!==z&&O!=="")return O;let G=r($).toLowerCase();return e[G]??z}function T(u){let $=u.split(";")[0]?.trim().toLowerCase()??"";return $.startsWith("text/")||$==="application/javascript"||$==="text/javascript"||$==="application/json"||$==="application/xml"||$==="image/svg+xml"||$.endsWith("+json")||$.endsWith("+xml")}function $0(u){return u.startsWith("text/html")}function O0(u,$,O=""){return`"${u}-${Math.trunc($)}${O}"`}function W0(u,$){let O=u.headers.get("If-None-Match");if(!O)return!1;return O.split(",").some((G)=>G.trim().replace(/^W\//,"")===$)}function D0(u,$){let O=u.headers.get("If-Modified-Since");if(!O)return!1;let G=Date.parse(O);if(Number.isNaN(G))return!1;return Math.trunc($/1000)*1000<=G}function G0(u,$,O){return u.headers.get("If-None-Match")?W0(u,$):D0(u,O)}function Q0(u){let $=u.headers.get("Accept");if(!$||$==="*/*")return!0;return $.includes("text/html")||$.includes("application/xhtml")}function Y0(u){let $=u.headers.get("Accept-Encoding")??"";if($.includes("zstd"))return"zstd";if($.includes("gzip"))return"gzip";return null}function Z0(u){if(u.startsWith("/__"))return!1;let $=u.slice(u.lastIndexOf("/")+1);if(!$)return!0;if($.includes("."))return!1;return!0}function v(u,$){let O=w(u),G=i(O);if(!G)return null;let Y=$.replace(/^\/+/,""),D=X0(Y);for(let W of D){let x=n(O,W);if(!x)continue;let k=c(G,x);if(!k)continue;try{let Z=b(k);if(Z.isFile())return{path:k,file:Bun.file(k),size:Z.size,mtimeMs:Z.mtimeMs}}catch{}}return null}function X0(u){if(!u||u===".")return["index.html"];let $=u.endsWith("/")?u.slice(0,-1):u;if(!$)return["index.html"];if(u.endsWith("/"))return[`${$}/index.html`];return[$,`${$}.html`,`${$}/index.html`]}function n(u,$){let O=w(u),G=t(s(O,$));if(G!==O&&!G.startsWith(O+R))return null;return G}function c(u,$){try{let O=g($);if(O!==u&&!O.startsWith(u+R))return null;return O}catch{return null}}var M=new Map;function i(u){let $=M.get(u);if($===void 0){try{$=g(u)}catch{$=null}M.set(u,$)}return $}function N0(u,$){let O=w(u),G=i(O);if(!G)return null;let Y=$.replace(/^\/+/,"").replace(/\/+$/,""),D=n(O,Y||".");if(!D)return null;let W=c(G,D);if(!W)return null;try{if(b(W).isDirectory())return W}catch{return null}return null}function x0(u){return o(u,{withFileTypes:!0}).filter(($)=>!$.name.startsWith(".")).map(($)=>({name:$.name,dir:$.isDirectory()})).sort(($,O)=>{if($.dir!==O.dir)return $.dir?-1:1;return $.name.localeCompare(O.name)})}function B0(u,$){let O=u.endsWith("/")?u:`${u}/`,G=k0(u),Y=$.map((W)=>{let x=J0(O,W.name)+(W.dir?"/":""),k=W.dir?`${W.name}/`:W.name;return` <li><a href="${_(x)}">${_(k)}</a></li>`}).join(`
4
+ `),D=G!==null?` <li><a href="${_(G)}">../</a></li>
5
5
  `:"";return`<!DOCTYPE html>
6
6
  <html lang="en">
7
7
  <head>
@@ -21,15 +21,15 @@ import{resolve as M0}from"path";var L="0.1.0";import{watch as j0}from"fs";import
21
21
  <body>
22
22
  <h1>Index of ${_(u||"/")}</h1>
23
23
  <ul>
24
- ${Q}${Z}
24
+ ${D}${Y}
25
25
  </ul>
26
26
  </body>
27
27
  </html>
28
- `}function B0(u){if(u==="/"||u==="")return null;let $=u.replace(/\/+$/,""),O=$.lastIndexOf("/");if(O<=0)return"/";return $.slice(0,O+1)}function J0(u,$){if(u==="/")return`/${$}`;return`${u.replace(/\/+$/,"")}/${$}`}function q(u,$){return async function(D){let Z=new URL(D.url),Q=decodeURIComponent(Z.pathname),W=D.method.toUpperCase();if(W!=="GET"&&W!=="HEAD"){let X=new Response("Method Not Allowed",{status:405,headers:{Allow:"GET, HEAD"}});return I(W,405,Q,u.quiet),j(X,u)}if(u.watch&&Q===P){let X=new Response(d,{headers:{"Content-Type":"text/javascript;charset=utf-8","Cache-Control":"no-store"}});return I(W,200,Q,u.quiet),E(W,j(X,u))}if(u.watch&&Q===f&&$){let X=$.subscribe();return I(W,200,Q,u.quiet),j(X,u)}if(Q===P||Q===f)return E(W,j(new Response("Not Found",{status:404}),u));let k=H(u.root,Q);if(k){let X=await y(D,k,u);return I(W,X.status,Q,u.quiet),E(W,j(X,u))}if(u.spa&&Y0(D)&&X0(Q)){let X=H(u.root,"/");if(X){let U=await y(D,X,u,!0);return I(W,U.status,Q,u.quiet),E(W,j(U,u))}}if(u.dir){let X=N0(u.root,Q);if(X){let U=k0(X),G=x0(Q,U),K=u.watch?w(G):G,J=new Response(K,{headers:{"Content-Type":"text/html;charset=utf-8","Cache-Control":"no-cache"}});return I(W,200,Q,u.quiet),E(W,j(J,u))}}let B=new Response("Not Found",{status:404});return I(W,404,Q,u.quiet),E(W,j(B,u))}}var v=new Map;async function K0(u,$){let O=v.get(u.key);if(O&&O.size===u.size&&O.mtimeMs===u.mtimeMs)return O.gz;try{let D=Bun.gzipSync(await $());return v.set(u.key,{size:u.size,mtimeMs:u.mtimeMs,gz:D}),D}catch{return null}}async function C(u,$,O,D){if(!Z0(u))return null;let Z=await K0(O,D);if(!Z)return null;return $.set("Content-Encoding","gzip"),$.set("Content-Length",String(Z.byteLength)),$.delete("Accept-Ranges"),$.append("Vary","Accept-Encoding"),new Response(Z,{status:200,headers:$})}async function y(u,$,O,D=!1){let{path:Z,file:Q,size:W,mtimeMs:k}=$,B=u0(Q,Z),X=D||$0(B),U=O0(W,k,O.watch&&X?"-live":"");if(Q0(u,U,k))return new Response(null,{status:304,headers:g(U,O,X,k)});let G=g(U,O,X,k);G.set("Content-Type",B),G.set("Accept-Ranges","bytes");let K=u.headers.get("Range");if(K&&u.method==="GET"){let J=U0(K,W);if(J==="invalid")return new Response(null,{status:416,headers:{"Content-Range":`bytes */${W}`}});if(J){let{start:Y,end:x}=J,V=x-Y+1,l=await Q.slice(Y,x+1).arrayBuffer();return G.set("Content-Range",`bytes ${Y}-${x}/${W}`),G.set("Content-Length",String(V)),new Response(l,{status:206,headers:G})}}if(O.watch&&X&&u.method==="GET"){if(O.compress&&M(B)){let Y=await C(u,G,{key:`${Z}\x00live`,size:W,mtimeMs:k},async()=>a.encode(w(await Q.text())));if(Y)return Y}let J=w(await Q.text());return G.set("Content-Length",String(Buffer.byteLength(J))),new Response(J,{status:200,headers:G})}if(O.compress&&u.method==="GET"&&M(B)&&W<2000000){let J=await C(u,G,{key:Z,size:W,mtimeMs:k},async()=>new Uint8Array(await Q.arrayBuffer()));if(J)return J}return G.set("Content-Length",String(W)),new Response(Q,{status:200,headers:G})}function g(u,$,O,D){let Z=new Headers;if(Z.set("ETag",u),Z.set("Last-Modified",new Date(D).toUTCString()),$.watch||!$.cache||O)return Z.set("Cache-Control","no-cache"),Z;return Z.set("Cache-Control","public, max-age=31536000, immutable"),Z}function U0(u,$){let O=/^bytes=(\d*)-(\d*)$/.exec(u.trim());if(!O)return null;let D=O[1],Z=O[2],Q,W;if(D===""&&Z==="")return"invalid";if(D===""){let k=Number(Z);if(!Number.isFinite(k)||k<=0)return"invalid";Q=Math.max(0,$-k),W=$-1}else Q=Number(D),W=Z===""?$-1:Number(Z);if(!Number.isFinite(Q)||!Number.isFinite(W)||Q<0||W<Q||Q>=$)return"invalid";return W=Math.min(W,$-1),{start:Q,end:W}}function j(u,$){if(!$.cors)return u;let O=new Headers(u.headers);return O.set("Access-Control-Allow-Origin","*"),O.set("Access-Control-Allow-Headers","*"),O.set("Access-Control-Allow-Methods","GET, HEAD, OPTIONS"),new Response(u.body,{status:u.status,statusText:u.statusText,headers:O})}function E(u,$){if(u!=="HEAD")return $;return new Response(null,{status:$.status,statusText:$.statusText,headers:$.headers})}var P="/__live.js",f="/__events",d=`(()=>{const e=new EventSource("${f}");e.onmessage=()=>location.reload();e.onerror=()=>{e.close();setTimeout(()=>location.reload(),1000)}})()`,m=`<script src="${P}"></script>`;function w(u){let $=u.toLowerCase().lastIndexOf("</body>");if($===-1)return u+m;return u.slice(0,$)+m+u.slice($)}function _0(){let u=new Set,$=new TextEncoder;function O(){let Z,Q=new ReadableStream({start(W){Z=W,u.add(W),W.enqueue($.encode(`: connected
28
+ `}function k0(u){if(u==="/"||u==="")return null;let $=u.replace(/\/+$/,""),O=$.lastIndexOf("/");if(O<=0)return"/";return $.slice(0,O+1)}function J0(u,$){if(u==="/")return`/${$}`;return`${u.replace(/\/+$/,"")}/${$}`}function q(u,$){return async function(G){let Y=new URL(G.url),D=decodeURIComponent(Y.pathname),W=G.method.toUpperCase();if(W!=="GET"&&W!=="HEAD"){let Z=new Response("Method Not Allowed",{status:405,headers:{Allow:"GET, HEAD"}});return V(W,405,D,u.quiet),K(Z,u)}if(u.watch&&D===F){let Z=new Response(m,{headers:{"Content-Type":"text/javascript;charset=utf-8","Cache-Control":"no-store"}});return V(W,200,D,u.quiet),f(W,K(Z,u))}if(u.watch&&D===P&&$){let Z=$.subscribe();return V(W,200,D,u.quiet),K(Z,u)}if(D===F||D===P)return f(W,K(new Response("Not Found",{status:404}),u));let x=v(u.root,D);if(x){let Z=await y(G,x,u);return V(W,Z.status,D,u.quiet),f(W,K(Z,u))}if(u.spa&&Q0(G)&&Z0(D)){let Z=v(u.root,"/");if(Z){let U=await y(G,Z,u,!0);return V(W,U.status,D,u.quiet),f(W,K(U,u))}}if(u.dir){let Z=N0(u.root,D);if(Z){let U=x0(Z),X=B0(D,U),j=u.watch?S(X):X,B=new Response(j,{headers:{"Content-Type":"text/html;charset=utf-8","Cache-Control":"no-cache"}});return V(W,200,D,u.quiet),f(W,K(B,u))}}let k=new Response("Not Found",{status:404});return V(W,404,D,u.quiet),f(W,K(k,u))}}var H=new Map;async function U0(u,$,O){let G=`${u.key}:${$}`,Y=H.get(G);if(Y&&Y.size===u.size&&Y.mtimeMs===u.mtimeMs)return Y.data;try{let D=await O(),W=$==="zstd"?new Uint8Array(Bun.zstdCompressSync(D)):Bun.gzipSync(D);return H.set(G,{size:u.size,mtimeMs:u.mtimeMs,data:W}),W}catch{return null}}async function A(u,$,O,G){let Y=Y0(u);if(!Y)return null;let D=await U0(O,Y,G);if(!D)return null;return $.set("Content-Encoding",Y),$.set("Content-Length",String(D.byteLength)),$.delete("Accept-Ranges"),$.append("Vary","Accept-Encoding"),new Response(D,{status:200,headers:$})}async function y(u,$,O,G=!1){let{path:Y,file:D,size:W,mtimeMs:x}=$,k=u0(D,Y),Z=G||$0(k),U=O0(W,x,O.watch&&Z?"-live":"");if(G0(u,U,x))return new Response(null,{status:304,headers:C(U,O,Z,x)});let X=C(U,O,Z,x);X.set("Content-Type",k),X.set("Accept-Ranges","bytes");let j=u.headers.get("Range");if(j&&u.method==="GET"){let B=j0(j,W);if(B==="invalid")return new Response(null,{status:416,headers:{"Content-Range":`bytes */${W}`}});if(B){let{start:Q,end:J}=B,I=J-Q+1,l=await D.slice(Q,J+1).arrayBuffer();return X.set("Content-Range",`bytes ${Q}-${J}/${W}`),X.set("Content-Length",String(I)),new Response(l,{status:206,headers:X})}}if(O.watch&&Z&&u.method==="GET"){if(O.compress&&T(k)){let Q=await A(u,X,{key:`${Y}\x00live`,size:W,mtimeMs:x},async()=>a.encode(S(await D.text())));if(Q)return Q}let B=S(await D.text());return X.set("Content-Length",String(Buffer.byteLength(B))),new Response(B,{status:200,headers:X})}if(O.compress&&u.method==="GET"&&T(k)&&W<2000000){let B=await A(u,X,{key:Y,size:W,mtimeMs:x},async()=>new Uint8Array(await D.arrayBuffer()));if(B)return B}return X.set("Content-Length",String(W)),new Response(D,{status:200,headers:X})}function C(u,$,O,G){let Y=new Headers;if(Y.set("ETag",u),Y.set("Last-Modified",new Date(G).toUTCString()),$.watch||!$.cache||O)return Y.set("Cache-Control","no-cache"),Y;return Y.set("Cache-Control","public, max-age=31536000, immutable"),Y}function j0(u,$){let O=/^bytes=(\d*)-(\d*)$/.exec(u.trim());if(!O)return null;let G=O[1],Y=O[2],D,W;if(G===""&&Y==="")return"invalid";if(G===""){let x=Number(Y);if(!Number.isFinite(x)||x<=0)return"invalid";D=Math.max(0,$-x),W=$-1}else D=Number(G),W=Y===""?$-1:Number(Y);if(!Number.isFinite(D)||!Number.isFinite(W)||D<0||W<D||D>=$)return"invalid";return W=Math.min(W,$-1),{start:D,end:W}}function K(u,$){if(!$.cors)return u;let O=new Headers(u.headers);return O.set("Access-Control-Allow-Origin","*"),O.set("Access-Control-Allow-Headers","*"),O.set("Access-Control-Allow-Methods","GET, HEAD, OPTIONS"),new Response(u.body,{status:u.status,statusText:u.statusText,headers:O})}function f(u,$){if(u!=="HEAD")return $;return new Response(null,{status:$.status,statusText:$.statusText,headers:$.headers})}var F="/__live.js",P="/__events",m=`(()=>{const e=new EventSource("${P}");e.onmessage=()=>location.reload();e.onerror=()=>{e.close();setTimeout(()=>location.reload(),1000)}})()`,d=`<script src="${F}"></script>`;function S(u){let $=u.toLowerCase().lastIndexOf("</body>");if($===-1)return u+d;return u.slice(0,$)+d+u.slice($)}function _0(){let u=new Set,$=new TextEncoder;function O(){let Y,D=new ReadableStream({start(W){Y=W,u.add(W),W.enqueue($.encode(`: connected
29
29
 
30
- `))},cancel(){u.delete(Z)}});return new Response(Q,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}function D(Z="reload"){let Q=$.encode(`data: ${Z}
30
+ `))},cancel(){u.delete(Y)}});return new Response(D,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}function G(Y="reload"){let D=$.encode(`data: ${Y}
31
31
 
32
- `);for(let W of u)try{W.enqueue(Q)}catch{u.delete(W)}}return{subscribe:O,broadcast:D}}var N={reset:"\x1B[0m",dim:"\x1B[2m",green:"\x1B[32m",yellow:"\x1B[33m",red:"\x1B[31m",cyan:"\x1B[36m",bold:"\x1B[1m"},F0=/[\x00-\x1f\x7f]/g;function P0(u){return u.replace(F0,"")}function I(u,$,O,D){if(D)return;let Z=$<300?`${N.green}\u2713${N.reset}`:$<400?`${N.yellow}\u25CB${N.reset}`:`${N.red}\u2717${N.reset}`;console.log(`${Z} ${N.dim}${u}${N.reset} ${$} ${P0(O)}`)}function f0(u,$=!1){if($)return;console.log(`${N.cyan}\u2502${N.reset} ${u}`)}function w0(){let u=I0();for(let $ of Object.values(u))for(let O of $??[])if(O.family==="IPv4"&&!O.internal)return O.address;return null}function S0(u,$,O,D){if(console.log(),console.log(` ${N.bold}deserved${N.reset} ${N.dim}serving${N.reset} ${$}`),console.log(` ${N.green}->${N.reset} ${N.cyan}${u}${N.reset}`),D)console.log(` ${N.green}->${N.reset} ${N.cyan}${D}${N.reset}`);if(O.length)console.log(` ${N.dim}${O.join(" ")}${N.reset}`);console.log()}function L0(u){if(u)return;console.log(`${N.yellow}*${N.reset} reload`)}async function T0(u){let $=process.platform;try{if($==="darwin")await Bun.spawn(["open",u],{stdout:"ignore",stderr:"ignore"}).exited;else if($==="win32")await Bun.spawn(["cmd","/c","start","",u],{stdout:"ignore",stderr:"ignore"}).exited;else await Bun.spawn(["xdg-open",u],{stdout:"ignore",stderr:"ignore"}).exited}catch{console.error(`Could not open browser for ${u}`)}}async function h(u){let $=E0(u.root),O=u.watch?_0():void 0,D=q({...u,root:$},O),Z=u.host==="localhost"?"127.0.0.1":u.host,Q;try{Q=Bun.serve({port:u.port,hostname:Z,fetch:async(K)=>{if(u.cors&&K.method==="OPTIONS")return new Response(null,{status:204,headers:{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Headers":"*","Access-Control-Allow-Methods":"GET, HEAD, OPTIONS"}});return D(K)}})}catch(K){console.error(`Error: could not bind to ${u.host}:${u.port}`),console.error(K instanceof Error?K.message:String(K)),process.exit(1)}let W=u.host==="0.0.0.0"||u.host==="::",B=`http://${W?"localhost":u.host}:${Q.port}`,X=W?w0():null,U=X?`http://${X}:${Q.port}`:void 0,G=[];if(u.spa)G.push("--spa");if(u.watch)G.push("--watch");if(u.cors)G.push("--cors");if(!u.dir)G.push("--no-dir");if(!u.compress)G.push("--no-compress");if(!u.cache)G.push("--no-cache");if(S0(B,V0(process.cwd(),$)||".",G,U),u.watch&&O){let K=null,J=j0($,{recursive:!0},(Y,x)=>{if(x?.includes("node_modules")||x?.startsWith("."))return;if(K)clearTimeout(K);K=setTimeout(()=>{L0(u.quiet),O.broadcast("reload")},80)});process.on("SIGINT",()=>{J.close(),Q.stop(),process.exit(0)}),f0("watching for changes",u.quiet)}if(u.open)await T0(B);return Q}function H0(){console.log(`
32
+ `);for(let W of u)try{W.enqueue(D)}catch{u.delete(W)}}return{subscribe:O,broadcast:G}}var N={reset:"\x1B[0m",dim:"\x1B[2m",green:"\x1B[32m",yellow:"\x1B[33m",red:"\x1B[31m",cyan:"\x1B[36m",bold:"\x1B[1m"},E0=/[\x00-\x1f\x7f]/g;function F0(u){return u.replace(E0,"")}function V(u,$,O,G){if(G)return;let Y=$<300?`${N.green}\u2713${N.reset}`:$<400?`${N.yellow}\u25CB${N.reset}`:`${N.red}\u2717${N.reset}`;console.log(`${Y} ${N.dim}${u}${N.reset} ${$} ${F0(O)}`)}function P0(u,$=!1){if($)return;console.log(`${N.cyan}\u2502${N.reset} ${u}`)}function S0(){let u=K0();for(let $ of Object.values(u))for(let O of $??[])if(O.family==="IPv4"&&!O.internal)return O.address;return null}function w0(u,$,O,G){if(console.log(),console.log(` ${N.bold}deserved${N.reset} ${N.dim}serving${N.reset} ${$}`),console.log(` ${N.green}->${N.reset} ${N.cyan}${u}${N.reset}`),G)console.log(` ${N.green}->${N.reset} ${N.cyan}${G}${N.reset}`);if(O.length)console.log(` ${N.dim}${O.join(" ")}${N.reset}`);console.log()}function L0(u){if(u)return;console.log(`${N.yellow}*${N.reset} reload`)}async function z0(u){let $=process.platform;try{if($==="darwin")await Bun.spawn(["open",u],{stdout:"ignore",stderr:"ignore"}).exited;else if($==="win32")await Bun.spawn(["cmd","/c","start","",u],{stdout:"ignore",stderr:"ignore"}).exited;else await Bun.spawn(["xdg-open",u],{stdout:"ignore",stderr:"ignore"}).exited}catch{console.error(`Could not open browser for ${u}`)}}async function h(u){let $=f0(u.root),O=u.watch?_0():void 0,G=q({...u,root:$},O),Y=u.host==="localhost"?"127.0.0.1":u.host,D;try{D=Bun.serve({port:u.port,hostname:Y,fetch:async(Q)=>{if(u.cors&&Q.method==="OPTIONS")return new Response(null,{status:204,headers:{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Headers":"*","Access-Control-Allow-Methods":"GET, HEAD, OPTIONS"}});return G(Q)}})}catch(Q){console.error(`Error: could not bind to ${u.host}:${u.port}`),console.error(Q instanceof Error?Q.message:String(Q)),process.exit(1)}let W=u.host==="0.0.0.0"||u.host==="::",k=`http://${W?"localhost":u.host}:${D.port}`,Z=W?S0():null,U=Z?`http://${Z}:${D.port}`:void 0,X=[];if(u.spa)X.push("--spa");if(u.watch)X.push("--watch");if(u.cors)X.push("--cors");if(!u.dir)X.push("--no-dir");if(!u.compress)X.push("--no-compress");if(u.cache)X.push("--cache");w0(k,V0(process.cwd(),$)||".",X,U);let j;if(u.watch&&O){let Q=null;j=I0($,{recursive:!0},(J,I)=>{if(I?.includes("node_modules")||I?.startsWith("."))return;if(Q)clearTimeout(Q);Q=setTimeout(()=>{L0(u.quiet),O.broadcast("reload")},80)}),P0("watching for changes",u.quiet)}let B=async()=>{j?.close(),await D.stop(),process.exit(0)};if(process.on("SIGINT",B),process.on("SIGTERM",B),u.open)await z0(k);return D}function v0(){console.log(`
33
33
  deserved - tiny Bun static file server
34
34
 
35
35
  Usage
@@ -44,9 +44,9 @@ ${Q}${Z}
44
44
  --cors Enable CORS
45
45
  --dir Enable directory listing (default)
46
46
  --no-dir Disable directory listing
47
- --cache Cache headers (default)
48
- --no-cache Disable cache headers
49
- --compress Gzip (default)
47
+ --cache Long-lived, immutable cache headers for assets
48
+ --no-cache Disable cache headers (default)
49
+ --compress zstd or gzip, negotiated per request (default)
50
50
  --no-compress Disable compression
51
51
  -q, --quiet Suppress request logs
52
52
  -h, --help Show help
@@ -57,4 +57,4 @@ ${Q}${Z}
57
57
  deserved dist --spa
58
58
  deserved dist --port 8080
59
59
  deserved docs --watch --open
60
- `)}function n0(u){let $=u.slice(2),O=".",D=3000,Z="localhost",Q=!1,W=!1,k=!1,B=!1,X=!0,U=!0,G=!0,K=!1;for(let J=0;J<$.length;J++){let Y=$[J];if(Y===void 0)break;if(Y==="-h"||Y==="--help")H0(),process.exit(0);if(Y==="-v"||Y==="--version")console.log(L),process.exit(0);if(Y==="-s"||Y==="--spa"){Q=!0;continue}if(Y==="-w"||Y==="--watch"){W=!0;continue}if(Y==="-o"||Y==="--open"){k=!0;continue}if(Y==="--cors"){B=!0;continue}if(Y==="--dir"){X=!0;continue}if(Y==="--no-dir"){X=!1;continue}if(Y==="--cache"){U=!0;continue}if(Y==="--no-cache"){U=!1;continue}if(Y==="--compress"){G=!0;continue}if(Y==="--no-compress"){G=!1;continue}if(Y==="-q"||Y==="--quiet"){K=!0;continue}if(Y==="-p"||Y==="--port"){let x=$[++J];if(!x||x.startsWith("-"))F(`Missing value for ${Y}`);let V=Number(x);if(!Number.isInteger(V)||V<0||V>65535)F(`Invalid port: ${x}`);D=V;continue}if(Y==="-H"||Y==="--host"){let x=$[++J];if(!x||x.startsWith("-"))F(`Missing value for ${Y}`);Z=x;continue}if(Y.startsWith("--port=")){let x=Number(Y.slice(7));if(!Number.isInteger(x)||x<0||x>65535)F(`Invalid port: ${Y}`);D=x;continue}if(Y.startsWith("--host=")){Z=Y.slice(7);continue}if(Y.startsWith("-"))F(`Unknown option: ${Y}`);O=Y}return{root:M0(O),port:D,host:Z,spa:Q,watch:W,open:k,cors:B,dir:X,cache:U,compress:G,quiet:K}}function F(u){console.error(`Error: ${u}`),console.error("Run deserved --help."),process.exit(1)}if(import.meta.main){let u=n0(process.argv);await h(u)}export{n0 as parseArgs};
60
+ `)}function M0(u){let $=u.slice(2),O=".",G=3000,Y="localhost",D=!1,W=!1,x=!1,k=!1,Z=!0,U=!1,X=!0,j=!1;for(let B=0;B<$.length;B++){let Q=$[B];if(Q===void 0)break;if(Q==="-h"||Q==="--help")v0(),process.exit(0);if(Q==="-v"||Q==="--version")console.log(L),process.exit(0);if(Q==="-s"||Q==="--spa"){D=!0;continue}if(Q==="-w"||Q==="--watch"){W=!0;continue}if(Q==="-o"||Q==="--open"){x=!0;continue}if(Q==="--cors"){k=!0;continue}if(Q==="--dir"){Z=!0;continue}if(Q==="--no-dir"){Z=!1;continue}if(Q==="--cache"){U=!0;continue}if(Q==="--no-cache"){U=!1;continue}if(Q==="--compress"){X=!0;continue}if(Q==="--no-compress"){X=!1;continue}if(Q==="-q"||Q==="--quiet"){j=!0;continue}if(Q==="-p"||Q==="--port"){let J=$[++B];if(!J||J.startsWith("-"))E(`Missing value for ${Q}`);let I=Number(J);if(!Number.isInteger(I)||I<0||I>65535)E(`Invalid port: ${J}`);G=I;continue}if(Q==="-H"||Q==="--host"){let J=$[++B];if(!J||J.startsWith("-"))E(`Missing value for ${Q}`);Y=J;continue}if(Q.startsWith("--port=")){let J=Number(Q.slice(7));if(!Number.isInteger(J)||J<0||J>65535)E(`Invalid port: ${Q}`);G=J;continue}if(Q.startsWith("--host=")){Y=Q.slice(7);continue}if(Q.startsWith("-"))E(`Unknown option: ${Q}`);O=Q}return{root:T0(O),port:G,host:Y,spa:D,watch:W,open:x,cors:k,dir:Z,cache:U,compress:X,quiet:j}}function E(u){console.error(`Error: ${u}`),console.error("Run deserved --help."),process.exit(1)}if(import.meta.main){let u=M0(process.argv);await h(u)}export{M0 as parseArgs};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deserved",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "A tiny Bun static file server: SPA, live reload, zero dependencies",
5
5
  "license": "MIT",
6
6
  "type": "module",