Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Handle probe failures and same-origin MP4 fallback

Richie McIlroy20534b81f6ab
diff --git a/apps/web/__tests__/unit/playback-source.test.ts b/apps/web/__tests__/unit/playback-source.test.ts
index 863db2e19..2191ae1a6 100644
--- a/apps/web/__tests__/unit/playback-source.test.ts
+++ b/apps/web/__tests__/unit/playback-source.test.ts
@@ -235,7 +235,34 @@ describe("resolvePlaybackSource", () => {
expect(result).toBeNull();
});
- it("falls back after MP4 network errors and returns null when no source works", async () => {
+ it("uses a same-origin MP4 source when the probe is blocked after redirect", async () => {
+ const fetchImpl = vi
+ .fn<typeof fetch>()
+ .mockRejectedValueOnce(new TypeError("Failed to fetch"));
+
+ const result = await resolvePlaybackSource({
+ videoSrc: "/api/playlist?videoType=mp4",
+ rawFallbackSrc: "/api/playlist?videoType=raw-preview",
+ enableCrossOrigin: true,
+ fetchImpl,
+ now: () => 350,
+ });
+
+ expect(fetchImpl).toHaveBeenCalledTimes(1);
+ expect(fetchImpl).toHaveBeenCalledWith(
+ "/api/playlist?videoType=mp4&_t=350",
+ {
+ headers: { range: "bytes=0-0" },
+ },
+ );
+ expect(result).toEqual({
+ url: "/api/playlist?videoType=mp4&_t=350",
+ type: "mp4",
+ supportsCrossOrigin: false,
+ });
+ });
+
+ it("falls back after absolute MP4 network errors and returns null when no source works", async () => {
const fetchImpl = vi
.fn<typeof fetch>()
.mockRejectedValueOnce(new Error("network"))
@@ -246,7 +273,7 @@ describe("resolvePlaybackSource", () => {
);
const result = await resolvePlaybackSource({
- videoSrc: "/api/playlist?videoType=mp4",
+ videoSrc: "https://cap.so/api/playlist?videoType=mp4",
rawFallbackSrc: "/api/playlist?videoType=raw-preview",
fetchImpl,
now: () => 400,
diff --git a/apps/web/app/s/[videoId]/_components/playback-source.ts b/apps/web/app/s/[videoId]/_components/playback-source.ts
index 3499195b9..d02c1bb61 100644
--- a/apps/web/app/s/[videoId]/_components/playback-source.ts
+++ b/apps/web/app/s/[videoId]/_components/playback-source.ts
@@ -11,6 +11,13 @@ type ProbeResult = {
response: Response;
};
+type ProbeFailure = {
+ url: string;
+ reason: "network-error" | "unavailable";
+};
+
+type ProbeOutcome = ProbeFailure | ProbeResult;
+
type ResolvePlaybackSourceInput = {
videoSrc: string;
rawFallbackSrc?: string;
@@ -31,6 +38,14 @@ function isPlayableProbeResponse(response: Response): boolean {
return response.ok || response.status === 206;
}
+function isProbeResult(outcome: ProbeOutcome): outcome is ProbeResult {
+ return "response" in outcome;
+}
+
+function canUseUnprobedSource(url: string): boolean {
+ return url.startsWith("/") && !url.startsWith("//");
+}
+
function isWebMContentType(contentType: string, url: string): boolean {
return (
contentType.toLowerCase().includes("video/webm") ||
@@ -42,7 +57,7 @@ async function probePlaybackSource(
url: string,
fetchImpl: typeof fetch,
now: () => number,
-): Promise<ProbeResult | null> {
+): Promise<ProbeOutcome> {
const requestUrl = appendCacheBust(url, now());
try {
@@ -51,7 +66,10 @@ async function probePlaybackSource(
});
if (!isPlayableProbeResponse(response)) {
- return null;
+ return {
+ url: requestUrl,
+ reason: "unavailable",
+ };
}
return {
@@ -59,7 +77,10 @@ async function probePlaybackSource(
response,
};
} catch {
- return null;
+ return {
+ url: requestUrl,
+ reason: "network-error",
+ };
}
}
@@ -123,7 +144,7 @@ export async function resolvePlaybackSource({
const rawResult = await probePlaybackSource(rawFallbackSrc, fetchImpl, now);
- if (!rawResult) {
+ if (!isProbeResult(rawResult)) {
return null;
}
@@ -150,7 +171,7 @@ export async function resolvePlaybackSource({
const mp4Result = await probePlaybackSource(videoSrc, fetchImpl, now);
- if (mp4Result) {
+ if (isProbeResult(mp4Result)) {
return {
url: mp4Result.url,
type: "mp4",
@@ -160,5 +181,13 @@ export async function resolvePlaybackSource({
};
}
+ if (mp4Result.reason === "network-error" && canUseUnprobedSource(videoSrc)) {
+ return {
+ url: mp4Result.url,
+ type: "mp4",
+ supportsCrossOrigin: false,
+ };
+ }
+
return await resolveRaw();
}