mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 18:34:09 +00:00
refactor(server): fold webauthn, account erasure, oauth and the wiki out of services/ (#1840)
* refactor(server): move the webauthn config and the account erasure into nest/auth webauthnConfig resolved the RP ID through its own getSetting() on the db singleton; it is a WebauthnConfigService over DatabaseService now, injected into AuthService (passkey_configured) and PasskeyService (four ceremonies). readEnv() is still read per call, so an operator flipping the RP ID at runtime takes effect on the next ceremony instead of after a restart. userCleanupService follows as UserCleanupService. It keeps taking removeUserFromBudgetItems from budget.bridge rather than injecting BudgetService: BudgetModule imports AuthModule for the demo guard in BudgetMcp, so AuthModule cannot import BudgetModule back, and breaking that edge the other way drags BudgetController, BudgetMcp and its trips.bridge chain into every graph that imports AuthModule. The legacy module had no tests of its own - the erasure only ever ran through AdminService.deleteUser and TripsService.deleteGuest - so USER-CLEANUP-001..009 pin both halves, including the orphan data dir that no permissions row can describe and the all-or-nothing deletion transaction. * refactor(server): fold the oauth 2.1 server out of services/ 694 lines move into OauthService over DatabaseService, AddonsService and AuditService. The thin wrapper that only forwarded is gone, and so is the second path to the MCP addon flag - validateAuthorizeRequest read it through addons.bridge while the wrapper read it through the injected service, which would have let the two drift. The pending authorization codes stay a module singleton in oauth.pending-codes.ts instead of becoming instance state. The SDK still serves /oauth/authorize, /oauth/register and /oauth/consent from its own router, mounted on Express before app.init(), so it reaches the domain through the new oauth.bridge.ts - two maps would break the authorization-code flow silently, since the SDK only ever answers "Authorization grant is invalid.". The import-time sweep keeps its .unref() and is a named function now so a test can drive it. scopes and sessionManager are imported deep, never through the src/mcp barrel: the barrel re-exports revokeUserSessionsForClient and pulls the domain bridges back in, which is the nest -> mcp -> nest cycle. Same comment as admin.service and auth.service carry. The 70 cases of the legacy suite moved over unchanged, bound to a hand-built service. Fifteen more cover what free functions never let anyone reach: the code store at capacity, an expired code, an expired access token, refresh against a public and an unknown client, the RFC 8707 resource checks, and that a code written through the container is read back through the bridge. * refactor(server): move the wiki reader into the help module wikiService had exactly one consumer, HelpController, and touches no database - it stays a plain module rather than becoming a provider, the way auth/ephemeral-tokens.ts does, so the text and asset caches keep being one per process without any ceremony around it. The WIKI_DIR anchor needed one more level: src/services sits three below the repo root, src/nest/help sits four. The existing path-resolution case caught it, which is what it is there for - a wrong anchor makes help degrade to the GitHub fallback without anybody noticing. Also brings the nest README back in line: services/ is down to five top-level files, and the passkey and budget entries no longer point at paths that were deleted since they were written.
This commit is contained in:
@@ -8,11 +8,11 @@ import {
|
||||
WikiNotFound,
|
||||
type WikiPage,
|
||||
type WikiNavSection,
|
||||
} from '../../services/wikiService';
|
||||
} from './wiki';
|
||||
|
||||
/**
|
||||
* /api/help — embedded TREK wiki, served from the `wiki/` directory that ships
|
||||
* with the app (see wikiService for the GitHub fallback). Content is public docs,
|
||||
* with the app (see wiki.ts for the GitHub fallback). Content is public docs,
|
||||
* so these endpoints are unauthenticated; that also lets <img> tags load the
|
||||
* proxied assets without sending credentials.
|
||||
*/
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { HelpController } from './help.controller';
|
||||
|
||||
/** /api/help — the bundled `wiki/` directory, read via wikiService. */
|
||||
/** /api/help — the bundled `wiki/` directory, read via ./wiki. */
|
||||
@Module({
|
||||
controllers: [HelpController],
|
||||
})
|
||||
|
||||
@@ -0,0 +1,259 @@
|
||||
import { existsSync, promises as fs } from 'fs';
|
||||
import path from 'path';
|
||||
import { readEnv } from '../../app-config';
|
||||
|
||||
/**
|
||||
* In-app Help/Wiki content, sourced from the `wiki/**` directory that ships with
|
||||
* the app — the same content that CI mirrors to the public GitHub wiki. Reading
|
||||
* from disk keeps the help pages pinned to the running version (a v1.2 install
|
||||
* shows v1.2 docs, not whatever `main` says) and works offline.
|
||||
*
|
||||
* If that directory can't be resolved — an unusual layout, an image built without
|
||||
* it — we fall back to fetching from the GitHub wiki over the network and caching
|
||||
* hourly, so help degrades instead of disappearing. The client never talks to
|
||||
* GitHub directly either way; images are proxied through /api/help/asset.
|
||||
*/
|
||||
|
||||
const REPO = 'liketrek/TREK';
|
||||
const RAW_BASE = `https://raw.githubusercontent.com/${REPO}/main/wiki`;
|
||||
const TTL_MS = 60 * 60 * 1000; // remote fallback only: refresh from GitHub at most hourly
|
||||
const SLUG_RE = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
||||
|
||||
/**
|
||||
* `server/{src,dist}/nest/help` both sit four levels under the repo root, so this
|
||||
* one anchor resolves in dev, a built source install, vitest, and Docker (where
|
||||
* the Dockerfile copies `wiki/` to /app/wiki). `process.cwd()` would not — Docker
|
||||
* runs the server from /app/server. The depth is counted from THIS file: it was
|
||||
* three while the module lived in `src/services`.
|
||||
*/
|
||||
const WIKI_DIR = readEnv().paths.wikiDir ?? path.join(__dirname, '..', '..', '..', '..', 'wiki');
|
||||
|
||||
/**
|
||||
* Probe for the sidebar rather than the bare directory: an empty or half-copied
|
||||
* `wiki/` should fall back to GitHub, not serve an empty table of contents.
|
||||
*/
|
||||
const useLocalWiki = existsSync(path.join(WIKI_DIR, '_Sidebar.md'));
|
||||
|
||||
if (!useLocalWiki) {
|
||||
console.warn(
|
||||
`[help] wiki not found at ${WIKI_DIR} — falling back to the GitHub wiki (help may not match this version)`,
|
||||
);
|
||||
}
|
||||
|
||||
export class WikiNotFound extends Error {
|
||||
status = 404;
|
||||
}
|
||||
|
||||
interface TextEntry {
|
||||
data: string;
|
||||
ts: number;
|
||||
}
|
||||
const textCache = new Map<string, TextEntry>();
|
||||
const assetCache = new Map<string, { buf: Buffer; type: string; ts: number }>();
|
||||
|
||||
const fresh = (ts: number): boolean => Date.now() - ts < TTL_MS;
|
||||
|
||||
/** Resolve a path inside the wiki dir, refusing anything that escapes it. */
|
||||
function resolveInWiki(rel: string): string {
|
||||
const root = path.resolve(WIKI_DIR);
|
||||
const full = path.resolve(root, rel);
|
||||
if (full !== root && !full.startsWith(root + path.sep)) throw new WikiNotFound(rel);
|
||||
return full;
|
||||
}
|
||||
|
||||
/** Fetch a wiki text file: local disk, or GitHub with cache → stale-cache fallback. */
|
||||
async function fetchText(file: string): Promise<string> {
|
||||
if (useLocalWiki) {
|
||||
try {
|
||||
return await fs.readFile(resolveInWiki(file), 'utf8');
|
||||
} catch (err) {
|
||||
if (err instanceof WikiNotFound) throw err;
|
||||
if ((err as NodeJS.ErrnoException).code === 'ENOENT') throw new WikiNotFound(file);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
const cached = textCache.get(file);
|
||||
if (cached && fresh(cached.ts)) return cached.data;
|
||||
try {
|
||||
const res = await fetch(`${RAW_BASE}/${encodeURIComponent(file)}`, {
|
||||
headers: { 'User-Agent': 'TREK-help', Accept: 'text/plain' },
|
||||
});
|
||||
if (res.ok) {
|
||||
const text = await res.text();
|
||||
textCache.set(file, { data: text, ts: Date.now() });
|
||||
return text;
|
||||
}
|
||||
if (res.status === 404) throw new WikiNotFound(file);
|
||||
} catch (err) {
|
||||
if (err instanceof WikiNotFound) throw err;
|
||||
// network/parse error — fall through to stale cache
|
||||
}
|
||||
if (cached) return cached.data; // serve stale rather than fail
|
||||
throw new WikiNotFound(file);
|
||||
}
|
||||
|
||||
export interface WikiNavItem {
|
||||
title: string;
|
||||
slug: string;
|
||||
}
|
||||
export interface WikiNavSection {
|
||||
title: string;
|
||||
pages: WikiNavItem[];
|
||||
}
|
||||
|
||||
/** Parse the wiki `_Sidebar.md` into ordered sections of `[[Title|Slug]]` links. */
|
||||
function parseSidebar(md: string): WikiNavSection[] {
|
||||
const sections: WikiNavSection[] = [];
|
||||
let current: WikiNavSection | null = null;
|
||||
for (const raw of md.split('\n')) {
|
||||
const heading = raw.match(/^#{1,4}\s+(.+?)\s*$/);
|
||||
if (heading) {
|
||||
current = { title: heading[1].replace(/[*_`]/g, '').trim(), pages: [] };
|
||||
sections.push(current);
|
||||
continue;
|
||||
}
|
||||
const link = raw.match(/^\s*[-*]\s*\[\[([^\]]+)\]\]/);
|
||||
if (link) {
|
||||
if (!current) {
|
||||
current = { title: '', pages: [] };
|
||||
sections.push(current);
|
||||
}
|
||||
const inner = link[1];
|
||||
const [title, slugRaw] = inner.includes('|') ? inner.split('|') : [inner, inner];
|
||||
const slug = slugRaw.trim().replace(/\s+/g, '-');
|
||||
if (SLUG_RE.test(slug)) current.pages.push({ title: title.trim(), slug });
|
||||
}
|
||||
}
|
||||
return sections.filter((s) => s.pages.length > 0);
|
||||
}
|
||||
|
||||
/** Rewrite GitHub-wiki `[[..]]` links to /help routes and proxy relative images. */
|
||||
function processMarkdown(md: string): string {
|
||||
// Strip HTML comments (e.g. `<!-- TODO: screenshot … -->` placeholders) — the
|
||||
// markdown renderer would otherwise surface them as raw text.
|
||||
let out = md.replace(/<!--[\s\S]*?-->/g, '');
|
||||
out = out.replace(/\[\[([^\]]+)\]\]/g, (_m, inner: string) => {
|
||||
const [titleRaw, slugRaw] = inner.includes('|') ? inner.split('|') : [inner, inner];
|
||||
const slug = slugRaw.trim().replace(/\s+/g, '-');
|
||||
// `[[Plugin Development#talking-to-plugins|Plugin-Development]]` must not
|
||||
// render its anchor as visible link text.
|
||||
const [title, anchor] = titleRaw.includes('#') ? titleRaw.split('#') : [titleRaw, ''];
|
||||
const hash = anchor ? `#${anchor.trim()}` : '';
|
||||
return `[${title.trim()}](/help/${slug}${hash})`;
|
||||
});
|
||||
out = out.replace(/!\[([^\]]*)\]\(([^)\s]+)([^)]*)\)/g, (m, alt: string, url: string) => {
|
||||
if (/^https?:\/\//i.test(url) || url.startsWith('/api/help/asset/')) return m;
|
||||
const clean = url.replace(/^\.?\//, '').replace(/^wiki\//, '');
|
||||
return ``;
|
||||
});
|
||||
// Bare relative links — `[Currencies](Currencies)`, the native GitHub-wiki
|
||||
// spelling and by far the most common in these pages (455 of them across 81
|
||||
// files, against 114 `[[..]]` links). GitHub resolves them against the wiki
|
||||
// root; in-app they used to fall through to HelpPage's external-link branch
|
||||
// and open a dead tab. Rewriting them here fixes every page at once and keeps
|
||||
// the source GitHub-compatible, so contributors can keep writing either form.
|
||||
//
|
||||
// Runs last: `[[..]]` links and images have already become absolute paths by
|
||||
// this point, so the leading-slash guard skips them.
|
||||
out = outsideCode(out, (segment) =>
|
||||
segment.replace(/(^|[^!])\[([^\]]+)\]\(([^)\s]+)\)/g, (m, prefix: string, text: string, url: string) => {
|
||||
if (/^(https?:|mailto:|tel:|#|\/)/i.test(url)) return m;
|
||||
const [pageRaw, anchor] = url.includes('#') ? url.split('#') : [url, ''];
|
||||
const page = pageRaw.replace(/^\.?\//, '').replace(/\.md$/i, '').trim();
|
||||
if (!page || !SLUG_RE.test(page)) return m;
|
||||
return `${prefix}[${text}](/help/${page}${anchor ? `#${anchor}` : ''})`;
|
||||
}),
|
||||
);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply `fn` to the parts of the markdown that are NOT code, leaving fenced
|
||||
* blocks and inline spans untouched.
|
||||
*
|
||||
* Without this the link rewriter corrupts code samples: Plugin-Development.md
|
||||
* documents `actions[key](ctx)`, which reads as a markdown link and would be
|
||||
* rewritten to `actions[key](/help/ctx)` inside what is supposed to be a
|
||||
* verbatim snippet.
|
||||
*/
|
||||
function outsideCode(md: string, fn: (segment: string) => string): string {
|
||||
// Alternation order matters: fenced blocks first, so a ``` fence containing
|
||||
// backticks is consumed whole rather than being split by the inline rule.
|
||||
const CODE = /(```[\s\S]*?```|~~~[\s\S]*?~~~|`[^`\n]*`)/g;
|
||||
const parts = md.split(CODE);
|
||||
// split() with a capturing group yields [text, code, text, code, …].
|
||||
return parts.map((part, i) => (i % 2 === 1 ? part : fn(part))).join('');
|
||||
}
|
||||
|
||||
function extractTitle(md: string, fallback: string): string {
|
||||
const h1 = md.match(/^#\s+(.+?)\s*$/m);
|
||||
return h1 ? h1[1].replace(/[*_`]/g, '').trim() : fallback.replace(/-/g, ' ');
|
||||
}
|
||||
|
||||
export interface WikiPage {
|
||||
slug: string;
|
||||
title: string;
|
||||
markdown: string;
|
||||
}
|
||||
|
||||
/** True when help is served from the bundled wiki rather than fetched from GitHub. */
|
||||
export const isLocalWiki = (): boolean => useLocalWiki;
|
||||
|
||||
export async function getWikiIndex(): Promise<{ sections: WikiNavSection[] }> {
|
||||
const md = await fetchText('_Sidebar.md');
|
||||
return { sections: parseSidebar(md) };
|
||||
}
|
||||
|
||||
export async function getWikiPage(slug: string): Promise<WikiPage> {
|
||||
if (!SLUG_RE.test(slug)) throw new WikiNotFound(slug);
|
||||
const md = await fetchText(`${slug}.md`);
|
||||
return { slug, title: extractTitle(md, slug), markdown: processMarkdown(md) };
|
||||
}
|
||||
|
||||
const ASSET_TYPES: Record<string, string> = {
|
||||
'.png': 'image/png',
|
||||
'.jpg': 'image/jpeg',
|
||||
'.jpeg': 'image/jpeg',
|
||||
'.gif': 'image/gif',
|
||||
'.webp': 'image/webp',
|
||||
'.svg': 'image/svg+xml',
|
||||
};
|
||||
|
||||
/** Read a wiki image from disk, or proxy it from GitHub so the browser never calls it directly. */
|
||||
export async function getWikiAsset(assetPath: string): Promise<{ buf: Buffer; type: string }> {
|
||||
// Defend against traversal; allow nested image folders.
|
||||
if (assetPath.includes('..') || !/^[A-Za-z0-9/._-]+$/.test(assetPath)) throw new WikiNotFound(assetPath);
|
||||
const ext = path.extname(assetPath).toLowerCase();
|
||||
const type = ASSET_TYPES[ext];
|
||||
if (!type) throw new WikiNotFound(assetPath);
|
||||
|
||||
if (useLocalWiki) {
|
||||
try {
|
||||
// resolveInWiki re-checks containment: the regex above is a filter, this is the boundary.
|
||||
const buf = await fs.readFile(resolveInWiki(assetPath));
|
||||
return { buf, type };
|
||||
} catch (err) {
|
||||
if (err instanceof WikiNotFound) throw err;
|
||||
if ((err as NodeJS.ErrnoException).code === 'ENOENT') throw new WikiNotFound(assetPath);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
const cached = assetCache.get(assetPath);
|
||||
if (cached && fresh(cached.ts)) return { buf: cached.buf, type: cached.type };
|
||||
try {
|
||||
const res = await fetch(`${RAW_BASE}/${assetPath.split('/').map(encodeURIComponent).join('/')}`, {
|
||||
headers: { 'User-Agent': 'TREK-help' },
|
||||
});
|
||||
if (res.ok) {
|
||||
const buf = Buffer.from(await res.arrayBuffer());
|
||||
assetCache.set(assetPath, { buf, type, ts: Date.now() });
|
||||
return { buf, type };
|
||||
}
|
||||
} catch {
|
||||
/* fall through */
|
||||
}
|
||||
if (cached) return { buf: cached.buf, type: cached.type };
|
||||
throw new WikiNotFound(assetPath);
|
||||
}
|
||||
Reference in New Issue
Block a user