@noy4/docserve 0.3.0 → 0.3.2
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 +31 -3
- package/server/index.mjs +25 -8
- 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),
|
|
@@ -78,6 +78,27 @@ export async function apiFiles(docsDir) {
|
|
|
78
78
|
// files that changed.
|
|
79
79
|
const metaCache = new Map() // "path:mtimeMs" -> { title, date, favicon }
|
|
80
80
|
|
|
81
|
+
// Decode <title> entities so cards read "A & B", not "A & B".
|
|
82
|
+
const ENTITIES = {
|
|
83
|
+
amp: "&",
|
|
84
|
+
lt: "<",
|
|
85
|
+
gt: ">",
|
|
86
|
+
quot: '"',
|
|
87
|
+
apos: "'",
|
|
88
|
+
nbsp: "\u00a0",
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function decodeEntities(s) {
|
|
92
|
+
return s.replace(/&(#[xX]?[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);/g, (m, ref) => {
|
|
93
|
+
if (ref[0] === "#") {
|
|
94
|
+
const hex = /[xX]/.test(ref[1])
|
|
95
|
+
const cp = parseInt(hex ? ref.slice(2) : ref.slice(1), hex ? 16 : 10)
|
|
96
|
+
return cp > 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : m
|
|
97
|
+
}
|
|
98
|
+
return ENTITIES[ref.toLowerCase()] ?? m
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
81
102
|
async function metaOf(full, mtimeMs, docsDir) {
|
|
82
103
|
const key = `${full}:${mtimeMs}`
|
|
83
104
|
const cached = metaCache.get(key)
|
|
@@ -88,7 +109,10 @@ async function metaOf(full, mtimeMs, docsDir) {
|
|
|
88
109
|
}
|
|
89
110
|
|
|
90
111
|
async function extractMeta(full, docsDir) {
|
|
91
|
-
const findTitle = (text) =>
|
|
112
|
+
const findTitle = (text) => {
|
|
113
|
+
const raw = text.match(/<title>([^<]*)<\/title>/)?.[1]?.trim()
|
|
114
|
+
return raw == null ? null : decodeEntities(raw)
|
|
115
|
+
}
|
|
92
116
|
const findDate = (text) => {
|
|
93
117
|
for (const tag of text.match(/<meta\b[^>]*>/gi) ?? []) {
|
|
94
118
|
if (!/\bname\s*=\s*["']created_at["']/i.test(tag)) continue
|
|
@@ -131,7 +155,11 @@ async function extractMeta(full, docsDir) {
|
|
|
131
155
|
}
|
|
132
156
|
// Titles and meta tags live in <head>; read only the first 4 KB.
|
|
133
157
|
const head = await read(4096, 0)
|
|
134
|
-
let meta = {
|
|
158
|
+
let meta = {
|
|
159
|
+
title: findTitle(head),
|
|
160
|
+
date: findDate(head),
|
|
161
|
+
favicon: findFavicon(head),
|
|
162
|
+
}
|
|
135
163
|
if (meta.title === null || meta.date === null || meta.favicon === null) {
|
|
136
164
|
// Fallback for documents with a long preamble (inline favicon etc.)
|
|
137
165
|
const text = await read(1 << 20, 0)
|
package/server/index.mjs
CHANGED
|
@@ -6,11 +6,12 @@
|
|
|
6
6
|
// │ │ ├─ /api/files ──▶ apiFiles() │
|
|
7
7
|
// │ │ ├─ /, /index.html ──▶ gallery template │
|
|
8
8
|
// │ │ └─ other paths ──▶ raw files ──▶ injectIntoHtml() │
|
|
9
|
-
// │
|
|
9
|
+
// │ │ └─ /dir/ ──▶ dir/index.html │
|
|
10
|
+
// │ ├─ WebSocketServer("/__reload") ◀─ FileWatcher │
|
|
10
11
|
// │ └─ listen ──▶ listener.start() + writeState() │
|
|
11
12
|
// └────────────────────────────────────────────────────────┘
|
|
12
13
|
import { createServer } from "node:http"
|
|
13
|
-
import { readFile } from "node:fs/promises"
|
|
14
|
+
import { readFile, stat } from "node:fs/promises"
|
|
14
15
|
import os from "node:os"
|
|
15
16
|
import { basename, extname, resolve } from "node:path"
|
|
16
17
|
import { exec } from "node:child_process"
|
|
@@ -18,7 +19,7 @@ import { clearState, writeState } from "./state.mjs"
|
|
|
18
19
|
import { apiFiles, isWithin, normalizedRelative } from "./files.mjs"
|
|
19
20
|
import { injectIntoHtml, WS_PATH } from "./inject.mjs"
|
|
20
21
|
import { WebSocketServer } from "./websocket.mjs"
|
|
21
|
-
import {
|
|
22
|
+
import { FileWatcher } from "./watch.mjs"
|
|
22
23
|
|
|
23
24
|
const MAX_PORT_TRIES = 20 // on conflict, try the next port up to 20 times
|
|
24
25
|
const INDEX_TEMPLATE = resolve(import.meta.dirname, "..", "index.html")
|
|
@@ -49,14 +50,14 @@ const MIME = {
|
|
|
49
50
|
|
|
50
51
|
export async function runServer({ docsDir, port: initialPort, open = false }) {
|
|
51
52
|
const wss = new WebSocketServer({ path: WS_PATH })
|
|
52
|
-
const
|
|
53
|
+
const fileWatcher = new FileWatcher({ wss, docsDir, templatePath: INDEX_TEMPLATE })
|
|
53
54
|
let port = initialPort
|
|
54
55
|
|
|
55
56
|
const server = createServer(createNodeServerAdapter(createHandler({ docsDir })))
|
|
56
57
|
wss.attach(server)
|
|
57
58
|
|
|
58
59
|
const cleanup = () => {
|
|
59
|
-
|
|
60
|
+
fileWatcher.close()
|
|
60
61
|
wss.close()
|
|
61
62
|
clearState()
|
|
62
63
|
server.closeAllConnections()
|
|
@@ -75,13 +76,13 @@ export async function runServer({ docsDir, port: initialPort, open = false }) {
|
|
|
75
76
|
}
|
|
76
77
|
})
|
|
77
78
|
server.listen(port, "127.0.0.1", async () => {
|
|
78
|
-
await
|
|
79
|
+
await fileWatcher.start()
|
|
79
80
|
const state = { pid: process.pid, port, url: `http://localhost:${port}/`, docsDir }
|
|
80
81
|
writeState(state)
|
|
81
82
|
console.log(state.url)
|
|
82
83
|
if (open && process.platform === "darwin") exec(`open ${state.url}`)
|
|
83
84
|
})
|
|
84
|
-
return { server, wss,
|
|
85
|
+
return { server, wss, fileWatcher, close: cleanup }
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
// --- Node server adapter ---
|
|
@@ -135,7 +136,9 @@ function createHandler({ docsDir }) {
|
|
|
135
136
|
return new Response("Bad Request", { status: 400 })
|
|
136
137
|
}
|
|
137
138
|
|
|
138
|
-
|
|
139
|
+
// /sub/ → /sub/index.html
|
|
140
|
+
const target = pathname.endsWith("/") ? `${pathname}index.html` : pathname
|
|
141
|
+
const path = resolve(docsDir, target.slice(1))
|
|
139
142
|
if (!isWithin(docsDir, path)) {
|
|
140
143
|
return new Response("Forbidden", { status: 403 })
|
|
141
144
|
}
|
|
@@ -151,6 +154,12 @@ function createHandler({ docsDir }) {
|
|
|
151
154
|
}
|
|
152
155
|
return new Response(body, { headers: { "content-type": type } })
|
|
153
156
|
} catch {
|
|
157
|
+
// Redirect /sub → /sub/
|
|
158
|
+
try {
|
|
159
|
+
if ((await stat(path)).isDirectory()) {
|
|
160
|
+
return Response.redirect(new URL(`${url.pathname}/`, url), 301)
|
|
161
|
+
}
|
|
162
|
+
} catch { }
|
|
154
163
|
return new Response("Not Found", { status: 404 })
|
|
155
164
|
}
|
|
156
165
|
}
|
|
@@ -164,6 +173,7 @@ async function galleryResponse(docsDir) {
|
|
|
164
173
|
html = html
|
|
165
174
|
.replaceAll("__DOCS_DIR_NAME__", escapeHtml(basename(docsDir) || docsDir))
|
|
166
175
|
.replaceAll("__DOCS_DIR__", escapeHtml(displayPath(docsDir)))
|
|
176
|
+
.replaceAll("__FAVICON_HUE__", String(faviconHue(docsDir)))
|
|
167
177
|
return new Response(html, { headers: { "content-type": "text/html; charset=utf-8" } })
|
|
168
178
|
} catch {
|
|
169
179
|
return new Response("Gallery template missing", { status: 500 })
|
|
@@ -176,6 +186,13 @@ function displayPath(p) {
|
|
|
176
186
|
return p.startsWith(`${home}/`) ? `~${p.slice(home.length)}` : p
|
|
177
187
|
}
|
|
178
188
|
|
|
189
|
+
// Deterministic hue for the folder favicon: the same path always yields the same color.
|
|
190
|
+
function faviconHue(s) {
|
|
191
|
+
let h = 0
|
|
192
|
+
for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) | 0
|
|
193
|
+
return ((h % 360) + 360) % 360
|
|
194
|
+
}
|
|
195
|
+
|
|
179
196
|
function escapeHtml(s) {
|
|
180
197
|
return s.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">")
|
|
181
198
|
}
|
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
|