@mattebox/player-ads 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +36 -2
- package/dist/ad-info.d.ts +25 -0
- package/dist/ad-info.d.ts.map +1 -0
- package/dist/ad-info.js +80 -0
- package/dist/ad-info.js.map +1 -0
- package/dist/ad-link.d.ts +19 -0
- package/dist/ad-link.d.ts.map +1 -0
- package/dist/ad-link.js +51 -0
- package/dist/ad-link.js.map +1 -0
- package/dist/ad-progress.d.ts +18 -0
- package/dist/ad-progress.d.ts.map +1 -0
- package/dist/ad-progress.js +51 -0
- package/dist/ad-progress.js.map +1 -0
- package/dist/ad-skip-button.d.ts +21 -0
- package/dist/ad-skip-button.d.ts.map +1 -0
- package/dist/ad-skip-button.js +66 -0
- package/dist/ad-skip-button.js.map +1 -0
- package/dist/beacons.d.ts +29 -0
- package/dist/beacons.d.ts.map +1 -0
- package/dist/beacons.js +44 -0
- package/dist/beacons.js.map +1 -0
- package/dist/cdn/mattebox-player-ads.min.js +1 -0
- package/dist/component.d.ts +54 -0
- package/dist/component.d.ts.map +1 -0
- package/dist/component.js +95 -0
- package/dist/component.js.map +1 -0
- package/dist/controller.d.ts +178 -0
- package/dist/controller.d.ts.map +1 -0
- package/dist/controller.js +799 -0
- package/dist/controller.js.map +1 -0
- package/dist/es2015/_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/asyncToGenerator.js +26 -0
- package/dist/es2015/_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/defineProperty.js +11 -0
- package/dist/es2015/_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/objectSpread2.js +24 -0
- package/dist/es2015/_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/toPrimitive.js +13 -0
- package/dist/es2015/_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/toPropertyKey.js +8 -0
- package/dist/es2015/_virtual/_@oxc-project_runtime@0.155.0/helpers/esm/typeof.js +10 -0
- package/dist/es2015/ad-info.js +79 -0
- package/dist/es2015/ad-link.js +53 -0
- package/dist/es2015/ad-progress.js +51 -0
- package/dist/es2015/ad-skip-button.js +68 -0
- package/dist/es2015/beacons.js +42 -0
- package/dist/es2015/component.js +94 -0
- package/dist/es2015/controller.js +826 -0
- package/dist/es2015/host.js +18 -0
- package/dist/es2015/index.js +71 -0
- package/dist/es2015/locales/ar.js +13 -0
- package/dist/es2015/locales/ca.js +13 -0
- package/dist/es2015/locales/de.js +13 -0
- package/dist/es2015/locales/es.js +13 -0
- package/dist/es2015/locales/fr.js +13 -0
- package/dist/es2015/locales/hi.js +13 -0
- package/dist/es2015/locales/it.js +13 -0
- package/dist/es2015/locales/ja.js +13 -0
- package/dist/es2015/locales/ko.js +13 -0
- package/dist/es2015/locales/pt.js +13 -0
- package/dist/es2015/locales/zh.js +13 -0
- package/dist/es2015/shared.js +26 -0
- package/dist/es2015/vast.js +255 -0
- package/dist/es2015/vmap.js +73 -0
- package/dist/host.d.ts +14 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +20 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +73 -0
- package/dist/index.js.map +1 -0
- package/dist/locales/ar.d.ts +13 -0
- package/dist/locales/ar.d.ts.map +1 -0
- package/dist/locales/ar.js +15 -0
- package/dist/locales/ar.js.map +1 -0
- package/dist/locales/ca.d.ts +13 -0
- package/dist/locales/ca.d.ts.map +1 -0
- package/dist/locales/ca.js +15 -0
- package/dist/locales/ca.js.map +1 -0
- package/dist/locales/de.d.ts +13 -0
- package/dist/locales/de.d.ts.map +1 -0
- package/dist/locales/de.js +15 -0
- package/dist/locales/de.js.map +1 -0
- package/dist/locales/es.d.ts +13 -0
- package/dist/locales/es.d.ts.map +1 -0
- package/dist/locales/es.js +15 -0
- package/dist/locales/es.js.map +1 -0
- package/dist/locales/fr.d.ts +13 -0
- package/dist/locales/fr.d.ts.map +1 -0
- package/dist/locales/fr.js +15 -0
- package/dist/locales/fr.js.map +1 -0
- package/dist/locales/hi.d.ts +13 -0
- package/dist/locales/hi.d.ts.map +1 -0
- package/dist/locales/hi.js +15 -0
- package/dist/locales/hi.js.map +1 -0
- package/dist/locales/it.d.ts +13 -0
- package/dist/locales/it.d.ts.map +1 -0
- package/dist/locales/it.js +15 -0
- package/dist/locales/it.js.map +1 -0
- package/dist/locales/ja.d.ts +13 -0
- package/dist/locales/ja.d.ts.map +1 -0
- package/dist/locales/ja.js +15 -0
- package/dist/locales/ja.js.map +1 -0
- package/dist/locales/ko.d.ts +13 -0
- package/dist/locales/ko.d.ts.map +1 -0
- package/dist/locales/ko.js +15 -0
- package/dist/locales/ko.js.map +1 -0
- package/dist/locales/pt.d.ts +13 -0
- package/dist/locales/pt.d.ts.map +1 -0
- package/dist/locales/pt.js +15 -0
- package/dist/locales/pt.js.map +1 -0
- package/dist/locales/zh.d.ts +13 -0
- package/dist/locales/zh.d.ts.map +1 -0
- package/dist/locales/zh.js +15 -0
- package/dist/locales/zh.js.map +1 -0
- package/dist/messages.d.ts +30 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/shared.d.ts +7 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +25 -0
- package/dist/shared.js.map +1 -0
- package/dist/vast.d.ts +94 -0
- package/dist/vast.d.ts.map +1 -0
- package/dist/vast.js +254 -0
- package/dist/vast.js.map +1 -0
- package/dist/vmap.d.ts +31 -0
- package/dist/vmap.d.ts.map +1 -0
- package/dist/vmap.js +71 -0
- package/dist/vmap.js.map +1 -0
- package/package.json +60 -4
package/dist/shared.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/shared.ts
|
|
2
|
+
/**
|
|
3
|
+
* What the ad elements share: their style, the text of a template, and the
|
|
4
|
+
* time of the ad. The tokens come through the `--mbx-*` custom properties
|
|
5
|
+
* the player sets, which inherit into every control.
|
|
6
|
+
*/
|
|
7
|
+
const AD_CSS = ":host{z-index:2;color:var(--mbx-text);font:500 .875em / 1.2 var(--mbx-font);display:inline-flex;position:absolute}:host([hidden]){display:none}[part~=panel]{border-radius:var(--mbx-radius);backdrop-filter:blur(12px);background:#1c1c20b8;flex-direction:column;display:inline-flex;overflow:hidden}[part~=row]{white-space:nowrap;align-items:center;height:2.25em;padding:0 .85em;font-weight:600;display:inline-flex}[part~=time]{color:#d6d0c6;font-variant-numeric:tabular-nums;margin-inline-start:.4em;font-weight:500}[part~=line]{background:#ffffff2e;height:3px}[part~=line][hidden]{display:none}[part~=line-fill]{background:var(--mbx-ad,#f5c518);height:3px}[part~=button]{border-radius:var(--mbx-radius);backdrop-filter:blur(12px);height:2.75em;color:inherit;font:inherit;white-space:nowrap;cursor:pointer;background:#1c1c20b8;border:0;align-items:center;padding:0 .85em 0 1.1em;font-size:1.07em;font-weight:600;display:inline-flex}[part~=button]:hover:not(:disabled){background:#1c1c20e6}[part~=button]:disabled{cursor:default;color:#d6d0c6;padding-inline-end:1.1em}[part~=button]:focus{outline:2px solid var(--mbx-accent);outline-offset:2px}[part~=button]:focus:not(:focus-visible){outline:none}[part~=chevron]{width:1.25em;height:1.25em;margin-inline-start:.3em}[part~=chevron][hidden]{display:none}[part~=icon-button]{width:2.75em;height:2.75em;color:inherit;cursor:pointer;background:0 0;border:0;justify-content:center;align-items:center;padding:0;display:inline-flex}[part~=disc]{backdrop-filter:blur(12px);background:#1c1c20b8;border-radius:50%;justify-content:center;align-items:center;width:2.25em;height:2.25em;display:inline-flex}[part~=icon-button]:hover [part~=disc]{background:#1c1c20e6}[part~=icon-button]:focus{outline:2px solid var(--mbx-accent);outline-offset:-2px;border-radius:50%}[part~=icon-button]:focus:not(:focus-visible){outline:none}[part~=icon],::slotted([slot=icon]){fill:currentColor;width:1.5em;height:1.5em}[part~=live]{clip:rect(0 0 0 0);white-space:nowrap;width:1px;height:1px;position:absolute;overflow:hidden}";
|
|
8
|
+
/** A `<style>` with the ad elements' style and the element's own placement. */
|
|
9
|
+
function style(placement) {
|
|
10
|
+
const node = document.createElement("style");
|
|
11
|
+
node.textContent = `${AD_CSS}:host{${placement}}`;
|
|
12
|
+
return node;
|
|
13
|
+
}
|
|
14
|
+
/** `text` with each `{name}` replaced by its value. */
|
|
15
|
+
function fill(text, values) {
|
|
16
|
+
return text.replace(/\{(\w+)\}/g, (match, name) => String(values[name] ?? match));
|
|
17
|
+
}
|
|
18
|
+
/** `m:ss`, the time an ad counts down in. */
|
|
19
|
+
function clock(seconds) {
|
|
20
|
+
const whole = Math.max(0, Math.ceil(seconds));
|
|
21
|
+
return `${Math.floor(whole / 60)}:${String(whole % 60).padStart(2, "0")}`;
|
|
22
|
+
}
|
|
23
|
+
export { clock, fill, style };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","names":[],"sources":["../src/shared.ts"],"sourcesContent":["/**\n * What the ad elements share: their style, the text of a template, and the\n * time of the ad. The tokens come through the `--mbx-*` custom properties\n * the player sets, which inherit into every control.\n */\nimport AD_CSS from './ad.css?inline';\n\n/** A `<style>` with the ad elements' style and the element's own placement. */\nexport function style(placement: string): HTMLStyleElement {\n const node = document.createElement('style');\n node.textContent = `${AD_CSS}:host{${placement}}`;\n return node;\n}\n\n/** `text` with each `{name}` replaced by its value. */\nexport function fill(text: string, values: Readonly<Record<string, string | number>>): string {\n return text.replace(/\\{(\\w+)\\}/g, (match, name: string) => String(values[name] ?? match));\n}\n\n/** `m:ss`, the time an ad counts down in. */\nexport function clock(seconds: number): string {\n const whole = Math.max(0, Math.ceil(seconds));\n return `${Math.floor(whole / 60)}:${String(whole % 60).padStart(2, '0')}`;\n}\n"],"mappings":";;;;;;AAKA,MAAA,SAAA;;AAGA,SAAA,MAAA,WAAA;CACA,MAAA,OAAA,SAAA,cAAA,OAAA;CACA,KAAA,cAAA,GAAA,OAAA,QAAA,UAAA;CACA,OAAA;AACA;;AAGA,SAAA,KAAA,MAAA,QAAA;CACA,OAAA,KAAA,QAAA,eAAA,OAAA,SAAA,OAAA,OAAA,SAAA,KAAA,CAAA;AACA;;AAGA,SAAA,MAAA,SAAA;CACA,MAAA,QAAA,KAAA,IAAA,GAAA,KAAA,KAAA,OAAA,CAAA;CACA,OAAA,GAAA,KAAA,MAAA,QAAA,EAAA,EAAA,GAAA,OAAA,QAAA,EAAA,CAAA,CAAA,SAAA,GAAA,GAAA;AACA"}
|
package/dist/vast.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VAST, the IAB's Video Ad Serving Template, versions 2 to 4.3: the ads of
|
|
3
|
+
* a document, with the wrappers they come through followed and merged. A
|
|
4
|
+
* wrapper adds its impressions, errors, tracking and click tracking to the
|
|
5
|
+
* ads it leads to (VAST 4.3 §2.4.1). The ad server's own scripts (VPAID,
|
|
6
|
+
* SIMID) are not played; verifications, companions and extensions are kept
|
|
7
|
+
* as data for the page.
|
|
8
|
+
*/
|
|
9
|
+
/** One playable file of an ad. */
|
|
10
|
+
export interface VastMediaFile {
|
|
11
|
+
readonly url: string;
|
|
12
|
+
readonly type: string;
|
|
13
|
+
readonly delivery: 'progressive' | 'streaming';
|
|
14
|
+
readonly width?: number;
|
|
15
|
+
readonly height?: number;
|
|
16
|
+
readonly bitrate?: number;
|
|
17
|
+
readonly apiFramework?: string;
|
|
18
|
+
}
|
|
19
|
+
/** A beacon an event of the ad sends, such as `start` or `firstQuartile`. */
|
|
20
|
+
export interface VastTracking {
|
|
21
|
+
readonly event: string;
|
|
22
|
+
readonly url: string;
|
|
23
|
+
/** For a `progress` event: seconds into the ad. */
|
|
24
|
+
readonly offset?: number;
|
|
25
|
+
}
|
|
26
|
+
/** A verification script the advertiser names, for the page to load (VAST 4.1 `<Verification>`). */
|
|
27
|
+
export interface VastVerification {
|
|
28
|
+
readonly vendor?: string;
|
|
29
|
+
readonly script?: string;
|
|
30
|
+
readonly parameters?: string;
|
|
31
|
+
}
|
|
32
|
+
/** A banner shown beside the player during the ad (VAST `<Companion>`). */
|
|
33
|
+
export interface VastCompanion {
|
|
34
|
+
readonly width?: number;
|
|
35
|
+
readonly height?: number;
|
|
36
|
+
/** The image, the frame or the HTML, as the ad server gives it. */
|
|
37
|
+
readonly resource?: string;
|
|
38
|
+
readonly kind?: 'static' | 'iframe' | 'html';
|
|
39
|
+
readonly clickThrough?: string;
|
|
40
|
+
}
|
|
41
|
+
/** A linear ad, ready to play. */
|
|
42
|
+
export interface VastAd {
|
|
43
|
+
readonly id?: string;
|
|
44
|
+
/** Its place in a pod. Absent for a standalone ad. */
|
|
45
|
+
readonly sequence?: number;
|
|
46
|
+
readonly system?: string;
|
|
47
|
+
readonly title?: string;
|
|
48
|
+
/** In seconds. */
|
|
49
|
+
readonly duration: number;
|
|
50
|
+
/** Seconds after which the viewer may skip. Absent: the ad cannot be skipped. */
|
|
51
|
+
readonly skipOffset?: number;
|
|
52
|
+
readonly impressions: readonly string[];
|
|
53
|
+
readonly errors: readonly string[];
|
|
54
|
+
readonly mediaFiles: readonly VastMediaFile[];
|
|
55
|
+
readonly tracking: readonly VastTracking[];
|
|
56
|
+
readonly clickThrough?: string;
|
|
57
|
+
readonly clickTracking: readonly string[];
|
|
58
|
+
readonly viewable: readonly string[];
|
|
59
|
+
readonly notViewable: readonly string[];
|
|
60
|
+
readonly verifications: readonly VastVerification[];
|
|
61
|
+
readonly companions: readonly VastCompanion[];
|
|
62
|
+
/** Each `<Extension>` as XML. */
|
|
63
|
+
readonly extensions: readonly string[];
|
|
64
|
+
}
|
|
65
|
+
/** What a VAST request gave: the ads to play, or why there are none. */
|
|
66
|
+
export interface VastResult {
|
|
67
|
+
readonly ads: readonly VastAd[];
|
|
68
|
+
/** The error beacons to send with `code` when there are no ads. */
|
|
69
|
+
readonly errors: readonly string[];
|
|
70
|
+
/** The VAST error code of a failure (VAST 4.3 §2.3.6.3). */
|
|
71
|
+
readonly code?: number;
|
|
72
|
+
}
|
|
73
|
+
/** The longest chain of wrappers followed (VAST 4.3 §2.4.1.1 recommends at most 5). */
|
|
74
|
+
export declare const WRAPPER_LIMIT = 5;
|
|
75
|
+
/** Milliseconds a VAST request may take before error 301. */
|
|
76
|
+
export declare const TIMEOUT = 5000;
|
|
77
|
+
/** Seconds of `HH:MM:SS` or `HH:MM:SS.mmm`, or undefined. */
|
|
78
|
+
export declare function clock(value: string | undefined | null): number | undefined;
|
|
79
|
+
/** Seconds of an offset: a clock time, or a percentage of `duration`. */
|
|
80
|
+
export declare function offsetOf(value: string | null, duration: number): number | undefined;
|
|
81
|
+
/** Fetches a VAST document's text: the page's hook, or `fetch` with credentials, as ad servers expect cookies. */
|
|
82
|
+
export type VastFetch = (url: string, signal: AbortSignal) => Promise<string>;
|
|
83
|
+
export declare const fetchText: VastFetch;
|
|
84
|
+
/**
|
|
85
|
+
* The ads a VAST document leads to. A pod (ads with a `sequence`) plays in
|
|
86
|
+
* order; without one, the first standalone ad plays. Wrappers are followed
|
|
87
|
+
* up to `WRAPPER_LIMIT` deep.
|
|
88
|
+
*/
|
|
89
|
+
export declare function resolveVast(source: {
|
|
90
|
+
readonly url: string;
|
|
91
|
+
} | {
|
|
92
|
+
readonly xml: string;
|
|
93
|
+
}, load?: VastFetch, depth?: number, wrappers?: boolean): Promise<VastResult>;
|
|
94
|
+
//# sourceMappingURL=vast.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vast.d.ts","sourceRoot":"","sources":["../src/vast.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,kCAAkC;AAClC,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,QAAQ,EAAE,aAAa,GAAG,WAAW,CAAC;IAC/C,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM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AkH;AAClH,MAAM,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;AAE9E,eAAO,MAAM,SAAS,EAAE,SAIvB,CAAC;AAEF;;;;GAIG;AACH,wBAAsB,WAAW,CAC/B,MAAM,EAAE;IAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;CAAE,EAC3D,IAAI,GAAE,SAAqB,EAC3B,KAAK,SAAI,EACT,QAAQ,UAAO,GACd,OAAO,CAAC,UAAU,CAAC,CA4ErB"}
|
package/dist/vast.js
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/** Milliseconds a VAST request may take before error 301. */
|
|
2
|
+
const TIMEOUT = 5e3;
|
|
3
|
+
const children = (parent, name) => Array.from(parent.children).filter((child) => child.localName === name);
|
|
4
|
+
const child = (parent, name) => children(parent, name)[0];
|
|
5
|
+
const text = (element) => {
|
|
6
|
+
const value = element?.textContent?.trim();
|
|
7
|
+
return value === void 0 || value === "" ? void 0 : value;
|
|
8
|
+
};
|
|
9
|
+
const texts = (parent, name) => parent === void 0 ? [] : children(parent, name).flatMap((e) => text(e) ?? []);
|
|
10
|
+
const numberOf = (element, name) => {
|
|
11
|
+
const value = Number(element.getAttribute(name));
|
|
12
|
+
return element.hasAttribute(name) && Number.isFinite(value) ? value : void 0;
|
|
13
|
+
};
|
|
14
|
+
/** Seconds of `HH:MM:SS` or `HH:MM:SS.mmm`, or undefined. */
|
|
15
|
+
function clock(value) {
|
|
16
|
+
const match = /^(\d+):(\d{2}):(\d{2}(?:\.\d+)?)$/.exec(value?.trim() ?? "");
|
|
17
|
+
if (match === null) return void 0;
|
|
18
|
+
return Number(match[1]) * 3600 + Number(match[2]) * 60 + Number(match[3]);
|
|
19
|
+
}
|
|
20
|
+
/** Seconds of an offset: a clock time, or a percentage of `duration`. */
|
|
21
|
+
function offsetOf(value, duration) {
|
|
22
|
+
const trimmed = value?.trim() ?? "";
|
|
23
|
+
if (trimmed.endsWith("%")) {
|
|
24
|
+
const percent = Number(trimmed.slice(0, -1));
|
|
25
|
+
return Number.isFinite(percent) ? duration * percent / 100 : void 0;
|
|
26
|
+
}
|
|
27
|
+
return clock(trimmed);
|
|
28
|
+
}
|
|
29
|
+
function layerOf(ad, body) {
|
|
30
|
+
const creatives = child(body, "Creatives");
|
|
31
|
+
const linears = creatives === void 0 ? [] : children(creatives, "Creative").flatMap((c) => children(c, "Linear"));
|
|
32
|
+
const tracking = [];
|
|
33
|
+
const clickTracking = [];
|
|
34
|
+
for (const linear of linears) {
|
|
35
|
+
const duration = clock(text(child(linear, "Duration"))) ?? 0;
|
|
36
|
+
for (const entry of children(child(linear, "TrackingEvents") ?? linear, "Tracking")) {
|
|
37
|
+
const url = text(entry);
|
|
38
|
+
const event = entry.getAttribute("event");
|
|
39
|
+
if (url === void 0 || event === null) continue;
|
|
40
|
+
const offset = event === "progress" ? offsetOf(entry.getAttribute("offset"), duration) : void 0;
|
|
41
|
+
tracking.push({
|
|
42
|
+
event,
|
|
43
|
+
url,
|
|
44
|
+
...offset !== void 0 ? { offset } : {}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
clickTracking.push(...texts(child(linear, "VideoClicks"), "ClickTracking"));
|
|
48
|
+
}
|
|
49
|
+
const viewable = child(body, "ViewableImpression");
|
|
50
|
+
const verifications = child(body, "AdVerifications") ?? child(ad, "AdVerifications");
|
|
51
|
+
return {
|
|
52
|
+
impressions: texts(body, "Impression"),
|
|
53
|
+
errors: texts(body, "Error"),
|
|
54
|
+
tracking,
|
|
55
|
+
clickTracking,
|
|
56
|
+
viewable: texts(viewable, "Viewable"),
|
|
57
|
+
notViewable: texts(viewable, "NotViewable"),
|
|
58
|
+
verifications: verifications === void 0 ? [] : children(verifications, "Verification").map((v) => {
|
|
59
|
+
const vendor = v.getAttribute("vendor");
|
|
60
|
+
const script = text(child(v, "JavaScriptResource"));
|
|
61
|
+
const parameters = text(child(v, "VerificationParameters"));
|
|
62
|
+
return {
|
|
63
|
+
...vendor !== null ? { vendor } : {},
|
|
64
|
+
...script !== void 0 ? { script } : {},
|
|
65
|
+
...parameters !== void 0 ? { parameters } : {}
|
|
66
|
+
};
|
|
67
|
+
})
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function companionsOf(body) {
|
|
71
|
+
const out = [];
|
|
72
|
+
const creatives = child(body, "Creatives");
|
|
73
|
+
for (const creative of creatives === void 0 ? [] : children(creatives, "Creative")) {
|
|
74
|
+
const set = child(creative, "CompanionAds");
|
|
75
|
+
for (const companion of set === void 0 ? [] : children(set, "Companion")) {
|
|
76
|
+
const width = numberOf(companion, "width");
|
|
77
|
+
const height = numberOf(companion, "height");
|
|
78
|
+
const found = [
|
|
79
|
+
["static", "StaticResource"],
|
|
80
|
+
["iframe", "IFrameResource"],
|
|
81
|
+
["html", "HTMLResource"]
|
|
82
|
+
].find(([, name]) => child(companion, name) !== void 0);
|
|
83
|
+
const resource = found === void 0 ? void 0 : text(child(companion, found[1]));
|
|
84
|
+
const clickThrough = text(child(companion, "CompanionClickThrough"));
|
|
85
|
+
out.push({
|
|
86
|
+
...width !== void 0 ? { width } : {},
|
|
87
|
+
...height !== void 0 ? { height } : {},
|
|
88
|
+
...resource !== void 0 && found !== void 0 ? {
|
|
89
|
+
resource,
|
|
90
|
+
kind: found[0]
|
|
91
|
+
} : {},
|
|
92
|
+
...clickThrough !== void 0 ? { clickThrough } : {}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
/** The ad of an `<InLine>`, without what wrappers add, or null without a linear creative. */
|
|
99
|
+
function inlineAd(ad, inline) {
|
|
100
|
+
const creatives = child(inline, "Creatives");
|
|
101
|
+
const linear = (creatives === void 0 ? [] : children(creatives, "Creative")).flatMap((c) => children(c, "Linear"))[0];
|
|
102
|
+
if (linear === void 0) return null;
|
|
103
|
+
const duration = clock(text(child(linear, "Duration"))) ?? 0;
|
|
104
|
+
const skipOffset = offsetOf(linear.getAttribute("skipoffset"), duration);
|
|
105
|
+
const files = child(linear, "MediaFiles");
|
|
106
|
+
const mediaFiles = (files === void 0 ? [] : children(files, "MediaFile")).flatMap((file) => {
|
|
107
|
+
const url = text(file);
|
|
108
|
+
if (url === void 0) return [];
|
|
109
|
+
const width = numberOf(file, "width");
|
|
110
|
+
const height = numberOf(file, "height");
|
|
111
|
+
const bitrate = numberOf(file, "bitrate");
|
|
112
|
+
const api = file.getAttribute("apiFramework");
|
|
113
|
+
return [{
|
|
114
|
+
url,
|
|
115
|
+
type: file.getAttribute("type") ?? "",
|
|
116
|
+
delivery: file.getAttribute("delivery") === "streaming" ? "streaming" : "progressive",
|
|
117
|
+
...width !== void 0 ? { width } : {},
|
|
118
|
+
...height !== void 0 ? { height } : {},
|
|
119
|
+
...bitrate !== void 0 ? { bitrate } : {},
|
|
120
|
+
...api !== null ? { apiFramework: api } : {}
|
|
121
|
+
}];
|
|
122
|
+
});
|
|
123
|
+
const layer = layerOf(ad, inline);
|
|
124
|
+
const id = ad.getAttribute("id");
|
|
125
|
+
const sequence = numberOf(ad, "sequence");
|
|
126
|
+
const system = text(child(inline, "AdSystem"));
|
|
127
|
+
const title = text(child(inline, "AdTitle"));
|
|
128
|
+
const clickThrough = text(child(child(linear, "VideoClicks") ?? linear, "ClickThrough"));
|
|
129
|
+
const extensions = child(inline, "Extensions");
|
|
130
|
+
return {
|
|
131
|
+
...id !== null ? { id } : {},
|
|
132
|
+
...sequence !== void 0 ? { sequence } : {},
|
|
133
|
+
...system !== void 0 ? { system } : {},
|
|
134
|
+
...title !== void 0 ? { title } : {},
|
|
135
|
+
duration,
|
|
136
|
+
...skipOffset !== void 0 ? { skipOffset } : {},
|
|
137
|
+
...layer,
|
|
138
|
+
mediaFiles,
|
|
139
|
+
...clickThrough !== void 0 ? { clickThrough } : {},
|
|
140
|
+
companions: companionsOf(inline),
|
|
141
|
+
extensions: extensions === void 0 ? [] : children(extensions, "Extension").map((e) => new XMLSerializer().serializeToString(e))
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/** Adds a wrapper's beacons to an ad it led to. */
|
|
145
|
+
function merged(ad, layer) {
|
|
146
|
+
return {
|
|
147
|
+
...ad,
|
|
148
|
+
impressions: [...layer.impressions, ...ad.impressions],
|
|
149
|
+
errors: [...layer.errors, ...ad.errors],
|
|
150
|
+
tracking: [...layer.tracking, ...ad.tracking],
|
|
151
|
+
clickTracking: [...layer.clickTracking, ...ad.clickTracking],
|
|
152
|
+
viewable: [...layer.viewable, ...ad.viewable],
|
|
153
|
+
notViewable: [...layer.notViewable, ...ad.notViewable],
|
|
154
|
+
verifications: [...layer.verifications, ...ad.verifications]
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
const fetchText = async (url, signal) => {
|
|
158
|
+
const response = await fetch(url, {
|
|
159
|
+
credentials: "include",
|
|
160
|
+
signal
|
|
161
|
+
});
|
|
162
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
163
|
+
return response.text();
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* The ads a VAST document leads to. A pod (ads with a `sequence`) plays in
|
|
167
|
+
* order; without one, the first standalone ad plays. Wrappers are followed
|
|
168
|
+
* up to `WRAPPER_LIMIT` deep.
|
|
169
|
+
*/
|
|
170
|
+
async function resolveVast(source, load = fetchText, depth = 0, wrappers = true) {
|
|
171
|
+
let xml;
|
|
172
|
+
if ("xml" in source) xml = source.xml;
|
|
173
|
+
else {
|
|
174
|
+
const controller = new AbortController();
|
|
175
|
+
const timer = setTimeout(() => controller.abort(), TIMEOUT);
|
|
176
|
+
try {
|
|
177
|
+
xml = await load(source.url, controller.signal);
|
|
178
|
+
} catch {
|
|
179
|
+
return {
|
|
180
|
+
ads: [],
|
|
181
|
+
errors: [],
|
|
182
|
+
code: depth > 0 ? 301 : 900
|
|
183
|
+
};
|
|
184
|
+
} finally {
|
|
185
|
+
clearTimeout(timer);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const doc = new DOMParser().parseFromString(xml, "application/xml");
|
|
189
|
+
const root = doc.documentElement;
|
|
190
|
+
if (root === null || root.localName !== "VAST" || doc.getElementsByTagName("parsererror").length > 0) return {
|
|
191
|
+
ads: [],
|
|
192
|
+
errors: [],
|
|
193
|
+
code: 100
|
|
194
|
+
};
|
|
195
|
+
const nodes = children(root, "Ad");
|
|
196
|
+
if (nodes.length === 0) return {
|
|
197
|
+
ads: [],
|
|
198
|
+
errors: texts(root, "Error"),
|
|
199
|
+
code: 303
|
|
200
|
+
};
|
|
201
|
+
const pod = nodes.filter((ad) => ad.hasAttribute("sequence"));
|
|
202
|
+
const chosen = pod.length > 0 ? pod.sort((a, b) => (numberOf(a, "sequence") ?? 0) - (numberOf(b, "sequence") ?? 0)) : nodes.slice(0, 1);
|
|
203
|
+
const ads = [];
|
|
204
|
+
const errors = [];
|
|
205
|
+
let code;
|
|
206
|
+
for (const ad of chosen) {
|
|
207
|
+
const inline = child(ad, "InLine");
|
|
208
|
+
if (inline !== void 0) {
|
|
209
|
+
const found = inlineAd(ad, inline);
|
|
210
|
+
if (found === null) code = 201;
|
|
211
|
+
else ads.push(found);
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
const wrapper = child(ad, "Wrapper");
|
|
215
|
+
const next = text(wrapper === void 0 ? void 0 : child(wrapper, "VASTAdTagURI"));
|
|
216
|
+
if (wrapper === void 0 || next === void 0) {
|
|
217
|
+
code = 300;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const layer = layerOf(ad, wrapper);
|
|
221
|
+
if (depth + 1 > 5 || !wrappers) {
|
|
222
|
+
errors.push(...layer.errors);
|
|
223
|
+
code = 302;
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
const base = "xml" in source ? location.href : source.url;
|
|
227
|
+
const follow = wrapper.getAttribute("followAdditionalWrappers") !== "false";
|
|
228
|
+
const result = await resolveVast({ url: new URL(next, base).href }, load, depth + 1, follow);
|
|
229
|
+
if (result.ads.length === 0) {
|
|
230
|
+
errors.push(...layer.errors, ...result.errors);
|
|
231
|
+
code = result.code ?? 303;
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
const single = wrapper.getAttribute("allowMultipleAds") === "false";
|
|
235
|
+
ads.push(...(single ? result.ads.slice(0, 1) : result.ads).map((found) => merged(found, layer)));
|
|
236
|
+
}
|
|
237
|
+
if (ads.length === 0 && pod.length > 0) {
|
|
238
|
+
const standalone = nodes.filter((ad) => !ad.hasAttribute("sequence"))[0];
|
|
239
|
+
const inline = standalone === void 0 ? void 0 : child(standalone, "InLine");
|
|
240
|
+
const found = standalone === void 0 || inline === void 0 ? null : inlineAd(standalone, inline);
|
|
241
|
+
if (found !== null) return {
|
|
242
|
+
ads: [found],
|
|
243
|
+
errors: []
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
ads,
|
|
248
|
+
errors,
|
|
249
|
+
...ads.length === 0 ? { code: code ?? 303 } : {}
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
export { TIMEOUT, clock, fetchText, offsetOf, resolveVast };
|
|
253
|
+
|
|
254
|
+
//# sourceMappingURL=vast.js.map
|
package/dist/vast.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vast.js","names":[],"sources":["../src/vast.ts"],"sourcesContent":["/**\n * VAST, the IAB's Video Ad Serving Template, versions 2 to 4.3: the ads of\n * a document, with the wrappers they come through followed and merged. A\n * wrapper adds its impressions, errors, tracking and click tracking to the\n * ads it leads to (VAST 4.3 §2.4.1). The ad server's own scripts (VPAID,\n * SIMID) are not played; verifications, companions and extensions are kept\n * as data for the page.\n */\n\n/** One playable file of an ad. */\nexport interface VastMediaFile {\n readonly url: string;\n readonly type: string;\n readonly delivery: 'progressive' | 'streaming';\n readonly width?: number;\n readonly height?: number;\n readonly bitrate?: number;\n readonly apiFramework?: string;\n}\n\n/** A beacon an event of the ad sends, such as `start` or `firstQuartile`. */\nexport interface VastTracking {\n readonly event: string;\n readonly url: string;\n /** For a `progress` event: seconds into the ad. */\n readonly offset?: number;\n}\n\n/** A verification script the advertiser names, for the page to load (VAST 4.1 `<Verification>`). */\nexport interface VastVerification {\n readonly vendor?: string;\n readonly script?: string;\n readonly parameters?: string;\n}\n\n/** A banner shown beside the player during the ad (VAST `<Companion>`). */\nexport interface VastCompanion {\n readonly width?: number;\n readonly height?: number;\n /** The image, the frame or the HTML, as the ad server gives it. */\n readonly resource?: string;\n readonly kind?: 'static' | 'iframe' | 'html';\n readonly clickThrough?: string;\n}\n\n/** A linear ad, ready to play. */\nexport interface VastAd {\n readonly id?: string;\n /** Its place in a pod. Absent for a standalone ad. */\n readonly sequence?: number;\n readonly system?: string;\n readonly title?: string;\n /** In seconds. */\n readonly duration: number;\n /** Seconds after which the viewer may skip. Absent: the ad cannot be skipped. */\n readonly skipOffset?: number;\n readonly impressions: readonly string[];\n readonly errors: readonly string[];\n readonly mediaFiles: readonly VastMediaFile[];\n readonly tracking: readonly VastTracking[];\n readonly clickThrough?: string;\n readonly clickTracking: readonly string[];\n readonly viewable: readonly string[];\n readonly notViewable: readonly string[];\n readonly verifications: readonly VastVerification[];\n readonly companions: readonly VastCompanion[];\n /** Each `<Extension>` as XML. */\n readonly extensions: readonly string[];\n}\n\n/** What a VAST request gave: the ads to play, or why there are none. */\nexport interface VastResult {\n readonly ads: readonly VastAd[];\n /** The error beacons to send with `code` when there are no ads. */\n readonly errors: readonly string[];\n /** The VAST error code of a failure (VAST 4.3 §2.3.6.3). */\n readonly code?: number;\n}\n\n/** The longest chain of wrappers followed (VAST 4.3 §2.4.1.1 recommends at most 5). */\nexport const WRAPPER_LIMIT = 5;\n/** Milliseconds a VAST request may take before error 301. */\nexport const TIMEOUT = 5000;\n\nconst children = (parent: Element, name: string): Element[] =>\n Array.from(parent.children).filter((child) => child.localName === name);\nconst child = (parent: Element, name: string): Element | undefined => children(parent, name)[0];\nconst text = (element: Element | undefined): string | undefined => {\n const value = element?.textContent?.trim();\n return value === undefined || value === '' ? undefined : value;\n};\nconst texts = (parent: Element | undefined, name: string): string[] =>\n parent === undefined ? [] : children(parent, name).flatMap((e) => text(e) ?? []);\nconst numberOf = (element: Element, name: string): number | undefined => {\n const value = Number(element.getAttribute(name));\n return element.hasAttribute(name) && Number.isFinite(value) ? value : undefined;\n};\n\n/** Seconds of `HH:MM:SS` or `HH:MM:SS.mmm`, or undefined. */\nexport function clock(value: string | undefined | null): number | undefined {\n const match = /^(\\d+):(\\d{2}):(\\d{2}(?:\\.\\d+)?)$/.exec(value?.trim() ?? '');\n if (match === null) return undefined;\n return Number(match[1]) * 3600 + Number(match[2]) * 60 + Number(match[3]);\n}\n\n/** Seconds of an offset: a clock time, or a percentage of `duration`. */\nexport function offsetOf(value: string | null, duration: number): number | undefined {\n const trimmed = value?.trim() ?? '';\n if (trimmed.endsWith('%')) {\n const percent = Number(trimmed.slice(0, -1));\n return Number.isFinite(percent) ? (duration * percent) / 100 : undefined;\n }\n return clock(trimmed);\n}\n\n/** One `<Ad>`'s linear creative and its beacons, inline or from a wrapper. */\ninterface Layer {\n readonly impressions: string[];\n readonly errors: string[];\n readonly tracking: VastTracking[];\n readonly clickTracking: string[];\n readonly viewable: string[];\n readonly notViewable: string[];\n readonly verifications: VastVerification[];\n}\n\nfunction layerOf(ad: Element, body: Element): Layer {\n const creatives = child(body, 'Creatives');\n const linears =\n creatives === undefined\n ? []\n : children(creatives, 'Creative').flatMap((c) => children(c, 'Linear'));\n const tracking: VastTracking[] = [];\n const clickTracking: string[] = [];\n for (const linear of linears) {\n const duration = clock(text(child(linear, 'Duration'))) ?? 0;\n for (const entry of children(child(linear, 'TrackingEvents') ?? linear, 'Tracking')) {\n const url = text(entry);\n const event = entry.getAttribute('event');\n if (url === undefined || event === null) continue;\n const offset =\n event === 'progress' ? offsetOf(entry.getAttribute('offset'), duration) : undefined;\n tracking.push({ event, url, ...(offset !== undefined ? { offset } : {}) });\n }\n clickTracking.push(...texts(child(linear, 'VideoClicks'), 'ClickTracking'));\n }\n const viewable = child(body, 'ViewableImpression');\n const verifications = child(body, 'AdVerifications') ?? child(ad, 'AdVerifications');\n return {\n impressions: texts(body, 'Impression'),\n errors: texts(body, 'Error'),\n tracking,\n clickTracking,\n viewable: texts(viewable, 'Viewable'),\n notViewable: texts(viewable, 'NotViewable'),\n verifications:\n verifications === undefined\n ? []\n : children(verifications, 'Verification').map((v) => {\n const vendor = v.getAttribute('vendor');\n const script = text(child(v, 'JavaScriptResource'));\n const parameters = text(child(v, 'VerificationParameters'));\n return {\n ...(vendor !== null ? { vendor } : {}),\n ...(script !== undefined ? { script } : {}),\n ...(parameters !== undefined ? { parameters } : {}),\n };\n }),\n };\n}\n\nfunction companionsOf(body: Element): VastCompanion[] {\n const out: VastCompanion[] = [];\n const creatives = child(body, 'Creatives');\n for (const creative of creatives === undefined ? [] : children(creatives, 'Creative')) {\n const set = child(creative, 'CompanionAds');\n for (const companion of set === undefined ? [] : children(set, 'Companion')) {\n const width = numberOf(companion, 'width');\n const height = numberOf(companion, 'height');\n const resources = [\n ['static', 'StaticResource'],\n ['iframe', 'IFrameResource'],\n ['html', 'HTMLResource'],\n ] as const;\n const found = resources.find(([, name]) => child(companion, name) !== undefined);\n const resource = found === undefined ? undefined : text(child(companion, found[1]));\n const clickThrough = text(child(companion, 'CompanionClickThrough'));\n out.push({\n ...(width !== undefined ? { width } : {}),\n ...(height !== undefined ? { height } : {}),\n ...(resource !== undefined && found !== undefined ? { resource, kind: found[0] } : {}),\n ...(clickThrough !== undefined ? { clickThrough } : {}),\n });\n }\n }\n return out;\n}\n\n/** The ad of an `<InLine>`, without what wrappers add, or null without a linear creative. */\nfunction inlineAd(ad: Element, inline: Element): VastAd | null {\n const creatives = child(inline, 'Creatives');\n const linear = (creatives === undefined ? [] : children(creatives, 'Creative')).flatMap((c) =>\n children(c, 'Linear'),\n )[0];\n if (linear === undefined) return null;\n const duration = clock(text(child(linear, 'Duration'))) ?? 0;\n const skipOffset = offsetOf(linear.getAttribute('skipoffset'), duration);\n const files = child(linear, 'MediaFiles');\n const mediaFiles: VastMediaFile[] = (\n files === undefined ? [] : children(files, 'MediaFile')\n ).flatMap((file) => {\n const url = text(file);\n if (url === undefined) return [];\n const width = numberOf(file, 'width');\n const height = numberOf(file, 'height');\n const bitrate = numberOf(file, 'bitrate');\n const api = file.getAttribute('apiFramework');\n return [\n {\n url,\n type: file.getAttribute('type') ?? '',\n delivery: file.getAttribute('delivery') === 'streaming' ? 'streaming' : 'progressive',\n ...(width !== undefined ? { width } : {}),\n ...(height !== undefined ? { height } : {}),\n ...(bitrate !== undefined ? { bitrate } : {}),\n ...(api !== null ? { apiFramework: api } : {}),\n },\n ];\n });\n const layer = layerOf(ad, inline);\n const id = ad.getAttribute('id');\n const sequence = numberOf(ad, 'sequence');\n const system = text(child(inline, 'AdSystem'));\n const title = text(child(inline, 'AdTitle'));\n const clickThrough = text(child(child(linear, 'VideoClicks') ?? linear, 'ClickThrough'));\n const extensions = child(inline, 'Extensions');\n return {\n ...(id !== null ? { id } : {}),\n ...(sequence !== undefined ? { sequence } : {}),\n ...(system !== undefined ? { system } : {}),\n ...(title !== undefined ? { title } : {}),\n duration,\n ...(skipOffset !== undefined ? { skipOffset } : {}),\n ...layer,\n mediaFiles,\n ...(clickThrough !== undefined ? { clickThrough } : {}),\n companions: companionsOf(inline),\n extensions:\n extensions === undefined\n ? []\n : children(extensions, 'Extension').map((e) => new XMLSerializer().serializeToString(e)),\n };\n}\n\n/** Adds a wrapper's beacons to an ad it led to. */\nfunction merged(ad: VastAd, layer: Layer): VastAd {\n return {\n ...ad,\n impressions: [...layer.impressions, ...ad.impressions],\n errors: [...layer.errors, ...ad.errors],\n tracking: [...layer.tracking, ...ad.tracking],\n clickTracking: [...layer.clickTracking, ...ad.clickTracking],\n viewable: [...layer.viewable, ...ad.viewable],\n notViewable: [...layer.notViewable, ...ad.notViewable],\n verifications: [...layer.verifications, ...ad.verifications],\n };\n}\n\n/** Fetches a VAST document's text: the page's hook, or `fetch` with credentials, as ad servers expect cookies. */\nexport type VastFetch = (url: string, signal: AbortSignal) => Promise<string>;\n\nexport const fetchText: VastFetch = async (url, signal) => {\n const response = await fetch(url, { credentials: 'include', signal });\n if (!response.ok) throw new Error(`HTTP ${response.status}`);\n return response.text();\n};\n\n/**\n * The ads a VAST document leads to. A pod (ads with a `sequence`) plays in\n * order; without one, the first standalone ad plays. Wrappers are followed\n * up to `WRAPPER_LIMIT` deep.\n */\nexport async function resolveVast(\n source: { readonly url: string } | { readonly xml: string },\n load: VastFetch = fetchText,\n depth = 0,\n wrappers = true,\n): Promise<VastResult> {\n let xml: string;\n if ('xml' in source) xml = source.xml;\n else {\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), TIMEOUT);\n try {\n xml = await load(source.url, controller.signal);\n } catch {\n return { ads: [], errors: [], code: depth > 0 ? 301 : 900 };\n } finally {\n clearTimeout(timer);\n }\n }\n const doc = new DOMParser().parseFromString(xml, 'application/xml');\n const root = doc.documentElement;\n if (\n root === null ||\n root.localName !== 'VAST' ||\n doc.getElementsByTagName('parsererror').length > 0\n ) {\n return { ads: [], errors: [], code: 100 };\n }\n const nodes = children(root, 'Ad');\n if (nodes.length === 0) return { ads: [], errors: texts(root, 'Error'), code: 303 };\n const pod = nodes.filter((ad) => ad.hasAttribute('sequence'));\n const chosen =\n pod.length > 0\n ? pod.sort((a, b) => (numberOf(a, 'sequence') ?? 0) - (numberOf(b, 'sequence') ?? 0))\n : nodes.slice(0, 1);\n const ads: VastAd[] = [];\n const errors: string[] = [];\n let code: number | undefined;\n for (const ad of chosen) {\n const inline = child(ad, 'InLine');\n if (inline !== undefined) {\n const found = inlineAd(ad, inline);\n if (found === null) code = 201;\n else ads.push(found);\n continue;\n }\n const wrapper = child(ad, 'Wrapper');\n const next = text(wrapper === undefined ? undefined : child(wrapper, 'VASTAdTagURI'));\n if (wrapper === undefined || next === undefined) {\n code = 300;\n continue;\n }\n const layer = layerOf(ad, wrapper);\n if (depth + 1 > WRAPPER_LIMIT || !wrappers) {\n errors.push(...layer.errors);\n code = 302;\n continue;\n }\n const base = 'xml' in source ? location.href : source.url;\n const follow = wrapper.getAttribute('followAdditionalWrappers') !== 'false';\n const result = await resolveVast({ url: new URL(next, base).href }, load, depth + 1, follow);\n if (result.ads.length === 0) {\n errors.push(...layer.errors, ...result.errors);\n code = result.code ?? 303;\n continue;\n }\n // allowMultipleAds=\"false\" takes one ad of what the wrapper leads to.\n const single = wrapper.getAttribute('allowMultipleAds') === 'false';\n ads.push(\n ...(single ? result.ads.slice(0, 1) : result.ads).map((found) => merged(found, layer)),\n );\n }\n if (ads.length === 0 && pod.length > 0) {\n // A pod that failed falls back to the standalone ads, as VAST 4.3 §2.3.3 lets a player.\n const standalone = nodes.filter((ad) => !ad.hasAttribute('sequence'))[0];\n const inline = standalone === undefined ? undefined : child(standalone, 'InLine');\n const found =\n standalone === undefined || inline === undefined ? null : inlineAd(standalone, inline);\n if (found !== null) return { ads: [found], errors: [] };\n }\n return { ads, errors, ...(ads.length === 0 ? { code: code ?? 303 } : {}) };\n}\n"],"mappings":";AAkFA,MAAa,UAAU;AAEvB,MAAM,YAAY,QAAiB,SACjC,MAAM,KAAK,OAAO,QAAQ,CAAC,CAAC,QAAQ,UAAU,MAAM,cAAc,IAAI;AACxE,MAAM,SAAS,QAAiB,SAAsC,SAAS,QAAQ,IAAI,CAAC,CAAC;AAC7F,MAAM,QAAQ,YAAqD;CACjE,MAAM,QAAQ,SAAS,aAAa,KAAK;CACzC,OAAO,UAAU,KAAA,KAAa,UAAU,KAAK,KAAA,IAAY;AAC3D;AACA,MAAM,SAAS,QAA6B,SAC1C,WAAW,KAAA,IAAY,CAAC,IAAI,SAAS,QAAQ,IAAI,CAAC,CAAC,SAAS,MAAM,KAAK,CAAC,KAAK,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AAC,KAAK,GAAG,MAAM,QAAQ,GAAG,MAAM;EAC3F,IAAI,OAAO,IAAI,WAAW,GAAG;GAC3B,OAAO,KAAK,GAAG,MAAM,QAAQ,GAAG,OAAO,MAAM;GAC7C,OAAO,OAAO,QAAQ;GACtB;EACF;EAEA,MAAM,SAAS,QAAQ,aAAa,kBAAkB,MAAM;EAC5D,IAAI,KACF,IAAI,SAAS,OAAO,IAAI,MAAM,GAAG,CAAC,IAAI,OAAO,IAAA,CAAK,KAAK,UAAU,OAAO,OAAO,KAAK,CAAC,CACvF;CACF;CACA,IAAI,IAAI,WAAW,KAAK,IAAI,SAAS,GAAG;EAEtC,MAAM,aAAa,MAAM,QAAQ,OAAO,CAAC,GAAG,aAAa,UAAU,CAAC,CAAC,CAAC;EACtE,MAAM,SAAS,eAAe,KAAA,IAAY,KAAA,IAAY,MAAM,YAAY,QAAQ;EAChF,MAAM,QACJ,eAAe,KAAA,KAAa,WAAW,KAAA,IAAY,OAAO,SAAS,YAAY,MAAM;EACvF,IAAI,UAAU,MAAM,OAAO;GAAE,KAAK,CAAC,KAAK;GAAG,QAAQ,CAAC;EAAE;CACxD;CACA,OAAO;EAAE;EAAK;EAAQ,GAAI,IAAI,WAAW,IAAI,EAAE,MAAM,QAAQ,IAAI,IAAI,CAAC;CAAG;AAC3E"}
|
package/dist/vmap.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Where a break goes: before, after, at a time, at a share of the duration, or at a chapter position. */
|
|
2
|
+
export type BreakOffset = 'start' | 'end' | {
|
|
3
|
+
readonly time: number;
|
|
4
|
+
} | {
|
|
5
|
+
readonly percent: number;
|
|
6
|
+
} | {
|
|
7
|
+
readonly position: number;
|
|
8
|
+
};
|
|
9
|
+
/** A break of the schedule, before its VAST is fetched. */
|
|
10
|
+
export interface ScheduledBreak {
|
|
11
|
+
readonly id: string;
|
|
12
|
+
readonly offset: BreakOffset;
|
|
13
|
+
/** The VAST URL, or the VAST document the VMAP carries. */
|
|
14
|
+
readonly vast: {
|
|
15
|
+
readonly url: string;
|
|
16
|
+
} | {
|
|
17
|
+
readonly xml: string;
|
|
18
|
+
};
|
|
19
|
+
/** The VMAP's own beacons: `breakStart`, `breakEnd`, `error`. */
|
|
20
|
+
readonly tracking: readonly {
|
|
21
|
+
readonly event: string;
|
|
22
|
+
readonly url: string;
|
|
23
|
+
}[];
|
|
24
|
+
}
|
|
25
|
+
/** The offset of `timeOffset` (VMAP 1.0.1 §2.4). */
|
|
26
|
+
export declare function breakOffset(value: string | null): BreakOffset | null;
|
|
27
|
+
/** The breaks of a VMAP document, in document order, or null when the text is not VMAP. */
|
|
28
|
+
export declare function parseVmap(xml: string, base: string): ScheduledBreak[] | null;
|
|
29
|
+
/** The programme time of an offset, or null when it needs what is not known yet. */
|
|
30
|
+
export declare function offsetTime(offset: BreakOffset, duration: number, chapters?: readonly number[]): number | null;
|
|
31
|
+
//# sourceMappingURL=vmap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vmap.d.ts","sourceRoot":"","sources":["../src/vmap.ts"],"names":[],"mappings":"AAOA,0GAA0G;AAC1G,MAAM,MAAM,WAAW,GACnB,OAAO,GACP,KAAK,GACL;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACzB;IAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAC5B;IAAE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAElC,2DAA2D;AAC3D,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,2DAA2D;IAC3D,QAAQ,CAAC,IAAI,EAAE;QAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC;IACnE,iEAAiE;IACjE,QAAQ,CAAC,QAAQ,EAAE,SAAS;QAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CAChF;AAED,oDAAoD;AACpD,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,WAAW,GAAG,IAAI,CAapE;AAED,2FAA2F;AAC3F,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,cAAc,EAAE,GAAG,IAAI,CAyC5E;AAED,oFAAoF;AACpF,wBAAgB,UAAU,CACxB,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,MAAM,EAChB,QAAQ,GAAE,SAAS,MAAM,EAAO,GAC/B,MAAM,GAAG,IAAI,CAOf"}
|
package/dist/vmap.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { clock } from "./vast.js";
|
|
2
|
+
// src/vmap.ts
|
|
3
|
+
/**
|
|
4
|
+
* VMAP 1.0.1, the IAB's Video Multiple Ad Playlist: when the breaks of a
|
|
5
|
+
* programme go, and which VAST each one asks for. Only linear breaks are
|
|
6
|
+
* read; a nonlinear or display break is left out.
|
|
7
|
+
*/
|
|
8
|
+
/** The offset of `timeOffset` (VMAP 1.0.1 §2.4). */
|
|
9
|
+
function breakOffset(value) {
|
|
10
|
+
const trimmed = value?.trim() ?? "";
|
|
11
|
+
if (trimmed === "start" || trimmed === "end") return trimmed;
|
|
12
|
+
if (trimmed.endsWith("%")) {
|
|
13
|
+
const percent = Number(trimmed.slice(0, -1));
|
|
14
|
+
return Number.isFinite(percent) ? { percent } : null;
|
|
15
|
+
}
|
|
16
|
+
if (trimmed.startsWith("#")) {
|
|
17
|
+
const position = Number(trimmed.slice(1));
|
|
18
|
+
return Number.isInteger(position) ? { position } : null;
|
|
19
|
+
}
|
|
20
|
+
const time = clock(trimmed);
|
|
21
|
+
return time === void 0 ? null : { time };
|
|
22
|
+
}
|
|
23
|
+
/** The breaks of a VMAP document, in document order, or null when the text is not VMAP. */
|
|
24
|
+
function parseVmap(xml, base) {
|
|
25
|
+
const root = new DOMParser().parseFromString(xml, "application/xml").documentElement;
|
|
26
|
+
if (root === null || root.localName !== "VMAP") return null;
|
|
27
|
+
const out = [];
|
|
28
|
+
let index = 0;
|
|
29
|
+
for (const entry of Array.from(root.children).filter((e) => e.localName === "AdBreak")) {
|
|
30
|
+
index += 1;
|
|
31
|
+
const types = (entry.getAttribute("breakType") ?? "linear").split(",");
|
|
32
|
+
const offset = breakOffset(entry.getAttribute("timeOffset"));
|
|
33
|
+
if (!types.includes("linear") || offset === null) continue;
|
|
34
|
+
const source = Array.from(entry.children).find((e) => e.localName === "AdSource");
|
|
35
|
+
const tag = source === void 0 ? void 0 : Array.from(source.children).find((e) => e.localName === "AdTagURI");
|
|
36
|
+
const data = source === void 0 ? void 0 : Array.from(source.children).find((e) => e.localName === "VASTAdData");
|
|
37
|
+
const vastElement = data === void 0 ? void 0 : Array.from(data.children).find((e) => e.localName === "VAST");
|
|
38
|
+
const url = tag?.textContent?.trim();
|
|
39
|
+
let vast = null;
|
|
40
|
+
if (url !== void 0 && url !== "") vast = { url: new URL(url, base).href };
|
|
41
|
+
else if (vastElement !== void 0) vast = { xml: new XMLSerializer().serializeToString(vastElement) };
|
|
42
|
+
if (vast === null) continue;
|
|
43
|
+
const events = Array.from(entry.children).find((e) => e.localName === "TrackingEvents");
|
|
44
|
+
const tracking = (events === void 0 ? [] : Array.from(events.children)).flatMap((e) => {
|
|
45
|
+
const event = e.getAttribute("event");
|
|
46
|
+
const target = e.textContent?.trim();
|
|
47
|
+
return event === null || target === void 0 || target === "" ? [] : [{
|
|
48
|
+
event,
|
|
49
|
+
url: target
|
|
50
|
+
}];
|
|
51
|
+
});
|
|
52
|
+
out.push({
|
|
53
|
+
id: entry.getAttribute("breakId") ?? `break-${index}`,
|
|
54
|
+
offset,
|
|
55
|
+
vast,
|
|
56
|
+
tracking
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
/** The programme time of an offset, or null when it needs what is not known yet. */
|
|
62
|
+
function offsetTime(offset, duration, chapters = []) {
|
|
63
|
+
if (offset === "start") return 0;
|
|
64
|
+
if (offset === "end") return Number.isFinite(duration) ? duration : null;
|
|
65
|
+
if ("time" in offset) return offset.time;
|
|
66
|
+
if ("percent" in offset) return Number.isFinite(duration) ? duration * offset.percent / 100 : null;
|
|
67
|
+
return chapters[offset.position - 1] ?? null;
|
|
68
|
+
}
|
|
69
|
+
export { breakOffset, offsetTime, parseVmap };
|
|
70
|
+
|
|
71
|
+
//# sourceMappingURL=vmap.js.map
|
package/dist/vmap.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vmap.js","names":[],"sources":["../src/vmap.ts"],"sourcesContent":["/**\n * VMAP 1.0.1, the IAB's Video Multiple Ad Playlist: when the breaks of a\n * programme go, and which VAST each one asks for. Only linear breaks are\n * read; a nonlinear or display break is left out.\n */\nimport { clock } from './vast.js';\n\n/** Where a break goes: before, after, at a time, at a share of the duration, or at a chapter position. */\nexport type BreakOffset =\n | 'start'\n | 'end'\n | { readonly time: number }\n | { readonly percent: number }\n | { readonly position: number };\n\n/** A break of the schedule, before its VAST is fetched. */\nexport interface ScheduledBreak {\n readonly id: string;\n readonly offset: BreakOffset;\n /** The VAST URL, or the VAST document the VMAP carries. */\n readonly vast: { readonly url: string } | { readonly xml: string };\n /** The VMAP's own beacons: `breakStart`, `breakEnd`, `error`. */\n readonly tracking: readonly { readonly event: string; readonly url: string }[];\n}\n\n/** The offset of `timeOffset` (VMAP 1.0.1 §2.4). */\nexport function breakOffset(value: string | null): BreakOffset | null {\n const trimmed = value?.trim() ?? '';\n if (trimmed === 'start' || trimmed === 'end') return trimmed;\n if (trimmed.endsWith('%')) {\n const percent = Number(trimmed.slice(0, -1));\n return Number.isFinite(percent) ? { percent } : null;\n }\n if (trimmed.startsWith('#')) {\n const position = Number(trimmed.slice(1));\n return Number.isInteger(position) ? { position } : null;\n }\n const time = clock(trimmed);\n return time === undefined ? null : { time };\n}\n\n/** The breaks of a VMAP document, in document order, or null when the text is not VMAP. */\nexport function parseVmap(xml: string, base: string): ScheduledBreak[] | null {\n const doc = new DOMParser().parseFromString(xml, 'application/xml');\n const root = doc.documentElement;\n if (root === null || root.localName !== 'VMAP') return null;\n const out: ScheduledBreak[] = [];\n let index = 0;\n for (const entry of Array.from(root.children).filter((e) => e.localName === 'AdBreak')) {\n index += 1;\n const types = (entry.getAttribute('breakType') ?? 'linear').split(',');\n const offset = breakOffset(entry.getAttribute('timeOffset'));\n if (!types.includes('linear') || offset === null) continue;\n const source = Array.from(entry.children).find((e) => e.localName === 'AdSource');\n const tag =\n source === undefined\n ? undefined\n : Array.from(source.children).find((e) => e.localName === 'AdTagURI');\n const data =\n source === undefined\n ? undefined\n : Array.from(source.children).find((e) => e.localName === 'VASTAdData');\n const vastElement =\n data === undefined\n ? undefined\n : Array.from(data.children).find((e) => e.localName === 'VAST');\n const url = tag?.textContent?.trim();\n let vast: ScheduledBreak['vast'] | null = null;\n if (url !== undefined && url !== '') vast = { url: new URL(url, base).href };\n else if (vastElement !== undefined)\n vast = { xml: new XMLSerializer().serializeToString(vastElement) };\n if (vast === null) continue;\n const events = Array.from(entry.children).find((e) => e.localName === 'TrackingEvents');\n const tracking = (events === undefined ? [] : Array.from(events.children)).flatMap((e) => {\n const event = e.getAttribute('event');\n const target = e.textContent?.trim();\n return event === null || target === undefined || target === ''\n ? []\n : [{ event, url: target }];\n });\n out.push({ id: entry.getAttribute('breakId') ?? `break-${index}`, offset, vast, tracking });\n }\n return out;\n}\n\n/** The programme time of an offset, or null when it needs what is not known yet. */\nexport function offsetTime(\n offset: BreakOffset,\n duration: number,\n chapters: readonly number[] = [],\n): number | null {\n if (offset === 'start') return 0;\n if (offset === 'end') return Number.isFinite(duration) ? duration : null;\n if ('time' in offset) return offset.time;\n if ('percent' in offset)\n return Number.isFinite(duration) ? (duration * offset.percent) / 100 : null;\n return chapters[offset.position - 1] ?? null;\n}\n"],"mappings":";;;;;;;;AA0BA,SAAgB,YAAY,OAA0C;CACpE,MAAM,UAAU,OAAO,KAAK,KAAK;CACjC,IAAI,YAAY,WAAW,YAAY,OAAO,OAAO;CACrD,IAAI,QAAQ,SAAS,GAAG,GAAG;EACzB,MAAM,UAAU,OAAO,QAAQ,MAAM,GAAG,EAAE,CAAC;EAC3C,OAAO,OAAO,SAAS,OAAO,IAAI,EAAE,QAAQ,IAAI;CAClD;CACA,IAAI,QAAQ,WAAW,GAAG,GAAG;EAC3B,MAAM,WAAW,OAAO,QAAQ,MAAM,CAAC,CAAC;EACxC,OAAO,OAAO,UAAU,QAAQ,IAAI,EAAE,SAAS,IAAI;CACrD;CACA,MAAM,OAAO,MAAM,OAAO;CAC1B,OAAO,SAAS,KAAA,IAAY,OAAO,EAAE,KAAK;AAC5C;;AAGA,SAAgB,UAAU,KAAa,MAAuC;CAE5E,MAAM,OADM,IAAI,UAAU,CAAC,CAAC,gBAAgB,KAAK,iBAClC,CAAC,CAAC;CACjB,IAAI,SAAS,QAAQ,KAAK,cAAc,QAAQ,OAAO;CACvD,MAAM,MAAwB,CAAC;CAC/B,IAAI,QAAQ;CACZ,KAAK,MAAM,SAAS,MAAM,KAAK,KAAK,QAAQ,CAAC,CAAC,QAAQ,MAAM,EAAE,cAAc,SAAS,GAAG;EACtF,SAAS;EACT,MAAM,SAAS,MAAM,aAAa,WAAW,KAAK,SAAA,CAAU,MAAM,GAAG;EACrE,MAAM,SAAS,YAAY,MAAM,aAAa,YAAY,CAAC;EAC3D,IAAI,CAAC,MAAM,SAAS,QAAQ,KAAK,WAAW,MAAM;EAClD,MAAM,SAAS,MAAM,KAAK,MAAM,QAAQ,CAAC,CAAC,MAAM,MAAM,EAAE,cAAc,UAAU;EAChF,MAAM,MACJ,WAAW,KAAA,IACP,KAAA,IACA,MAAM,KAAK,O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IAAI,aAAa,QACf,OAAO,OAAO,SAAS,QAAQ,IAAK,WAAW,OAAO,UAAW,MAAM;CACzE,OAAO,SAAS,OAAO,WAAW,MAAM;AAC1C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mattebox/player-ads",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Ads in <mattebox-player>: HLS interstitials, DASH alternative MPD, SCTE-35 cue-outs, VAST 4 and VMAP, with the ad controls and the beacons.",
|
|
5
|
+
"author": "Josep Boix Requesens",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/jboix/mattebox-player.git",
|
|
9
|
+
"directory": "packages/ads"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/jboix/mattebox-player#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/jboix/mattebox-player/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"mattebox",
|
|
17
|
+
"player",
|
|
18
|
+
"custom-element",
|
|
19
|
+
"ads",
|
|
20
|
+
"vast",
|
|
21
|
+
"vmap",
|
|
22
|
+
"interstitials",
|
|
23
|
+
"scte-35"
|
|
24
|
+
],
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"type": "module",
|
|
27
|
+
"sideEffects": true,
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=20.19"
|
|
30
|
+
},
|
|
31
|
+
"main": "./dist/es2015/index.js",
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"modern": "./dist/index.js",
|
|
37
|
+
"default": "./dist/es2015/index.js"
|
|
38
|
+
},
|
|
39
|
+
"./locales/*": {
|
|
40
|
+
"types": "./dist/locales/*.d.ts",
|
|
41
|
+
"modern": "./dist/locales/*.js",
|
|
42
|
+
"default": "./dist/es2015/locales/*.js"
|
|
43
|
+
},
|
|
44
|
+
"./package.json": "./package.json"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"dist"
|
|
48
|
+
],
|
|
49
|
+
"scripts": {
|
|
50
|
+
"clean": "rm -rf dist",
|
|
51
|
+
"build": "npm run clean && npm run build:modern && npm run build:es2015 && npm run build:cdn",
|
|
52
|
+
"build:modern": "tsc -p tsconfig.build.json --emitDeclarationOnly && rolldown -c rolldown.modern.config.mjs",
|
|
53
|
+
"build:es2015": "rolldown -c rolldown.dist.config.mjs",
|
|
54
|
+
"build:cdn": "rolldown -c rolldown.cdn.config.mjs",
|
|
55
|
+
"check:package": "publint && attw --pack . --format table --no-emoji",
|
|
56
|
+
"release:ci": "semantic-release"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"@mattebox/player": ">=0.13.0",
|
|
60
|
+
"mattebox": "^0.17.0"
|
|
61
|
+
}
|
|
62
|
+
}
|