vast-builder
An awesome library with great API which offer a complete support for IAB Video Ad Serving Template standard: VAST2, VAST3 and VAST4
Main features are :
- validate a VAST from string input
- awesome API to create 100% iab valid VAST 2, 3 & 4
- validate created VAST
Why is it the best ? :
- made by developers for developers
- fully maintained
- 100% test coverage, including stress test and memory leak test see
- build directly on top of the documentation see
- extermly fast, without any native dependancy
- full JSdoc for functionnal intellisense autocomplete (including params)
with only one stand alone dependance.
All APIs are directly generated on top of standard IAB specifications documents : https://www.iab.com/vast/
Getting started
Install
It requires node 8 or above.
npm i vast-builder --save
yarn add vast-builder
Validate existing VAST
const { validate } = require('vast-builder');
const bool = validate(
`<VAST version="x">
<Ad>
// ....
</Ad>
</VAST>`,
options
);
Option | Default | Description |
---|
logWarn | false | Validation warning and error will be printed to stderr. |
throwOnError | false | Validation errors will now throw an exception. |
Create new VAST
const createVast = require('vast-builder');
const vast2 = createVast.v2(options);
const vast3 = createVast.v3(options);
const vast4 = createVast.v4(options);
Sample
Here is a sample VAST3 Ad :
const vast3 = createVast.v3();
vast3.attachAd()
.attachInLine()
.addImpression('imp_link')
.addAdSystem('Society')
.addAdTitle('Title')
.attachCreatives()
.attachCreative()
.attachLinear()
.attachTrackingEvents()
.attachTracking('content',{event:'start'}).back()
.addDuration('00:30:00')
.attachMediaFiles()
.attachMediaFile('my_video', {
delivery: 'streaming',
type: 'video/mp4',
width: '600',
height: '400'
})
.back()
.attachIcons()
.attachIcon({
program: 'my_program',
width: '50',
height: '50',
xPosition: 'bottom',
yPosition: 'left'
})
.attachStaticResource('ressource_link', {creativeType:'image/jpeg'})
const render = vast3.toXml();
Will render this VAST :
<?xml version="1.0" encoding="utf-8"?>
<VAST version="3.0">
<Ad>
<InLine>
<Impression><![CDATA[imp_link]]></Impression>
<AdSystem><![CDATA[Society]]></AdSystem>
<AdTitle><![CDATA[Title]]></AdTitle>
<Creatives>
<Creative>
<Linear>
<TrackingEvents>
<Tracking event="start"><![CDATA[content]]></Tracking>
</TrackingEvents>
<Duration><![CDATA[00:30:00]]></Duration>
<MediaFiles>
<MediaFile delivery="streaming" type="video/mp4" width="600" height="400"><![CDATA[my_video]]></MediaFile>
</MediaFiles>
<Icons>
<Icon program="my_program" width="50" height="50" xPosition="bottom" yPosition="left">
<StaticResource creativeType="image/jpeg"><![CDATA[ressource_link]]></StaticResource>
</Icon>
</Icons>
</Linear>
</Creative>
</Creatives>
</InLine>
</Ad>
</VAST>
You can also use intermediates objects, the result will be exactly the same :
const vast3 = createVast.v3();
const Ad = vast3.attachAd();
const InLine = Ad.attachInLine();
Inline.addImpression('imp_link');
Inline.addAdSystem('Society');
Inline.addAdTitle('Title');
const Creatives = Inline.attachCreatives();
Fast learn
This is a recursive API, meaning all elements can invoke all same methods from each returned object. This way and you can navigate between elements easly.
As XML is a tree, in this API:
- when you "attach" an element to another, you move on the new child a level lower
- when you "add" an element to another, you stay on the current object on the same level
Here is a demo with helping indentation :
vast3.attachAd()
.attachInLine()
.addImpression('imp_link')
.addAdSystem('Society')
.attachCreatives()
.attachCreative()
.attachLinear()
.attachTrackingEvents()
.addTracking('s',
{event: 'start'}
)
.and()
.addDuration('00:30:00')
.attachMediaFiles()
API
This package does no magic under the hood, the API is very redondant and always the same, for easy learn and easy maintainability
Full API
Full APIs are availables here :
Common Node API
Every elements inherits from a generic VastElement, all methods return a VastElement child allowing to chain methods calls.
VastElement.addValidTag();
VastElement.addValidTag(content);
VastElement.addValidTag(attributes);
VastElement.addValidTag(content, attributes);
VastElement.attachValidTag();
VastElement.attachValidTag(content);
VastElement.attachValidTag(attributes);
VastElement.attachValidTag(content, attributes);
const child = VastElement.dangerouslyAttachCustomTag(tagName, content, attributes);
const self = VastElement.dangerouslyAddCustomTag(tagName, content, attributes);
const father = VastElement.and();
const grandFather = VastElement.back();
VastElement.cdata();
const stringContent = VastElement.content;
const attributes = VastElement.attrs;
const attrsObject = VastElement.getAttrs();
const childs = VastElement.getChilds(name);
const childs = VastElement.childs;
const intVersion = VastElement.getVastVersion();
const boolValid = VastElement.validate();
const xmlVast = VastElement.toXml();
Options
You can pass options to the createVast.vX(options)
method.
Availables options are :
Option | Default | Description |
---|
cdata | true | Force all contents to use <![CDATA[ ]]></a> tags. |
logWarn | true | Validation warning and error will be printed to stderr. |
throwOnError | false | Validation errors will now throw an exception. |
validateOnBuild | false | Run a validation before build, usefull for development environment. |
spaces | 2 | Number of spaces to be used for indenting XML output. Passing characters like ' ' or '\t' are also accepted. |
Following options are also available and inherited from awesome xml-js package :
Option | Default | Description |
---|
fullTagEmptyElement | false | Whether to produce element without sub-elements as full tag pairs <a></a> rather than self closing tag <a/> . |
indentCdata | false | Whether to write CData in a new line and indent it. Will generate <a>\n <![CDATA[foo]]></a> instead of <a><![CDATA[foo]]></a> . |
indentAttributes | false | Whether to print attributes across multiple lines and indent them (when spaces is not 0 ). |
ignoreDeclaration | false | Whether to ignore writing declaration directives of xml. For example, <?xml?> will be ignored. |
ignoreInstruction | false | Whether to ignore writing processing instruction of xml. For example, <?go there?> will be ignored. |
ignoreAttributes | false | Whether to ignore writing attributes of the elements. For example, x="1" in <a x="1"></a> will be ignored |
ignoreComment | false | Whether to ignore writing comments of the elements. That is, no <!-- --> will be generated. |
ignoreCdata | false | Whether to ignore writing CData of the elements. That is, no <![CDATA[ ]]> will be generated. |
ignoreDoctype | false | Whether to ignore writing Doctype of the elements. That is, no <!DOCTYPE > will be generated. |
ignoreText | false | Whether to ignore writing texts of the elements. For example, hi text in <a>hi</a> will be ignored. |
Contribute
All PR are welcome. This project and it's documentation are automatically generated from specs/*.yml files.
This command do all builds :
yarn build-api
The tests fixtures are generated by the tests if they not existed. To clean them use :
yarn clean-fixtures
Thinks to check in the next commit if the result is still valid.
Stress test
You can clone this project to compare performance between this package and a native based one (vast-xml) : https://github.com/DavidBabel/vast-builder-stress-test
Actual mesured speed test for 50000 generated VAST is :
- this package: 28s
- vast-xml package: 29s
and we have a much better api ;)
License
MIT. Copyright (c) David Babel.
Contribs
Thanks for your gentle contribs :
Donations: If you like this package, want it to be maintened and use it to makes millions, you can buy me a coffee ☕ or a beer 🍺.