deserved 0.6.1 → 0.7.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/index.d.ts +16 -7
- package/package.json +1 -1
- package/src/cli.ts +2 -1
- package/src/handlers.ts +21 -26
- package/src/index.ts +3 -2
- package/src/live.ts +4 -0
- package/src/log.ts +5 -6
package/index.d.ts
CHANGED
|
@@ -13,13 +13,21 @@ export type Options = {
|
|
|
13
13
|
compress: boolean;
|
|
14
14
|
quiet: boolean;
|
|
15
15
|
};
|
|
16
|
-
|
|
16
|
+
/**
|
|
17
|
+
* The live-reload hub `serve` creates under `watch`, which a handler
|
|
18
|
+
* streams change events from.
|
|
19
|
+
*/
|
|
20
|
+
export type Hub = ReturnType<typeof createSseHub>;
|
|
17
21
|
declare function createSseHub(): {
|
|
18
22
|
subscribe: () => Response;
|
|
19
23
|
broadcast: (data?: string) => void;
|
|
20
24
|
close: () => void;
|
|
21
25
|
};
|
|
22
|
-
|
|
26
|
+
/**
|
|
27
|
+
* The request handler {@link createHandler} returns: a `fetch` for
|
|
28
|
+
* `Bun.serve`, with its caches' sizes and a way to clear each.
|
|
29
|
+
*/
|
|
30
|
+
export type Handler = {
|
|
23
31
|
(req: Request): Promise<Response>;
|
|
24
32
|
invalidateResolutionCache(): void;
|
|
25
33
|
invalidateCompressedCache(): void;
|
|
@@ -48,6 +56,11 @@ export declare class BindError extends Error {
|
|
|
48
56
|
export declare class RootError extends Error {
|
|
49
57
|
constructor(message: string, options?: ErrorOptions);
|
|
50
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Starts a server for `options.root`, over the defaults; a bind failure
|
|
61
|
+
* rejects instead of exiting.
|
|
62
|
+
*/
|
|
63
|
+
export declare function serve(options?: Partial<Options>): Promise<ServerHandle>;
|
|
51
64
|
/** A running server: where it listens, and `stop()` to close it. */
|
|
52
65
|
export type ServerHandle = {
|
|
53
66
|
port: number;
|
|
@@ -55,8 +68,4 @@ export type ServerHandle = {
|
|
|
55
68
|
url: string;
|
|
56
69
|
stop: () => Promise<void>;
|
|
57
70
|
};
|
|
58
|
-
|
|
59
|
-
* Starts a server for `options.root`, over the defaults; a bind failure
|
|
60
|
-
* rejects instead of exiting.
|
|
61
|
-
*/
|
|
62
|
-
export declare function serve(options?: Partial<Options>): Promise<ServerHandle>;
|
|
71
|
+
export {};
|
package/package.json
CHANGED
package/src/cli.ts
CHANGED
|
@@ -100,7 +100,8 @@ export function parseArgs(
|
|
|
100
100
|
continue;
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
//
|
|
103
|
+
// Only the long form carries its value inline (`--port=8080`);
|
|
104
|
+
// otherwise the value is the next arg (`-p 8080`).
|
|
104
105
|
const eq = a.startsWith("--") ? a.indexOf("=") : -1;
|
|
105
106
|
const flag = eq === -1 ? a : a.slice(0, eq);
|
|
106
107
|
const key = VALUE_FLAGS.get(flag);
|
package/src/handlers.ts
CHANGED
|
@@ -22,7 +22,11 @@ type ResolvedFile = {
|
|
|
22
22
|
kind: "file" | "html-ext" | "dir-index";
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
/**
|
|
26
|
+
* The request handler {@link createHandler} returns: a `fetch` for
|
|
27
|
+
* `Bun.serve`, with its caches' sizes and a way to clear each.
|
|
28
|
+
*/
|
|
29
|
+
export type Handler = {
|
|
26
30
|
(req: Request): Promise<Response>;
|
|
27
31
|
invalidateResolutionCache(): void;
|
|
28
32
|
invalidateCompressedCache(): void;
|
|
@@ -175,8 +179,8 @@ export function shouldSpaFallback(pathname: string): boolean {
|
|
|
175
179
|
|
|
176
180
|
const WELL_KNOWN = "/.well-known";
|
|
177
181
|
|
|
178
|
-
//
|
|
179
|
-
// /.well-known/ is the one dot-directory
|
|
182
|
+
// security: dotfiles (.env, .git/) stay private even when serving a
|
|
183
|
+
// project root; /.well-known/ is the one dot-directory meant to be public.
|
|
180
184
|
export function isHiddenPath(pathname: string): boolean {
|
|
181
185
|
const from =
|
|
182
186
|
pathname.startsWith(WELL_KNOWN) &&
|
|
@@ -245,9 +249,9 @@ function markRecent<T>(map: Map<string, T>, key: string, entry: T): void {
|
|
|
245
249
|
map.set(key, entry);
|
|
246
250
|
}
|
|
247
251
|
|
|
248
|
-
//
|
|
249
|
-
// (e.g. adding about.html for a pathname previously cached as a 404)
|
|
250
|
-
//
|
|
252
|
+
// Without --watch nothing reports a newly created or deleted candidate
|
|
253
|
+
// (e.g. adding about.html for a pathname previously cached as a 404),
|
|
254
|
+
// so this window bounds how long it stays unseen.
|
|
251
255
|
export const RESOLUTION_TTL_MS = 500;
|
|
252
256
|
|
|
253
257
|
type Cached<T> = { value: T | null; at: number };
|
|
@@ -285,9 +289,8 @@ function createResolutionCache(
|
|
|
285
289
|
const files = new Map<string, Cached<ResolvedFile>>();
|
|
286
290
|
const dirs = new Map<string, Cached<string>>();
|
|
287
291
|
|
|
288
|
-
//
|
|
289
|
-
//
|
|
290
|
-
// re-probe.
|
|
292
|
+
// A fresh cached winner still goes through `revalidate`, since it can be
|
|
293
|
+
// deleted or change kind; its null forces a re-probe.
|
|
291
294
|
function lookup<T>(
|
|
292
295
|
map: Map<string, Cached<T>>,
|
|
293
296
|
pathname: string,
|
|
@@ -794,13 +797,8 @@ type PendingCompression = {
|
|
|
794
797
|
promise: Promise<Blob | null>;
|
|
795
798
|
};
|
|
796
799
|
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
* createResolutionCache above, this keeps the (potentially large)
|
|
800
|
-
* compressed byte buffers it retains reclaimable as soon as the handler
|
|
801
|
-
* itself is no longer referenced, instead of pinning them for the life of
|
|
802
|
-
* the process the way a module-level cache would.
|
|
803
|
-
*/
|
|
800
|
+
// Scoped to one handler, so its buffers are freed with the handler, where
|
|
801
|
+
// a module-level cache would pin them for the life of the process.
|
|
804
802
|
function createCompressionCache() {
|
|
805
803
|
const cache = new Map<string, CompressedEntry>();
|
|
806
804
|
let bytes = 0;
|
|
@@ -825,13 +823,9 @@ function createCompressionCache() {
|
|
|
825
823
|
}
|
|
826
824
|
}
|
|
827
825
|
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
* watch-injected HTML. loadRaw runs on miss. Concurrent misses for the
|
|
832
|
-
* same (key, encoding, version) single-flight onto one in-flight
|
|
833
|
-
* compression.
|
|
834
|
-
*/
|
|
826
|
+
// Keyed by (key, encoding, size, mtimeMs), so a caller must pick a
|
|
827
|
+
// distinct key when one path can yield different bytes, as watch-injected
|
|
828
|
+
// HTML does. Concurrent misses for one entry share one compression.
|
|
835
829
|
const getCompressed: GetCompressed = async (resolved, encoding, loadRaw) => {
|
|
836
830
|
const cacheKey = `${resolved.key}:${encoding}`;
|
|
837
831
|
const cached = cache.get(cacheKey);
|
|
@@ -1022,9 +1016,10 @@ function httpDate(mtimeMs: number): string {
|
|
|
1022
1016
|
return lastHttpDate;
|
|
1023
1017
|
}
|
|
1024
1018
|
|
|
1025
|
-
//
|
|
1026
|
-
// ~205ns vs ~5ns for an object literal, and
|
|
1027
|
-
// accepts either. Response still builds a
|
|
1019
|
+
// perf: a plain object, not Headers: `new Headers()` + 5 `set()` calls
|
|
1020
|
+
// costs ~205ns vs ~5ns for an object literal, and
|
|
1021
|
+
// `new Response(..., {headers})` accepts either. Response still builds a
|
|
1022
|
+
// real Headers instance from it.
|
|
1028
1023
|
export function baseHeaders(
|
|
1029
1024
|
etag: string,
|
|
1030
1025
|
opts: Options,
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export { createHandler } from "./handlers";
|
|
1
|
+
export { createHandler, type Handler } from "./handlers";
|
|
2
|
+
export type { Hub } from "./live";
|
|
2
3
|
export { DEFAULT_OPTIONS, type Options } from "./options";
|
|
3
|
-
export { BindError, RootError, type ServerHandle
|
|
4
|
+
export { BindError, RootError, serve, type ServerHandle } from "./server";
|
package/src/live.ts
CHANGED
|
@@ -84,6 +84,10 @@ export function createSseHub() {
|
|
|
84
84
|
return { subscribe, broadcast, close };
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
+
/**
|
|
88
|
+
* The live-reload hub `serve` creates under `watch`, which a handler
|
|
89
|
+
* streams change events from.
|
|
90
|
+
*/
|
|
87
91
|
export type Hub = ReturnType<typeof createSseHub>;
|
|
88
92
|
|
|
89
93
|
export function isIgnoredWatchPath(filename: string): boolean {
|
package/src/log.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
1
|
+
// spec: https://no-color.org (NO_COLOR); piped output (CI logs, `| tee`)
|
|
2
|
+
// is plain text too.
|
|
3
3
|
export function shouldColor(
|
|
4
4
|
isTTY: boolean | undefined,
|
|
5
5
|
env: Record<string, string | undefined>,
|
|
@@ -35,10 +35,9 @@ const ICON_ERR = `${c.red}✗${c.reset}`;
|
|
|
35
35
|
const DIM = c.dim;
|
|
36
36
|
const RESET = c.reset;
|
|
37
37
|
|
|
38
|
-
//
|
|
39
|
-
// console.log/write per request costs a syscall
|
|
40
|
-
//
|
|
41
|
-
// microtask queue.
|
|
38
|
+
// Lines queue here and flush once per tick, at the end of the current
|
|
39
|
+
// microtask queue, because a console.log/write per request costs a syscall
|
|
40
|
+
// each, which adds up under load.
|
|
42
41
|
let pendingLines: string[] = [];
|
|
43
42
|
|
|
44
43
|
function scheduleFlush(): void {
|