From c6dfb8a1e475ed69dda13843b95046415443c65e Mon Sep 17 00:00:00 2001 From: Elia Maino Date: Wed, 23 Jan 2019 14:36:15 +0100 Subject: [PATCH] Bundle release 2.2.1 --- dist/vast-client-module.min.js | 2 +- dist/vast-client-node.js | 53 +++++++++++++++++----------------- dist/vast-client-node.min.js | 2 +- dist/vast-client.js | 51 ++++++++++++++++---------------- dist/vast-client.min.js | 2 +- 5 files changed, 54 insertions(+), 56 deletions(-) diff --git a/dist/vast-client-module.min.js b/dist/vast-client-module.min.js index 006c08df..8a63679c 100644 --- a/dist/vast-client-module.min.js +++ b/dist/vast-client-module.min.js @@ -1 +1 @@ -class Ad{constructor(){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=[]}}class AdExtension{constructor(){this.attributes={},this.children=[]}}class AdExtensionChild{constructor(){this.name=null,this.value=null,this.attributes={}}}class CompanionAd{constructor(){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={}}}class Creative{constructor(e={}){this.id=e.id||null,this.adId=e.adId||null,this.sequence=e.sequence||null,this.apiFramework=e.apiFramework||null,this.trackingEvents={}}}class CreativeCompanion extends Creative{constructor(e={}){super(e),this.type="companion",this.variations=[]}}function track(e,t){resolveURLTemplates(e,t).forEach(e=>{if("undefined"!=typeof window&&null!==window){(new Image).src=e}})}function resolveURLTemplates(e,t={}){const r=[];t.ASSETURI&&(t.ASSETURI=encodeURIComponentRFC3986(t.ASSETURI)),t.CONTENTPLAYHEAD&&(t.CONTENTPLAYHEAD=encodeURIComponentRFC3986(t.CONTENTPLAYHEAD)),t.ERRORCODE&&!/^[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;for(let i in e){let s=e[i];if("string"==typeof s){for(let e in t){const r=t[e],i=`[${e}]`,n=`%%${e}%%`;s=(s=s.replace(i,r)).replace(n,r)}r.push(s)}}return r}function encodeURIComponentRFC3986(e){return encodeURIComponent(e).replace(/[!'()*]/g,e=>`%${e.charCodeAt(0).toString(16)}`)}function leftpad(e){return e.length<8?range(0,8-e.length,!1).map(e=>"0").join("")+e:e}function range(e,t,r){let i=[],s=en;s?t++:t--)i.push(t);return i}function isNumeric(e){return!isNaN(parseFloat(e))&&isFinite(e)}function flatten(e){return e.reduce((e,t)=>e.concat(Array.isArray(t)?flatten(t):t),[])}const util={track:track,resolveURLTemplates:resolveURLTemplates,encodeURIComponentRFC3986:encodeURIComponentRFC3986,leftpad:leftpad,range:range,isNumeric:isNumeric,flatten:flatten};function childByName(e,t){const r=e.childNodes;for(let e in r){const i=r[e];if(i.nodeName===t)return i}}function childrenByName(e,t){const r=[],i=e.childNodes;for(let e in i){const s=i[e];s.nodeName===t&&r.push(s)}return r}function resolveVastAdTagURI(e,t){if(!t)return e;if(0===e.indexOf("//")){const{protocol:t}=location;return`${t}${e}`}if(-1===e.indexOf("://")){return`${t.slice(0,t.lastIndexOf("/"))}/${e}`}return 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){const i=t.getAttribute(e);i&&r.setAttribute(e,i)}function parseDuration(e){if(null==e)return-1;if(util.isNumeric(e))return parseInt(e);const t=e.split(":");if(3!==t.length)return-1;const r=t[2].split(".");let i=parseInt(r[0]);2===r.length&&(i+=parseFloat(`0.${r[1]}`));const s=parseInt(60*t[1]),n=parseInt(60*t[0]*60);return isNaN(n)||isNaN(s)||isNaN(i)||s>3600||i>60?-1:n+s+i}function splitVAST(e){const t=[];let r=null;return e.forEach((i,s)=>{if(i.sequence&&(i.sequence=parseInt(i.sequence,10)),i.sequence>1){const t=e[s-1];if(t&&t.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(e=>{if(t.trackingEvents&&t.trackingEvents[e.type])for(let r in t.trackingEvents[e.type]){const i=t.trackingEvents[e.type][r];e.trackingEvents[r]||(e.trackingEvents[r]=[]),e.trackingEvents[r]=e.trackingEvents[r].concat(i)}}),t.videoClickTrackingURLTemplates&&t.videoClickTrackingURLTemplates.length&&e.creatives.forEach(e=>{"linear"===e.type&&(e.videoClickTrackingURLTemplates=e.videoClickTrackingURLTemplates.concat(t.videoClickTrackingURLTemplates))}),t.videoCustomClickURLTemplates&&t.videoCustomClickURLTemplates.length&&e.creatives.forEach(e=>{"linear"===e.type&&(e.videoCustomClickURLTemplates=e.videoCustomClickURLTemplates.concat(t.videoCustomClickURLTemplates))}),t.videoClickThroughURLTemplate&&e.creatives.forEach(e=>{"linear"===e.type&&null==e.videoClickThroughURLTemplate&&(e.videoClickThroughURLTemplate=t.videoClickThroughURLTemplate)})}const parserUtils={childByName:childByName,childrenByName:childrenByName,resolveVastAdTagURI:resolveVastAdTagURI,parseBoolean:parseBoolean,parseNodeText:parseNodeText,copyNodeAttribute:copyNodeAttribute,parseDuration:parseDuration,splitVAST:splitVAST,mergeWrapperAdData:mergeWrapperAdData};function parseCreativeCompanion(e,t){const r=new CreativeCompanion(t);return parserUtils.childrenByName(e,"Companion").forEach(e=>{const 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(e=>{t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"IFrameResource").forEach(e=>{t.type=e.getAttribute("creativeType")||0,t.iframeResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"StaticResource").forEach(r=>{t.type=r.getAttribute("creativeType")||0,parserUtils.childrenByName(e,"AltText").forEach(e=>{t.altText=parserUtils.parseNodeText(e)}),t.staticResource=parserUtils.parseNodeText(r)}),parserUtils.childrenByName(e,"TrackingEvents").forEach(e=>{parserUtils.childrenByName(e,"Tracking").forEach(e=>{const r=e.getAttribute("event"),i=parserUtils.parseNodeText(e);r&&i&&(null==t.trackingEvents[r]&&(t.trackingEvents[r]=[]),t.trackingEvents[r].push(i))})}),parserUtils.childrenByName(e,"CompanionClickTracking").forEach(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}class CreativeLinear extends Creative{constructor(e={}){super(e),this.type="linear",this.duration=0,this.skipDelay=null,this.mediaFiles=[],this.videoClickThroughURLTemplate=null,this.videoClickTrackingURLTemplates=[],this.videoCustomClickURLTemplates=[],this.adParameters=null,this.icons=[]}}class Icon{constructor(){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}}class MediaFile{constructor(){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){let r;const i=new CreativeLinear(t);i.duration=parserUtils.parseDuration(parserUtils.parseNodeText(parserUtils.childByName(e,"Duration")));const s=e.getAttribute("skipoffset");if(null==s)i.skipDelay=null;else if("%"===s.charAt(s.length-1)&&-1!==i.duration){const e=parseInt(s,10);i.skipDelay=i.duration*(e/100)}else i.skipDelay=parserUtils.parseDuration(s);const n=parserUtils.childByName(e,"VideoClicks");n&&(i.videoClickThroughURLTemplate=parserUtils.parseNodeText(parserUtils.childByName(n,"ClickThrough")),parserUtils.childrenByName(n,"ClickTracking").forEach(e=>{i.videoClickTrackingURLTemplates.push(parserUtils.parseNodeText(e))}),parserUtils.childrenByName(n,"CustomClick").forEach(e=>{i.videoCustomClickURLTemplates.push(parserUtils.parseNodeText(e))}));const a=parserUtils.childByName(e,"AdParameters");a&&(i.adParameters=parserUtils.parseNodeText(a)),parserUtils.childrenByName(e,"TrackingEvents").forEach(e=>{parserUtils.childrenByName(e,"Tracking").forEach(e=>{let t=e.getAttribute("event");const s=parserUtils.parseNodeText(e);if(t&&s){if("progress"===t){if(!(r=e.getAttribute("offset")))return;t="%"===r.charAt(r.length-1)?`progress-${r}`:`progress-${Math.round(parserUtils.parseDuration(r))}`}null==i.trackingEvents[t]&&(i.trackingEvents[t]=[]),i.trackingEvents[t].push(s)}})}),parserUtils.childrenByName(e,"MediaFiles").forEach(e=>{parserUtils.childrenByName(e,"MediaFile").forEach(e=>{const 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);let r=e.getAttribute("scalable");r&&"string"==typeof r&&("true"===(r=r.toLowerCase())?t.scalable=!0:"false"===r&&(t.scalable=!1));let s=e.getAttribute("maintainAspectRatio");s&&"string"==typeof s&&("true"===(s=s.toLowerCase())?t.maintainAspectRatio=!0:"false"===s&&(t.maintainAspectRatio=!1)),i.mediaFiles.push(t)})});const o=parserUtils.childByName(e,"Icons");return o&&parserUtils.childrenByName(o,"Icon").forEach(e=>{const 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(e=>{t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"IFrameResource").forEach(e=>{t.type=e.getAttribute("creativeType")||0,t.iframeResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"StaticResource").forEach(e=>{t.type=e.getAttribute("creativeType")||0,t.staticResource=parserUtils.parseNodeText(e)});const r=parserUtils.childByName(e,"IconClicks");r&&(t.iconClickThroughURLTemplate=parserUtils.parseNodeText(parserUtils.childByName(r,"IconClickThrough")),parserUtils.childrenByName(r,"IconClickTracking").forEach(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)}class CreativeNonLinear extends Creative{constructor(e={}){super(e),this.type="nonlinear",this.variations=[]}}class NonLinearAd{constructor(){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){const r=new CreativeNonLinear(t);return parserUtils.childrenByName(e,"TrackingEvents").forEach(e=>{let t,i;parserUtils.childrenByName(e,"Tracking").forEach(e=>{t=e.getAttribute("event"),i=parserUtils.parseNodeText(e),t&&i&&(null==r.trackingEvents[t]&&(r.trackingEvents[t]=[]),r.trackingEvents[t].push(i))})}),parserUtils.childrenByName(e,"NonLinear").forEach(e=>{const 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(e=>{t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"IFrameResource").forEach(e=>{t.type=e.getAttribute("creativeType")||0,t.iframeResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"StaticResource").forEach(e=>{t.type=e.getAttribute("creativeType")||0,t.staticResource=parserUtils.parseNodeText(e)});const i=parserUtils.childByName(e,"AdParameters");i&&(t.adParameters=parserUtils.parseNodeText(i)),t.nonlinearClickThroughURLTemplate=parserUtils.parseNodeText(parserUtils.childByName(e,"NonLinearClickThrough")),parserUtils.childrenByName(e,"NonLinearClickTracking").forEach(e=>{t.nonlinearClickTrackingURLTemplates.push(parserUtils.parseNodeText(e))}),r.variations.push(t)}),r}function parseAd(e){const t=e.childNodes;for(let r in t){const 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){const t=e.childNodes,r=new Ad;r.id=e.getAttribute("id")||null,r.sequence=e.getAttribute("sequence")||null;for(let e in t){const i=t[e];switch(i.nodeName){case"Error":r.errorURLTemplates.push(parserUtils.parseNodeText(i));break;case"Impression":r.impressionURLTemplates.push(parserUtils.parseNodeText(i));break;case"Creatives":parserUtils.childrenByName(i,"Creative").forEach(e=>{const t={id:e.getAttribute("id")||null,adId:parseCreativeAdIdAttribute(e),sequence:e.getAttribute("sequence")||null,apiFramework:e.getAttribute("apiFramework")||null};for(let i in e.childNodes){const s=e.childNodes[i];switch(s.nodeName){case"Linear":let e=parseCreativeLinear(s,t);e&&r.creatives.push(e);break;case"NonLinearAds":let i=parseCreativeNonLinear(s,t);i&&r.creatives.push(i);break;case"CompanionAds":let n=parseCreativeCompanion(s,t);n&&r.creatives.push(n)}}});break;case"Extensions":parseExtensions(r.extensions,parserUtils.childrenByName(i,"Extension"));break;case"AdSystem":r.system={value:parserUtils.parseNodeText(i),version:i.getAttribute("version")||null};break;case"AdTitle":r.title=parserUtils.parseNodeText(i);break;case"Description":r.description=parserUtils.parseNodeText(i);break;case"Advertiser":r.advertiser=parserUtils.parseNodeText(i);break;case"Pricing":r.pricing={value:parserUtils.parseNodeText(i),model:i.getAttribute("model")||null,currency:i.getAttribute("currency")||null};break;case"Survey":r.survey=parserUtils.parseNodeText(i)}}return r}function parseWrapper(e){const t=parseInLine(e);let 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(e=>{if(-1!==["linear","nonlinear"].indexOf(e.type)){if(e.trackingEvents){t.trackingEvents||(t.trackingEvents={}),t.trackingEvents[e.type]||(t.trackingEvents[e.type]={});for(let r in e.trackingEvents){const i=e.trackingEvents[r];t.trackingEvents[e.type][r]||(t.trackingEvents[e.type][r]=[]),i.forEach(i=>{t.trackingEvents[e.type][r].push(i)})}}e.videoClickTrackingURLTemplates&&(t.videoClickTrackingURLTemplates||(t.videoClickTrackingURLTemplates=[]),e.videoClickTrackingURLTemplates.forEach(e=>{t.videoClickTrackingURLTemplates.push(e)})),e.videoClickThroughURLTemplate&&(t.videoClickThroughURLTemplate=e.videoClickThroughURLTemplate),e.videoCustomClickURLTemplates&&(t.videoCustomClickURLTemplates||(t.videoCustomClickURLTemplates=[]),e.videoCustomClickURLTemplates.forEach(e=>{t.videoCustomClickURLTemplates.push(e)}))}}),t.nextWrapperURL)return t}function parseExtensions(e,t){t.forEach(t=>{const r=new AdExtension,i=t.attributes,s=t.childNodes;if(t.attributes)for(let e in i){const t=i[e];t.nodeName&&t.nodeValue&&(r.attributes[t.nodeName]=t.nodeValue)}for(let e in s){const t=s[e],i=parserUtils.parseNodeText(t);if("#comment"!==t.nodeName&&""!==i){const e=new AdExtensionChild;if(e.name=t.nodeName,e.value=i,t.attributes){const r=t.attributes;for(let t in r){const i=r[t];e.attributes[i.nodeName]=i.nodeValue}}r.children.push(e)}}e.push(r)})}function parseCreativeAdIdAttribute(e){return e.getAttribute("AdID")||e.getAttribute("adID")||e.getAttribute("adId")||null}var domain;function EventHandlers(){}function EventEmitter(){EventEmitter.init.call(this)}function $getMaxListeners(e){return void 0===e._maxListeners?EventEmitter.defaultMaxListeners:e._maxListeners}function emitNone(e,t,r){if(t)e.call(r);else for(var i=e.length,s=arrayClone(e,i),n=0;n0&&a.length>s){a.warned=!0;var o=new Error("Possible EventEmitter memory leak detected. "+a.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");o.name="MaxListenersExceededWarning",o.emitter=e,o.type=t,o.count=a.length,emitWarning(o)}}else a=n[t]=r,++e._eventsCount;return e}function emitWarning(e){"function"==typeof console.warn?console.warn(e):console.log(e)}function _onceWrap(e,t,r){var i=!1;function s(){e.removeListener(t,s),i||(i=!0,r.apply(e,arguments))}return s.listener=r,s}function listenerCount(e){var t=this._events;if(t){var r=t[e];if("function"==typeof r)return 1;if(r)return r.length}return 0}function spliceOne(e,t){for(var r=t,i=r+1,s=e.length;i0;)if(r[n]===t||r[n].listener&&r[n].listener===t){a=r[n].listener,s=n;break}if(s<0)return this;if(1===r.length){if(r[0]=void 0,0==--this._eventsCount)return this._events=new EventHandlers,this;delete i[e]}else spliceOne(r,s);i.removeListener&&this.emit("removeListener",e,a||t)}return this},EventEmitter.prototype.removeAllListeners=function(e){var t,r;if(!(r=this._events))return this;if(!r.removeListener)return 0===arguments.length?(this._events=new EventHandlers,this._eventsCount=0):r[e]&&(0==--this._eventsCount?this._events=new EventHandlers:delete r[e]),this;if(0===arguments.length){for(var i,s=Object.keys(r),n=0;n0?Reflect.ownKeys(this._events):[]};const flashURLHandler={get:get,supported:supported};function get$1(e,t,r){r(new Error("Please bundle the library for node to use the node urlHandler"))}const nodeURLHandler={get:get$1};function xhr(){try{const e=new window.XMLHttpRequest;return"withCredentials"in e?e:null}catch(e){return console.log("Error in XHRURLHandler support check:",e),null}}function supported$1(){return!!xhr()}function get$2(e,t,r){if("https:"===window.location.protocol&&0===e.indexOf("http://"))return r(new Error("XHRURLHandler: Cannot go from HTTPS to HTTP."));try{const i=i();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"))}}const XHRURLHandler={get:get$2,supported:supported$1};function get$3(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"))}const urlHandler={get:get$3};class VASTResponse{constructor(){this.ads=[],this.errorURLTemplates=[]}}const DEFAULT_MAX_WRAPPER_DEPTH=10,DEFAULT_EVENT_DATA={ERRORCODE:900,extensions:[]};class VASTParser extends EventEmitter{constructor(){super(),this.remainingAds=[],this.parentURLs=[],this.errorURLTemplates=[],this.rootErrorURLTemplates=[],this.maxWrapperDepth=null,this.URLTemplateFilters=[],this.fetchingOptions={}}addURLTemplateFilter(e){"function"==typeof e&&this.URLTemplateFilters.push(e)}removeURLTemplateFilter(){this.URLTemplateFilters.pop()}countURLTemplateFilters(){return this.URLTemplateFilters.length}clearURLTemplateFilters(){this.URLTemplateFilters=[]}trackVastError(e,t,...r){this.emit("VAST-error",Object.assign(DEFAULT_EVENT_DATA,t,...r)),util.track(e,t)}getErrorURLTemplates(){return this.rootErrorURLTemplates.concat(this.errorURLTemplates)}fetchVAST(e,t,r){return new Promise((i,s)=>{this.URLTemplateFilters.forEach(t=>{e=t(e)}),this.parentURLs.push(e),this.emit("VAST-resolving",{url:e,wrapperDepth:t,originalUrl:r}),this.urlHandler.get(e,this.fetchingOptions,(t,r)=>{this.emit("VAST-resolved",{url:e,error:t}),t?s(t):i(r)})})}initParsingStatus(e={}){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||urlHandler}getRemainingAds(e){if(0===this.remainingAds.length)return Promise.reject(new Error("No more ads are available for the given VAST"));const t=e?util.flatten(this.remainingAds):this.remainingAds.shift();return this.errorURLTemplates=[],this.parentURLs=[],this.resolveAds(t,{wrapperDepth:0,originalUrl:this.rootURL}).then(e=>this.buildVASTResponse(e))}getAndParseVAST(e,t={}){return this.initParsingStatus(t),this.rootURL=e,this.fetchVAST(e).then(r=>(t.originalUrl=e,t.isRootVAST=!0,this.parse(r,t).then(e=>this.buildVASTResponse(e))))}parseVAST(e,t={}){return this.initParsingStatus(t),t.isRootVAST=!0,this.parse(e,t).then(e=>this.buildVASTResponse(e))}buildVASTResponse(e){const t=new VASTResponse;return t.ads=e,t.errorURLTemplates=this.getErrorURLTemplates(),this.completeWrapperResolving(t),t}parse(e,{resolveAll:t=!0,wrapperSequence:r=null,originalUrl:i=null,wrapperDepth:s=0,isRootVAST:n=!1}){if(!e||!e.documentElement||"VAST"!==e.documentElement.nodeName)return Promise.reject(new Error("Invalid VAST XMLDocument"));let a=[];const o=e.documentElement.childNodes;for(let e in o){const t=o[e];if("Error"===t.nodeName){const e=parserUtils.parseNodeText(t);n?this.rootErrorURLTemplates.push(e):this.errorURLTemplates.push(e)}if("Ad"===t.nodeName){const e=parseAd(t);e?a.push(e):this.trackVastError(this.getErrorURLTemplates(),{ERRORCODE:101})}}const l=a.length,c=a[l-1];return 1===l&&void 0!==r&&null!==r&&c&&!c.sequence&&(c.sequence=r),!1===t&&(this.remainingAds=parserUtils.splitVAST(a),a=this.remainingAds.shift()),this.resolveAds(a,{wrapperDepth:s,originalUrl:i})}resolveAds(e=[],{wrapperDepth:t,originalUrl:r}){const i=[];return e.forEach(e=>{const s=this.resolveWrappers(e,t,r);i.push(s)}),Promise.all(i).then(e=>{const i=util.flatten(e);if(!i&&this.remainingAds.length>0){const e=this.remainingAds.shift();return this.resolveAds(e,{wrapperDepth:t,originalUrl:r})}return i})}resolveWrappers(e,t,r){return new Promise((i,s)=>{if(t++,!e.nextWrapperURL)return delete e.nextWrapperURL,i(e);if(t>=this.maxWrapperDepth||-1!==this.parentURLs.indexOf(e.nextWrapperURL))return e.errorCode=302,delete e.nextWrapperURL,i(e);e.nextWrapperURL=parserUtils.resolveVastAdTagURI(e.nextWrapperURL,r);const n=e.sequence;r=e.nextWrapperURL,this.fetchVAST(e.nextWrapperURL,t,r).then(s=>this.parse(s,{originalUrl:r,wrapperSequence:n,wrapperDepth:t}).then(t=>{if(delete e.nextWrapperURL,0===t.length)return e.creatives=[],i(e);t.forEach(t=>{t&&parserUtils.mergeWrapperAdData(t,e)}),i(t)})).catch(t=>{e.errorCode=301,e.errorMessage=t.message,i(e)})})}completeWrapperResolving(e){if(0===e.ads.length)this.trackVastError(e.errorURLTemplates,{ERRORCODE:303});else for(let t=e.ads.length-1;t>=0;t--){let 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))}}}let storage=null;const DEFAULT_STORAGE={data:{},length:0,getItem(e){return this.data[e]},setItem(e,t){this.data[e]=t,this.length=Object.keys(this.data).length},removeItem(e){delete data[e],this.length=Object.keys(this.data).length},clear(){this.data={},this.length=0}};class Storage{constructor(){this.storage=this.initStorage()}initStorage(){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}isStorageDisabled(e){const 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}getItem(e){return this.storage.getItem(e)}setItem(e,t){return this.storage.setItem(e,t)}removeItem(e){return this.storage.removeItem(e)}clear(){return this.storage.clear()}}class VASTClient{constructor(e,t,r){this.cappingFreeLunch=e||0,this.cappingMinimumTimeInterval=t||0,this.defaultOptions={withCredentials:!1,timeout:0},this.vastParser=new VASTParser,this.storage=r||new Storage,void 0===this.lastSuccessfulAd&&(this.lastSuccessfulAd=0),void 0===this.totalCalls&&(this.totalCalls=0),void 0===this.totalCallsTimeout&&(this.totalCallsTimeout=0)}getParser(){return this.vastParser}get lastSuccessfulAd(){return this.storage.getItem("vast-client-last-successful-ad")}set lastSuccessfulAd(e){this.storage.setItem("vast-client-last-successful-ad",e)}get totalCalls(){return this.storage.getItem("vast-client-total-calls")}set totalCalls(e){this.storage.setItem("vast-client-total-calls",e)}get totalCallsTimeout(){return this.storage.getItem("vast-client-total-calls-timeout")}set totalCallsTimeout(e){this.storage.setItem("vast-client-total-calls-timeout",e)}hasRemainingAds(){return this.vastParser.remainingAds.length>0}getNextAds(e){return this.vastParser.getRemainingAds(e)}get(e,t={}){const r=Date.now();return(t=Object.assign(this.defaultOptions,t)).hasOwnProperty("resolveAll")||(t.resolveAll=!1),this.totalCallsTimeout{if(this.cappingFreeLunch>=this.totalCalls)return s(new Error(`VAST call canceled – FreeLunch capping not reached yet ${this.totalCalls}/${this.cappingFreeLunch}`));const n=r-this.lastSuccessfulAd;if(n<0)this.lastSuccessfulAd=0;else if(ni(e)).catch(e=>s(e))})}}const DEFAULT_SKIP_DELAY=-1;class VASTTracker extends EventEmitter{constructor(e,t,r,i=null){super(),this.ad=t,this.creative=r,this.variation=i,this.muted=!1,this.impressed=!1,this.skippable=!1,this.trackingEvents={},this._alreadyTriggeredQuartiles={},this.emitAlwaysEvents=["creativeView","start","firstQuartile","midpoint","thirdQuartile","complete","resume","pause","rewind","skip","closeLinear","close"];for(let e in this.creative.trackingEvents){const t=this.creative.trackingEvents[e];this.trackingEvents[e]=t.slice(0)}this.creative instanceof CreativeLinear?this._initLinearTracking():this._initVariationTracking(),e&&this.on("start",()=>{e.lastSuccessfulAd=Date.now()})}_initLinearTracking(){this.linear=!0,this.skipDelay=this.creative.skipDelay,this.setDuration(this.creative.duration),this.clickThroughURLTemplate=this.creative.videoClickThroughURLTemplate,this.clickTrackingURLTemplates=this.creative.videoClickTrackingURLTemplates}_initVariationTracking(){if(this.linear=!1,this.skipDelay=DEFAULT_SKIP_DELAY,this.variation){for(let e in this.variation.trackingEvents){const 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)}}setDuration(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}}setProgress(e){const t=this.skipDelay||DEFAULT_SKIP_DELAY;if(-1===t||this.skippable||(t>e?this.emit("skip-countdown",t-e):(this.skippable=!0,this.emit("skip-countdown",0))),this.assetDuration>0){const t=[];if(e>0){const r=Math.round(e/this.assetDuration*100);t.push("start"),t.push(`progress-${r}%`),t.push(`progress-${Math.round(e)}`);for(let r in this.quartiles)this.isQuartileReached(r,this.quartiles[r],e)&&(t.push(r),this._alreadyTriggeredQuartiles[r]=!0)}t.forEach(e=>{this.track(e,!0)}),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))}trackURLs(e,t={}){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)}progressFormatted(){const e=parseInt(this.progress);let t=e/3600;t.length<2&&(t=`0${t}`);let r=e/60%60;r.length<2&&(r=`0${r}`);let i=e%60;return i.length<2&&(i=`0${r}`),`${t}:${r}:${i}.${parseInt(100*(this.progress-e))}`}}export{VASTClient,VASTParser,VASTTracker}; +class Ad{constructor(){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=[]}}class AdExtension{constructor(){this.attributes={},this.children=[]}}class AdExtensionChild{constructor(){this.name=null,this.value=null,this.attributes={}}}class CompanionAd{constructor(){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={}}}class Creative{constructor(e={}){this.id=e.id||null,this.adId=e.adId||null,this.sequence=e.sequence||null,this.apiFramework=e.apiFramework||null,this.trackingEvents={}}}class CreativeCompanion extends Creative{constructor(e={}){super(e),this.type="companion",this.variations=[]}}function track(e,t){resolveURLTemplates(e,t).forEach(e=>{if("undefined"!=typeof window&&null!==window){(new Image).src=e}})}function resolveURLTemplates(e,t={}){const r=[];t.ASSETURI&&(t.ASSETURI=encodeURIComponentRFC3986(t.ASSETURI)),t.CONTENTPLAYHEAD&&(t.CONTENTPLAYHEAD=encodeURIComponentRFC3986(t.CONTENTPLAYHEAD)),t.ERRORCODE&&!/^[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;for(let i in e){let s=e[i];if("string"==typeof s){for(let e in t){const r=t[e],i=`[${e}]`,n=`%%${e}%%`;s=(s=s.replace(i,r)).replace(n,r)}r.push(s)}}return r}function encodeURIComponentRFC3986(e){return encodeURIComponent(e).replace(/[!'()*]/g,e=>`%${e.charCodeAt(0).toString(16)}`)}function leftpad(e){return e.length<8?range(0,8-e.length,!1).map(e=>"0").join("")+e:e}function range(e,t,r){let i=[],s=en;s?t++:t--)i.push(t);return i}function isNumeric(e){return!isNaN(parseFloat(e))&&isFinite(e)}function flatten(e){return e.reduce((e,t)=>e.concat(Array.isArray(t)?flatten(t):t),[])}const util={track:track,resolveURLTemplates:resolveURLTemplates,encodeURIComponentRFC3986:encodeURIComponentRFC3986,leftpad:leftpad,range:range,isNumeric:isNumeric,flatten:flatten};function childByName(e,t){const r=e.childNodes;for(let e in r){const i=r[e];if(i.nodeName===t)return i}}function childrenByName(e,t){const r=[],i=e.childNodes;for(let e in i){const s=i[e];s.nodeName===t&&r.push(s)}return r}function resolveVastAdTagURI(e,t){if(!t)return e;if(0===e.indexOf("//")){const{protocol:t}=location;return`${t}${e}`}if(-1===e.indexOf("://")){return`${t.slice(0,t.lastIndexOf("/"))}/${e}`}return 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){const i=t.getAttribute(e);i&&r.setAttribute(e,i)}function parseDuration(e){if(null==e)return-1;if(util.isNumeric(e))return parseInt(e);const t=e.split(":");if(3!==t.length)return-1;const r=t[2].split(".");let i=parseInt(r[0]);2===r.length&&(i+=parseFloat(`0.${r[1]}`));const s=parseInt(60*t[1]),n=parseInt(60*t[0]*60);return isNaN(n)||isNaN(s)||isNaN(i)||s>3600||i>60?-1:n+s+i}function splitVAST(e){const t=[];let r=null;return e.forEach((i,s)=>{if(i.sequence&&(i.sequence=parseInt(i.sequence,10)),i.sequence>1){const t=e[s-1];if(t&&t.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(e=>{if(t.trackingEvents&&t.trackingEvents[e.type])for(let r in t.trackingEvents[e.type]){const i=t.trackingEvents[e.type][r];e.trackingEvents[r]||(e.trackingEvents[r]=[]),e.trackingEvents[r]=e.trackingEvents[r].concat(i)}}),t.videoClickTrackingURLTemplates&&t.videoClickTrackingURLTemplates.length&&e.creatives.forEach(e=>{"linear"===e.type&&(e.videoClickTrackingURLTemplates=e.videoClickTrackingURLTemplates.concat(t.videoClickTrackingURLTemplates))}),t.videoCustomClickURLTemplates&&t.videoCustomClickURLTemplates.length&&e.creatives.forEach(e=>{"linear"===e.type&&(e.videoCustomClickURLTemplates=e.videoCustomClickURLTemplates.concat(t.videoCustomClickURLTemplates))}),t.videoClickThroughURLTemplate&&e.creatives.forEach(e=>{"linear"===e.type&&null==e.videoClickThroughURLTemplate&&(e.videoClickThroughURLTemplate=t.videoClickThroughURLTemplate)})}const parserUtils={childByName:childByName,childrenByName:childrenByName,resolveVastAdTagURI:resolveVastAdTagURI,parseBoolean:parseBoolean,parseNodeText:parseNodeText,copyNodeAttribute:copyNodeAttribute,parseDuration:parseDuration,splitVAST:splitVAST,mergeWrapperAdData:mergeWrapperAdData};function parseCreativeCompanion(e,t){const r=new CreativeCompanion(t);return parserUtils.childrenByName(e,"Companion").forEach(e=>{const 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(e=>{t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"IFrameResource").forEach(e=>{t.type=e.getAttribute("creativeType")||0,t.iframeResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"StaticResource").forEach(r=>{t.type=r.getAttribute("creativeType")||0,parserUtils.childrenByName(e,"AltText").forEach(e=>{t.altText=parserUtils.parseNodeText(e)}),t.staticResource=parserUtils.parseNodeText(r)}),parserUtils.childrenByName(e,"TrackingEvents").forEach(e=>{parserUtils.childrenByName(e,"Tracking").forEach(e=>{const r=e.getAttribute("event"),i=parserUtils.parseNodeText(e);r&&i&&(null==t.trackingEvents[r]&&(t.trackingEvents[r]=[]),t.trackingEvents[r].push(i))})}),parserUtils.childrenByName(e,"CompanionClickTracking").forEach(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}class CreativeLinear extends Creative{constructor(e={}){super(e),this.type="linear",this.duration=0,this.skipDelay=null,this.mediaFiles=[],this.videoClickThroughURLTemplate=null,this.videoClickTrackingURLTemplates=[],this.videoCustomClickURLTemplates=[],this.adParameters=null,this.icons=[]}}class Icon{constructor(){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}}class MediaFile{constructor(){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){let r;const i=new CreativeLinear(t);i.duration=parserUtils.parseDuration(parserUtils.parseNodeText(parserUtils.childByName(e,"Duration")));const s=e.getAttribute("skipoffset");if(null==s)i.skipDelay=null;else if("%"===s.charAt(s.length-1)&&-1!==i.duration){const e=parseInt(s,10);i.skipDelay=i.duration*(e/100)}else i.skipDelay=parserUtils.parseDuration(s);const n=parserUtils.childByName(e,"VideoClicks");n&&(i.videoClickThroughURLTemplate=parserUtils.parseNodeText(parserUtils.childByName(n,"ClickThrough")),parserUtils.childrenByName(n,"ClickTracking").forEach(e=>{i.videoClickTrackingURLTemplates.push(parserUtils.parseNodeText(e))}),parserUtils.childrenByName(n,"CustomClick").forEach(e=>{i.videoCustomClickURLTemplates.push(parserUtils.parseNodeText(e))}));const a=parserUtils.childByName(e,"AdParameters");a&&(i.adParameters=parserUtils.parseNodeText(a)),parserUtils.childrenByName(e,"TrackingEvents").forEach(e=>{parserUtils.childrenByName(e,"Tracking").forEach(e=>{let t=e.getAttribute("event");const s=parserUtils.parseNodeText(e);if(t&&s){if("progress"===t){if(!(r=e.getAttribute("offset")))return;t="%"===r.charAt(r.length-1)?`progress-${r}`:`progress-${Math.round(parserUtils.parseDuration(r))}`}null==i.trackingEvents[t]&&(i.trackingEvents[t]=[]),i.trackingEvents[t].push(s)}})}),parserUtils.childrenByName(e,"MediaFiles").forEach(e=>{parserUtils.childrenByName(e,"MediaFile").forEach(e=>{const 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);let r=e.getAttribute("scalable");r&&"string"==typeof r&&("true"===(r=r.toLowerCase())?t.scalable=!0:"false"===r&&(t.scalable=!1));let s=e.getAttribute("maintainAspectRatio");s&&"string"==typeof s&&("true"===(s=s.toLowerCase())?t.maintainAspectRatio=!0:"false"===s&&(t.maintainAspectRatio=!1)),i.mediaFiles.push(t)})});const o=parserUtils.childByName(e,"Icons");return o&&parserUtils.childrenByName(o,"Icon").forEach(e=>{const 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(e=>{t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"IFrameResource").forEach(e=>{t.type=e.getAttribute("creativeType")||0,t.iframeResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"StaticResource").forEach(e=>{t.type=e.getAttribute("creativeType")||0,t.staticResource=parserUtils.parseNodeText(e)});const r=parserUtils.childByName(e,"IconClicks");r&&(t.iconClickThroughURLTemplate=parserUtils.parseNodeText(parserUtils.childByName(r,"IconClickThrough")),parserUtils.childrenByName(r,"IconClickTracking").forEach(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)}class CreativeNonLinear extends Creative{constructor(e={}){super(e),this.type="nonlinear",this.variations=[]}}class NonLinearAd{constructor(){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){const r=new CreativeNonLinear(t);return parserUtils.childrenByName(e,"TrackingEvents").forEach(e=>{let t,i;parserUtils.childrenByName(e,"Tracking").forEach(e=>{t=e.getAttribute("event"),i=parserUtils.parseNodeText(e),t&&i&&(null==r.trackingEvents[t]&&(r.trackingEvents[t]=[]),r.trackingEvents[t].push(i))})}),parserUtils.childrenByName(e,"NonLinear").forEach(e=>{const 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(e=>{t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"IFrameResource").forEach(e=>{t.type=e.getAttribute("creativeType")||0,t.iframeResource=parserUtils.parseNodeText(e)}),parserUtils.childrenByName(e,"StaticResource").forEach(e=>{t.type=e.getAttribute("creativeType")||0,t.staticResource=parserUtils.parseNodeText(e)});const i=parserUtils.childByName(e,"AdParameters");i&&(t.adParameters=parserUtils.parseNodeText(i)),t.nonlinearClickThroughURLTemplate=parserUtils.parseNodeText(parserUtils.childByName(e,"NonLinearClickThrough")),parserUtils.childrenByName(e,"NonLinearClickTracking").forEach(e=>{t.nonlinearClickTrackingURLTemplates.push(parserUtils.parseNodeText(e))}),r.variations.push(t)}),r}function parseAd(e){const t=e.childNodes;for(let r in t){const 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){const t=e.childNodes,r=new Ad;r.id=e.getAttribute("id")||null,r.sequence=e.getAttribute("sequence")||null;for(let e in t){const i=t[e];switch(i.nodeName){case"Error":r.errorURLTemplates.push(parserUtils.parseNodeText(i));break;case"Impression":r.impressionURLTemplates.push(parserUtils.parseNodeText(i));break;case"Creatives":parserUtils.childrenByName(i,"Creative").forEach(e=>{const t={id:e.getAttribute("id")||null,adId:parseCreativeAdIdAttribute(e),sequence:e.getAttribute("sequence")||null,apiFramework:e.getAttribute("apiFramework")||null};for(let i in e.childNodes){const s=e.childNodes[i];switch(s.nodeName){case"Linear":let e=parseCreativeLinear(s,t);e&&r.creatives.push(e);break;case"NonLinearAds":let i=parseCreativeNonLinear(s,t);i&&r.creatives.push(i);break;case"CompanionAds":let n=parseCreativeCompanion(s,t);n&&r.creatives.push(n)}}});break;case"Extensions":parseExtensions(r.extensions,parserUtils.childrenByName(i,"Extension"));break;case"AdSystem":r.system={value:parserUtils.parseNodeText(i),version:i.getAttribute("version")||null};break;case"AdTitle":r.title=parserUtils.parseNodeText(i);break;case"Description":r.description=parserUtils.parseNodeText(i);break;case"Advertiser":r.advertiser=parserUtils.parseNodeText(i);break;case"Pricing":r.pricing={value:parserUtils.parseNodeText(i),model:i.getAttribute("model")||null,currency:i.getAttribute("currency")||null};break;case"Survey":r.survey=parserUtils.parseNodeText(i)}}return r}function parseWrapper(e){const t=parseInLine(e);let 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(e=>{if(-1!==["linear","nonlinear"].indexOf(e.type)){if(e.trackingEvents){t.trackingEvents||(t.trackingEvents={}),t.trackingEvents[e.type]||(t.trackingEvents[e.type]={});for(let r in e.trackingEvents){const i=e.trackingEvents[r];t.trackingEvents[e.type][r]||(t.trackingEvents[e.type][r]=[]),i.forEach(i=>{t.trackingEvents[e.type][r].push(i)})}}e.videoClickTrackingURLTemplates&&(t.videoClickTrackingURLTemplates||(t.videoClickTrackingURLTemplates=[]),e.videoClickTrackingURLTemplates.forEach(e=>{t.videoClickTrackingURLTemplates.push(e)})),e.videoClickThroughURLTemplate&&(t.videoClickThroughURLTemplate=e.videoClickThroughURLTemplate),e.videoCustomClickURLTemplates&&(t.videoCustomClickURLTemplates||(t.videoCustomClickURLTemplates=[]),e.videoCustomClickURLTemplates.forEach(e=>{t.videoCustomClickURLTemplates.push(e)}))}}),t.nextWrapperURL)return t}function parseExtensions(e,t){t.forEach(t=>{const r=new AdExtension,i=t.attributes,s=t.childNodes;if(t.attributes)for(let e in i){const t=i[e];t.nodeName&&t.nodeValue&&(r.attributes[t.nodeName]=t.nodeValue)}for(let e in s){const t=s[e],i=parserUtils.parseNodeText(t);if("#comment"!==t.nodeName&&""!==i){const e=new AdExtensionChild;if(e.name=t.nodeName,e.value=i,t.attributes){const r=t.attributes;for(let t in r){const i=r[t];e.attributes[i.nodeName]=i.nodeValue}}r.children.push(e)}}e.push(r)})}function parseCreativeAdIdAttribute(e){return e.getAttribute("AdID")||e.getAttribute("adID")||e.getAttribute("adId")||null}var domain;function EventHandlers(){}function EventEmitter(){EventEmitter.init.call(this)}function $getMaxListeners(e){return void 0===e._maxListeners?EventEmitter.defaultMaxListeners:e._maxListeners}function emitNone(e,t,r){if(t)e.call(r);else for(var i=e.length,s=arrayClone(e,i),n=0;n0&&a.length>s){a.warned=!0;var o=new Error("Possible EventEmitter memory leak detected. "+a.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");o.name="MaxListenersExceededWarning",o.emitter=e,o.type=t,o.count=a.length,emitWarning(o)}}else a=n[t]=r,++e._eventsCount;return e}function emitWarning(e){"function"==typeof console.warn?console.warn(e):console.log(e)}function _onceWrap(e,t,r){var i=!1;function s(){e.removeListener(t,s),i||(i=!0,r.apply(e,arguments))}return s.listener=r,s}function listenerCount(e){var t=this._events;if(t){var r=t[e];if("function"==typeof r)return 1;if(r)return r.length}return 0}function spliceOne(e,t){for(var r=t,i=r+1,s=e.length;i0;)if(r[n]===t||r[n].listener&&r[n].listener===t){a=r[n].listener,s=n;break}if(s<0)return this;if(1===r.length){if(r[0]=void 0,0==--this._eventsCount)return this._events=new EventHandlers,this;delete i[e]}else spliceOne(r,s);i.removeListener&&this.emit("removeListener",e,a||t)}return this},EventEmitter.prototype.removeAllListeners=function(e){var t,r;if(!(r=this._events))return this;if(!r.removeListener)return 0===arguments.length?(this._events=new EventHandlers,this._eventsCount=0):r[e]&&(0==--this._eventsCount?this._events=new EventHandlers:delete r[e]),this;if(0===arguments.length){for(var i,s=Object.keys(r),n=0;n0?Reflect.ownKeys(this._events):[]};const flashURLHandler={get:get,supported:supported};function get$1(e,t,r){r(new Error("Please bundle the library for node to use the node urlHandler"))}const nodeURLHandler={get:get$1};function xhr(){try{const e=new window.XMLHttpRequest;return"withCredentials"in e?e:null}catch(e){return console.log("Error in XHRURLHandler support check:",e),null}}function supported$1(){return!!xhr()}function get$2(e,t,r){if("https:"===window.location.protocol&&0===e.indexOf("http://"))return r(new Error("XHRURLHandler: Cannot go from HTTPS to HTTP."));try{const 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"))}}const XHRURLHandler={get:get$2,supported:supported$1};function get$3(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"))}const urlHandler={get:get$3};class VASTResponse{constructor(){this.ads=[],this.errorURLTemplates=[]}}const DEFAULT_MAX_WRAPPER_DEPTH=10,DEFAULT_EVENT_DATA={ERRORCODE:900,extensions:[]};class VASTParser extends EventEmitter{constructor(){super(),this.remainingAds=[],this.parentURLs=[],this.errorURLTemplates=[],this.rootErrorURLTemplates=[],this.maxWrapperDepth=null,this.URLTemplateFilters=[],this.fetchingOptions={}}addURLTemplateFilter(e){"function"==typeof e&&this.URLTemplateFilters.push(e)}removeURLTemplateFilter(){this.URLTemplateFilters.pop()}countURLTemplateFilters(){return this.URLTemplateFilters.length}clearURLTemplateFilters(){this.URLTemplateFilters=[]}trackVastError(e,t,...r){this.emit("VAST-error",Object.assign(DEFAULT_EVENT_DATA,t,...r)),util.track(e,t)}getErrorURLTemplates(){return this.rootErrorURLTemplates.concat(this.errorURLTemplates)}fetchVAST(e,t,r){return new Promise((i,s)=>{this.URLTemplateFilters.forEach(t=>{e=t(e)}),this.parentURLs.push(e),this.emit("VAST-resolving",{url:e,wrapperDepth:t,originalUrl:r}),this.urlHandler.get(e,this.fetchingOptions,(t,r)=>{this.emit("VAST-resolved",{url:e,error:t}),t?s(t):i(r)})})}initParsingStatus(e={}){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||urlHandler}getRemainingAds(e){if(0===this.remainingAds.length)return Promise.reject(new Error("No more ads are available for the given VAST"));const t=e?util.flatten(this.remainingAds):this.remainingAds.shift();return this.errorURLTemplates=[],this.parentURLs=[],this.resolveAds(t,{wrapperDepth:0,originalUrl:this.rootURL}).then(e=>this.buildVASTResponse(e))}getAndParseVAST(e,t={}){return this.initParsingStatus(t),this.rootURL=e,this.fetchVAST(e).then(r=>(t.originalUrl=e,t.isRootVAST=!0,this.parse(r,t).then(e=>this.buildVASTResponse(e))))}parseVAST(e,t={}){return this.initParsingStatus(t),t.isRootVAST=!0,this.parse(e,t).then(e=>this.buildVASTResponse(e))}buildVASTResponse(e){const t=new VASTResponse;return t.ads=e,t.errorURLTemplates=this.getErrorURLTemplates(),this.completeWrapperResolving(t),t}parse(e,{resolveAll:t=!0,wrapperSequence:r=null,originalUrl:i=null,wrapperDepth:s=0,isRootVAST:n=!1}){if(!e||!e.documentElement||"VAST"!==e.documentElement.nodeName)return Promise.reject(new Error("Invalid VAST XMLDocument"));let a=[];const o=e.documentElement.childNodes;for(let e in o){const t=o[e];if("Error"===t.nodeName){const e=parserUtils.parseNodeText(t);n?this.rootErrorURLTemplates.push(e):this.errorURLTemplates.push(e)}if("Ad"===t.nodeName){const e=parseAd(t);e?a.push(e):this.trackVastError(this.getErrorURLTemplates(),{ERRORCODE:101})}}const l=a.length,c=a[l-1];return 1===l&&void 0!==r&&null!==r&&c&&!c.sequence&&(c.sequence=r),!1===t&&(this.remainingAds=parserUtils.splitVAST(a),a=this.remainingAds.shift()),this.resolveAds(a,{wrapperDepth:s,originalUrl:i})}resolveAds(e=[],{wrapperDepth:t,originalUrl:r}){const i=[];return e.forEach(e=>{const s=this.resolveWrappers(e,t,r);i.push(s)}),Promise.all(i).then(e=>{const i=util.flatten(e);if(!i&&this.remainingAds.length>0){const e=this.remainingAds.shift();return this.resolveAds(e,{wrapperDepth:t,originalUrl:r})}return i})}resolveWrappers(e,t,r){return new Promise((i,s)=>{if(t++,!e.nextWrapperURL)return delete e.nextWrapperURL,i(e);if(t>=this.maxWrapperDepth||-1!==this.parentURLs.indexOf(e.nextWrapperURL))return e.errorCode=302,delete e.nextWrapperURL,i(e);e.nextWrapperURL=parserUtils.resolveVastAdTagURI(e.nextWrapperURL,r);const n=e.sequence;r=e.nextWrapperURL,this.fetchVAST(e.nextWrapperURL,t,r).then(s=>this.parse(s,{originalUrl:r,wrapperSequence:n,wrapperDepth:t}).then(t=>{if(delete e.nextWrapperURL,0===t.length)return e.creatives=[],i(e);t.forEach(t=>{t&&parserUtils.mergeWrapperAdData(t,e)}),i(t)})).catch(t=>{e.errorCode=301,e.errorMessage=t.message,i(e)})})}completeWrapperResolving(e){if(0===e.ads.length)this.trackVastError(e.errorURLTemplates,{ERRORCODE:303});else for(let t=e.ads.length-1;t>=0;t--){let 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))}}}let storage=null;const DEFAULT_STORAGE={data:{},length:0,getItem(e){return this.data[e]},setItem(e,t){this.data[e]=t,this.length=Object.keys(this.data).length},removeItem(e){delete data[e],this.length=Object.keys(this.data).length},clear(){this.data={},this.length=0}};class Storage{constructor(){this.storage=this.initStorage()}initStorage(){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}isStorageDisabled(e){const 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}getItem(e){return this.storage.getItem(e)}setItem(e,t){return this.storage.setItem(e,t)}removeItem(e){return this.storage.removeItem(e)}clear(){return this.storage.clear()}}class VASTClient{constructor(e,t,r){this.cappingFreeLunch=e||0,this.cappingMinimumTimeInterval=t||0,this.defaultOptions={withCredentials:!1,timeout:0},this.vastParser=new VASTParser,this.storage=r||new Storage,void 0===this.lastSuccessfulAd&&(this.lastSuccessfulAd=0),void 0===this.totalCalls&&(this.totalCalls=0),void 0===this.totalCallsTimeout&&(this.totalCallsTimeout=0)}getParser(){return this.vastParser}get lastSuccessfulAd(){return this.storage.getItem("vast-client-last-successful-ad")}set lastSuccessfulAd(e){this.storage.setItem("vast-client-last-successful-ad",e)}get totalCalls(){return this.storage.getItem("vast-client-total-calls")}set totalCalls(e){this.storage.setItem("vast-client-total-calls",e)}get totalCallsTimeout(){return this.storage.getItem("vast-client-total-calls-timeout")}set totalCallsTimeout(e){this.storage.setItem("vast-client-total-calls-timeout",e)}hasRemainingAds(){return this.vastParser.remainingAds.length>0}getNextAds(e){return this.vastParser.getRemainingAds(e)}get(e,t={}){const r=Date.now();return(t=Object.assign(this.defaultOptions,t)).hasOwnProperty("resolveAll")||(t.resolveAll=!1),this.totalCallsTimeout{if(this.cappingFreeLunch>=this.totalCalls)return s(new Error(`VAST call canceled – FreeLunch capping not reached yet ${this.totalCalls}/${this.cappingFreeLunch}`));const n=r-this.lastSuccessfulAd;if(n<0)this.lastSuccessfulAd=0;else if(ni(e)).catch(e=>s(e))})}}const DEFAULT_SKIP_DELAY=-1;class VASTTracker extends EventEmitter{constructor(e,t,r,i=null){super(),this.ad=t,this.creative=r,this.variation=i,this.muted=!1,this.impressed=!1,this.skippable=!1,this.trackingEvents={},this._alreadyTriggeredQuartiles={},this.emitAlwaysEvents=["creativeView","start","firstQuartile","midpoint","thirdQuartile","complete","resume","pause","rewind","skip","closeLinear","close"];for(let e in this.creative.trackingEvents){const t=this.creative.trackingEvents[e];this.trackingEvents[e]=t.slice(0)}this.creative instanceof CreativeLinear?this._initLinearTracking():this._initVariationTracking(),e&&this.on("start",()=>{e.lastSuccessfulAd=Date.now()})}_initLinearTracking(){this.linear=!0,this.skipDelay=this.creative.skipDelay,this.setDuration(this.creative.duration),this.clickThroughURLTemplate=this.creative.videoClickThroughURLTemplate,this.clickTrackingURLTemplates=this.creative.videoClickTrackingURLTemplates}_initVariationTracking(){if(this.linear=!1,this.skipDelay=DEFAULT_SKIP_DELAY,this.variation){for(let e in this.variation.trackingEvents){const 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)}}setDuration(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}}setProgress(e){const t=this.skipDelay||DEFAULT_SKIP_DELAY;if(-1===t||this.skippable||(t>e?this.emit("skip-countdown",t-e):(this.skippable=!0,this.emit("skip-countdown",0))),this.assetDuration>0){const t=[];if(e>0){const r=Math.round(e/this.assetDuration*100);t.push("start"),t.push(`progress-${r}%`),t.push(`progress-${Math.round(e)}`);for(let r in this.quartiles)this.isQuartileReached(r,this.quartiles[r],e)&&(t.push(r),this._alreadyTriggeredQuartiles[r]=!0)}t.forEach(e=>{this.track(e,!0)}),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))}trackURLs(e,t={}){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)}progressFormatted(){const e=parseInt(this.progress);let t=e/3600;t.length<2&&(t=`0${t}`);let r=e/60%60;r.length<2&&(r=`0${r}`);let i=e%60;return i.length<2&&(i=`0${r}`),`${t}:${r}:${i}.${parseInt(100*(this.progress-e))}`}}export{VASTClient,VASTParser,VASTTracker}; diff --git a/dist/vast-client-node.js b/dist/vast-client-node.js index d79986e7..d83735ce 100644 --- a/dist/vast-client-node.js +++ b/dist/vast-client-node.js @@ -1142,11 +1142,11 @@ function parseCreativeAdIdAttribute(creativeElement) { } function xdr() { - var xdr = void 0; + var request = void 0; if (window.XDomainRequest) { - xdr = new XDomainRequest(); + request = new XDomainRequest(); } - return xdr; + return request; } function supported() { @@ -1161,16 +1161,14 @@ function get$1(url, options, cb) { } else { return cb(new Error('FlashURLHandler: Microsoft.XMLDOM format not supported')); } - - var xdr = xdr(); - xdr.open('GET', url); - xdr.timeout = options.timeout || 0; - xdr.withCredentials = options.withCredentials || false; - xdr.send(); - xdr.onprogress = function () {}; - - xdr.onload = function () { - xmlDocument.loadXML(xdr.responseText); + request.open('GET', url); + request.timeout = options.timeout || 0; + request.withCredentials = options.withCredentials || false; + request.send(); + request.onprogress = function () {}; + + request.onload = function () { + xmlDocument.loadXML(request.responseText); cb(null, xmlDocument); }; } @@ -1237,10 +1235,10 @@ var nodeURLHandler = { function xhr() { try { - var _xhr = new window.XMLHttpRequest(); - if ('withCredentials' in _xhr) { + var request = new window.XMLHttpRequest(); + if ('withCredentials' in request) { // check CORS support - return _xhr; + return request; } return null; } catch (err) { @@ -1259,21 +1257,22 @@ function get$3(url, options, cb) { } try { - var _xhr2 = _xhr2(); - _xhr2.open('GET', url); - _xhr2.timeout = options.timeout || 0; - _xhr2.withCredentials = options.withCredentials || false; - _xhr2.overrideMimeType && _xhr2.overrideMimeType('text/xml'); - _xhr2.onreadystatechange = function () { - if (_xhr2.readyState === 4) { - if (_xhr2.status === 200) { - cb(null, _xhr2.responseXML); + var request = xhr(); + + request.open('GET', url); + request.timeout = options.timeout || 0; + request.withCredentials = options.withCredentials || false; + request.overrideMimeType && request.overrideMimeType('text/xml'); + request.onreadystatechange = function () { + if (request.readyState === 4) { + if (request.status === 200) { + cb(null, request.responseXML); } else { - cb(new Error('XHRURLHandler: ' + _xhr2.statusText)); + cb(new Error('XHRURLHandler: ' + request.statusText)); } } }; - _xhr2.send(); + request.send(); } catch (error) { cb(new Error('XHRURLHandler: Unexpected error')); } diff --git a/dist/vast-client-node.min.js b/dist/vast-client-node.min.js index 88904319..033eaef9 100644 --- a/dist/vast-client-node.min.js +++ b/dist/vast-client-node.min.js @@ -1 +1 @@ -"use strict";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;r0&&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){resolveURLTemplates(e,t).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=[];for(var i in t.ASSETURI&&(t.ASSETURI=encodeURIComponentRFC3986(t.ASSETURI)),t.CONTENTPLAYHEAD&&(t.CONTENTPLAYHEAD=encodeURIComponentRFC3986(t.CONTENTPLAYHEAD)),t.ERRORCODE&&!/^[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 a=e[i];if("string"==typeof a){for(var n in t){var s=t[n],o="["+n+"]",l="%%"+n+"%%";a=(a=a.replace(o,s)).replace(l,s)}r.push(a)}}return r}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(e){return"0"}).join("")+e:e}function range(e,t,r){for(var i=[],a=en;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)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];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&&(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&&(null==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(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))}null==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&&(null==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];switch(a.nodeName){case"Linear":var n=parseCreativeLinear(a,t);n&&r.creatives.push(n);break;case"NonLinearAds":var s=parseCreativeNonLinear(a,t);s&&r.creatives.push(s);break;case"CompanionAds":var o=parseCreativeCompanion(a,t);o&&r.creatives.push(o)}}});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];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&&(t.videoClickTrackingURLTemplates||(t.videoClickTrackingURLTemplates=[]),e.videoClickTrackingURLTemplates.forEach(function(e){t.videoClickTrackingURLTemplates.push(e)})),e.videoClickThroughURLTemplate&&(t.videoClickThroughURLTemplate=e.videoClickThroughURLTemplate),e.videoCustomClickURLTemplates&&(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=a();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){var r=void 0;n+=e,clearTimeout(r),r=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 console.log("Error in XHRURLHandler support check:",e),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=i();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=[]},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;a0&&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||urlHandler}},{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(),this.completeWrapperResolving(t),t}},{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;if(!e||!e.documentElement||"VAST"!==e.documentElement.nodeName)return Promise.reject(new Error("Invalid VAST XMLDocument"));var h=[],d=e.documentElement.childNodes;for(var m in d){var v=d[m];if("Error"===v.nodeName){var f=parserUtils.parseNodeText(v);p?this.rootErrorURLTemplates.push(f):this.errorURLTemplates.push(f)}if("Ad"===v.nodeName){var g=parseAd(v);g?h.push(g):this.trackVastError(this.getErrorURLTemplates(),{ERRORCODE:101})}}var T=h.length,k=h[T-1];return 1===T&&void 0!==n&&null!==n&&k&&!k.sequence&&(k.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,n){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 s=e.sequence;r=e.nextWrapperURL,i.fetchVAST(e.nextWrapperURL,t,r).then(function(n){return i.parse(n,{originalUrl:r,wrapperSequence:s,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 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=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(s3&&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)}),e0&&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]:{};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)}},{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; +"use strict";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;r0&&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){resolveURLTemplates(e,t).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=[];for(var i in t.ASSETURI&&(t.ASSETURI=encodeURIComponentRFC3986(t.ASSETURI)),t.CONTENTPLAYHEAD&&(t.CONTENTPLAYHEAD=encodeURIComponentRFC3986(t.CONTENTPLAYHEAD)),t.ERRORCODE&&!/^[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 a=e[i];if("string"==typeof a){for(var n in t){var s=t[n],o="["+n+"]",l="%%"+n+"%%";a=(a=a.replace(o,s)).replace(l,s)}r.push(a)}}return r}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(e){return"0"}).join("")+e:e}function range(e,t,r){for(var i=[],a=en;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)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];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&&(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&&(null==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(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))}null==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&&(null==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];switch(a.nodeName){case"Linear":var n=parseCreativeLinear(a,t);n&&r.creatives.push(n);break;case"NonLinearAds":var s=parseCreativeNonLinear(a,t);s&&r.creatives.push(s);break;case"CompanionAds":var o=parseCreativeCompanion(a,t);o&&r.creatives.push(o)}}});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];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&&(t.videoClickTrackingURLTemplates||(t.videoClickTrackingURLTemplates=[]),e.videoClickTrackingURLTemplates.forEach(function(e){t.videoClickTrackingURLTemplates.push(e)})),e.videoClickThroughURLTemplate&&(t.videoClickThroughURLTemplate=e.videoClickThroughURLTemplate),e.videoCustomClickURLTemplates&&(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,request.open("GET",e),request.timeout=t.timeout||0,request.withCredentials=t.withCredentials||!1,request.send(),request.onprogress=function(){},request.onload=function(){i.loadXML(request.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){var r=void 0;n+=e,clearTimeout(r),r=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 console.log("Error in XHRURLHandler support check:",e),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=[]},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;a0&&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||urlHandler}},{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(),this.completeWrapperResolving(t),t}},{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;if(!e||!e.documentElement||"VAST"!==e.documentElement.nodeName)return Promise.reject(new Error("Invalid VAST XMLDocument"));var h=[],d=e.documentElement.childNodes;for(var m in d){var v=d[m];if("Error"===v.nodeName){var f=parserUtils.parseNodeText(v);p?this.rootErrorURLTemplates.push(f):this.errorURLTemplates.push(f)}if("Ad"===v.nodeName){var g=parseAd(v);g?h.push(g):this.trackVastError(this.getErrorURLTemplates(),{ERRORCODE:101})}}var T=h.length,k=h[T-1];return 1===T&&void 0!==n&&null!==n&&k&&!k.sequence&&(k.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,n){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 s=e.sequence;r=e.nextWrapperURL,i.fetchVAST(e.nextWrapperURL,t,r).then(function(n){return i.parse(n,{originalUrl:r,wrapperSequence:s,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 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=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(s3&&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)}),e0&&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]:{};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)}},{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; diff --git a/dist/vast-client.js b/dist/vast-client.js index 01174099..cecc7816 100644 --- a/dist/vast-client.js +++ b/dist/vast-client.js @@ -1611,11 +1611,11 @@ var VAST = (function (exports) { } function xdr() { - var xdr = void 0; + var request = void 0; if (window.XDomainRequest) { - xdr = new XDomainRequest(); + request = new XDomainRequest(); } - return xdr; + return request; } function supported() { @@ -1630,16 +1630,14 @@ var VAST = (function (exports) { } else { return cb(new Error('FlashURLHandler: Microsoft.XMLDOM format not supported')); } + request.open('GET', url); + request.timeout = options.timeout || 0; + request.withCredentials = options.withCredentials || false; + request.send(); + request.onprogress = function () {}; - var xdr = xdr(); - xdr.open('GET', url); - xdr.timeout = options.timeout || 0; - xdr.withCredentials = options.withCredentials || false; - xdr.send(); - xdr.onprogress = function () {}; - - xdr.onload = function () { - xmlDocument.loadXML(xdr.responseText); + request.onload = function () { + xmlDocument.loadXML(request.responseText); cb(null, xmlDocument); }; } @@ -1662,10 +1660,10 @@ var VAST = (function (exports) { function xhr() { try { - var _xhr = new window.XMLHttpRequest(); - if ('withCredentials' in _xhr) { + var request = new window.XMLHttpRequest(); + if ('withCredentials' in request) { // check CORS support - return _xhr; + return request; } return null; } catch (err) { @@ -1684,21 +1682,22 @@ var VAST = (function (exports) { } try { - var _xhr2 = _xhr2(); - _xhr2.open('GET', url); - _xhr2.timeout = options.timeout || 0; - _xhr2.withCredentials = options.withCredentials || false; - _xhr2.overrideMimeType && _xhr2.overrideMimeType('text/xml'); - _xhr2.onreadystatechange = function () { - if (_xhr2.readyState === 4) { - if (_xhr2.status === 200) { - cb(null, _xhr2.responseXML); + var request = xhr(); + + request.open('GET', url); + request.timeout = options.timeout || 0; + request.withCredentials = options.withCredentials || false; + request.overrideMimeType && request.overrideMimeType('text/xml'); + request.onreadystatechange = function () { + if (request.readyState === 4) { + if (request.status === 200) { + cb(null, request.responseXML); } else { - cb(new Error('XHRURLHandler: ' + _xhr2.statusText)); + cb(new Error('XHRURLHandler: ' + request.statusText)); } } }; - _xhr2.send(); + request.send(); } catch (error) { cb(new Error('XHRURLHandler: Unexpected error')); } diff --git a/dist/vast-client.min.js b/dist/vast-client.min.js index e7063fda..1b4fa3b6 100644 --- a/dist/vast-client.min.js +++ b/dist/vast-client.min.js @@ -1 +1 @@ -var VAST=function(e){"use strict";var t=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},i=function(){function e(e,t){for(var i=0;i0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),this.id=i.id||null,this.adId=i.adId||null,this.sequence=i.sequence||null,this.apiFramework=i.apiFramework||null,this.trackingEvents={}},u=function(e){function i(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,i);var r=n(this,(i.__proto__||Object.getPrototypeOf(i)).call(this,e));return r.type="companion",r.variations=[],r}return r(i,c),i}();function h(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=[];for(var r in t.ASSETURI&&(t.ASSETURI=p(t.ASSETURI)),t.CONTENTPLAYHEAD&&(t.CONTENTPLAYHEAD=p(t.CONTENTPLAYHEAD)),t.ERRORCODE&&!/^[0-9]{3}$/.test(t.ERRORCODE)&&(t.ERRORCODE=900),t.CACHEBUSTING=d(Math.round(1e8*Math.random()).toString()),t.TIMESTAMP=p((new Date).toISOString()),t.RANDOM=t.random=t.CACHEBUSTING,e){var n=e[r];if("string"==typeof n){for(var a in t){var s=t[a],o="["+a+"]",l="%%"+a+"%%";n=(n=n.replace(o,s)).replace(l,s)}i.push(n)}}return i}function p(e){return encodeURIComponent(e).replace(/[!'()*]/g,function(e){return"%"+e.charCodeAt(0).toString(16)})}function d(e){return e.length<8?f(0,8-e.length,!1).map(function(e){return"0"}).join("")+e:e}function f(e,t,i){for(var r=[],n=ea;n?s++:s--)r.push(s);return r}var v={track:function(e,t){h(e,t).forEach(function(e){"undefined"!=typeof window&&null!==window&&((new Image).src=e)})},resolveURLTemplates:h,encodeURIComponentRFC3986:p,leftpad:d,range:f,isNumeric:function(e){return!isNaN(parseFloat(e))&&isFinite(e)},flatten:function e(t){return t.reduce(function(t,i){return t.concat(Array.isArray(i)?e(i):i)},[])}};var m={childByName:function(e,t){var i=e.childNodes;for(var r in i){var n=i[r];if(n.nodeName===t)return n}},childrenByName:function(e,t){var i=[],r=e.childNodes;for(var n in r){var a=r[n];a.nodeName===t&&i.push(a)}return i},resolveVastAdTagURI:function(e,t){return t?0===e.indexOf("//")?""+location.protocol+e:-1===e.indexOf("://")?t.slice(0,t.lastIndexOf("/"))+"/"+e:e:e},parseBoolean:function(e){return-1!==["true","TRUE","1"].indexOf(e)},parseNodeText:function(e){return e&&(e.textContent||e.text||"").trim()},copyNodeAttribute:function(e,t,i){var r=t.getAttribute(e);r&&i.setAttribute(e,r)},parseDuration:function(e){if(null==e)return-1;if(v.isNumeric(e))return parseInt(e);var t=e.split(":");if(3!==t.length)return-1;var i=t[2].split("."),r=parseInt(i[0]);2===i.length&&(r+=parseFloat("0."+i[1]));var n=parseInt(60*t[1]),a=parseInt(60*t[0]*60);return isNaN(a)||isNaN(n)||isNaN(r)||n>3600||r>60?-1:a+n+r},splitVAST:function(e){var t=[],i=null;return e.forEach(function(r,n){if(r.sequence&&(r.sequence=parseInt(r.sequence,10)),r.sequence>1){var a=e[n-1];if(a&&a.sequence===r.sequence-1)return void(i&&i.push(r));delete r.sequence}i=[r],t.push(i)}),t},mergeWrapperAdData:function(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 i in t.trackingEvents[e.type]){var r=t.trackingEvents[e.type][i];e.trackingEvents[i]||(e.trackingEvents[i]=[]),e.trackingEvents[i]=e.trackingEvents[i].concat(r)}}),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&&(e.videoClickThroughURLTemplate=t.videoClickThroughURLTemplate)})}};function g(e,t){var i=new u(t);return m.childrenByName(e,"Companion").forEach(function(e){var t=new l;t.id=e.getAttribute("id")||null,t.width=e.getAttribute("width"),t.height=e.getAttribute("height"),t.companionClickTrackingURLTemplates=[],m.childrenByName(e,"HTMLResource").forEach(function(e){t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=m.parseNodeText(e)}),m.childrenByName(e,"IFrameResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.iframeResource=m.parseNodeText(e)}),m.childrenByName(e,"StaticResource").forEach(function(i){t.type=i.getAttribute("creativeType")||0,m.childrenByName(e,"AltText").forEach(function(e){t.altText=m.parseNodeText(e)}),t.staticResource=m.parseNodeText(i)}),m.childrenByName(e,"TrackingEvents").forEach(function(e){m.childrenByName(e,"Tracking").forEach(function(e){var i=e.getAttribute("event"),r=m.parseNodeText(e);i&&r&&(null==t.trackingEvents[i]&&(t.trackingEvents[i]=[]),t.trackingEvents[i].push(r))})}),m.childrenByName(e,"CompanionClickTracking").forEach(function(e){t.companionClickTrackingURLTemplates.push(m.parseNodeText(e))}),t.companionClickThroughURLTemplate=m.parseNodeText(m.childByName(e,"CompanionClickThrough")),t.companionClickTrackingURLTemplate=m.parseNodeText(m.childByName(e,"CompanionClickTracking")),i.variations.push(t)}),i}var T=function(e){function i(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,i);var r=n(this,(i.__proto__||Object.getPrototypeOf(i)).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 r(i,c),i}(),k=function e(){t(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},y=function e(){t(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 R(e,t){var i=void 0,r=new T(t);r.duration=m.parseDuration(m.parseNodeText(m.childByName(e,"Duration")));var n=e.getAttribute("skipoffset");if(null==n)r.skipDelay=null;else if("%"===n.charAt(n.length-1)&&-1!==r.duration){var a=parseInt(n,10);r.skipDelay=r.duration*(a/100)}else r.skipDelay=m.parseDuration(n);var s=m.childByName(e,"VideoClicks");s&&(r.videoClickThroughURLTemplate=m.parseNodeText(m.childByName(s,"ClickThrough")),m.childrenByName(s,"ClickTracking").forEach(function(e){r.videoClickTrackingURLTemplates.push(m.parseNodeText(e))}),m.childrenByName(s,"CustomClick").forEach(function(e){r.videoCustomClickURLTemplates.push(m.parseNodeText(e))}));var o=m.childByName(e,"AdParameters");o&&(r.adParameters=m.parseNodeText(o)),m.childrenByName(e,"TrackingEvents").forEach(function(e){m.childrenByName(e,"Tracking").forEach(function(e){var t=e.getAttribute("event"),n=m.parseNodeText(e);if(t&&n){if("progress"===t){if(!(i=e.getAttribute("offset")))return;t="%"===i.charAt(i.length-1)?"progress-"+i:"progress-"+Math.round(m.parseDuration(i))}null==r.trackingEvents[t]&&(r.trackingEvents[t]=[]),r.trackingEvents[t].push(n)}})}),m.childrenByName(e,"MediaFiles").forEach(function(e){m.childrenByName(e,"MediaFile").forEach(function(e){var t=new y;t.id=e.getAttribute("id"),t.fileURL=m.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 i=e.getAttribute("scalable");i&&"string"==typeof i&&("true"===(i=i.toLowerCase())?t.scalable=!0:"false"===i&&(t.scalable=!1));var n=e.getAttribute("maintainAspectRatio");n&&"string"==typeof n&&("true"===(n=n.toLowerCase())?t.maintainAspectRatio=!0:"false"===n&&(t.maintainAspectRatio=!1)),r.mediaFiles.push(t)})});var l=m.childByName(e,"Icons");return l&&m.childrenByName(l,"Icon").forEach(function(e){var t=new k;t.program=e.getAttribute("program"),t.height=parseInt(e.getAttribute("height")||0),t.width=parseInt(e.getAttribute("width")||0),t.xPosition=function(e){if(-1!==["left","right"].indexOf(e))return e;return parseInt(e||0)}(e.getAttribute("xPosition")),t.yPosition=function(e){if(-1!==["top","bottom"].indexOf(e))return e;return parseInt(e||0)}(e.getAttribute("yPosition")),t.apiFramework=e.getAttribute("apiFramework"),t.offset=m.parseDuration(e.getAttribute("offset")),t.duration=m.parseDuration(e.getAttribute("duration")),m.childrenByName(e,"HTMLResource").forEach(function(e){t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=m.parseNodeText(e)}),m.childrenByName(e,"IFrameResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.iframeResource=m.parseNodeText(e)}),m.childrenByName(e,"StaticResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.staticResource=m.parseNodeText(e)});var i=m.childByName(e,"IconClicks");i&&(t.iconClickThroughURLTemplate=m.parseNodeText(m.childByName(i,"IconClickThrough")),m.childrenByName(i,"IconClickTracking").forEach(function(e){t.iconClickTrackingURLTemplates.push(m.parseNodeText(e))})),t.iconViewTrackingURLTemplate=m.parseNodeText(m.childByName(e,"IconViewTracking")),r.icons.push(t)}),r}var L=function(e){function i(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,i);var r=n(this,(i.__proto__||Object.getPrototypeOf(i)).call(this,e));return r.type="nonlinear",r.variations=[],r}return r(i,c),i}(),A=function e(){t(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 E(e,t){var i=new L(t);return m.childrenByName(e,"TrackingEvents").forEach(function(e){var t=void 0,r=void 0;m.childrenByName(e,"Tracking").forEach(function(e){t=e.getAttribute("event"),r=m.parseNodeText(e),t&&r&&(null==i.trackingEvents[t]&&(i.trackingEvents[t]=[]),i.trackingEvents[t].push(r))})}),m.childrenByName(e,"NonLinear").forEach(function(e){var t=new A;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=m.parseBoolean(e.getAttribute("scalable")),t.maintainAspectRatio=m.parseBoolean(e.getAttribute("maintainAspectRatio")),t.minSuggestedDuration=m.parseDuration(e.getAttribute("minSuggestedDuration")),t.apiFramework=e.getAttribute("apiFramework"),m.childrenByName(e,"HTMLResource").forEach(function(e){t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=m.parseNodeText(e)}),m.childrenByName(e,"IFrameResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.iframeResource=m.parseNodeText(e)}),m.childrenByName(e,"StaticResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.staticResource=m.parseNodeText(e)});var r=m.childByName(e,"AdParameters");r&&(t.adParameters=m.parseNodeText(r)),t.nonlinearClickThroughURLTemplate=m.parseNodeText(m.childByName(e,"NonLinearClickThrough")),m.childrenByName(e,"NonLinearClickTracking").forEach(function(e){t.nonlinearClickTrackingURLTemplates.push(m.parseNodeText(e))}),i.variations.push(t)}),i}function U(e){var t=e.childNodes;for(var i in t){var r=t[i];if(-1!==["Wrapper","InLine"].indexOf(r.nodeName)){if(m.copyNodeAttribute("id",e,r),m.copyNodeAttribute("sequence",e,r),"Wrapper"===r.nodeName)return C(r);if("InLine"===r.nodeName)return w(r)}}}function w(e){var t=e.childNodes,i=new a;for(var r in i.id=e.getAttribute("id")||null,i.sequence=e.getAttribute("sequence")||null,t){var n=t[r];switch(n.nodeName){case"Error":i.errorURLTemplates.push(m.parseNodeText(n));break;case"Impression":i.impressionURLTemplates.push(m.parseNodeText(n));break;case"Creatives":m.childrenByName(n,"Creative").forEach(function(e){var t={id:e.getAttribute("id")||null,adId:N(e),sequence:e.getAttribute("sequence")||null,apiFramework:e.getAttribute("apiFramework")||null};for(var r in e.childNodes){var n=e.childNodes[r];switch(n.nodeName){case"Linear":var a=R(n,t);a&&i.creatives.push(a);break;case"NonLinearAds":var s=E(n,t);s&&i.creatives.push(s);break;case"CompanionAds":var o=g(n,t);o&&i.creatives.push(o)}}});break;case"Extensions":b(i.extensions,m.childrenByName(n,"Extension"));break;case"AdSystem":i.system={value:m.parseNodeText(n),version:n.getAttribute("version")||null};break;case"AdTitle":i.title=m.parseNodeText(n);break;case"Description":i.description=m.parseNodeText(n);break;case"Advertiser":i.advertiser=m.parseNodeText(n);break;case"Pricing":i.pricing={value:m.parseNodeText(n),model:n.getAttribute("model")||null,currency:n.getAttribute("currency")||null};break;case"Survey":i.survey=m.parseNodeText(n)}}return i}function C(e){var t=w(e),i=m.childByName(e,"VASTAdTagURI");if(i?t.nextWrapperURL=m.parseNodeText(i):(i=m.childByName(e,"VASTAdTagURL"))&&(t.nextWrapperURL=m.parseNodeText(m.childByName(i,"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 i=function(i){var r=e.trackingEvents[i];t.trackingEvents[e.type][i]||(t.trackingEvents[e.type][i]=[]),r.forEach(function(r){t.trackingEvents[e.type][i].push(r)})};for(var r in e.trackingEvents)i(r)}e.videoClickTrackingURLTemplates&&(t.videoClickTrackingURLTemplates||(t.videoClickTrackingURLTemplates=[]),e.videoClickTrackingURLTemplates.forEach(function(e){t.videoClickTrackingURLTemplates.push(e)})),e.videoClickThroughURLTemplate&&(t.videoClickThroughURLTemplate=e.videoClickThroughURLTemplate),e.videoCustomClickURLTemplates&&(t.videoCustomClickURLTemplates||(t.videoCustomClickURLTemplates=[]),e.videoCustomClickURLTemplates.forEach(function(e){t.videoCustomClickURLTemplates.push(e)}))}}),t.nextWrapperURL)return t}function b(e,t){t.forEach(function(t){var i=new s,r=t.attributes,n=t.childNodes;if(t.attributes)for(var a in r){var l=r[a];l.nodeName&&l.nodeValue&&(i.attributes[l.nodeName]=l.nodeValue)}for(var c in n){var u=n[c],h=m.parseNodeText(u);if("#comment"!==u.nodeName&&""!==h){var p=new o;if(p.name=u.nodeName,p.value=h,u.attributes){var d=u.attributes;for(var f in d){var v=d[f];p.attributes[v.nodeName]=v.nodeValue}}i.children.push(p)}}e.push(i)})}function N(e){return e.getAttribute("AdID")||e.getAttribute("adID")||e.getAttribute("adId")||null}function x(){}function S(){S.init.call(this)}function I(e){return void 0===e._maxListeners?S.defaultMaxListeners:e._maxListeners}function D(e,t,i,r){var n,a,s,o;if("function"!=typeof i)throw new TypeError('"listener" argument must be a function');if((a=e._events)?(a.newListener&&(e.emit("newListener",t,i.listener?i.listener:i),a=e._events),s=a[t]):(a=e._events=new x,e._eventsCount=0),s){if("function"==typeof s?s=a[t]=r?[i,s]:[s,i]:r?s.unshift(i):s.push(i),!s.warned&&(n=I(e))&&n>0&&s.length>n){s.warned=!0;var l=new Error("Possible EventEmitter memory leak detected. "+s.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");l.name="MaxListenersExceededWarning",l.emitter=e,l.type=t,l.count=s.length,o=l,"function"==typeof console.warn?console.warn(o):console.log(o)}}else s=a[t]=i,++e._eventsCount;return e}function O(e,t,i){var r=!1;function n(){e.removeListener(t,n),r||(r=!0,i.apply(e,arguments))}return n.listener=i,n}function _(e){var t=this._events;if(t){var i=t[e];if("function"==typeof i)return 1;if(i)return i.length}return 0}function B(e,t){for(var i=new Array(t);t--;)i[t]=e[t];return i}x.prototype=Object.create(null),S.EventEmitter=S,S.usingDomains=!1,S.prototype.domain=void 0,S.prototype._events=void 0,S.prototype._maxListeners=void 0,S.defaultMaxListeners=10,S.init=function(){this.domain=null,S.usingDomains&&(!(void 0).active||this instanceof(void 0).Domain||(this.domain=(void 0).active)),this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new x,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},S.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},S.prototype.getMaxListeners=function(){return I(this)},S.prototype.emit=function(e){var t,i,r,n,a,s,o,l="error"===e;if(s=this._events)l=l&&null==s.error;else if(!l)return!1;if(o=this.domain,l){if(t=arguments[1],!o){if(t instanceof Error)throw t;var c=new Error('Uncaught, unspecified "error" event. ('+t+")");throw c.context=t,c}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=o,t.domainThrown=!1,o.emit("error",t),!1}if(!(i=s[e]))return!1;var u="function"==typeof i;switch(r=arguments.length){case 1:!function(e,t,i){if(t)e.call(i);else for(var r=e.length,n=B(e,r),a=0;a0;)if(i[a]===t||i[a].listener&&i[a].listener===t){s=i[a].listener,n=a;break}if(n<0)return this;if(1===i.length){if(i[0]=void 0,0==--this._eventsCount)return this._events=new x,this;delete r[e]}else!function(e,t){for(var i=t,r=i+1,n=e.length;r0?Reflect.ownKeys(this._events):[]};var P={get:function(e,t,i){var r="function"==typeof window.ActiveXObject?new window.ActiveXObject("Microsoft.XMLDOM"):void 0;if(!r)return i(new Error("FlashURLHandler: Microsoft.XMLDOM format not supported"));r.async=!1;var n=n();n.open("GET",e),n.timeout=t.timeout||0,n.withCredentials=t.withCredentials||!1,n.send(),n.onprogress=function(){},n.onload=function(){r.loadXML(n.responseText),i(null,r)}},supported:function(){return!!function(){var e=void 0;return window.XDomainRequest&&(e=new XDomainRequest),e}()}};var F={get:function(e,t,i){i(new Error("Please bundle the library for node to use the node urlHandler"))}};var V={get:function(e,t,i){if("https:"===window.location.protocol&&0===e.indexOf("http://"))return i(new Error("XHRURLHandler: Cannot go from HTTPS to HTTP."));try{var r=r();r.open("GET",e),r.timeout=t.timeout||0,r.withCredentials=t.withCredentials||!1,r.overrideMimeType&&r.overrideMimeType("text/xml"),r.onreadystatechange=function(){4===r.readyState&&(200===r.status?i(null,r.responseXML):i(new Error("XHRURLHandler: "+r.statusText)))},r.send()}catch(e){i(new Error("XHRURLHandler: Unexpected error"))}},supported:function(){return!!function(){try{var e=new window.XMLHttpRequest;return"withCredentials"in e?e:null}catch(e){return console.log("Error in XHRURLHandler support check:",e),null}}()}};var M={get:function(e,t,i){return i||("function"==typeof t&&(i=t),t={}),"undefined"==typeof window||null===window?F.get(e,t,i):V.supported()?V.get(e,t,i):P.supported()?P.get(e,t,i):i(new Error("Current context is not supported by any of the default URLHandlers. Please provide a custom URLHandler"))}},H={ERRORCODE:900,extensions:[]},W=function(e){function a(){t(this,a);var e=n(this,(a.__proto__||Object.getPrototypeOf(a)).call(this));return e.remainingAds=[],e.parentURLs=[],e.errorURLTemplates=[],e.rootErrorURLTemplates=[],e.maxWrapperDepth=null,e.URLTemplateFilters=[],e.fetchingOptions={},e}return r(a,S),i(a,[{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 i=arguments.length,r=Array(i>2?i-2:0),n=2;n0&&void 0!==arguments[0]?arguments[0]:{};this.rootURL="",this.remainingAds=[],this.parentURLs=[],this.errorURLTemplates=[],this.rootErrorURLTemplates=[],this.maxWrapperDepth=e.wrapperLimit||10,this.fetchingOptions={timeout:e.timeout,withCredentials:e.withCredentials},this.urlHandler=e.urlhandler||M}},{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 i=e?v.flatten(this.remainingAds):this.remainingAds.shift();return this.errorURLTemplates=[],this.parentURLs=[],this.resolveAds(i,{wrapperDepth:0,originalUrl:this.rootURL}).then(function(e){return t.buildVASTResponse(e)})}},{key:"getAndParseVAST",value:function(e){var t=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return this.initParsingStatus(i),this.rootURL=e,this.fetchVAST(e).then(function(r){return i.originalUrl=e,i.isRootVAST=!0,t.parse(r,i).then(function(e){return t.buildVASTResponse(e)})})}},{key:"parseVAST",value:function(e){var t=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return this.initParsingStatus(i),i.isRootVAST=!0,this.parse(e,i).then(function(e){return t.buildVASTResponse(e)})}},{key:"buildVASTResponse",value:function(e){var i=new function e(){t(this,e),this.ads=[],this.errorURLTemplates=[]};return i.ads=e,i.errorURLTemplates=this.getErrorURLTemplates(),this.completeWrapperResolving(i),i}},{key:"parse",value:function(e,t){var i=t.resolveAll,r=void 0===i||i,n=t.wrapperSequence,a=void 0===n?null:n,s=t.originalUrl,o=void 0===s?null:s,l=t.wrapperDepth,c=void 0===l?0:l,u=t.isRootVAST,h=void 0!==u&&u;if(!e||!e.documentElement||"VAST"!==e.documentElement.nodeName)return Promise.reject(new Error("Invalid VAST XMLDocument"));var p=[],d=e.documentElement.childNodes;for(var f in d){var v=d[f];if("Error"===v.nodeName){var g=m.parseNodeText(v);h?this.rootErrorURLTemplates.push(g):this.errorURLTemplates.push(g)}if("Ad"===v.nodeName){var T=U(v);T?p.push(T):this.trackVastError(this.getErrorURLTemplates(),{ERRORCODE:101})}}var k=p.length,y=p[k-1];return 1===k&&void 0!==a&&null!==a&&y&&!y.sequence&&(y.sequence=a),!1===r&&(this.remainingAds=m.splitVAST(p),p=this.remainingAds.shift()),this.resolveAds(p,{wrapperDepth:c,originalUrl:o})}},{key:"resolveAds",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],i=arguments[1],r=i.wrapperDepth,n=i.originalUrl,a=[];return t.forEach(function(t){var i=e.resolveWrappers(t,r,n);a.push(i)}),Promise.all(a).then(function(t){var i=v.flatten(t);if(!i&&e.remainingAds.length>0){var a=e.remainingAds.shift();return e.resolveAds(a,{wrapperDepth:r,originalUrl:n})}return i})}},{key:"resolveWrappers",value:function(e,t,i){var r=this;return new Promise(function(n,a){if(t++,!e.nextWrapperURL)return delete e.nextWrapperURL,n(e);if(t>=r.maxWrapperDepth||-1!==r.parentURLs.indexOf(e.nextWrapperURL))return e.errorCode=302,delete e.nextWrapperURL,n(e);e.nextWrapperURL=m.resolveVastAdTagURI(e.nextWrapperURL,i);var s=e.sequence;i=e.nextWrapperURL,r.fetchVAST(e.nextWrapperURL,t,i).then(function(a){return r.parse(a,{originalUrl:i,wrapperSequence:s,wrapperDepth:t}).then(function(t){if(delete e.nextWrapperURL,0===t.length)return e.creatives=[],n(e);t.forEach(function(t){t&&m.mergeWrapperAdData(t,e)}),n(t)})}).catch(function(t){e.errorCode=301,e.errorMessage=t.message,n(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 i=e.ads[t];(i.errorCode||0===i.creatives.length)&&(this.trackVastError(i.errorURLTemplates.concat(e.errorURLTemplates),{ERRORCODE:i.errorCode||303},{ERRORMESSAGE:i.errorMessage||""},{extensions:i.extensions},{system:i.system}),e.ads.splice(t,1))}}}]),a}(),q=null,j={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 data[e],this.length=Object.keys(this.data).length},clear:function(){this.data={},this.length=0}},X=function(){function e(){t(this,e),this.storage=this.initStorage()}return i(e,[{key:"initStorage",value:function(){if(q)return q;try{q="undefined"!=typeof window&&null!==window?window.localStorage||window.sessionStorage:null}catch(e){q=null}return q&&!this.isStorageDisabled(q)||(q=j).clear(),q}},{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}(),Q=function(){function e(i,r,n){t(this,e),this.cappingFreeLunch=i||0,this.cappingMinimumTimeInterval=r||0,this.defaultOptions={withCredentials:!1,timeout:0},this.vastParser=new W,this.storage=n||new X,void 0===this.lastSuccessfulAd&&(this.lastSuccessfulAd=0),void 0===this.totalCalls&&(this.totalCalls=0),void 0===this.totalCallsTimeout&&(this.totalCallsTimeout=0)}return i(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,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=Date.now();return(i=Object.assign(this.defaultOptions,i)).hasOwnProperty("resolveAll")||(i.resolveAll=!1),this.totalCallsTimeout=t.totalCalls)return a(new Error("VAST call canceled – FreeLunch capping not reached yet "+t.totalCalls+"/"+t.cappingFreeLunch));var s=r-t.lastSuccessfulAd;if(s<0)t.lastSuccessfulAd=0;else if(s3&&void 0!==arguments[3]?arguments[3]:null;t(this,a);var o=n(this,(a.__proto__||Object.getPrototypeOf(a)).call(this));for(var l in o.ad=i,o.creative=r,o.variation=s,o.muted=!1,o.impressed=!1,o.skippable=!1,o.trackingEvents={},o._alreadyTriggeredQuartiles={},o.emitAlwaysEvents=["creativeView","start","firstQuartile","midpoint","thirdQuartile","complete","resume","pause","rewind","skip","closeLinear","close"],o.creative.trackingEvents){var c=o.creative.trackingEvents[l];o.trackingEvents[l]=c.slice(0)}return o.creative instanceof T?o._initLinearTracking():o._initVariationTracking(),e&&o.on("start",function(){e.lastSuccessfulAd=Date.now()}),o}return r(a,S),i(a,[{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=-1,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 A?(this.clickThroughURLTemplate=this.variation.nonlinearClickThroughURLTemplate,this.clickTrackingURLTemplates=this.variation.nonlinearClickTrackingURLTemplates,this.setDuration(this.variation.minSuggestedDuration)):this.variation instanceof l&&(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,i=this.skipDelay||-1;if(-1===i||this.skippable||(i>e?this.emit("skip-countdown",i-e):(this.skippable=!0,this.emit("skip-countdown",0))),this.assetDuration>0){var r=[];if(e>0){var n=Math.round(e/this.assetDuration*100);for(var a in r.push("start"),r.push("progress-"+n+"%"),r.push("progress-"+Math.round(e)),this.quartiles)this.isQuartileReached(a,this.quartiles[a],e)&&(r.push(a),this._alreadyTriggeredQuartiles[a]=!0)}r.forEach(function(e){t.track(e,!0)}),e0&&void 0!==arguments[0]?arguments[0]:null;this.clickTrackingURLTemplates&&this.clickTrackingURLTemplates.length&&this.trackURLs(this.clickTrackingURLTemplates);var t=this.clickThroughURLTemplate||e;if(t){var i=this.linear?{CONTENTPLAYHEAD:this.progressFormatted()}:{},r=v.resolveURLTemplates([t],i)[0];this.emit("clickthrough",r)}}},{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 i=this.trackingEvents[e],r=this.emitAlwaysEvents.indexOf(e)>-1;i?(this.emit(e,""),this.trackURLs(i)):r&&this.emit(e,""),t&&(delete this.trackingEvents[e],r&&this.emitAlwaysEvents.splice(this.emitAlwaysEvents.indexOf(e),1))}},{key:"trackURLs",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};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()),v.track(e,t)}},{key:"progressFormatted",value:function(){var e=parseInt(this.progress),t=e/3600;t.length<2&&(t="0"+t);var i=e/60%60;i.length<2&&(i="0"+i);var r=e%60;return r.length<2&&(r="0"+i),t+":"+i+":"+r+"."+parseInt(100*(this.progress-e))}}]),a}();return e.VASTClient=Q,e.VASTParser=W,e.VASTTracker=G,e}({}); +var VAST=function(e){"use strict";var t=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},i=function(){function e(e,t){for(var i=0;i0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),this.id=i.id||null,this.adId=i.adId||null,this.sequence=i.sequence||null,this.apiFramework=i.apiFramework||null,this.trackingEvents={}},u=function(e){function i(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,i);var r=n(this,(i.__proto__||Object.getPrototypeOf(i)).call(this,e));return r.type="companion",r.variations=[],r}return r(i,c),i}();function h(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=[];for(var r in t.ASSETURI&&(t.ASSETURI=p(t.ASSETURI)),t.CONTENTPLAYHEAD&&(t.CONTENTPLAYHEAD=p(t.CONTENTPLAYHEAD)),t.ERRORCODE&&!/^[0-9]{3}$/.test(t.ERRORCODE)&&(t.ERRORCODE=900),t.CACHEBUSTING=d(Math.round(1e8*Math.random()).toString()),t.TIMESTAMP=p((new Date).toISOString()),t.RANDOM=t.random=t.CACHEBUSTING,e){var n=e[r];if("string"==typeof n){for(var a in t){var s=t[a],o="["+a+"]",l="%%"+a+"%%";n=(n=n.replace(o,s)).replace(l,s)}i.push(n)}}return i}function p(e){return encodeURIComponent(e).replace(/[!'()*]/g,function(e){return"%"+e.charCodeAt(0).toString(16)})}function d(e){return e.length<8?f(0,8-e.length,!1).map(function(e){return"0"}).join("")+e:e}function f(e,t,i){for(var r=[],n=ea;n?s++:s--)r.push(s);return r}var v={track:function(e,t){h(e,t).forEach(function(e){"undefined"!=typeof window&&null!==window&&((new Image).src=e)})},resolveURLTemplates:h,encodeURIComponentRFC3986:p,leftpad:d,range:f,isNumeric:function(e){return!isNaN(parseFloat(e))&&isFinite(e)},flatten:function e(t){return t.reduce(function(t,i){return t.concat(Array.isArray(i)?e(i):i)},[])}};var m={childByName:function(e,t){var i=e.childNodes;for(var r in i){var n=i[r];if(n.nodeName===t)return n}},childrenByName:function(e,t){var i=[],r=e.childNodes;for(var n in r){var a=r[n];a.nodeName===t&&i.push(a)}return i},resolveVastAdTagURI:function(e,t){return t?0===e.indexOf("//")?""+location.protocol+e:-1===e.indexOf("://")?t.slice(0,t.lastIndexOf("/"))+"/"+e:e:e},parseBoolean:function(e){return-1!==["true","TRUE","1"].indexOf(e)},parseNodeText:function(e){return e&&(e.textContent||e.text||"").trim()},copyNodeAttribute:function(e,t,i){var r=t.getAttribute(e);r&&i.setAttribute(e,r)},parseDuration:function(e){if(null==e)return-1;if(v.isNumeric(e))return parseInt(e);var t=e.split(":");if(3!==t.length)return-1;var i=t[2].split("."),r=parseInt(i[0]);2===i.length&&(r+=parseFloat("0."+i[1]));var n=parseInt(60*t[1]),a=parseInt(60*t[0]*60);return isNaN(a)||isNaN(n)||isNaN(r)||n>3600||r>60?-1:a+n+r},splitVAST:function(e){var t=[],i=null;return e.forEach(function(r,n){if(r.sequence&&(r.sequence=parseInt(r.sequence,10)),r.sequence>1){var a=e[n-1];if(a&&a.sequence===r.sequence-1)return void(i&&i.push(r));delete r.sequence}i=[r],t.push(i)}),t},mergeWrapperAdData:function(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 i in t.trackingEvents[e.type]){var r=t.trackingEvents[e.type][i];e.trackingEvents[i]||(e.trackingEvents[i]=[]),e.trackingEvents[i]=e.trackingEvents[i].concat(r)}}),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&&(e.videoClickThroughURLTemplate=t.videoClickThroughURLTemplate)})}};function g(e,t){var i=new u(t);return m.childrenByName(e,"Companion").forEach(function(e){var t=new l;t.id=e.getAttribute("id")||null,t.width=e.getAttribute("width"),t.height=e.getAttribute("height"),t.companionClickTrackingURLTemplates=[],m.childrenByName(e,"HTMLResource").forEach(function(e){t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=m.parseNodeText(e)}),m.childrenByName(e,"IFrameResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.iframeResource=m.parseNodeText(e)}),m.childrenByName(e,"StaticResource").forEach(function(i){t.type=i.getAttribute("creativeType")||0,m.childrenByName(e,"AltText").forEach(function(e){t.altText=m.parseNodeText(e)}),t.staticResource=m.parseNodeText(i)}),m.childrenByName(e,"TrackingEvents").forEach(function(e){m.childrenByName(e,"Tracking").forEach(function(e){var i=e.getAttribute("event"),r=m.parseNodeText(e);i&&r&&(null==t.trackingEvents[i]&&(t.trackingEvents[i]=[]),t.trackingEvents[i].push(r))})}),m.childrenByName(e,"CompanionClickTracking").forEach(function(e){t.companionClickTrackingURLTemplates.push(m.parseNodeText(e))}),t.companionClickThroughURLTemplate=m.parseNodeText(m.childByName(e,"CompanionClickThrough")),t.companionClickTrackingURLTemplate=m.parseNodeText(m.childByName(e,"CompanionClickTracking")),i.variations.push(t)}),i}var T=function(e){function i(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,i);var r=n(this,(i.__proto__||Object.getPrototypeOf(i)).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 r(i,c),i}(),k=function e(){t(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},y=function e(){t(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 R(e,t){var i=void 0,r=new T(t);r.duration=m.parseDuration(m.parseNodeText(m.childByName(e,"Duration")));var n=e.getAttribute("skipoffset");if(null==n)r.skipDelay=null;else if("%"===n.charAt(n.length-1)&&-1!==r.duration){var a=parseInt(n,10);r.skipDelay=r.duration*(a/100)}else r.skipDelay=m.parseDuration(n);var s=m.childByName(e,"VideoClicks");s&&(r.videoClickThroughURLTemplate=m.parseNodeText(m.childByName(s,"ClickThrough")),m.childrenByName(s,"ClickTracking").forEach(function(e){r.videoClickTrackingURLTemplates.push(m.parseNodeText(e))}),m.childrenByName(s,"CustomClick").forEach(function(e){r.videoCustomClickURLTemplates.push(m.parseNodeText(e))}));var o=m.childByName(e,"AdParameters");o&&(r.adParameters=m.parseNodeText(o)),m.childrenByName(e,"TrackingEvents").forEach(function(e){m.childrenByName(e,"Tracking").forEach(function(e){var t=e.getAttribute("event"),n=m.parseNodeText(e);if(t&&n){if("progress"===t){if(!(i=e.getAttribute("offset")))return;t="%"===i.charAt(i.length-1)?"progress-"+i:"progress-"+Math.round(m.parseDuration(i))}null==r.trackingEvents[t]&&(r.trackingEvents[t]=[]),r.trackingEvents[t].push(n)}})}),m.childrenByName(e,"MediaFiles").forEach(function(e){m.childrenByName(e,"MediaFile").forEach(function(e){var t=new y;t.id=e.getAttribute("id"),t.fileURL=m.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 i=e.getAttribute("scalable");i&&"string"==typeof i&&("true"===(i=i.toLowerCase())?t.scalable=!0:"false"===i&&(t.scalable=!1));var n=e.getAttribute("maintainAspectRatio");n&&"string"==typeof n&&("true"===(n=n.toLowerCase())?t.maintainAspectRatio=!0:"false"===n&&(t.maintainAspectRatio=!1)),r.mediaFiles.push(t)})});var l=m.childByName(e,"Icons");return l&&m.childrenByName(l,"Icon").forEach(function(e){var t=new k;t.program=e.getAttribute("program"),t.height=parseInt(e.getAttribute("height")||0),t.width=parseInt(e.getAttribute("width")||0),t.xPosition=function(e){if(-1!==["left","right"].indexOf(e))return e;return parseInt(e||0)}(e.getAttribute("xPosition")),t.yPosition=function(e){if(-1!==["top","bottom"].indexOf(e))return e;return parseInt(e||0)}(e.getAttribute("yPosition")),t.apiFramework=e.getAttribute("apiFramework"),t.offset=m.parseDuration(e.getAttribute("offset")),t.duration=m.parseDuration(e.getAttribute("duration")),m.childrenByName(e,"HTMLResource").forEach(function(e){t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=m.parseNodeText(e)}),m.childrenByName(e,"IFrameResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.iframeResource=m.parseNodeText(e)}),m.childrenByName(e,"StaticResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.staticResource=m.parseNodeText(e)});var i=m.childByName(e,"IconClicks");i&&(t.iconClickThroughURLTemplate=m.parseNodeText(m.childByName(i,"IconClickThrough")),m.childrenByName(i,"IconClickTracking").forEach(function(e){t.iconClickTrackingURLTemplates.push(m.parseNodeText(e))})),t.iconViewTrackingURLTemplate=m.parseNodeText(m.childByName(e,"IconViewTracking")),r.icons.push(t)}),r}var L=function(e){function i(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,i);var r=n(this,(i.__proto__||Object.getPrototypeOf(i)).call(this,e));return r.type="nonlinear",r.variations=[],r}return r(i,c),i}(),A=function e(){t(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 E(e,t){var i=new L(t);return m.childrenByName(e,"TrackingEvents").forEach(function(e){var t=void 0,r=void 0;m.childrenByName(e,"Tracking").forEach(function(e){t=e.getAttribute("event"),r=m.parseNodeText(e),t&&r&&(null==i.trackingEvents[t]&&(i.trackingEvents[t]=[]),i.trackingEvents[t].push(r))})}),m.childrenByName(e,"NonLinear").forEach(function(e){var t=new A;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=m.parseBoolean(e.getAttribute("scalable")),t.maintainAspectRatio=m.parseBoolean(e.getAttribute("maintainAspectRatio")),t.minSuggestedDuration=m.parseDuration(e.getAttribute("minSuggestedDuration")),t.apiFramework=e.getAttribute("apiFramework"),m.childrenByName(e,"HTMLResource").forEach(function(e){t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=m.parseNodeText(e)}),m.childrenByName(e,"IFrameResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.iframeResource=m.parseNodeText(e)}),m.childrenByName(e,"StaticResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.staticResource=m.parseNodeText(e)});var r=m.childByName(e,"AdParameters");r&&(t.adParameters=m.parseNodeText(r)),t.nonlinearClickThroughURLTemplate=m.parseNodeText(m.childByName(e,"NonLinearClickThrough")),m.childrenByName(e,"NonLinearClickTracking").forEach(function(e){t.nonlinearClickTrackingURLTemplates.push(m.parseNodeText(e))}),i.variations.push(t)}),i}function U(e){var t=e.childNodes;for(var i in t){var r=t[i];if(-1!==["Wrapper","InLine"].indexOf(r.nodeName)){if(m.copyNodeAttribute("id",e,r),m.copyNodeAttribute("sequence",e,r),"Wrapper"===r.nodeName)return C(r);if("InLine"===r.nodeName)return w(r)}}}function w(e){var t=e.childNodes,i=new a;for(var r in i.id=e.getAttribute("id")||null,i.sequence=e.getAttribute("sequence")||null,t){var n=t[r];switch(n.nodeName){case"Error":i.errorURLTemplates.push(m.parseNodeText(n));break;case"Impression":i.impressionURLTemplates.push(m.parseNodeText(n));break;case"Creatives":m.childrenByName(n,"Creative").forEach(function(e){var t={id:e.getAttribute("id")||null,adId:N(e),sequence:e.getAttribute("sequence")||null,apiFramework:e.getAttribute("apiFramework")||null};for(var r in e.childNodes){var n=e.childNodes[r];switch(n.nodeName){case"Linear":var a=R(n,t);a&&i.creatives.push(a);break;case"NonLinearAds":var s=E(n,t);s&&i.creatives.push(s);break;case"CompanionAds":var o=g(n,t);o&&i.creatives.push(o)}}});break;case"Extensions":b(i.extensions,m.childrenByName(n,"Extension"));break;case"AdSystem":i.system={value:m.parseNodeText(n),version:n.getAttribute("version")||null};break;case"AdTitle":i.title=m.parseNodeText(n);break;case"Description":i.description=m.parseNodeText(n);break;case"Advertiser":i.advertiser=m.parseNodeText(n);break;case"Pricing":i.pricing={value:m.parseNodeText(n),model:n.getAttribute("model")||null,currency:n.getAttribute("currency")||null};break;case"Survey":i.survey=m.parseNodeText(n)}}return i}function C(e){var t=w(e),i=m.childByName(e,"VASTAdTagURI");if(i?t.nextWrapperURL=m.parseNodeText(i):(i=m.childByName(e,"VASTAdTagURL"))&&(t.nextWrapperURL=m.parseNodeText(m.childByName(i,"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 i=function(i){var r=e.trackingEvents[i];t.trackingEvents[e.type][i]||(t.trackingEvents[e.type][i]=[]),r.forEach(function(r){t.trackingEvents[e.type][i].push(r)})};for(var r in e.trackingEvents)i(r)}e.videoClickTrackingURLTemplates&&(t.videoClickTrackingURLTemplates||(t.videoClickTrackingURLTemplates=[]),e.videoClickTrackingURLTemplates.forEach(function(e){t.videoClickTrackingURLTemplates.push(e)})),e.videoClickThroughURLTemplate&&(t.videoClickThroughURLTemplate=e.videoClickThroughURLTemplate),e.videoCustomClickURLTemplates&&(t.videoCustomClickURLTemplates||(t.videoCustomClickURLTemplates=[]),e.videoCustomClickURLTemplates.forEach(function(e){t.videoCustomClickURLTemplates.push(e)}))}}),t.nextWrapperURL)return t}function b(e,t){t.forEach(function(t){var i=new s,r=t.attributes,n=t.childNodes;if(t.attributes)for(var a in r){var l=r[a];l.nodeName&&l.nodeValue&&(i.attributes[l.nodeName]=l.nodeValue)}for(var c in n){var u=n[c],h=m.parseNodeText(u);if("#comment"!==u.nodeName&&""!==h){var p=new o;if(p.name=u.nodeName,p.value=h,u.attributes){var d=u.attributes;for(var f in d){var v=d[f];p.attributes[v.nodeName]=v.nodeValue}}i.children.push(p)}}e.push(i)})}function N(e){return e.getAttribute("AdID")||e.getAttribute("adID")||e.getAttribute("adId")||null}function x(){}function S(){S.init.call(this)}function I(e){return void 0===e._maxListeners?S.defaultMaxListeners:e._maxListeners}function D(e,t,i,r){var n,a,s,o;if("function"!=typeof i)throw new TypeError('"listener" argument must be a function');if((a=e._events)?(a.newListener&&(e.emit("newListener",t,i.listener?i.listener:i),a=e._events),s=a[t]):(a=e._events=new x,e._eventsCount=0),s){if("function"==typeof s?s=a[t]=r?[i,s]:[s,i]:r?s.unshift(i):s.push(i),!s.warned&&(n=I(e))&&n>0&&s.length>n){s.warned=!0;var l=new Error("Possible EventEmitter memory leak detected. "+s.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");l.name="MaxListenersExceededWarning",l.emitter=e,l.type=t,l.count=s.length,o=l,"function"==typeof console.warn?console.warn(o):console.log(o)}}else s=a[t]=i,++e._eventsCount;return e}function O(e,t,i){var r=!1;function n(){e.removeListener(t,n),r||(r=!0,i.apply(e,arguments))}return n.listener=i,n}function _(e){var t=this._events;if(t){var i=t[e];if("function"==typeof i)return 1;if(i)return i.length}return 0}function B(e,t){for(var i=new Array(t);t--;)i[t]=e[t];return i}x.prototype=Object.create(null),S.EventEmitter=S,S.usingDomains=!1,S.prototype.domain=void 0,S.prototype._events=void 0,S.prototype._maxListeners=void 0,S.defaultMaxListeners=10,S.init=function(){this.domain=null,S.usingDomains&&(!(void 0).active||this instanceof(void 0).Domain||(this.domain=(void 0).active)),this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new x,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},S.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},S.prototype.getMaxListeners=function(){return I(this)},S.prototype.emit=function(e){var t,i,r,n,a,s,o,l="error"===e;if(s=this._events)l=l&&null==s.error;else if(!l)return!1;if(o=this.domain,l){if(t=arguments[1],!o){if(t instanceof Error)throw t;var c=new Error('Uncaught, unspecified "error" event. ('+t+")");throw c.context=t,c}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=o,t.domainThrown=!1,o.emit("error",t),!1}if(!(i=s[e]))return!1;var u="function"==typeof i;switch(r=arguments.length){case 1:!function(e,t,i){if(t)e.call(i);else for(var r=e.length,n=B(e,r),a=0;a0;)if(i[a]===t||i[a].listener&&i[a].listener===t){s=i[a].listener,n=a;break}if(n<0)return this;if(1===i.length){if(i[0]=void 0,0==--this._eventsCount)return this._events=new x,this;delete r[e]}else!function(e,t){for(var i=t,r=i+1,n=e.length;r0?Reflect.ownKeys(this._events):[]};var P={get:function(e,t,i){var r="function"==typeof window.ActiveXObject?new window.ActiveXObject("Microsoft.XMLDOM"):void 0;if(!r)return i(new Error("FlashURLHandler: Microsoft.XMLDOM format not supported"));r.async=!1,request.open("GET",e),request.timeout=t.timeout||0,request.withCredentials=t.withCredentials||!1,request.send(),request.onprogress=function(){},request.onload=function(){r.loadXML(request.responseText),i(null,r)}},supported:function(){return e=void 0,window.XDomainRequest&&(e=new XDomainRequest),!!e;var e}};var F={get:function(e,t,i){i(new Error("Please bundle the library for node to use the node urlHandler"))}};function V(){try{var e=new window.XMLHttpRequest;return"withCredentials"in e?e:null}catch(e){return console.log("Error in XHRURLHandler support check:",e),null}}var M={get:function(e,t,i){if("https:"===window.location.protocol&&0===e.indexOf("http://"))return i(new Error("XHRURLHandler: Cannot go from HTTPS to HTTP."));try{var r=V();r.open("GET",e),r.timeout=t.timeout||0,r.withCredentials=t.withCredentials||!1,r.overrideMimeType&&r.overrideMimeType("text/xml"),r.onreadystatechange=function(){4===r.readyState&&(200===r.status?i(null,r.responseXML):i(new Error("XHRURLHandler: "+r.statusText)))},r.send()}catch(e){i(new Error("XHRURLHandler: Unexpected error"))}},supported:function(){return!!V()}};var q={get:function(e,t,i){return i||("function"==typeof t&&(i=t),t={}),"undefined"==typeof window||null===window?F.get(e,t,i):M.supported()?M.get(e,t,i):P.supported()?P.get(e,t,i):i(new Error("Current context is not supported by any of the default URLHandlers. Please provide a custom URLHandler"))}},H={ERRORCODE:900,extensions:[]},W=function(e){function a(){t(this,a);var e=n(this,(a.__proto__||Object.getPrototypeOf(a)).call(this));return e.remainingAds=[],e.parentURLs=[],e.errorURLTemplates=[],e.rootErrorURLTemplates=[],e.maxWrapperDepth=null,e.URLTemplateFilters=[],e.fetchingOptions={},e}return r(a,S),i(a,[{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 i=arguments.length,r=Array(i>2?i-2:0),n=2;n0&&void 0!==arguments[0]?arguments[0]:{};this.rootURL="",this.remainingAds=[],this.parentURLs=[],this.errorURLTemplates=[],this.rootErrorURLTemplates=[],this.maxWrapperDepth=e.wrapperLimit||10,this.fetchingOptions={timeout:e.timeout,withCredentials:e.withCredentials},this.urlHandler=e.urlhandler||q}},{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 i=e?v.flatten(this.remainingAds):this.remainingAds.shift();return this.errorURLTemplates=[],this.parentURLs=[],this.resolveAds(i,{wrapperDepth:0,originalUrl:this.rootURL}).then(function(e){return t.buildVASTResponse(e)})}},{key:"getAndParseVAST",value:function(e){var t=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return this.initParsingStatus(i),this.rootURL=e,this.fetchVAST(e).then(function(r){return i.originalUrl=e,i.isRootVAST=!0,t.parse(r,i).then(function(e){return t.buildVASTResponse(e)})})}},{key:"parseVAST",value:function(e){var t=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return this.initParsingStatus(i),i.isRootVAST=!0,this.parse(e,i).then(function(e){return t.buildVASTResponse(e)})}},{key:"buildVASTResponse",value:function(e){var i=new function e(){t(this,e),this.ads=[],this.errorURLTemplates=[]};return i.ads=e,i.errorURLTemplates=this.getErrorURLTemplates(),this.completeWrapperResolving(i),i}},{key:"parse",value:function(e,t){var i=t.resolveAll,r=void 0===i||i,n=t.wrapperSequence,a=void 0===n?null:n,s=t.originalUrl,o=void 0===s?null:s,l=t.wrapperDepth,c=void 0===l?0:l,u=t.isRootVAST,h=void 0!==u&&u;if(!e||!e.documentElement||"VAST"!==e.documentElement.nodeName)return Promise.reject(new Error("Invalid VAST XMLDocument"));var p=[],d=e.documentElement.childNodes;for(var f in d){var v=d[f];if("Error"===v.nodeName){var g=m.parseNodeText(v);h?this.rootErrorURLTemplates.push(g):this.errorURLTemplates.push(g)}if("Ad"===v.nodeName){var T=U(v);T?p.push(T):this.trackVastError(this.getErrorURLTemplates(),{ERRORCODE:101})}}var k=p.length,y=p[k-1];return 1===k&&void 0!==a&&null!==a&&y&&!y.sequence&&(y.sequence=a),!1===r&&(this.remainingAds=m.splitVAST(p),p=this.remainingAds.shift()),this.resolveAds(p,{wrapperDepth:c,originalUrl:o})}},{key:"resolveAds",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],i=arguments[1],r=i.wrapperDepth,n=i.originalUrl,a=[];return t.forEach(function(t){var i=e.resolveWrappers(t,r,n);a.push(i)}),Promise.all(a).then(function(t){var i=v.flatten(t);if(!i&&e.remainingAds.length>0){var a=e.remainingAds.shift();return e.resolveAds(a,{wrapperDepth:r,originalUrl:n})}return i})}},{key:"resolveWrappers",value:function(e,t,i){var r=this;return new Promise(function(n,a){if(t++,!e.nextWrapperURL)return delete e.nextWrapperURL,n(e);if(t>=r.maxWrapperDepth||-1!==r.parentURLs.indexOf(e.nextWrapperURL))return e.errorCode=302,delete e.nextWrapperURL,n(e);e.nextWrapperURL=m.resolveVastAdTagURI(e.nextWrapperURL,i);var s=e.sequence;i=e.nextWrapperURL,r.fetchVAST(e.nextWrapperURL,t,i).then(function(a){return r.parse(a,{originalUrl:i,wrapperSequence:s,wrapperDepth:t}).then(function(t){if(delete e.nextWrapperURL,0===t.length)return e.creatives=[],n(e);t.forEach(function(t){t&&m.mergeWrapperAdData(t,e)}),n(t)})}).catch(function(t){e.errorCode=301,e.errorMessage=t.message,n(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 i=e.ads[t];(i.errorCode||0===i.creatives.length)&&(this.trackVastError(i.errorURLTemplates.concat(e.errorURLTemplates),{ERRORCODE:i.errorCode||303},{ERRORMESSAGE:i.errorMessage||""},{extensions:i.extensions},{system:i.system}),e.ads.splice(t,1))}}}]),a}(),j=null,X={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 data[e],this.length=Object.keys(this.data).length},clear:function(){this.data={},this.length=0}},Q=function(){function e(){t(this,e),this.storage=this.initStorage()}return i(e,[{key:"initStorage",value:function(){if(j)return j;try{j="undefined"!=typeof window&&null!==window?window.localStorage||window.sessionStorage:null}catch(e){j=null}return j&&!this.isStorageDisabled(j)||(j=X).clear(),j}},{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}(),G=function(){function e(i,r,n){t(this,e),this.cappingFreeLunch=i||0,this.cappingMinimumTimeInterval=r||0,this.defaultOptions={withCredentials:!1,timeout:0},this.vastParser=new W,this.storage=n||new Q,void 0===this.lastSuccessfulAd&&(this.lastSuccessfulAd=0),void 0===this.totalCalls&&(this.totalCalls=0),void 0===this.totalCallsTimeout&&(this.totalCallsTimeout=0)}return i(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,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=Date.now();return(i=Object.assign(this.defaultOptions,i)).hasOwnProperty("resolveAll")||(i.resolveAll=!1),this.totalCallsTimeout=t.totalCalls)return a(new Error("VAST call canceled – FreeLunch capping not reached yet "+t.totalCalls+"/"+t.cappingFreeLunch));var s=r-t.lastSuccessfulAd;if(s<0)t.lastSuccessfulAd=0;else if(s3&&void 0!==arguments[3]?arguments[3]:null;t(this,a);var o=n(this,(a.__proto__||Object.getPrototypeOf(a)).call(this));for(var l in o.ad=i,o.creative=r,o.variation=s,o.muted=!1,o.impressed=!1,o.skippable=!1,o.trackingEvents={},o._alreadyTriggeredQuartiles={},o.emitAlwaysEvents=["creativeView","start","firstQuartile","midpoint","thirdQuartile","complete","resume","pause","rewind","skip","closeLinear","close"],o.creative.trackingEvents){var c=o.creative.trackingEvents[l];o.trackingEvents[l]=c.slice(0)}return o.creative instanceof T?o._initLinearTracking():o._initVariationTracking(),e&&o.on("start",function(){e.lastSuccessfulAd=Date.now()}),o}return r(a,S),i(a,[{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=-1,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 A?(this.clickThroughURLTemplate=this.variation.nonlinearClickThroughURLTemplate,this.clickTrackingURLTemplates=this.variation.nonlinearClickTrackingURLTemplates,this.setDuration(this.variation.minSuggestedDuration)):this.variation instanceof l&&(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,i=this.skipDelay||-1;if(-1===i||this.skippable||(i>e?this.emit("skip-countdown",i-e):(this.skippable=!0,this.emit("skip-countdown",0))),this.assetDuration>0){var r=[];if(e>0){var n=Math.round(e/this.assetDuration*100);for(var a in r.push("start"),r.push("progress-"+n+"%"),r.push("progress-"+Math.round(e)),this.quartiles)this.isQuartileReached(a,this.quartiles[a],e)&&(r.push(a),this._alreadyTriggeredQuartiles[a]=!0)}r.forEach(function(e){t.track(e,!0)}),e0&&void 0!==arguments[0]?arguments[0]:null;this.clickTrackingURLTemplates&&this.clickTrackingURLTemplates.length&&this.trackURLs(this.clickTrackingURLTemplates);var t=this.clickThroughURLTemplate||e;if(t){var i=this.linear?{CONTENTPLAYHEAD:this.progressFormatted()}:{},r=v.resolveURLTemplates([t],i)[0];this.emit("clickthrough",r)}}},{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 i=this.trackingEvents[e],r=this.emitAlwaysEvents.indexOf(e)>-1;i?(this.emit(e,""),this.trackURLs(i)):r&&this.emit(e,""),t&&(delete this.trackingEvents[e],r&&this.emitAlwaysEvents.splice(this.emitAlwaysEvents.indexOf(e),1))}},{key:"trackURLs",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};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()),v.track(e,t)}},{key:"progressFormatted",value:function(){var e=parseInt(this.progress),t=e/3600;t.length<2&&(t="0"+t);var i=e/60%60;i.length<2&&(i="0"+i);var r=e%60;return r.length<2&&(r="0"+i),t+":"+i+":"+r+"."+parseInt(100*(this.progress-e))}}]),a}();return e.VASTClient=G,e.VASTParser=W,e.VASTTracker=Y,e}({});