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
12 changes: 9 additions & 3 deletions apps/radio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,15 @@ playlists are resolved directly in the browser. Bandcamp's fresh, validated
`seep.eu.org`,
`proxy.cors.sh`, then `cors.zme.ink`; the browser range-probes the pool under one
deadline before selecting the highest-priority working relay. YouTube tries the
curated, release-tested providers `pipedapi.wireway.ch`, `yt.omada.cafe`,
`invidious.nikkosphere.com`, then `y.com.sb`, with per-provider and whole-pool
deadlines. See `BANDCAMP_RELAY_RESEARCH.md` and
curated providers `pipedapi.wireway.ch`, `yt.omada.cafe`,
`invidious.nikkosphere.com`, `yt.chocolatemoo53.com`, then `y.com.sb`, with a
five-second deadline per provider and a thirty-second deadline for the pool.
Chocolatemoo opts in to browser media redirects: the starting and final URLs
must pass public-URL/DNS validation and match its curated origins. Browser Fetch
controls the intermediate hops, which JavaScript cannot inspect through manual
redirects; this policy trusts the operator's routing. Successful verification
returns the final companion URL to the player. Other providers and all API
requests keep rejecting redirects. See `BANDCAMP_RELAY_RESEARCH.md` and
`YOUTUBE_PROVIDER_RESEARCH.md` for the release probes and caveats.

## Analytics
Expand Down
82 changes: 79 additions & 3 deletions apps/radio/src/lib/youtube/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ describe("curated YouTube client", () => {
id: "invidious-nikkosphere",
kind: "invidious",
},
{
baseUrl: "https://yt.chocolatemoo53.com",
id: "invidious-chocolatemoo",
kind: "invidious",
mediaRedirectOrigins: [
"https://yt.chocolatemoo53.com",
"https://yt-comp1.chocolatemoo53.com",
"https://yt-comp2.chocolatemoo53.com",
"https://yt-comp3.chocolatemoo53.com",
"https://yt-comp4.chocolatemoo53.com",
"https://yt-comp5.chocolatemoo53.com",
"https://yt-comp6.chocolatemoo53.com",
],
},
{
baseUrl: "https://y.com.sb",
id: "invidious-y-com-sb",
Expand All @@ -51,6 +65,7 @@ describe("curated YouTube client", () => {
{ id: "piped-wireway", kind: "piped" },
{ id: "invidious-omada", kind: "invidious" },
{ id: "invidious-nikkosphere", kind: "invidious" },
{ id: "invidious-chocolatemoo", kind: "invidious" },
{ id: "invidious-y-com-sb", kind: "invidious" },
]);
});
Expand All @@ -61,6 +76,67 @@ describe("curated YouTube client", () => {
);
});

test("falls back to Chocolatemoo and returns its verified companion URL", async () => {
const hosts: string[] = [];
const companionUrl = "https://yt-comp4.chocolatemoo53.com/videoplayback";
const fetchImpl = mock((input: RequestInfo | URL, init?: RequestInit) => {
const url = new URL(String(input));
hosts.push(url.host);
if (url.host !== "yt.chocolatemoo53.com") {
return Promise.resolve(new Response("unavailable", { status: 503 }));
}
if (new Headers(init?.headers).has("Range")) {
if (init?.redirect !== "follow") {
return Promise.reject(new TypeError("Unexpected redirect"));
}
const response = new Response(new Uint8Array([1]), {
headers: {
"Content-Length": "65536",
"Content-Type": "audio/webm",
},
status: 206,
});
Object.defineProperty(response, "url", { value: companionUrl });
return Promise.resolve(response);
}
expect(init?.redirect).toBe("error");
return Promise.resolve(
Response.json({
adaptiveFormats: [
{
bitrate: "128000",
type: "audio/webm",
url: "/videoplayback",
},
],
author: "Artist",
lengthSeconds: 180,
liveNow: false,
title: "Track",
videoId: "abcdefghijk",
videoThumbnails: [],
})
);
});
const client = createCuratedYouTubeClient(CURATED_YOUTUBE_PROVIDERS, {
fetchImpl: fetchImpl as unknown as typeof fetch,
});

await expect(
client.resolveItem("https://youtube.com/watch?v=abcdefghijk")
).resolves.toMatchObject({
metadata: { streamUrl: companionUrl },
streamUrl: companionUrl,
});
expect(hosts).toEqual([
"pipedapi.wireway.ch",
"yt.omada.cafe",
"invidious.nikkosphere.com",
"yt.chocolatemoo53.com",
"yt.chocolatemoo53.com",
]);
});

