@noy4/docserve 0.3.3 → 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.
- package/bin.mjs +8 -213
- package/dist/gallery/assets/index-CGV64tuk.css +1 -0
- package/dist/gallery/assets/index-Cf8Uf0vA.js +10 -0
- package/dist/gallery/assets/index-Cf8Uf0vA.js.map +1 -0
- package/dist/gallery/index.html +15 -0
- package/dist/node/cli.mjs +4095 -0
- package/dist/node/cli.mjs.map +7 -0
- package/dist/widget/index.js +3410 -2527
- package/package.json +15 -5
- package/src/node/cli.mjs +155 -0
- package/src/node/{files.mjs → docs/files.mjs} +1 -1
- package/src/node/{inject.mjs → docs/inject.mjs} +1 -1
- package/src/node/{meta.mjs → docs/meta.mjs} +1 -1
- package/src/node/http/app.mjs +20 -0
- package/src/node/{assets.mjs → http/assets.mjs} +3 -2
- package/src/node/http/gallery.mjs +45 -0
- package/src/node/http/response.mjs +31 -0
- package/src/node/http/routes.mjs +59 -0
- package/src/node/{serve-static.mjs → http/serve-static.mjs} +1 -1
- package/src/node/{server.mjs → http/server.mjs} +10 -10
- package/src/node/{state.mjs → instances/state.mjs} +9 -5
- package/src/node/instances/view.mjs +19 -0
- package/src/node/{watch.mjs → live/watch.mjs} +1 -1
- package/src/node/paths.mjs +10 -0
- package/src/node/rpc/files.mjs +5 -0
- package/src/node/rpc/index.mjs +39 -0
- package/src/node/rpc/route.mjs +34 -0
- package/src/node/rpc/servers.mjs +16 -0
- package/src/plugins/collapse/client.js +45 -0
- package/src/plugins/collapse/collapse.css +74 -0
- package/src/plugins/collapse/commands.ts +81 -0
- package/src/plugins/collapse/model.ts +123 -0
- package/src/plugins/collapse/sections.ts +105 -0
- package/src/plugins/collapse/view.ts +84 -0
- package/src/plugins/servers/client.js +77 -0
- package/src/plugins/servers/commands.ts +30 -0
- package/src/plugins/servers/model.ts +106 -0
- package/src/plugins/servers/servers.css +144 -0
- package/src/plugins/servers/ui/ServersPopover.tsx +71 -0
- package/src/plugins/sidebar/client.js +1 -0
- package/src/plugins/sidebar/icons.js +1 -1
- package/src/plugins/sidebar/ui/PageList.tsx +1 -1
- package/src/plugins/sidebar/ui/RadioMenu.tsx +1 -1
- package/src/plugins/sidebar/ui/Sidebar.tsx +3 -3
- package/src/plugins/sidebar/ui/TreeList.tsx +10 -3
- package/src/plugins/toc/client.js +55 -0
- package/src/plugins/toc/commands.ts +49 -0
- package/src/plugins/toc/heading-html.ts +40 -0
- package/src/plugins/toc/model.ts +178 -0
- package/src/plugins/toc/outline.js +94 -0
- package/src/plugins/toc/scroll-spy.ts +35 -0
- package/src/plugins/toc/toc.css +214 -0
- package/src/plugins/toc/ui/TocPanel.tsx +87 -0
- package/src/{plugins/sidebar/ui → shared/client}/preview-model.ts +18 -4
- package/src/{plugins/sidebar/ui/useModel.ts → shared/client/use-model.ts} +1 -8
- package/src/shared/types.d.ts +17 -1
- package/src/widget/{pages.js → api/pages.js} +4 -7
- package/src/widget/api/rpc-client.js +25 -0
- package/src/widget/api/servers.js +46 -0
- package/src/widget/host.js +10 -8
- package/src/widget/index.js +11 -1
- package/src/widget/lib/headings.js +132 -0
- package/src/widget/{docks.js → shell/docks.js} +2 -2
- package/src/widget/{palette.js → shell/palette.js} +3 -3
- package/src/widget/{placement.js → shell/placement.js} +1 -1
- package/src/widget/{ui.js → shell/ui.js} +1 -1
- package/src/widget/widget.css +15 -2
- package/index.html +0 -365
- package/src/node/adapter.mjs +0 -67
- package/src/node/gallery.mjs +0 -33
- package/src/node/routes.mjs +0 -105
- /package/src/node/{dates.mjs → docs/dates.mjs} +0 -0
- /package/src/node/{docs-info.mjs → docs/info.mjs} +0 -0
- /package/src/node/{scan.mjs → docs/scan.mjs} +0 -0
- /package/src/node/{websocket.mjs → live/websocket.mjs} +0 -0
- /package/src/widget/{live.js → api/live.js} +0 -0
- /package/src/widget/{rpc.js → api/rpc.js} +0 -0
- /package/src/widget/{palette.css → shell/palette.css} +0 -0
- /package/src/widget/{styles.js → shell/styles.js} +0 -0
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>
|
package/src/node/adapter.mjs
DELETED
|
@@ -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
|
-
}
|
package/src/node/gallery.mjs
DELETED
|
@@ -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("&", "&").replaceAll("<", "<").replaceAll(">", ">")
|
|
33
|
-
}
|
package/src/node/routes.mjs
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
// HTTP routing: an ordered table of handlers over one docs static handler.
|
|
2
|
-
// Nothing here knows about listening, signals or sockets.
|
|
3
|
-
import { resolve } from "node:path"
|
|
4
|
-
import { serveAsset } from "./assets.mjs"
|
|
5
|
-
import { apiFiles } from "./files.mjs"
|
|
6
|
-
import { galleryResponse } from "./gallery.mjs"
|
|
7
|
-
import { injectIntoHtml } from "./inject.mjs"
|
|
8
|
-
import { createStaticHandler } from "./serve-static.mjs"
|
|
9
|
-
|
|
10
|
-
const WIDGET_DIR = resolve(import.meta.dirname, "..", "widget")
|
|
11
|
-
const PLUGINS_DIR = resolve(import.meta.dirname, "..", "plugins")
|
|
12
|
-
const DIST_WIDGET_DIR = resolve(import.meta.dirname, "..", "..", "dist", "widget")
|
|
13
|
-
const INTERNAL_PREFIX = "/__docserve/"
|
|
14
|
-
const WIDGET_PREFIX = `${INTERNAL_PREFIX}widget/`
|
|
15
|
-
const PLUGINS_PREFIX = `${INTERNAL_PREFIX}plugins/`
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @param {{ docsDir: string }} options
|
|
19
|
-
* @returns {(request: Request) => Promise<Response>}
|
|
20
|
-
*/
|
|
21
|
-
export function createHandler({ docsDir }) {
|
|
22
|
-
// Every other HTML response is injected; the gallery template is not.
|
|
23
|
-
const docs = createStaticHandler({
|
|
24
|
-
root: docsDir,
|
|
25
|
-
headers: { "cache-control": "no-store" },
|
|
26
|
-
transform: (html, relative) => injectIntoHtml(html, relative, docsDir),
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
/** @type {Array<{ match: (pathname: string) => boolean, handle: (request: Request, url: URL) => Promise<Response> }>} */
|
|
30
|
-
const routes = [
|
|
31
|
-
{ match: (pathname) => pathname === "/api/files", handle: serveFiles },
|
|
32
|
-
{ match: (pathname) => pathname.startsWith(INTERNAL_PREFIX), handle: serveInternal },
|
|
33
|
-
{ match: (pathname) => pathname === "/" || pathname === "/index.html", handle: serveGallery },
|
|
34
|
-
{ match: () => true, handle: serveDocs },
|
|
35
|
-
]
|
|
36
|
-
|
|
37
|
-
return async function handler(request) {
|
|
38
|
-
const url = new URL(request.url)
|
|
39
|
-
const route = routes.find((candidate) => candidate.match(url.pathname))
|
|
40
|
-
return route.handle(request, url)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* @returns {Promise<Response>}
|
|
45
|
-
*/
|
|
46
|
-
async function serveFiles() {
|
|
47
|
-
try {
|
|
48
|
-
return Response.json(await apiFiles(docsDir), { headers: { "cache-control": "no-store" } })
|
|
49
|
-
} catch (err) {
|
|
50
|
-
console.error(err)
|
|
51
|
-
return Response.json([], { headers: { "cache-control": "no-store" } })
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Widget bundle and plugin sources, read per request (widget JS comes from dist).
|
|
57
|
-
* @param {Request} request
|
|
58
|
-
* @param {URL} url
|
|
59
|
-
* @returns {Promise<Response>}
|
|
60
|
-
*/
|
|
61
|
-
async function serveInternal(request, url) {
|
|
62
|
-
if (url.pathname.startsWith(WIDGET_PREFIX))
|
|
63
|
-
return serveWidget(url.pathname.slice(WIDGET_PREFIX.length))
|
|
64
|
-
if (url.pathname.startsWith(PLUGINS_PREFIX))
|
|
65
|
-
return serveAsset(PLUGINS_DIR, url.pathname.slice(PLUGINS_PREFIX.length))
|
|
66
|
-
return new Response("Not Found", { status: 404 })
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Built widget first, source files (CSS) as the fallback. A JS request that
|
|
71
|
-
* misses the build is an error: the source entry now has bare imports.
|
|
72
|
-
* @param {string} relative
|
|
73
|
-
* @returns {Promise<Response>}
|
|
74
|
-
*/
|
|
75
|
-
async function serveWidget(relative) {
|
|
76
|
-
if (!relative) return new Response("Not Found", { status: 404 })
|
|
77
|
-
|
|
78
|
-
const built = await serveAsset(DIST_WIDGET_DIR, relative)
|
|
79
|
-
if (built.status !== 404) return built
|
|
80
|
-
|
|
81
|
-
if (/\.m?js$/.test(relative)) {
|
|
82
|
-
return new Response("Widget bundle missing: run `pnpm -C cli build`", {
|
|
83
|
-
status: 503,
|
|
84
|
-
headers: { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store" },
|
|
85
|
-
})
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return serveAsset(WIDGET_DIR, relative)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* @returns {Promise<Response>}
|
|
93
|
-
*/
|
|
94
|
-
async function serveGallery() {
|
|
95
|
-
return galleryResponse(docsDir)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* @param {Request} request
|
|
100
|
-
* @returns {Promise<Response>}
|
|
101
|
-
*/
|
|
102
|
-
async function serveDocs(request) {
|
|
103
|
-
return (await docs(request)) ?? new Response("Not Found", { status: 404 })
|
|
104
|
-
}
|
|
105
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|