mirror of
https://github.com/anomalyco/opencode.git
synced 2026-09-28 13:33:35 +08:00
refactor(console): redirect desktop downloads (#49984)
Co-authored-by: thdxr <826656+thdxr@users.noreply.github.com>
This commit is contained in:
co-authored by
thdxr
parent
ae93d4afb3
commit
fee476bb90
@@ -1,5 +1,4 @@
|
||||
import type { APIEvent } from "@solidjs/start"
|
||||
import { waitUntil } from "@opencode-ai/console-resource"
|
||||
import type { DownloadPlatform } from "../types"
|
||||
|
||||
const prodAssetNames: Record<string, string> = {
|
||||
@@ -20,13 +19,6 @@ const betaAssetNames: Record<string, string> = {
|
||||
"linux-x64-rpm": "opencode-desktop-linux-x86_64.rpm",
|
||||
} satisfies Record<DownloadPlatform, string>
|
||||
|
||||
// Doing this on the server lets us preserve the original name for platforms we don't care to rename for
|
||||
const downloadNames: Record<string, string> = {
|
||||
"darwin-aarch64-dmg": "OpenCode Desktop.dmg",
|
||||
"darwin-x64-dmg": "OpenCode Desktop.dmg",
|
||||
"windows-x64-nsis": "OpenCode Desktop Installer.exe",
|
||||
} satisfies { [K in DownloadPlatform]?: string }
|
||||
|
||||
export async function GET({ params: { platform, channel } }: APIEvent) {
|
||||
const assetName = channel === "stable" ? prodAssetNames[platform] : betaAssetNames[platform]
|
||||
if (!assetName) return new Response(null, { status: 404 })
|
||||
@@ -37,30 +29,7 @@ export async function GET({ params: { platform, channel } }: APIEvent) {
|
||||
if (!release.ok) return new Response(null, { status: release.status })
|
||||
const location = getAssetUrl(await release.json(), assetName)
|
||||
if (!location) return new Response(null, { status: 502 })
|
||||
|
||||
const key = new Request(location)
|
||||
const cache = (caches as CacheStorage & { default: Cache }).default
|
||||
const cached = await cache.match(key)
|
||||
if (cached) return download(cached, platform, "HIT")
|
||||
|
||||
const resp = await fetch(location)
|
||||
if (!resp.ok) return resp
|
||||
|
||||
const headers = new Headers(resp.headers)
|
||||
headers.set("cache-control", "public, max-age=31536000, immutable")
|
||||
headers.delete("set-cookie")
|
||||
const result = new Response(resp.body, { status: resp.status, statusText: resp.statusText, headers })
|
||||
waitUntil(cache.put(key, result.clone()))
|
||||
return download(result, platform, "MISS")
|
||||
}
|
||||
|
||||
function download(resp: Response, platform: string, cache: "HIT" | "MISS") {
|
||||
const downloadName = downloadNames[platform]
|
||||
const headers = new Headers(resp.headers)
|
||||
if (downloadName) headers.set("content-disposition", `attachment; filename="${downloadName}"`)
|
||||
headers.set("x-opencode-cache", cache)
|
||||
|
||||
return new Response(resp.body, { status: resp.status, statusText: resp.statusText, headers })
|
||||
return Response.redirect(location, 302)
|
||||
}
|
||||
|
||||
function getAssetUrl(input: unknown, assetName: string) {
|
||||
|
||||
Reference in New Issue
Block a user