deserved 0.7.0 → 0.7.1

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/index.d.ts CHANGED
@@ -35,19 +35,7 @@ export type Handler = {
35
35
  compressedCacheBytes(): number;
36
36
  };
37
37
  /** The settings `serve()` and the CLI start from. */
38
- export declare const DEFAULT_OPTIONS: {
39
- root: string;
40
- port: number;
41
- host: string;
42
- spa: false;
43
- watch: false;
44
- open: false;
45
- cors: false;
46
- dir: true;
47
- cache: false;
48
- compress: true;
49
- quiet: false;
50
- };
38
+ export declare const DEFAULT_OPTIONS: Options;
51
39
  /** The server couldn't listen on the host and port. */
52
40
  export declare class BindError extends Error {
53
41
  constructor(host: string, port: number, options?: ErrorOptions);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deserved",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "A tiny Bun static file server: SPA, live reload, zero dependencies.",
5
5
  "license": "MIT",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
package/src/cli.ts CHANGED
@@ -149,7 +149,16 @@ function fail(msg: string): never {
149
149
  if (import.meta.main) {
150
150
  const opts = parseArgs(process.argv);
151
151
 
152
- let handle: Awaited<ReturnType<typeof startServer>>;
152
+ // Registered before the server starts, so a signal sent once the banner
153
+ // prints, while `--watch` is still setting up, still shuts it down.
154
+ let handle: Awaited<ReturnType<typeof startServer>> | undefined;
155
+ const shutdown = async () => {
156
+ await handle?.stop();
157
+ process.exit(0);
158
+ };
159
+ process.on("SIGINT", shutdown);
160
+ process.on("SIGTERM", shutdown);
161
+
153
162
  try {
154
163
  handle = await startServer(opts, { portExplicit: opts.portExplicit });
155
164
  } catch (err) {
@@ -167,11 +176,4 @@ if (import.meta.main) {
167
176
  }
168
177
  throw err;
169
178
  }
170
-
171
- const shutdown = async () => {
172
- await handle.stop();
173
- process.exit(0);
174
- };
175
- process.on("SIGINT", shutdown);
176
- process.on("SIGTERM", shutdown);
177
179
  }
package/src/handlers.ts CHANGED
@@ -594,16 +594,17 @@ export function createHandler(
594
594
  const res = new Response("Internal Server Error", { status: 500 });
595
595
  return finish(method, rawPathname, opts, res);
596
596
  }
597
- } as Handler;
598
-
599
- handle.invalidateResolutionCache = () => {
600
- resolution.invalidate();
601
- ctx.notFound = null;
602
597
  };
603
- handle.invalidateCompressedCache = compression.invalidate;
604
- handle.resolutionCacheSize = resolution.size;
605
- handle.compressedCacheBytes = compression.bytes;
606
- return handle;
598
+
599
+ return Object.assign(handle, {
600
+ invalidateResolutionCache: () => {
601
+ resolution.invalidate();
602
+ ctx.notFound = null;
603
+ },
604
+ invalidateCompressedCache: compression.invalidate,
605
+ resolutionCacheSize: resolution.size,
606
+ compressedCacheBytes: compression.bytes,
607
+ });
607
608
  }
608
609
 
