Wiki: drop the unused lookup, search and section routes
Nothing in the page or the link scanner called /api/wiki/lookup, /api/wiki/search or /api/wiki/section. Remove the routes and what existed only for them: readTitles and the POST allowance, WikiClient.search and SearchHit, TTL.search, snippet(), the mock's search/page answer, WikiClient.section and Section. WikiClient.lookup stays (the scanner uses it), as do suggest and article. Tests of the removed code go; input validation and off mode are now checked on remaining routes. The mock's Eukaryote fixture gets its own page id instead of sharing Hostile article's. README and docs/wikipedia.md list what exists. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
1 parent
60d26908d6
commit
5c867b2eef
9 files changed
+24
-190
No files matched your search
+3
-44
@@ -1,12 +1,9 @@
|
||||
/**
|
||||
* HTTP routes over server/wiki.ts:
|
||||
*
|
||||
* GET /api/wiki/lookup?titles=A|B|C or POST { titles: string[] } (≤ 200 titles)
|
||||
* GET /api/wiki/suggest?q=Brou&limit=8 (q ≤ 200 chars)
|
||||
* GET /api/wiki/search?q=fixed+point&limit=10 (q ≤ 300 chars)
|
||||
* GET /api/wiki/suggest?q=Brou&limit=8 (q ≤ 200 chars)
|
||||
* GET /api/wiki/summary?title=Octopus[&cached=1]
|
||||
* GET /api/wiki/article?title=Octopus[&full=1]
|
||||
* GET /api/wiki/section?title=Octopus&id=3
|
||||
* GET /api/wiki/senses?title=Set the articles a disambiguation page lists
|
||||
*
|
||||
* summary &cached=1 answers from the server's cache only, never waiting on Wikipedia (for model
|
||||
@@ -22,9 +19,7 @@ import type { IncomingMessage, ServerResponse } from "node:http";
|
||||
import { HttpError, abortOnClose, sendJson as send } from "./util";
|
||||
import type { CallOpts, Result, Uncached, WikiClient } from "./wiki";
|
||||
|
||||
const MAX_TITLES = 200;
|
||||
const MAX_TITLE = 255;
|
||||
const MAX_BODY = 64 * 1024;
|
||||
|
||||
const bad = (message: string) => new HttpError(400, message);
|
||||
|
||||
@@ -50,55 +45,22 @@ function int(url: URL, name: string, fallback: number, lo: number, hi: number):
|
||||
return n;
|
||||
}
|
||||
|
||||
async function readTitles(req: IncomingMessage, url: URL): Promise<string[]> {
|
||||
let titles: unknown;
|
||||
if (req.method === "POST") {
|
||||
let size = 0;
|
||||
const chunks: Buffer[] = [];
|
||||
for await (const c of req) {
|
||||
size += (c as Buffer).length;
|
||||
if (size > MAX_BODY) throw bad("body too large");
|
||||
chunks.push(c as Buffer);
|
||||
}
|
||||
try {
|
||||
titles = (JSON.parse(Buffer.concat(chunks).toString("utf8") || "{}") as { titles?: unknown }).titles;
|
||||
} catch {
|
||||
throw bad("body is not JSON");
|
||||
}
|
||||
} else {
|
||||
titles = text(url, "titles", MAX_TITLES * (MAX_TITLE + 1)).split("|");
|
||||
}
|
||||
if (!Array.isArray(titles) || !titles.every((t) => typeof t === "string")) throw bad("titles must be a list of strings");
|
||||
if (titles.length > MAX_TITLES) throw bad(`at most ${MAX_TITLES} titles`);
|
||||
if (titles.some((t) => t.length > MAX_TITLE)) throw bad(`a title is longer than ${MAX_TITLE} characters`);
|
||||
return titles;
|
||||
}
|
||||
|
||||
/** Route /api/wiki/*. Returns false for any other path. */
|
||||
export function wikiRoutes(client: () => WikiClient) {
|
||||
return async function handle(req: IncomingMessage, res: ServerResponse, url: URL): Promise<boolean> {
|
||||
if (!url.pathname.startsWith("/api/wiki/")) return false;
|
||||
const route = url.pathname.slice("/api/wiki/".length);
|
||||
const allowed = route === "lookup" ? ["GET", "POST"] : ["GET"];
|
||||
if (!allowed.includes(req.method ?? "GET")) {
|
||||
send(res, 405, { ok: false, message: `use ${allowed.join(" or ")}` }, { allow: allowed.join(", ") });
|
||||
if ((req.method ?? "GET") !== "GET") {
|
||||
send(res, 405, { ok: false, message: "use GET" }, { allow: "GET" });
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
const w = client();
|
||||
const o: CallOpts = { priority: url.searchParams.get("bulk") === "1" ? "bulk" : "interactive", signal: abortOnClose(res) };
|
||||
switch (route) {
|
||||
case "lookup": {
|
||||
const results = await w.lookup(await readTitles(req, url), o);
|
||||
send(res, 200, { ok: true, results });
|
||||
return true;
|
||||
}
|
||||
case "suggest":
|
||||
reply(res, await w.suggest(text(url, "q", 200), int(url, "limit", 8, 1, 20), o), "suggestions");
|
||||
return true;
|
||||
case "search":
|
||||
reply(res, await w.search(text(url, "q", 300), int(url, "limit", 10, 1, 50), o), "results");
|
||||
return true;
|
||||
case "summary": {
|
||||
const title = text(url, "title", MAX_TITLE);
|
||||
reply(res, url.searchParams.get("cached") === "1" ? await w.summary(title, { ...o, cacheOnly: true }) : await w.summary(title, o), "summary");
|
||||
@@ -107,9 +69,6 @@ export function wikiRoutes(client: () => WikiClient) {
|
||||
case "article":
|
||||
reply(res, await w.article(text(url, "title", MAX_TITLE), { ...o, sections: url.searchParams.get("full") === "1" }), "article");
|
||||
return true;
|
||||
case "section":
|
||||
reply(res, await w.section(text(url, "title", MAX_TITLE), int(url, "id", 0, 0, 10_000), o), "section");
|
||||
return true;
|
||||
case "senses":
|
||||
reply(res, await w.senses(text(url, "title", MAX_TITLE), o), "senses");
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user