react-native-vast-client
Version:
JavaScript VAST Client
2 lines (1 loc) • 35.8 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var events=require("events"),classCallCheck=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},createClass=function(){function e(e,t){for(var r=0;r<t.length;r++){var i=t[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,r,i){return r&&e(t.prototype,r),i&&e(t,i),t}}(),inherits=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)},possibleConstructorReturn=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t},Ad=function e(){classCallCheck(this,e),this.id=null,this.sequence=null,this.system=null,this.title=null,this.description=null,this.advertiser=null,this.pricing=null,this.survey=null,this.errorURLTemplates=[],this.impressionURLTemplates=[],this.creatives=[],this.extensions=[]},AdExtension=function e(){classCallCheck(this,e),this.attributes={},this.children=[]},AdExtensionChild=function e(){classCallCheck(this,e),this.name=null,this.value=null,this.attributes={}},CompanionAd=function e(){classCallCheck(this,e),this.id=null,this.width=0,this.height=0,this.type=null,this.staticResource=null,this.htmlResource=null,this.iframeResource=null,this.altText=null,this.companionClickThroughURLTemplate=null,this.companionClickTrackingURLTemplates=[],this.trackingEvents={}},Creative=function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};classCallCheck(this,e),this.id=t.id||null,this.adId=t.adId||null,this.sequence=t.sequence||null,this.apiFramework=t.apiFramework||null,this.trackingEvents={}},CreativeCompanion=function(e){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};classCallCheck(this,t);var r=possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.type="companion",r.variations=[],r}return inherits(t,Creative),t}();function track(e,t,r){resolveURLTemplates(e,t,r).forEach(function(e){"undefined"!=typeof window&&null!==window&&((new Image).src=e)})}function resolveURLTemplates(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=[];for(var a in t.ASSETURI&&(t.ASSETURI=encodeURIComponentRFC3986(t.ASSETURI)),t.CONTENTPLAYHEAD&&(t.CONTENTPLAYHEAD=encodeURIComponentRFC3986(t.CONTENTPLAYHEAD)),!t.ERRORCODE||r.isCustomCode||/^[0-9]{3}$/.test(t.ERRORCODE)||(t.ERRORCODE=900),t.CACHEBUSTING=leftpad(Math.round(1e8*Math.random()).toString()),t.TIMESTAMP=encodeURIComponentRFC3986((new Date).toISOString()),t.RANDOM=t.random=t.CACHEBUSTING,e){var n=e[a];if("string"==typeof n){for(var s in t){var o=t[s],l="["+s+"]",c="%%"+s+"%%";n=(n=n.replace(l,o)).replace(c,o)}i.push(n)}}return i}function encodeURIComponentRFC3986(e){return encodeURIComponent(e).replace(/[!'()*]/g,function(e){return"%"+e.charCodeAt(0).toString(16)})}function leftpad(e){return e.length<8?range(0,8-e.length,!1).map(function(){return"0"}).join("")+e:e}function range(e,t,r){for(var i=[],a=e<t,n=r?a?t+1:t-1:t,s=e;a?s<n:s>n;a?s++:s--)i.push(s);return i}function isNumeric(e){return!isNaN(parseFloat(e))&&isFinite(e)}function flatten(e){return e.reduce(function(e,t){return e.concat(Array.isArray(t)?flatten(t):t)},[])}var util={track:track,resolveURLTemplates:resolveURLTemplates,encodeURIComponentRFC3986:encodeURIComponentRFC3986,leftpad:leftpad,range:range,isNumeric:isNumeric,flatten:flatten};function childByName(e,t){var r=e.childNodes;for(var i in r){var a=r[i];if(a.nodeName===t)return a}}function childrenByName(e,t){var r=[],i=e.childNodes;for(var a in i){var n=i[a];n.nodeName===t&&r.push(n)}return r}function resolveVastAdTagURI(e,t){return t?0===e.indexOf("//")?""+location.protocol+e:-1===e.indexOf("://")?t.slice(0,t.lastIndexOf("/"))+"/"+e:e:e}function parseBoolean(e){return-1!==["true","TRUE","1"].indexOf(e)}function parseNodeText(e){return e&&(e.textContent||e.text||"").trim()}function copyNodeAttribute(e,t,r){var i=t.getAttribute(e);i&&r.setAttribute(e,i)}function parseDuration(e){if(null===e||void 0===e)return-1;if(util.isNumeric(e))return parseInt(e);var t=e.split(":");if(3!==t.length)return-1;var r=t[2].split("."),i=parseInt(r[0]);2===r.length&&(i+=parseFloat("0."+r[1]));var a=parseInt(60*t[1]),n=parseInt(60*t[0]*60);return isNaN(n)||isNaN(a)||isNaN(i)||a>3600||i>60?-1:n+a+i}function splitVAST(e){var t=[],r=null;return e.forEach(function(i,a){if(i.sequence&&(i.sequence=parseInt(i.sequence,10)),i.sequence>1){var n=e[a-1];if(n&&n.sequence===i.sequence-1)return void(r&&r.push(i));delete i.sequence}r=[i],t.push(r)}),t}function mergeWrapperAdData(e,t){e.errorURLTemplates=t.errorURLTemplates.concat(e.errorURLTemplates),e.impressionURLTemplates=t.impressionURLTemplates.concat(e.impressionURLTemplates),e.extensions=t.extensions.concat(e.extensions),e.creatives.forEach(function(e){if(t.trackingEvents&&t.trackingEvents[e.type])for(var r in t.trackingEvents[e.type]){var i=t.trackingEvents[e.type][r];Array.isArray(e.trackingEvents[r])||(e.trackingEvents[r]=[]),e.trackingEvents[r]=e.trackingEvents[r].concat(i)}}),t.videoClickTrackingURLTemplates&&t.videoClickTrackingURLTemplates.length&&e.creatives.forEach(function(e){"linear"===e.type&&(e.videoClickTrackingURLTemplates=e.videoClickTrackingURLTemplates.concat(t.videoClickTrackingURLTemplates))}),t.videoCustomClickURLTemplates&&t.videoCustomClickURLTemplates.length&&e.creatives.forEach(function(e){"linear"===e.type&&(e.videoCustomClickURLTemplates=e.videoCustomClickURLTemplates.concat(t.videoCustomClickURLTemplates))}),t.videoClickThroughURLTemplate&&e.creatives.forEach(function(e){"linear"!==e.type||null!==e.videoClickThroughURLTemplate&&void 0!==e.videoClickThroughURLTemplate||(e.videoClickThroughURLTemplate=t.videoClickThroughURLTemplate)})}var parserUtils={childByName:childByName,childrenByName:childrenByName,resolveVastAdTagURI:resolveVastAdTagURI,parseBoolean:parseBoolean,parseNodeText:parseNodeText,copyNodeAttribute:copyNodeAttribute,parseDuration:parseDuration,splitVAST:splitVAST,mergeWrapperAdData:mergeWrapperAdData};function parseCreativeCompanion(e,t){var r=new CreativeCompanion(t);return parserUtils.childrenByName(e,"Companion").forEach(function(e){var t=new CompanionAd;t.id=e.getAttribute("id")||null,t.width=e.getAttribute("width"),t.height=e.getAttribute("height"),t.companionClickTrackingURLTemplates=[],parserUtils.childrenByName(e,"HTMLResource").forEach(function(e){t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"IFrameResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.iframeResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"StaticResource").forEach(function(r){t.type=r.getAttribute("creativeType")||0,parserUtils.childrenByName(e,"AltText").forEach(function(e){t.altText=parserUtils.parseNodeText(e)}),t.staticResource=parserUtils.parseNodeText(r)}),parserUtils.childrenByName(e,"TrackingEvents").forEach(function(e){parserUtils.childrenByName(e,"Tracking").forEach(function(e){var r=e.getAttribute("event"),i=parserUtils.parseNodeText(e);r&&i&&(Array.isArray(t.trackingEvents[r])||(t.trackingEvents[r]=[]),t.trackingEvents[r].push(i))})}),parserUtils.childrenByName(e,"CompanionClickTracking").forEach(function(e){t.companionClickTrackingURLTemplates.push(parserUtils.parseNodeText(e))}),t.companionClickThroughURLTemplate=parserUtils.parseNodeText(parserUtils.childByName(e,"CompanionClickThrough")),t.companionClickTrackingURLTemplate=parserUtils.parseNodeText(parserUtils.childByName(e,"CompanionClickTracking")),r.variations.push(t)}),r}var CreativeLinear=function(e){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};classCallCheck(this,t);var r=possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.type="linear",r.duration=0,r.skipDelay=null,r.mediaFiles=[],r.videoClickThroughURLTemplate=null,r.videoClickTrackingURLTemplates=[],r.videoCustomClickURLTemplates=[],r.adParameters=null,r.icons=[],r}return inherits(t,Creative),t}(),Icon=function e(){classCallCheck(this,e),this.program=null,this.height=0,this.width=0,this.xPosition=0,this.yPosition=0,this.apiFramework=null,this.offset=null,this.duration=0,this.type=null,this.staticResource=null,this.htmlResource=null,this.iframeResource=null,this.iconClickThroughURLTemplate=null,this.iconClickTrackingURLTemplates=[],this.iconViewTrackingURLTemplate=null},MediaFile=function e(){classCallCheck(this,e),this.id=null,this.fileURL=null,this.deliveryType="progressive",this.mimeType=null,this.codec=null,this.bitrate=0,this.minBitrate=0,this.maxBitrate=0,this.width=0,this.height=0,this.apiFramework=null,this.scalable=null,this.maintainAspectRatio=null};function parseCreativeLinear(e,t){var r=void 0,i=new CreativeLinear(t);i.duration=parserUtils.parseDuration(parserUtils.parseNodeText(parserUtils.childByName(e,"Duration")));var a=e.getAttribute("skipoffset");if(void 0===a||null===a)i.skipDelay=null;else if("%"===a.charAt(a.length-1)&&-1!==i.duration){var n=parseInt(a,10);i.skipDelay=i.duration*(n/100)}else i.skipDelay=parserUtils.parseDuration(a);var s=parserUtils.childByName(e,"VideoClicks");s&&(i.videoClickThroughURLTemplate=parserUtils.parseNodeText(parserUtils.childByName(s,"ClickThrough")),parserUtils.childrenByName(s,"ClickTracking").forEach(function(e){i.videoClickTrackingURLTemplates.push(parserUtils.parseNodeText(e))}),parserUtils.childrenByName(s,"CustomClick").forEach(function(e){i.videoCustomClickURLTemplates.push(parserUtils.parseNodeText(e))}));var o=parserUtils.childByName(e,"AdParameters");o&&(i.adParameters=parserUtils.parseNodeText(o)),parserUtils.childrenByName(e,"TrackingEvents").forEach(function(e){parserUtils.childrenByName(e,"Tracking").forEach(function(e){var t=e.getAttribute("event"),a=parserUtils.parseNodeText(e);if(t&&a){if("progress"===t){if(!(r=e.getAttribute("offset")))return;t="%"===r.charAt(r.length-1)?"progress-"+r:"progress-"+Math.round(parserUtils.parseDuration(r))}Array.isArray(i.trackingEvents[t])||(i.trackingEvents[t]=[]),i.trackingEvents[t].push(a)}})}),parserUtils.childrenByName(e,"MediaFiles").forEach(function(e){parserUtils.childrenByName(e,"MediaFile").forEach(function(e){var t=new MediaFile;t.id=e.getAttribute("id"),t.fileURL=parserUtils.parseNodeText(e),t.deliveryType=e.getAttribute("delivery"),t.codec=e.getAttribute("codec"),t.mimeType=e.getAttribute("type"),t.apiFramework=e.getAttribute("apiFramework"),t.bitrate=parseInt(e.getAttribute("bitrate")||0),t.minBitrate=parseInt(e.getAttribute("minBitrate")||0),t.maxBitrate=parseInt(e.getAttribute("maxBitrate")||0),t.width=parseInt(e.getAttribute("width")||0),t.height=parseInt(e.getAttribute("height")||0);var r=e.getAttribute("scalable");r&&"string"==typeof r&&("true"===(r=r.toLowerCase())?t.scalable=!0:"false"===r&&(t.scalable=!1));var a=e.getAttribute("maintainAspectRatio");a&&"string"==typeof a&&("true"===(a=a.toLowerCase())?t.maintainAspectRatio=!0:"false"===a&&(t.maintainAspectRatio=!1)),i.mediaFiles.push(t)})});var l=parserUtils.childByName(e,"Icons");return l&&parserUtils.childrenByName(l,"Icon").forEach(function(e){var t=new Icon;t.program=e.getAttribute("program"),t.height=parseInt(e.getAttribute("height")||0),t.width=parseInt(e.getAttribute("width")||0),t.xPosition=parseXPosition(e.getAttribute("xPosition")),t.yPosition=parseYPosition(e.getAttribute("yPosition")),t.apiFramework=e.getAttribute("apiFramework"),t.offset=parserUtils.parseDuration(e.getAttribute("offset")),t.duration=parserUtils.parseDuration(e.getAttribute("duration")),parserUtils.childrenByName(e,"HTMLResource").forEach(function(e){t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"IFrameResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.iframeResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"StaticResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.staticResource=parserUtils.parseNodeText(e)});var r=parserUtils.childByName(e,"IconClicks");r&&(t.iconClickThroughURLTemplate=parserUtils.parseNodeText(parserUtils.childByName(r,"IconClickThrough")),parserUtils.childrenByName(r,"IconClickTracking").forEach(function(e){t.iconClickTrackingURLTemplates.push(parserUtils.parseNodeText(e))})),t.iconViewTrackingURLTemplate=parserUtils.parseNodeText(parserUtils.childByName(e,"IconViewTracking")),i.icons.push(t)}),i}function parseXPosition(e){return-1!==["left","right"].indexOf(e)?e:parseInt(e||0)}function parseYPosition(e){return-1!==["top","bottom"].indexOf(e)?e:parseInt(e||0)}var CreativeNonLinear=function(e){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};classCallCheck(this,t);var r=possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.type="nonlinear",r.variations=[],r}return inherits(t,Creative),t}(),NonLinearAd=function e(){classCallCheck(this,e),this.id=null,this.width=0,this.height=0,this.expandedWidth=0,this.expandedHeight=0,this.scalable=!0,this.maintainAspectRatio=!0,this.minSuggestedDuration=0,this.apiFramework="static",this.type=null,this.staticResource=null,this.htmlResource=null,this.iframeResource=null,this.nonlinearClickThroughURLTemplate=null,this.nonlinearClickTrackingURLTemplates=[],this.adParameters=null};function parseCreativeNonLinear(e,t){var r=new CreativeNonLinear(t);return parserUtils.childrenByName(e,"TrackingEvents").forEach(function(e){var t=void 0,i=void 0;parserUtils.childrenByName(e,"Tracking").forEach(function(e){t=e.getAttribute("event"),i=parserUtils.parseNodeText(e),t&&i&&(Array.isArray(r.trackingEvents[t])||(r.trackingEvents[t]=[]),r.trackingEvents[t].push(i))})}),parserUtils.childrenByName(e,"NonLinear").forEach(function(e){var t=new NonLinearAd;t.id=e.getAttribute("id")||null,t.width=e.getAttribute("width"),t.height=e.getAttribute("height"),t.expandedWidth=e.getAttribute("expandedWidth"),t.expandedHeight=e.getAttribute("expandedHeight"),t.scalable=parserUtils.parseBoolean(e.getAttribute("scalable")),t.maintainAspectRatio=parserUtils.parseBoolean(e.getAttribute("maintainAspectRatio")),t.minSuggestedDuration=parserUtils.parseDuration(e.getAttribute("minSuggestedDuration")),t.apiFramework=e.getAttribute("apiFramework"),parserUtils.childrenByName(e,"HTMLResource").forEach(function(e){t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"IFrameResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.iframeResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"StaticResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.staticResource=parserUtils.parseNodeText(e)});var i=parserUtils.childByName(e,"AdParameters");i&&(t.adParameters=parserUtils.parseNodeText(i)),t.nonlinearClickThroughURLTemplate=parserUtils.parseNodeText(parserUtils.childByName(e,"NonLinearClickThrough")),parserUtils.childrenByName(e,"NonLinearClickTracking").forEach(function(e){t.nonlinearClickTrackingURLTemplates.push(parserUtils.parseNodeText(e))}),r.variations.push(t)}),r}function parseAd(e){var t=e.childNodes;for(var r in t){var i=t[r];if(-1!==["Wrapper","InLine"].indexOf(i.nodeName)){if(parserUtils.copyNodeAttribute("id",e,i),parserUtils.copyNodeAttribute("sequence",e,i),"Wrapper"===i.nodeName)return parseWrapper(i);if("InLine"===i.nodeName)return parseInLine(i)}}}function parseInLine(e){var t=e.childNodes,r=new Ad;for(var i in r.id=e.getAttribute("id")||null,r.sequence=e.getAttribute("sequence")||null,t){var a=t[i];switch(a.nodeName){case"Error":r.errorURLTemplates.push(parserUtils.parseNodeText(a));break;case"Impression":r.impressionURLTemplates.push(parserUtils.parseNodeText(a));break;case"Creatives":parserUtils.childrenByName(a,"Creative").forEach(function(e){var t={id:e.getAttribute("id")||null,adId:parseCreativeAdIdAttribute(e),sequence:e.getAttribute("sequence")||null,apiFramework:e.getAttribute("apiFramework")||null};for(var i in e.childNodes){var a=e.childNodes[i],n=void 0;switch(a.nodeName){case"Linear":(n=parseCreativeLinear(a,t))&&r.creatives.push(n);break;case"NonLinearAds":(n=parseCreativeNonLinear(a,t))&&r.creatives.push(n);break;case"CompanionAds":(n=parseCreativeCompanion(a,t))&&r.creatives.push(n)}}});break;case"Extensions":parseExtensions(r.extensions,parserUtils.childrenByName(a,"Extension"));break;case"AdSystem":r.system={value:parserUtils.parseNodeText(a),version:a.getAttribute("version")||null};break;case"AdTitle":r.title=parserUtils.parseNodeText(a);break;case"Description":r.description=parserUtils.parseNodeText(a);break;case"Advertiser":r.advertiser=parserUtils.parseNodeText(a);break;case"Pricing":r.pricing={value:parserUtils.parseNodeText(a),model:a.getAttribute("model")||null,currency:a.getAttribute("currency")||null};break;case"Survey":r.survey=parserUtils.parseNodeText(a)}}return r}function parseWrapper(e){var t=parseInLine(e),r=parserUtils.childByName(e,"VASTAdTagURI");if(r?t.nextWrapperURL=parserUtils.parseNodeText(r):(r=parserUtils.childByName(e,"VASTAdTagURL"))&&(t.nextWrapperURL=parserUtils.parseNodeText(parserUtils.childByName(r,"URL"))),t.creatives.forEach(function(e){if(-1!==["linear","nonlinear"].indexOf(e.type)){if(e.trackingEvents){t.trackingEvents||(t.trackingEvents={}),t.trackingEvents[e.type]||(t.trackingEvents[e.type]={});var r=function(r){var i=e.trackingEvents[r];Array.isArray(t.trackingEvents[e.type][r])||(t.trackingEvents[e.type][r]=[]),i.forEach(function(i){t.trackingEvents[e.type][r].push(i)})};for(var i in e.trackingEvents)r(i)}e.videoClickTrackingURLTemplates&&(Array.isArray(t.videoClickTrackingURLTemplates)||(t.videoClickTrackingURLTemplates=[]),e.videoClickTrackingURLTemplates.forEach(function(e){t.videoClickTrackingURLTemplates.push(e)})),e.videoClickThroughURLTemplate&&(t.videoClickThroughURLTemplate=e.videoClickThroughURLTemplate),e.videoCustomClickURLTemplates&&(Array.isArray(t.videoCustomClickURLTemplates)||(t.videoCustomClickURLTemplates=[]),e.videoCustomClickURLTemplates.forEach(function(e){t.videoCustomClickURLTemplates.push(e)}))}}),t.nextWrapperURL)return t}function parseExtensions(e,t){t.forEach(function(t){var r=new AdExtension,i=t.attributes,a=t.childNodes;if(t.attributes)for(var n in i){var s=i[n];s.nodeName&&s.nodeValue&&(r.attributes[s.nodeName]=s.nodeValue)}for(var o in a){var l=a[o],c=parserUtils.parseNodeText(l);if("#comment"!==l.nodeName&&""!==c){var u=new AdExtensionChild;if(u.name=l.nodeName,u.value=c,l.attributes){var p=l.attributes;for(var h in p){var d=p[h];u.attributes[d.nodeName]=d.nodeValue}}r.children.push(u)}}e.push(r)})}function parseCreativeAdIdAttribute(e){return e.getAttribute("AdID")||e.getAttribute("adID")||e.getAttribute("adId")||null}function xdr(){var e=void 0;return window.XDomainRequest&&(e=new XDomainRequest),e}function supported(){return!!xdr()}function get$1(e,t,r){var i="function"==typeof window.ActiveXObject?new window.ActiveXObject("Microsoft.XMLDOM"):void 0;if(!i)return r(new Error("FlashURLHandler: Microsoft.XMLDOM format not supported"));i.async=!1;var a=xdr();a.open("GET",e),a.timeout=t.timeout||0,a.withCredentials=t.withCredentials||!1,a.send(),a.onprogress=function(){},a.onload=function(){i.loadXML(a.responseText),r(null,i)}}var flashURLHandler={get:get$1,supported:supported},uri=require("url"),fs=require("fs"),http=require("http"),https=require("https"),DOMParser=require("xmldom").DOMParser;function get$2(e,t,r){var i="https:"===(e=uri.parse(e)).protocol?https:http;if("file:"===e.protocol)fs.readFile(e.pathname,"utf8",function(e,t){if(e)return r(e);var i=(new DOMParser).parseFromString(t);r(null,i)});else{var a=void 0,n="",s=i.get(e.href,function(e){e.on("data",function(e){n+=e,clearTimeout(a),a=setTimeout(o,t.timeout||12e4)}),e.on("end",function(){clearTimeout(a);var e=(new DOMParser).parseFromString(n);r(null,e)})});s.on("error",function(e){clearTimeout(a),r(e)});var o=function(e){return function(){return e.abort()}}(s);a=setTimeout(o,t.timeout||12e4)}}var nodeURLHandler={get:get$2};function xhr(){try{var e=new window.XMLHttpRequest;return"withCredentials"in e?e:null}catch(e){return null}}function supported$1(){return!!xhr()}function get$3(e,t,r){if("https:"===window.location.protocol&&0===e.indexOf("http://"))return r(new Error("XHRURLHandler: Cannot go from HTTPS to HTTP."));try{var i=xhr();i.open("GET",e),i.timeout=t.timeout||0,i.withCredentials=t.withCredentials||!1,i.overrideMimeType&&i.overrideMimeType("text/xml"),i.onreadystatechange=function(){4===i.readyState&&(200===i.status?r(null,i.responseXML):r(new Error("XHRURLHandler: "+i.statusText)))},i.send()}catch(e){r(new Error("XHRURLHandler: Unexpected error"))}}var XHRURLHandler={get:get$3,supported:supported$1};function get$4(e,t,r){return r||("function"==typeof t&&(r=t),t={}),"undefined"==typeof window||null===window?nodeURLHandler.get(e,t,r):XHRURLHandler.supported()?XHRURLHandler.get(e,t,r):flashURLHandler.supported()?flashURLHandler.get(e,t,r):r(new Error("Current context is not supported by any of the default URLHandlers. Please provide a custom URLHandler"))}var urlHandler={get:get$4},VASTResponse=function e(){classCallCheck(this,e),this.ads=[],this.errorURLTemplates=[],this.version=null},DEFAULT_MAX_WRAPPER_DEPTH=10,DEFAULT_EVENT_DATA={ERRORCODE:900,extensions:[]},VASTParser=function(e){function t(){classCallCheck(this,t);var e=possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).call(this));return e.remainingAds=[],e.parentURLs=[],e.errorURLTemplates=[],e.rootErrorURLTemplates=[],e.maxWrapperDepth=null,e.URLTemplateFilters=[],e.fetchingOptions={},e}return inherits(t,e),createClass(t,[{key:"addURLTemplateFilter",value:function(e){"function"==typeof e&&this.URLTemplateFilters.push(e)}},{key:"removeURLTemplateFilter",value:function(){this.URLTemplateFilters.pop()}},{key:"countURLTemplateFilters",value:function(){return this.URLTemplateFilters.length}},{key:"clearURLTemplateFilters",value:function(){this.URLTemplateFilters=[]}},{key:"trackVastError",value:function(e,t){for(var r=arguments.length,i=Array(r>2?r-2:0),a=2;a<r;a++)i[a-2]=arguments[a];this.emit("VAST-error",Object.assign.apply(Object,[{},DEFAULT_EVENT_DATA,t].concat(i))),util.track(e,t)}},{key:"getErrorURLTemplates",value:function(){return this.rootErrorURLTemplates.concat(this.errorURLTemplates)}},{key:"fetchVAST",value:function(e,t,r){var i=this;return new Promise(function(a,n){i.URLTemplateFilters.forEach(function(t){e=t(e)}),i.parentURLs.push(e),i.emit("VAST-resolving",{url:e,wrapperDepth:t,originalUrl:r}),i.urlHandler.get(e,i.fetchingOptions,function(t,r){i.emit("VAST-resolved",{url:e,error:t}),t?n(t):a(r)})})}},{key:"initParsingStatus",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.rootURL="",this.remainingAds=[],this.parentURLs=[],this.errorURLTemplates=[],this.rootErrorURLTemplates=[],this.maxWrapperDepth=e.wrapperLimit||DEFAULT_MAX_WRAPPER_DEPTH,this.fetchingOptions={timeout:e.timeout,withCredentials:e.withCredentials},this.urlHandler=e.urlHandler||e.urlhandler||urlHandler,this.vastVersion=null}},{key:"getRemainingAds",value:function(e){var t=this;if(0===this.remainingAds.length)return Promise.reject(new Error("No more ads are available for the given VAST"));var r=e?util.flatten(this.remainingAds):this.remainingAds.shift();return this.errorURLTemplates=[],this.parentURLs=[],this.resolveAds(r,{wrapperDepth:0,originalUrl:this.rootURL}).then(function(e){return t.buildVASTResponse(e)})}},{key:"getAndParseVAST",value:function(e){var t=this,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return this.initParsingStatus(r),this.rootURL=e,this.fetchVAST(e).then(function(i){return r.originalUrl=e,r.isRootVAST=!0,t.parse(i,r).then(function(e){return t.buildVASTResponse(e)})})}},{key:"parseVAST",value:function(e){var t=this,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return this.initParsingStatus(r),r.isRootVAST=!0,this.parse(e,r).then(function(e){return t.buildVASTResponse(e)})}},{key:"buildVASTResponse",value:function(e){var t=new VASTResponse;return t.ads=e,t.errorURLTemplates=this.getErrorURLTemplates(),t.version=this.vastVersion,this.completeWrapperResolving(t),t}},{key:"parseVastXml",value:function(e,t){var r=t.isRootVAST,i=void 0!==r&&r;if(!e||!e.documentElement||"VAST"!==e.documentElement.nodeName)throw new Error("Invalid VAST XMLDocument");var a=[],n=e.documentElement.childNodes;if(i){var s=e.documentElement.getAttribute("version");s&&(this.vastVersion=s)}for(var o in n){var l=n[o];if("Error"===l.nodeName){var c=parserUtils.parseNodeText(l);i?this.rootErrorURLTemplates.push(c):this.errorURLTemplates.push(c)}if("Ad"===l.nodeName){var u=parseAd(l);u?a.push(u):this.trackVastError(this.getErrorURLTemplates(),{ERRORCODE:101})}}return a}},{key:"parse",value:function(e,t){var r=t.resolveAll,i=void 0===r||r,a=t.wrapperSequence,n=void 0===a?null:a,s=t.originalUrl,o=void 0===s?null:s,l=t.wrapperDepth,c=void 0===l?0:l,u=t.isRootVAST,p=void 0!==u&&u,h=[];try{h=this.parseVastXml(e,{isRootVAST:p})}catch(e){return Promise.reject(e)}var d=h.length,v=h[d-1];return 1===d&&void 0!==n&&null!==n&&v&&!v.sequence&&(v.sequence=n),!1===i&&(this.remainingAds=parserUtils.splitVAST(h),h=this.remainingAds.shift()),this.resolveAds(h,{wrapperDepth:c,originalUrl:o})}},{key:"resolveAds",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],r=arguments[1],i=r.wrapperDepth,a=r.originalUrl,n=[];return t.forEach(function(t){var r=e.resolveWrappers(t,i,a);n.push(r)}),Promise.all(n).then(function(t){var r=util.flatten(t);if(!r&&e.remainingAds.length>0){var n=e.remainingAds.shift();return e.resolveAds(n,{wrapperDepth:i,originalUrl:a})}return r})}},{key:"resolveWrappers",value:function(e,t,r){var i=this;return new Promise(function(a){if(t++,!e.nextWrapperURL)return delete e.nextWrapperURL,a(e);if(t>=i.maxWrapperDepth||-1!==i.parentURLs.indexOf(e.nextWrapperURL))return e.errorCode=302,delete e.nextWrapperURL,a(e);e.nextWrapperURL=parserUtils.resolveVastAdTagURI(e.nextWrapperURL,r);var n=e.sequence;r=e.nextWrapperURL,i.fetchVAST(e.nextWrapperURL,t,r).then(function(s){return i.parse(s,{originalUrl:r,wrapperSequence:n,wrapperDepth:t}).then(function(t){if(delete e.nextWrapperURL,0===t.length)return e.creatives=[],a(e);t.forEach(function(t){t&&parserUtils.mergeWrapperAdData(t,e)}),a(t)})}).catch(function(t){e.errorCode=301,e.errorMessage=t.message,a(e)})})}},{key:"completeWrapperResolving",value:function(e){if(0===e.ads.length)this.trackVastError(e.errorURLTemplates,{ERRORCODE:303});else for(var t=e.ads.length-1;t>=0;t--){var r=e.ads[t];(r.errorCode||0===r.creatives.length)&&(this.trackVastError(r.errorURLTemplates.concat(e.errorURLTemplates),{ERRORCODE:r.errorCode||303},{ERRORMESSAGE:r.errorMessage||""},{extensions:r.extensions},{system:r.system}),e.ads.splice(t,1))}}}]),t}(events.EventEmitter),storage=null,DEFAULT_STORAGE={data:{},length:0,getItem:function(e){return this.data[e]},setItem:function(e,t){this.data[e]=t,this.length=Object.keys(this.data).length},removeItem:function(e){delete this.data[e],this.length=Object.keys(this.data).length},clear:function(){this.data={},this.length=0}},Storage=function(){function e(){classCallCheck(this,e),this.storage=this.initStorage()}return createClass(e,[{key:"initStorage",value:function(){if(storage)return storage;try{storage="undefined"!=typeof window&&null!==window?window.localStorage||window.sessionStorage:null}catch(e){storage=null}return storage&&!this.isStorageDisabled(storage)||(storage=DEFAULT_STORAGE).clear(),storage}},{key:"isStorageDisabled",value:function(e){var t="__VASTStorage__";try{if(e.setItem(t,t),e.getItem(t)!==t)return e.removeItem(t),!0}catch(e){return!0}return e.removeItem(t),!1}},{key:"getItem",value:function(e){return this.storage.getItem(e)}},{key:"setItem",value:function(e,t){return this.storage.setItem(e,t)}},{key:"removeItem",value:function(e){return this.storage.removeItem(e)}},{key:"clear",value:function(){return this.storage.clear()}}]),e}(),VASTClient=function(){function e(t,r,i){classCallCheck(this,e),this.cappingFreeLunch=t||0,this.cappingMinimumTimeInterval=r||0,this.defaultOptions={withCredentials:!1,timeout:0},this.vastParser=new VASTParser,this.storage=i||new Storage,void 0===this.lastSuccessfulAd&&(this.lastSuccessfulAd=0),void 0===this.totalCalls&&(this.totalCalls=0),void 0===this.totalCallsTimeout&&(this.totalCallsTimeout=0)}return createClass(e,[{key:"getParser",value:function(){return this.vastParser}},{key:"hasRemainingAds",value:function(){return this.vastParser.remainingAds.length>0}},{key:"getNextAds",value:function(e){return this.vastParser.getRemainingAds(e)}},{key:"get",value:function(e){var t=this,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=Date.now();return(r=Object.assign({},this.defaultOptions,r)).hasOwnProperty("resolveAll")||(r.resolveAll=!1),this.totalCallsTimeout<i?(this.totalCalls=1,this.totalCallsTimeout=i+36e5):this.totalCalls++,new Promise(function(a,n){if(t.cappingFreeLunch>=t.totalCalls)return n(new Error("VAST call canceled – FreeLunch capping not reached yet "+t.totalCalls+"/"+t.cappingFreeLunch));var s=i-t.lastSuccessfulAd;if(s<0)t.lastSuccessfulAd=0;else if(s<t.cappingMinimumTimeInterval)return n(new Error("VAST call canceled – ("+t.cappingMinimumTimeInterval+")ms minimum interval reached"));t.vastParser.getAndParseVAST(e,r).then(function(e){return a(e)}).catch(function(e){return n(e)})})}},{key:"lastSuccessfulAd",get:function(){return this.storage.getItem("vast-client-last-successful-ad")},set:function(e){this.storage.setItem("vast-client-last-successful-ad",e)}},{key:"totalCalls",get:function(){return this.storage.getItem("vast-client-total-calls")},set:function(e){this.storage.setItem("vast-client-total-calls",e)}},{key:"totalCallsTimeout",get:function(){return this.storage.getItem("vast-client-total-calls-timeout")},set:function(e){this.storage.setItem("vast-client-total-calls-timeout",e)}}]),e}(),DEFAULT_SKIP_DELAY=-1,VASTTracker=function(e){function t(e,r,i){var a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:null;classCallCheck(this,t);var n=possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).call(this));for(var s in n.ad=r,n.creative=i,n.variation=a,n.muted=!1,n.impressed=!1,n.skippable=!1,n.trackingEvents={},n._alreadyTriggeredQuartiles={},n.emitAlwaysEvents=["creativeView","start","firstQuartile","midpoint","thirdQuartile","complete","resume","pause","rewind","skip","closeLinear","close"],n.creative.trackingEvents){var o=n.creative.trackingEvents[s];n.trackingEvents[s]=o.slice(0)}return n.creative instanceof CreativeLinear?n._initLinearTracking():n._initVariationTracking(),e&&n.on("start",function(){e.lastSuccessfulAd=Date.now()}),n}return inherits(t,e),createClass(t,[{key:"_initLinearTracking",value:function(){this.linear=!0,this.skipDelay=this.creative.skipDelay,this.setDuration(this.creative.duration),this.clickThroughURLTemplate=this.creative.videoClickThroughURLTemplate,this.clickTrackingURLTemplates=this.creative.videoClickTrackingURLTemplates}},{key:"_initVariationTracking",value:function(){if(this.linear=!1,this.skipDelay=DEFAULT_SKIP_DELAY,this.variation){for(var e in this.variation.trackingEvents){var t=this.variation.trackingEvents[e];this.trackingEvents[e]?this.trackingEvents[e]=this.trackingEvents[e].concat(t.slice(0)):this.trackingEvents[e]=t.slice(0)}this.variation instanceof NonLinearAd?(this.clickThroughURLTemplate=this.variation.nonlinearClickThroughURLTemplate,this.clickTrackingURLTemplates=this.variation.nonlinearClickTrackingURLTemplates,this.setDuration(this.variation.minSuggestedDuration)):this.variation instanceof CompanionAd&&(this.clickThroughURLTemplate=this.variation.companionClickThroughURLTemplate,this.clickTrackingURLTemplates=this.variation.companionClickTrackingURLTemplates)}}},{key:"setDuration",value:function(e){this.assetDuration=e,this.quartiles={firstQuartile:Math.round(25*this.assetDuration)/100,midpoint:Math.round(50*this.assetDuration)/100,thirdQuartile:Math.round(75*this.assetDuration)/100}}},{key:"setProgress",value:function(e){var t=this,r=this.skipDelay||DEFAULT_SKIP_DELAY;if(-1===r||this.skippable||(r>e?this.emit("skip-countdown",r-e):(this.skippable=!0,this.emit("skip-countdown",0))),this.assetDuration>0){var i=[];if(e>0){var a=Math.round(e/this.assetDuration*100);for(var n in i.push("start"),i.push("progress-"+a+"%"),i.push("progress-"+Math.round(e)),this.quartiles)this.isQuartileReached(n,this.quartiles[n],e)&&(i.push(n),this._alreadyTriggeredQuartiles[n]=!0)}i.forEach(function(e){t.track(e,!0)}),e<this.progress&&this.track("rewind")}this.progress=e}},{key:"isQuartileReached",value:function(e,t,r){var i=!1;return t<=r&&!this._alreadyTriggeredQuartiles[e]&&(i=!0),i}},{key:"setMuted",value:function(e){this.muted!==e&&this.track(e?"mute":"unmute"),this.muted=e}},{key:"setPaused",value:function(e){this.paused!==e&&this.track(e?"pause":"resume"),this.paused=e}},{key:"setFullscreen",value:function(e){this.fullscreen!==e&&this.track(e?"fullscreen":"exitFullscreen"),this.fullscreen=e}},{key:"setExpand",value:function(e){this.expanded!==e&&this.track(e?"expand":"collapse"),this.expanded=e}},{key:"setSkipDelay",value:function(e){"number"==typeof e&&(this.skipDelay=e)}},{key:"trackImpression",value:function(){this.impressed||(this.impressed=!0,this.trackURLs(this.ad.impressionURLTemplates),this.track("creativeView"))}},{key:"errorWithCode",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];this.trackURLs(this.ad.errorURLTemplates,{ERRORCODE:e},{isCustomCode:t})}},{key:"complete",value:function(){this.track("complete")}},{key:"close",value:function(){this.track(this.linear?"closeLinear":"close")}},{key:"skip",value:function(){this.track("skip")}},{key:"click",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;this.clickTrackingURLTemplates&&this.clickTrackingURLTemplates.length&&this.trackURLs(this.clickTrackingURLTemplates);var t=this.clickThroughURLTemplate||e;if(t){var r=this.linear?{CONTENTPLAYHEAD:this.progressFormatted()}:{},i=util.resolveURLTemplates([t],r)[0];this.emit("clickthrough",i)}}},{key:"track",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];"closeLinear"===e&&!this.trackingEvents[e]&&this.trackingEvents.close&&(e="close");var r=this.trackingEvents[e],i=this.emitAlwaysEvents.indexOf(e)>-1;r?(this.emit(e,""),this.trackURLs(r)):i&&this.emit(e,""),t&&(delete this.trackingEvents[e],i&&this.emitAlwaysEvents.splice(this.emitAlwaysEvents.indexOf(e),1))}},{key:"trackURLs",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};this.linear&&(this.creative&&this.creative.mediaFiles&&this.creative.mediaFiles[0]&&this.creative.mediaFiles[0].fileURL&&(t.ASSETURI=this.creative.mediaFiles[0].fileURL),t.CONTENTPLAYHEAD=this.progressFormatted()),util.track(e,t,r)}},{key:"progressFormatted",value:function(){var e=parseInt(this.progress),t=e/3600;t.length<2&&(t="0"+t);var r=e/60%60;r.length<2&&(r="0"+r);var i=e%60;return i.length<2&&(i="0"+r),t+":"+r+":"+i+"."+parseInt(100*(this.progress-e))}}]),t}(events.EventEmitter);exports.VASTClient=VASTClient,exports.VASTParser=VASTParser,exports.VASTTracker=VASTTracker;