docs / app.tsmain at 8463f41
// app.ts — THE DOCS APP, as config/worker.ts mounts it on docs.<domain>: the files of the mind's
// repos, written together in a browser. public/app.js says what the page does. This file serves
// the page and answers where the editor's processors are (processors/classes.ts) and which version
// of them this commit holds. One more path, /notes/<name>.html, answers a stored HTML note inline
// (notes.ts).
import { type AppDescription, serveFiles, type IterateConfigEntrypoint } from "iterate/sdk";
import { openFile } from "@repos/stdlib/open-files.ts";
import theme from "@repos/stdlib/theme.css";
import index from "./index.html";
import { isNotePath, noteResponse } from "./notes.ts";
import processors from "./processors/";
import files from "./public/";
/** What Docs says of itself to anyone: the mind's sign-in pages show its name and icon. */
const DOCS: AppDescription = { name: "Docs", icon: "/icon.svg", for: "members" };
/** The repo of the mind that holds the processors' code: this one. A page starts a doc's
* processors from that repo's own files, with the packages that its package.json lists. */
const PROCESSORS_REPO = "/repos/docs";
/** The module that exports the processors' classes, as a path in that repo. */
const PROCESSORS_MODULE = "processors/classes.ts";
// The processors' source as TEXT (a folder import hands files over, it does not load them), for
// one thing: a name for this version of it. The page starts a doc's processor from this repo's
// files under that name, so a commit that changes the processors starts them anew, and a commit
// that changes anything else leaves them running.
let version: Promise<string> | undefined;
/** Where the processors' classes are and which version of them this commit holds: what
* /processors.json answers. Another app of the mind that opens a doc (the Tasks app, its task
* sheet) answers the same, so that both pages start a doc's processors under ONE name. */
export async function processorsAnswer() {
return {
repo: PROCESSORS_REPO,
mainModule: PROCESSORS_MODULE,
version: await (version ||= versionOf(processors)),
};
}
/** `icon`: the answer for the mind's icon, which every app serves as its own (open-files.ts of
* /repos/stdlib). */
export function docsApp({ icon }: { icon: () => Response }) {
return {
/** The app's description and icon, for anyone: worker.ts asks this before its guard. */
open: (request: Request) => openFile(request, DOCS, icon),
/** Every request on the app's host. worker.ts guards it, so a member alone gets here. */
async fetch(request: Request, host: Pick<IterateConfigEntrypoint, "auth" | "getItx">) {
const url = new URL(request.url);
// A stored HTML note, read in the browser: /notes/<name>.html is the mind's file of that
// path, fenced so that it cannot act as the member (notes.ts).
if (isNotePath(url.pathname)) {
using itx = host.getItx();
return await noteResponse(request, (path) => itx.files.get(path).bytes());
}
// Where the processors' classes are and which version of them this commit holds.
if (url.pathname === "/processors.json") return Response.json(await processorsAnswer());
// The page's own socket (/itx) and what @iterate-com/ui's shell asks of its host
// (/.auth/session.json, /.auth/minds.json, /.auth/logout).
const page = host.auth.memberPage(request);
if (page) return page;
// the app's own files, and the theme that every app of this mind shares
return serveFiles(request, { index, files: { ...files, "theme.css": theme } });
},
};
}
/** A short name for the processors' source: a hash of every file's path and content. */
async function versionOf(source: Record<string, string | Uint8Array>): Promise<string> {
const encoder = new TextEncoder();
const parts = Object.keys(source)
.sort()
.flatMap((path) => {
const content = source[path]!;
return [
encoder.encode(`\0${path}\0`),
typeof content === "string" ? encoder.encode(content) : content,
];
});
const bytes = new Uint8Array(parts.reduce((sum, part) => sum + part.byteLength, 0));
let at = 0;
for (const part of parts) {
bytes.set(part, at);
at += part.byteLength;
}
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
return [...digest.slice(0, 12)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
}