Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions functions/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Serves the latest Upright disk image from this domain. The release lives on the
// upstream repository; this function finds the latest tag, fetches the .dmg server-side
// and streams it back, so the browser never sees a redirect or the upstream address.
//
// The latest tag comes from the redirect on /releases/latest rather than the API, which
// rate-limits shared egress IPs. The tag is cached for an hour; the body is not cached.
const REPO = "https://github.com/dpdev69/upright";
const TAG_RE = /^v\d+\.\d+\.\d+$/;
const TAG_TTL = 3600;
const UPSTREAM_HEADERS = { "User-Agent": "upright-site-download" };
const UNAVAILABLE =
"The download is temporarily unavailable. Please try again in a few minutes, or email support@hellobasalt.dev.";

// "https://…/releases/tag/v1.1.2" -> "v1.1.2", or null for anything else.
export function tagFromLocation(location) {
if (!location) return null;
let path;
try { path = new URL(location, REPO + "/").pathname; } catch { return null; }
const m = path.match(/\/releases\/tag\/([^/]+)\/?$/);
if (!m) return null;
let tag;
try { tag = decodeURIComponent(m[1]); } catch { return null; }
return TAG_RE.test(tag) ? tag : null;
}

// "v1.1.2" -> { filename: "Upright-1.1.2.dmg", url: "…/releases/download/v1.1.2/Upright-1.1.2.dmg" }
export function assetFor(tag) {
if (typeof tag !== "string" || !TAG_RE.test(tag)) return null;
const filename = `Upright-${tag.slice(1)}.dmg`;
return { filename, url: `${REPO}/releases/download/${tag}/${filename}` };
}

function unavailable() {
return new Response(UNAVAILABLE + "\n", {
status: 503,
headers: {
"Content-Type": "text/plain; charset=utf-8",
"Cache-Control": "no-store",
"Retry-After": "300",
"X-Robots-Tag": "noindex",
},
});
}

async function latestTag(origin, waitUntil) {
const key = new Request(`${origin}/__cache/download-latest-tag`);
try {
const hit = await caches.default.match(key);
if (hit) {
const tag = (await hit.text()).trim();
if (TAG_RE.test(tag)) return tag;
}
} catch { /* no cache: look it up */ }

const res = await fetch(`${REPO}/releases/latest`, { redirect: "manual", headers: UPSTREAM_HEADERS });
if (res.body) await res.body.cancel();
if (res.status < 300 || res.status > 399) return null;
const tag = tagFromLocation(res.headers.get("Location"));
if (!tag) return null;

try {
const entry = new Response(tag, { headers: { "Cache-Control": `public, max-age=${TAG_TTL}` } });
waitUntil(caches.default.put(key, entry).catch(() => {}));
} catch { /* caching is best effort */ }
return tag;
}

async function serve(context) {
try {
const origin = new URL(context.request.url).origin;
const tag = await latestTag(origin, (p) => context.waitUntil(p));
const asset = tag && assetFor(tag);
if (!asset) return unavailable();

const up = await fetch(asset.url, { redirect: "follow", headers: UPSTREAM_HEADERS });
if (!up.ok || !up.body) {
if (up.body) await up.body.cancel();
return unavailable();
}

const headers = new Headers({
"Content-Type": "application/x-apple-diskimage",
"Content-Disposition": `attachment; filename="${asset.filename}"`,
"Cache-Control": "public, max-age=3600",
"X-Robots-Tag": "noindex",
"X-Content-Type-Options": "nosniff",
});
const length = up.headers.get("Content-Length");
if (length && /^\d+$/.test(length)) headers.set("Content-Length", length);
return new Response(up.body, { status: 200, headers });
} catch {
return unavailable();
}
}

export const onRequestGet = serve;

