Bucksley / website / functions / _middleware.js
| 1 | // Cloudflare Pages Function. _routes.json limits it to the paths below, so every other |
| 2 | // request is served straight from static assets. |
| 3 | // - /downloads/<file> streams installers from the R2 bucket bound as DOWNLOADS (when bound), |
| 4 | // so installers over Pages' 25 MiB file limit keep their URLs on the site. |
| 5 | // - LEGACY_APPCAST sends old app builds to the feed they can still verify. |
| 6 | // Set to { path: "/appcast.xml", userAgent: /^App\/1\.0\.[0-3] Sparkle\//, to: "/appcast-legacy.xml" } only if old builds need a legacy feed. |
| 7 | const LEGACY_APPCAST = null; |
| 8 | |
| 9 | // /careers was removed (the owner 2026-12-02). Answer it here with the normal 404 page, without asking |
| 10 | // the asset layer, which still holds a stale copy of the old careers page. |
| 11 | const REMOVED_PATHS = new Set(['/careers', '/careers/']); |
| 12 | |
| 13 | async function removedPage(env, url) { |
| 14 | let page = await env.ASSETS.fetch(new Request(new URL('/404.html', url))); |
| 15 | if (page.status >= 300 && page.status < 400) page = await env.ASSETS.fetch(new Request(new URL(page.headers.get('location') || '/404', url))); |
| 16 | return new Response(page.body, { status: 404, headers: { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' } }); |
| 17 | } |
| 18 | |
| 19 | export async function onRequest(context) { |
| 20 | const { request, env, next } = context; |
| 21 | const url = new URL(request.url); |
| 22 | if (REMOVED_PATHS.has(url.pathname)) return removedPage(env, url); |
| 23 | const path = url.pathname; |
| 24 | |
| 25 | if (LEGACY_APPCAST && path === LEGACY_APPCAST.path && LEGACY_APPCAST.userAgent.test(request.headers.get('user-agent') || '')) { |
| 26 | return Response.redirect(new URL(LEGACY_APPCAST.to, url).toString(), 307); |
| 27 | } |
| 28 | |
| 29 | if (path.startsWith('/downloads/') && env.DOWNLOADS && path.length > '/downloads/'.length) { |
| 30 | const key = decodeURIComponent(path.slice('/downloads/'.length)); |
| 31 | if (request.method !== 'GET' && request.method !== 'HEAD') return new Response('Method not allowed', { status: 405 }); |
| 32 | const object = await env.DOWNLOADS.get(key, { range: request.headers, onlyIf: request.headers }); |
| 33 | if (object === null) return next(); |
| 34 | const headers = new Headers(); |
| 35 | object.writeHttpMetadata(headers); |
| 36 | headers.set('etag', object.httpEtag); |
| 37 | headers.set('accept-ranges', 'bytes'); |
| 38 | headers.set('content-disposition', 'attachment'); |
| 39 | headers.set('cache-control', 'public, max-age=3600'); |
| 40 | if (/\.dmg$/i.test(key)) headers.set('content-type', 'application/x-apple-diskimage'); |
| 41 | else if (/\.zip$/i.test(key)) headers.set('content-type', 'application/zip'); |
| 42 | headers.set('x-content-type-options', 'nosniff'); |
| 43 | if (!('body' in object)) return new Response(null, { status: 304, headers }); |
| 44 | let status = 200; |
| 45 | if (object.range && request.headers.has('range')) { |
| 46 | const { offset = 0, length = object.size - offset } = object.range; |
| 47 | headers.set('content-range', `bytes ${offset}-${offset + length - 1}/${object.size}`); |
| 48 | headers.set('content-length', String(length)); |
| 49 | status = 206; |
| 50 | } else { |
| 51 | headers.set('content-length', String(object.size)); |
| 52 | } |
| 53 | return new Response(request.method === 'HEAD' ? null : object.body, { status, headers }); |
| 54 | } |
| 55 | |
| 56 | return next(); |
| 57 | } |