/** * 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-<version>.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. */exportconst MAC_DOWNLOAD_PREFIX = "/mac/download/";/** The version-free address a button, a page or a person can keep. */exportconst MAC_DOWNLOAD_LATEST_PATH = "/mac/download/latest";/** Whether a request is for the un-versioned download. Exact. */exportfunction 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-<major>.<minor>.<patch>.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. */exportconst MAC_ZIP_NAME = /^CottonDB-(\d+\.\d+\.\d+)\.zip$/;/** The object key a download path names, or null when the path is not one. */exportfunction macDownloadKey(pathname: string): string | null {if (!pathname.startsWith(MAC_DOWNLOAD_PREFIX)) returnnull;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. */exportfunction macZipName(version: string): string {return`CottonDB-${version}.zip`;}/** The canonical path a version is served at. */exportfunction 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. */exportconst MAC_DOWNLOAD_CACHE_CONTROL = "public, max-age=31536000, immutable";/** `latest` is a pointer, and a pointer that is cached is a pointer that lies. */exportconst MAC_LATEST_CACHE_CONTROL = "no-store";/** The type. Sparkle and every browser want the same one. */exportconst MAC_DOWNLOAD_CONTENT_TYPE = "application/zip";/** One release, as the appcast describes it. */exportinterfaceMacRelease {/** `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. */exportfunction latestRelease(xml: string): MacRelease | null {const item = xml.match(/<item>[\s\S]*?<\/item>/)?.[0];if (!item) returnnull;const enclosure = item.match(/<enclosure\b[^>]*>/)?.[0];if (!enclosure) returnnull;const url = enclosure.match(/\burl="([^"]+)"/)?.[1];const length = enclosure.match(/\blength="(\d+)"/)?.[1];const version = item.match(/<sparkle:shortVersionString>([^<]+)<\/sparkle:shortVersionString>/)?.[1];if (!url || !length || !version) returnnull;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. */exportfunction downloadPathForEnclosure(url: string): string | null {let pathname: string;try { pathname = new URL(url).pathname; } catch {returnnull; }return macDownloadKey(pathname) ? pathname : null;}