Skip to content

Commit

Permalink
fix: handle situations where there is only one ad in VAST
Browse files Browse the repository at this point in the history
  • Loading branch information
Lunkers committed Feb 5, 2025
1 parent 4826cb1 commit 7e42766
Showing 1 changed file with 58 additions and 22 deletions.
80 changes: 58 additions & 22 deletions src/vast/vastApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,29 @@ const getVastXml = async (
const getCreatives = async (vastXml: any): Promise<ManifestAsset[]> => {

Check warning on line 254 in src/vast/vastApi.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
try {
if (vastXml.VAST.Ad) {
const creatives = vastXml.VAST.Ad.reduce(
(acc: ManifestAsset[], ad: any) => {
const adId = ad.InLine.Creatives.Creative.UniversalAdId[
if (!Array.isArray(vastXml.VAST.Ad)) {
const adId = vastXml.VAST.Ad.InLine.Creatives.Creative.UniversalAdId[
'#text'
].replace(/[^a-zA-Z0-9]/g, '');
const mediaFile =
vastXml.VAST.Ad.InLine.Creatives.Creative.Linear.MediaFiles.MediaFile[
'#text'
].replace(/[^a-zA-Z0-9]/g, '');
const mediaFile =
ad.InLine.Creatives.Creative.Linear.MediaFiles.MediaFile['#text'];
return [...acc, { creativeId: adId, masterPlaylistUrl: mediaFile }];
},
[]
);
return creatives;
];
return [{ creativeId: adId, masterPlaylistUrl: mediaFile }];
} else {
const creatives = vastXml.VAST.Ad.reduce(
(acc: ManifestAsset[], ad: any) => {

Check warning on line 268 in src/vast/vastApi.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const adId = ad.InLine.Creatives.Creative.UniversalAdId[
'#text'
].replace(/[^a-zA-Z0-9]/g, '');
const mediaFile =
ad.InLine.Creatives.Creative.Linear.MediaFiles.MediaFile['#text'];
return [...acc, { creativeId: adId, masterPlaylistUrl: mediaFile }];
},
[]
);
return creatives;
}
}
return [];
} catch (error) {
Expand All @@ -279,20 +290,41 @@ const replaceMediaFiles = (vastXml: string, assets: ManifestAsset[]) => {
const parser = new XMLParser({ ignoreAttributes: false });
const parsedVAST = parser.parse(vastXml);
if (parsedVAST.VAST.Ad) {
parsedVAST.VAST.Ad = parsedVAST.VAST.Ad.reduce((acc: any[], ad: any) => {
const adId = ad.InLine.Creatives.Creative.UniversalAdId[
'#text'
].replace(/[^a-zA-Z0-9]/g, '');
if (!Array.isArray(parsedVAST.VAST.Ad)) {
const adId = reFormatAdId(
parsedVAST.VAST.Ad.InLine.Creatives.Creative.UniversalAdId['#text']
);
const asset = assets.find((asset) => asset.creativeId === adId);
if (asset) {
ad.InLine.Creatives.Creative.Linear.MediaFiles.MediaFile['#text'] =
asset.masterPlaylistUrl;
ad.InLine.Creatives.Creative.Linear.MediaFiles.MediaFile['@_type'] =
'application/x-mpegURL';
acc.push(ad);
logger.debug('Replacing media file', { asset });
parsedVAST.VAST.Ad.InLine.Creatives.Creative.Linear.MediaFiles.MediaFile[
'#text'
] = asset.masterPlaylistUrl;
parsedVAST.VAST.Ad.InLine.Creatives.Creative.Linear.MediaFiles.MediaFile[
'@_type'
] = 'application/x-mpegURL';
}
return acc;
}, []);
} else {
parsedVAST.VAST.Ad = parsedVAST.VAST.Ad.reduce(
(acc: any[], ad: any) => {

Check warning on line 309 in src/vast/vastApi.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const adId = reFormatAdId(
ad.InLine.Creatives.Creative.UniversalAdId['#text']
);
const asset = assets.find((asset) => asset.creativeId === adId);
if (asset) {
ad.InLine.Creatives.Creative.Linear.MediaFiles.MediaFile[
'#text'
] = asset.masterPlaylistUrl;
ad.InLine.Creatives.Creative.Linear.MediaFiles.MediaFile[
'@_type'
] = 'application/x-mpegURL';
acc.push(ad);
}
return acc;
},
[]
);
}
}
const builder = new XMLBuilder({ format: true, ignoreAttributes: false });
const modifiedVastXml = builder.build(parsedVAST);
Expand All @@ -303,6 +335,10 @@ const replaceMediaFiles = (vastXml: string, assets: ManifestAsset[]) => {
}
};

const reFormatAdId = (adId: string) => {
return adId.replace(/[^a-zA-Z0-9]/g, '');
};

const parseVast = (vastXml: string) => {
try {
const parser = new XMLParser({ ignoreAttributes: false });
Expand Down

0 comments on commit 7e42766

Please sign in to comment.