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.

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