test("bounds the complete failover operation", async () => {
const fetchImpl = mock((_input: RequestInfo | URL, init?: RequestInit) =>
pendingUntilAborted(init)
Expand All @@ -86,7 +162,7 @@ describe("curated YouTube client", () => {

test("gives every provider a turn during two-stage resolution", async () => {
const requests: string[] = [];
const providers = [1, 2, 3, 4].map((index) => ({
const providers = [1, 2, 3, 4, 5].map((index) => ({
baseUrl: `https://provider-${index}.example`,
id: `provider-${index}`,
kind: "piped" as const,
Expand Down Expand Up @@ -128,8 +204,8 @@ describe("curated YouTube client", () => {
await expect(client.resolveStream("abcdefghijk")).rejects.toThrow(
"Every configured YouTube provider failed"
);
expect(requests.filter((url) => url.includes("/streams/"))).toHaveLength(4);
expect(requests.filter((url) => url.includes("media-"))).toHaveLength(4);
expect(requests.filter((url) => url.includes("/streams/"))).toHaveLength(5);
expect(requests.filter((url) => url.includes("media-"))).toHaveLength(5);
});

test("rejects an invalid pool timeout", () => {
Expand Down
19 changes: 18 additions & 1 deletion apps/radio/src/lib/youtube/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type CuratedYouTubeProvider = {
baseUrl: string;
id: string;
kind: YouTubeProviderKind;
mediaRedirectOrigins?: readonly string[];
};

// See YOUTUBE_PROVIDER_RESEARCH.md.
Expand All @@ -32,6 +33,20 @@ export const CURATED_YOUTUBE_PROVIDERS: readonly CuratedYouTubeProvider[] = [
id: "invidious-nikkosphere",
kind: "invidious",
},
{
baseUrl: "https://yt.chocolatemoo53.com",
id: "invidious-chocolatemoo",
kind: "invidious",
mediaRedirectOrigins: [
"https://yt.chocolatemoo53.com",
"https://yt-comp1.chocolatemoo53.com",
"https://yt-comp2.chocolatemoo53.com",
"https://yt-comp3.chocolatemoo53.com",
"https://yt-comp4.chocolatemoo53.com",
"https://yt-comp5.chocolatemoo53.com",
"https://yt-comp6.chocolatemoo53.com",
],
},
{
baseUrl: "https://y.com.sb",
id: "invidious-y-com-sb",
Expand All @@ -54,7 +69,8 @@ type YouTubeClientOptions = Pick<
};

const DEFAULT_PROVIDER_TIMEOUT_MS = 5000;
const DEFAULT_OPERATION_TIMEOUT_MS = 25_000;
// Five providers get a full five-second attempt, plus time for pool overhead.
const DEFAULT_OPERATION_TIMEOUT_MS = 30_000;

type DeadlineSignal = {
cleanup: () => void;
Expand Down Expand Up @@ -197,6 +213,7 @@ export function createCuratedYouTubeClient(
...providerOptions,
baseUrl: provider.baseUrl,
id: provider.id,
mediaRedirectOrigins: provider.mediaRedirectOrigins,
timeoutMs,
};
const adapter =
Expand Down
7 changes: 5 additions & 2 deletions packages/platforms/src/youtube/invidious-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,11 @@ async function resolveVideoStream(
"No YouTube audio stream found"
);
}
const streamUrl = await resolveProviderUrl(context, stream.url, signal);
await verifyProviderMedia(context, streamUrl, signal);
const streamUrl = await verifyProviderMedia(
context,
await resolveProviderUrl(context, stream.url, signal),
signal
);
return { streamUrl, video };
}

Expand Down
7 changes: 5 additions & 2 deletions packages/platforms/src/youtube/piped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,11 @@ async function resolveVideoStream(
"No YouTube audio stream found"
);
}
const streamUrl = await resolveProviderUrl(context, stream.url, signal);
await verifyProviderMedia(context, streamUrl, signal);
const streamUrl = await verifyProviderMedia(
context,
await resolveProviderUrl(context, stream.url, signal),
signal
);
return { streamUrl, video };
}

Expand Down
Loading
Loading