@noy4/docserve 0.3.4 → 0.3.5

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.
Files changed (61) hide show
  1. package/bin.mjs +8 -213
  2. package/dist/gallery/assets/index-CGV64tuk.css +1 -0
  3. package/dist/gallery/assets/index-Cf8Uf0vA.js +10 -0
  4. package/dist/gallery/assets/index-Cf8Uf0vA.js.map +1 -0
  5. package/dist/gallery/index.html +15 -0
  6. package/dist/node/cli.mjs +4095 -0
  7. package/dist/node/cli.mjs.map +7 -0
  8. package/dist/widget/index.js +730 -495
  9. package/package.json +14 -4
  10. package/src/node/cli.mjs +155 -0
  11. package/src/node/{files.mjs → docs/files.mjs} +1 -1
  12. package/src/node/{inject.mjs → docs/inject.mjs} +1 -1
  13. package/src/node/{meta.mjs → docs/meta.mjs} +1 -1
  14. package/src/node/http/app.mjs +20 -0
  15. package/src/node/{assets.mjs → http/assets.mjs} +3 -2
  16. package/src/node/http/gallery.mjs +45 -0
  17. package/src/node/http/response.mjs +31 -0
  18. package/src/node/http/routes.mjs +59 -0
  19. package/src/node/{serve-static.mjs → http/serve-static.mjs} +1 -1
  20. package/src/node/{server.mjs → http/server.mjs} +10 -10
  21. package/src/node/{state.mjs → instances/state.mjs} +9 -5
  22. package/src/node/instances/view.mjs +19 -0
  23. package/src/node/{watch.mjs → live/watch.mjs} +1 -1
  24. package/src/node/paths.mjs +10 -0
  25. package/src/node/rpc/files.mjs +5 -0
  26. package/src/node/rpc/index.mjs +39 -0
  27. package/src/node/rpc/route.mjs +34 -0
  28. package/src/node/rpc/servers.mjs +16 -0
  29. package/src/plugins/servers/client.js +77 -0
  30. package/src/plugins/servers/commands.ts +30 -0
  31. package/src/plugins/servers/model.ts +106 -0
  32. package/src/plugins/servers/servers.css +144 -0
  33. package/src/plugins/servers/ui/ServersPopover.tsx +71 -0
  34. package/src/plugins/sidebar/ui/PageList.tsx +1 -1
  35. package/src/plugins/sidebar/ui/RadioMenu.tsx +1 -1
  36. package/src/plugins/sidebar/ui/Sidebar.tsx +3 -3
  37. package/src/plugins/sidebar/ui/TreeList.tsx +10 -3
  38. package/src/{plugins/sidebar/ui → shared/client}/preview-model.ts +18 -4
  39. package/src/{plugins/sidebar/ui/useModel.ts → shared/client/use-model.ts} +1 -8
  40. package/src/shared/types.d.ts +17 -1
  41. package/src/widget/{pages.js → api/pages.js} +4 -7
  42. package/src/widget/api/rpc-client.js +25 -0
  43. package/src/widget/api/servers.js +46 -0
  44. package/src/widget/host.js +10 -8
  45. package/src/widget/index.js +2 -0
  46. package/src/widget/{docks.js → shell/docks.js} +2 -2
  47. package/src/widget/{palette.js → shell/palette.js} +3 -3
  48. package/src/widget/{placement.js → shell/placement.js} +1 -1
  49. package/src/widget/{ui.js → shell/ui.js} +1 -1
  50. package/index.html +0 -365
  51. package/src/node/adapter.mjs +0 -67
  52. package/src/node/gallery.mjs +0 -33
  53. package/src/node/routes.mjs +0 -105
  54. /package/src/node/{dates.mjs → docs/dates.mjs} +0 -0
  55. /package/src/node/{docs-info.mjs → docs/info.mjs} +0 -0
  56. /package/src/node/{scan.mjs → docs/scan.mjs} +0 -0
  57. /package/src/node/{websocket.mjs → live/websocket.mjs} +0 -0
  58. /package/src/widget/{live.js → api/live.js} +0 -0
  59. /package/src/widget/{rpc.js → api/rpc.js} +0 -0
  60. /package/src/widget/{palette.css → shell/palette.css} +0 -0
  61. /package/src/widget/{styles.js → shell/styles.js} +0 -0
