The VASTParser
class provides a parser to manage the fetching (getAndParseVAST
method) and direct parsing (parseVAST
method) of VAST documents.
The behavior of this component may be confused with the one of VASTClient
, since they both provide a way to fetch and parse a VAST document.
VASTClient
has to be intended as the preferred way to manage a sequence of VAST requests on a higher level, while VASTParser
offers a set of method to follow in more detail the parsing process.
VASTParser
provides methods to fetch a VAST resource because of his ability to resolving the wrapper chain (recursive fetch and parse).
Use an instance of this class directly only if you don't need any control on multiple calls, otherwise access it through an instance of VASTClient
.
Whenever an error occurs during the VAST parsing, the parser will call automatically all related tracking error URLs. Reported errors are:
no_ad
: The VAST document is empty- VAST error
101
: VAST schema validation error. - VAST error
301
: Timeout of VAST URI provided in Wrapper element. - VAST error
302
: Wrapper limit reached. - VAST error
303
: No VAST response after one or more Wrappers.
The constructor signature is:
constructor()
To get an instance of VASTParser
, simply import it and create one using the constructor:
import { VASTParser } from 'vast-client'
// With default values
const vastParser = new VASTParser();
VASTParser
extends a custom EventEmitter
, therefore is possible to add event listeners.
Here is the list of event emitted by the class:
Event is triggered whenever there is an unsupported, empty VAST or a parsing error. It carries the following data:
ERRORCODE: Number
ERRORMESSAGE: String [optional]
extensions: Array [optional]
system: Object [optional]
vastParser.on('VAST-error', ({ ERRORCODE, ERRORMESSAGE, extensions, system }) => {
// Deal with the error
});
Event is triggered when the VAST required values are missing according to IAB specifications. It carries the following data:
message: String
parentElement: String
specVersion: Number
vastParser.on('VAST-warning', ({ message, parentElement, specVersion }) => {
// Deal with the warning
});
Event is triggered when fetchVAST
function is called, before the fetching started. It carries the following data:
url: String
previousUrl: String|Null
wrapperDepth: Number
maxWrapperDepth: Number
timeout: Number
vastParser.on('VAST-resolving', ({ url, wrapperDepth, previousUrl }) => {
// Access to the info
});
Event is triggered when fetchVAST
function is called, after the fetching was done. It carries the following data:
url: String
previousUrl: String|Null
wrapperDepth: Number
error: Error|Null
duration: Number
byteLength: Number|undefined
statusCode: Number|undefined
vastParser.on('VAST-resolved', ({ url, error }) => {
// Access to the info
});
Event is triggered when parseVastXml
function is called, when an Ad tag has been parsed. It carries the following data:
url: String
wrapperDepth: Number
type: 'ERROR'|'WRAPPER'|'INLINE'
adIndex: Number|undefined
vastParser.on('VAST-resolving', ({ url, wrapperDepth, previousUrl }) => {
// Access to the info
});
Instance of the support class URLHandler
, is used to make the requests.
Adds a filter function to the array of filters which are called before fetching a VAST document.
filter: function
- The filter function to be added at the end of the array
vastParser.addURLTemplateFilter( vastUrl => {
return url.replace('[DOMAIN]', 'mywebsite.com')
});
/*
For a VASTAdTagURI defined as :
<VASTAdTagURI>http://example.dailymotion.com/vast.php?domain=[DOMAIN]</VASTAdTagURI>
HTTP request will be:
http://example.dailymotion.com/vast.php?domain=mywebsite.com
*/
Removes the last element of the url templates filters array.
const replaceDomain = () => {
return url.replace('[DOMAIN]', 'mywebsite.com')
};
vastParser.addURLTemplateFilter(replaceDomain);
// ...
vastParser.removeURLTemplateFilter(replaceDomain);
// [DOMAIN] placeholder is no longer replaced
Returns the number of filters of the url templates filters array.
vastParser.addURLTemplateFilter( vastUrl => {
return url.replace('[DOMAIN]', 'mywebsite.com')
});
vastParser.countUrlTemplateFilters();
// returns 1
Removes all the filter functions from the url templates filters array.
vastParser.addURLTemplateFilter( vastUrl => {
return url.replace('[DOMAIN]', 'mywebsite.com')
});
vastParser.clearUrlTemplateFilters();
// [DOMAIN] placeholder is no longer replaced
Tracks the error provided in the errorCode parameter and emits a VAST-error
event for the given error.
urlTemplates: Array
- An Array of url templates to use to make the tracking callerrorCode: Object
- An Object containing the error datadata: Object
- One (or more) Object containing additional data
Fetches a VAST document for the given url. Returns a Promise
which resolves with the fetched xml or rejects with an error, according to the result of the request.
url: String
- The url to request the VAST documentwrapperDepth: Number
- Number of wrappers that have occurredpreviousUrl: String
- The url of the previous VAST
VAST-resolved
VAST-resolving
Fetches and parses a VAST for the given url.
Returns a Promise
which either resolves with the fully parsed VASTResponse
or rejects with an Error
.
url: String
- The url to request the VAST documentoptions: Object
- An optional Object of parameters to be used in the requesttimeout: Number
- A custom timeout for the requests (default120000
)withCredentials: Boolean
- A boolean to enable the withCredentials options for the XHR URLHandler (defaultfalse
)wrapperLimit: Number
- A number of Wrapper responses that can be received with no InLine response (default10
)urlHandler: URLHandler
- Custom urlhandler to be used instead of the default onesurlhandlers
urlhandler: URLHandler
- Fulfills the same purpose asurlHandler
, which is the preferred parameter to useallowMultipleAds: Boolean
- A boolean value that identifies whether multiple ads are allowed in the requested VAST response. This will override any value of allowMultipleAds attribute set in the VASTfollowAdditionalWrappers: Boolean
- A boolean value that identifies whether subsequent Wrappers after a requested VAST response is allowed. This will override any value of followAdditionalWrappers attribute set in the VAST
VAST-resolved
VAST-resolving
VAST-warning
vastParser.getAndParseVAST('http://example.dailymotion.com/vast.xml')
.then(res => {
// Do something with the parsed VAST response
})
.catch(err => {
// Deal with the error
});
// With some options
const options = {
timoeut: 5000,
withCredentials: true,
wrapperLimit: 7
}
vastParser.getAndParseVAST('http://example.dailymotion.com/vast.xml', options)
.then(res => {
// Do something with the parsed VAST response
})
.catch(err => {
// Deal with the error
});
Parses the given xml Object into a VASTResponse.
Returns a Promise
which either resolves with the fully parsed VASTResponse
or rejects with an Error
.
vastXml: Object
- An object representing an xml documentoptions: Object
- An optional Object of parameters to be used in the parsing processtimeout: Number
- A custom timeout for the possible wrapper resolving requests (default120000
)withCredentials: Boolean
- A boolean to enable the withCredentials options for the XHR URLHandler (defaultfalse
)wrapperLimit: Number
- A number of Wrapper responses that can be received with no InLine response (default10
)urlHandler: URLHandler
- Custom urlhandler to be used instead of the default onesurlhandlers
urlhandler: URLHandler
- Fulfills the same purpose asurlHandler
, which is the preferred parameter to useallowMultipleAds: Boolean
- A boolean value that identifies whether multiple ads are allowed in the requested VAST response. This will override any value of allowMultipleAds attribute set in the VASTfollowAdditionalWrappers: Boolean
- A boolean value that identifies whether subsequent Wrappers after a requested VAST response is allowed. This will override any value of followAdditionalWrappers attribute set in the VASTrequestDuration: Number
- The fetching time of the XML in ms. Provide it with byteLength to have a more accurate estimated bitrate.byteLength: Number
- The size of the request in bytes. Provide it with requestDuration to have a more accurate estimated bitrate.
VAST-resolved
VAST-resolving
VAST-warning
const vastXml = (new window.DOMParser()).parseFromString(xmlStr, "text/xml");
vastParser.parseVAST(vastXml)
.then(res => {
// Do something with the parsed VAST response
})
.catch(err => {
// Deal with the error
});
// Or with some options
const options = {
timoeut: 5000,
withCredentials: true,
wrapperLimit: 7
}
vastParser.parseVAST(vastXml, options)
.then(res => {
// Do something with the parsed VAST response
})
.catch(err => {
// Deal with the error
});
Returns the average of the estimated bitrates in kilo bit per secondes.
const options = {
requestDuration: 200,
byteLength: 1000,
}
vastParser.parseVAST(vastXml, options)
.then(res => {
vastParser.getEstimatedBitrate()
// returns 40
})
.catch(err => {
// Deal with the error
});
These methods documentation is provided in order to make the parser internal logic clearer. It should not be considered as part of the class public API
Parses the given xml Object into an array of unwrapped ads. Returns a Promise
which resolves with the array or rejects with an error according to the result of the parsing.
vastXml: Object
- An object representing an xml document.options: Object
- An optional Object of parameters to be used in the parsing process.
Parses the given xml Object into an array of ads. Returns the array or throws an Error
if an invalid VAST XML is provided.
vastXml: Object
- An object representing an xml document.options: Object
- An optional Object of parameters to be used in the parsing process.
VAST-ad-parsed
Resolves each ad in a VAST (by calling resolveWrappers). If no ads are returned and there are remaining ads from a previous VAST (like an ad buffet), it will resolve the remaining ads.
ads: Array<Ad>
- An array of ads to be unwrapped in parallel.options: Object
- An Object of parameters to be used in the unwrapping process.
Resolves the wrappers for the given ad in a recursive way. Returns a Promise
which resolves with the unwrapped ad or rejects with an error.
ad: Ad
- An ad to be unwrapped.wrapperDepth: Number
- The reached depth in the wrapper resolving chain.previousUrl: String
- The url of the previous VAST
Takes care of handling errors when the wrappers are resolved.
vastResponse: VASTResponse
- A resolved VASTResponse
Merges the data between an unwrapped ad and his wrapper.
unwrappedAd: Ad
- The 'unwrapped' Ad.wrapper: Ad
- The wrapper Ad.