adrien 0.1.0 → 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/README.md +3 -2
- package/dist/index.js +372 -18
- package/dist/web/assets/app.js +13 -0
- package/dist/web/assets/archivo-latin-wdth-normal.woff2 +0 -0
- package/dist/web/assets/decision-view.js +32 -0
- package/dist/web/assets/graph-view.js +1 -0
- package/dist/web/assets/index.css +2 -0
- package/dist/web/favicon.svg +1 -0
- package/dist/web/index.html +17 -0
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -72,8 +72,9 @@ Run from the code repo (or log repo). Any MCP client that speaks stdio works the
|
|
|
72
72
|
|
|
73
73
|
## Commands
|
|
74
74
|
|
|
75
|
-
`init`, `new`, `list`, `show`, `search`, `for`, `check`, `lint`, `export`, `accept`, `status`, `supersede`, `renumber`, `sync`, `mcp`.
|
|
76
|
-
Run `adrien <command> --help`. Add `--json` for machine-readable output.
|
|
75
|
+
`init`, `new`, `list`, `show`, `search`, `for`, `check`, `lint`, `export`, `accept`, `status`, `supersede`, `renumber`, `sync`, `mcp`, `serve`.
|
|
76
|
+
Run `adrien <command> --help`. Add `--json` for machine-readable output. `adrien serve` starts a read-only
|
|
77
|
+
[local HTTP API](https://github.com/moumine9/adrien/blob/main/docs/api.md) on `127.0.0.1:4310`.
|
|
77
78
|
|
|
78
79
|
## Documentation
|
|
79
80
|
|
package/dist/index.js
CHANGED
|
@@ -10,9 +10,13 @@ import { execFile, spawn } from "node:child_process";
|
|
|
10
10
|
import { promisify } from "node:util";
|
|
11
11
|
import { fileURLToPath } from "node:url";
|
|
12
12
|
import { createHash } from "node:crypto";
|
|
13
|
-
import { Command, CommanderError } from "commander";
|
|
13
|
+
import { Command, CommanderError, InvalidArgumentError } from "commander";
|
|
14
14
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
15
15
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
16
|
+
import { Hono } from "hono";
|
|
17
|
+
import { readFile, realpath, stat } from "node:fs/promises";
|
|
18
|
+
import { getMimeType } from "hono/utils/mime";
|
|
19
|
+
import { serve } from "@hono/node-server";
|
|
16
20
|
// ../core/src/anchors.ts
|
|
17
21
|
function parseAnchor(s) {
|
|
18
22
|
const i = s.indexOf(":");
|
|
@@ -59,9 +63,9 @@ function parseIdFormat(fmt) {
|
|
|
59
63
|
const m = /^(.*)\{n(?::(\d+))?\}(.*)$/.exec(fmt);
|
|
60
64
|
if (!m) throw new AdrienError("CONFIG_INVALID", `id_format "${fmt}" must contain {n} or {n:WIDTH}`);
|
|
61
65
|
return {
|
|
62
|
-
prefix: m[1]
|
|
66
|
+
prefix: m[1],
|
|
63
67
|
width: m[2] ? Number(m[2]) : 1,
|
|
64
|
-
suffix: m[3]
|
|
68
|
+
suffix: m[3]
|
|
65
69
|
};
|
|
66
70
|
}
|
|
67
71
|
function formatId(n, fmt) {
|
|
@@ -318,7 +322,7 @@ function parseAdr(text, path, opts) {
|
|
|
318
322
|
});
|
|
319
323
|
if (!parsed.success) {
|
|
320
324
|
const issue = parsed.error.issues[0];
|
|
321
|
-
throw parseError(path, `${issue?.path.join(".")
|
|
325
|
+
throw parseError(path, `${issue?.path.join(".")}: ${issue?.message}`);
|
|
322
326
|
}
|
|
323
327
|
return {
|
|
324
328
|
meta: {
|
|
@@ -541,7 +545,6 @@ async function readTreeAtRef(repoDir, ref, subdir) {
|
|
|
541
545
|
]);
|
|
542
546
|
if (!listing) return [];
|
|
543
547
|
const paths = listing.split("\0").filter(Boolean);
|
|
544
|
-
if (paths.length === 0) return [];
|
|
545
548
|
return parseBatch(await catFileBatch(repoDir, paths.map((p) => `${ref}:${p}`)), paths);
|
|
546
549
|
}
|
|
547
550
|
/** The text of one file at `ref`, or null when the ref or the file does not exist. */
|
|
@@ -606,7 +609,7 @@ function normalizeRepoUrl(url) {
|
|
|
606
609
|
return `${u.hostname.toLowerCase()}/${trimRepoPath(u.pathname)}`;
|
|
607
610
|
}
|
|
608
611
|
const scp = WINDOWS_DRIVE.test(s) ? null : SCP_LIKE.exec(s);
|
|
609
|
-
if (scp) return `${
|
|
612
|
+
if (scp) return `${scp[1].toLowerCase()}/${trimRepoPath(scp[2])}`;
|
|
610
613
|
return trimRepoLocal(s);
|
|
611
614
|
}
|
|
612
615
|
function trimRepoLocal(p) {
|
|
@@ -898,7 +901,7 @@ const prose = (body) => body.replace(FENCE, "").replace(COMMENT, "").replace(INL
|
|
|
898
901
|
function decisionText(body) {
|
|
899
902
|
const m = /^##\s+(?:Decision Outcome|Decision)\s*$([\s\S]*?)(?=^##\s|(?![\s\S]))/im.exec(body);
|
|
900
903
|
if (!m) return null;
|
|
901
|
-
return
|
|
904
|
+
return m[1].replace(COMMENT, "").replace(/^#+\s.*$/gm, "").trim();
|
|
902
905
|
}
|
|
903
906
|
function adrienRules(adr) {
|
|
904
907
|
const at = {
|
|
@@ -1046,7 +1049,7 @@ async function checkouts(ctx, here) {
|
|
|
1046
1049
|
const roots = /* @__PURE__ */ new Map();
|
|
1047
1050
|
const add = (component, repoRoot) => roots.set(component, join(repoRoot, componentRoot(ctx.config, component)));
|
|
1048
1051
|
if (here) {
|
|
1049
|
-
for (const c of ctx.codeRepo
|
|
1052
|
+
if (ctx.codeRepo) for (const c of ctx.codeRepo.components) add(c, ctx.codeRepo.root);
|
|
1050
1053
|
return roots;
|
|
1051
1054
|
}
|
|
1052
1055
|
const logTop = await gitToplevel(ctx.logRoot) ?? ctx.logRoot;
|
|
@@ -1446,7 +1449,7 @@ var AdrIndex = class AdrIndex {
|
|
|
1446
1449
|
insertFts.run(doc.layer, path, meta.title, meta.review_rule ?? "", body);
|
|
1447
1450
|
}
|
|
1448
1451
|
for (const key of existing.keys()) {
|
|
1449
|
-
const [layer
|
|
1452
|
+
const [layer, path] = key.split("\0");
|
|
1450
1453
|
remove(layer, path);
|
|
1451
1454
|
stats.deleted++;
|
|
1452
1455
|
}
|
|
@@ -1678,7 +1681,7 @@ async function open(cli, opts) {
|
|
|
1678
1681
|
view: await loadLogView(ctx.logRoot, ctx.config)
|
|
1679
1682
|
};
|
|
1680
1683
|
}
|
|
1681
|
-
async function withIndex$
|
|
1684
|
+
async function withIndex$2(ctx, view, use) {
|
|
1682
1685
|
const index = await AdrIndex.open(ctx.logRoot);
|
|
1683
1686
|
try {
|
|
1684
1687
|
index.sync(view);
|
|
@@ -1869,7 +1872,7 @@ function registerList(program, cli) {
|
|
|
1869
1872
|
program.command("list").description("List ADRs (base and pending)").option("--tag <tag>", "only ADRs with this tag (repeatable: all must match)", collect, []).option("--component <id>", "only ADRs for this component (repeatable: any matches)", collect, []).option("--status <status>", "only this status (repeatable)", collect, []).option("--pending", "only pending (unmerged) changes").action(async (opts, cmd) => {
|
|
1870
1873
|
const globals = cmd.optsWithGlobals();
|
|
1871
1874
|
const { ctx, view } = await open(cli, globals);
|
|
1872
|
-
const rows = await withIndex$
|
|
1875
|
+
const rows = await withIndex$2(ctx, view, (index) => index.list({
|
|
1873
1876
|
tags: opts.tag,
|
|
1874
1877
|
components: opts.component,
|
|
1875
1878
|
status: opts.status,
|
|
@@ -1887,7 +1890,7 @@ const ok = (result) => ({
|
|
|
1887
1890
|
text: JSON.stringify(result)
|
|
1888
1891
|
}]
|
|
1889
1892
|
});
|
|
1890
|
-
const fail = (text) => ({
|
|
1893
|
+
const fail$1 = (text) => ({
|
|
1891
1894
|
isError: true,
|
|
1892
1895
|
content: [{
|
|
1893
1896
|
type: "text",
|
|
@@ -1914,12 +1917,12 @@ function handler(opts, run) {
|
|
|
1914
1917
|
const { ctx, view } = await load(opts);
|
|
1915
1918
|
return await run(args, ctx, view);
|
|
1916
1919
|
} catch (e) {
|
|
1917
|
-
if (e instanceof AdrienError) return fail(`${e.code}: ${e.message}`);
|
|
1920
|
+
if (e instanceof AdrienError) return fail$1(`${e.code}: ${e.message}`);
|
|
1918
1921
|
throw e;
|
|
1919
1922
|
}
|
|
1920
1923
|
};
|
|
1921
1924
|
}
|
|
1922
|
-
async function withIndex(ctx, view, use) {
|
|
1925
|
+
async function withIndex$1(ctx, view, use) {
|
|
1923
1926
|
const index = await AdrIndex.open(ctx.logRoot);
|
|
1924
1927
|
try {
|
|
1925
1928
|
index.sync(view);
|
|
@@ -1956,7 +1959,7 @@ function registerTools(server, opts) {
|
|
|
1956
1959
|
status: status.optional(),
|
|
1957
1960
|
limit: z.number().int().min(1).max(50).default(10)
|
|
1958
1961
|
}
|
|
1959
|
-
}, handler(opts, async (args, ctx, view) => ok({ results: await withIndex(ctx, view, (index) => index.search(args.query, {
|
|
1962
|
+
}, handler(opts, async (args, ctx, view) => ok({ results: await withIndex$1(ctx, view, (index) => index.search(args.query, {
|
|
1960
1963
|
tags: args.tags,
|
|
1961
1964
|
components: args.components,
|
|
1962
1965
|
status: args.status,
|
|
@@ -1978,7 +1981,7 @@ function registerTools(server, opts) {
|
|
|
1978
1981
|
status: args.status,
|
|
1979
1982
|
layer: args.pending_only ? "pending" : "all"
|
|
1980
1983
|
};
|
|
1981
|
-
return ok({ results: await withIndex(ctx, view, (index) => index.list(filters)) });
|
|
1984
|
+
return ok({ results: await withIndex$1(ctx, view, (index) => index.list(filters)) });
|
|
1982
1985
|
}));
|
|
1983
1986
|
server.registerTool("adr_get", {
|
|
1984
1987
|
title: "Read one decision",
|
|
@@ -2146,11 +2149,361 @@ function registerSearch(program, cli) {
|
|
|
2146
2149
|
program.command("search").description("Full-text search over titles, review rules and bodies").argument("<query...>", "words to look for").action(async (query, _opts, cmd) => {
|
|
2147
2150
|
const globals = cmd.optsWithGlobals();
|
|
2148
2151
|
const { ctx, view } = await open(cli, globals);
|
|
2149
|
-
const rows = await withIndex$
|
|
2152
|
+
const rows = await withIndex$2(ctx, view, (index) => index.search(query.join(" ")));
|
|
2150
2153
|
if (globals.json) return printJson(cli, rows);
|
|
2151
2154
|
for (const row of rows) cli.out(formatRow(row));
|
|
2152
2155
|
});
|
|
2153
2156
|
}
|
|
2157
|
+
// ../api/src/host.ts
|
|
2158
|
+
const LOOPBACK = /* @__PURE__ */ new Set([
|
|
2159
|
+
"localhost",
|
|
2160
|
+
"127.0.0.1",
|
|
2161
|
+
"::1"
|
|
2162
|
+
]);
|
|
2163
|
+
/** The host name in a `Host` header or a bind address: lowercased, without its port or IPv6 brackets. */
|
|
2164
|
+
function hostnameOf(host) {
|
|
2165
|
+
const name = host.trim().toLowerCase();
|
|
2166
|
+
if (name.startsWith("[")) {
|
|
2167
|
+
const end = name.indexOf("]");
|
|
2168
|
+
return end === -1 ? name : name.slice(1, end);
|
|
2169
|
+
}
|
|
2170
|
+
const parts = name.split(":");
|
|
2171
|
+
return parts.length === 2 ? parts[0] : name;
|
|
2172
|
+
}
|
|
2173
|
+
/**
|
|
2174
|
+
* True when a request's `Host` names this machine: a loopback name, or the host the server was bound to. A web page
|
|
2175
|
+
* that rebinds its own domain to 127.0.0.1 still sends that domain, so it is refused.
|
|
2176
|
+
*/
|
|
2177
|
+
function isAllowedHost(host, bound) {
|
|
2178
|
+
const name = hostnameOf(host);
|
|
2179
|
+
return LOOPBACK.has(name) || bound !== void 0 && bound !== "" && name === hostnameOf(bound);
|
|
2180
|
+
}
|
|
2181
|
+
// ../api/src/views.ts
|
|
2182
|
+
/**
|
|
2183
|
+
* What the default branch would hold once this checkout is merged, one ADR per id: when ids collide (which `check`
|
|
2184
|
+
* reports), the first by path stands for the id, so the graph never has two nodes with one id.
|
|
2185
|
+
*/
|
|
2186
|
+
function mergedById(view) {
|
|
2187
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2188
|
+
const adrs = [];
|
|
2189
|
+
for (const adr of mergedAdrs(view)) {
|
|
2190
|
+
if (seen.has(adr.meta.id)) continue;
|
|
2191
|
+
seen.add(adr.meta.id);
|
|
2192
|
+
adrs.push(adr);
|
|
2193
|
+
}
|
|
2194
|
+
return adrs;
|
|
2195
|
+
}
|
|
2196
|
+
function metaOf(ctx, view, version) {
|
|
2197
|
+
const adrs = mergedById(view);
|
|
2198
|
+
const byStatus = Object.fromEntries(STATUSES.map((status) => [status, 0]));
|
|
2199
|
+
for (const adr of adrs) byStatus[adr.meta.status]++;
|
|
2200
|
+
return {
|
|
2201
|
+
version,
|
|
2202
|
+
product: ctx.config.product,
|
|
2203
|
+
adrDir: ctx.config.adr_dir,
|
|
2204
|
+
mode: view.mode,
|
|
2205
|
+
baseRef: view.baseRef,
|
|
2206
|
+
baseUnresolved: view.baseUnresolved === true,
|
|
2207
|
+
forkUnresolved: view.forkUnresolved === true,
|
|
2208
|
+
tags: ctx.config.tags,
|
|
2209
|
+
components: ctx.config.components,
|
|
2210
|
+
counts: {
|
|
2211
|
+
total: adrs.length,
|
|
2212
|
+
pending: view.pending.length,
|
|
2213
|
+
byStatus
|
|
2214
|
+
}
|
|
2215
|
+
};
|
|
2216
|
+
}
|
|
2217
|
+
const fileOf = (adr) => ({
|
|
2218
|
+
meta: adr.meta,
|
|
2219
|
+
body: adr.body,
|
|
2220
|
+
path: adr.path
|
|
2221
|
+
});
|
|
2222
|
+
function adrOf(view, id) {
|
|
2223
|
+
const base = view.base.find((a) => a.meta.id === id);
|
|
2224
|
+
const change = view.pending.find((c) => c.id === id);
|
|
2225
|
+
if (!base && !change) throw new AdrienError("ADR_NOT_FOUND", `No ADR ${id} in the log`);
|
|
2226
|
+
return {
|
|
2227
|
+
id,
|
|
2228
|
+
base: base ? fileOf(base) : null,
|
|
2229
|
+
pending: change ? {
|
|
2230
|
+
kind: change.kind,
|
|
2231
|
+
...change.current ? fileOf(change.current) : {}
|
|
2232
|
+
} : null
|
|
2233
|
+
};
|
|
2234
|
+
}
|
|
2235
|
+
function graphOf(view) {
|
|
2236
|
+
const adrs = mergedById(view);
|
|
2237
|
+
const pending = new Set(view.pending.filter((c) => c.kind !== "deleted").map((c) => c.id));
|
|
2238
|
+
const nodes = adrs.map(({ meta }) => ({
|
|
2239
|
+
id: meta.id,
|
|
2240
|
+
title: meta.title,
|
|
2241
|
+
status: meta.status,
|
|
2242
|
+
tags: meta.tags,
|
|
2243
|
+
pending: pending.has(meta.id)
|
|
2244
|
+
}));
|
|
2245
|
+
const ids = new Set(nodes.map((n) => n.id));
|
|
2246
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2247
|
+
const edges = [];
|
|
2248
|
+
const add = (from, to, type) => {
|
|
2249
|
+
const key = [type, ...type === "related" ? [from, to].sort() : [from, to]].join("\0");
|
|
2250
|
+
if (from === to || !ids.has(from) || !ids.has(to) || seen.has(key)) return;
|
|
2251
|
+
seen.add(key);
|
|
2252
|
+
edges.push({
|
|
2253
|
+
from,
|
|
2254
|
+
to,
|
|
2255
|
+
type
|
|
2256
|
+
});
|
|
2257
|
+
};
|
|
2258
|
+
for (const { meta } of adrs) {
|
|
2259
|
+
for (const older of meta.supersedes) add(meta.id, older, "supersedes");
|
|
2260
|
+
if (meta.superseded_by) add(meta.superseded_by, meta.id, "supersedes");
|
|
2261
|
+
for (const other of meta.related) add(meta.id, other, "related");
|
|
2262
|
+
}
|
|
2263
|
+
return {
|
|
2264
|
+
nodes,
|
|
2265
|
+
edges
|
|
2266
|
+
};
|
|
2267
|
+
}
|
|
2268
|
+
// ../api/src/web.ts
|
|
2269
|
+
const NOT_BUILT = "The Adrien web visualizer is not part of this build. The API is served at /api/meta (see docs/api.md).\n";
|
|
2270
|
+
/** A built web app: its real root and its index.html, which must be a file inside that root. */
|
|
2271
|
+
async function builtApp(webDir) {
|
|
2272
|
+
let root;
|
|
2273
|
+
try {
|
|
2274
|
+
root = await realpath(webDir);
|
|
2275
|
+
} catch {
|
|
2276
|
+
return null;
|
|
2277
|
+
}
|
|
2278
|
+
const index = await fileIn(root, ["index.html"]);
|
|
2279
|
+
return index ? {
|
|
2280
|
+
root,
|
|
2281
|
+
index
|
|
2282
|
+
} : null;
|
|
2283
|
+
}
|
|
2284
|
+
/**
|
|
2285
|
+
* The URL path as path segments, or null when one is not valid percent-encoding or could leave the web root: an
|
|
2286
|
+
* encoded `/` or `\`, a NUL, or a dot segment (URL parsing resolves plain `..` before it gets here).
|
|
2287
|
+
*/
|
|
2288
|
+
function segmentsOf(pathname) {
|
|
2289
|
+
const segments = [];
|
|
2290
|
+
for (const raw of pathname.split("/")) {
|
|
2291
|
+
if (!raw) continue;
|
|
2292
|
+
let segment;
|
|
2293
|
+
try {
|
|
2294
|
+
segment = decodeURIComponent(raw);
|
|
2295
|
+
} catch {
|
|
2296
|
+
return null;
|
|
2297
|
+
}
|
|
2298
|
+
if (segment === "." || segment === ".." || /[/\\\0]/.test(segment)) return null;
|
|
2299
|
+
segments.push(segment);
|
|
2300
|
+
}
|
|
2301
|
+
return segments;
|
|
2302
|
+
}
|
|
2303
|
+
/** The file a path names, once symlinks are resolved, only if it is a regular file inside the web root. */
|
|
2304
|
+
async function fileIn(root, segments) {
|
|
2305
|
+
try {
|
|
2306
|
+
const real = await realpath(join(root, ...segments));
|
|
2307
|
+
return isInside(real, root) && (await stat(real)).isFile() ? real : null;
|
|
2308
|
+
} catch {
|
|
2309
|
+
return null;
|
|
2310
|
+
}
|
|
2311
|
+
}
|
|
2312
|
+
async function send(c, file) {
|
|
2313
|
+
const body = new Uint8Array(await readFile(file));
|
|
2314
|
+
return c.body(body, 200, {
|
|
2315
|
+
"Content-Type": getMimeType(file) ?? "application/octet-stream",
|
|
2316
|
+
"X-Content-Type-Options": "nosniff",
|
|
2317
|
+
"Cache-Control": "no-cache"
|
|
2318
|
+
});
|
|
2319
|
+
}
|
|
2320
|
+
/** Serves the built web visualizer, with `index.html` for client-side routes (paths without an extension). */
|
|
2321
|
+
function serveWeb(webDir) {
|
|
2322
|
+
return async (c) => {
|
|
2323
|
+
const app = webDir === void 0 ? null : await builtApp(webDir);
|
|
2324
|
+
if (!app) return c.text(NOT_BUILT, 404);
|
|
2325
|
+
const segments = segmentsOf(new URL(c.req.url).pathname);
|
|
2326
|
+
if (!segments) return c.text("Bad request path.\n", 400);
|
|
2327
|
+
const file = await fileIn(app.root, segments);
|
|
2328
|
+
if (file) return send(c, file);
|
|
2329
|
+
if (!(segments.at(-1) ?? "").includes(".")) return send(c, app.index);
|
|
2330
|
+
return c.text("Not found.\n", 404);
|
|
2331
|
+
};
|
|
2332
|
+
}
|
|
2333
|
+
// ../api/src/app.ts
|
|
2334
|
+
const HTTP_STATUS = {
|
|
2335
|
+
ADR_NOT_FOUND: 404,
|
|
2336
|
+
INVALID_INPUT: 400
|
|
2337
|
+
};
|
|
2338
|
+
const fail = (c, status, code, message) => c.json({ error: {
|
|
2339
|
+
code,
|
|
2340
|
+
message
|
|
2341
|
+
} }, status);
|
|
2342
|
+
const isApi = (path) => path === "/api" || path.startsWith("/api/");
|
|
2343
|
+
const flag = (value) => value === "1" || value === "true";
|
|
2344
|
+
/** A repeatable query parameter, without empty values. */
|
|
2345
|
+
const all = (c, name) => (c.req.queries(name) ?? []).filter(Boolean);
|
|
2346
|
+
async function withIndex(ctx, view, use) {
|
|
2347
|
+
const index = await AdrIndex.open(ctx.logRoot);
|
|
2348
|
+
try {
|
|
2349
|
+
index.sync(view);
|
|
2350
|
+
return use(index);
|
|
2351
|
+
} finally {
|
|
2352
|
+
index.close();
|
|
2353
|
+
}
|
|
2354
|
+
}
|
|
2355
|
+
/** The read-only HTTP API of docs/api.md, and the web visualizer's files. Every request re-reads the log. */
|
|
2356
|
+
function createApp(opts) {
|
|
2357
|
+
const app = new Hono();
|
|
2358
|
+
const load = async () => {
|
|
2359
|
+
const ctx = await resolveContext({
|
|
2360
|
+
cwd: opts.cwd,
|
|
2361
|
+
log: opts.log,
|
|
2362
|
+
env: opts.env ?? process.env
|
|
2363
|
+
});
|
|
2364
|
+
return {
|
|
2365
|
+
ctx,
|
|
2366
|
+
view: await loadLogView(ctx.logRoot, ctx.config)
|
|
2367
|
+
};
|
|
2368
|
+
};
|
|
2369
|
+
app.use(async (c, next) => {
|
|
2370
|
+
await next();
|
|
2371
|
+
if (isApi(c.req.path)) c.header("Cache-Control", "no-store");
|
|
2372
|
+
});
|
|
2373
|
+
app.use(async (c, next) => {
|
|
2374
|
+
const host = c.req.header("host") ?? new URL(c.req.url).host;
|
|
2375
|
+
if (!isAllowedHost(host, opts.host)) return fail(c, 403, "FORBIDDEN_HOST", `Requests for host "${host}" are refused: open the URL adrien serve printed.`);
|
|
2376
|
+
if (c.req.method !== "GET" && c.req.method !== "HEAD") {
|
|
2377
|
+
c.header("Allow", "GET, HEAD");
|
|
2378
|
+
return fail(c, 405, "METHOD_NOT_ALLOWED", "This API is read-only: only GET and HEAD are served.");
|
|
2379
|
+
}
|
|
2380
|
+
await next();
|
|
2381
|
+
});
|
|
2382
|
+
app.get("/api/meta", async (c) => {
|
|
2383
|
+
const { ctx, view } = await load();
|
|
2384
|
+
return c.json(metaOf(ctx, view, opts.version ?? "0.0.0"));
|
|
2385
|
+
});
|
|
2386
|
+
app.get("/api/adrs", async (c) => {
|
|
2387
|
+
const status = all(c, "status");
|
|
2388
|
+
const unknown = status.filter((s) => !STATUSES.includes(s));
|
|
2389
|
+
if (unknown.length) throw new AdrienError("INVALID_INPUT", `Unknown status ${unknown.join(", ")}. Use: ${STATUSES.join(", ")}`);
|
|
2390
|
+
const filters = {
|
|
2391
|
+
tags: all(c, "tag"),
|
|
2392
|
+
components: all(c, "component"),
|
|
2393
|
+
status,
|
|
2394
|
+
layer: flag(c.req.query("pending")) ? "pending" : "all"
|
|
2395
|
+
};
|
|
2396
|
+
const q = c.req.query("q")?.trim();
|
|
2397
|
+
const { ctx, view } = await load();
|
|
2398
|
+
const limit = view.base.length + view.pending.length;
|
|
2399
|
+
const results = await withIndex(ctx, view, (index) => q ? index.search(q, filters, limit) : index.list(filters));
|
|
2400
|
+
return c.json({ results });
|
|
2401
|
+
});
|
|
2402
|
+
app.get("/api/adrs/:id", async (c) => {
|
|
2403
|
+
const { view } = await load();
|
|
2404
|
+
return c.json(adrOf(view, c.req.param("id")));
|
|
2405
|
+
});
|
|
2406
|
+
app.get("/api/graph", async (c) => {
|
|
2407
|
+
const { view } = await load();
|
|
2408
|
+
return c.json(graphOf(view));
|
|
2409
|
+
});
|
|
2410
|
+
app.get("/api/for", async (c) => {
|
|
2411
|
+
const paths = all(c, "path");
|
|
2412
|
+
if (!paths.length) throw new AdrienError("INVALID_INPUT", "Give at least one file: /api/for?path=src/a.ts");
|
|
2413
|
+
const { ctx, view } = await load();
|
|
2414
|
+
const note = unverifiedBaseNote(view);
|
|
2415
|
+
return c.json({
|
|
2416
|
+
files: adrsForFiles(ctx, view, paths),
|
|
2417
|
+
...note ? { notes: [note] } : {}
|
|
2418
|
+
});
|
|
2419
|
+
});
|
|
2420
|
+
app.get("/api/check", async (c) => {
|
|
2421
|
+
const { ctx, view } = await load();
|
|
2422
|
+
const diagnostics = await runCheck(ctx, view, { lint: flag(c.req.query("lint")) });
|
|
2423
|
+
return c.json({
|
|
2424
|
+
errors: diagnostics.filter((d) => d.severity === "error").length,
|
|
2425
|
+
warnings: diagnostics.filter((d) => d.severity === "warning").length,
|
|
2426
|
+
diagnostics
|
|
2427
|
+
});
|
|
2428
|
+
});
|
|
2429
|
+
app.get("/api/export", async (c) => {
|
|
2430
|
+
const format = c.req.query("format") ?? "markdown";
|
|
2431
|
+
if (!EXPORT_FORMATS.includes(format)) throw new AdrienError("INVALID_INPUT", `Unknown format "${format}". Use one of: ${EXPORT_FORMATS.join(", ")}`);
|
|
2432
|
+
const { ctx, view } = await load();
|
|
2433
|
+
return c.text(exportLog(ctx, view, format));
|
|
2434
|
+
});
|
|
2435
|
+
const web = serveWeb(opts.webDir);
|
|
2436
|
+
app.get("*", (c) => isApi(c.req.path) ? c.notFound() : web(c));
|
|
2437
|
+
app.notFound((c) => fail(c, 404, "NOT_FOUND", `No endpoint at ${c.req.path} (see docs/api.md).`));
|
|
2438
|
+
app.onError((e, c) => {
|
|
2439
|
+
if (e instanceof AdrienError) return fail(c, HTTP_STATUS[e.code] ?? 500, e.code, e.message);
|
|
2440
|
+
return fail(c, 500, "INTERNAL_ERROR", e.message);
|
|
2441
|
+
});
|
|
2442
|
+
return app;
|
|
2443
|
+
}
|
|
2444
|
+
// ../api/src/server.ts
|
|
2445
|
+
const DEFAULT_HOST = "127.0.0.1";
|
|
2446
|
+
const DEFAULT_PORT = 4310;
|
|
2447
|
+
function closeServer(server) {
|
|
2448
|
+
return new Promise((resolve) => {
|
|
2449
|
+
server.close(() => resolve());
|
|
2450
|
+
server.closeAllConnections();
|
|
2451
|
+
});
|
|
2452
|
+
}
|
|
2453
|
+
/** Listens on `host:port` (default 127.0.0.1:4310) and resolves once the socket is bound. */
|
|
2454
|
+
function startServer(opts) {
|
|
2455
|
+
const host = hostnameOf(opts.host ?? "127.0.0.1");
|
|
2456
|
+
const app = createApp({
|
|
2457
|
+
...opts,
|
|
2458
|
+
host
|
|
2459
|
+
});
|
|
2460
|
+
return new Promise((resolve, reject) => {
|
|
2461
|
+
const server = serve({
|
|
2462
|
+
fetch: app.fetch,
|
|
2463
|
+
hostname: host,
|
|
2464
|
+
port: opts.port ?? 4310
|
|
2465
|
+
}, (info) => {
|
|
2466
|
+
server.off("error", reject);
|
|
2467
|
+
resolve({
|
|
2468
|
+
url: `http://${host.includes(":") ? `[${host}]` : host}:${info.port}`,
|
|
2469
|
+
address: info.address,
|
|
2470
|
+
port: info.port,
|
|
2471
|
+
close: () => closeServer(server)
|
|
2472
|
+
});
|
|
2473
|
+
});
|
|
2474
|
+
server.once("error", reject);
|
|
2475
|
+
});
|
|
2476
|
+
}
|
|
2477
|
+
// src/commands/serve.ts
|
|
2478
|
+
const LISTEN_ERRORS = /* @__PURE__ */ new Set([
|
|
2479
|
+
"EADDRINUSE",
|
|
2480
|
+
"EADDRNOTAVAIL",
|
|
2481
|
+
"EACCES",
|
|
2482
|
+
"ENOTFOUND"
|
|
2483
|
+
]);
|
|
2484
|
+
function port(value) {
|
|
2485
|
+
if (!/^\d+$/.test(value) || Number(value) > 65535) throw new InvalidArgumentError("Use a port from 0 to 65535 (0 picks a free one).");
|
|
2486
|
+
return Number(value);
|
|
2487
|
+
}
|
|
2488
|
+
function registerServe(program, cli) {
|
|
2489
|
+
program.command("serve").description("Serve the read-only HTTP API and the web visualizer on this machine").option("--port <n>", "port to listen on (0 picks a free one)", port, DEFAULT_PORT).option("--host <host>", "address to bind; requests must name it or a loopback name", DEFAULT_HOST).action(async (opts, cmd) => {
|
|
2490
|
+
const { log } = cmd.optsWithGlobals();
|
|
2491
|
+
const ctx = await contextOf(cli, { log });
|
|
2492
|
+
const server = await startServer({
|
|
2493
|
+
cwd: cli.cwd,
|
|
2494
|
+
log,
|
|
2495
|
+
env: cli.env,
|
|
2496
|
+
version: program.version(),
|
|
2497
|
+
host: opts.host,
|
|
2498
|
+
port: opts.port,
|
|
2499
|
+
webDir: fileURLToPath(new URL("./web", import.meta.url))
|
|
2500
|
+
}).catch((e) => {
|
|
2501
|
+
if (!LISTEN_ERRORS.has(e.code ?? "")) throw e;
|
|
2502
|
+
throw new AdrienError("INVALID_INPUT", `Cannot listen on ${opts.host}:${opts.port}: ${e.message}`);
|
|
2503
|
+
});
|
|
2504
|
+
cli.out(`Adrien serving ${ctx.config.product} at ${server.url}\n`);
|
|
2505
|
+
});
|
|
2506
|
+
}
|
|
2154
2507
|
// src/commands/show.ts
|
|
2155
2508
|
function registerShow(program, cli) {
|
|
2156
2509
|
program.command("show").description("Print an ADR, with its pending change if any").argument("<id>", "ADR id").action(async (id, _opts, cmd) => {
|
|
@@ -2247,7 +2600,8 @@ function buildProgram(cli) {
|
|
|
2247
2600
|
registerRenumber,
|
|
2248
2601
|
registerSync,
|
|
2249
2602
|
registerExport,
|
|
2250
|
-
registerMcp
|
|
2603
|
+
registerMcp,
|
|
2604
|
+
registerServe
|
|
2251
2605
|
]) register(program, cli);
|
|
2252
2606
|
return program;
|
|
2253
2607
|
}
|