@@ -0,0 +1,46 @@
1
+ import { callServer } from "./rpc-client.js"
2
+
3
+ /**
4
+ * @param {{ rpc: RpcContext }} options
5
+ */
6
+ export function createServers({ rpc }) {
7
+ /** @type {DocServer[] | null} */
8
+ let cache = null
9
+ /** @type {Promise<DocServer[]> | null} */
10
+ let pending = null
11
+
12
+ rpc.register("docserve:servers:list", list)
13
+
14
+ return {
15
+ list,
16
+ invalidate() {
17
+ cache = null
18
+ pending = null
19
+ },
20
+ }
21
+
22
+ /**
23
+ * @param {{ force?: boolean }} [options]
24
+ * @returns {Promise<DocServer[]>}
25
+ */
26
+ async function list({ force = false } = {}) {
27
+ if (force) {
28
+ cache = null
29
+ pending = null
30
+ }
31
+ if (cache) return cache
32
+ if (pending) return pending
33
+
34
+ pending = callServer("docserve:servers:list", force ? [true] : [])
35
+ .then((data) => {
36
+ const servers = Array.isArray(data) ? data : []
37
+ cache = servers
38
+ return servers
39
+ })
40
+ .finally(() => {
41
+ pending = null
42
+ })
43
+
44
+ return pending
45
+ }
46
+ }
@@ -1,16 +1,17 @@
1
1
  // Widget host
2
2
  import { createCommands } from "./commands.js"
3
- import { createDocks } from "./docks.js"
3
+ import { createDocks } from "./shell/docks.js"
4
4
  import { el } from "./lib/dom.js"
5
5
  import { createEventEmitter } from "./lib/events.js"
6
- import { connectLive } from "./live.js"
7
- import { createPages } from "./pages.js"
8
- import { createPalette } from "./palette.js"
9
- import { createPlacement } from "./placement.js"
6
+ import { connectLive } from "./api/live.js"
7
+ import { createPages } from "./api/pages.js"
8
+ import { createPalette } from "./shell/palette.js"
9
+ import { createPlacement } from "./shell/placement.js"
10
10
  import { createPlugins } from "./plugins.js"
11
- import { createRpc } from "./rpc.js"
12
- import { reloadStyles, settled } from "./styles.js"
13
- import { createUi } from "./ui.js"
11
+ import { createRpc } from "./api/rpc.js"
12
+ import { createServers } from "./api/servers.js"
13
+ import { reloadStyles, settled } from "./shell/styles.js"
14
+ import { createUi } from "./shell/ui.js"
14
15
 
15
16
  /**
16
17
  * Create the widget without mounting it.
@@ -32,6 +33,7 @@ export function createHost() {
32
33
  // transport
33
34
  const rpc = createRpc()
34
35
  const pages = createPages({ rpc })
36
+ createServers({ rpc })
35
37
  const events = createEventEmitter()
36
38
 
37
39
  // context is filled below, services resolve it when called
@@ -2,6 +2,7 @@
2
2
  import { collapsePlugin } from "../plugins/collapse/client.js"
3
3
  import { copyPlugin } from "../plugins/copy/client.js"
4
4
  import { homePlugin } from "../plugins/home/client.js"
5
+ import { serversPlugin } from "../plugins/servers/client.js"
5
6
  import { sidebarPlugin } from "../plugins/sidebar/client.js"
6
7
  import { tocPlugin } from "../plugins/toc/client.js"
7
8
  import { createHost } from "./host.js"
@@ -12,6 +13,7 @@ const PLUGINS = [
12
13
  copyPlugin,
13
14
  sidebarPlugin,
14
15
  tocPlugin,
16
+ serversPlugin,
15
17
  ]
16
18
 
17
19
  // The server skips iframe previews; this guard covers pages served some other way.
@@ -1,7 +1,7 @@
1
1
  // The docks registry: one pill that every plugin registers items on. An item
2
2
  // either runs a command or is a link.
3
- import { formatKey } from "./commands.js"
4
- import { el } from "./lib/dom.js"
3
+ import { formatKey } from "../commands.js"
4
+ import { el } from "../lib/dom.js"
5
5
 
6
6
  /**
7
7
  * Build the dock and mount it into the shadow root.
@@ -1,7 +1,7 @@
1
1
  // Command palette overlay: filter, arrow keys and selection. It owns no
2
2
  // state — both the open flag and the registry live in `commands.js`.
3
- import { formatKey } from "./commands.js"
4
- import { el } from "./lib/dom.js"
3
+ import { formatKey } from "../commands.js"
4
+ import { el } from "../lib/dom.js"
5
5
 
6
6
  /**
7
7
  * @param {object} options
@@ -39,7 +39,7 @@ export function createPalette({ root, ui, commands, subscribe }) {
39
39
  const overlay = el("div", { class: "docserve-palette" }, [backdrop, dialog])
40
40
 
41
41
  root.append(overlay)
42
- ui.style("/__docserve/widget/palette.css")
42
+ ui.style("/__docserve/widget/shell/palette.css")
43
43
 
44
44
  /** @type {DocserveCommand[]} */
