@streamlayer/feature-advertisement 1.1.0 → 1.1.1-rc.1343
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/classic/package.json +5 -0
- package/lib/advertisement.d.ts +6 -6
- package/lib/advertisement.js +8 -8
- package/lib/cjs/advertisement.d.ts +64 -0
- package/lib/cjs/advertisement.js +307 -0
- package/lib/cjs/index.d.ts +19 -0
- package/lib/cjs/index.js +39 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/units/external-ad.d.ts +58 -0
- package/lib/cjs/units/external-ad.js +390 -0
- package/lib/cjs/units/external-pause-ad.d.ts +50 -0
- package/lib/cjs/units/external-pause-ad.js +253 -0
- package/lib/cjs/units/gam.d.ts +16 -0
- package/lib/cjs/units/gam.js +104 -0
- package/lib/cjs/units/parse-vast-extensions.d.ts +17 -0
- package/lib/cjs/units/parse-vast-extensions.js +52 -0
- package/lib/cjs/units/pause-ad-card.d.ts +49 -0
- package/lib/cjs/units/pause-ad-card.js +136 -0
- package/lib/cjs/units/pause-ad.d.ts +16 -0
- package/lib/cjs/units/pause-ad.js +52 -0
- package/lib/cjs/units/ssai-ad-break.d.ts +21 -0
- package/lib/cjs/units/ssai-ad-break.js +11 -0
- package/lib/cjs/units/ssai-creative.d.ts +89 -0
- package/lib/cjs/units/ssai-creative.js +136 -0
- package/lib/cjs/units/ssai-detector.d.ts +180 -0
- package/lib/cjs/units/ssai-detector.js +1156 -0
- package/lib/cjs/units/ssai-preload.d.ts +20 -0
- package/lib/cjs/units/ssai-preload.js +62 -0
- package/lib/cjs/units/ssai-providers.d.ts +117 -0
- package/lib/cjs/units/ssai-providers.js +263 -0
- package/lib/cjs/units/vast-tracker.d.ts +45 -0
- package/lib/cjs/units/vast-tracker.js +64 -0
- package/lib/classic/_virtual/_rollupPluginBabelHelpers.js +71 -0
- package/lib/classic/advertisement.js +331 -0
- package/lib/classic/cjs/_virtual/_rollupPluginBabelHelpers.js +77 -0
- package/lib/classic/cjs/advertisement.js +333 -0
- package/lib/classic/cjs/index.js +47 -0
- package/lib/classic/cjs/package.json +1 -0
- package/lib/classic/cjs/units/external-ad.js +453 -0
- package/lib/classic/cjs/units/external-pause-ad.js +274 -0
- package/lib/classic/cjs/units/gam.js +110 -0
- package/lib/classic/cjs/units/parse-vast-extensions.js +56 -0
- package/lib/classic/cjs/units/pause-ad-card.js +134 -0
- package/lib/classic/cjs/units/pause-ad.js +72 -0
- package/lib/classic/cjs/units/ssai-ad-break.js +11 -0
- package/lib/classic/cjs/units/ssai-creative.js +142 -0
- package/lib/classic/cjs/units/ssai-detector.js +1170 -0
- package/lib/classic/cjs/units/ssai-preload.js +72 -0
- package/lib/classic/cjs/units/ssai-providers.js +299 -0
- package/lib/classic/cjs/units/vast-tracker.js +81 -0
- package/lib/classic/index.js +28 -0
- package/lib/classic/units/external-ad.js +447 -0
- package/lib/classic/units/external-pause-ad.js +271 -0
- package/lib/classic/units/gam.js +108 -0
- package/lib/classic/units/parse-vast-extensions.js +54 -0
- package/lib/classic/units/pause-ad-card.js +131 -0
- package/lib/classic/units/pause-ad.js +70 -0
- package/lib/classic/units/ssai-ad-break.js +9 -0
- package/lib/classic/units/ssai-creative.js +140 -0
- package/lib/classic/units/ssai-detector.js +1167 -0
- package/lib/classic/units/ssai-preload.js +68 -0
- package/lib/classic/units/ssai-providers.js +289 -0
- package/lib/classic/units/vast-tracker.js +77 -0
- package/lib/index.d.ts +13 -13
- package/lib/index.js +8 -8
- package/lib/units/external-ad.d.ts +2 -2
- package/lib/units/external-ad.js +2 -2
- package/lib/units/pause-ad.d.ts +1 -1
- package/lib/units/pause-ad.js +1 -1
- package/lib/units/ssai-ad-break.d.ts +1 -1
- package/lib/units/ssai-creative.d.ts +1 -1
- package/lib/units/ssai-creative.js +1 -1
- package/lib/units/ssai-detector.d.ts +1 -1
- package/lib/units/ssai-preload.d.ts +1 -1
- package/lib/units/ssai-providers.d.ts +1 -1
- package/lib/units/vast-tracker.d.ts +1 -1
- package/package.json +41 -8
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var sdkWebLogger = require('@streamlayer/sdk-web-logger');
|
|
4
|
+
var vastClient = require('@dailymotion/vast-client');
|
|
5
|
+
require('@streamlayer/sdk-web-api');
|
|
6
|
+
var VMAP = require('vmap');
|
|
7
|
+
var gam = require('./gam.js');
|
|
8
|
+
var parseVastExtensions = require('./parse-vast-extensions.js');
|
|
9
|
+
|
|
10
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
|
+
|
|
12
|
+
var VMAP__default = /*#__PURE__*/_interopDefault(VMAP);
|
|
13
|
+
|
|
14
|
+
// Pins the `@dailymotion/vast-client` → `@types/vast-client` shim: TypeDoc
|
|
15
|
+
// compiles this file through dependent packages' programs, whose tsconfig
|
|
16
|
+
// `include` does not cover this package's ambient declarations.
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
|
|
18
|
+
/// <reference path="../module.d.ts" />
|
|
19
|
+
const log = sdkWebLogger.createLogger('ui:gam-external-ad');
|
|
20
|
+
/** Default budget for a single VAST resolution before it is treated as "no ad". */
|
|
21
|
+
const VAST_FETCH_TIMEOUT_MS = 10_000;
|
|
22
|
+
/**
|
|
23
|
+
* Resolve `promise`, but fall back to `undefined` if it doesn't settle within `ms`.
|
|
24
|
+
* A slow/unreachable VAST endpoint must degrade to "no ad" instead of leaving the
|
|
25
|
+
* fetcher (and the sidebar) stuck waiting. Uses a timer rather than `AbortController`
|
|
26
|
+
* so it stays safe on the ES5/Chrome-32 target.
|
|
27
|
+
*/
|
|
28
|
+
const withTimeout = (promise, ms) => {
|
|
29
|
+
return Promise.race([
|
|
30
|
+
promise,
|
|
31
|
+
new Promise((resolve) => {
|
|
32
|
+
setTimeout(() => resolve(undefined), ms);
|
|
33
|
+
}),
|
|
34
|
+
]);
|
|
35
|
+
};
|
|
36
|
+
// const preloadImage = (src: string) => {
|
|
37
|
+
// return new Promise((resolve, reject) => {
|
|
38
|
+
// const image = new Image()
|
|
39
|
+
// image.onload = () => resolve(src)
|
|
40
|
+
// image.onerror = () => reject()
|
|
41
|
+
// image.src = src
|
|
42
|
+
// })
|
|
43
|
+
// }
|
|
44
|
+
/**
|
|
45
|
+
* Get the first valid creative from an ad
|
|
46
|
+
*/
|
|
47
|
+
const getCreative = (ad) => {
|
|
48
|
+
return ad.creatives.find((creative) => creative.mediaFiles);
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Get the URL of the first media file in the creative
|
|
52
|
+
*/
|
|
53
|
+
const getMediaFileUrl = (creative) => {
|
|
54
|
+
const mediaFiles = creative?.mediaFiles;
|
|
55
|
+
if (!mediaFiles?.length) {
|
|
56
|
+
return '';
|
|
57
|
+
}
|
|
58
|
+
return mediaFiles.find((mediaFile) => mediaFile.fileURL)?.fileURL || '';
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Extract the linear creative's `<ClickThrough>` URL. Sources the sidebar CTA (QR code
|
|
62
|
+
* + click-through). `videoClickThroughURLTemplate` isn't strongly typed in the client lib.
|
|
63
|
+
*/
|
|
64
|
+
const getClickThrough = (creative) => {
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
66
|
+
// @ts-ignore — vast-client types `videoClickThroughURLTemplate` loosely
|
|
67
|
+
return creative?.videoClickThroughURLTemplate?.url || undefined;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Extract every `<Companion>` variation of the first `<CompanionAds>` creative that carries a
|
|
71
|
+
* `<StaticResource>`, in document order. `width`/`height` are XML attributes (strings at
|
|
72
|
+
* runtime), normalised to numbers so the size mapping can compare them.
|
|
73
|
+
*/
|
|
74
|
+
const getCompanions = (ad) => {
|
|
75
|
+
const creative = ad.creatives.find((item) => item.type === 'companion');
|
|
76
|
+
return (creative?.variations || []).reduce((acc, variation) => {
|
|
77
|
+
const imageUrl = variation?.staticResources?.[0]?.url;
|
|
78
|
+
if (!imageUrl) {
|
|
79
|
+
return acc;
|
|
80
|
+
}
|
|
81
|
+
return acc.concat({
|
|
82
|
+
imageUrl,
|
|
83
|
+
clickUrl: variation.companionClickThroughURLTemplate ?? undefined,
|
|
84
|
+
width: Number(variation.width) || undefined,
|
|
85
|
+
height: Number(variation.height) || undefined,
|
|
86
|
+
creative,
|
|
87
|
+
variation,
|
|
88
|
+
});
|
|
89
|
+
}, []);
|
|
90
|
+
};
|
|
91
|
+
/** Product companion mapping: the 300x75 companion is the logo, the banner-sized one the media. */
|
|
92
|
+
const LOGO_COMPANION_WIDTH = 300;
|
|
93
|
+
const LOGO_COMPANION_HEIGHT = 75;
|
|
94
|
+
const companionArea = (companion) => (companion.width || 0) * (companion.height || 0);
|
|
95
|
+
/**
|
|
96
|
+
* Split a `<CompanionAds>` creative's variations into the logo and media slots.
|
|
97
|
+
*
|
|
98
|
+
* The pair mapping activates only for a tag that ships an exact 300x75 companion next to at
|
|
99
|
+
* least one other. Deliberately conservative pending broader product sizes: on any other tag
|
|
100
|
+
* no companion is promoted to the logo slot and the first one stays the media/strip candidate.
|
|
101
|
+
*/
|
|
102
|
+
const selectCompanions = (companions) => {
|
|
103
|
+
const logo = companions.length > 1
|
|
104
|
+
? companions.find((companion) => companion.width === LOGO_COMPANION_WIDTH && companion.height === LOGO_COMPANION_HEIGHT)
|
|
105
|
+
: undefined;
|
|
106
|
+
if (!logo) {
|
|
107
|
+
return { logo: undefined, media: companions[0] };
|
|
108
|
+
}
|
|
109
|
+
const media = companions
|
|
110
|
+
.filter((companion) => companion !== logo)
|
|
111
|
+
.sort((a, b) => companionArea(a) - companionArea(b))
|
|
112
|
+
.pop();
|
|
113
|
+
return { logo, media };
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Extract the first `<NonLinearAds>` creative's static-image variation. Both the creative (which
|
|
117
|
+
* owns the `<TrackingEvents>`) and the variation (which owns the click-through) are kept —
|
|
118
|
+
* `VASTTracker` needs both.
|
|
119
|
+
*
|
|
120
|
+
* `@dailymotion/vast-client` 6.4.2 emits `nonlinearClickThroughURLTemplate` (lower-case `l`),
|
|
121
|
+
* which the `@types/vast-client` shim declares as `nonLinearClickThroughURLTemplate` — the
|
|
122
|
+
* runtime spelling is authoritative.
|
|
123
|
+
*/
|
|
124
|
+
const getNonLinear = (ad) => {
|
|
125
|
+
const creative = ad.creatives.find((item) => item.type === 'nonlinear');
|
|
126
|
+
const variation = creative?.variations?.[0];
|
|
127
|
+
const imageUrl = variation?.staticResource;
|
|
128
|
+
if (!creative || !variation || !imageUrl) {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
imageUrl,
|
|
133
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
134
|
+
// @ts-ignore — see the runtime-vs-shim spelling note above
|
|
135
|
+
clickUrl: variation.nonlinearClickThroughURLTemplate ?? undefined,
|
|
136
|
+
width: Number(variation.width) || undefined,
|
|
137
|
+
height: Number(variation.height) || undefined,
|
|
138
|
+
creative,
|
|
139
|
+
variation,
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Map a parsed VAST response into our `AdData` shape. Shared by both acquisition
|
|
144
|
+
* paths — the GAM/VMAP tag flow and the Prebid auction flow — so the two always
|
|
145
|
+
* produce identical downstream data. Returns `undefined` when the response has no
|
|
146
|
+
* ad carrying creatives.
|
|
147
|
+
*/
|
|
148
|
+
const buildAdData = (parsedVAST, order) => {
|
|
149
|
+
const validAd = parsedVAST.ads.find((ads) => ads.creatives.length > 0);
|
|
150
|
+
if (!validAd) {
|
|
151
|
+
log.warn({ order, ads: parsedVAST.ads.length }, 'vast response carries no ad with creatives — treated as no-ad');
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
const extension = validAd.extensions.find((ext) => ext.attributes.type.startsWith('streamlayer'))?.children[0];
|
|
155
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
156
|
+
// @ts-expect-error
|
|
157
|
+
const parsedExtension = parseVastExtensions.parseVastExtension(extension);
|
|
158
|
+
const creative = getCreative(validAd);
|
|
159
|
+
const mediaFileUrl = getMediaFileUrl(creative);
|
|
160
|
+
const companions = getCompanions(validAd);
|
|
161
|
+
const { logo, media: mediaCompanion } = selectCompanions(companions);
|
|
162
|
+
const nonLinear = getNonLinear(validAd);
|
|
163
|
+
const clickThrough = getClickThrough(creative) || nonLinear?.clickUrl || mediaCompanion?.clickUrl;
|
|
164
|
+
log.info({
|
|
165
|
+
order,
|
|
166
|
+
adId: validAd.id,
|
|
167
|
+
title: validAd.title || undefined,
|
|
168
|
+
found: {
|
|
169
|
+
linear: !!creative,
|
|
170
|
+
mediaFile: mediaFileUrl || undefined,
|
|
171
|
+
nonLinear: nonLinear ? `${nonLinear.width}x${nonLinear.height}` : undefined,
|
|
172
|
+
companions: companions.map((item) => `${item.width}x${item.height}`),
|
|
173
|
+
},
|
|
174
|
+
selected: {
|
|
175
|
+
logo: logo ? { size: `${logo.width}x${logo.height}`, url: logo.imageUrl } : undefined,
|
|
176
|
+
media: mediaCompanion
|
|
177
|
+
? { size: `${mediaCompanion.width}x${mediaCompanion.height}`, url: mediaCompanion.imageUrl }
|
|
178
|
+
: undefined,
|
|
179
|
+
clickThrough: !!clickThrough,
|
|
180
|
+
},
|
|
181
|
+
}, 'vast ad parsed');
|
|
182
|
+
return {
|
|
183
|
+
creative,
|
|
184
|
+
order,
|
|
185
|
+
mediaFileUrl,
|
|
186
|
+
companion: companions[0],
|
|
187
|
+
logo,
|
|
188
|
+
mediaCompanion,
|
|
189
|
+
nonLinear,
|
|
190
|
+
clickThrough,
|
|
191
|
+
title: validAd.title || undefined,
|
|
192
|
+
description: validAd.description || undefined,
|
|
193
|
+
ad: validAd,
|
|
194
|
+
extension: parsedExtension,
|
|
195
|
+
raw: parsedVAST,
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
/** Path of the self-hosted Prebid Server OpenRTB auction endpoint. */
|
|
199
|
+
const PREBID_AUCTION_PATH = '/openrtb2/auction';
|
|
200
|
+
/**
|
|
201
|
+
* A Prebid link points at our Prebid Server auction endpoint (staging:
|
|
202
|
+
* `https://prebid.next.streamlayer.io/openrtb2/auction`) rather than at a VAST tag.
|
|
203
|
+
* Detected by path so both staging and production hosts match.
|
|
204
|
+
*/
|
|
205
|
+
const isPrebidUrl = (url) => url.pathname.endsWith(PREBID_AUCTION_PATH);
|
|
206
|
+
/** Unique per-auction request id — Prebid responses are never cached/reused (RFC §5). */
|
|
207
|
+
const createRequestId = () => `sl-web-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
|
|
208
|
+
/**
|
|
209
|
+
* Build the OpenRTB request body for a single video auction. Placement config is
|
|
210
|
+
* carried on the Prebid link's query string so the consumer-facing interface stays a
|
|
211
|
+
* single URL string:
|
|
212
|
+
* - `configId` → `imp.ext.prebid.storedrequest.id` (production stored request).
|
|
213
|
+
* - `storedauctionresponse` → `imp.ext.prebid.storedauctionresponse.id` (staging mock).
|
|
214
|
+
* - `test=1` → top-level `test` flag.
|
|
215
|
+
* - `w` / `h` → video dimensions (default 1920×1080).
|
|
216
|
+
*/
|
|
217
|
+
const buildAuctionRequest = (url) => {
|
|
218
|
+
const params = url.searchParams;
|
|
219
|
+
const width = Number(params.get('w')) || 1920;
|
|
220
|
+
const height = Number(params.get('h')) || 1080;
|
|
221
|
+
const configId = params.get('configId') ?? params.get('config_id') ?? undefined;
|
|
222
|
+
const storedAuctionResponse = params.get('storedauctionresponse') ?? undefined;
|
|
223
|
+
const prebid = {};
|
|
224
|
+
if (storedAuctionResponse) {
|
|
225
|
+
prebid.storedauctionresponse = { id: storedAuctionResponse };
|
|
226
|
+
}
|
|
227
|
+
if (configId) {
|
|
228
|
+
prebid.storedrequest = { id: configId };
|
|
229
|
+
}
|
|
230
|
+
prebid.bidder = {
|
|
231
|
+
appnexus: { placementId: 13144370 },
|
|
232
|
+
};
|
|
233
|
+
const request = {
|
|
234
|
+
id: createRequestId(),
|
|
235
|
+
imp: [
|
|
236
|
+
{
|
|
237
|
+
id: '1',
|
|
238
|
+
video: {
|
|
239
|
+
mimes: ['video/mp4'],
|
|
240
|
+
protocols: [2, 3, 5, 6],
|
|
241
|
+
w: width,
|
|
242
|
+
h: height,
|
|
243
|
+
placement: 1,
|
|
244
|
+
linearity: 1,
|
|
245
|
+
},
|
|
246
|
+
ext: { prebid },
|
|
247
|
+
},
|
|
248
|
+
],
|
|
249
|
+
site: { page: typeof location !== 'undefined' ? location.href : url.origin },
|
|
250
|
+
tmax: 500,
|
|
251
|
+
};
|
|
252
|
+
if (params.get('test') === '1') {
|
|
253
|
+
request.test = 1;
|
|
254
|
+
}
|
|
255
|
+
return request;
|
|
256
|
+
};
|
|
257
|
+
/**
|
|
258
|
+
* Run one Prebid Server auction and turn the winning inline VAST into an `AdStore`.
|
|
259
|
+
* Every bid's `adm` is inline VAST XML (no URL to fetch); bids are ordered by price
|
|
260
|
+
* so the winner plays first (RFC §2). A single bad bid must not drop the batch.
|
|
261
|
+
*/
|
|
262
|
+
const fetchPrebidAds = async (prebidUrl) => {
|
|
263
|
+
const response = await fetch(prebidUrl.toString(), {
|
|
264
|
+
method: 'POST',
|
|
265
|
+
headers: { 'Content-Type': 'application/json' },
|
|
266
|
+
body: JSON.stringify(buildAuctionRequest(prebidUrl)),
|
|
267
|
+
});
|
|
268
|
+
if (!response.ok) {
|
|
269
|
+
throw new Error(`prebid auction failed with status ${response.status}`);
|
|
270
|
+
}
|
|
271
|
+
const auction = (await response.json());
|
|
272
|
+
const bids = (auction.seatbid ?? [])
|
|
273
|
+
.reduce((acc, seat) => acc.concat(seat.bid ?? []), [])
|
|
274
|
+
.filter((bid) => !!bid.adm)
|
|
275
|
+
// Highest price wins — the winner is order 0 (RFC §2).
|
|
276
|
+
.sort((a, b) => (b.price ?? 0) - (a.price ?? 0));
|
|
277
|
+
const parser = new DOMParser();
|
|
278
|
+
const loadedAds = await Promise.all(bids.map((bid, i) => {
|
|
279
|
+
const xmlDoc = parser.parseFromString(bid.adm, 'text/xml');
|
|
280
|
+
// A fresh `VASTParser` per bid isolates parser state, mirroring the fresh
|
|
281
|
+
// `VASTClient` per link in the tag flow. `resolveAll: true` follows any
|
|
282
|
+
// `VASTAdTagURI` wrapper chain the bid's inline VAST may still contain.
|
|
283
|
+
return (new vastClient.VASTParser()
|
|
284
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
285
|
+
// @ts-ignore
|
|
286
|
+
.parseVAST(xmlDoc, { resolveAll: true, wrapperLimit: 10, allowMultipleAds: true })
|
|
287
|
+
.then((parsedVAST) => buildAdData(parsedVAST, i))
|
|
288
|
+
.catch((err) => {
|
|
289
|
+
// A single failing bid must not drop the whole batch.
|
|
290
|
+
log.error(err, 'failed to parse prebid VAST');
|
|
291
|
+
return undefined;
|
|
292
|
+
}));
|
|
293
|
+
}));
|
|
294
|
+
const params = prebidUrl.searchParams;
|
|
295
|
+
const autoplay = params.get('vpa')?.replaceAll('?', '') !== 'click';
|
|
296
|
+
const muted = params.get('vpmute')?.replaceAll('?', '') === '1';
|
|
297
|
+
return {
|
|
298
|
+
ads: loadedAds.filter((d) => !!d),
|
|
299
|
+
autoplay,
|
|
300
|
+
muted,
|
|
301
|
+
};
|
|
302
|
+
};
|
|
303
|
+
const externalAd = (instance) => {
|
|
304
|
+
return (gamUrl) => instance.transport.nanoquery.createFetcherStore([gamUrl], {
|
|
305
|
+
dedupeTime: Infinity,
|
|
306
|
+
revalidateInterval: 0,
|
|
307
|
+
fetcher: async (adUrl) => {
|
|
308
|
+
if (!adUrl || typeof adUrl !== 'string') {
|
|
309
|
+
throw new Error('ad url is empty');
|
|
310
|
+
}
|
|
311
|
+
const parsedAdUrl = (() => {
|
|
312
|
+
try {
|
|
313
|
+
return new URL(adUrl);
|
|
314
|
+
}
|
|
315
|
+
catch {
|
|
316
|
+
return undefined;
|
|
317
|
+
}
|
|
318
|
+
})();
|
|
319
|
+
// Prebid links hit our Prebid Server auction endpoint instead of a VAST tag.
|
|
320
|
+
// Run the OpenRTB auction and parse the winning bid's inline VAST (`adm`) into
|
|
321
|
+
// the same `AdStore` shape the GAM/VMAP path returns. See rfcs/prebid.
|
|
322
|
+
if (parsedAdUrl && isPrebidUrl(parsedAdUrl)) {
|
|
323
|
+
return fetchPrebidAds(parsedAdUrl);
|
|
324
|
+
}
|
|
325
|
+
const generateUrl = gam.gam(adUrl);
|
|
326
|
+
const generatedUrl = await generateUrl();
|
|
327
|
+
const noncedLink = generatedUrl?.url;
|
|
328
|
+
if (!noncedLink) {
|
|
329
|
+
throw new Error('ad url is not generated');
|
|
330
|
+
}
|
|
331
|
+
let links;
|
|
332
|
+
if (noncedLink.searchParams.get('output')?.replaceAll('?', '') === 'vmap') {
|
|
333
|
+
const response = await withTimeout(fetch(noncedLink.toString()), VAST_FETCH_TIMEOUT_MS);
|
|
334
|
+
if (!response) {
|
|
335
|
+
throw new Error('vmap fetch timed out');
|
|
336
|
+
}
|
|
337
|
+
const parser = new DOMParser();
|
|
338
|
+
const xmlStr = await response.text();
|
|
339
|
+
const xmlDoc = parser.parseFromString(xmlStr, 'text/xml');
|
|
340
|
+
const vmap = new VMAP__default.default(xmlDoc);
|
|
341
|
+
links = vmap.adBreaks.map((ad) => {
|
|
342
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
343
|
+
// @ts-expect-error
|
|
344
|
+
return ad.adSource.adTagURI.uri;
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
links = [noncedLink.toString()];
|
|
349
|
+
}
|
|
350
|
+
const loadedAds = await Promise.all(links.map((link, i) => {
|
|
351
|
+
// `resolveAll: true` makes the client follow every `VASTAdTagURI` wrapper chain
|
|
352
|
+
// (up to the default depth of 10) and resolve all Ads, merging wrapper-level
|
|
353
|
+
// Impression/tracking URLs into the inline Ad. The default (`resolveAll: false`)
|
|
354
|
+
// only resolves the first standalone Ad and defers wrappers/remaining Ads to
|
|
355
|
+
// `getNextAds()`, which this fetcher never calls. A fresh `VASTClient` per link
|
|
356
|
+
// isolates parser state so concurrent (VMAP) resolutions don't clobber each other.
|
|
357
|
+
return withTimeout(new vastClient.VASTClient()
|
|
358
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
359
|
+
// @ts-ignore
|
|
360
|
+
.get(link, { resolveAll: true, wrapperLimit: 10, allowMultipleAds: true }), VAST_FETCH_TIMEOUT_MS)
|
|
361
|
+
.then((parsedVAST) => {
|
|
362
|
+
// `parsedVAST` is `undefined` when the resolution timed out (see `withTimeout`).
|
|
363
|
+
if (!parsedVAST) {
|
|
364
|
+
log.warn({ link, timeoutMs: VAST_FETCH_TIMEOUT_MS }, 'vast resolution timed out — treated as no-ad');
|
|
365
|
+
return undefined;
|
|
366
|
+
}
|
|
367
|
+
return buildAdData(parsedVAST, i);
|
|
368
|
+
})
|
|
369
|
+
.catch((err) => {
|
|
370
|
+
// A single failing wrapper chain must not drop the whole batch.
|
|
371
|
+
log.error(err, 'failed to resolve VAST');
|
|
372
|
+
return undefined;
|
|
373
|
+
});
|
|
374
|
+
}));
|
|
375
|
+
const autoplay = noncedLink.searchParams.get('vpa')?.replaceAll('?', '') !== 'click';
|
|
376
|
+
const muted = noncedLink.searchParams.get('vpmute')?.replaceAll('?', '') === '1';
|
|
377
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
378
|
+
// @ts-expect-error
|
|
379
|
+
const nonceManager = generatedUrl.manager;
|
|
380
|
+
return {
|
|
381
|
+
ads: loadedAds.filter((d) => !!d),
|
|
382
|
+
autoplay,
|
|
383
|
+
muted,
|
|
384
|
+
nonceManager,
|
|
385
|
+
};
|
|
386
|
+
},
|
|
387
|
+
});
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
exports.externalAd = externalAd;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createSingleStore, StreamLayerContext } from '@streamlayer/sdk-web-interfaces';
|
|
2
|
+
import '@streamlayer/sdk-web-api';
|
|
3
|
+
export type GAMStaticCore = {
|
|
4
|
+
id: string;
|
|
5
|
+
parentId: string;
|
|
6
|
+
url: string;
|
|
7
|
+
imageSrc?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Linear MP4 from the VAST response → renders full-bleed `<video>` (SC-322).
|
|
10
|
+
* Only produced when the host asked for it via one of the `fullbleed-video*`
|
|
11
|
+
* values of `ExternalPauseAdOptions.template`; a `'default'` ad can never carry
|
|
12
|
+
* this field.
|
|
13
|
+
*/
|
|
14
|
+
backgroundVideoSrc?: string;
|
|
15
|
+
adUrl?: string;
|
|
16
|
+
ad?: any;
|
|
17
|
+
creative?: any;
|
|
18
|
+
variation?: any;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Which VAST creative this request resolves, set by the host on
|
|
22
|
+
* `pauseAdExternalUrls[].template`:
|
|
23
|
+
* - `'default'` — static image (nonlinear → companion) → sidebar overlay.
|
|
24
|
+
* - `'fullbleed-video*'` — the **linear** creative's MP4 → full-screen video (SC-322).
|
|
25
|
+
*
|
|
26
|
+
* This is a resolution STRATEGY, not a renderer hint the fetcher may ignore. The
|
|
27
|
+
* `fullbleed-video` variants differ ONLY in the chrome the renderer draws around
|
|
28
|
+
* the video, so creative resolution treats them identically.
|
|
29
|
+
*/
|
|
30
|
+
export type PauseAdTemplate = 'default' | 'fullbleed-video' | 'fullbleed-video-center' | 'fullbleed-video-left';
|
|
31
|
+
/**
|
|
32
|
+
* `indexOf` rather than `Array.prototype.includes`: this package ships to Chrome 32
|
|
33
|
+
* TV webviews and must stay ES5-compatible without a polyfill.
|
|
34
|
+
*/
|
|
35
|
+
export declare const isFullbleedVideoTemplate: (template?: PauseAdTemplate) => boolean;
|
|
36
|
+
export type ExternalPauseAdOptions = {
|
|
37
|
+
refetchInterval: number;
|
|
38
|
+
prefetch: boolean;
|
|
39
|
+
$enabled: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Creative-resolution strategy. Defaults to `'default'`.
|
|
42
|
+
*
|
|
43
|
+
* NOT reactive — it is part of the store's IDENTITY (see the composite Map key
|
|
44
|
+
* in `Advertisement.getExternalPauseAd`), not of its state. `$enabled` is the
|
|
45
|
+
* only reactive option here; `refetchInterval`/`prefetch` are read once at
|
|
46
|
+
* store-creation time.
|
|
47
|
+
*/
|
|
48
|
+
template?: PauseAdTemplate;
|
|
49
|
+
};
|
|
50
|
+
export declare const externalPauseAd: (instance: StreamLayerContext) => (gamUrl: string, $enabled: ReturnType<typeof createSingleStore<boolean>>, options: ExternalPauseAdOptions) => import("@nanostores/query", { with: { "resolution-mode": "import" } }).FetcherStore<GAMStaticCore | undefined, any>;
|