diff --git a/packages/stitcher/src/filters.ts b/packages/stitcher/src/filters.ts index bff372a..02c830b 100644 --- a/packages/stitcher/src/filters.ts +++ b/packages/stitcher/src/filters.ts @@ -78,7 +78,7 @@ export function filterMasterPlaylist(master: MasterPlaylist, filter: Filter) { } } -export function parseFilterQuery(query: Record) { +export function getFilterFromQuery(query: Record) { const filter: Filter = {}; if ("filter.resolution" in query) { filter.resolution = query["filter.resolution"]; @@ -89,9 +89,12 @@ export function parseFilterQuery(query: Record) { return filter; } -export function filterQuery(filter?: Filter) { - return { - "filter.resolution": filter?.resolution, - "filter.audioLanguage": filter?.audioLanguage, - }; +export function getQueryParamsFromFilter(filter: Filter) { + const queryParams: Record = {}; + + Object.entries(filter).forEach(([key, value]) => { + queryParams[`filter.${key}`] = value; + }); + + return queryParams; } diff --git a/packages/stitcher/src/interstitials.ts b/packages/stitcher/src/interstitials.ts index bac54e2..98cdb3c 100644 --- a/packages/stitcher/src/interstitials.ts +++ b/packages/stitcher/src/interstitials.ts @@ -1,6 +1,6 @@ import { assert } from "shared/assert"; import { Group } from "./lib/group"; -import { buildProxyUrl, resolveUri } from "./lib/url"; +import { makeUrl, resolveUri } from "./lib/url"; import { fetchDuration } from "./playlist"; import { getAdMediasFromAdBreak } from "./vast"; import { parseVmap } from "./vmap"; @@ -44,12 +44,9 @@ export function getStaticDateRanges(startTime: DateTime, session: Session) { group.forEach((timeOffset, types) => { const startDate = startTime.plus({ seconds: timeOffset }); - const assetListUrl = buildProxyUrl( - `session/${session.id}/asset-list.json`, - { - startDate: startDate.toISO(), - }, - ); + const assetListUrl = makeUrl(`session/${session.id}/asset-list.json`, { + startDate: startDate.toISO(), + }); const clientAttributes: Record = { RESTRICT: "SKIP,JUMP", diff --git a/packages/stitcher/src/lib/url.ts b/packages/stitcher/src/lib/url.ts index 5d797f6..3ce71b5 100644 --- a/packages/stitcher/src/lib/url.ts +++ b/packages/stitcher/src/lib/url.ts @@ -1,9 +1,5 @@ import * as path from "path"; -import { encrypt } from "./crypto"; import { env } from "../env"; -import { filterQuery } from "../filters"; -import type { Filter } from "../filters"; -import type { Session } from "../session"; const uuidRegex = /^[a-z,0-9,-]{36,36}$/; @@ -63,33 +59,9 @@ export function joinUrl(urlFile: string, filePath: string) { return `${url.protocol}//${url.host}${path.join(url.pathname, filePath)}`; } -export function buildProxyUrl( +export function makeUrl( path: string, params: Record = {}, ) { return buildUrl(`${env.PUBLIC_STITCHER_ENDPOINT}/${path}`, params); } - -export function buildProxyMasterUrl(params: { - url: string; - session?: Session; - filter?: Filter; -}) { - return buildProxyUrl("out/master.m3u8", { - eurl: encrypt(params.url), - sid: params.session?.id, - ...filterQuery(params.filter), - }); -} - -export function buildProxyMediaUrl(params: { - type: string; - url: string; - session?: Session; -}) { - return buildProxyUrl("out/playlist.m3u8", { - type: params.type, - eurl: encrypt(params.url), - sid: params.session?.id, - }); -} diff --git a/packages/stitcher/src/playlist.ts b/packages/stitcher/src/playlist.ts index 56a41f8..a7ed4d0 100644 --- a/packages/stitcher/src/playlist.ts +++ b/packages/stitcher/src/playlist.ts @@ -1,8 +1,9 @@ import { DateTime } from "luxon"; import { assert } from "shared/assert"; -import { filterMasterPlaylist } from "./filters"; +import { filterMasterPlaylist, getQueryParamsFromFilter } from "./filters"; import { getAssets, getStaticDateRanges } from "./interstitials"; -import { buildProxyMediaUrl, joinUrl, resolveUri } from "./lib/url"; +import { encrypt } from "./lib/crypto"; +import { joinUrl, makeUrl, resolveUri } from "./lib/url"; import { groupRenditions, parseMasterPlaylist, @@ -26,7 +27,7 @@ export async function formatMasterPlaylist( for (const variant of master.variants) { const url = joinUrl(masterUrl, variant.uri); - variant.uri = buildProxyMediaUrl({ + variant.uri = makeMediaUrl({ url, session: options.session, type: "VIDEO", @@ -36,7 +37,7 @@ export async function formatMasterPlaylist( const renditions = groupRenditions(master.variants); renditions.forEach((rendition) => { const url = joinUrl(masterUrl, rendition.uri); - rendition.uri = buildProxyMediaUrl({ + rendition.uri = makeMediaUrl({ url, session: options.session, type: rendition.type, @@ -110,3 +111,27 @@ export async function fetchDuration(uri: string) { return acc; }, 0); } + +export function makeMasterUrl(params: { + url: string; + filter: Filter; + session?: Session; +}) { + return makeUrl("out/master.m3u8", { + eurl: encrypt(params.url), + sid: params.session?.id, + ...getQueryParamsFromFilter(params.filter), + }); +} + +function makeMediaUrl(params: { + type: string; + url: string; + session?: Session; +}) { + return makeUrl("out/playlist.m3u8", { + type: params.type, + eurl: encrypt(params.url), + sid: params.session?.id, + }); +} diff --git a/packages/stitcher/src/routes/session.ts b/packages/stitcher/src/routes/session.ts index af62105..9d71a8b 100644 --- a/packages/stitcher/src/routes/session.ts +++ b/packages/stitcher/src/routes/session.ts @@ -1,11 +1,11 @@ import { Elysia, t } from "elysia"; -import { parseFilterQuery } from "../filters"; +import { getFilterFromQuery } from "../filters"; import { decrypt } from "../lib/crypto"; -import { buildProxyMasterUrl } from "../lib/url"; import { formatAssetList, formatMasterPlaylist, formatMediaPlaylist, + makeMasterUrl, } from "../playlist"; import { createSession, @@ -19,10 +19,11 @@ export const session = new Elysia() async ({ body }) => { const session = await createSession(body); - const url = buildProxyMasterUrl({ + const filter = body.filter ?? {}; + const url = makeMasterUrl({ url: session.url, session, - filter: body.filter, + filter, }); return { url }; @@ -93,7 +94,7 @@ export const session = new Elysia() await processSessionOnMasterReq(session); - const filter = parseFilterQuery(query); + const filter = getFilterFromQuery(query); const url = decrypt(query.eurl);