Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: make desktop download redirects resilient

Richie McIlroy82a6926e0902
diff --git a/apps/web/__tests__/unit/download-route.test.ts b/apps/web/__tests__/unit/download-route.test.ts
new file mode 100644
index 000000000..81257ba11
--- /dev/null
+++ b/apps/web/__tests__/unit/download-route.test.ts
@@ -0,0 +1,52 @@
+import { NextRequest } from "next/server";
+import { describe, expect, it, vi } from "vitest";
+
+vi.mock("@/utils/releases", () => ({ getGitHubReleases: vi.fn() }));
+
+import { GET } from "@/app/(site)/download/[platform]/route";
+import { getGitHubReleases } from "@/utils/releases";
+
+const request = new NextRequest("https://cap.so/download/apple-silicon");
+
+describe("desktop download route", () => {
+ it("handles missing route parameters without throwing", async () => {
+ const response = await GET(request, {
+ params: Promise.resolve({} as { platform: string }),
+ });
+ expect(response.headers.get("location")).toBe("https://cap.so/download");
+ });
+
+ it("redirects to the resolved download and cancels its probe body", async () => {
+ const cancel = vi.fn();
+ vi.stubGlobal(
+ "fetch",
+ vi
+ .fn()
+ .mockResolvedValue({
+ status: 206,
+ url: "https://downloads.example/cap.dmg",
+ body: { cancel },
+ }),
+ );
+ const response = await GET(request, {
+ params: Promise.resolve({ platform: "Apple-Silicon" }),
+ });
+ expect(response.headers.get("location")).toBe(
+ "https://downloads.example/cap.dmg",
+ );
+ expect(cancel).toHaveBeenCalledOnce();
+ vi.unstubAllGlobals();
+ });
+
+ it("uses the GitHub fallback when the download provider fails", async () => {
+ vi.stubGlobal("fetch", vi.fn().mockRejectedValue(new Error("Unavailable")));
+ vi.mocked(getGitHubReleases).mockResolvedValue([
+ { downloads: { "macos-arm64": "https://github.com/cap.dmg" } },
+ ] as Awaited<ReturnType<typeof getGitHubReleases>>);
+ const response = await GET(request, {
+ params: Promise.resolve({ platform: "apple-silicon" }),
+ });
+ expect(response.headers.get("location")).toBe("https://github.com/cap.dmg");
+ vi.unstubAllGlobals();
+ });
+});
diff --git a/apps/web/app/(site)/download/[platform]/route.ts b/apps/web/app/(site)/download/[platform]/route.ts
index c9f7886f7..409596c13 100644
--- a/apps/web/app/(site)/download/[platform]/route.ts
+++ b/apps/web/app/(site)/download/[platform]/route.ts
@@ -1,13 +1,14 @@
import { type NextRequest, NextResponse } from "next/server";
import { getGitHubReleases, type ReleaseDownloadKey } from "@/utils/releases";
-export const runtime = "edge";
+export const runtime = "nodejs";
async function checkCrabNebulaDownload(
url: string,
): Promise<{ ok: true; finalUrl: string } | { ok: false }> {
try {
const res = await fetch(url, {
+ signal: AbortSignal.timeout(10_000),
redirect: "follow",
cache: "no-store",
headers: {
@@ -15,6 +16,7 @@ async function checkCrabNebulaDownload(
},
});
+ await res.body?.cancel();
if (res.status >= 200 && res.status < 300) {
return { ok: true, finalUrl: res.url };
}
@@ -43,7 +45,10 @@ export async function GET(
props: { params: Promise<{ platform: string }> },
) {
const params = await props.params;
- const platform = params.platform.toLowerCase();
+ const platform = params.platform?.toLowerCase();
+ if (!platform) {
+ return NextResponse.redirect(new URL("/download", request.url));
+ }
const downloadUrls: Record<
string,