609
610
  function finish(
@@ -761,7 +762,7 @@ type CompressedEntry = {
761
762
  // and every touch (read or write) re-inserts its key, so the front of the
762
763
  // map is always the least-recently-used entry -- evicting from there
763
764
  // gives LRU behavior without a separate linked list.
764
- export const COMPRESSED_CACHE_BYTE_BUDGET = 64 * 1024 * 1024;
765
+ export const COMPRESSED_CACHE_BYTE_BUDGET: number = 64 * 1024 * 1024;
765
766
 
766
767
  // Bun 1.4's CompressionStream is native, so gzip runs off the main thread
767
768
  // the same way Bun.zstdCompress does.
package/src/live.ts CHANGED
@@ -7,7 +7,7 @@ export const EVENTS_PATH = "/__events";
7
7
  // the source closes for good, so reload once to pick up the new server.
8
8
  // A source that never connected (a handler with no hub) stays put, so
9
9
  // it can't loop.
10
- export const LIVE_SCRIPT = `(()=>{let down=false;const e=new EventSource("${EVENTS_PATH}");e.onopen=()=>{if(down)location.reload()};e.onmessage=(m)=>{if(m.data==="css"){for(const l of document.querySelectorAll('link[rel="stylesheet"]')){const h=l.href.split("?")[0];l.href=h+"?t="+Date.now()}}else{location.reload()}};e.onerror=()=>{if(down&&e.readyState===2)location.reload();down=true}})()`;
10
+ export const LIVE_SCRIPT: string = `(()=>{let down=false;const e=new EventSource("${EVENTS_PATH}");e.onopen=()=>{if(down)location.reload()};e.onmessage=(m)=>{if(m.data==="css"){for(const l of document.querySelectorAll('link[rel="stylesheet"]')){const h=l.href.split("?")[0];l.href=h+"?t="+Date.now()}}else{location.reload()}};e.onerror=()=>{if(down&&e.readyState===2)location.reload();down=true}})()`;
11
11
 
12
12
  const INJECT = `<script src="${LIVE_PATH}"></script>`;
13
13
 
@@ -23,7 +23,11 @@ export const SSE_HEADERS = {
23
23
  Connection: "keep-alive",
24
24
  };
25
25
 
26
- export function createSseHub() {
26
+ export function createSseHub(): {
27
+ subscribe: () => Response;
28
+ broadcast: (data?: string) => void;
29
+ close: () => void;
30
+ } {
27
31
  const clients = new Set<ReadableStreamDefaultController<Uint8Array>>();
28
32
  const encoder = new TextEncoder();
29
33
  const PING = encoder.encode(": ping\n\n");
@@ -60,7 +64,7 @@ export function createSseHub() {
60
64
  return new Response(stream, { headers: SSE_HEADERS });
61
65
  }
62
66
 
63
- function broadcast(data = "reload") {
67
+ function broadcast(data = "reload"): void {
64
68
  const payload = encoder.encode(`data: ${data}\n\n`);
65
69
  for (const c of clients) {
66
70
  try {
@@ -71,7 +75,7 @@ export function createSseHub() {
71
75
  }
72
76
  }
73
77
 
74
- function close() {
78
+ function close(): void {
75
79
  clearInterval(heartbeat);
76
80
  for (const c of clients) {
77
81
  try {
package/src/log.ts CHANGED
@@ -20,10 +20,10 @@ const c = {
20
20
  bold: ansi(1),
21
21
  } as const;
22
22
 
23
- // eslint-disable-next-line no-control-regex -- strip control chars from logs
24
- const CONTROL_CHARS = /[\x00-\x1f\x7f]/g;
25
- // eslint-disable-next-line no-control-regex -- cheap pre-check so the common clean-path case skips replace()
26
- const HAS_CONTROL = /[\x00-\x1f\x7f]/;
23
+ // C0 and C1 controls, since either can start a terminal escape (ESC, CSI).
24
+ const CONTROL_CHARS = /\p{Cc}/gu;
25
+ // A pre-check, so a clean path, the common case, skips the replace.
26
+ const HAS_CONTROL = /\p{Cc}/u;
27
27
 
28
28
  function sanitizeForLog(s: string): string {
29
29
  return HAS_CONTROL.test(s) ? s.replace(CONTROL_CHARS, "") : s;
@@ -56,7 +56,7 @@ export function logRequest(
56
56
  status: number,
57
57
  path: string,
58
58
  quiet: boolean,
59
- ) {
59
+ ): void {
60
60
  if (quiet) return;
61
61
  const icon = status < 300 ? ICON_OK : status < 400 ? ICON_REDIRECT : ICON_ERR;
62
62
  pendingLines.push(
@@ -65,7 +65,7 @@ export function logRequest(
65
65
  scheduleFlush();
66
66
  }
67
67
 
68
- export function logInfo(msg: string, quiet = false) {
68
+ export function logInfo(msg: string, quiet = false): void {
69
69
  if (quiet) return;
70
70
  console.log(`${c.cyan}│${c.reset} ${msg}`);
71
71
  }
@@ -75,7 +75,7 @@ export function logBanner(
75
75
  root: string,
76
76
  flags: string[],
77
77
  networkUrl?: string,
78
- ) {
78
+ ): void {
79
79
  console.log();
80
80
  console.log(
81
81
  ` ${c.bold}deserved${c.reset} ${c.dim}serving${c.reset} ${root}`,
@@ -90,7 +90,7 @@ export function logBanner(
90
90
  console.log();
91
91
  }
92
92
 
93
- export function logReload(quiet: boolean, kind: "css" | "reload") {
93
+ export function logReload(quiet: boolean, kind: "css" | "reload"): void {
94
94
  if (quiet) return;
95
95
  console.log(`${c.yellow}*${c.reset} ${kind}`);
96
96
  }
package/src/options.ts CHANGED
@@ -14,7 +14,7 @@ export type Options = {
14
14
  };
15
15
 
16
16
  /** The settings `serve()` and the CLI start from. */
17
- export const DEFAULT_OPTIONS = {
17
+ export const DEFAULT_OPTIONS: Options = {
18
18
  root: ".",
19
19
  port: 3000,
20
20
  host: "localhost",
@@ -26,4 +26,4 @@ export const DEFAULT_OPTIONS = {
26
26
  cache: false,
27
27
  compress: true,
28
28
  quiet: false,
29
- } satisfies Options;
29
+ };