Diffinery

codician/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

158 lines · 7.6 KBRaw History
1/** @jsxImportSource hono/jsx */
2/**
3 * Favicons, app icons, the social preview image, robots.txt, sitemap.xml and the
4 * search/social meta tags every page carries.
5 *
6 * The PNG and ICO files come from scripts/make-icons.py (icons.generated.ts). Google
7 * shows a site's favicon in results when it is crawlable, square and a multiple of
8 * 48px, so /favicon.ico (16–64px) and /favicon-48.png are linked from every page.
9 */
10import { earlyAccessOpen } from "../lib/early-access.js";
11import { raw } from "hono/html";
12import type { AppContext } from "../auth/principal.js";
13import type { WebApp } from "./ui.js";
14import { ICON_FILES } from "./icons.generated.js";
15import { docsSitemapPaths } from "../docs/web.js";
16
17export const SITE = {
18 name: "Diffinery",
19 tagline: "Code hosting and CI for the agentic era",
20 description:
21 "Diffinery is code hosting and CI for the agentic era: Git repositories, pull requests with intent, GitHub Actions compatible workflows and AI agents as first-class collaborators, running on Cloudflare.",
22 color: "#f6821f",
23};
24
25/** The mark on an orange tile, for browsers that take SVG favicons. */
26export const FAVICON_SVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect width="24" height="24" rx="5.3" fill="${SITE.color}"/><g transform="translate(2.4 2.4) scale(.8)" fill="none" stroke="#fff" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><circle cx="6" cy="6" r="2.5"/><circle cx="6" cy="18" r="2.5"/><circle cx="18" cy="12" r="2.5"/><path d="M6 8.5v7M8.3 7.2c4 1 7.4 2.2 7.6 3.3M8.3 16.8c4-1 7.4-2.2 7.6-3.3"/></g></svg>`;
27
28const TYPES: Record<string, string> = { ico: "image/x-icon", png: "image/png" };
29const bytes = new Map<string, Uint8Array<ArrayBuffer>>();
30function file(name: string): Uint8Array<ArrayBuffer> | null {
31 const b64 = ICON_FILES[name];
32 if (!b64) return null;
33 let b = bytes.get(name);
34 if (!b) {
35 const bin = atob(b64);
36 b = new Uint8Array(bin.length);
37 for (let i = 0; i < bin.length; i++) b[i] = bin.charCodeAt(i);
38 bytes.set(name, b);
39 }
40 return b;
41}
42
43const CACHE = "public, max-age=604800";
44
45/** Paths that are never useful in search results. */
46const PRIVATE = /^\/(login|logout|auth|signin|signup|invite|settings|notifications|new|admin|request-access|early-access\/(?:confirm|remove)|-)(\/|$)/;
47
48export function registerSeo(web: WebApp): void {
49 for (const name of Object.keys(ICON_FILES)) {
50 const type = TYPES[name.split(".").pop()!]!;
51 web.get(`/${name}`, (c) => c.body(file(name)!, 200, { "content-type": type, "cache-control": CACHE, "x-content-type-options": "nosniff" }));
52 }
53 web.get("/favicon.svg", (c) => c.body(FAVICON_SVG, 200, { "content-type": "image/svg+xml", "cache-control": CACHE, "x-content-type-options": "nosniff" }));
54 web.get("/site.webmanifest", (c) =>
55 c.body(
56 JSON.stringify({
57 name: SITE.name,
58 short_name: SITE.name,
59 description: SITE.description,
60 start_url: "/",
61 display: "standalone",
62 background_color: "#ffffff",
63 theme_color: SITE.color,
64 icons: [
65 { src: "/icon-192.png", sizes: "192x192", type: "image/png" },
66 { src: "/icon-512.png", sizes: "512x512", type: "image/png" },
67 { src: "/icon-maskable-512.png", sizes: "512x512", type: "image/png", purpose: "maskable" },
68 ],
69 }),
70 200,
71 { "content-type": "application/manifest+json", "cache-control": CACHE },
72 ),
73 );
74 web.get("/robots.txt", (c) => {
75 const origin = c.env.PUBLIC_URL.replace(/\/$/, "");
76 const lines = [
77 "User-agent: *",
78 "Allow: /",
79 ...["/api/", "/mcp", "/runner/", "/auth/", "/login", "/signin", "/invite/", "/settings", "/notifications", "/new", "/admin", "/request-access", "/early-access/confirm", "/early-access/remove", "/-/", "/*.git/", "/*/raw/"].map((p) => `Disallow: ${p}`),
80 "",
81 `Sitemap: ${origin}/sitemap.xml`,
82 "",
83 ];
84 return c.text(lines.join("\n"), 200, { "cache-control": "public, max-age=86400" });
85 });
86 web.get("/sitemap.xml", async (c) => {
87 const origin = c.env.PUBLIC_URL.replace(/\/$/, "");
88 const repos = await c.get("dir").publicRepos(5000);
89 const esc = (s: string) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
90 const url = (loc: string, lastmod?: string | null) => `<url><loc>${esc(loc)}</loc>${lastmod ? `<lastmod>${esc(lastmod.slice(0, 10))}</lastmod>` : ""}</url>`;
91 const body = [
92 '<?xml version="1.0" encoding="UTF-8"?>',
93 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
94 url(`${origin}/`),
95 // The early access form, while the instance is invite-only (lib/early-access.ts).
96 ...(earlyAccessOpen(c.env) ? [url(`${origin}/early-access`)] : []),
97 ...docsSitemapPaths().map((p) => url(`${origin}${p}`)),
98 ...repos.map((r) => url(`${origin}/${encodeURIComponent(r.owner_login)}/${encodeURIComponent(r.name)}`, r.pushed_at ?? r.updated_at)),
99 "</urlset>",
100 ].join("\n");
101 return c.body(body, 200, { "content-type": "application/xml; charset=utf-8", "cache-control": "public, max-age=3600" });
102 });
103}
104
105export type SeoOpts = {
106 title: string;
107 description?: string;
108 /** Keep the page out of search results. */
109 noindex?: boolean;
110 /** Add WebSite and Organization structured data (the home page). */
111 home?: boolean;
112 nonce: string;
113};
114
115/** Icons plus search and social meta tags for the page's <head>. */
116export function SeoHead(c: AppContext, o: SeoOpts) {
117 const origin = c.env.PUBLIC_URL.replace(/\/$/, "");
118 const path = c.req.path;
119 const url = `${origin}${path}`;
120 const description = o.description?.trim() || SITE.description;
121 const noindex = o.noindex || PRIVATE.test(path);
122 const image = `${origin}/og-image.png`;
123 const ld = o.home
124 ? JSON.stringify([
125 { "@context": "https://schema.org", "@type": "WebSite", name: SITE.name, url: `${origin}/` },
126 { "@context": "https://schema.org", "@type": "Organization", name: SITE.name, url: `${origin}/`, logo: `${origin}/icon-512.png` },
127 ]).replace(/</g, "\\u003c")
128 : null;
129 return (
130 <>
131 <meta name="description" content={description} />
132 {noindex ? <meta name="robots" content="noindex, nofollow" /> : <link rel="canonical" href={url} />}
133 <link rel="icon" href="/favicon.ico" sizes="16x16 32x32 48x48 64x64" />
134 <link rel="icon" href="/favicon.svg" type="image/svg+xml" />
135 <link rel="icon" href="/favicon-48.png" sizes="48x48" type="image/png" />
136 <link rel="icon" href="/favicon-96.png" sizes="96x96" type="image/png" />
137 <link rel="apple-touch-icon" href="/apple-touch-icon.png" />
138 <link rel="manifest" href="/site.webmanifest" />
139 <meta name="theme-color" content={SITE.color} />
140 <meta name="application-name" content={SITE.name} />
141 <meta name="apple-mobile-web-app-title" content={SITE.name} />
142 <meta property="og:type" content="website" />
143 <meta property="og:site_name" content={SITE.name} />
144 <meta property="og:title" content={o.title} />
145 <meta property="og:description" content={description} />
146 <meta property="og:url" content={url} />
147 <meta property="og:image" content={image} />
148 <meta property="og:image:width" content="1200" />
149 <meta property="og:image:height" content="630" />
150 <meta property="og:image:alt" content={`${SITE.name}: ${SITE.tagline}`} />
151 <meta name="twitter:card" content="summary_large_image" />
152 <meta name="twitter:title" content={o.title} />
153 <meta name="twitter:description" content={description} />
154 <meta name="twitter:image" content={image} />
155 {ld ? <script type="application/ld+json" nonce={o.nonce}>{raw(ld)}</script> : null}
156 </>
157 );
158}