45
45
  let items = []
@@ -1,6 +1,6 @@
1
1
  // Placement: four viewport corners, drag the pill to move between them, and the
2
2
  // choice is remembered across loads.
3
- import { persisted } from "./lib/storage.js"
3
+ import { persisted } from "../lib/storage.js"
4
4
 
5
5
  const STORAGE_KEY = "docserve:dock-corner"
6
6
  /** Travel before a press counts as a drag, so clicks still click. */
@@ -1,5 +1,5 @@
1
1
  // UI helpers handed to plugins: mount points, stylesheets, announcements.
2
- import { el } from "./lib/dom.js"
2
+ import { el } from "../lib/dom.js"
3
3
 
4
4
  /**
5
5
  * @param {object} options
package/index.html DELETED
@@ -1,365 +0,0 @@
1
- <!doctype html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>__DOCS_DIR_NAME__</title>
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
- <style>
9
- :root {
10
- --bg: #0f1117;
11
- --card: #1a1d27;
12
- --border: #2a2e3d;
13
- --text: #e6e8ee;
14
- --muted: #9aa0b0;
15
- --accent: #6c8cff;
16
- --green: #4ade80;
17
- --btn-bg: rgba(15, 17, 23, 0.82);
18
- --folder-hue: __FAVICON_HUE__;
19
- }
20
- @media (prefers-color-scheme: light) {
21
- :root {
22
- --bg: #f8fafc;
23
- --card: #ffffff;
24
- --border: #e2e8f0;
25
- --text: #334155;
26
- --muted: #64748b;
27
- --accent: #2563eb;
28
- --green: #16a34a;
29
- --btn-bg: rgba(255, 255, 255, 0.85);
30
- }
31
- }
32
-
33
- * { box-sizing: border-box; margin: 0; padding: 0; }
34
-
35
- body {
36
- background: var(--bg);
37
- color: var(--text);
38
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
39
- padding: 40px 80px;
40
- }
41
-
42
- h1 {
43
- display: flex;
44
- align-items: center;
45
- gap: 10px;
46
- font-size: 1.6rem;
47
- margin-bottom: 8px;
48
- }
49
- h1 svg { width: 26px; height: 26px; color: hsl(var(--folder-hue) 70% 52%); flex-shrink: 0; }
50
- .sub { color: var(--muted); margin-bottom: 32px; font-size: 0.9rem; }
51
- .sub b { color: var(--green); }
52
-
53
- .sub code, .empty code {
54
- font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
55
- font-size: 0.78rem;
56
- background: var(--card);
57
- border: 1px solid var(--border);
58
- border-radius: 6px;
59
- padding: 2px 8px;
60
- word-break: break-all;
61
- }
62
-
63
- .grid {
64
- display: grid;
65
- grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
66
- gap: 16px;
67
- }
68
-
69
- .card {
70
- display: flex;
71
- flex-direction: column;
72
- overflow: hidden;
73
- background: var(--card);
74
- border: 1px solid var(--border);
75
- border-radius: 12px;
76
- color: inherit;
77
- text-decoration: none;
78
- transition: transform 0.15s ease, border-color 0.15s ease;
79
- }
80
-
81
- .card:hover { border-color: var(--accent); }
82
-
83
- .preview {
84
- position: relative;
85
- height: 160px;
86
- overflow: hidden;
87
- background: #fff;
88
- }
89
-
90
- .preview iframe {
91
- border: 0;
92
- transform-origin: top left;
93
- pointer-events: none;
94
- }
95
-
96
- .preview::after {
97
- content: "";
98
- position: absolute;
99
- inset: 0;
100
- }
101
-
102
- .copy-btn {
103
- position: absolute;
104
- top: 8px;
105
- right: 8px;
106
- z-index: 1;
107
- display: grid;
108
- place-items: center;
109
- width: 26px;
110
- height: 26px;
111
- padding: 0;
112
- border: 1px solid var(--border);
113
- border-radius: 7px;
114
- background: var(--btn-bg);
115
- color: var(--text);
116
- cursor: pointer;
117
- opacity: 0;
118
- transition: opacity 0.15s ease, color 0.15s ease, border-color 0.15s ease;
119
- }
120
-
121
- .copy-btn svg { width: 13px; height: 13px; }
122
- .copy-btn .ic-check { display: none; }
123
- .card:hover .copy-btn { opacity: 1; }
124
- .copy-btn:hover { border-color: var(--accent); color: var(--accent); }
125
- .copy-btn.copied { color: var(--green); border-color: var(--green); }
126
- .copy-btn.copied .ic-copy { display: none; }
127
- .copy-btn.copied .ic-check { display: block; }
128
-
129
- @media (hover: none) {
130
- .copy-btn { opacity: 0.7; }
131
- }
132
-
133
- .meta {
134
- display: flex;
135
- flex: 1;
136
- flex-direction: column;
137
- justify-content: space-between;
138
- gap: 8px;
139
- padding: 10px 12px;
140
- border-top: 1px solid var(--border);
141
- }
142
-
143
- .meta .title { font-size: 0.85rem; font-weight: 600; line-height: 1.4; }
144
- .meta .title .favicon {
145
- width: 16px;
146
- height: 16px;
147
- margin-right: 6px;
148
- vertical-align: -3px;
149
- border-radius: 4px;
150
- object-fit: contain;
151
- }
152
- .date { font-size: 0.7rem; color: var(--muted); }
153
-
154
- .empty {
155
- max-width: 480px;
156
- margin: 48px auto;
157
- padding: 32px 24px;
158
- border: 1px dashed var(--border);
159
- border-radius: 12px;
160
- color: var(--muted);
161
- text-align: center;
162
- }
163
-
164
- .empty b { color: var(--text); }
165
- </style>
166
- </head>
167
-
168
- <body>
169
- <h1>
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>
171
- __DOCS_DIR_NAME__
172
- </h1>
173
- <p class="sub">
174
- <code id="docs-dir">__DOCS_DIR__</code> — <b id="total">0</b> files
175
- </p>
176
-
177
- <div class="grid" id="grid" hidden></div>
178
-
179
- <div class="empty" id="empty" hidden>
180
- <b>No HTML files found</b>
181
- <p>Place .html files in <code id="empty-dir"></code> and they will appear here automatically</p>
182
- </div>
183
-
184
- <script type="module">
185
- const VIEWPORT_W = 960
186
- const VIEWPORT_H = 900
187
-
188
- function el(tag, props = {}, children = []) {
189
- const node = document.createElement(tag)
190
- for (const [key, value] of Object.entries(props)) {
191
- if (value == null || value === false) continue
192
- if (key === "class") node.className = value
193
- else if (key === "text") node.textContent = value
194
- else if (key === "html") node.innerHTML = value
195
- else if (key === "style" && typeof value === "object") Object.assign(node.style, value)
196
- else if (key.startsWith("on") && typeof value === "function") node.addEventListener(key.slice(2).toLowerCase(), value)
197
- else node.setAttribute(key, value === true ? "" : String(value))
198
- }
199
- for (const child of Array.isArray(children) ? children : [children]) {
200
- if (child == null || child === false) continue
201
- node.append(child)
202
- }
203
- return node
204
- }
205
-
206
- function icon(paths, { width = 2, class: name = "" } = {}) {
207
- return `<svg${name ? ` class="${name}"` : ""} viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="${width}" stroke-linecap="round" stroke-linejoin="round">${paths}</svg>`
208
- }
209
-
210
- const COPY_SVG = icon('<rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/>', { class: "ic-copy" })
211
- const CHECK_SVG = icon('<polyline points="20 6 9 17 4 12"/>', { class: "ic-check", width: 2.5 })
212
-
213
- class PreviewLoader {
214
- queue = []
215
- active = 0
216
- max = 3
217
- observer
218
-
219
- constructor({ max = 3, rootMargin = "400px" } = {}) {
220
- this.max = max
221
- this.observer = new IntersectionObserver((entries) => {
222
- for (const entry of entries) {
223
- if (!entry.isIntersecting) continue
224
- this.observer.unobserve(entry.target)
225
- const preview = entry.target
226
- const iframe = preview.querySelector("iframe")
227
- const src = iframe?.dataset.src
228
- if (iframe && src) this.enqueue(iframe, src)
229
- }
230
- }, { rootMargin })
231
- }
232
-
233
- watch(preview) {
234
- this.observer.observe(preview)
235
- }
236
-
237
- enqueue(iframe, src) {
238
- this.queue.push({ iframe, src })
239
- this.pump()
240
- }
241
-
242
- pump() {
243
- while (this.active < this.max && this.queue.length) {
244
- const item = this.queue.shift()
245
- if (!item) return
246
- this.active++
247
- item.iframe.addEventListener(
248
- "load",
249
- () => {
250
- this.active--
251
- this.pump()
252
- },
253
- { once: true },
254
- )
255
- item.iframe.src = item.src
256
- }
257
- }
258
- }
259
-
260
- const previews = new PreviewLoader()
261
-
262
- function fitPreviews() {
263
- for (const el of document.querySelectorAll(".preview")) {
264
- const iframe = el.querySelector("iframe")
265
- if (!iframe) continue
266
- iframe.style.width = `${VIEWPORT_W}px`
267
- iframe.style.height = `${VIEWPORT_H}px`
268
- iframe.style.transform = `scale(${el.clientWidth / VIEWPORT_W})`
269
- }
270
- }
271
-
272
- function displayDate(value) {
273
- const date = new Date(typeof value === "number" ? value * 1000 : value)
274
- if (Number.isNaN(date.getTime())) return ""
275
- return `${date.getFullYear()}/${String(date.getMonth() + 1).padStart(2, "0")}/${String(date.getDate()).padStart(2, "0")}`
276
- }
277
-
278
- function card(file) {
279
- return el("a", { class: "card", href: file.url, target: "_blank", rel: "noopener" }, [
280
- el("div", { class: "preview" }, [
281
- el("iframe", { tabindex: -1, title: file.title ?? "", "data-src": file.url }),
282
- el("button", {
283
- type: "button",
284
- class: "copy-btn",
285
- title: file.filePath,
286
- "aria-label": "Copy file path",
287
- html: COPY_SVG + CHECK_SVG,
288
- onclick: async (e) => {
289
- const btn = e.currentTarget
290
- e.preventDefault()
291
- e.stopPropagation()
292
- try {
293
- await navigator.clipboard.writeText(btn.title)
294
- btn.classList.add("copied")
295
- setTimeout(() => btn.classList.remove("copied"), 1200)
296
- } catch {}
297
- },
298
- }),
299
- ]),
300
- el("div", { class: "meta" }, [
301
- el("div", { class: "title" }, [
302
- file.favicon
303
- ? el("img", {
304
- class: "favicon",
305
- src: file.favicon,
306
- alt: "",
307
- loading: "lazy",
308
- onerror: (e) => e.currentTarget.remove(),
309
- })
310
- : null,
311
- el("span", { text: file.title }),
312
- ]),
313
- el("div", { class: "date", text: displayDate(file.created) }),
314
- ]),
315
- ])
316
- }
317
-
318
- async function load() {
319
- const docsDir = document.getElementById("docs-dir").textContent
320
- document.getElementById("empty-dir").textContent = docsDir
321
-
322
- let files = []
323
- try {
324
- const res = await fetch("/api/files")
325
- files = await res.json()
326
- } catch {}
327
- if (!Array.isArray(files)) files = []
328
-
329
- document.getElementById("total").textContent = String(files.length)
330
- if (files.length === 0) {
331
- document.getElementById("empty").hidden = false
332
- return
333
- }
334
-
335
- const grid = document.getElementById("grid")
336
- for (const f of files) grid.appendChild(card(f))
337
- grid.hidden = false
338
-
339
- for (const preview of grid.querySelectorAll(".preview")) {
340
- previews.watch(preview)
341
- }
342
- fitPreviews()
343
- new ResizeObserver(fitPreviews).observe(grid)
344
- }
345
-
346
- function connectLiveReload() {
347
- // Reload when the set of files or the gallery template itself changes;
348
- // a dropped socket recovers after restart.
349
- const ws = new WebSocket(`${location.protocol === "https:" ? "wss:" : "ws:"}//${location.host}/__reload`)
350
- ws.addEventListener("message", (e) => {
351
- let change
352
- try { change = JSON.parse(e.data) } catch { return }
353
- if (change?.type === "change" && (change.contentSetChanged || change.templateChanged))
354
- location.reload()
355
- })
356
- ws.addEventListener("close", () => setTimeout(() => location.reload(), 1000))
357
- }
358
-
359
- window.addEventListener("resize", fitPreviews)
360
- document.fonts?.ready.then(fitPreviews)
361
- load()
362
- connectLiveReload()
363
- </script>
364
- </body>
365
- </html>
@@ -1,67 +0,0 @@
1
- // node:http ⇄ Fetch adapter: one Request in, one Response out. Bodies are
2
- // streamed to the client; the server is read-only, so request bodies are not
3
- // forwarded.
4
- import { Readable } from "node:stream"
5
-
6
- /**
7
- * @param {(request: Request) => Promise<Response>} handler
8
- * @returns {(req: import("node:http").IncomingMessage, res: import("node:http").ServerResponse) => Promise<void>}
9
- */
10
- export function createNodeServerAdapter(handler) {
11
- return async function nodeHandler(req, res) {
12
- let response
13
- try {
14
- response = await handler(toWebRequest(req))
15
- } catch (err) {
16
- console.error(err)
17
- sendText(res, 500, "Internal Server Error")
18
- return
19
- }
20
-
21
- res.statusCode = response.status
22
- if (response.statusText) res.statusMessage = response.statusText
23
- response.headers.forEach((value, name) => res.setHeader(name, value))
24
-
25
- if (!response.body || req.method === "HEAD") {
26
- await response.body?.cancel().catch(() => { })
27
- res.end()
28
- return
29
- }
30
-
31
- const body = Readable.fromWeb(/** @type {any} */ (response.body))
32
- body.on("error", (err) => {
33
- console.error(err)
34
- res.destroy(err)
35
- })
36
- // The client went away before the body finished: stop reading the file.
37
- res.on("close", () => {
38
- if (!res.writableEnded) body.destroy()
39
- })
40
- body.pipe(res)
41
- }
42
- }
43
-
44
- /**
45
- * @param {import("node:http").IncomingMessage} req
46
- * @returns {Request}
47
- */
48
- function toWebRequest(req) {
49
- const host = req.headers.host || "localhost"
50
- const headers = new Headers()
51
- for (const [name, value] of Object.entries(req.headers)) {
52
- if (value === undefined) continue
53
- if (Array.isArray(value)) for (const item of value) headers.append(name, item)
54
- else headers.set(name, value)
55
- }
56
- return new Request(`http://${host}${req.url}`, { method: req.method, headers })
57
- }
58
-
59
- /**
60
- * @param {import("node:http").ServerResponse} res
61
- * @param {number} status
62
- * @param {string} body
63
- */
64
- function sendText(res, status, body) {
65
- res.writeHead(status, { "content-type": "text/plain; charset=utf-8" })
66
- res.end(body)
67
- }
@@ -1,33 +0,0 @@
1
- // The gallery template served at / and /index.html. Every other HTML file comes
2
- // from the docs folder and is injected instead.
3
- import { readFile } from "node:fs/promises"
4
- import { resolve } from "node:path"
5
- import { docsInfo } from "./docs-info.mjs"
6
-
7
- export const INDEX_TEMPLATE = resolve(import.meta.dirname, "..", "..", "index.html")
8
-
9
- /**
10
- * @param {string} docsDir
11
- * @returns {Promise<Response>}
12
- */
13
- export async function galleryResponse(docsDir) {
14
- try {
15
- const { name, path, hue } = docsInfo(docsDir)
16
- let html = await readFile(INDEX_TEMPLATE, "utf8")
17
- html = html
18
- .replaceAll("__DOCS_DIR_NAME__", escapeHtml(name))
19
- .replaceAll("__DOCS_DIR__", escapeHtml(path))
20
- .replaceAll("__FAVICON_HUE__", String(hue))
21
- return new Response(html, { headers: { "content-type": "text/html; charset=utf-8" } })
22
- } catch {
23
- return new Response("Gallery template missing", { status: 500 })
24
- }
25
- }
26
-
27
- /**
28
- * @param {string} s
29
- * @returns {string}
30
- */
31
- function escapeHtml(s) {
32
- return s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;")
33
- }