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.
- package/README.md +2 -2
- package/cli.js +10 -10
- 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
|
-
| `--
|
|
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
|
-
-
|
|
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
|
|
4
|
-
`),
|
|
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
|
-
${
|
|
24
|
+
${D}${Y}
|
|
25
25
|
</ul>
|
|
26
26
|
</body>
|
|
27
27
|
</html>
|
|
28
|
-
`}function
|
|
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(
|
|
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(
|
|
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
|
|
48
|
-
--no-cache Disable cache headers
|
|
49
|
-
--compress
|
|
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
|
|
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};
|