// Same status and headers as GET, without the body.
export async function onRequestHead(context) {
const res = await serve(context);
if (res.body) await res.body.cancel();
return new Response(null, { status: res.status, headers: res.headers });
}
16 changes: 4 additions & 12 deletions site/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
</head>
<body>
<a class="skip" href="#main">Skip to content</a>
<svg width="0" height="0" style="position:absolute" aria-hidden="true" focusable="false">
<defs>
<symbol id="i-github" viewBox="0 0 24 24"><path fill="currentColor" d="M12 2C6.48 2 2 6.58 2 12.23c0 4.52 2.87 8.35 6.84 9.7.5.1.68-.22.68-.49l-.01-1.7c-2.78.62-3.37-1.37-3.37-1.37-.46-1.18-1.11-1.5-1.11-1.5-.91-.64.07-.62.07-.62 1 .07 1.53 1.06 1.53 1.06.9 1.57 2.35 1.12 2.92.85.09-.67.35-1.12.64-1.38-2.22-.26-4.56-1.14-4.56-5.07 0-1.12.39-2.04 1.03-2.76-.1-.26-.45-1.3.1-2.71 0 0 .84-.28 2.75 1.05a9.4 9.4 0 0 1 5 0c1.91-1.33 2.75-1.05 2.75-1.05.55 1.41.2 2.45.1 2.71.64.72 1.03 1.64 1.03 2.76 0 3.94-2.34 4.81-4.57 5.06.36.32.68.94.68 1.9l-.01 2.81c0 .27.18.6.69.49A10.14 10.14 0 0 0 22 12.23C22 6.58 17.52 2 12 2Z"/></symbol>
</defs>
</svg>

<header class="nav">
<div class="wrap nav-inner">
Expand All @@ -35,8 +30,7 @@
<a href="/#faq">FAQ</a>
</nav>
<div class="nav-end">
<a class="nav-gh" href="https://github.com/dpdev69/upright" aria-label="GitHub"><svg aria-hidden="true"><use href="#i-github"/></svg><span>GitHub</span></a>
<a class="btn btn-primary btn-sm" href="https://github.com/dpdev69/upright/releases/latest">Download</a>
<a class="btn btn-primary btn-sm" href="/download">Download</a>
</div>
</div>
</header>
Expand All @@ -56,7 +50,7 @@ <h1>Page not found</h1>
<li><a href="/upright-vs-bettermouse">Upright vs BetterMouse</a></li>
<li><a href="/upright-vs-steermouse">Upright vs SteerMouse</a></li>
<li><a href="/mx-vertical">Upright on the Logitech MX Vertical</a></li>
<li><a href="https://github.com/dpdev69/upright/releases/latest">Download the latest release</a></li>
<li><a href="/download">Download the latest release</a></li>
</ul>
</div>
</div>
Expand All @@ -81,7 +75,6 @@ <h1>Page not found</h1>
<a href="/upright-vs-bettermouse">Upright vs BetterMouse</a>
<a href="/upright-vs-steermouse">Upright vs SteerMouse</a>
<a href="/upright-vs-openlogi">Upright vs OpenLogi</a>
<a href="https://github.com/dpdev69/upright">GitHub</a>
</nav>
<nav aria-labelledby="f-basalt">
<p class="f-title" id="f-basalt">More from Basalt</p>
Expand All @@ -90,7 +83,6 @@ <h1>Page not found</h1>
<a href="https://gocmeden.hellobasalt.dev/">Göçmeden, remote jobs</a>
<a href="https://cosmicpost.lol/">Cosmic Post, space photo keepsakes</a>
<a href="https://kacpapel.app/">Kaç Papel?, price game</a>
<a href="https://github.com/dpdev69/claude-fight-hud">Fight HUD for Claude Code</a>
<a href="https://folio.hellobasalt.dev/">Folio, opencode as a book</a>
<a href="https://byewall.hellobasalt.dev/">ByeWall, find archived pages</a>
<a href="https://kahve.hellobasalt.dev/">KahveDrop, coffee finder</a>
Expand All @@ -104,11 +96,11 @@ <h1>Page not found</h1>
<a href="/#faq">FAQ</a>
<a href="/privacy">Privacy</a>
<a href="/terms">Terms</a>
<a href="https://github.com/dpdev69/upright/blob/main/SECURITY.md">Security</a>
<a href="mailto:support@hellobasalt.dev?subject=Upright%20security">Security</a>
</nav>
</div>
<div class="footer-base">
<p>© 2026 Upright contributors · <a href="https://github.com/dpdev69/upright/blob/main/LICENSE">MIT license</a> · A <a href="https://hellobasalt.dev/">Basalt</a> app</p>
<p>© 2026 Upright contributors · <a href="/license">MIT license</a> · A <a href="https://hellobasalt.dev/">Basalt</a> app</p>
<p>Logitech, MX, Unifying, Bolt and Logi Options+ are trademarks of Logitech. Upright is an independent project and is not affiliated with or endorsed by Logitech.</p>
</div>
</div>
Expand Down
Loading
Loading