0
0
site: socialweb.computer built from the repo's README and docs
site/.gitignorenew+1−0
| @@ -0,0 +1 @@ | ||
out/ | ||
site/build.shnew+7−0
| @@ -0,0 +1,7 @@ | ||
#!/bin/sh | ||
# Renders README.md and the user docs to site/out, the tree of https://socialweb.computer/ (marked: site/deno.json, | ||
# locked by site/deno.lock; deno: site/deno.sh). | ||
# site/build.sh | ||
set -eu | ||
cd "$(dirname "$0")/.." | ||
exec site/deno.sh run --config site/deno.json --allow-read=. --allow-write=site/out --allow-run=git site/build.ts | ||
site/build.tsnew+181−0
| @@ -0,0 +1,181 @@ | ||
import { Marked } from "marked"; | ||
| ||
const REPO = "https://knotless.dev/socialweb.computer/socialweb-computer-kcp"; | ||
const REF = "main"; | ||
const SITE = "socialweb.computer"; | ||
const PAGES = [ | ||
"README.md", | ||
"docs/README.md", | ||
"docs/quickstart-prod.md", | ||
"docs/quickstart-local.md", | ||
"docs/bidder.md", | ||
"docs/gateway.md", | ||
"docs/deploy.md", | ||
]; | ||
const EXTRA_LINKS = [ | ||
{ href: "https://blog.socialweb.computer", text: "blog" }, | ||
{ | ||
href: "https://docs.google.com/presentation/d/1hvM3jk-t_3vkjIXr1gJL1fmtUMhHm76369Vos3QumZU/edit", | ||
text: "2026-08-04 talk: slides", | ||
}, | ||
{ href: "https://asciinema.org/a/1262425", text: "recording" }, | ||
]; | ||
| ||
const root = new URL("..", import.meta.url).pathname.replace(/\/$/, ""); | ||
const out = `${root}/site/out`; | ||
| ||
function outPath(src: string): string { | ||
if (src === "README.md") return "index.html"; | ||
return src.replace(/(^|\/)README\.md$/, "$1index.html").replace(/\.md$/, ".html"); | ||
} | ||
| ||
function dirname(p: string): string { | ||
const i = p.lastIndexOf("/"); | ||
return i < 0 ? "" : p.slice(0, i); | ||
} | ||
| ||
function normalize(p: string): string { | ||
const parts: string[] = []; | ||
for (const s of p.split("/")) { | ||
if (s === "" || s === ".") continue; | ||
if (s === "..") parts.pop(); | ||
else parts.push(s); | ||
} | ||
return parts.join("/"); | ||
} | ||
| ||
function relative(fromFile: string, toFile: string): string { | ||
const from = dirname(fromFile).split("/").filter(Boolean); | ||
const to = toFile.split("/"); | ||
let i = 0; | ||
while (i < from.length && i < to.length - 1 && from[i] === to[i]) i++; | ||
const up = from.slice(i).map(() => ".."); | ||
let rel = [...up, ...to.slice(i)].join("/"); | ||
if (rel === "index.html") rel = "./"; | ||
else if (rel.endsWith("/index.html")) rel = rel.slice(0, -"index.html".length); | ||
return rel; | ||
} | ||
| ||
function isDir(path: string): boolean { | ||
try { | ||
return Deno.statSync(`${root}/${path}`).isDirectory; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
| ||
function rewrite(src: string, href: string): string { | ||
if (/^[a-z][a-z0-9+.-]*:/i.test(href) || href.startsWith("#")) return href; | ||
const hash = href.indexOf("#"); | ||
const path = hash < 0 ? href : href.slice(0, hash); | ||
const frag = hash < 0 ? "" : href.slice(hash); | ||
const target = normalize(`${dirname(src)}/${path}`); | ||
if (PAGES.includes(target)) return relative(outPath(src), outPath(target)) + frag; | ||
const kind = path.endsWith("/") || isDir(target) ? "tree" : "blob"; | ||
return `${REPO}/${kind}/${REF}/${target}${frag}`; | ||
} | ||
| ||
function escape(s: string): string { | ||
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """); | ||
} | ||
| ||
function slug(text: string): string { | ||
return text.toLowerCase().trim().replace(/<[^>]*>/g, "").replace(/[^\p{L}\p{N}\s_-]/gu, "").replace(/\s/g, "-"); | ||
} | ||
| ||
function render(src: string, md: string): { title: string; body: string } { | ||
const seen = new Map<string, number>(); | ||
let title = ""; | ||
const marked = new Marked({ gfm: true }); | ||
marked.use({ | ||
walkTokens(token) { | ||
if (token.type === "link") token.href = rewrite(src, token.href); | ||
}, | ||
renderer: { | ||
heading({ tokens, depth, text }) { | ||
const plain = text.replace(/`/g, ""); | ||
if (depth === 1 && !title) title = plain; | ||
const base = slug(plain); | ||
const n = seen.get(base) ?? 0; | ||
seen.set(base, n + 1); | ||
const id = n ? `${base}-${n}` : base; | ||
const inner = this.parser.parseInline(tokens); | ||
return `<h${depth} id="${id}"><a class="anchor" href="#${id}">${inner}</a></h${depth}>\n`; | ||
}, | ||
html({ text }) { | ||
return escape(text); | ||
}, | ||
table(token) { | ||
const head = token.header.map((c) => `<th>${this.parser.parseInline(c.tokens)}</th>`).join(""); | ||
const rows = token.rows.map((r) => `<tr>${r.map((c) => `<td>${this.parser.parseInline(c.tokens)}</td>`).join("")}</tr>`).join("\n"); | ||
return `<div class="table"><table><thead><tr>${head}</tr></thead><tbody>${rows}</tbody></table></div>\n`; | ||
}, | ||
}, | ||
}); | ||
const body = marked.parse(md, { async: false }) as string; | ||
return { title: title || src, body }; | ||
} | ||
| ||
async function git(...args: string[]): Promise<string> { | ||
const r = await new Deno.Command("git", { args: ["-C", root, ...args], stdout: "piped", stderr: "inherit" }).output(); | ||
if (!r.success) throw new Error(`git ${args.join(" ")} failed`); | ||
return new TextDecoder().decode(r.stdout).trim(); | ||
} | ||
| ||
function page(src: string, title: string, body: string, commit: string, tagline: string, css: string): string { | ||
const here = outPath(src); | ||
const nav = [ | ||
{ href: REPO, text: "repository" }, | ||
{ href: relative(here, "index.html"), text: "README" }, | ||
{ href: relative(here, "docs/index.html"), text: "docs" }, | ||
{ href: `${REPO}/blob/${REF}/${src}`, text: "this page's source" }, | ||
]; | ||
const a = (l: { href: string; text: string }) => `<a href="${escape(l.href)}">${escape(l.text)}</a>`; | ||
const short = commit.slice(0, 12); | ||
const commitLink = commit.endsWith("-dirty") | ||
? escape(commit) | ||
: `<a href="${REPO}/commit/${commit}">${short}</a>`; | ||
return `<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta name="description" content="${escape(tagline)}"> | ||
<title>${escape(src === "README.md" ? SITE : `${title} - ${SITE}`)}</title> | ||
<link rel="icon" href="data:,"> | ||
<link rel="stylesheet" href="${relative(here, css)}"> | ||
</head> | ||
<body> | ||
<header> | ||
<p class="name"><a href="${relative(here, "index.html")}">${SITE}</a></p> | ||
<p class="tagline">${escape(tagline)}</p> | ||
<nav>${nav.map(a).join("\n")}</nav> | ||
</header> | ||
<main> | ||
${body}</main> | ||
<footer> | ||
<p>Rendered from <code>${escape(src)}</code> of ${a({ href: REPO, text: "socialweb-computer-kcp" })} at commit ${commitLink}. Unlicense (public domain).</p> | ||
<p>${EXTRA_LINKS.map(a).join(" | ")}</p> | ||
</footer> | ||
</body> | ||
</html> | ||
`; | ||
} | ||
| ||
const head = await git("rev-parse", "HEAD"); | ||
const dirty = (await git("status", "--porcelain", "--", ...PAGES, "site")) !== ""; | ||
const commit = dirty ? `${head}-dirty` : head; | ||
const readme = await Deno.readTextFile(`${root}/README.md`); | ||
const tagline = (readme.split(/\n\s*\n/).find((p) => p.trim() && !p.startsWith("#")) ?? "") | ||
.replace(/\s+/g, " ").replace(/`/g, "").trim().split(/(?<=\.) /)[0]; | ||
| ||
await Deno.remove(out, { recursive: true }).catch(() => {}); | ||
await Deno.mkdir(`${out}/docs`, { recursive: true }); | ||
await Deno.copyFile(`${root}/site/style.css`, `${out}/style.css`); | ||
for (const src of PAGES) { | ||
const md = await Deno.readTextFile(`${root}/${src}`); | ||
const { title, body } = render(src, md); | ||
await Deno.writeTextFile(`${out}/${outPath(src)}`, page(src, title, body, commit, tagline, "style.css")); | ||
} | ||
await Deno.writeTextFile(`${out}/COMMIT`, `${commit}\n`); | ||
console.log(`built ${out} from ${commit}: ${PAGES.length} pages`); | ||
site/deno.jsonnew+10−0
| @@ -0,0 +1,10 @@ | ||
{ | ||
"imports": { | ||
"marked": "npm:marked@18.0.5" | ||
}, | ||
"lock": { | ||
"path": "deno.lock", | ||
"frozen": true | ||
}, | ||
"nodeModulesDir": "none" | ||
} | ||
site/deno.locknew+17−0
| @@ -0,0 +1,17 @@ | ||
{ | ||
"version": "5", | ||
"specifiers": { | ||
"npm:marked@18.0.5": "18.0.5" | ||
}, | ||
"npm": { | ||
"marked@18.0.5": { | ||
"integrity": "sha512-S6GcvALHg6K4ohtu4E7x0a1AqhAjp6cV8KhLSyN9qVapnzJkusVBxZRcIU9AeYsbe6P1hKDusSbEOzGyyuce6w==", | ||
"bin": true | ||
} | ||
}, | ||
"workspace": { | ||
"dependencies": [ | ||
"npm:marked@18.0.5" | ||
] | ||
} | ||
} | ||
site/deno.shnew+13−0
| @@ -0,0 +1,13 @@ | ||
#!/bin/sh | ||
# Runs the pinned deno of tests/compositions/staging/page-tools.json: PATH's (or $DENO) when it is that version, | ||
# else the one tests/compositions/staging/tools.sh fetches and checks by its sha256. | ||
# site/deno.sh <deno arguments> | ||
set -eu | ||
cd "$(dirname "$0")/.." | ||
pin=$(jq -er .deno.version tests/compositions/staging/page-tools.json) | ||
deno=${DENO:-deno} | ||
if ! "$deno" --version 2>/dev/null | grep -q "^deno $pin "; then | ||
tools=$(SWC_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/socialweb-site" SWC_REPO="$PWD" bash tests/compositions/staging/tools.sh deno) | ||
deno=$tools/deno/deno | ||
fi | ||
exec "$deno" "$@" | ||
site/deploy.shnew+71−0
| @@ -0,0 +1,71 @@ | ||
#!/bin/sh | ||
# Builds site/out and serves it at https://socialweb.computer/ from the gateway's /var/www/socialweb.computer (Caddy's | ||
# site block for it is in /etc/caddy/Caddyfile; nothing here touches Caddy or any other site). It prints what it would | ||
# change, saves the tree it replaces to /var/backups/socialweb.computer-<UTC time>.tgz on the gateway, swaps the new | ||
# tree in and checks every built file against what https://socialweb.computer/ serves. | ||
# site/deploy.sh [--build-only] [--dry-run] | ||
set -eu | ||
cd "$(dirname "$0")/.." | ||
SITE=socialweb.computer | ||
SSH_TARGET=${SSH_TARGET:-root@socialweb.computer} | ||
SSH_PORT=${SSH_PORT:-1997} | ||
build_only= dry_run= | ||
for a in "$@"; do | ||
case $a in | ||
--build-only) build_only=1 ;; | ||
--dry-run) dry_run=1 ;; | ||
*) echo "usage: site/deploy.sh [--build-only] [--dry-run]" >&2; exit 2 ;; | ||
esac | ||
done | ||
ssh="ssh -p $SSH_PORT -o BatchMode=yes $SSH_TARGET" | ||
out=site/out | ||
| ||
site/build.sh | ||
bad=$(cd "$out" && find . -mindepth 1 \( -name '.*' -o \( ! -type d ! -type f \) -o \( -type f ! -name '*.html' ! -name '*.css' ! -name '*.svg' ! -name '*.png' ! -name '*.ico' ! -name '*.txt' ! -name COMMIT \) \) -print) | ||
if [ -n "$bad" ]; then | ||
echo "deploy: refusing: $out holds files that are not site files:" >&2 | ||
echo "$bad" >&2 | ||
exit 1 | ||
fi | ||
commit=$(cat "$out/COMMIT") | ||
[ -z "$build_only" ] || exit 0 | ||
| ||
mkdir -p "$HOME/.tmp" | ||
work=$(mktemp -d "${TMPDIR:-$HOME/.tmp}/site-deploy.XXXXXX") | ||
trap 'rm -rf "$work"' EXIT | ||
(cd "$out" && find . -type f -exec sha256sum {} + | sort -k2) > "$work/new" | ||
$ssh "d=/var/www/$SITE; [ ! -d \"\$d\" ] || (cd \"\$d\" && find . ! -type d ! -type f -printf 'NOT-A-FILE %p\n'; find . -type f -exec sha256sum {} + | sort -k2)" > "$work/old" | ||
echo "deploy: $SSH_TARGET:/var/www/$SITE now -> $commit" | ||
awk 'NR == FNR { old[$2] = $1; next } { seen[$2] = 1; if (!($2 in old)) print " add " $2; else if (old[$2] != $1) print " change " $2 } | ||
END { for (p in old) if (!(p in seen)) print " remove " p }' "$work/old" "$work/new" | sort -k2 | ||
[ -z "$dry_run" ] || { echo "deploy: --dry-run: nothing changed"; exit 0; } | ||
| ||
case $commit in *-dirty) | ||
echo "deploy: refusing: the tree has changes not in a commit ($commit); commit them first" >&2 | ||
exit 1 ;; | ||
esac | ||
if [ -z "$(git for-each-ref --contains "$commit" refs/remotes/swc)" ]; then | ||
echo "deploy: refusing: $commit is on no swc branch; push it first (the footer links it)" >&2 | ||
exit 1 | ||
fi | ||
| ||
tar -C "$out" -cf - . | $ssh "rm -rf /var/www/$SITE.new && mkdir -p /var/www/$SITE.new && tar -C /var/www/$SITE.new --no-same-owner -xf -" | ||
$ssh "SITE=$SITE sh -s" <<'REMOTE' | ||
set -eu | ||
d=/var/www/$SITE | ||
ts=$(date -u +%Y%m%dT%H%M%SZ) | ||
find "$d.new" -type d -exec chmod 0755 {} + | ||
find "$d.new" -type f -exec chmod 0644 {} + | ||
chown -R caddy:caddy "$d.new" | ||
if [ -d "$d" ]; then | ||
tar -C /var/www -czf "/var/backups/$SITE-$ts.tgz" "$SITE" | ||
chmod 0600 "/var/backups/$SITE-$ts.tgz" | ||
echo "backup /var/backups/$SITE-$ts.tgz" | ||
fi | ||
rm -rf "$d.old" | ||
[ ! -d "$d" ] || mv "$d" "$d.old" | ||
mv "$d.new" "$d" | ||
rm -rf "$d.old" | ||
REMOTE | ||
echo "deployed $commit" | ||
site/deno.sh run --no-config --no-lock --allow-read="$out" --allow-net="$SITE" site/verify.ts "https://$SITE/" "$out" | ||
site/README.mdnew+30−0
| @@ -0,0 +1,30 @@ | ||
# site | ||
| ||
The source of https://socialweb.computer/: `build.ts` renders [README.md](../README.md) to `out/index.html` and the | ||
user docs (`docs/README.md`, `docs/quickstart-prod.md`, `docs/quickstart-local.md`, `docs/bidder.md`, | ||
`docs/gateway.md`, `docs/deploy.md`) to `out/docs/`, so the page is the README of the commit it was built from. A link | ||
to a rendered doc goes to its page; any other repository link goes to the file on | ||
https://knotless.dev/socialweb.computer/socialweb-computer-kcp (main). The footer and `out/COMMIT` name the commit. | ||
| ||
Dependencies: jq, git, the deno pinned in `tests/compositions/staging/page-tools.json` (`deno.sh` uses PATH's when it | ||
is that version, else fetches it and checks its sha256), and marked pinned in `deno.json` and locked in `deno.lock`. | ||
| ||
Build, then look at `site/out/index.html`: | ||
| ||
```sh | ||
site/build.sh | ||
``` | ||
| ||
Deploy (ssh as root to the gateway, port 1997; `--dry-run` prints what would change and stops). It refuses a tree with | ||
uncommitted changes or a commit on no `swc` branch, and anything in `out/` but html, css, svg, png, ico, txt and | ||
`COMMIT`; it saves the tree it replaces to `/var/backups/socialweb.computer-<UTC time>.tgz` on the gateway, swaps in | ||
the new one and checks every file against what https://socialweb.computer/ serves. Caddy is not touched: its site | ||
block for socialweb.computer is in the gateway's `/etc/caddy/Caddyfile`. | ||
| ||
```sh | ||
site/deploy.sh --dry-run | ||
site/deploy.sh | ||
``` | ||
| ||
Roll back: on the gateway, `tar -C /var/www -xzf /var/backups/socialweb.computer-<UTC time>.tgz` after moving | ||
`/var/www/socialweb.computer` aside. | ||
site/style.cssnew+134−0
| @@ -0,0 +1,134 @@ | ||
:root { | ||
--bg: #ffffff; | ||
--fg: #1b1f24; | ||
--muted: #59636e; | ||
--link: #0b5cad; | ||
--code-bg: #f3f4f6; | ||
--border: #d8dde3; | ||
color-scheme: light dark; | ||
} | ||
| ||
@media (prefers-color-scheme: dark) { | ||
:root { | ||
--bg: #0f1216; | ||
--fg: #e4e7eb; | ||
--muted: #9aa4ae; | ||
--link: #6cb0ff; | ||
--code-bg: #1a1f26; | ||
--border: #2d343d; | ||
} | ||
} | ||
| ||
* { | ||
box-sizing: border-box; | ||
} | ||
| ||
html { | ||
-webkit-text-size-adjust: 100%; | ||
} | ||
| ||
body { | ||
margin: 0 auto; | ||
max-width: 52rem; | ||
padding: 0 16px 3rem; | ||
background: var(--bg); | ||
color: var(--fg); | ||
font: 16px/1.6 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; | ||
overflow-wrap: break-word; | ||
} | ||
| ||
a { | ||
color: var(--link); | ||
} | ||
| ||
header { | ||
border-bottom: 1px solid var(--border); | ||
padding: 1rem 0 0.75rem; | ||
} | ||
| ||
header p { | ||
margin: 0; | ||
} | ||
| ||
header .name a { | ||
color: var(--fg); | ||
font-weight: 700; | ||
font-size: 1.25rem; | ||
text-decoration: none; | ||
} | ||
| ||
header .tagline { | ||
color: var(--muted); | ||
} | ||
| ||
nav { | ||
display: flex; | ||
flex-wrap: wrap; | ||
gap: 0.25rem 1rem; | ||
margin-top: 0.5rem; | ||
} | ||
| ||
h1, h2, h3, h4 { | ||
line-height: 1.25; | ||
margin: 1.75rem 0 0.75rem; | ||
} | ||
| ||
h1 { | ||
font-size: 1.75rem; | ||
} | ||
| ||
h2 { | ||
font-size: 1.35rem; | ||
border-bottom: 1px solid var(--border); | ||
padding-bottom: 0.25rem; | ||
} | ||
| ||
h1 a.anchor, h2 a.anchor, h3 a.anchor, h4 a.anchor { | ||
color: inherit; | ||
text-decoration: none; | ||
} | ||
| ||
code { | ||
font: 0.9em/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; | ||
background: var(--code-bg); | ||
border-radius: 4px; | ||
padding: 0.1em 0.3em; | ||
} | ||
| ||
pre { | ||
background: var(--code-bg); | ||
border: 1px solid var(--border); | ||
border-radius: 6px; | ||
padding: 0.75rem 1rem; | ||
overflow-x: auto; | ||
} | ||
| ||
pre code { | ||
background: none; | ||
padding: 0; | ||
white-space: pre; | ||
} | ||
| ||
.table { | ||
overflow-x: auto; | ||
} | ||
| ||
table { | ||
border-collapse: collapse; | ||
margin: 1rem 0; | ||
} | ||
| ||
th, td { | ||
border: 1px solid var(--border); | ||
padding: 0.35rem 0.6rem; | ||
text-align: left; | ||
vertical-align: top; | ||
} | ||
| ||
footer { | ||
border-top: 1px solid var(--border); | ||
margin-top: 3rem; | ||
padding-top: 0.75rem; | ||
color: var(--muted); | ||
font-size: 0.9rem; | ||
} | ||
site/verify.tsnew+40−0
| @@ -0,0 +1,40 @@ | ||
const [origin, dir] = Deno.args; | ||
| ||
async function sha256(bytes: Uint8Array): Promise<string> { | ||
const d = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes)); | ||
return [...d].map((b) => b.toString(16).padStart(2, "0")).join(""); | ||
} | ||
| ||
async function* files(base: string, rel = ""): AsyncGenerator<string> { | ||
for await (const e of Deno.readDir(`${base}/${rel}`)) { | ||
const p = rel ? `${rel}/${e.name}` : e.name; | ||
if (e.isDirectory) yield* files(base, p); | ||
else yield p; | ||
} | ||
} | ||
| ||
let bad = 0; | ||
const paths = []; | ||
for await (const p of files(dir)) paths.push(p); | ||
paths.sort(); | ||
for (const p of paths) { | ||
const url = new URL(p === "index.html" ? "" : p, origin).href; | ||
const want = await sha256(await Deno.readFile(`${dir}/${p}`)); | ||
let got = ""; | ||
let status = 0; | ||
try { | ||
const r = await fetch(url, { headers: { "cache-control": "no-cache" } }); | ||
status = r.status; | ||
got = await sha256(new Uint8Array(await r.arrayBuffer())); | ||
} catch (e) { | ||
got = String(e); | ||
} | ||
const ok = status === 200 && got === want; | ||
if (!ok) bad++; | ||
console.log(`${ok ? "OK " : "FAIL"} ${url} ${status} ${got === want ? want : `want ${want} got ${got}`}`); | ||
} | ||
if (bad) { | ||
console.error(`verify: ${bad} of ${paths.length} differ`); | ||
Deno.exit(1); | ||
} | ||
console.log(`verify: ${paths.length} of ${paths.length} match`); | ||