@bigmistqke/whiteboard 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +628 -2
- package/bin/whiteboard.ts +488 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/whiteboard.js +542 -0
- package/dist/client/runtime.js +544 -0
- package/dist/define.js +7 -0
- package/dist/plugins/callout/README.md +13 -0
- package/dist/plugins/callout/index.js +27 -0
- package/dist/plugins/callout/style.css +12 -0
- package/dist/plugins/chat/README.md +46 -0
- package/dist/plugins/chat/client.js +3 -0
- package/dist/plugins/chat/index.js +47 -0
- package/dist/plugins/chat/style.css +24 -0
- package/dist/plugins/claude-code/index.js +120 -0
- package/dist/plugins/code/README.md +42 -0
- package/dist/plugins/code/diff.js +65 -0
- package/dist/plugins/code/index.js +77 -0
- package/dist/plugins/code/style.css +29 -0
- package/dist/plugins/comments/client.js +10 -0
- package/dist/plugins/comments/index.js +54 -0
- package/dist/plugins/comments/style.css +40 -0
- package/dist/plugins/details/README.md +17 -0
- package/dist/plugins/details/index.js +17 -0
- package/dist/plugins/details/style.css +3 -0
- package/dist/plugins/forge/README.md +12 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +111 -0
- package/dist/plugins/form/index.js +102 -0
- package/dist/plugins/form/style.css +41 -0
- package/dist/plugins/gallery/README.md +30 -0
- package/dist/plugins/gallery/client.js +1 -0
- package/dist/plugins/gallery/index.js +74 -0
- package/dist/plugins/gallery/style.css +27 -0
- package/dist/plugins/grid/README.md +33 -0
- package/dist/plugins/grid/index.js +33 -0
- package/dist/plugins/grid/style.css +25 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/client.js +1 -0
- package/dist/plugins/iframe/index.js +40 -0
- package/dist/plugins/iframe/style.css +29 -0
- package/dist/plugins/index.js +30 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +35 -0
- package/dist/plugins/mermaid/style.css +16 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +17 -0
- package/dist/plugins/terminal/client.js +41 -0
- package/dist/plugins/terminal/host.js +332 -0
- package/dist/plugins/terminal/index.js +171 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +57 -0
- package/dist/plugins/terminal/style.css +357 -0
- package/dist/solid/index.js +36 -0
- package/dist/src/auth.js +54 -0
- package/dist/src/client/page/base.css +295 -0
- package/dist/src/client/page/main.js +1228 -0
- package/dist/src/config.js +76 -0
- package/dist/src/events.js +183 -0
- package/dist/src/html.js +34 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/load.js +77 -0
- package/dist/src/loader.js +126 -0
- package/dist/src/package.js +33 -0
- package/dist/src/pages/blocks.js +50 -0
- package/dist/src/pages/index.js +15 -0
- package/dist/src/pages/markdown.js +410 -0
- package/dist/src/pages/render.js +311 -0
- package/dist/src/plugins.js +238 -0
- package/dist/src/running.js +24 -0
- package/dist/src/serve.js +71 -0
- package/dist/src/server.js +725 -0
- package/dist/src/status.js +71 -0
- package/package.json +131 -4
- package/plugins/callout/README.md +13 -0
- package/plugins/callout/index.ts +25 -0
- package/plugins/callout/style.css +12 -0
- package/plugins/chat/README.md +46 -0
- package/plugins/chat/autosize.ts +101 -0
- package/plugins/chat/client.tsx +93 -0
- package/plugins/chat/index.ts +61 -0
- package/plugins/chat/style.css +24 -0
- package/plugins/claude-code/index.ts +136 -0
- package/plugins/code/README.md +42 -0
- package/plugins/code/diff.ts +64 -0
- package/plugins/code/index.ts +94 -0
- package/plugins/code/style.css +29 -0
- package/plugins/comments/client.ts +492 -0
- package/plugins/comments/index.ts +54 -0
- package/plugins/comments/style.css +40 -0
- package/plugins/details/README.md +17 -0
- package/plugins/details/index.ts +21 -0
- package/plugins/details/style.css +3 -0
- package/plugins/forge/README.md +12 -0
- package/plugins/forge/index.ts +71 -0
- package/plugins/form/README.md +111 -0
- package/plugins/form/index.ts +104 -0
- package/plugins/form/style.css +41 -0
- package/plugins/gallery/README.md +30 -0
- package/plugins/gallery/client.tsx +64 -0
- package/plugins/gallery/index.ts +67 -0
- package/plugins/gallery/style.css +27 -0
- package/plugins/grid/README.md +33 -0
- package/plugins/grid/index.ts +36 -0
- package/plugins/grid/style.css +25 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/client.ts +46 -0
- package/plugins/iframe/index.ts +54 -0
- package/plugins/iframe/style.css +29 -0
- package/plugins/index.ts +21 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +40 -0
- package/plugins/mermaid/style.css +16 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +27 -0
- package/plugins/terminal/client.ts +317 -0
- package/plugins/terminal/host.ts +326 -0
- package/plugins/terminal/index.ts +138 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/server.ts +215 -0
- package/plugins/terminal/sizes.ts +60 -0
- package/plugins/terminal/style.css +71 -0
- package/skills/whiteboard/SKILL.md +302 -0
- package/solid/index.tsx +59 -0
- package/src/auth.ts +66 -0
- package/src/client/forms.ts +185 -0
- package/src/client/markdown.ts +36 -0
- package/src/client/page/base.css +295 -0
- package/src/client/page/elsewhere.ts +70 -0
- package/src/client/page/fresh.ts +205 -0
- package/src/client/page/live.ts +30 -0
- package/src/client/page/main.ts +272 -0
- package/src/client/page/orphans.ts +46 -0
- package/src/client/page/sort.ts +38 -0
- package/src/client/page/theme.ts +32 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +182 -0
- package/src/client/read.ts +73 -0
- package/src/client/runtime.ts +183 -0
- package/src/client/toast.ts +25 -0
- package/src/config.ts +96 -0
- package/src/define.ts +612 -0
- package/src/events.ts +218 -0
- package/src/html.ts +40 -0
- package/src/icons.ts +8 -0
- package/src/load.ts +89 -0
- package/src/loader.ts +112 -0
- package/src/package.ts +39 -0
- package/src/pages/blocks.ts +70 -0
- package/src/pages/index.ts +42 -0
- package/src/pages/markdown.ts +364 -0
- package/src/pages/render.ts +233 -0
- package/src/plugins.ts +197 -0
- package/src/running.ts +22 -0
- package/src/serve.ts +52 -0
- package/src/server.ts +667 -0
- package/src/status.ts +113 -0
|
@@ -0,0 +1,725 @@
|
|
|
1
|
+
import { appendEvent, forAgent, matches, pickedUp, readEvents } from "./events.js";
|
|
2
|
+
import { html, raw } from "./html.js";
|
|
3
|
+
import { createConfigLoader, pluginDir, toPath } from "./loader.js";
|
|
4
|
+
import { describeRoutes, dispatch, mountRoutes, pluginCommands, usage } from "./plugins.js";
|
|
5
|
+
import { createAuth } from "./auth.js";
|
|
6
|
+
import { FROM_SOURCE, PACKAGE_DIR } from "./package.js";
|
|
7
|
+
import { awaiting, covers, fromReader, heard, questionState, unreadMessages } from "./status.js";
|
|
8
|
+
import { titleOf } from "./pages/render.js";
|
|
9
|
+
import _taggedTemplateLiteral from "../_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js";
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
// src/server.ts
|
|
13
|
+
var _templateObject;
|
|
14
|
+
const ENGINE_DIR = PACKAGE_DIR;
|
|
15
|
+
const RUNTIME = path.join(ENGINE_DIR, "dist", "client", "runtime.js");
|
|
16
|
+
const PAGE_CLIENT = path.join(ENGINE_DIR, "dist", "src", "client", "page", "main.js");
|
|
17
|
+
const MIME = {
|
|
18
|
+
".css": "text/css",
|
|
19
|
+
".js": "text/javascript",
|
|
20
|
+
".mjs": "text/javascript",
|
|
21
|
+
".json": "application/json",
|
|
22
|
+
".png": "image/png",
|
|
23
|
+
".jpg": "image/jpeg",
|
|
24
|
+
".jpeg": "image/jpeg",
|
|
25
|
+
".gif": "image/gif",
|
|
26
|
+
".webp": "image/webp",
|
|
27
|
+
".avif": "image/avif",
|
|
28
|
+
".svg": "image/svg+xml",
|
|
29
|
+
".pdf": "application/pdf",
|
|
30
|
+
".mp4": "video/mp4",
|
|
31
|
+
".webm": "video/webm",
|
|
32
|
+
".woff": "font/woff",
|
|
33
|
+
".woff2": "font/woff2",
|
|
34
|
+
".txt": "text/plain; charset=utf-8",
|
|
35
|
+
".md": "text/markdown; charset=utf-8",
|
|
36
|
+
".csv": "text/csv; charset=utf-8",
|
|
37
|
+
".html": "text/html; charset=utf-8"
|
|
38
|
+
};
|
|
39
|
+
const OWN_FILES = /* @__PURE__ */ new Set([
|
|
40
|
+
"events.ndjson",
|
|
41
|
+
"server.json",
|
|
42
|
+
"server.log",
|
|
43
|
+
"server.lock",
|
|
44
|
+
"token"
|
|
45
|
+
]);
|
|
46
|
+
const SKIP_DIRS = /* @__PURE__ */ new Set(["node_modules", ".git"]);
|
|
47
|
+
const FRONTMATTER = /^---\r?\n([\s\S]*?)\r?\n---\r?\n/;
|
|
48
|
+
function createServer(project) {
|
|
49
|
+
const loader = createConfigLoader(project);
|
|
50
|
+
const auth = createAuth(project.wb);
|
|
51
|
+
const watchers = /* @__PURE__ */ new Set();
|
|
52
|
+
const streams = /* @__PURE__ */ new Set();
|
|
53
|
+
const watch = (dir, options, fn) => {
|
|
54
|
+
try {
|
|
55
|
+
watchers.add(fs.watch(dir, options, (_evt, name) => {
|
|
56
|
+
if (name) fn(String(name));
|
|
57
|
+
}));
|
|
58
|
+
} catch {}
|
|
59
|
+
};
|
|
60
|
+
const setups = /* @__PURE__ */ new WeakMap();
|
|
61
|
+
const pluginDirs = /* @__PURE__ */ new Set();
|
|
62
|
+
/** The hooks of the loaded config's plugins, set up once per config. */
|
|
63
|
+
async function pluginHooks() {
|
|
64
|
+
const c = await config();
|
|
65
|
+
if (!setups.has(c)) setups.set(c, Promise.all(c.plugins.map(async (e) => {
|
|
66
|
+
try {
|
|
67
|
+
pluginDirs.add(e.name);
|
|
68
|
+
const dataDir = pluginDir(project.wb, e.name);
|
|
69
|
+
const hooks = await e.setup?.({
|
|
70
|
+
project: {
|
|
71
|
+
name: project.name,
|
|
72
|
+
dir: project.dir,
|
|
73
|
+
wb: project.wb
|
|
74
|
+
},
|
|
75
|
+
dataDir,
|
|
76
|
+
authorized: auth.authorized,
|
|
77
|
+
asset
|
|
78
|
+
}) ?? {};
|
|
79
|
+
return {
|
|
80
|
+
name: e.name,
|
|
81
|
+
...hooks,
|
|
82
|
+
table: mountRoutes(e.name, hooks.routes),
|
|
83
|
+
plugin: e
|
|
84
|
+
};
|
|
85
|
+
} catch (err) {
|
|
86
|
+
console.error(`plugin ${e.name}:`, err);
|
|
87
|
+
return {
|
|
88
|
+
name: e.name,
|
|
89
|
+
table: [],
|
|
90
|
+
plugin: e,
|
|
91
|
+
error: err
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
})));
|
|
95
|
+
return setups.get(c);
|
|
96
|
+
}
|
|
97
|
+
async function shellExtras(req) {
|
|
98
|
+
const hooks = await pluginHooks();
|
|
99
|
+
const notices = hooks.filter((h) => h.error).map((h) => `plugin "${h.name}" failed to start: ${h.error.message}`);
|
|
100
|
+
const parts = await Promise.all(hooks.map(async (h) => {
|
|
101
|
+
try {
|
|
102
|
+
return await h.shell?.(req) ?? null;
|
|
103
|
+
} catch (err) {
|
|
104
|
+
console.error(`plugin ${h.name}:`, err);
|
|
105
|
+
notices.push(`plugin "${h.name}" failed on this page: ${err.message}`);
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
}));
|
|
109
|
+
const clients = hooks.filter((h) => h.client && !h.error).map((h) => ({
|
|
110
|
+
name: h.name,
|
|
111
|
+
client: asset(h.client)
|
|
112
|
+
}));
|
|
113
|
+
const messages = [...(await config()).forReader];
|
|
114
|
+
return {
|
|
115
|
+
head: parts.map((x) => raw(String(x?.head ?? ""))),
|
|
116
|
+
body: parts.map((x) => raw(String(x?.body ?? ""))),
|
|
117
|
+
clients,
|
|
118
|
+
messages,
|
|
119
|
+
notices
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** A page with what the plugins add, their notices among the page's. */
|
|
123
|
+
async function page(req, stored, rendered) {
|
|
124
|
+
const extras = await shellExtras(req);
|
|
125
|
+
return shell(stored, {
|
|
126
|
+
...rendered,
|
|
127
|
+
notices: [...rendered.notices, ...extras.notices]
|
|
128
|
+
}, extras);
|
|
129
|
+
}
|
|
130
|
+
/** The loaded config. Also applies its `files` setting to what is served and watched. */
|
|
131
|
+
async function config() {
|
|
132
|
+
const c = await loader.config();
|
|
133
|
+
setFiles(c.files);
|
|
134
|
+
return c;
|
|
135
|
+
}
|
|
136
|
+
const assets = /* @__PURE__ */ new Set();
|
|
137
|
+
const assetDirs = /* @__PURE__ */ new Set();
|
|
138
|
+
const BROWSER_ASSET = /\.(m?js|css|map|svg|png|jpe?g|gif|webp|avif|ico|woff2?|ttf|otf|wasm)$/i;
|
|
139
|
+
const servable = (file) => assets.has(file) || assetDirs.has(path.dirname(file)) && BROWSER_ASSET.test(file);
|
|
140
|
+
function asset(ref) {
|
|
141
|
+
const file = toPath(ref);
|
|
142
|
+
if (!assets.has(file)) {
|
|
143
|
+
assets.add(file);
|
|
144
|
+
const dir = path.dirname(file);
|
|
145
|
+
if (!assetDirs.has(dir)) {
|
|
146
|
+
assetDirs.add(dir);
|
|
147
|
+
if (!within(dir, project.wb) && !ENGINE_WATCHED.some((d) => within(dir, d))) watch(dir, {}, (name) => onSourceChange(path.join(dir, name)));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return `/_/file${file.split(path.sep).map(encodeURIComponent).join("/")}`;
|
|
151
|
+
}
|
|
152
|
+
const within = (file, dir) => file === dir || file.startsWith(dir + path.sep);
|
|
153
|
+
/** Resolve a path inside a base folder, refusing anything that escapes it. */
|
|
154
|
+
function inside(base, rel) {
|
|
155
|
+
const p = path.resolve(base, "." + path.posix.normalize("/" + rel));
|
|
156
|
+
return within(p, base) ? p : null;
|
|
157
|
+
}
|
|
158
|
+
const isRepoId = (id) => id === "-" || id.startsWith("-/");
|
|
159
|
+
function frontmatter(source) {
|
|
160
|
+
const m = FRONTMATTER.exec(source);
|
|
161
|
+
const out = {};
|
|
162
|
+
if (m) for (const line of m[1].split(/\r?\n/)) {
|
|
163
|
+
const kv = /^([\w-]+)\s*:\s*(.*)$/.exec(line);
|
|
164
|
+
if (kv) out[kv[1]] = kv[2].trim().replace(/^["']|["']$/g, "");
|
|
165
|
+
}
|
|
166
|
+
return out;
|
|
167
|
+
}
|
|
168
|
+
function pageSource(id, file) {
|
|
169
|
+
const source = fs.readFileSync(file, "utf8");
|
|
170
|
+
return {
|
|
171
|
+
kind: "page",
|
|
172
|
+
id,
|
|
173
|
+
file,
|
|
174
|
+
mtime: fs.statSync(file).mtimeMs,
|
|
175
|
+
source,
|
|
176
|
+
body: source.replace(FRONTMATTER, ""),
|
|
177
|
+
frontmatter: frontmatter(source)
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/** The pages in a folder of the pages folder, newest first, with when each was last answered. */
|
|
181
|
+
function pagesIn(dir) {
|
|
182
|
+
const last = /* @__PURE__ */ new Map();
|
|
183
|
+
for (const e of readEvents(project.eventsFile)) if (fromReader(e)) last.set(e.page, e.ts);
|
|
184
|
+
const out = [];
|
|
185
|
+
const walk = (d) => {
|
|
186
|
+
if (!fs.existsSync(d)) return;
|
|
187
|
+
for (const ent of fs.readdirSync(d, { withFileTypes: true })) {
|
|
188
|
+
if (ent.name.startsWith(".") || ent.name === "node_modules") continue;
|
|
189
|
+
const full = path.join(d, ent.name);
|
|
190
|
+
if (ent.isDirectory()) walk(full);
|
|
191
|
+
else if (ent.name.endsWith(".md")) {
|
|
192
|
+
const id = path.relative(project.pagesDir, full).split(path.sep).join("/").replace(/\.md$/, "");
|
|
193
|
+
out.push({
|
|
194
|
+
id,
|
|
195
|
+
file: full,
|
|
196
|
+
mtime: fs.statSync(full).mtimeMs,
|
|
197
|
+
lastEvent: last.get(id) ?? null,
|
|
198
|
+
awaiting: 0
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
walk(dir);
|
|
204
|
+
return out.sort((a, b) => b.mtime - a.mtime);
|
|
205
|
+
}
|
|
206
|
+
/** Text files show as files; anything with a NUL byte near the start is binary. */
|
|
207
|
+
function isText(file) {
|
|
208
|
+
const fd = fs.openSync(file, "r");
|
|
209
|
+
const buf = Buffer.alloc(8192);
|
|
210
|
+
const n = fs.readSync(fd, buf, 0, buf.length, 0);
|
|
211
|
+
fs.closeSync(fd);
|
|
212
|
+
return !buf.subarray(0, n).includes(0);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* A page id → the stored page, or one of: { redirect } (a folder asked for without its slash),
|
|
216
|
+
* { raw: file } (serve the bytes), null (nothing there).
|
|
217
|
+
*/
|
|
218
|
+
function stored(id) {
|
|
219
|
+
if (!id) return {
|
|
220
|
+
kind: "pages",
|
|
221
|
+
id: "",
|
|
222
|
+
file: project.pagesDir,
|
|
223
|
+
mtime: null,
|
|
224
|
+
pages: pagesIn(project.pagesDir)
|
|
225
|
+
};
|
|
226
|
+
if (isRepoId(id)) {
|
|
227
|
+
const rel = id.slice(2);
|
|
228
|
+
const target = inside(project.dir, rel);
|
|
229
|
+
if (!target || path.relative(project.dir, target).split(path.sep).includes(".git") || !fs.existsSync(target)) return null;
|
|
230
|
+
const stat = fs.statSync(target);
|
|
231
|
+
if (stat.isDirectory()) {
|
|
232
|
+
const entries = fs.readdirSync(target, { withFileTypes: true }).filter((e) => !SKIP_DIRS.has(e.name) && e.name !== ".DS_Store").map((e) => ({
|
|
233
|
+
name: e.name,
|
|
234
|
+
dir: e.isDirectory()
|
|
235
|
+
})).sort((a, b) => Number(b.dir) - Number(a.dir) || a.name.localeCompare(b.name));
|
|
236
|
+
return {
|
|
237
|
+
kind: "folder",
|
|
238
|
+
id,
|
|
239
|
+
file: target,
|
|
240
|
+
mtime: stat.mtimeMs,
|
|
241
|
+
entries
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
if (/\.(md|markdown)$/i.test(target)) return pageSource(id, target);
|
|
245
|
+
if (stat.size > 1e6 || !isText(target)) return { raw: target };
|
|
246
|
+
return {
|
|
247
|
+
kind: "file",
|
|
248
|
+
id,
|
|
249
|
+
file: target,
|
|
250
|
+
mtime: stat.mtimeMs,
|
|
251
|
+
source: fs.readFileSync(target, "utf8")
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
const target = inside(project.pagesDir, id);
|
|
255
|
+
if (!target) return null;
|
|
256
|
+
if (path.extname(id) && fs.existsSync(target) && fs.statSync(target).isFile()) return { raw: target };
|
|
257
|
+
if (fs.existsSync(target) && fs.statSync(target).isDirectory()) {
|
|
258
|
+
const index = path.join(target, "index.md");
|
|
259
|
+
if (fs.existsSync(index)) return pageSource(id, index);
|
|
260
|
+
return {
|
|
261
|
+
kind: "pages",
|
|
262
|
+
id,
|
|
263
|
+
file: target,
|
|
264
|
+
mtime: null,
|
|
265
|
+
pages: pagesIn(target)
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
const file = target + ".md";
|
|
269
|
+
return fs.existsSync(file) ? pageSource(id, file) : null;
|
|
270
|
+
}
|
|
271
|
+
/** The URL that serves a file or folder on disk, or null when nothing here serves it. */
|
|
272
|
+
function urlOf(abs) {
|
|
273
|
+
const rel = (dir) => path.relative(dir, abs).split(path.sep).map(encodeURIComponent).join("/");
|
|
274
|
+
if (within(abs, project.pagesDir)) return `/${rel(project.pagesDir).replace(/\.md$/, "")}`;
|
|
275
|
+
if (FILES && within(abs, project.dir)) {
|
|
276
|
+
if (rel(project.dir).split("/").some((part) => SKIP_DIRS.has(part))) return null;
|
|
277
|
+
return `/-/${rel(project.dir)}`;
|
|
278
|
+
}
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
/** For links: the target if it exists (also with .md added), mapped to its URL. */
|
|
282
|
+
function urlFor(abs) {
|
|
283
|
+
for (const candidate of [abs, abs + ".md"]) if (fs.existsSync(candidate)) {
|
|
284
|
+
const url = urlOf(candidate);
|
|
285
|
+
if (url) return url + (fs.statSync(candidate).isDirectory() && !url.endsWith("/") ? "/" : "");
|
|
286
|
+
}
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
/** A config that failed to load: the page as stored, as text, under the error. */
|
|
290
|
+
function rawRender(page) {
|
|
291
|
+
const list = (items) => html`<ul>${items.map(([href, text]) => html`<li><a href="${href}">${text}</a></li>`)}</ul>`;
|
|
292
|
+
if (page.kind === "pages") return { html: list(page.pages.map((p) => [`/${p.id}`, p.id])) };
|
|
293
|
+
if (page.kind === "folder") return { html: list(page.entries.map((e) => [encodeURIComponent(e.name) + (e.dir ? "/" : ""), e.name + (e.dir ? "/" : "")])) };
|
|
294
|
+
return { html: html`<pre>${page.source}</pre>` };
|
|
295
|
+
}
|
|
296
|
+
const context = () => ({
|
|
297
|
+
project: {
|
|
298
|
+
name: project.name,
|
|
299
|
+
dir: project.dir,
|
|
300
|
+
files: !!FILES
|
|
301
|
+
},
|
|
302
|
+
asset,
|
|
303
|
+
urlFor
|
|
304
|
+
});
|
|
305
|
+
/** Render a stored page as markdown, with the plugins' blocks, and anything the reader should be told. */
|
|
306
|
+
async function render(page) {
|
|
307
|
+
const { pages, errors } = await config();
|
|
308
|
+
const notices = errors.map((e) => `config: ${e.message}`);
|
|
309
|
+
let out;
|
|
310
|
+
let rendered = !!pages;
|
|
311
|
+
try {
|
|
312
|
+
out = pages ? await pages.render(page, context()) : rawRender(page);
|
|
313
|
+
} catch (err) {
|
|
314
|
+
console.error("render:", err);
|
|
315
|
+
notices.push(`the page failed to render: ${err.message}`);
|
|
316
|
+
out = rawRender(page);
|
|
317
|
+
rendered = false;
|
|
318
|
+
}
|
|
319
|
+
const strings = {
|
|
320
|
+
...out,
|
|
321
|
+
html: String(out.html ?? ""),
|
|
322
|
+
...out.head != null ? { head: String(out.head) } : {}
|
|
323
|
+
};
|
|
324
|
+
return {
|
|
325
|
+
page: rendered,
|
|
326
|
+
out: strings,
|
|
327
|
+
notices
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Per page: the questions it asks that have no answer yet, the reader's events no wait or watch
|
|
332
|
+
* picked up, who is listening, and the session that claimed it. What `whiteboard status` and the
|
|
333
|
+
* hooks report. The questions are a page's named forms.
|
|
334
|
+
*/
|
|
335
|
+
async function status(only) {
|
|
336
|
+
const { pages, handedOut, forReader } = await config();
|
|
337
|
+
const isForAgent = forAgent(handedOut);
|
|
338
|
+
const all = readEvents(project.eventsFile);
|
|
339
|
+
const picked = pickedUp(all);
|
|
340
|
+
const owner = /* @__PURE__ */ new Map();
|
|
341
|
+
for (const e of all) if (e.type === "whiteboard:claim" && !fromReader(e)) owner.set(e.page, e.data?.session ?? null);
|
|
342
|
+
const out = [];
|
|
343
|
+
for (const p of pagesIn(project.pagesDir)) {
|
|
344
|
+
if (only && p.id !== only && !p.id.startsWith(only + "/")) continue;
|
|
345
|
+
const page = pageSource(p.id, p.file);
|
|
346
|
+
let asks = [];
|
|
347
|
+
try {
|
|
348
|
+
asks = pages?.questions(page) ?? [];
|
|
349
|
+
} catch (err) {
|
|
350
|
+
console.error("questions:", err);
|
|
351
|
+
}
|
|
352
|
+
const events = all.filter((e) => e.page === p.id);
|
|
353
|
+
const listening = listenersFor(p.id);
|
|
354
|
+
const questions = asks.map((name) => ({
|
|
355
|
+
name,
|
|
356
|
+
state: questionState(name, events, listening, (id) => picked.has(id)).state
|
|
357
|
+
}));
|
|
358
|
+
const named = (...states) => questions.filter((q) => states.includes(q.state)).map((q) => q.name);
|
|
359
|
+
out.push({
|
|
360
|
+
page: p.id,
|
|
361
|
+
title: titleOf(p.file, path.basename(p.id)),
|
|
362
|
+
file: p.file,
|
|
363
|
+
owner: owner.get(p.id) ?? null,
|
|
364
|
+
asks,
|
|
365
|
+
questions,
|
|
366
|
+
open: named("open"),
|
|
367
|
+
unheard: named("open").filter((name) => !heard(name, listening)),
|
|
368
|
+
unread: unreadMessages(events, (type) => forReader.has(type)),
|
|
369
|
+
unpicked: events.filter((e) => fromReader(e) && isForAgent(e.type) && !picked.has(e.id)),
|
|
370
|
+
listeners: listening
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
return out;
|
|
374
|
+
}
|
|
375
|
+
/** How to write pages here, as markdown: the pages' own docs, then each plugin's. */
|
|
376
|
+
async function docs() {
|
|
377
|
+
const { pages, errors } = await config();
|
|
378
|
+
const plugins = await Promise.all((await pluginHooks()).map(async (h) => ({
|
|
379
|
+
name: h.name,
|
|
380
|
+
error: h.error?.message ?? null,
|
|
381
|
+
docs: h.plugin.docs ? await h.plugin.docs() : null,
|
|
382
|
+
routes: describeRoutes(h.table),
|
|
383
|
+
commands: pluginCommands(h.plugin).map((c) => ({
|
|
384
|
+
name: c.name,
|
|
385
|
+
usage: usage(c.command),
|
|
386
|
+
description: c.command.description
|
|
387
|
+
})),
|
|
388
|
+
events: Object.entries(h.plugin.events ?? {}).map(([type, spec]) => ({
|
|
389
|
+
type,
|
|
390
|
+
handOut: !!spec?.handOut,
|
|
391
|
+
description: spec?.description ?? ""
|
|
392
|
+
}))
|
|
393
|
+
})));
|
|
394
|
+
return {
|
|
395
|
+
errors: errors.map((e) => e.message),
|
|
396
|
+
pages: pages?.docs(context()) ?? null,
|
|
397
|
+
plugins
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
const json = (s) => JSON.stringify(s).replace(/</g, "\\u003c");
|
|
401
|
+
const DOCK_STYLE = "#wb-dock{position:fixed;right:16px;bottom:calc(16px + var(--wb-inset-bottom,0px));z-index:10;display:flex;flex-direction:column;align-items:flex-end;gap:6px}#wb-dock:empty{display:none}#wb-toasts{position:fixed;left:50%;transform:translateX(-50%);bottom:calc(16px + var(--wb-inset-bottom,0px));z-index:30;display:grid;gap:8px;width:min(440px,100vw - 32px);pointer-events:none}.wb-toast{pointer-events:auto;cursor:pointer;padding:10px 14px;border-radius:8px;font:.85rem/1.45 var(--sans,system-ui,sans-serif);background:var(--bg,Canvas);color:var(--text,CanvasText);border:1px solid var(--border,#8884);box-shadow:0 8px 28px rgb(0 0 0/.18)}.wb-toast[data-kind=warn]{border-left:4px solid var(--warn,#c98a00)}.wb-toast[data-action]{border-left:4px solid var(--accent,#1d5bbf)}";
|
|
402
|
+
/** `extras`: what plugins add to <head> and <body> (see shellExtras). */
|
|
403
|
+
function shell(page, { page: rendered, out, notices }, extras = {
|
|
404
|
+
head: [],
|
|
405
|
+
body: [],
|
|
406
|
+
clients: []
|
|
407
|
+
}) {
|
|
408
|
+
const handoff = {
|
|
409
|
+
id: page.id,
|
|
410
|
+
kind: page.kind,
|
|
411
|
+
client: rendered ? asset(PAGE_CLIENT) : null,
|
|
412
|
+
data: out.data ?? null,
|
|
413
|
+
plugins: extras.clients,
|
|
414
|
+
messages: extras.messages ?? []
|
|
415
|
+
};
|
|
416
|
+
return String(html(_templateObject || (_templateObject = _taggedTemplateLiteral([
|
|
417
|
+
"<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n<title>",
|
|
418
|
+
"</title>\n<style>",
|
|
419
|
+
"</style>\n",
|
|
420
|
+
"",
|
|
421
|
+
"</head>\n<body data-page=\"",
|
|
422
|
+
"\" data-kind=\"",
|
|
423
|
+
"\">\n",
|
|
424
|
+
"\n<div id=\"wb-root\">",
|
|
425
|
+
"</div>\n<div id=\"wb-dock\"></div>\n<script type=\"application/json\" id=\"wb-page\">",
|
|
426
|
+
"<\/script>\n<script type=\"module\" src=\"/_/runtime.js\"><\/script>",
|
|
427
|
+
"</body></html>"
|
|
428
|
+
])), out.title || page.id || project.name, DOCK_STYLE, raw(out.head ?? ""), extras.head, page.id, page.kind, notices.map((n) => html`<pre class="wb-error" role="alert">${n}</pre>`), raw(String(out.html)), raw(json(handoff)), extras.body));
|
|
429
|
+
}
|
|
430
|
+
/** A listener as the server holds it. */
|
|
431
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
432
|
+
const listenersFor = (page) => [...listeners].filter((l) => page == null || covers(l, page)).map(({ page, type, command, fallback }) => ({
|
|
433
|
+
page,
|
|
434
|
+
type,
|
|
435
|
+
command,
|
|
436
|
+
...fallback && { fallback }
|
|
437
|
+
}));
|
|
438
|
+
const clients = /* @__PURE__ */ new Set();
|
|
439
|
+
function notify(client, kind) {
|
|
440
|
+
client.pending ??= /* @__PURE__ */ new Set();
|
|
441
|
+
if (client.pending.size === 0) setTimeout(() => {
|
|
442
|
+
for (const k of client.pending) client.res.write(`event: ${k}\ndata: {}\n\n`);
|
|
443
|
+
client.pending.clear();
|
|
444
|
+
}, 60);
|
|
445
|
+
client.pending.add(kind);
|
|
446
|
+
}
|
|
447
|
+
const notifyAll = (kind) => {
|
|
448
|
+
for (const c of clients) notify(c, kind);
|
|
449
|
+
};
|
|
450
|
+
function pageChanged(id) {
|
|
451
|
+
for (const c of clients) if (c.isDir ? !c.page || id.startsWith(c.page + "/") : c.page === id || c.page + "/index" === id) notify(c, "change");
|
|
452
|
+
for (const c of clients) if (c.isDir && !c.page) notify(c, "change");
|
|
453
|
+
}
|
|
454
|
+
/** A file in the project changed; `f` is its path relative to the project folder, with / separators. */
|
|
455
|
+
function fileChanged(f) {
|
|
456
|
+
if (f.split("/").some((part) => SKIP_DIRS.has(part))) return;
|
|
457
|
+
if (f.startsWith(".whiteboard/")) {
|
|
458
|
+
const inner = f.slice(12);
|
|
459
|
+
if (inner === "events.ndjson") return notifyAll("events");
|
|
460
|
+
if (inner.startsWith("pages/")) {
|
|
461
|
+
if (inner.endsWith(".md")) pageChanged(inner.slice(6).replace(/\.md$/, ""));
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (OWN_FILES.has(inner) || pluginDirs.has(inner.split("/")[0])) return;
|
|
465
|
+
return onSourceChange(path.join(project.dir, f));
|
|
466
|
+
}
|
|
467
|
+
if (!FILES) return;
|
|
468
|
+
pageChanged(`-/${f}`);
|
|
469
|
+
const parent = f.includes("/") ? f.slice(0, f.lastIndexOf("/")) : "";
|
|
470
|
+
pageChanged(`-${parent ? "/" + parent : ""}`);
|
|
471
|
+
}
|
|
472
|
+
let FILES = null;
|
|
473
|
+
let watcher = null;
|
|
474
|
+
function setFiles(files) {
|
|
475
|
+
if (files === FILES) return;
|
|
476
|
+
FILES = files;
|
|
477
|
+
if (watcher) {
|
|
478
|
+
watcher.close();
|
|
479
|
+
watchers.delete(watcher);
|
|
480
|
+
}
|
|
481
|
+
const base = files ? project.dir : project.wb;
|
|
482
|
+
const prefix = files ? "" : ".whiteboard/";
|
|
483
|
+
watcher = fs.watch(base, { recursive: true }, (_evt, filename) => {
|
|
484
|
+
if (filename) fileChanged(prefix + filename.replace(/\\/g, "/"));
|
|
485
|
+
});
|
|
486
|
+
watchers.add(watcher);
|
|
487
|
+
}
|
|
488
|
+
let reloadTimer;
|
|
489
|
+
async function onSourceChange(file) {
|
|
490
|
+
if (within(file, project.pagesDir)) return;
|
|
491
|
+
if (path.dirname(file) === project.wb && OWN_FILES.has(path.basename(file))) return;
|
|
492
|
+
if ([...pluginDirs].some((name) => within(file, path.join(project.wb, name)))) return;
|
|
493
|
+
if (file.endsWith(".css")) return notifyAll("style");
|
|
494
|
+
if (file.endsWith(".md")) return notifyAll("change");
|
|
495
|
+
if (file === RUNTIME || assets.has(file)) return notifyAll("reload");
|
|
496
|
+
clearTimeout(reloadTimer);
|
|
497
|
+
reloadTimer = setTimeout(() => {
|
|
498
|
+
loader.invalidate();
|
|
499
|
+
config().then(() => notifyAll("change"));
|
|
500
|
+
}, 80);
|
|
501
|
+
}
|
|
502
|
+
const ENGINE_BUILD = path.join(ENGINE_DIR, "dist");
|
|
503
|
+
const ENGINE_WATCHED = !FROM_SOURCE ? [] : [ENGINE_BUILD, ...["plugins"].map((d) => path.join(ENGINE_DIR, d))];
|
|
504
|
+
for (const dir of ENGINE_WATCHED) watch(dir, { recursive: true }, (name) => onSourceChange(path.join(dir, name)));
|
|
505
|
+
function send(res, status, body, type = "text/html; charset=utf-8") {
|
|
506
|
+
res.writeHead(status, {
|
|
507
|
+
"content-type": type,
|
|
508
|
+
"cache-control": "no-store"
|
|
509
|
+
});
|
|
510
|
+
res.end(body);
|
|
511
|
+
}
|
|
512
|
+
const sendJson = (res, status, data) => send(res, status, JSON.stringify(data), "application/json");
|
|
513
|
+
const sendFile = (res, file) => send(res, 200, fs.readFileSync(file), MIME[path.extname(file).toLowerCase()] || "application/octet-stream");
|
|
514
|
+
function readBody(req) {
|
|
515
|
+
return new Promise((resolve, reject) => {
|
|
516
|
+
const chunks = [];
|
|
517
|
+
let size = 0;
|
|
518
|
+
req.on("data", (c) => {
|
|
519
|
+
size += c.length;
|
|
520
|
+
if (size > 1e6) {
|
|
521
|
+
reject(/* @__PURE__ */ new Error("body too large"));
|
|
522
|
+
req.destroy();
|
|
523
|
+
} else chunks.push(c);
|
|
524
|
+
});
|
|
525
|
+
req.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
|
|
526
|
+
req.on("error", reject);
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
async function resolve(id, requested) {
|
|
530
|
+
await config();
|
|
531
|
+
if (isRepoId(id) && !FILES) return { notFound: "This project does not serve its files: its config sets files: false." };
|
|
532
|
+
if (id === "_/docs") {
|
|
533
|
+
const { pages, plugins } = await docs();
|
|
534
|
+
const source = [pages ?? "", ...plugins.filter((p) => p.docs).map((p) => `<a id="plugin-${p.name}"></a>\n\n${p.docs}`)].join("\n\n---\n\n");
|
|
535
|
+
return { page: {
|
|
536
|
+
kind: "page",
|
|
537
|
+
id,
|
|
538
|
+
file: path.join(project.pagesDir, "_docs.md"),
|
|
539
|
+
mtime: null,
|
|
540
|
+
source,
|
|
541
|
+
body: source,
|
|
542
|
+
frontmatter: {}
|
|
543
|
+
} };
|
|
544
|
+
}
|
|
545
|
+
const found = stored(id);
|
|
546
|
+
if (!found) return { notFound: `${isRepoId(id) ? id.slice(2) : id} does not exist${isRepoId(id) ? "" : " yet. This page loads as soon as it does"}.` };
|
|
547
|
+
if ("raw" in found) return found;
|
|
548
|
+
if ((found.kind === "folder" || found.kind === "pages" && id) && !requested.endsWith("/")) return { redirect: `/${id}/` };
|
|
549
|
+
if (found.kind === "pages") {
|
|
550
|
+
const counts = new Map((await status(found.id || null)).map((s) => [s.page, awaiting(s)]));
|
|
551
|
+
for (const p of found.pages) p.awaiting = counts.get(p.id) ?? 0;
|
|
552
|
+
}
|
|
553
|
+
return { page: found };
|
|
554
|
+
}
|
|
555
|
+
async function handle(req, res) {
|
|
556
|
+
try {
|
|
557
|
+
const url = new URL((req.url ?? "/").replace(/^\/+/, "/"), "http://localhost");
|
|
558
|
+
const p = decodeURIComponent(url.pathname);
|
|
559
|
+
const q = Object.fromEntries(url.searchParams);
|
|
560
|
+
if (p === "/favicon.ico") {
|
|
561
|
+
res.writeHead(204);
|
|
562
|
+
res.end();
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
if (p === "/_/runtime.js") return sendFile(res, RUNTIME);
|
|
566
|
+
if (auth.handle(req, res, p, q)) return;
|
|
567
|
+
if (p.startsWith("/api/")) {
|
|
568
|
+
const hooks = await pluginHooks();
|
|
569
|
+
for (const h of hooks) if (await dispatch(h.table, req, res, url, auth.authorized)) return;
|
|
570
|
+
const owner = hooks.find((h) => p.startsWith(`/api/${h.name}/`) || p === `/api/${h.name}`);
|
|
571
|
+
if (owner) return sendJson(res, 404, { error: `plugin ${owner.name} has no route ${req.method} ${p}` });
|
|
572
|
+
}
|
|
573
|
+
if (p.startsWith("/_/file/")) {
|
|
574
|
+
const file = path.resolve(p.slice(7));
|
|
575
|
+
if (!servable(file) || !fs.existsSync(file) || !fs.statSync(file).isFile()) return send(res, 404, "not found", "text/plain");
|
|
576
|
+
return sendFile(res, file);
|
|
577
|
+
}
|
|
578
|
+
if (p === "/api/health") return sendJson(res, 200, {
|
|
579
|
+
ok: true,
|
|
580
|
+
dir: project.dir,
|
|
581
|
+
pid: process.pid
|
|
582
|
+
});
|
|
583
|
+
if (p === "/api/events" && req.method === "POST") {
|
|
584
|
+
const body = JSON.parse(await readBody(req));
|
|
585
|
+
if (!body.type) return sendJson(res, 400, { error: "event type required" });
|
|
586
|
+
return sendJson(res, 200, appendEvent(project.eventsFile, body));
|
|
587
|
+
}
|
|
588
|
+
if (p === "/api/markdown" && req.method === "POST") {
|
|
589
|
+
const { texts } = JSON.parse(await readBody(req));
|
|
590
|
+
if (!Array.isArray(texts)) return sendJson(res, 400, { error: "texts: an array of strings" });
|
|
591
|
+
const { pages } = await config();
|
|
592
|
+
const out = await Promise.all(texts.map((t) => pages ? pages.message(String(t ?? ""), context()) : Promise.resolve({
|
|
593
|
+
html: String(html`${String(t ?? "")}`),
|
|
594
|
+
styles: []
|
|
595
|
+
})));
|
|
596
|
+
return sendJson(res, 200, {
|
|
597
|
+
html: out.map((o) => o.html),
|
|
598
|
+
styles: [...new Set(out.flatMap((o) => o.styles))]
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
if (p === "/api/events") {
|
|
602
|
+
const all = readEvents(project.eventsFile);
|
|
603
|
+
const picked = pickedUp(all);
|
|
604
|
+
return sendJson(res, 200, all.filter((e) => matches(e, q)).map((e) => picked.has(e.id) ? {
|
|
605
|
+
...e,
|
|
606
|
+
pickedUp: true
|
|
607
|
+
} : e));
|
|
608
|
+
}
|
|
609
|
+
if (p === "/api/docs") return sendJson(res, 200, await docs());
|
|
610
|
+
if (p === "/api/status") return sendJson(res, 200, await status(q.page || null));
|
|
611
|
+
if (p === "/api/listeners") return sendJson(res, 200, listenersFor(q.page ?? null));
|
|
612
|
+
if (p === "/api/listen") {
|
|
613
|
+
const fallback = "fallback" in q;
|
|
614
|
+
if (fallback && [...listeners].some((l) => l.fallback)) return sendJson(res, 409, { error: "another agent is the fallback already" });
|
|
615
|
+
res.writeHead(200, {
|
|
616
|
+
"content-type": "text/event-stream",
|
|
617
|
+
"cache-control": "no-store",
|
|
618
|
+
connection: "keep-alive"
|
|
619
|
+
});
|
|
620
|
+
res.write(": listening\n\n");
|
|
621
|
+
const l = {
|
|
622
|
+
page: q.page || "",
|
|
623
|
+
type: q.type || null,
|
|
624
|
+
command: q.command === "wait" ? "wait" : "watch",
|
|
625
|
+
...fallback && { fallback }
|
|
626
|
+
};
|
|
627
|
+
listeners.add(l);
|
|
628
|
+
notifyAll("listeners");
|
|
629
|
+
const ping = setInterval(() => res.write(": ping\n\n"), 25e3);
|
|
630
|
+
const end = () => {
|
|
631
|
+
clearInterval(ping);
|
|
632
|
+
listeners.delete(l);
|
|
633
|
+
streams.delete(end);
|
|
634
|
+
};
|
|
635
|
+
streams.add(() => {
|
|
636
|
+
end();
|
|
637
|
+
res.end();
|
|
638
|
+
});
|
|
639
|
+
req.on("close", () => {
|
|
640
|
+
end();
|
|
641
|
+
notifyAll("listeners");
|
|
642
|
+
});
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
if (p === "/api/page" || p === "/api/render") {
|
|
646
|
+
const r = await resolve(q.page ?? "", "/");
|
|
647
|
+
if (!("page" in r)) return sendJson(res, 404, { error: "notFound" in r ? r.notFound : "not a page" });
|
|
648
|
+
if (p === "/api/page") return sendJson(res, 200, r.page);
|
|
649
|
+
const { out, notices } = await render(r.page);
|
|
650
|
+
return sendJson(res, 200, {
|
|
651
|
+
...out,
|
|
652
|
+
notices,
|
|
653
|
+
mtime: r.page.mtime
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
if (p === "/api/watch") {
|
|
657
|
+
res.writeHead(200, {
|
|
658
|
+
"content-type": "text/event-stream",
|
|
659
|
+
"cache-control": "no-store",
|
|
660
|
+
connection: "keep-alive"
|
|
661
|
+
});
|
|
662
|
+
res.write(": connected\n\n");
|
|
663
|
+
const client = {
|
|
664
|
+
res,
|
|
665
|
+
page: q.page || "",
|
|
666
|
+
isDir: "dir" in q
|
|
667
|
+
};
|
|
668
|
+
clients.add(client);
|
|
669
|
+
const ping = setInterval(() => res.write(": ping\n\n"), 25e3);
|
|
670
|
+
const end = () => {
|
|
671
|
+
clearInterval(ping);
|
|
672
|
+
clients.delete(client);
|
|
673
|
+
streams.delete(end);
|
|
674
|
+
};
|
|
675
|
+
streams.add(() => {
|
|
676
|
+
end();
|
|
677
|
+
res.end();
|
|
678
|
+
});
|
|
679
|
+
req.on("close", end);
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
const path_ = p.replace(/^\/+/, "").replace(/\/+$/, "");
|
|
683
|
+
const id = isRepoId(path_) ? path_ : path_.replace(/\.md$/, "");
|
|
684
|
+
const r = await resolve(id, p);
|
|
685
|
+
if ("redirect" in r) {
|
|
686
|
+
res.writeHead(302, { location: r.redirect + url.search });
|
|
687
|
+
res.end();
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
if ("raw" in r) return sendFile(res, r.raw);
|
|
691
|
+
if ("notFound" in r) return send(res, 404, await page(req, {
|
|
692
|
+
kind: "page",
|
|
693
|
+
id,
|
|
694
|
+
file: "",
|
|
695
|
+
mtime: null,
|
|
696
|
+
source: r.notFound,
|
|
697
|
+
body: r.notFound,
|
|
698
|
+
frontmatter: {}
|
|
699
|
+
}, {
|
|
700
|
+
page: false,
|
|
701
|
+
out: {
|
|
702
|
+
title: "Not found",
|
|
703
|
+
html: html`<h1>Not found</h1><p>${r.notFound}</p>`
|
|
704
|
+
},
|
|
705
|
+
notices: []
|
|
706
|
+
}));
|
|
707
|
+
return send(res, 200, await page(req, r.page, await render(r.page)));
|
|
708
|
+
} catch (err) {
|
|
709
|
+
console.error(err);
|
|
710
|
+
send(res, 500, String(err?.stack || err), "text/plain");
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
return {
|
|
714
|
+
handle,
|
|
715
|
+
status: (page = null) => status(page),
|
|
716
|
+
config,
|
|
717
|
+
close() {
|
|
718
|
+
for (const w of watchers) w.close();
|
|
719
|
+
watchers.clear();
|
|
720
|
+
for (const stop of [...streams]) stop();
|
|
721
|
+
clearTimeout(reloadTimer);
|
|
722
|
+
}
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
export { createServer };
|