UNPKG

videojs-vast-vpaid

Version:
186 lines (138 loc) 5.05 kB
const VPAIDFLASHClient = require('vpaid-flash-client/js/VPAIDFLASHClient'); const utilities = require('../../utils/utilityFunctions'); const VPAIDHTML5Tech = require('../vpaid/VPAIDHTML5Tech'); const VPAIDFlashTech = require('../vpaid/VPAIDFlashTech'); const vastUtil = { track: function track (URLMacros, variables) { const sources = vastUtil.parseURLMacros(URLMacros, variables); const trackImgs = []; sources.forEach((src) => { const img = new Image(); img.src = src; trackImgs.push(img); }); return trackImgs; }, parseURLMacros: function parseMacros (URLMacros, variables) { const parsedURLs = []; variables = variables || {}; if (!variables.CACHEBUSTING) { variables.CACHEBUSTING = Math.round(Math.random() * 1.0e+10); } URLMacros.forEach((URLMacro) => { parsedURLs.push(vastUtil._parseURLMacro(URLMacro, variables)); }); return parsedURLs; }, parseURLMacro: function parseMacro (URLMacro, variables) { variables = variables || {}; if (!variables.CACHEBUSTING) { variables.CACHEBUSTING = Math.round(Math.random() * 1.0e+10); } return vastUtil._parseURLMacro(URLMacro, variables); }, _parseURLMacro: function parseMacro (URLMacro, variables) { variables = variables || {}; utilities.forEach(variables, (value, key) => { //eslint-disable-next-line URLMacro = URLMacro.replace(new RegExp('\\[' + key + '\\\]', 'gm'), value); }); return URLMacro; }, parseDuration: function parseDuration (durationStr) { const durationRegex = /(\d\d):(\d\d):(\d\d)(\.(\d\d\d))?/; let match, durationInMs; if (utilities.isString(durationStr)) { match = durationStr.match(durationRegex); if (match) { durationInMs = parseHoursToMs(match[1]) + parseMinToMs(match[2]) + parseSecToMs(match[3]) + parseInt(match[5] || 0, 10); } } return isNaN(durationInMs) ? null : durationInMs; /** * local functions ***/ function parseHoursToMs (hourStr) { return parseInt(hourStr, 10) * 60 * 60 * 1000; } function parseMinToMs (minStr) { return parseInt(minStr, 10) * 60 * 1000; } function parseSecToMs (secStr) { return parseInt(secStr, 10) * 1000; } }, parseImpressions: function parseImpressions (impressions) { if (impressions) { impressions = utilities.isArray(impressions) ? impressions : [impressions]; return utilities.transformArray(impressions, (impression) => { if (utilities.isNotEmptyString(impression.keyValue)) { return impression.keyValue; } return undefined; }); } return []; }, // We assume that the progress is going to arrive in milliseconds formatProgress: function formatProgress (progress) { let hours, minutes, seconds, milliseconds; hours = progress / (60 * 60 * 1000); hours = Math.floor(hours); minutes = progress / (60 * 1000) % 60; minutes = Math.floor(minutes); seconds = progress / 1000 % 60; seconds = Math.floor(seconds); milliseconds = progress % 1000; return utilities.toFixedDigits(hours, 2) + ':' + utilities.toFixedDigits(minutes, 2) + ':' + utilities.toFixedDigits(seconds, 2) + '.' + utilities.toFixedDigits(milliseconds, 3); }, parseOffset: function parseOffset (offset, duration) { if (isPercentage(offset)) { return calculatePercentage(offset, duration); } return vastUtil.parseDuration(offset); /** * Local function ***/ function isPercentage (offset) { const percentageRegex = /^\d+(\.\d+)?%$/g; return percentageRegex.test(offset); } function calculatePercentage (percentStr, duration) { if (duration) { return calcPercent(duration, parseFloat(percentStr.replace('%', ''))); } return null; } function calcPercent (quantity, percent) { return quantity * percent / 100; } }, // List of supported VPAID technologies VPAID_techs: [ VPAIDFlashTech, VPAIDHTML5Tech ], isVPAID: function isVPAIDMediaFile (mediaFile) { return Boolean(mediaFile) && mediaFile.apiFramework === 'VPAID'; }, findSupportedVPAIDTech: function findSupportedVPAIDTech (mimeType) { let i, len, VPAIDTech; for (i = 0, len = this.VPAID_techs.length; i < len; i += 1) { VPAIDTech = this.VPAID_techs[i]; if (VPAIDTech.supports(mimeType)) { return VPAIDTech; } } return null; }, isFlashSupported: function isFlashSupported () { return VPAIDFLASHClient.isSupported(); }, /** * Necessary step for VPAIDFLAShClient to know if flash is supported and not blocked. * IMPORTANT NOTE: This is an async test and needs to be run as soon as possible. * * @param vpaidFlashLoaderPath the path to the vpaidFlashLoader swf obj. */ runFlashSupportCheck: function runFlashSupportCheck (vpaidFlashLoaderPath) { VPAIDFLASHClient.runFlashTest({data: vpaidFlashLoaderPath}); } }; module.exports = vastUtil;