Skip to content

Commit

Permalink
fix: Store vmap text instead of full response object
Browse files Browse the repository at this point in the history
  • Loading branch information
matvp91 committed Nov 15, 2024
1 parent a07771e commit 34246e1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
16 changes: 7 additions & 9 deletions packages/stitcher/src/interstitials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { env } from "./env";
import { resolveUri, toAssetProtocol } from "./lib/url";
import { fetchMasterPlaylistDuration } from "./playlist";
import { getAdMediasFromAdBreak } from "./vast";
import { parseVmap } from "./vmap";
import type { DateRange } from "./parser";
import type { Session } from "./session";
import type { VmapResponse } from "./vmap";
Expand All @@ -28,7 +29,8 @@ export function getStaticDateRanges(session: Session) {
const group: Record<string, InterstitialType[]> = {};

if (session.vmapResponse) {
for (const adBreak of session.vmapResponse.adBreaks) {
const vmap = parseVmap(session.vmapResponse);
for (const adBreak of vmap.adBreaks) {
const dateTime = session.startTime.plus({ seconds: adBreak.timeOffset });
groupTimeOffset(group, dateTime, "ad");
}
Expand Down Expand Up @@ -88,12 +90,8 @@ export async function getAssets(session: Session, lookupDate: DateTime) {
const assets: InterstitialAsset[] = [];

if (session.vmapResponse) {
await formatStaticAdBreaks(
assets,
session.vmapResponse,
session.startTime,
lookupDate,
);
const vmap = parseVmap(session.vmapResponse);
await formatStaticAdBreaks(assets, vmap, session.startTime, lookupDate);
}

if (session.interstitials) {
Expand All @@ -110,11 +108,11 @@ export async function getAssets(session: Session, lookupDate: DateTime) {

async function formatStaticAdBreaks(
assets: InterstitialAsset[],
vmapResponse: VmapResponse,
vmap: VmapResponse,
baseDate: DateTime,
lookupDate: DateTime,
) {
const adBreak = vmapResponse.adBreaks.find((adBreak) =>
const adBreak = vmap.adBreaks.find((adBreak) =>
isEqualTimeOffset(baseDate, adBreak.timeOffset, lookupDate),
);

Expand Down
3 changes: 1 addition & 2 deletions packages/stitcher/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { kv } from "./kv";
import { resolveUri } from "./lib/url";
import { fetchVmap } from "./vmap";
import type { Interstitial, InterstitialType } from "./interstitials";
import type { VmapResponse } from "./vmap";

export interface Session {
id: string;
Expand All @@ -14,7 +13,7 @@ export interface Session {
vmap?: {
url: string;
};
vmapResponse?: VmapResponse;
vmapResponse?: string;
interstitials?: Interstitial[];
}

Expand Down
11 changes: 5 additions & 6 deletions packages/stitcher/src/vmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export interface VmapResponse {
adBreaks: VmapAdBreak[];
}

export async function fetchVmap(url: string): Promise<VmapResponse> {
const doc = await getXml(url);
export function parseVmap(text: string): VmapResponse {
const parser = new DOMParser();
const doc = parser.parseFromString(text, "text/xml");
const rootElement = doc.documentElement;

if (rootElement.localName !== "VMAP") {
Expand Down Expand Up @@ -103,17 +104,15 @@ function getVastData(element: Element) {
return xmlSerializer.serializeToString(vastAdData.firstChild);
}

async function getXml(url: string) {
export async function fetchVmap(url: string) {
const response = await fetch(url, {
headers: {
"User-Agent": USER_AGENT,
},
});

const text = await response.text();
const parser = new DOMParser();

return parser.parseFromString(text, "text/xml");
return text;
}

function childList(node: Element) {
Expand Down

0 comments on commit 34246e1

Please sign in to comment.