A url link now opens in the app instead of the browser. The server fetches the page (robots.txt honoured, public addresses only, size and time caps), keeps a snapshot on disk until it is reloaded, and cuts it into a lead and sections: by its headings, else by pseudo-headings, else in chunks, or by a model when asked (s), within a daily budget. The pane has its own amber ground, the author and date in its head when the page gives them, and the source in its foot. W opens the original, R reads it again. See docs/pages.md. Pictures and videos are kept, in web pages and in Wikipedia articles: pictures through the server (only addresses seen in a page that was read), videos as a still that loads the player on a click. A section that is only a link (a target, no text, no sections) opens as its article at once, and L opens a target to the right of the node's own pane, never under it. Article text is sized to read like the reader's own. Panes: a pane that already has all its column can give beside the others' tabs no longer peeks over them on hover, and the curves of tabs under a peeking pane are no longer drawn over it. The keypress timing test judges each of its two passes on its own, so a stall from outside the app does not fail it. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
180 lines
8.4 KiB
TypeScript
180 lines
8.4 KiB
TypeScript
/**
|
|
* HTTP routes over server/page.ts:
|
|
*
|
|
* GET /api/page/article?url=https://… the page as a lead and sections
|
|
* GET /api/page/summary?url=https://…[&cached=1] title, site, author, date, first sentence
|
|
* GET /api/page/media?u=https://… an image a page read here shows
|
|
* POST /api/page/reload { url } read the page again (the reader's Reload)
|
|
* POST /api/page/arrange { url } summarise it with a model, and arrange a page
|
|
* divided by length into sections (pageArrange.ts)
|
|
*
|
|
* summary &cached=1 answers from the snapshot cache only, never reading the page: 404
|
|
* { ok: false, reason: "uncached" } when it has none. media serves only addresses that appear in
|
|
* a kept snapshot (it is not an open proxy): its pictures, and the stills of its videos (a
|
|
* poster, a YouTube thumbnail); image types only, with headers that keep the browser from
|
|
* treating the answer as anything but an image.
|
|
* Answers: 200 { ok: true, cached, article | summary }; 400 bad input; 403 a cross-site request;
|
|
* 404 uncached; 422 a WebUnusable for a page that cannot be shown (robots, refused, type, empty,
|
|
* too-large); 503 a WebUnusable when the site could not be reached or failed (offline, error).
|
|
* A request the browser abandons is not answered; the read goes on, so the next open is immediate.
|
|
* With EX_PAGES=off the server wires in pageOff instead: every route answers 503 reason "off".
|
|
*
|
|
* reload answers 200 { ok: true, article } with the new copy, or with the last copy and
|
|
* `stale: <why>` when the page no longer answers; a page never shown before that still cannot be
|
|
* shown is a 422 or 503 WebUnusable, as for article. arrange never reads the page: it works on the
|
|
* kept copy. 200 { ok: true, article, called, arranged: "sections" | "summary", note? }; refused
|
|
* with { ok: false, reason, message }: 404 uncached (not read here yet), 409 old (a copy read before
|
|
* arranging existed), 422 too-long, 429 budget or plan, 502 failed (the call failed), 503 no-model
|
|
* or off (EX_PAGES_ARRANGE=off).
|
|
*
|
|
* Every route refuses a request another site's page makes (Sec-Fetch-Site, else Origin, against
|
|
* the request's own Host). Requests with neither header (curl, the tests) are allowed. The two
|
|
* that act (reload reads a site, arrange spends model budget) are POST only; the others GET only.
|
|
*/
|
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
import type { MediaResult, PageClient, PageResult, WebUnusable } from "./page";
|
|
import type { ArrangeRefusal, Arranger } from "./pageArrange";
|
|
import { HttpError, abortOnClose, readJson, sendJson as send } from "./util";
|
|
import type { Uncached } from "./wiki";
|
|
|
|
const MAX_URL = 2000;
|
|
|
|
const bad = (message: string) => new HttpError(400, message);
|
|
|
|
/** Why this request is refused as cross-site, or null if it may go on. */
|
|
export function crossSite(req: IncomingMessage): string | null {
|
|
const site = req.headers["sec-fetch-site"];
|
|
if (site && site !== "same-origin" && site !== "none") return `a ${site} request`;
|
|
const origin = req.headers.origin;
|
|
if (origin === undefined) return null;
|
|
try {
|
|
return new URL(origin).host === req.headers.host ? null : `a request from ${origin}`;
|
|
} catch {
|
|
return `a request from origin ${origin}`;
|
|
}
|
|
}
|
|
|
|
export function address(url: URL, name: string): string {
|
|
return checked(url.searchParams.get(name), name);
|
|
}
|
|
|
|
function checked(given: unknown, name: string): string {
|
|
const v = (typeof given === "string" ? given : "").trim();
|
|
if (!v) throw bad(`${name} is required`);
|
|
if (v.length > MAX_URL) throw bad(`${name} is longer than ${MAX_URL} characters`);
|
|
let x: URL;
|
|
try {
|
|
x = new URL(v);
|
|
} catch {
|
|
throw bad(`${name} is not an address`);
|
|
}
|
|
if (x.protocol !== "http:" && x.protocol !== "https:") throw bad(`${name} must be an http or https address`);
|
|
return v;
|
|
}
|
|
|
|
const LASTING = new Set(["robots", "refused", "type", "empty", "too-large"]);
|
|
|
|
/**
|
|
* An image as the media routes send it (this one, and /api/wiki/media): with headers that keep
|
|
* the browser from treating it as anything but an image, cached a week by this browser only.
|
|
*/
|
|
export function sendMedia(res: ServerResponse, r: MediaResult | { ok: false; reason: "aborted" }) {
|
|
if (r.ok) {
|
|
res.writeHead(200, {
|
|
"content-type": r.type,
|
|
"content-length": String(r.body.length),
|
|
"cache-control": "private, max-age=604800",
|
|
"content-security-policy": "sandbox",
|
|
"x-content-type-options": "nosniff",
|
|
});
|
|
res.end(r.body);
|
|
} else if (!("reason" in r)) send(res, r.status, { ok: false, message: r.message });
|
|
}
|
|
|
|
/** The routes that act, and are POST only; the rest are GET only. */
|
|
const POSTS = new Set(["reload", "arrange"]);
|
|
|
|
const REFUSED: Record<ArrangeRefusal, number> = { uncached: 404, old: 409, "too-long": 422, budget: 429, plan: 429, failed: 502, "no-model": 503, off: 503 };
|
|
|
|
function reply<V>(res: ServerResponse, r: PageResult<V> | Uncached | { ok: false; reason: "aborted" }, key: string) {
|
|
if (r.ok) return send(res, 200, { ok: true, cached: r.cached, [key]: r.value });
|
|
if (r.reason === "aborted") return; // the browser has gone
|
|
if (r.reason === "uncached") return send(res, 404, r);
|
|
return send(res, LASTING.has(r.reason) ? 422 : 503, r);
|
|
}
|
|
|
|
/** Route /api/page/*. Returns false for any other path. Without an arranger, arrange answers 503 off. */
|
|
export function pageRoutes(client: () => PageClient, arranger?: () => Arranger) {
|
|
return async function handle(req: IncomingMessage, res: ServerResponse, url: URL): Promise<boolean> {
|
|
if (!url.pathname.startsWith("/api/page/")) return false;
|
|
const route = url.pathname.slice("/api/page/".length);
|
|
const refused = crossSite(req);
|
|
if (refused) {
|
|
send(res, 403, { ok: false, message: `Refused ${refused}: these routes answer this app's own pages only.` });
|
|
return true;
|
|
}
|
|
const method = POSTS.has(route) ? "POST" : "GET";
|
|
if ((req.method ?? "GET") !== method) {
|
|
send(res, 405, { ok: false, message: `use ${method}` }, { allow: method });
|
|
return true;
|
|
}
|
|
try {
|
|
const p = client();
|
|
const signal = abortOnClose(res);
|
|
switch (route) {
|
|
case "reload": {
|
|
const body = await readJson<{ url?: unknown }>(req, 16 * 1024);
|
|
const r = await p.reload(checked(body.url, "url"), { signal });
|
|
if (r.ok) send(res, 200, { ok: true, cached: false, article: r.value, ...(r.stale ? { stale: r.stale } : {}) });
|
|
else reply(res, r, "article");
|
|
return true;
|
|
}
|
|
case "arrange": {
|
|
const body = await readJson<{ url?: unknown }>(req, 16 * 1024);
|
|
const u = checked(body.url, "url");
|
|
if (!arranger) {
|
|
send(res, 503, { ok: false, reason: "off", message: "Arranging pages with a model is not available on this server." });
|
|
return true;
|
|
}
|
|
// Not abandoned when the browser goes: the call is made once, and its result kept for the next ask.
|
|
const r = await arranger().arrange(u);
|
|
if (r.ok) send(res, 200, r);
|
|
else send(res, REFUSED[r.reason], r);
|
|
return true;
|
|
}
|
|
case "article":
|
|
reply(res, await p.article(address(url, "url"), { signal }), "article");
|
|
return true;
|
|
case "summary": {
|
|
const u = address(url, "url");
|
|
reply(res, await p.summary(u, { signal, cacheOnly: url.searchParams.get("cached") === "1" }), "summary");
|
|
return true;
|
|
}
|
|
case "media":
|
|
sendMedia(res, await p.media(address(url, "u"), { signal }));
|
|
return true;
|
|
default:
|
|
send(res, 404, { ok: false, message: `no route /api/page/${route}` });
|
|
return true;
|
|
}
|
|
} catch (e) {
|
|
if (e instanceof HttpError) send(res, e.status, { ok: false, message: e.message });
|
|
else send(res, 500, { ok: false, message: (e as Error).message ?? String(e) });
|
|
return true;
|
|
}
|
|
};
|
|
}
|
|
|
|
/** EX_PAGES=off: the routes exist, so the UI can tell "turned off" from "unreachable", but none works. */
|
|
export async function pageOff(_req: IncomingMessage, res: ServerResponse, url: URL): Promise<boolean> {
|
|
if (!url.pathname.startsWith("/api/page/")) return false;
|
|
const off: WebUnusable = {
|
|
ok: false,
|
|
reason: "off",
|
|
message: "Reading web pages is turned off on this server (EX_PAGES=off).",
|
|
url: url.searchParams.get("url") ?? url.searchParams.get("u") ?? "",
|
|
};
|
|
send(res, 503, off);
|
|
return true;
|
|
}
|