Files
v10/dist/dev/media/utils/cdn.js
T

56 lines
2.1 KiB
JavaScript

//#region src/media/utils/cdn.ts
/**
* Default {@link GetCdnId}: the URL's origin (scheme + host + port); falls back
* to the raw string when the URL can't be parsed, so the return value is always
* a stable grouping key.
*/
function getCdnId(url) {
try {
return new URL(url).origin;
} catch {
return url;
}
}
const CDN_TYPE_PRIORITY = {
video: 0,
audio: 1,
text: 2
};
/**
* The distinct CDNs a presentation's tracks are served from, ordered video CDNs
* first, then audio, then text (manifest order within a type). The head is the
* primary CDN — the one a sticky pick defaults to — and is always video-derived
* when the source has video. Returns `[]` for an unresolved presentation with
* no tracks.
*
* Redundant-stream sources list the same content on multiple hosts (e.g. Mux's
* `?redundant_streams=true`), so each host contributes its own candidate tracks;
* this collapses them to the set of CDNs across every track type. The CDN-id
* derivation defaults to {@link getCdnId}; pass a consumer-configured `getId` to
* key on something other than origin.
*/
function getOrderedCdnIds(presentation, getId = getCdnId) {
const seen = /* @__PURE__ */ new Set();
const ids = [];
const selectionSets = [...presentation.selectionSets ?? []].sort((a, b) => CDN_TYPE_PRIORITY[a.type] - CDN_TYPE_PRIORITY[b.type]);
for (const selectionSet of selectionSets) for (const switchingSet of selectionSet.switchingSets) for (const track of switchingSet.tracks) {
const id = getId(track.url);
if (seen.has(id)) continue;
seen.add(id);
ids.push(id);
}
return ids;
}
/**
* Add a CDN id to a failed-CDN list, preserving order and ignoring duplicates.
* Idempotent: re-adding an already-present id returns the same array reference
* (so a no-op trip doesn't churn the `failedCdns` signal). The failover trip in
* `resolve-track` and the segment loaders feed this into `failedCdns` via `update`.
*/
function addFailedCdn(failed, cdn) {
return failed?.includes(cdn) ? failed : [...failed ?? [], cdn];
}
//#endregion
export { addFailedCdn, getCdnId, getOrderedCdnIds };
//# sourceMappingURL=cdn.js.map