g1t/apps/web/app/lib/public-cache.ts

25 lines1,266 bytesCodeBlame
1/**
2 * The signed-out page cache (workers/app.ts) keeps a repository's pages for
3 * visitors who are not signed in. Before a kept page is served, the
4 * repository is asked for again: one that was made private, or deleted,
5 * since the page was kept is never served from the cache, wherever it was
6 * kept. This says which repository a kept page belongs to.
7 */
8
9/** `<workspace>/<repository>` of a project page's path, or null for any other page. */
10export function repositoryOfPage(pathname: string): string | null {
11 const [, workspace, rest] = /^\/([^/]+)\/([^/]+)/.exec(pathname) ?? [];
12 if (!workspace || !rest || rest === "-") return null;
13 const name = rest.replace(/\.data$/, "");
14 if (!name) return null;
15 return `${decodeURIComponent(workspace).toLowerCase()}/${decodeURIComponent(name).toLowerCase()}`;
16}
17
18/** Whether repos' `visibility` answer says the repository is still there and public. */
19export function stillPublic(answer: unknown, path: string): boolean {
20 if (!Array.isArray(answer)) return false;
21 const found = answer.find((v): v is { path: string; is_private: boolean } => {
22 return typeof v === "object" && v !== null && (v as { path?: unknown }).path === path;
23 });
24 return found !== undefined && found.is_private === false;
25}