@noy4/docserve 0.3.0 → 0.3.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.html +6 -5
- package/package.json +1 -1
- package/server/files.mjs +1 -1
- package/server/index.mjs +14 -6
- package/server/watch.mjs +2 -2
package/index.html
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
6
|
<title>__DOCS_DIR_NAME__</title>
|
|
7
|
-
<link rel="icon"
|
|
7
|
+
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='hsl(__FAVICON_HUE__ 70%25 52%)'%3E%3Cpath d='M6.1 10L4 18V8h17a2 2 0 0 0-2-2h-7l-2-2H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h15c.9 0 1.7-.6 1.9-1.5l2.3-8.5zM19 18H6l1.6-6h13z'/%3E%3C/svg%3E">
|
|
8
8
|
<style>
|
|
9
9
|
:root {
|
|
10
10
|
--bg: #0f1117;
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
--accent: #6c8cff;
|
|
16
16
|
--green: #4ade80;
|
|
17
17
|
--btn-bg: rgba(15, 17, 23, 0.82);
|
|
18
|
+
--folder-hue: __FAVICON_HUE__;
|
|
18
19
|
}
|
|
19
20
|
@media (prefers-color-scheme: light) {
|
|
20
21
|
:root {
|
|
@@ -45,11 +46,11 @@
|
|
|
45
46
|
font-size: 1.6rem;
|
|
46
47
|
margin-bottom: 8px;
|
|
47
48
|
}
|
|
48
|
-
h1 svg { width: 26px; height: 26px; color: var(--
|
|
49
|
+
h1 svg { width: 26px; height: 26px; color: hsl(var(--folder-hue) 70% 52%); flex-shrink: 0; }
|
|
49
50
|
.sub { color: var(--muted); margin-bottom: 32px; font-size: 0.9rem; }
|
|
50
51
|
.sub b { color: var(--green); }
|
|
51
52
|
|
|
52
|
-
.sub code {
|
|
53
|
+
.sub code, .empty code {
|
|
53
54
|
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
|
54
55
|
font-size: 0.78rem;
|
|
55
56
|
background: var(--card);
|
|
@@ -166,7 +167,7 @@
|
|
|
166
167
|
|
|
167
168
|
<body>
|
|
168
169
|
<h1>
|
|
169
|
-
<svg viewBox="0 0 24 24" fill="
|
|
170
|
+
<svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M6.1 10L4 18V8h17a2 2 0 0 0-2-2h-7l-2-2H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h15c.9 0 1.7-.6 1.9-1.5l2.3-8.5zM19 18H6l1.6-6h13z"/></svg>
|
|
170
171
|
__DOCS_DIR_NAME__
|
|
171
172
|
</h1>
|
|
172
173
|
<p class="sub">
|
|
@@ -281,7 +282,7 @@
|
|
|
281
282
|
el("button", {
|
|
282
283
|
type: "button",
|
|
283
284
|
class: "copy-btn",
|
|
284
|
-
title: file.
|
|
285
|
+
title: file.filePath,
|
|
285
286
|
"aria-label": "Copy file path",
|
|
286
287
|
html: COPY_SVG + CHECK_SVG,
|
|
287
288
|
onclick: async (e) => {
|
package/package.json
CHANGED
package/server/files.mjs
CHANGED
|
@@ -63,7 +63,7 @@ export async function apiFiles(docsDir) {
|
|
|
63
63
|
files.push({
|
|
64
64
|
title: title ?? basename(full),
|
|
65
65
|
url: `/${rel}`,
|
|
66
|
-
|
|
66
|
+
filePath: full,
|
|
67
67
|
favicon: favicon ?? undefined,
|
|
68
68
|
created: date ?? created.get(key) ?? Math.floor(st.birthtimeMs / 1000),
|
|
69
69
|
modified: modified.get(key) ?? Math.floor(st.mtimeMs / 1000),
|
package/server/index.mjs
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// │ │ ├─ /api/files ──▶ apiFiles() │
|
|
7
7
|
// │ │ ├─ /, /index.html ──▶ gallery template │
|
|
8
8
|
// │ │ └─ other paths ──▶ raw files ──▶ injectIntoHtml() │
|
|
9
|
-
// │ ├─ WebSocketServer("/__reload") ◀─
|
|
9
|
+
// │ ├─ WebSocketServer("/__reload") ◀─ FileWatcher │
|
|
10
10
|
// │ └─ listen ──▶ listener.start() + writeState() │
|
|
11
11
|
// └────────────────────────────────────────────────────────┘
|
|
12
12
|
import { createServer } from "node:http"
|
|
@@ -18,7 +18,7 @@ import { clearState, writeState } from "./state.mjs"
|
|
|
18
18
|
import { apiFiles, isWithin, normalizedRelative } from "./files.mjs"
|
|
19
19
|
import { injectIntoHtml, WS_PATH } from "./inject.mjs"
|
|
20
20
|
import { WebSocketServer } from "./websocket.mjs"
|
|
21
|
-
import {
|
|
21
|
+
import { FileWatcher } from "./watch.mjs"
|
|
22
22
|
|
|
23
23
|
const MAX_PORT_TRIES = 20 // on conflict, try the next port up to 20 times
|
|
24
24
|
const INDEX_TEMPLATE = resolve(import.meta.dirname, "..", "index.html")
|
|
@@ -49,14 +49,14 @@ const MIME = {
|
|
|
49
49
|
|
|
50
50
|
export async function runServer({ docsDir, port: initialPort, open = false }) {
|
|
51
51
|
const wss = new WebSocketServer({ path: WS_PATH })
|
|
52
|
-
const
|
|
52
|
+
const fileWatcher = new FileWatcher({ wss, docsDir, templatePath: INDEX_TEMPLATE })
|
|
53
53
|
let port = initialPort
|
|
54
54
|
|
|
55
55
|
const server = createServer(createNodeServerAdapter(createHandler({ docsDir })))
|
|
56
56
|
wss.attach(server)
|
|
57
57
|
|
|
58
58
|
const cleanup = () => {
|
|
59
|
-
|
|
59
|
+
fileWatcher.close()
|
|
60
60
|
wss.close()
|
|
61
61
|
clearState()
|
|
62
62
|
server.closeAllConnections()
|
|
@@ -75,13 +75,13 @@ export async function runServer({ docsDir, port: initialPort, open = false }) {
|
|
|
75
75
|
}
|
|
76
76
|
})
|
|
77
77
|
server.listen(port, "127.0.0.1", async () => {
|
|
78
|
-
await
|
|
78
|
+
await fileWatcher.start()
|
|
79
79
|
const state = { pid: process.pid, port, url: `http://localhost:${port}/`, docsDir }
|
|
80
80
|
writeState(state)
|
|
81
81
|
console.log(state.url)
|
|
82
82
|
if (open && process.platform === "darwin") exec(`open ${state.url}`)
|
|
83
83
|
})
|
|
84
|
-
return { server, wss,
|
|
84
|
+
return { server, wss, fileWatcher, close: cleanup }
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
// --- Node server adapter ---
|
|
@@ -164,6 +164,7 @@ async function galleryResponse(docsDir) {
|
|
|
164
164
|
html = html
|
|
165
165
|
.replaceAll("__DOCS_DIR_NAME__", escapeHtml(basename(docsDir) || docsDir))
|
|
166
166
|
.replaceAll("__DOCS_DIR__", escapeHtml(displayPath(docsDir)))
|
|
167
|
+
.replaceAll("__FAVICON_HUE__", String(faviconHue(docsDir)))
|
|
167
168
|
return new Response(html, { headers: { "content-type": "text/html; charset=utf-8" } })
|
|
168
169
|
} catch {
|
|
169
170
|
return new Response("Gallery template missing", { status: 500 })
|
|
@@ -176,6 +177,13 @@ function displayPath(p) {
|
|
|
176
177
|
return p.startsWith(`${home}/`) ? `~${p.slice(home.length)}` : p
|
|
177
178
|
}
|
|
178
179
|
|
|
180
|
+
// Deterministic hue for the folder favicon: the same path always yields the same color.
|
|
181
|
+
function faviconHue(s) {
|
|
182
|
+
let h = 0
|
|
183
|
+
for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) | 0
|
|
184
|
+
return ((h % 360) + 360) % 360
|
|
185
|
+
}
|
|
186
|
+
|
|
179
187
|
function escapeHtml(s) {
|
|
180
188
|
return s.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">")
|
|
181
189
|
}
|
package/server/watch.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// FileWatcher: watch docsDir recursively for *.html changes and broadcast
|
|
2
2
|
// debounced batches over the reload socket. The gallery template is watched
|
|
3
3
|
// too; its edits set templateChanged so gallery tabs pick them up even though
|
|
4
4
|
// nothing in docsDir changed.
|
|
@@ -12,7 +12,7 @@ import { isWithin, listHtmlFiles, normalizedRelative, shouldIgnore } from "./fil
|
|
|
12
12
|
const DEBOUNCE_MS = 50 // like livePreview.previewDebounceDelay
|
|
13
13
|
const TEMPLATE_TOUCH = "\u0000template" // sentinel: never a real docsDir-relative path
|
|
14
14
|
|
|
15
|
-
export class
|
|
15
|
+
export class FileWatcher {
|
|
16
16
|
constructor(options = {}) {
|
|
17
17
|
this.wss = options.wss
|
|
18
18
|
this.docsDir = options.docsDir
|