/** * The Mac app's DOWNLOAD — the zip itself, served from cottondb.com. * * It used to be a Forgejo release asset, and the appcast's enclosure pointed * there. `decemberfm/cottondb` is a private repo, so that URL answers * `303 → /user/login` to anyone without a session — which is every copy of * Sparkle and every person who clicked "Download for Mac". Measured 2026-09-13 * and again 2026-09-16: no Mac has ever updated itself through the feed. * * So one canonical URL serves them: * * https://cottondb.com/mac/download/CottonDB-.zip * * plus `/mac/download/latest`, a 302 to whichever zip the served appcast names * first — the address a person's "Download for Mac" button points at, so a * release never has to edit the page. * * WHERE THE BYTES ARE (architect, 10 Oct 2026). A Mac build is a Cotton * release record: its zip and its stamped Sparkle item are the record's files, * held as media of the project database. This Worker forwards the download and * the feed to the serving Mac (`MAC_FEED_ORIGIN`, through the relay), which * answers off the release rows (`crates/cotton-server/src/routes/mac_feed.rs`). * Nothing copies a zip into a bucket, and a site deploy never touches a build. */ /** Everything under here is the download surface. One prefix, no aliases. */ export const MAC_DOWNLOAD_PREFIX = "/mac/download/"; /** The version-free address a button, a page or a person can keep. */ export const MAC_DOWNLOAD_LATEST_PATH = "/mac/download/latest"; /** Whether a request is for the un-versioned download. Exact. */ export function isMacLatestPath(pathname: string): boolean { return pathname === MAC_DOWNLOAD_LATEST_PATH; } /** * A zip's file name, exactly as the release lane writes it. * * One shape and nothing else: `CottonDB-...zip`. The key * in the bucket IS this name, so the grammar is what keeps a request from * naming an object the release lane never wrote — no path segments, no `..`, * no query-shaped tail. */ export const MAC_ZIP_NAME = /^CottonDB-(\d+\.\d+\.\d+)\.zip$/; /** The object key a download path names, or null when the path is not one. */ export function macDownloadKey(pathname: string): string | null { if (!pathname.startsWith(MAC_DOWNLOAD_PREFIX)) return null; const name = pathname.slice(MAC_DOWNLOAD_PREFIX.length); return MAC_ZIP_NAME.test(name) ? name : null; } /** The zip's file name for a version. The single place the name is spelled. */ export function macZipName(version: string): string { return `CottonDB-${version}.zip`; } /** The canonical path a version is served at. */ export function macDownloadPath(version: string): string { return `${MAC_DOWNLOAD_PREFIX}${macZipName(version)}`; } /** * What a zip is served as. * * `immutable`, and a year of it: a version's bytes never change — a new build * is a new version and a new URL. So a browser that has the file never asks * again, and Cloudflare's edge holds it for everybody else. */ export const MAC_DOWNLOAD_CACHE_CONTROL = "public, max-age=31536000, immutable"; /** `latest` is a pointer, and a pointer that is cached is a pointer that lies. */ export const MAC_LATEST_CACHE_CONTROL = "no-store"; /** The type. Sparkle and every browser want the same one. */ export const MAC_DOWNLOAD_CONTENT_TYPE = "application/zip"; /** One release, as the appcast describes it. */ export interface MacRelease { /** `sparkle:shortVersionString`, e.g. `0.4.6`. */ version: string; /** The enclosure URL, verbatim. */ url: string; /** `length` in bytes, as the signed feed states it. */ length: number; } /** * The newest release in an appcast — the FIRST item, which is the order the * serving Mac writes its feed in. * * A deliberately small parser: the feed is the serving Mac's own, not * arbitrary XML off the network, and a Worker has no DOM. */ export function latestRelease(xml: string): MacRelease | null { const item = xml.match(/[\s\S]*?<\/item>/)?.[0]; if (!item) return null; const enclosure = item.match(/]*>/)?.[0]; if (!enclosure) return null; const url = enclosure.match(/\burl="([^"]+)"/)?.[1]; const length = enclosure.match(/\blength="(\d+)"/)?.[1]; const version = item.match(/([^<]+)<\/sparkle:shortVersionString>/)?.[1]; if (!url || !length || !version) return null; return { version: version.trim(), url, length: Number(length) }; } /** * The path on THIS origin that an enclosure URL names, or null. * * The redirect takes the PATH, never the URL: a feed is a file, and a file * that could redirect this site's visitors to any host is an open redirect * one bad edit away. So the enclosure has to name a download this site * serves, and anything else is a feed problem the Worker says out loud. */ export function downloadPathForEnclosure(url: string): string | null { let pathname: string; try { pathname = new URL(url).pathname; } catch { return null; } return macDownloadKey(pathname) ? pathname : null; }