@bigmistqke/workpad 0.0.0-stage → 0.18.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 +781 -2
- package/bin/workpad.ts +1509 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/workpad.js +1190 -0
- package/dist/client/runtime.js +1194 -0
- package/dist/define.js +27 -0
- package/dist/plugins/alert/README.md +18 -0
- package/dist/plugins/alert/index.js +40 -0
- package/dist/plugins/alert/public/style.css +40 -0
- package/dist/plugins/ansi/README.md +36 -0
- package/dist/plugins/ansi/index.js +63 -0
- package/dist/plugins/ansi/public/style.css +28 -0
- package/dist/plugins/badge/README.md +32 -0
- package/dist/plugins/badge/index.js +28 -0
- package/dist/plugins/badge/public/style.css +30 -0
- package/dist/plugins/chat/README.md +81 -0
- package/dist/plugins/chat/index.js +96 -0
- package/dist/plugins/chat/public/client.js +3 -0
- package/dist/plugins/chat/public/style.css +143 -0
- package/dist/plugins/claude/README.md +116 -0
- package/dist/plugins/claude/channel.js +130 -0
- package/dist/plugins/claude/doctor.js +316 -0
- package/dist/plugins/claude/index.js +199 -0
- package/dist/plugins/claude/session.js +68 -0
- package/dist/plugins/code/README.md +54 -0
- package/dist/plugins/code/diff.js +75 -0
- package/dist/plugins/code/index.js +105 -0
- package/dist/plugins/code/public/client.js +1 -0
- package/dist/plugins/code/public/style.css +99 -0
- package/dist/plugins/comments/README.md +30 -0
- package/dist/plugins/comments/index.js +39 -0
- package/dist/plugins/comments/public/client.js +24 -0
- package/dist/plugins/comments/public/style.css +224 -0
- package/dist/plugins/details/README.md +24 -0
- package/dist/plugins/details/index.js +28 -0
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/draft/README.md +14 -0
- package/dist/plugins/draft/index.js +27 -0
- package/dist/plugins/draft/public/style.css +21 -0
- package/dist/plugins/forge/README.md +22 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +126 -0
- package/dist/plugins/form/index.js +179 -0
- package/dist/plugins/form/public/style.css +172 -0
- package/dist/plugins/grid/README.md +45 -0
- package/dist/plugins/grid/index.js +35 -0
- package/dist/plugins/grid/public/style.css +98 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/index.js +49 -0
- package/dist/plugins/iframe/public/style.css +28 -0
- package/dist/plugins/index.js +44 -0
- package/dist/plugins/lightbox/README.md +24 -0
- package/dist/plugins/lightbox/index.js +20 -0
- package/dist/plugins/lightbox/public/client.js +4 -0
- package/dist/plugins/lightbox/public/style.css +66 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +42 -0
- package/dist/plugins/mermaid/public/style.css +21 -0
- package/dist/plugins/opencode/README.md +48 -0
- package/dist/plugins/opencode/channel.js +92 -0
- package/dist/plugins/opencode/index.js +64 -0
- package/dist/plugins/opencode/shim.js +150 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +23 -0
- package/dist/plugins/tabs/README.md +44 -0
- package/dist/plugins/tabs/index.js +75 -0
- package/dist/plugins/tabs/public/client.js +1 -0
- package/dist/plugins/tabs/public/style.css +98 -0
- package/dist/plugins/terminal/README.md +63 -0
- package/dist/plugins/terminal/host.js +338 -0
- package/dist/plugins/terminal/index.js +170 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/public/client.js +41 -0
- package/dist/plugins/terminal/public/style.css +511 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +58 -0
- package/dist/plugins/timeline/README.md +48 -0
- package/dist/plugins/timeline/index.js +83 -0
- package/dist/plugins/timeline/public/style.css +138 -0
- package/dist/plugins/toc/README.md +40 -0
- package/dist/plugins/toc/index.js +29 -0
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +117 -0
- package/dist/plugins/window/README.md +51 -0
- package/dist/plugins/window/index.js +91 -0
- package/dist/solid/index.js +41 -0
- package/dist/src/agents.js +81 -0
- package/dist/src/anchors.js +52 -0
- package/dist/src/api.js +282 -0
- package/dist/src/archive.js +49 -0
- package/dist/src/auth.js +67 -0
- package/dist/src/build.js +159 -0
- package/dist/src/client/page/base.css +1762 -0
- package/dist/src/client/page/main.js +2098 -0
- package/dist/src/config.js +76 -0
- package/dist/src/document.js +93 -0
- package/dist/src/entries.js +264 -0
- package/dist/src/events.js +322 -0
- package/dist/src/files.js +47 -0
- package/dist/src/formstate.js +65 -0
- package/dist/src/help.js +211 -0
- package/dist/src/html.js +34 -0
- package/dist/src/hub.js +341 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/listen.js +132 -0
- package/dist/src/listeners.js +64 -0
- package/dist/src/load.js +81 -0
- package/dist/src/loader.js +179 -0
- package/dist/src/migrate.js +314 -0
- package/dist/src/package.js +52 -0
- package/dist/src/pages/blocks.js +67 -0
- package/dist/src/pages/board.js +59 -0
- package/dist/src/pages/index.js +27 -0
- package/dist/src/pages/markdown.js +863 -0
- package/dist/src/pages/render.js +665 -0
- package/dist/src/pages/window.js +29 -0
- package/dist/src/plugins.js +283 -0
- package/dist/src/public.js +24 -0
- package/dist/src/rename.js +130 -0
- package/dist/src/running.js +146 -0
- package/dist/src/serve.js +188 -0
- package/dist/src/server.js +1105 -0
- package/dist/src/site.js +179 -0
- package/dist/src/state.js +155 -0
- package/dist/src/status.js +323 -0
- package/dist/src/thread.js +68 -0
- package/dist/src/watchdog.js +38 -0
- package/dist/src/ws.js +128 -0
- package/package.json +154 -4
- package/plugins/README.md +304 -0
- package/plugins/alert/README.md +18 -0
- package/plugins/alert/index.ts +50 -0
- package/plugins/alert/public/style.css +40 -0
- package/plugins/ansi/README.md +36 -0
- package/plugins/ansi/index.ts +95 -0
- package/plugins/ansi/public/style.css +28 -0
- package/plugins/badge/README.md +32 -0
- package/plugins/badge/index.ts +36 -0
- package/plugins/badge/public/style.css +30 -0
- package/plugins/chat/README.md +81 -0
- package/plugins/chat/autosize.ts +139 -0
- package/plugins/chat/client.tsx +187 -0
- package/plugins/chat/index.ts +146 -0
- package/plugins/chat/public/style.css +143 -0
- package/plugins/claude/README.md +116 -0
- package/plugins/claude/channel.ts +136 -0
- package/plugins/claude/doctor.ts +274 -0
- package/plugins/claude/index.ts +248 -0
- package/plugins/claude/session.ts +70 -0
- package/plugins/code/README.md +54 -0
- package/plugins/code/client.ts +73 -0
- package/plugins/code/diff.ts +96 -0
- package/plugins/code/index.ts +136 -0
- package/plugins/code/public/style.css +99 -0
- package/plugins/comments/README.md +30 -0
- package/plugins/comments/client.ts +723 -0
- package/plugins/comments/index.ts +48 -0
- package/plugins/comments/public/style.css +224 -0
- package/plugins/details/README.md +24 -0
- package/plugins/details/index.ts +30 -0
- package/plugins/details/public/style.css +7 -0
- package/plugins/draft/README.md +14 -0
- package/plugins/draft/index.ts +23 -0
- package/plugins/draft/public/style.css +21 -0
- package/plugins/forge/README.md +22 -0
- package/plugins/forge/index.ts +84 -0
- package/plugins/form/README.md +126 -0
- package/plugins/form/index.ts +199 -0
- package/plugins/form/public/style.css +172 -0
- package/plugins/grid/README.md +45 -0
- package/plugins/grid/index.ts +41 -0
- package/plugins/grid/public/style.css +98 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/index.ts +53 -0
- package/plugins/iframe/public/style.css +28 -0
- package/plugins/index.ts +49 -0
- package/plugins/lightbox/README.md +24 -0
- package/plugins/lightbox/client.ts +109 -0
- package/plugins/lightbox/index.ts +25 -0
- package/plugins/lightbox/public/style.css +66 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +46 -0
- package/plugins/mermaid/public/style.css +21 -0
- package/plugins/opencode/README.md +48 -0
- package/plugins/opencode/channel.ts +112 -0
- package/plugins/opencode/index.ts +91 -0
- package/plugins/opencode/shim.ts +166 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +28 -0
- package/plugins/tabs/README.md +44 -0
- package/plugins/tabs/client.ts +96 -0
- package/plugins/tabs/index.ts +103 -0
- package/plugins/tabs/public/style.css +98 -0
- package/plugins/terminal/README.md +63 -0
- package/plugins/terminal/client.ts +391 -0
- package/plugins/terminal/host.ts +434 -0
- package/plugins/terminal/index.ts +199 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +266 -0
- package/plugins/terminal/sizes.ts +72 -0
- package/plugins/timeline/README.md +48 -0
- package/plugins/timeline/index.ts +82 -0
- package/plugins/timeline/public/style.css +138 -0
- package/plugins/toc/README.md +40 -0
- package/plugins/toc/client.ts +236 -0
- package/plugins/toc/index.ts +55 -0
- package/plugins/toc/public/style.css +117 -0
- package/plugins/window/README.md +51 -0
- package/plugins/window/index.ts +92 -0
- package/skills/workpad/SKILL.md +588 -0
- package/solid/index.tsx +80 -0
- package/src/agents.ts +133 -0
- package/src/anchors.ts +85 -0
- package/src/api.ts +444 -0
- package/src/archive.ts +75 -0
- package/src/auth.ts +99 -0
- package/src/build.ts +213 -0
- package/src/client/base.ts +19 -0
- package/src/client/forms.ts +420 -0
- package/src/client/markdown.ts +147 -0
- package/src/client/page/base.css +1762 -0
- package/src/client/page/board.ts +137 -0
- package/src/client/page/elsewhere.ts +153 -0
- package/src/client/page/fresh.ts +343 -0
- package/src/client/page/heading-link.ts +32 -0
- package/src/client/page/live.ts +134 -0
- package/src/client/page/main.ts +398 -0
- package/src/client/page/orphans.ts +64 -0
- package/src/client/page/place.ts +260 -0
- package/src/client/page/sort.ts +49 -0
- package/src/client/page/sticky.ts +58 -0
- package/src/client/page/theme.ts +36 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +278 -0
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +111 -0
- package/src/client/runtime.ts +499 -0
- package/src/client/scroll.ts +90 -0
- package/src/client/toast.ts +99 -0
- package/src/config.ts +111 -0
- package/src/define.ts +997 -0
- package/src/document.ts +191 -0
- package/src/entries.ts +352 -0
- package/src/events.ts +463 -0
- package/src/files.ts +74 -0
- package/src/formstate.ts +72 -0
- package/src/help.ts +238 -0
- package/src/html.ts +44 -0
- package/src/hub.ts +426 -0
- package/src/icons.ts +18 -0
- package/src/listen.ts +171 -0
- package/src/listeners.ts +89 -0
- package/src/load.ts +113 -0
- package/src/loader.ts +227 -0
- package/src/migrate.ts +352 -0
- package/src/package.ts +62 -0
- package/src/pages/blocks.ts +106 -0
- package/src/pages/board.ts +89 -0
- package/src/pages/index.ts +79 -0
- package/src/pages/markdown.ts +976 -0
- package/src/pages/render.ts +748 -0
- package/src/pages/window.ts +69 -0
- package/src/plugins.ts +319 -0
- package/src/public.ts +39 -0
- package/src/rename.ts +147 -0
- package/src/running.ts +171 -0
- package/src/serve.ts +221 -0
- package/src/server.ts +1385 -0
- package/src/site.ts +170 -0
- package/src/state.ts +327 -0
- package/src/status.ts +466 -0
- package/src/thread.ts +98 -0
- package/src/watchdog.ts +53 -0
- package/src/ws.ts +155 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { html, raw } from "../html.js";
|
|
2
|
+
// src/pages/window.ts
|
|
3
|
+
const svg = (d) => raw(`<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round">${d}</svg>`);
|
|
4
|
+
const OPEN = svg("<path d=\"M9 2.5h4.5V7M13.5 2.5 7.5 8.5M11.5 9.5v3a1 1 0 0 1-1 1h-7a1 1 0 0 1-1-1v-7a1 1 0 0 1 1-1h3\"/>");
|
|
5
|
+
const MAXIMIZE = svg("<path d=\"M2.5 6V2.5H6M10 2.5h3.5V6M13.5 10v3.5H10M6 13.5H2.5V10\"/>");
|
|
6
|
+
const RESTORE = svg("<path d=\"M6 2.5V6H2.5M13.5 6H10V2.5M10 13.5V10h3.5M2.5 10H6v3.5\"/>");
|
|
7
|
+
/** A window around a block's HTML: see the top of this file. */
|
|
8
|
+
function windowFrame({ body, label = null, open = null, wide = false, height, className = "", controls = "" }) {
|
|
9
|
+
const name = label != null && String(label) !== "" ? html`<span class="wp-window-title">${raw(String(label))}</span>` : open ? html`<span class="wp-window-address">${open}</span>` : html``;
|
|
10
|
+
const header = html`<div class="wp-window-bar">
|
|
11
|
+
<span class="wp-window-name" ${open ? html` title="${open}"` : ""}>${name}</span>
|
|
12
|
+
${raw(String(controls))}
|
|
13
|
+
${open && html`<a class="wp-window-button" href="${open}" target="_blank" rel="noopener" title="Open on its own" aria-label="Open on its own">${OPEN}</a>`}
|
|
14
|
+
<button
|
|
15
|
+
type="button"
|
|
16
|
+
class="wp-window-button"
|
|
17
|
+
data-wp-maximize
|
|
18
|
+
title="Fill the window"
|
|
19
|
+
aria-label="Fill the window"
|
|
20
|
+
>
|
|
21
|
+
<span class="wp-window-max">${MAXIMIZE}</span><span class="wp-window-restore">${RESTORE}</span>
|
|
22
|
+
</button>
|
|
23
|
+
</div>`;
|
|
24
|
+
return html`<div class="wp-window${wide && raw(" wp-wide")}${className && raw(" " + className)}">
|
|
25
|
+
${header}
|
|
26
|
+
<div class="wp-window-body" ${height ? html` style="height: ${height}"` : ""}>${raw(String(body))}</div>
|
|
27
|
+
</div>`;
|
|
28
|
+
}
|
|
29
|
+
export { windowFrame };
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
// src/plugins.ts
|
|
3
|
+
const TYPES = {
|
|
4
|
+
string: (v) => typeof v === "string",
|
|
5
|
+
number: (v) => typeof v === "number" && Number.isFinite(v),
|
|
6
|
+
boolean: (v) => typeof v === "boolean",
|
|
7
|
+
"string[]": (v) => Array.isArray(v) && v.every((x) => typeof x === "string")
|
|
8
|
+
};
|
|
9
|
+
function parseType(spec, where) {
|
|
10
|
+
const optional = spec.endsWith("?");
|
|
11
|
+
const type = optional ? spec.slice(0, -1) : spec;
|
|
12
|
+
if (!(type in TYPES)) throw new Error(`${where}: unknown type "${spec}" (one of ${Object.keys(TYPES).join(", ")}, with ? when optional)`);
|
|
13
|
+
return {
|
|
14
|
+
type,
|
|
15
|
+
optional
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** What is wrong with `value` against `spec` ({ name: 'string?' }), or null. Unknown fields are wrong too. */
|
|
19
|
+
function checkFields(spec = {}, input = {}, where) {
|
|
20
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) return `${where} must be an object`;
|
|
21
|
+
const value = input;
|
|
22
|
+
for (const [name, s] of Object.entries(spec)) {
|
|
23
|
+
const { type, optional } = parseType(s, where);
|
|
24
|
+
if (value[name] === void 0) {
|
|
25
|
+
if (!optional) return `${where}: ${name} is missing`;
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (!TYPES[type](value[name])) return `${where}: ${name} must be ${type}`;
|
|
29
|
+
}
|
|
30
|
+
for (const name of Object.keys(value)) if (!(name in spec)) return `${where}: unknown field ${name}`;
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
/** Query strings are text: a number or boolean in the spec is read from it. */
|
|
34
|
+
function fromQuery(spec = {}, params) {
|
|
35
|
+
const out = {};
|
|
36
|
+
for (const [name, value] of params) {
|
|
37
|
+
const field = spec[name];
|
|
38
|
+
const type = field ? parseType(field, "query").type : "string";
|
|
39
|
+
out[name] = type === "number" ? Number(value) : type === "boolean" ? value === "true" || value === "" : value;
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
const ROUTE = /^(GET|POST|PUT|PATCH|DELETE) (\/[\w\-/]*)$/;
|
|
44
|
+
/** A plugin's routes, checked and mounted under /api/<name>/. Throws on a malformed one. */
|
|
45
|
+
function mountRoutes(name, routes = {}) {
|
|
46
|
+
const table = [];
|
|
47
|
+
for (const [key, route] of Object.entries(routes)) {
|
|
48
|
+
const m = ROUTE.exec(key);
|
|
49
|
+
if (!m) throw new Error(`plugin ${name}: route "${key}" must look like "GET /path"`);
|
|
50
|
+
if (typeof route?.run !== "function") throw new Error(`plugin ${name}: route "${key}" has no run function`);
|
|
51
|
+
for (const [field, spec] of Object.entries({
|
|
52
|
+
...route.body,
|
|
53
|
+
...route.query
|
|
54
|
+
})) parseType(spec, `plugin ${name}, route "${key}", ${field}`);
|
|
55
|
+
const [, method, at] = m;
|
|
56
|
+
const path = `/api/${name}${at === "/" ? "" : at.replace(/\/$/, "")}`;
|
|
57
|
+
if (table.some((r) => r.method === method && r.path === path)) throw new Error(`plugin ${name}: route "${key}" is declared twice`);
|
|
58
|
+
table.push({
|
|
59
|
+
method,
|
|
60
|
+
path,
|
|
61
|
+
route,
|
|
62
|
+
plugin: name
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return table;
|
|
66
|
+
}
|
|
67
|
+
function readJson(req) {
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
let body = "";
|
|
70
|
+
req.on("data", (c) => {
|
|
71
|
+
body += c;
|
|
72
|
+
if (body.length > 1e6) {
|
|
73
|
+
reject(/* @__PURE__ */ new Error("body too large"));
|
|
74
|
+
req.destroy();
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
req.on("end", () => {
|
|
78
|
+
try {
|
|
79
|
+
resolve(body ? JSON.parse(body) : {});
|
|
80
|
+
} catch {
|
|
81
|
+
reject(/* @__PURE__ */ new Error("the body is not JSON"));
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
req.on("error", reject);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const json = (res, status, data) => {
|
|
88
|
+
res.writeHead(status, {
|
|
89
|
+
"content-type": "application/json",
|
|
90
|
+
"cache-control": "no-store"
|
|
91
|
+
});
|
|
92
|
+
res.end(JSON.stringify(data));
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Answer a request with the route it matches: false when none does. The core checks the token
|
|
96
|
+
* (`auth`), the query and the body before the route's code runs. A route returns what is sent as
|
|
97
|
+
* JSON; a stream route (`stream: true`) writes to `res` itself. An error it throws is sent with its
|
|
98
|
+
* `status` (default 500).
|
|
99
|
+
*/
|
|
100
|
+
async function dispatch(table, req, res, url, authorized) {
|
|
101
|
+
const forPath = table.filter((r) => r.path === url.pathname);
|
|
102
|
+
if (!forPath.length) return false;
|
|
103
|
+
const hit = forPath.find((r) => r.method === req.method);
|
|
104
|
+
if (!hit) {
|
|
105
|
+
res.setHeader("allow", forPath.map((r) => r.method).join(", "));
|
|
106
|
+
json(res, 405, { error: `use ${forPath.map((r) => r.method).join(" or ")}` });
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
const { route } = hit;
|
|
110
|
+
if (route.auth && !authorized(req)) {
|
|
111
|
+
json(res, 403, { error: "this needs the project's token: open a link from the command line, or send x-workpad-token" });
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
const query = fromQuery(route.query, url.searchParams);
|
|
116
|
+
const queryError = route.query ? checkFields(route.query, query, "query") : null;
|
|
117
|
+
if (queryError) {
|
|
118
|
+
json(res, 400, { error: queryError });
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
let body = {};
|
|
122
|
+
if (hit.method !== "GET" && hit.method !== "DELETE") {
|
|
123
|
+
const input = await readJson(req);
|
|
124
|
+
const bodyError = checkFields(route.body, input, "body");
|
|
125
|
+
if (bodyError) {
|
|
126
|
+
json(res, 400, { error: bodyError });
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
body = input;
|
|
130
|
+
}
|
|
131
|
+
if (route.stream) {
|
|
132
|
+
await route.run({
|
|
133
|
+
body,
|
|
134
|
+
query,
|
|
135
|
+
req,
|
|
136
|
+
res
|
|
137
|
+
});
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
const out = await route.run({
|
|
141
|
+
body,
|
|
142
|
+
query,
|
|
143
|
+
req
|
|
144
|
+
});
|
|
145
|
+
json(res, 200, out ?? { ok: true });
|
|
146
|
+
} catch (err) {
|
|
147
|
+
if (res.headersSent) {
|
|
148
|
+
res.end();
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
const e = err;
|
|
152
|
+
json(res, e?.status ?? 500, { error: e?.message ?? String(err) });
|
|
153
|
+
}
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
/** The routes for the docs: method, path, what it does, what it takes. */
|
|
157
|
+
const describeRoutes = (table) => table.map(({ method, path, route }) => ({
|
|
158
|
+
method,
|
|
159
|
+
path,
|
|
160
|
+
description: route.description ?? "",
|
|
161
|
+
auth: !!route.auth,
|
|
162
|
+
stream: !!route.stream,
|
|
163
|
+
body: route.body ?? null,
|
|
164
|
+
query: route.query ?? null
|
|
165
|
+
}));
|
|
166
|
+
/** The commands of a plugin as `workpad <name> [sub]`, checked. */
|
|
167
|
+
function pluginCommands(plugin) {
|
|
168
|
+
return Object.entries(plugin.commands ?? {}).map(([sub, command]) => {
|
|
169
|
+
const where = `plugin ${plugin.name}, command "${sub}"`;
|
|
170
|
+
if (sub && !/^[\w-]+$/.test(sub)) throw new Error(`${where}: use letters, digits, - and _`);
|
|
171
|
+
checkCommand(command, where);
|
|
172
|
+
return {
|
|
173
|
+
plugin: plugin.name,
|
|
174
|
+
from: "plugin",
|
|
175
|
+
sub,
|
|
176
|
+
command,
|
|
177
|
+
name: [plugin.name, sub].filter(Boolean).join(" ")
|
|
178
|
+
};
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
function checkCommand(command, where) {
|
|
182
|
+
if (typeof command?.run !== "function") throw new Error(`${where} has no run function`);
|
|
183
|
+
if (command.live !== void 0 && typeof command.live !== "boolean") throw new Error(`${where}: live is true or false`);
|
|
184
|
+
for (const [name, o] of Object.entries(command.options ?? {})) parseType(o.type, `${where}, option ${name}`);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* The config's own commands (`commands` in defineConfig), as `workpad <name>`, checked: a name is
|
|
188
|
+
* letters, digits, - and _, and is none of `taken` (the built-in commands, the core's folders) nor a
|
|
189
|
+
* plugin's name, which would be two commands under one name.
|
|
190
|
+
*/
|
|
191
|
+
function configCommands(commands, { taken, plugins, where }) {
|
|
192
|
+
return Object.entries(commands ?? {}).map(([name, command]) => {
|
|
193
|
+
const at = `command "${name}" in ${where}`;
|
|
194
|
+
if (!/^[\w-]+$/.test(name)) throw new Error(`${at}: name it with letters, digits, - and _`);
|
|
195
|
+
if (taken.has(name)) throw new Error(`${at}: \`workpad ${name}\` is workpad's own (a built-in command or a folder of the core); name it otherwise`);
|
|
196
|
+
if (plugins.includes(name)) throw new Error(`${at}: the plugin ${name} has that name, and its commands are \`workpad ${name}\`; name it otherwise`);
|
|
197
|
+
checkCommand(command, at);
|
|
198
|
+
return {
|
|
199
|
+
plugin: name,
|
|
200
|
+
from: "config",
|
|
201
|
+
sub: "",
|
|
202
|
+
command,
|
|
203
|
+
name
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
/** Do these arguments ask for help (--help or -h before any `--`)? */
|
|
208
|
+
function asksHelp(args) {
|
|
209
|
+
const cut = args.indexOf("--");
|
|
210
|
+
return (cut < 0 ? args : args.slice(0, cut)).some((a) => a === "--help" || a === "-h");
|
|
211
|
+
}
|
|
212
|
+
/** `workpad <plugin> [sub] --help`: its usage, description, and each option with what it means. */
|
|
213
|
+
function pluginCommandHelp({ name, command }) {
|
|
214
|
+
const options = Object.entries(command.options ?? {}).map(([key, o]) => {
|
|
215
|
+
const { type } = parseType(o.type, key);
|
|
216
|
+
return ` ${`--${key}${type === "boolean" ? "" : ` <${o.value ?? type}>`}${o.short ? `, -${o.short}` : ""}`.padEnd(22)} ${o.description ?? ""}`.trimEnd();
|
|
217
|
+
});
|
|
218
|
+
return [
|
|
219
|
+
`usage: workpad ${[name, usage(command)].filter(Boolean).join(" ")}`,
|
|
220
|
+
"",
|
|
221
|
+
command.description,
|
|
222
|
+
...options.length ? ["", ...options] : [],
|
|
223
|
+
...command.rest ? ["", `after --: ${command.rest}, passed on as they are`] : []
|
|
224
|
+
].join("\n");
|
|
225
|
+
}
|
|
226
|
+
/** `[page] [--restart] [-- <arguments>]` */
|
|
227
|
+
function usage(command) {
|
|
228
|
+
const options = Object.entries(command.options ?? {}).map(([name, o]) => {
|
|
229
|
+
const { type } = parseType(o.type, name);
|
|
230
|
+
return `[--${name}${type === "boolean" ? "" : ` <${o.value ?? type}>`}]`;
|
|
231
|
+
});
|
|
232
|
+
return [
|
|
233
|
+
command.args ?? "",
|
|
234
|
+
...options,
|
|
235
|
+
command.rest ? `[-- ${command.rest}]` : ""
|
|
236
|
+
].filter(Boolean).join(" ");
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Parse a command's arguments by its declared options; throws with the usage on a wrong one. What
|
|
240
|
+
* follows `--` is passed on untouched, as `rest`, when the command takes it.
|
|
241
|
+
*/
|
|
242
|
+
function parseCommandArgs(command, all, name) {
|
|
243
|
+
const cut = all.indexOf("--");
|
|
244
|
+
const args = cut < 0 ? all : all.slice(0, cut);
|
|
245
|
+
const rest = cut < 0 ? [] : all.slice(cut + 1);
|
|
246
|
+
if (rest.length && !command.rest) throw new Error(`workpad ${name} takes nothing after --\nusage: workpad ${name} ${usage(command)}`);
|
|
247
|
+
const options = Object.fromEntries(Object.entries(command.options ?? {}).map(([key, o]) => {
|
|
248
|
+
const { type } = parseType(o.type, key);
|
|
249
|
+
return [key, {
|
|
250
|
+
type: type === "boolean" ? "boolean" : "string",
|
|
251
|
+
...o.short ? { short: o.short } : {}
|
|
252
|
+
}];
|
|
253
|
+
}));
|
|
254
|
+
let parsed;
|
|
255
|
+
try {
|
|
256
|
+
parsed = parseArgs({
|
|
257
|
+
args,
|
|
258
|
+
options,
|
|
259
|
+
allowPositionals: true,
|
|
260
|
+
strict: true
|
|
261
|
+
});
|
|
262
|
+
} catch (err) {
|
|
263
|
+
throw new Error(`${err.message}\nusage: workpad ${name} ${usage(command)}`);
|
|
264
|
+
}
|
|
265
|
+
const values = { ...parsed.values };
|
|
266
|
+
for (const [key, o] of Object.entries(command.options ?? {})) {
|
|
267
|
+
const { type, optional } = parseType(o.type, key);
|
|
268
|
+
if (values[key] === void 0) {
|
|
269
|
+
if (!optional && type !== "boolean") throw new Error(`--${key} is missing\nusage: workpad ${name} ${usage(command)}`);
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
if (type === "number") {
|
|
273
|
+
values[key] = Number(values[key]);
|
|
274
|
+
if (!Number.isFinite(values[key])) throw new Error(`--${key} must be a number`);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return {
|
|
278
|
+
options: values,
|
|
279
|
+
positionals: parsed.positionals,
|
|
280
|
+
rest
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
export { asksHelp, configCommands, describeRoutes, dispatch, mountRoutes, parseCommandArgs, pluginCommandHelp, pluginCommands, usage };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
// src/public.ts
|
|
4
|
+
const asPath = (ref) => ref instanceof URL || ref.startsWith("file:") ? fileURLToPath(ref) : ref;
|
|
5
|
+
/** The folder a plugin's browser files are served from. */
|
|
6
|
+
function publicDirOf(plugin, wp) {
|
|
7
|
+
return path.resolve(plugin.public != null ? asPath(plugin.public) : path.join(wp, "public"));
|
|
8
|
+
}
|
|
9
|
+
/** Is `file` inside `dir`? */
|
|
10
|
+
const within = (file, dir) => {
|
|
11
|
+
const rel = path.relative(dir, file);
|
|
12
|
+
return !!rel && !rel.startsWith("..") && !path.isAbsolute(rel);
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* A file a plugin names, as a path: a name within its public folder (`style.css`,
|
|
16
|
+
* `client/main.js`), or a URL or absolute path, which must be inside it. Anything else is an
|
|
17
|
+
* error, said when the config loads, rather than a file that quietly isn't served.
|
|
18
|
+
*/
|
|
19
|
+
function inPublic(dir, ref, what) {
|
|
20
|
+
const file = typeof ref === "string" && !ref.startsWith("file:") && !path.isAbsolute(ref) ? path.resolve(dir, ref) : path.resolve(asPath(ref));
|
|
21
|
+
if (!within(file, dir)) throw new Error(`${what}: ${file} is not in its public folder (${dir}); a plugin's browser files live there`);
|
|
22
|
+
return file;
|
|
23
|
+
}
|
|
24
|
+
export { inPublic, publicDirOf, within };
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { alive } from "./running.js";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
// src/rename.ts
|
|
6
|
+
const OLD = ".whiteboard";
|
|
7
|
+
/** The project folder still under the old name: the nearest folder upward with a `.whiteboard/` folder and no `.workpad/`. */
|
|
8
|
+
function oldProjectDir(from = process.cwd()) {
|
|
9
|
+
let dir = path.resolve(from);
|
|
10
|
+
for (;;) {
|
|
11
|
+
if (isDir(path.join(dir, ".whiteboard")) && !isDir(path.join(dir, ".workpad"))) return dir;
|
|
12
|
+
const up = path.dirname(dir);
|
|
13
|
+
if (up === dir) return null;
|
|
14
|
+
dir = up;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** The code of a config or plugin, in the new names. */
|
|
18
|
+
function renameCode(source) {
|
|
19
|
+
return source.replace(/@bigmistqke\/whiteboard\b/g, "@bigmistqke/workpad").replace(/\buseWhiteboard\b/g, "useWorkpad").replace(/\bWbEvent\b/g, "WpEvent").replace(/\bctx\.wb\b/g, "ctx.wp").replace(/\bwb-(?=[a-z])/g, "wp-").replace(/\bdataset\.wb(?=[A-Z])/g, "dataset.wp").replace(/\bWHITEBOARD_/g, "WORKPAD_");
|
|
20
|
+
}
|
|
21
|
+
/** A line of the log, its type in the new name. */
|
|
22
|
+
function renameEvent(line) {
|
|
23
|
+
if (!line.includes("\"whiteboard:")) return line;
|
|
24
|
+
try {
|
|
25
|
+
const e = JSON.parse(line);
|
|
26
|
+
if (typeof e.type !== "string" || !e.type.startsWith("whiteboard:")) return line;
|
|
27
|
+
return JSON.stringify({
|
|
28
|
+
...e,
|
|
29
|
+
type: `workpad:${e.type.slice(11)}`
|
|
30
|
+
});
|
|
31
|
+
} catch {
|
|
32
|
+
return line;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** What moving the project in `dir` to the new names takes; throws when its server is running. */
|
|
36
|
+
function projectSteps(dir) {
|
|
37
|
+
const from = path.join(dir, OLD);
|
|
38
|
+
const to = path.join(dir, ".workpad");
|
|
39
|
+
refuseRunning(path.join(from, "server.json"), `the server of ${dir}`, "workpad stop");
|
|
40
|
+
const steps = [{
|
|
41
|
+
what: `${rel(from)} → ${rel(to)}`,
|
|
42
|
+
run: () => fs.renameSync(from, to)
|
|
43
|
+
}];
|
|
44
|
+
const log = path.join(from, "events.ndjson");
|
|
45
|
+
if (fs.existsSync(log)) {
|
|
46
|
+
const lines = fs.readFileSync(log, "utf8").split("\n");
|
|
47
|
+
const renamed = lines.map(renameEvent);
|
|
48
|
+
const n = renamed.filter((l, i) => l !== lines[i]).length;
|
|
49
|
+
if (n) steps.push({
|
|
50
|
+
what: ` events.ndjson: ${n} whiteboard:* event${n === 1 ? "" : "s"} → workpad:*`,
|
|
51
|
+
run: () => fs.writeFileSync(path.join(to, "events.ndjson"), renamed.join("\n"))
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
for (const file of codeFiles(from)) {
|
|
55
|
+
const before = fs.readFileSync(file, "utf8");
|
|
56
|
+
const after = renameCode(before);
|
|
57
|
+
if (after !== before) steps.push({
|
|
58
|
+
what: ` ${path.relative(from, file)}: the new names`,
|
|
59
|
+
run: () => fs.writeFileSync(path.join(to, path.relative(from, file)), after)
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const ignore = path.join(dir, ".gitignore");
|
|
63
|
+
if (fs.existsSync(ignore)) {
|
|
64
|
+
const before = fs.readFileSync(ignore, "utf8");
|
|
65
|
+
const after = before.replace(/^(\/?)\.whiteboard(\/?)$/gm, "$1.workpad$2");
|
|
66
|
+
if (after !== before) steps.push({
|
|
67
|
+
what: ` ${rel(ignore)}: .whiteboard → .workpad`,
|
|
68
|
+
run: () => fs.writeFileSync(ignore, after)
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return steps;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The machine's folders under the old name, moved where workpad looks: the machine config
|
|
75
|
+
* (`~/.config/whiteboard`) and the shared server's state (`~/.local/state/whiteboard`). Only the
|
|
76
|
+
* defaults: a folder set with $WHITEBOARD_HOME stays, and is $WORKPAD_HOME now.
|
|
77
|
+
*/
|
|
78
|
+
function machineSteps(env = process.env) {
|
|
79
|
+
if (env.WORKPAD_HOME) return [];
|
|
80
|
+
const config = env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config");
|
|
81
|
+
const state = env.WORKPAD_STATE ? null : env.XDG_STATE_HOME || path.join(os.homedir(), ".local", "state");
|
|
82
|
+
const steps = [];
|
|
83
|
+
for (const base of [config, state]) {
|
|
84
|
+
if (!base) continue;
|
|
85
|
+
const from = path.join(base, "whiteboard");
|
|
86
|
+
const to = path.join(base, "workpad");
|
|
87
|
+
if (!isDir(from) || fs.existsSync(to)) continue;
|
|
88
|
+
refuseRunning(path.join(from, "server.json"), "the shared server", "workpad stop --shared");
|
|
89
|
+
steps.push({
|
|
90
|
+
what: `${tilde(from)} → ${tilde(to)}`,
|
|
91
|
+
run: () => fs.renameSync(from, to)
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return steps;
|
|
95
|
+
}
|
|
96
|
+
function refuseRunning(record, who, stop) {
|
|
97
|
+
try {
|
|
98
|
+
const { pid } = JSON.parse(fs.readFileSync(record, "utf8"));
|
|
99
|
+
if (typeof pid === "number" && alive(pid)) throw new Error(`${who} is running (pid ${pid}) under the old name: stop it first (${stop}, or kill ${pid})`);
|
|
100
|
+
} catch (e) {
|
|
101
|
+
if (e instanceof Error && e.message.includes("is running")) throw e;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** A project folder's config and plugins: the code that may name the old package, ctx.wb or wb- classes. */
|
|
105
|
+
function codeFiles(folder) {
|
|
106
|
+
const out = [];
|
|
107
|
+
const walk = (dir, top) => {
|
|
108
|
+
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
109
|
+
const p = path.join(dir, e.name);
|
|
110
|
+
if (e.isDirectory()) {
|
|
111
|
+
if (top ? e.name === "plugins" : e.name !== "node_modules") walk(p, false);
|
|
112
|
+
} else if (/\.(ts|tsx|js|jsx|mjs|css)$/.test(e.name) && (!top || e.name.startsWith("config."))) out.push(p);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
walk(folder, true);
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
function isDir(p) {
|
|
119
|
+
try {
|
|
120
|
+
return fs.statSync(p).isDirectory();
|
|
121
|
+
} catch {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const rel = (p) => {
|
|
126
|
+
const r = path.relative(process.cwd(), p);
|
|
127
|
+
return r && !r.startsWith("..") ? r : p;
|
|
128
|
+
};
|
|
129
|
+
const tilde = (p) => p.startsWith(os.homedir()) ? `~${p.slice(os.homedir().length)}` : p;
|
|
130
|
+
export { OLD, machineSteps, oldProjectDir, projectSteps, renameCode, renameEvent };
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { machineServerFile, matchesEntries, readMachineConfig } from "./entries.js";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import net from "node:net";
|
|
4
|
+
// src/running.ts
|
|
5
|
+
/**
|
|
6
|
+
* How long a server may take to say it is there: a loaded machine (many agents, builds) can take
|
|
7
|
+
* seconds, and a server taken for gone is one a command starts again or does without.
|
|
8
|
+
*/
|
|
9
|
+
const HEALTH_TIMEOUT = 5e3;
|
|
10
|
+
/** The port the last server of this project recorded, or null. */
|
|
11
|
+
function recordedPort(project) {
|
|
12
|
+
try {
|
|
13
|
+
return Number(JSON.parse(fs.readFileSync(project.serverFile, "utf8")).port) || null;
|
|
14
|
+
} catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Where the server a record (a server.json) names is reached: the URL it recorded (a server in a
|
|
20
|
+
* container, see serverUrl in entries.ts), else its port at localhost; null without a record.
|
|
21
|
+
*/
|
|
22
|
+
function recordedOrigin(file) {
|
|
23
|
+
try {
|
|
24
|
+
const record = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
25
|
+
if (typeof record.url === "string" && record.url) return record.url;
|
|
26
|
+
return Number(record.port) ? `http://localhost:${Number(record.port)}` : null;
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** The process server.json names for this project, if it names one. */
|
|
32
|
+
function recordedPid(project) {
|
|
33
|
+
try {
|
|
34
|
+
return Number(JSON.parse(fs.readFileSync(project.serverFile, "utf8")).pid) || null;
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** Does process `pid` run? One of another user's (EPERM) does too. */
|
|
40
|
+
function alive(pid) {
|
|
41
|
+
try {
|
|
42
|
+
process.kill(pid, 0);
|
|
43
|
+
return true;
|
|
44
|
+
} catch (err) {
|
|
45
|
+
return err.code === "EPERM";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** The base URL of this project's running server, or null. `except`: a process to not count (the asking server itself). */
|
|
49
|
+
async function runningServer(project, { except } = {}) {
|
|
50
|
+
const base = recordedOrigin(project.serverFile);
|
|
51
|
+
if (!base) return null;
|
|
52
|
+
try {
|
|
53
|
+
const res = await fetch(`${base}/api/health`, { signal: AbortSignal.timeout(HEALTH_TIMEOUT) });
|
|
54
|
+
const health = res.ok ? await res.json() : null;
|
|
55
|
+
return health?.dir === project.dir && health.pid !== except ? base : null;
|
|
56
|
+
} catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** The port the shared server recorded in the machine folder, or null. */
|
|
61
|
+
function recordedSharedPort(file) {
|
|
62
|
+
try {
|
|
63
|
+
return Number(JSON.parse(fs.readFileSync(file, "utf8")).port) || null;
|
|
64
|
+
} catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The machine's running shared server, or null: where its record says it is, checked by asking it.
|
|
70
|
+
* `rescan`: have it look for projects first (one just made, say). `except`: a process not to count.
|
|
71
|
+
*/
|
|
72
|
+
async function sharedServer(file, { rescan = false, except } = {}) {
|
|
73
|
+
const origin = recordedOrigin(file);
|
|
74
|
+
if (!origin) return null;
|
|
75
|
+
try {
|
|
76
|
+
const res = await fetch(`${origin}/api/health${rescan ? "?rescan" : ""}`, { signal: AbortSignal.timeout(HEALTH_TIMEOUT) });
|
|
77
|
+
const health = res.ok ? await res.json() : null;
|
|
78
|
+
if (!health?.hub || health.pid === except) return null;
|
|
79
|
+
return {
|
|
80
|
+
origin,
|
|
81
|
+
pid: health.pid,
|
|
82
|
+
version: String(health.version ?? "0.0.0"),
|
|
83
|
+
projects: health.projects ?? []
|
|
84
|
+
};
|
|
85
|
+
} catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Does something accept connections at `origin` (a server's, as recordedOrigin gives it)? For a
|
|
91
|
+
* server whose health check got no answer: one that accepts but doesn't answer is stuck (its event
|
|
92
|
+
* loop blocked, or out of file descriptors), which is not the same as one that isn't running, and is
|
|
93
|
+
* said otherwise. Localhost is asked at 127.0.0.1, where the servers listen by default.
|
|
94
|
+
*/
|
|
95
|
+
function accepts(origin, timeout = 1e3) {
|
|
96
|
+
const url = new URL(origin);
|
|
97
|
+
const host = url.hostname === "localhost" ? "127.0.0.1" : url.hostname.replace(/^\[|\]$/g, "");
|
|
98
|
+
const port = Number(url.port) || (url.protocol === "https:" ? 443 : 80);
|
|
99
|
+
return new Promise((resolve) => {
|
|
100
|
+
const socket = net.connect({
|
|
101
|
+
port,
|
|
102
|
+
host
|
|
103
|
+
});
|
|
104
|
+
const done = (ok) => {
|
|
105
|
+
socket.destroy();
|
|
106
|
+
resolve(ok);
|
|
107
|
+
};
|
|
108
|
+
socket.setTimeout(timeout, () => done(false));
|
|
109
|
+
socket.once("connect", () => done(true));
|
|
110
|
+
socket.once("error", () => done(false));
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/** A server at `origin` that accepts connections and gives its health check no answer at all. */
|
|
114
|
+
async function stuckAt(origin) {
|
|
115
|
+
if (!await accepts(origin)) return false;
|
|
116
|
+
try {
|
|
117
|
+
await fetch(`${origin}/api/health`, { signal: AbortSignal.timeout(2e3) });
|
|
118
|
+
return false;
|
|
119
|
+
} catch {
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** What to say about a server at `origin` that accepts connections but didn't answer its health check. */
|
|
124
|
+
const stuck = (origin, log) => `the server at ${origin} accepts connections but doesn't answer: it is stuck (its event loop blocked, or out of file descriptors). Restart it; ${log} may say why`;
|
|
125
|
+
/**
|
|
126
|
+
* Is this project one of the machine's entries (see src/entries.ts)? Then the shared server serves
|
|
127
|
+
* it, under its path, and no server of its own runs. A project outside every include runs its own.
|
|
128
|
+
*/
|
|
129
|
+
function isShared(proj) {
|
|
130
|
+
const config = readMachineConfig();
|
|
131
|
+
return !!config && !!matchesEntries(config, proj.dir);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* The URL a project is served at now (`http://localhost:4777`, or `http://localhost:4777/rg/develop`
|
|
135
|
+
* under the shared server; at the URL the server recorded when it has one,
|
|
136
|
+
* `https://workpad.local/rg/develop`), or null when nothing serves it.
|
|
137
|
+
*/
|
|
138
|
+
async function projectServer(proj) {
|
|
139
|
+
if (!isShared(proj)) return runningServer(proj);
|
|
140
|
+
const file = machineServerFile();
|
|
141
|
+
const shared = await sharedServer(file);
|
|
142
|
+
if (!shared) return null;
|
|
143
|
+
const mine = shared.projects.find((p) => p.dir === proj.dir) ?? (await sharedServer(file, { rescan: true }))?.projects.find((p) => p.dir === proj.dir);
|
|
144
|
+
return mine ? `${shared.origin}${mine.base}` : null;
|
|
145
|
+
}
|
|
146
|
+
export { accepts, alive, isShared, projectServer, recordedOrigin, recordedPid, recordedPort, recordedSharedPort, runningServer, sharedServer, stuck, stuckAt };
|